-
Notifications
You must be signed in to change notification settings - Fork 260
/
autopilot_interface.h
310 lines (244 loc) · 8.92 KB
/
autopilot_interface.h
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
/****************************************************************************
*
* Copyright (c) 2014 MAVlink Development Team. All rights reserved.
* Author: Trent Lukaczyk, <aerialhedgehog@gmail.com>
* Jaycee Lock, <jaycee.lock@gmail.com>
* Lorenz Meier, <lm@inf.ethz.ch>
*
* 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 autopilot_interface.h
*
* @brief Autopilot interface definition
*
* Functions for sending and recieving commands to an autopilot via MAVlink
*
* @author Trent Lukaczyk, <aerialhedgehog@gmail.com>
* @author Jaycee Lock, <jaycee.lock@gmail.com>
* @author Lorenz Meier, <lm@inf.ethz.ch>
*
*/
#ifndef AUTOPILOT_INTERFACE_H_
#define AUTOPILOT_INTERFACE_H_
// ------------------------------------------------------------------------------
// Includes
// ------------------------------------------------------------------------------
#include "generic_port.h"
#include <signal.h>
#include <time.h>
#include <sys/time.h>
#include <pthread.h> // This uses POSIX Threads
#include <unistd.h> // UNIX standard function definitions
#include <mutex>
#include <common/mavlink.h>
// ------------------------------------------------------------------------------
// Defines
// ------------------------------------------------------------------------------
/**
* Defines for mavlink_set_position_target_local_ned_t.type_mask
*
* Bitmask to indicate which dimensions should be ignored by the vehicle
*
* a value of 0b0000000000000000 or 0b0000001000000000 indicates that none of
* the setpoint dimensions should be ignored.
*
* If bit 10 is set the floats afx afy afz should be interpreted as force
* instead of acceleration.
*
* Mapping:
* bit 1: x,
* bit 2: y,
* bit 3: z,
* bit 4: vx,
* bit 5: vy,
* bit 6: vz,
* bit 7: ax,
* bit 8: ay,
* bit 9: az,
* bit 10: is force setpoint,
* bit 11: yaw,
* bit 12: yaw rate
* remaining bits unused
*
* Combine bitmasks with bitwise &
*
* Example for position and yaw angle:
* uint16_t type_mask =
* MAVLINK_MSG_SET_POSITION_TARGET_LOCAL_NED_POSITION &
* MAVLINK_MSG_SET_POSITION_TARGET_LOCAL_NED_YAW_ANGLE;
*/
// bit number 876543210987654321
#define MAVLINK_MSG_SET_POSITION_TARGET_LOCAL_NED_POSITION 0b0000110111111000
#define MAVLINK_MSG_SET_POSITION_TARGET_LOCAL_NED_VELOCITY 0b0000110111000111
#define MAVLINK_MSG_SET_POSITION_TARGET_LOCAL_NED_ACCELERATION 0b0000110000111111
#define MAVLINK_MSG_SET_POSITION_TARGET_LOCAL_NED_FORCE 0b0000111000111111
#define MAVLINK_MSG_SET_POSITION_TARGET_LOCAL_NED_YAW_ANGLE 0b0000100111111111
#define MAVLINK_MSG_SET_POSITION_TARGET_LOCAL_NED_YAW_RATE 0b0000010111111111
#define MAVLINK_MSG_SET_POSITION_TARGET_LOCAL_NED_TAKEOFF 0x1000
#define MAVLINK_MSG_SET_POSITION_TARGET_LOCAL_NED_LAND 0x2000
#define MAVLINK_MSG_SET_POSITION_TARGET_LOCAL_NED_LOITER 0x3000
#define MAVLINK_MSG_SET_POSITION_TARGET_LOCAL_NED_IDLE 0x4000
// ------------------------------------------------------------------------------
// Prototypes
// ------------------------------------------------------------------------------
// helper functions
uint64_t get_time_usec();
void set_position(float x, float y, float z, mavlink_set_position_target_local_ned_t &sp);
void set_velocity(float vx, float vy, float vz, mavlink_set_position_target_local_ned_t &sp);
void set_acceleration(float ax, float ay, float az, mavlink_set_position_target_local_ned_t &sp);
void set_yaw(float yaw, mavlink_set_position_target_local_ned_t &sp);
void set_yaw_rate(float yaw_rate, mavlink_set_position_target_local_ned_t &sp);
void* start_autopilot_interface_read_thread(void *args);
void* start_autopilot_interface_write_thread(void *args);
// ------------------------------------------------------------------------------
// Data Structures
// ------------------------------------------------------------------------------
struct Time_Stamps
{
Time_Stamps()
{
reset_timestamps();
}
uint64_t heartbeat;
uint64_t sys_status;
uint64_t battery_status;
uint64_t radio_status;
uint64_t local_position_ned;
uint64_t global_position_int;
uint64_t position_target_local_ned;
uint64_t position_target_global_int;
uint64_t highres_imu;
uint64_t attitude;
void
reset_timestamps()
{
heartbeat = 0;
sys_status = 0;
battery_status = 0;
radio_status = 0;
local_position_ned = 0;
global_position_int = 0;
position_target_local_ned = 0;
position_target_global_int = 0;
highres_imu = 0;
attitude = 0;
}
};
// Struct containing information on the MAV we are currently connected to
struct Mavlink_Messages {
int sysid;
int compid;
// Heartbeat
mavlink_heartbeat_t heartbeat;
// System Status
mavlink_sys_status_t sys_status;
// Battery Status
mavlink_battery_status_t battery_status;
// Radio Status
mavlink_radio_status_t radio_status;
// Local Position
mavlink_local_position_ned_t local_position_ned;
// Global Position
mavlink_global_position_int_t global_position_int;
// Local Position Target
mavlink_position_target_local_ned_t position_target_local_ned;
// Global Position Target
mavlink_position_target_global_int_t position_target_global_int;
// HiRes IMU
mavlink_highres_imu_t highres_imu;
// Attitude
mavlink_attitude_t attitude;
// System Parameters?
// Time Stamps
Time_Stamps time_stamps;
void
reset_timestamps()
{
time_stamps.reset_timestamps();
}
};
// ----------------------------------------------------------------------------------
// Autopilot Interface Class
// ----------------------------------------------------------------------------------
/*
* Autopilot Interface Class
*
* 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.
*/
class Autopilot_Interface
{
public:
Autopilot_Interface();
Autopilot_Interface(Generic_Port *port_);
~Autopilot_Interface();
char reading_status;
char writing_status;
char control_status;
uint64_t write_count;
int system_id;
int autopilot_id;
int companion_id;
Mavlink_Messages current_messages;
mavlink_set_position_target_local_ned_t initial_position;
void update_setpoint(mavlink_set_position_target_local_ned_t setpoint);
void read_messages();
int write_message(mavlink_message_t message);
int arm_disarm( bool flag );
void enable_offboard_control();
void disable_offboard_control();
void start();
void stop();
void start_read_thread();
void start_write_thread(void);
void handle_quit( int sig );
private:
Generic_Port *port;
bool time_to_exit;
pthread_t read_tid;
pthread_t write_tid;
struct {
std::mutex mutex;
mavlink_set_position_target_local_ned_t data;
} current_setpoint;
void read_thread();
void write_thread(void);
int toggle_offboard_control( bool flag );
void write_setpoint();
};
#endif // AUTOPILOT_INTERFACE_H_