-
Notifications
You must be signed in to change notification settings - Fork 261
/
mavlink_control.cpp
468 lines (375 loc) · 13.5 KB
/
mavlink_control.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
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
/****************************************************************************
*
* Copyright (c) 2014 MAVlink Development Team. All rights reserved.
* Author: Trent Lukaczyk, <[email protected]>
* Jaycee Lock, <[email protected]>
* Lorenz Meier, <[email protected]>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name PX4 nor the names of its contributors may be
* used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
****************************************************************************/
/**
* @file mavlink_control.cpp
*
* @brief An example offboard control process via mavlink
*
* This process connects an external MAVLink UART device to send an receive data
*
* @author Trent Lukaczyk, <[email protected]>
* @author Jaycee Lock, <[email protected]>
* @author Lorenz Meier, <[email protected]>
*
*/
// ------------------------------------------------------------------------------
// Includes
// ------------------------------------------------------------------------------
#include "mavlink_control.h"
// ------------------------------------------------------------------------------
// TOP
// ------------------------------------------------------------------------------
int
top (int argc, char **argv)
{
// --------------------------------------------------------------------------
// PARSE THE COMMANDS
// --------------------------------------------------------------------------
// Default input arguments
#ifdef __APPLE__
char *uart_name = (char*)"/dev/tty.usbmodem1";
#else
char *uart_name = (char*)"/dev/ttyUSB0";
#endif
int baudrate = 57600;
bool use_udp = false;
char *udp_ip = (char*)"127.0.0.1";
int udp_port = 14540;
bool autotakeoff = false;
// do the parse, will throw an int if it fails
parse_commandline(argc, argv, uart_name, baudrate, use_udp, udp_ip, udp_port, autotakeoff);
// --------------------------------------------------------------------------
// PORT and THREAD STARTUP
// --------------------------------------------------------------------------
/*
* Instantiate a generic port object
*
* This object handles the opening and closing of the offboard computer's
* port over which it will communicate to an autopilot. It has
* methods to read and write a mavlink_message_t object. To help with read
* and write in the context of pthreading, it gaurds port operations with a
* pthread mutex lock. It can be a serial or an UDP port.
*
*/
Generic_Port *port;
if(use_udp)
{
port = new UDP_Port(udp_ip, udp_port);
}
else
{
port = new Serial_Port(uart_name, baudrate);
}
/*
* Instantiate an autopilot interface object
*
* This starts two threads for read and write over MAVlink. The read thread
* listens for any MAVlink message and pushes it to the current_messages
* attribute. The write thread at the moment only streams a position target
* in the local NED frame (mavlink_set_position_target_local_ned_t), which
* is changed by using the method update_setpoint(). Sending these messages
* are only half the requirement to get response from the autopilot, a signal
* to enter "offboard_control" mode is sent by using the enable_offboard_control()
* method. Signal the exit of this mode with disable_offboard_control(). It's
* important that one way or another this program signals offboard mode exit,
* otherwise the vehicle will go into failsafe.
*
*/
Autopilot_Interface autopilot_interface(port);
/*
* Setup interrupt signal handler
*
* Responds to early exits signaled with Ctrl-C. The handler will command
* to exit offboard mode if required, and close threads and the port.
* The handler in this example needs references to the above objects.
*
*/
port_quit = port;
autopilot_interface_quit = &autopilot_interface;
signal(SIGINT,quit_handler);
/*
* Start the port and autopilot_interface
* This is where the port is opened, and read and write threads are started.
*/
port->start();
autopilot_interface.start();
// --------------------------------------------------------------------------
// RUN COMMANDS
// --------------------------------------------------------------------------
/*
* Now we can implement the algorithm we want on top of the autopilot interface
*/
commands(autopilot_interface, autotakeoff);
// --------------------------------------------------------------------------
// THREAD and PORT SHUTDOWN
// --------------------------------------------------------------------------
/*
* Now that we are done we can stop the threads and close the port
*/
autopilot_interface.stop();
port->stop();
delete port;
// --------------------------------------------------------------------------
// DONE
// --------------------------------------------------------------------------
// woot!
return 0;
}
// ------------------------------------------------------------------------------
// COMMANDS
// ------------------------------------------------------------------------------
void
commands(Autopilot_Interface &api, bool autotakeoff)
{
// --------------------------------------------------------------------------
// START OFFBOARD MODE
// --------------------------------------------------------------------------
api.enable_offboard_control();
usleep(100); // give some time to let it sink in
// now the autopilot is accepting setpoint commands
if(autotakeoff)
{
// arm autopilot
api.arm_disarm(true);
usleep(100); // give some time to let it sink in
}
// --------------------------------------------------------------------------
// SEND OFFBOARD COMMANDS
// --------------------------------------------------------------------------
printf("SEND OFFBOARD COMMANDS\n");
// initialize command data strtuctures
mavlink_set_position_target_local_ned_t sp;
mavlink_set_position_target_local_ned_t ip = api.initial_position;
// autopilot_interface.h provides some helper functions to build the command
// Example 1 - Fly up by to 2m
set_position( ip.x , // [m]
ip.y , // [m]
ip.z - 2.0 , // [m]
sp );
if(autotakeoff)
{
sp.type_mask |= MAVLINK_MSG_SET_POSITION_TARGET_LOCAL_NED_TAKEOFF;
}
// SEND THE COMMAND
api.update_setpoint(sp);
// NOW pixhawk will try to move
// Wait for 8 seconds, check position
for (int i=0; i < 8; i++)
{
mavlink_local_position_ned_t pos = api.current_messages.local_position_ned;
printf("%i CURRENT POSITION XYZ = [ % .4f , % .4f , % .4f ] \n", i, pos.x, pos.y, pos.z);
sleep(1);
}
// Example 2 - Set Velocity
set_velocity( -1.0 , // [m/s]
-1.0 , // [m/s]
0.0 , // [m/s]
sp );
// Example 2.1 - Append Yaw Command
set_yaw( ip.yaw + 90.0/180.0*M_PI, // [rad]
sp );
// SEND THE COMMAND
api.update_setpoint(sp);
// NOW pixhawk will try to move
// Wait for 4 seconds, check position
for (int i=0; i < 4; i++)
{
mavlink_local_position_ned_t pos = api.current_messages.local_position_ned;
printf("%i CURRENT POSITION XYZ = [ % .4f , % .4f , % .4f ] \n", i, pos.x, pos.y, pos.z);
sleep(1);
}
if(autotakeoff)
{
// Example 3 - Land using fixed velocity
set_velocity( 0.0 , // [m/s]
0.0 , // [m/s]
1.0 , // [m/s]
sp );
sp.type_mask |= MAVLINK_MSG_SET_POSITION_TARGET_LOCAL_NED_LAND;
// SEND THE COMMAND
api.update_setpoint(sp);
// NOW pixhawk will try to move
// Wait for 8 seconds, check position
for (int i=0; i < 8; i++)
{
mavlink_local_position_ned_t pos = api.current_messages.local_position_ned;
printf("%i CURRENT POSITION XYZ = [ % .4f , % .4f , % .4f ] \n", i, pos.x, pos.y, pos.z);
sleep(1);
}
printf("\n");
// disarm autopilot
api.arm_disarm(false);
usleep(100); // give some time to let it sink in
}
// --------------------------------------------------------------------------
// STOP OFFBOARD MODE
// --------------------------------------------------------------------------
api.disable_offboard_control();
// now pixhawk isn't listening to setpoint commands
// --------------------------------------------------------------------------
// GET A MESSAGE
// --------------------------------------------------------------------------
printf("READ SOME MESSAGES \n");
// copy current messages
Mavlink_Messages messages = api.current_messages;
// local position in ned frame
mavlink_local_position_ned_t pos = messages.local_position_ned;
printf("Got message LOCAL_POSITION_NED (spec: https://mavlink.io/en/messages/common.html#LOCAL_POSITION_NED)\n");
printf(" pos (NED): %f %f %f (m)\n", pos.x, pos.y, pos.z );
// hires imu
mavlink_highres_imu_t imu = messages.highres_imu;
printf("Got message HIGHRES_IMU (spec: https://mavlink.io/en/messages/common.html#HIGHRES_IMU)\n");
printf(" ap time: %lu \n", imu.time_usec);
printf(" acc (NED): % f % f % f (m/s^2)\n", imu.xacc , imu.yacc , imu.zacc );
printf(" gyro (NED): % f % f % f (rad/s)\n", imu.xgyro, imu.ygyro, imu.zgyro);
printf(" mag (NED): % f % f % f (Ga)\n" , imu.xmag , imu.ymag , imu.zmag );
printf(" baro: %f (mBar) \n" , imu.abs_pressure);
printf(" altitude: %f (m) \n" , imu.pressure_alt);
printf(" temperature: %f C \n" , imu.temperature );
printf("\n");
// --------------------------------------------------------------------------
// END OF COMMANDS
// --------------------------------------------------------------------------
return;
}
// ------------------------------------------------------------------------------
// Parse Command Line
// ------------------------------------------------------------------------------
// throws EXIT_FAILURE if could not open the port
void
parse_commandline(int argc, char **argv, char *&uart_name, int &baudrate,
bool &use_udp, char *&udp_ip, int &udp_port, bool &autotakeoff)
{
// string for command line usage
const char *commandline_usage = "usage: mavlink_control [-d <devicename> -b <baudrate>] [-u <udp_ip> -p <udp_port>] [-a ]";
// Read input arguments
for (int i = 1; i < argc; i++) { // argv[0] is "mavlink"
// Help
if (strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "--help") == 0) {
printf("%s\n",commandline_usage);
throw EXIT_FAILURE;
}
// UART device ID
if (strcmp(argv[i], "-d") == 0 || strcmp(argv[i], "--device") == 0) {
if (argc > i + 1) {
i++;
uart_name = argv[i];
} else {
printf("%s\n",commandline_usage);
throw EXIT_FAILURE;
}
}
// Baud rate
if (strcmp(argv[i], "-b") == 0 || strcmp(argv[i], "--baud") == 0) {
if (argc > i + 1) {
i++;
baudrate = atoi(argv[i]);
} else {
printf("%s\n",commandline_usage);
throw EXIT_FAILURE;
}
}
// UDP ip
if (strcmp(argv[i], "-u") == 0 || strcmp(argv[i], "--udp_ip") == 0) {
if (argc > i + 1) {
i++;
udp_ip = argv[i];
use_udp = true;
} else {
printf("%s\n",commandline_usage);
throw EXIT_FAILURE;
}
}
// UDP port
if (strcmp(argv[i], "-p") == 0 || strcmp(argv[i], "--port") == 0) {
if (argc > i + 1) {
i++;
udp_port = atoi(argv[i]);
} else {
printf("%s\n",commandline_usage);
throw EXIT_FAILURE;
}
}
// Autotakeoff
if (strcmp(argv[i], "-a") == 0 || strcmp(argv[i], "--autotakeoff") == 0) {
autotakeoff = true;
}
}
// end: for each input argument
// Done!
return;
}
// ------------------------------------------------------------------------------
// Quit Signal Handler
// ------------------------------------------------------------------------------
// this function is called when you press Ctrl-C
void
quit_handler( int sig )
{
printf("\n");
printf("TERMINATING AT USER REQUEST\n");
printf("\n");
// autopilot interface
try {
autopilot_interface_quit->handle_quit(sig);
}
catch (int error){}
// port
try {
port_quit->stop();
}
catch (int error){}
// end program here
exit(0);
}
// ------------------------------------------------------------------------------
// Main
// ------------------------------------------------------------------------------
int
main(int argc, char **argv)
{
// This program uses throw, wrap one big try/catch here
try
{
int result = top(argc,argv);
return result;
}
catch ( int error )
{
fprintf(stderr,"mavlink_control threw exception %i \n" , error);
return error;
}
}