mavlink.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017, James Jackson and Daniel Koch, BYU MAGICC Lab
3  *
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  * * Redistributions of source code must retain the above copyright notice, this
10  * list of conditions and the following disclaimer.
11  *
12  * * Redistributions in binary form must reproduce the above copyright notice,
13  * this list of conditions and the following disclaimer in the documentation
14  * and/or other materials provided with the distribution.
15  *
16  * * Neither the name of the copyright holder nor the names of its
17  * contributors may be used to endorse or promote products derived from
18  * this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31 #include <cstdint>
32 
33 #include "board.h"
34 
35 #include "mavlink.h"
36 
37 namespace rosflight_firmware
38 {
39 
41  board_(board)
42 {}
43 
44 void Mavlink::init(uint32_t baud_rate, uint32_t dev)
45 {
46  board_.serial_init(baud_rate, dev);
47  initialized_ = true;
48 }
49 
50 void Mavlink::receive(void)
51 {
53  {
56  }
57 }
58 
59 void Mavlink::send_attitude_quaternion(uint8_t system_id,
60  uint64_t timestamp_us,
61  const turbomath::Quaternion &attitude,
62  const turbomath::Vector &angular_velocity)
63 {
64  mavlink_message_t msg;
66  timestamp_us / 1000,
67  attitude.w,
68  attitude.x,
69  attitude.y,
70  attitude.z,
71  angular_velocity.x,
72  angular_velocity.y,
73  angular_velocity.z);
74  send_message(msg);
75 }
76 
77 void Mavlink::send_baro(uint8_t system_id, float altitude, float pressure, float temperature)
78 {
79  mavlink_message_t msg;
80  mavlink_msg_small_baro_pack(system_id, compid_, &msg, altitude, pressure, temperature);
81  send_message(msg);
82 }
83 
84 void Mavlink::send_command_ack(uint8_t system_id, Command command, bool success)
85 {
86  ROSFLIGHT_CMD rosflight_cmd = ROSFLIGHT_CMD_ENUM_END;
87  switch (command)
88  {
90  rosflight_cmd = ROSFLIGHT_CMD_READ_PARAMS;
91  break;
93  rosflight_cmd = ROSFLIGHT_CMD_WRITE_PARAMS;
94  break;
96  rosflight_cmd = ROSFLIGHT_CMD_SET_PARAM_DEFAULTS;
97  break;
99  rosflight_cmd = ROSFLIGHT_CMD_ACCEL_CALIBRATION;
100  break;
102  rosflight_cmd = ROSFLIGHT_CMD_GYRO_CALIBRATION;
103  break;
105  rosflight_cmd = ROSFLIGHT_CMD_BARO_CALIBRATION;
106  break;
108  rosflight_cmd = ROSFLIGHT_CMD_AIRSPEED_CALIBRATION;
109  break;
111  rosflight_cmd = ROSFLIGHT_CMD_RC_CALIBRATION;
112  break;
114  rosflight_cmd = ROSFLIGHT_CMD_REBOOT;
115  break;
117  rosflight_cmd = ROSFLIGHT_CMD_REBOOT_TO_BOOTLOADER;
118  break;
120  rosflight_cmd = ROSFLIGHT_CMD_SEND_VERSION;
121  break;
122  }
123 
124  mavlink_message_t msg;
126  rosflight_cmd, (success) ? ROSFLIGHT_CMD_SUCCESS : ROSFLIGHT_CMD_FAILED);
127  send_message(msg);
128 }
129 
130 void Mavlink::send_diff_pressure(uint8_t system_id, float velocity, float pressure, float temperature)
131 {
132  mavlink_message_t msg;
133  mavlink_msg_diff_pressure_pack(system_id, compid_, &msg, velocity, pressure, temperature);
134  send_message(msg);
135 }
136 
137 void Mavlink::send_heartbeat(uint8_t system_id, bool fixed_wing)
138 {
139  mavlink_message_t msg;
140  mavlink_msg_heartbeat_pack(system_id, compid_, &msg,
142  0, 0, 0, 0);
143  send_message(msg);
144 }
145 
146 void Mavlink::send_imu(uint8_t system_id, uint64_t timestamp_us,
147  const turbomath::Vector &accel,
148  const turbomath::Vector &gyro,
149  float temperature)
150 {
151  mavlink_message_t msg;
152  mavlink_msg_small_imu_pack(system_id, compid_, &msg,
153  timestamp_us,
154  accel.x,
155  accel.y,
156  accel.z,
157  gyro.x,
158  gyro.y,
159  gyro.z,
160  temperature);
161  send_message(msg);
162 }
163 void Mavlink::send_gnss(uint8_t system_id, uint32_t time_of_week, uint8_t fix_type, uint64_t time, uint64_t nanos,
164  int32_t lat,
165  int32_t lon, int32_t height, int32_t vel_n, int32_t vel_e, int32_t vel_d, uint32_t h_acc, uint32_t v_acc,
166  int32_t ecef_x, int32_t ecef_y, int32_t ecef_z, uint32_t p_acc, int32_t ecef_v_x, int32_t ecef_v_y,
167  int32_t ecef_v_z, uint32_t s_acc, uint64_t rosflight_timestamp)
168 {
169  mavlink_message_t msg;
171  compid_,
172  &msg,
173  time_of_week,
174  fix_type,
175  time,
176  nanos,
177  lat,
178  lon,
179  height,
180  vel_n,
181  vel_e,
182  vel_d,
183  h_acc,
184  v_acc,
185  ecef_x,
186  ecef_y,
187  ecef_z,
188  p_acc,
189  ecef_v_x,
190  ecef_v_y,
191  ecef_v_z,
192  s_acc,
193  rosflight_timestamp);
194  send_message(msg);
195 }
196 
197 void Mavlink::send_gnss_raw(uint8_t system_id, uint32_t time_of_week, uint16_t year, uint8_t month, uint8_t day,
198  uint8_t hour, uint8_t min, uint8_t sec, uint8_t valid, uint32_t t_acc,
199  int32_t nano, uint8_t fix_type, uint8_t num_sat,
200  int32_t lon, int32_t lat, int32_t height, int32_t height_msl,
201  uint32_t h_acc, uint32_t v_acc, int32_t vel_n, int32_t vel_e,
202  int32_t vel_d, int32_t g_speed, int32_t head_mot, uint32_t s_acc,
203  uint32_t head_acc, uint16_t p_dop, uint64_t rosflight_timestamp)
204 {
205  mavlink_message_t msg;
207  data.time_of_week = time_of_week;
208  data.year = year;
209  data.month = month;
210  data.day = day;
211  data.hour = hour;
212  data.min = min;
213  data.sec = sec;
214  data.valid = valid;
215  data.t_acc = t_acc;
216  data.nano = nano;
217  data.fix_type = fix_type;
218  data.num_sat = num_sat;
219  data.lon = lon;
220  data.lat = lat;
221  data.height = height;
222  data.height_msl = height_msl;
223  data.h_acc = h_acc;
224  data.v_acc = v_acc;
225  data.vel_n = vel_n;
226  data.vel_e = vel_e;
227  data.vel_d = vel_d;
228  data.g_speed = g_speed;
229  data.head_mot = head_mot;
230  data.s_acc = s_acc;
231  data.head_acc = head_acc;
232  data.p_dop = p_dop;
233  data.rosflight_timestamp = rosflight_timestamp;
234  mavlink_msg_rosflight_gnss_raw_encode(system_id, compid_, &msg, &data);
235  send_message(msg);
236 }
237 
238 void Mavlink::send_log_message(uint8_t system_id, LogSeverity severity, const char *text)
239 {
240  MAV_SEVERITY mavlink_severity = MAV_SEVERITY_ENUM_END;
241  switch (severity)
242  {
244  mavlink_severity = MAV_SEVERITY_INFO;
245  break;
247  mavlink_severity = MAV_SEVERITY_WARNING;
248  break;
250  mavlink_severity = MAV_SEVERITY_ERROR;
251  break;
253  mavlink_severity = MAV_SEVERITY_CRITICAL;
254  break;
255  }
256 
257  mavlink_message_t msg;
258  mavlink_msg_statustext_pack(system_id, compid_, &msg, static_cast<uint8_t>(mavlink_severity), text);
259  send_message(msg);
260 }
261 
262 void Mavlink::send_mag(uint8_t system_id, const turbomath::Vector &mag)
263 {
264  mavlink_message_t msg;
265  mavlink_msg_small_mag_pack(system_id, compid_, &msg, mag.x, mag.y, mag.z);
266  send_message(msg);
267 }
268 
269 void Mavlink::send_named_value_int(uint8_t system_id, uint32_t timestamp_ms, const char *const name, int32_t value)
270 {
271  mavlink_message_t msg;
272  mavlink_msg_named_value_int_pack(system_id, compid_, &msg, timestamp_ms, name, value);
273  send_message(msg);
274 }
275 
276 void Mavlink::send_named_value_float(uint8_t system_id, uint32_t timestamp_ms, const char *const name, float value)
277 {
278  mavlink_message_t msg;
279  mavlink_msg_named_value_float_pack(system_id, compid_, &msg, timestamp_ms, name, value);
280  send_message(msg);
281 }
282 
283 void Mavlink::send_output_raw(uint8_t system_id, uint32_t timestamp_ms, const float raw_outputs[8])
284 {
285  mavlink_message_t msg;
286  mavlink_msg_rosflight_output_raw_pack(system_id, compid_, &msg, timestamp_ms, raw_outputs);
287  send_message(msg);
288 }
289 
290 void Mavlink::send_param_value_int(uint8_t system_id,
291  uint16_t index,
292  const char *const name,
293  int32_t value,
294  uint16_t param_count)
295 {
296  mavlink_param_union_t param;
297  param.param_int32 = value;
298 
299  mavlink_message_t msg;
300  mavlink_msg_param_value_pack(system_id, 0, &msg, name, param.param_float, MAV_PARAM_TYPE_INT32, param_count, index);
301  send_message(msg);
302 }
303 
304 void Mavlink::send_param_value_float(uint8_t system_id,
305  uint16_t index,
306  const char *const name,
307  float value,
308  uint16_t param_count)
309 {
310  mavlink_message_t msg;
311  mavlink_msg_param_value_pack(system_id, 0, &msg, name, value, MAV_PARAM_TYPE_REAL32, param_count, index);
312  send_message(msg);
313 }
314 
315 void Mavlink::send_rc_raw(uint8_t system_id, uint32_t timestamp_ms, const uint16_t channels[8])
316 {
317  mavlink_message_t msg;
318  mavlink_msg_rc_channels_pack(system_id, compid_, &msg,
319  timestamp_ms,
320  0,
321  channels[0],
322  channels[1],
323  channels[2],
324  channels[3],
325  channels[4],
326  channels[5],
327  channels[6],
328  channels[7],
329  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
330  send_message(msg);
331 }
332 
333 void Mavlink::send_sonar(uint8_t system_id, /* TODO enum type*/uint8_t type, float range, float max_range,
334  float min_range)
335 {
336  (void) type;
337  mavlink_message_t msg;
338  mavlink_msg_small_range_pack(system_id, compid_, &msg, /* TODO */ROSFLIGHT_RANGE_SONAR, range, max_range, min_range);
339  send_message(msg);
340 }
341 
342 void Mavlink::send_status(uint8_t system_id,
343  bool armed,
344  bool failsafe,
345  bool rc_override,
346  bool offboard,
347  uint8_t error_code,
348  uint8_t control_mode,
349  int16_t num_errors,
350  int16_t loop_time_us)
351 {
352  mavlink_message_t msg;
354  armed,
355  failsafe,
356  rc_override,
357  offboard,
358  error_code,
359  control_mode,
360  num_errors,
361  loop_time_us);
362  send_message(msg);
363 }
364 
365 void Mavlink::send_timesync(uint8_t system_id, int64_t tc1, int64_t ts1)
366 {
367  mavlink_message_t msg;
368  mavlink_msg_timesync_pack(system_id, compid_, &msg, tc1, ts1);
369  send_message(msg);
370 }
371 
372 void Mavlink::send_version(uint8_t system_id, const char *const version)
373 {
374  mavlink_message_t msg;
375  mavlink_msg_rosflight_version_pack(system_id, compid_, &msg, version);
376  send_message(msg);
377 }
378 void Mavlink::send_error_data(uint8_t system_id, const BackupData &error_data)
379 {
380  mavlink_message_t msg;
381  bool rearm = (error_data.state.armed && error_data.arm_status==rosflight_firmware::ARM_MAGIC);
382  mavlink_msg_rosflight_hard_error_pack(system_id,compid_, &msg, error_data.error_code, error_data.debug_info.pc,
383  error_data.reset_count, rearm);
384  send_message(msg);
385 }
386 
387 void Mavlink::send_message(const mavlink_message_t &msg)
388 {
389  if (initialized_)
390  {
391  uint8_t data[MAVLINK_MAX_PACKET_LEN];
392  uint16_t len = mavlink_msg_to_send_buffer(data, &msg);
393  board_.serial_write(data, len);
394  }
395 }
396 
397 void Mavlink::handle_msg_param_request_list(const mavlink_message_t *const msg)
398 {
402 }
403 
404 void Mavlink::handle_msg_param_request_read(const mavlink_message_t *const msg)
405 {
409 }
410 
411 void Mavlink::handle_msg_param_set(const mavlink_message_t *const msg)
412 {
414  mavlink_msg_param_set_decode(msg, &set);
415 
416  mavlink_param_union_t param;
417  param.param_float = set.param_value;
418  param.type = set.param_type;
419 
420  switch (param.type)
421  {
423  param_set_int_callback_(set.target_system, set.param_id, param.param_int32);
424  break;
426  param_set_float_callback_(set.target_system, set.param_id, param.param_float);
427  break;
428  default:
429  // unsupported parameter type
430  break;
431  }
432 }
433 
434 void Mavlink::handle_msg_rosflight_cmd(const mavlink_message_t *const msg)
435 {
438 
440  switch (cmd.command)
441  {
444  break;
447  break;
450  break;
453  break;
456  break;
459  break;
462  break;
465  break;
468  break;
471  break;
474  break;
475  default: // unsupported command; report failure then return without calling command callback
476  mavlink_message_t out_msg;
478  send_message(out_msg);
479  // log(LogSeverity::LOG_ERROR, "Unsupported ROSFLIGHT CMD %d", command);
480  return;
481  }
482 
483  command_callback_(command);
484 }
485 
486 void Mavlink::handle_msg_timesync(const mavlink_message_t *const msg)
487 {
488  mavlink_timesync_t tsync;
489  mavlink_msg_timesync_decode(msg, &tsync);
490  timesync_callback_(tsync.tc1, tsync.ts1);
491 }
492 
493 void Mavlink::handle_msg_offboard_control(const mavlink_message_t *const msg)
494 {
497 
499  switch (ctrl.mode)
500  {
501  case MODE_PASS_THROUGH:
503  break;
506  break;
509  break;
510  default:
511  // invalid mode; ignore message and return without calling callback
512  return;
513  }
514 
515  control.x.value = ctrl.x;
516  control.y.value = ctrl.y;
517  control.z.value = ctrl.z;
518  control.F.value = ctrl.F;
519 
520  control.x.valid = !(ctrl.ignore & IGNORE_VALUE1);
521  control.y.valid = !(ctrl.ignore & IGNORE_VALUE2);
522  control.z.valid = !(ctrl.ignore & IGNORE_VALUE3);
523  control.F.valid = !(ctrl.ignore & IGNORE_VALUE4);
524 
526 }
527 
528 void Mavlink::handle_msg_attitude_correction(const mavlink_message_t *const msg)
529 {
532 
533  turbomath::Quaternion q_correction;
534  q_correction.w = q_msg.qw;
535  q_correction.x = q_msg.qx;
536  q_correction.y = q_msg.qy;
537  q_correction.z = q_msg.qz;
538 
539  attitude_correction_callback_(q_correction);
540 }
541 void Mavlink::handle_msg_heartbeat(const mavlink_message_t *const msg)
542 {
543  //none of the information from the heartbeat is used
544  (void)msg;
545  this->heartbeat_callback_();
546 }
547 
549 {
550  switch (in_buf_.msgid)
551  {
554  break;
557  break;
560  break;
563  break;
566  break;
569  break;
572  break;
575  break;
576  default:
577  break;
578  }
579 }
580 
581 } // namespace rosflight_firmware
static volatile int16_t gyro[3]
Definition: drv_mpu6050.c:277
bool param(const std::string &param_name, T &param_val, const T &default_val)
float altitude
Definition: ms4525.c:41
float pressure
Definition: ms4525.c:41
static uint8_t cmd
Definition: drv_hmc5883l.c:79
debug_info_t debug_info
Definition: board.h:59
virtual void serial_write(const uint8_t *src, size_t len)=0
float temperature
Definition: ms4525.c:41
virtual uint16_t serial_bytes_available()=0
static volatile int16_t accel[3]
Definition: drv_mpu6050.c:276
ROSLIB_DECL std::string command(const std::string &cmd)
static const channelConfig_t channels[CC_CHANNELS_PER_TIMER]
Definition: drv_timer.c:111
StateManager::State state
Definition: board.h:63
virtual void serial_init(uint32_t baud_rate, uint32_t dev)=0
const uint32_t ARM_MAGIC
Definition: board.h:67
virtual uint8_t serial_read()=0


rosflight_firmware
Author(s): Daniel Koch , James Jackson
autogenerated on Wed Jul 3 2019 19:59:24