-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwaypoint.cpp
162 lines (148 loc) · 4.02 KB
/
waypoint.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#include"ros/ros.h"
#include"sensor_msgs/NavSatFix.h"
#include<math.h>
#include<std_msgs/Float64.h>
#include<std_msgs/Float32MultiArray.h>
#include<std_msgs/Int8.h>
float r=6378,encodis=0,PI=3.1414;
float slat;
float slon;
float dlat,dlon,x1,x2,x3,x4,d,a,c,n,m;
float encoder;
float distance;
int8_t output;
float angle;
float angle_current;
int count=1;
int turn=0;
int flag_lon=0,flag_lat=0;
float lon[200],lat[200];
int num=0;
void chattercallback(const sensor_msgs::NavSatFix::ConstPtr& msg)
{
x1=slat*PI/180;
x2=dlat*PI/180;
x3=(dlat-slat)*PI/180;
x4=(dlon-slon)*PI/180;
a=(sin(x3/2)*sin(x3/2)) + (cos(x1)*(cos(x2)*sin(x4/2)*sin(x4/2)));
c=2*atan2(sqrt(a),sqrt(1-a));
d = r*c*1000;
m=sin(x4)*cos(x2);
n=(cos(x1)*sin(x2))-(sin(x1)*cos(x2)*cos(x4));
angle=(atan2(m,n));
angle = (180/PI)*angle;
angle = (int)(angle+360)%360;
//ros::param::get("/routing_machine/destination/latitude", global_name);
//ROS_INFO("destination is:[%f]", global_name);
}
void encodercallback(const std_msgs::Float64::ConstPtr& msg1)
{
encoder = msg1->data;
distance = ((37.68/320)/100)*encoder;
distance=distance-turn;
if (((angle_current)-angle)>10)
{
output = 2; //left
}
else if (((angle_current)-angle)<-10)
{
output = 3; //right
}
else
{
if (distance>d && count<num)
{
output =5; //stop
slon=dlon;
slat=dlat;
count++;
dlon=lon[count];
dlat=lat[count];
turn=turn+distance;
ROS_INFO("******************** CHANGE OF WAYPOINT **********************")
ROS_INFO("the start has been changed to :");
ROS_INFO("start latitude: [%f]",slat);
ROS_INFO("start longitude: [%f]",slon);
ROS_INFO("the destination has been changed to :");
ROS_INFO("destination latitude: [%f]",dlat);
ROS_INFO("destination longitude: [%f]",dlon);
}
if(distance<=d)
{
output =1; //forward
}
}
}
void compass(const std_msgs::Float64::ConstPtr& msg1)
{
angle_current = msg1->data;
angle_current=int(angle_current)%360;
ROS_INFO("start latitude: [%f]",slat);
ROS_INFO("start longitude: [%f]",slon);
ROS_INFO("destination latitude: [%f]",dlat);
ROS_INFO("destination longitude: [%f]",dlon);
ROS_INFO("current angle: [%f]", angle_current);
ROS_INFO("distance of encoder is:[%f]", distance);
ROS_INFO("distance to destination:[%f]", d);
ROS_INFO("angle is:[%f]", angle);
ROS_INFO("output is [%d]", output);
}
void latitudecallback(const std_msgs::Float32MultiArray::ConstPtr& msg)
{
if(flag_lat==0)
{
int i = 0;
// print all the remaining numbers
for(std::vector<float>::const_iterator it = msg->data.begin(); it != msg->data.end(); ++it)
{
lat[i] = *it;
i++;
}
slat=lat[0];
dlat=lat[1];
ROS_INFO("slat [%f]", slat);
ROS_INFO("dlat [%f]",dlat);
flag_lat=1;
}
}
void longitudecallback(const std_msgs::Float32MultiArray::ConstPtr& msg)
{
if(flag_lon==0)
{
int i = 0;
// print all the remaining numbers
for(std::vector<float>::const_iterator it = msg->data.begin(); it != msg->data.end(); ++it)
{
lon[i] = *it;
i++;
num++;
}
slon=lon[0];
dlon=lon[1];
ROS_INFO("slon [%f]", slon);
ROS_INFO("dlon [%f]", dlon);
flag_lon=1;
}
}
int main(int argc, char **argv)
{
ros::init(argc,argv, "example1_c");
ros::NodeHandle n;
ros::Subscriber sub3 = n.subscribe("latitude", 1000, latitudecallback);
ros::Subscriber sub4 = n.subscribe("longitude", 1000, longitudecallback);
ros::Subscriber sub = n.subscribe("/mavros/global_position/global", 1000, chattercallback);
ros::Subscriber sub2 = n.subscribe("/mavros/global_position/compass_hdg", 1000, compass);
ros::Subscriber sub1 = n.subscribe("/my_topic", 1000, encodercallback);
ros::Publisher chatter_pub = n.advertise<std_msgs::Int8>("chatter", 1000);
ros::Rate loop_rate(10);
while (ros::ok())
{
std_msgs::Int8 msg;
msg.data = output;
chatter_pub.publish(msg);
ros::spinOnce();
loop_rate.sleep();
}
ros::spin();
return 0;
}