-
Notifications
You must be signed in to change notification settings - Fork 0
/
CPS_Drone_Navigation_Code.txt
320 lines (262 loc) · 8.33 KB
/
CPS_Drone_Navigation_Code.txt
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
/**
* @file offb_node.cpp
* @brief offboard example node, written with mavros version 0.14.2, px4 flight
* stack and tested in Gazebo SITL
*/
#include <ros/ros.h>
#include <geometry_msgs/PoseStamped.h>
#include <mavros_msgs/CommandBool.h>
#include <mavros_msgs/CommandTOL.h>
#include <mavros_msgs/SetMode.h>
#include <mavros_msgs/State.h>
#define FLIGHT_ALTITUDE 0.5f
mavros_msgs::State current_state;
void state_cb(const mavros_msgs::State::ConstPtr& msg){
current_state = *msg;
}
mavros_msgs::ManualControl current_input;
void state_cb(const mavros_msgs::ManualControl::ConstPtr& msg){
current_input = *msg;
ROS_INFO(current_input.buttons);
}
int main(int argc, char **argv)
{
ros::init(argc, argv, "offb_node");
ros::NodeHandle nh;
ros::Subscriber state_sub = nh.subscribe<mavros_msgs::State>
("mavros/state", 10, state_cb);
ros::Subscriber manual_sub = nh.subscribe<mavros_msgs::ManualControl>
("mavros/manual_control/control ", 10, state_cb);
ros::Publisher local_pos_pub = nh.advertise<geometry_msgs::PoseStamped>
("mavros/setpoint_position/local", 10);
ros::Publisher global_pos_pub = nh.advertise<mavros_msgs::GlobalPositionTarget>
("mavros/setpoint_position/global", 10);
ros::ServiceClient arming_client = nh.serviceClient<mavros_msgs::CommandBool>
("mavros/cmd/arming");
ros::ServiceClient land_client = nh.serviceClient<mavros_msgs::CommandTOL>
("mavros/cmd/land");
ros::ServiceClient set_mode_client = nh.serviceClient<mavros_msgs::SetMode>
("mavros/set_mode");
//the setpoint publishing rate MUST be faster than 2Hz
ros::Rate rate(20.0);
// wait for FCU connection
while(ros::ok() && current_state.connected){
ros::spinOnce();
rate.sleep();
ROS_INFO("connecting to FCT...");
}
mavros_msgs::GlobalPositionTarget global_pose;
global_pose.latitude = 32.266505;
global_pose.longitude = -111.27434599999998;
global_pose.altitude = 661;
geometry_msgs::PoseStamped pose;
pose.pose.position.x = 0;
pose.pose.position.y = 0;
pose.pose.position.z = FLIGHT_ALTITUDE;
//send a few setpoints before starting
for(int i = 100; ros::ok() && i > 0; --i){
local_pos_pub.publish(pose);
ros::spinOnce();
rate.sleep();
}
mavros_msgs::SetMode offb_set_mode;
offb_set_mode.request.custom_mode = "OFFBOARD";
mavros_msgs::CommandBool arm_cmd;
arm_cmd.request.value = true;
mavros_msgs::CommandTOL land_cmd;
land_cmd.request.yaw = 0;
land_cmd.request.latitude = 0;
land_cmd.request.longitude = 0;
land_cmd.request.altitude = 0;
ros::Time last_request = ros::Time::now();
// change to offboard mode and arm
while(ros::ok() && !current_state.armed){
if( current_state.mode != "OFFBOARD" &&
(ros::Time::now() - last_request > ros::Duration(5.0))){
ROS_INFO(current_state.mode.c_str());
if( set_mode_client.call(offb_set_mode) &&
offb_set_mode.response.mode_sent){
ROS_INFO("Offboard enabled");
}
last_request = ros::Time::now();
} else {
if( !current_state.armed &&
(ros::Time::now() - last_request > ros::Duration(5.0))){
if( arming_client.call(arm_cmd) &&
arm_cmd.response.success){
ROS_INFO("Vehicle armed");
}
last_request = ros::Time::now();
}
}
local_pos_pub.publish(pose);
ros::spinOnce();
rate.sleep();
}
// go to the first waypoint
pose.pose.position.x = 0;
pose.pose.position.y = 0;
pose.pose.position.z = FLIGHT_ALTITUDE;
ROS_INFO("going to the first way point");
for(int i = 0; ros::ok() && i < 10*20 ; ++i){
global_pos_pub.publish(global_pose);
ros::spinOnce();
rate.sleep();
}
ROS_INFO("first way point finished!");
ROS_INFO("going to the first way point");
for(int i = 0; ros::ok() && i < 10*20; ++i){
local_pos_pub.publish(pose);
ros::spinOnce();
rate.sleep();
}
ROS_INFO("first way point finished!");
// go to the second waypoint
pose.pose.position.x = 0;
pose.pose.position.y = 0.1;
pose.pose.position.z = FLIGHT_ALTITUDE;
//send setpoints for 10 seconds
ROS_INFO("going to second way point");
for(int i = 0; ros::ok() && i < 10*20; ++i){
local_pos_pub.publish(pose);
ros::spinOnce();
rate.sleep();
}
ROS_INFO("second way point finished!");
// go to the third waypoint
pose.pose.position.x = 0.1;
pose.pose.position.y = 0.1;
pose.pose.position.z = FLIGHT_ALTITUDE;
//send setpoints for 10 seconds
ROS_INFO("going to third way point");
for(int i = 0; ros::ok() && i < 10*20; ++i){
local_pos_pub.publish(pose);
ros::spinOnce();
rate.sleep();
}
ROS_INFO("third way point finished!");
// go to the forth waypoint
pose.pose.position.x = 0.1;
pose.pose.position.y = 0;
pose.pose.position.z = FLIGHT_ALTITUDE;
//send setpoints for 10 seconds
ROS_INFO("going to forth way point");
for(int i = 0; ros::ok() && i < 10*20; ++i){
local_pos_pub.publish(pose);
ros::spinOnce();
rate.sleep();
}
ROS_INFO("forth way point finished!");
pose.pose.position.x = 0;
pose.pose.position.y = 0;
pose.pose.position.z = FLIGHT_ALTITUDE;
ROS_INFO("going back to the first point!");
//send setpoints for 10 seconds
for(int i = 0; ros::ok() && i < 10*20; ++i){
local_pos_pub.publish(pose);
ros::spinOnce();
rate.sleep();
}
ROS_INFO("tring to land");
while (!(land_client.call(land_cmd) &&
land_cmd.response.success)){
//local_pos_pub.publish(pose);
ROS_INFO("tring to land");
ros::spinOnce();
rate.sleep();
}
return 0;
}
//
// Created by Arda on 5/8/2018.
//
#ifndef PX4_ROUTE_H
#define PX4_ROUTE_H
//#include <geometry_msgs/PoseStamped.h>
class Route {
private:
float foci;
float verti;
/*
struct Point{
float x;
float y;
float z;
};
std::queue<Point> wpoints;
*/
queue <geometry_msgs::PoseStamped> wpoints;
public:
//Class Constructor
Route();
//Alternative Constructor;
Route(float fociIn, float vertiIn);
//Shows the next point on route.
geometry_msgs::PoseStamped nextPoint();
//Pop the front value
void pop();
//Prints the route
void print();
//Returns isEmpty value of queue
bool isEmpty();
};
#endif //PX4_ROUTE_H
//
// Created by Arda on 5/8/2018.
//
#include <geometry_msgs/PoseStamped.h>
#include <queue>
#include <iostream>
#include "Route.h"
#include <math.h>
#define FLIGHT_ALTITUDE 0.5f
#define SCAN_RADIUS 50.0
//Class Constructor
Route::Route():foci(0),verti(0){}
//Alternative Constructor;
Route::Route(float fociIn, float vertiIn):foci(fociIn),verti(vertiIn){
geometry_msgs::PoseStamped pose;
pose.pose.position.z = FLIGHT_ALTITUDE;
double k = 50;//Directly proportional to amount of circles in spiral
double s = 5;//Invertly proportional to the distance between each point
double dt = 1;
double t = 0;
while(t<k){
pose.pose.position.x = (foci*t*cos(t))/k;
pose.pose.position.y = (verti*t*sin(t))/k;
wpoints.push(pose);
t+= M_PI/(dt*s);
dt = t/M_PI + 1;
}
}
geometry_msgs::PoseStamped Route::nextPoint(){
if(!wpoints.isEmpty()){
geometry_msgs::PoseStamped nWayPoint = wpoints.front();
return nWayPoint;
}else{
geometry_msgs::PoseStamped pose;
std::cout<<"No more Waypoints left"<<std::endl;
return pose;
}
}
void Route::pop(){
if(!wpoints.isEmpty()) {
wpoints.pop();
}else{
std::cout<<"No more Waypoints left"<<std::endl;
}
}
void Route::print(){
if(!wpoints.isEmpty()){
for(size_t i =0;i<4;i++) {
float x = wpoints.front().pose.position.x;
float y = wpoints.front().pose.position.y;
float z = wpoints.front().pose.position.z;
std::cout << "Waypoint "<<(i+1)<<": ("<<x<<" , "<<y<<" , "<<z<<")"<<std::endl;
wpoints.pop();
}
}
}
bool Route::isEmpty(){
return wpoints.isEmpty();
}