-
Notifications
You must be signed in to change notification settings - Fork 0
/
mavlink_control.cpp
374 lines (292 loc) · 11.3 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
/****************************************************************************
*
* 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;
// do the parse, will throw an int if it fails
parse_commandline(argc, argv, uart_name, baudrate);
// --------------------------------------------------------------------------
// PORT and THREAD STARTUP
// --------------------------------------------------------------------------
/*
* Instantiate a serial port object
*
* This object handles the opening and closing of the offboard computer's
* serial 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.
*
*/
Serial_Port 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(&serial_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.
*
*/
serial_port_quit = &serial_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.
*/
serial_port.start();
autopilot_interface.start();
// --------------------------------------------------------------------------
// RUN COMMANDS
// --------------------------------------------------------------------------
/*
* Now we can implement the algorithm we want on top of the autopilot interface
*/
commands(autopilot_interface);
// --------------------------------------------------------------------------
// THREAD and PORT SHUTDOWN
// --------------------------------------------------------------------------
/*
* Now that we are done we can stop the threads and close the port
*/
autopilot_interface.stop();
serial_port.stop();
// --------------------------------------------------------------------------
// DONE
// --------------------------------------------------------------------------
// woot!
return 0;
}
// ------------------------------------------------------------------------------
// COMMANDS
// ------------------------------------------------------------------------------
void
commands(Autopilot_Interface &api)
{
// --------------------------------------------------------------------------
// START OFFBOARD MODE
// --------------------------------------------------------------------------
api.enable_offboard_control();
usleep(100); // give some time to let it sink in
// now the autopilot is accepting setpoint commands
// --------------------------------------------------------------------------
// 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 - Set Velocity
// set_velocity( -1.0 , // [m/s]
// -1.0 , // [m/s]
// 0.0 , // [m/s]
// sp );
// Example 2 - Set Position
set_position( ip.x - 5.0 , // [m]
ip.y - 5.0 , // [m]
ip.z , // [m]
sp );
// Example 1.2 - Append Yaw Command
set_yaw( ip.yaw , // [rad]
sp );
// 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");
// --------------------------------------------------------------------------
// 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://pixhawk.ethz.ch/mavlink/#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://pixhawk.ethz.ch/mavlink/#HIGHRES_IMU)\n");
printf(" ap time: %llu \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)
{
// string for command line usage
const char *commandline_usage = "usage: mavlink_serial -d <devicename> -b <baudrate>";
// 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) {
uart_name = argv[i + 1];
} 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) {
baudrate = atoi(argv[i + 1]);
} else {
printf("%s\n",commandline_usage);
throw EXIT_FAILURE;
}
}
}
// 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){}
// serial port
try {
serial_port_quit->handle_quit(sig);
}
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;
}
}