aboutsummaryrefslogtreecommitdiff
path: root/src/modules/mavlink/mavlink_receiver.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/modules/mavlink/mavlink_receiver.cpp')
-rw-r--r--src/modules/mavlink/mavlink_receiver.cpp1328
1 files changed, 724 insertions, 604 deletions
diff --git a/src/modules/mavlink/mavlink_receiver.cpp b/src/modules/mavlink/mavlink_receiver.cpp
index 7b6fad658..99ec98ee9 100644
--- a/src/modules/mavlink/mavlink_receiver.cpp
+++ b/src/modules/mavlink/mavlink_receiver.cpp
@@ -1,7 +1,6 @@
/****************************************************************************
*
- * Copyright (C) 2012 PX4 Development Team. All rights reserved.
- * Author: Lorenz Meier <lm@inf.ethz.ch>
+ * Copyright (c) 2012-2014 PX4 Development Team. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
@@ -33,10 +32,11 @@
****************************************************************************/
/**
- * @file mavlink_receiver.c
+ * @file mavlink_receiver.cpp
* MAVLink protocol message receive and dispatch
*
* @author Lorenz Meier <lm@inf.ethz.ch>
+ * @author Anton Babushkin <anton.babushkin@me.com>
*/
/* XXX trim includes */
@@ -77,272 +77,334 @@
__BEGIN_DECLS
#include "mavlink_bridge_header.h"
-#include "waypoints.h"
-#include "orb_topics.h"
-#include "missionlib.h"
-#include "mavlink_hil.h"
-#include "mavlink_parameters.h"
-#include "util.h"
-
-extern bool gcs_link;
+#include "mavlink_receiver.h"
+#include "mavlink_main.h"
__END_DECLS
-/* XXX should be in a header somewhere */
-extern "C" pthread_t receive_start(int uart);
-
-static void handle_message(mavlink_message_t *msg);
-static void *receive_thread(void *arg);
-
-static mavlink_status_t status;
-static struct vehicle_vicon_position_s vicon_position;
-static struct vehicle_command_s vcmd;
-static struct offboard_control_setpoint_s offboard_control_sp;
-
-struct vehicle_global_position_s hil_global_pos;
-struct vehicle_local_position_s hil_local_pos;
-struct vehicle_attitude_s hil_attitude;
-struct vehicle_gps_position_s hil_gps;
-struct sensor_combined_s hil_sensors;
-struct battery_status_s hil_battery_status;
-static orb_advert_t pub_hil_global_pos = -1;
-static orb_advert_t pub_hil_local_pos = -1;
-static orb_advert_t pub_hil_attitude = -1;
-static orb_advert_t pub_hil_gps = -1;
-static orb_advert_t pub_hil_sensors = -1;
-static orb_advert_t pub_hil_gyro = -1;
-static orb_advert_t pub_hil_accel = -1;
-static orb_advert_t pub_hil_mag = -1;
-static orb_advert_t pub_hil_baro = -1;
-static orb_advert_t pub_hil_airspeed = -1;
-static orb_advert_t pub_hil_battery = -1;
-
-/* packet counter */
-static int hil_counter = 0;
-static int hil_frames = 0;
-static uint64_t old_timestamp = 0;
-
-static orb_advert_t cmd_pub = -1;
-static orb_advert_t flow_pub = -1;
-
-static orb_advert_t offboard_control_sp_pub = -1;
-static orb_advert_t vicon_position_pub = -1;
-static orb_advert_t telemetry_status_pub = -1;
-
-// variables for HIL reference position
-static int32_t lat0 = 0;
-static int32_t lon0 = 0;
-static double alt0 = 0;
-
-static void
-handle_message(mavlink_message_t *msg)
+static const float mg2ms2 = CONSTANTS_ONE_G / 1000.0f;
+
+MavlinkReceiver::MavlinkReceiver(Mavlink *parent) :
+ _mavlink(parent),
+
+ _global_pos_pub(-1),
+ _local_pos_pub(-1),
+ _attitude_pub(-1),
+ _gps_pub(-1),
+ _sensors_pub(-1),
+ _gyro_pub(-1),
+ _accel_pub(-1),
+ _mag_pub(-1),
+ _baro_pub(-1),
+ _airspeed_pub(-1),
+ _battery_pub(-1),
+ _cmd_pub(-1),
+ _flow_pub(-1),
+ _offboard_control_sp_pub(-1),
+ _local_pos_sp_pub(-1),
+ _global_vel_sp_pub(-1),
+ _att_sp_pub(-1),
+ _rates_sp_pub(-1),
+ _vicon_position_pub(-1),
+ _telemetry_status_pub(-1),
+ _rc_pub(-1),
+ _manual_pub(-1),
+ _telemetry_heartbeat_time(0),
+ _radio_status_available(false),
+ _control_mode_sub(-1),
+ _hil_frames(0),
+ _old_timestamp(0),
+ _hil_local_proj_inited(0),
+ _hil_local_alt0(0.0)
{
- if (msg->msgid == MAVLINK_MSG_ID_COMMAND_LONG) {
+ _control_mode_sub = orb_subscribe(ORB_ID(vehicle_control_mode));
+ memset(&hil_local_pos, 0, sizeof(hil_local_pos));
+ memset(&_control_mode, 0, sizeof(_control_mode));
- mavlink_command_long_t cmd_mavlink;
- mavlink_msg_command_long_decode(msg, &cmd_mavlink);
+ // make sure the FTP server is started
+ (void)MavlinkFTP::getServer();
+}
- if (cmd_mavlink.target_system == mavlink_system.sysid && ((cmd_mavlink.target_component == mavlink_system.compid)
- || (cmd_mavlink.target_component == MAV_COMP_ID_ALL))) {
- //check for MAVLINK terminate command
- if (cmd_mavlink.command == MAV_CMD_PREFLIGHT_REBOOT_SHUTDOWN && ((int)cmd_mavlink.param1) == 3) {
- /* This is the link shutdown command, terminate mavlink */
- printf("[mavlink] Terminating .. \n");
- fflush(stdout);
- usleep(50000);
+MavlinkReceiver::~MavlinkReceiver()
+{
+}
- /* terminate other threads and this thread */
- thread_should_exit = true;
+void
+MavlinkReceiver::handle_message(mavlink_message_t *msg)
+{
+ switch (msg->msgid) {
+ case MAVLINK_MSG_ID_COMMAND_LONG:
+ handle_message_command_long(msg);
+ break;
- } else {
+ case MAVLINK_MSG_ID_OPTICAL_FLOW:
+ handle_message_optical_flow(msg);
+ break;
- /* Copy the content of mavlink_command_long_t cmd_mavlink into command_t cmd */
- vcmd.param1 = cmd_mavlink.param1;
- vcmd.param2 = cmd_mavlink.param2;
- vcmd.param3 = cmd_mavlink.param3;
- vcmd.param4 = cmd_mavlink.param4;
- vcmd.param5 = cmd_mavlink.param5;
- vcmd.param6 = cmd_mavlink.param6;
- vcmd.param7 = cmd_mavlink.param7;
- // XXX do proper translation
- vcmd.command = (enum VEHICLE_CMD)cmd_mavlink.command;
- vcmd.target_system = cmd_mavlink.target_system;
- vcmd.target_component = cmd_mavlink.target_component;
- vcmd.source_system = msg->sysid;
- vcmd.source_component = msg->compid;
- vcmd.confirmation = cmd_mavlink.confirmation;
-
- /* check if topic is advertised */
- if (cmd_pub <= 0) {
- cmd_pub = orb_advertise(ORB_ID(vehicle_command), &vcmd);
+ case MAVLINK_MSG_ID_SET_MODE:
+ handle_message_set_mode(msg);
+ break;
- } else {
- /* publish */
- orb_publish(ORB_ID(vehicle_command), cmd_pub, &vcmd);
- }
- }
- }
- }
+ case MAVLINK_MSG_ID_VICON_POSITION_ESTIMATE:
+ handle_message_vicon_position_estimate(msg);
+ break;
- if (msg->msgid == MAVLINK_MSG_ID_OPTICAL_FLOW) {
- mavlink_optical_flow_t flow;
- mavlink_msg_optical_flow_decode(msg, &flow);
+ case MAVLINK_MSG_ID_SET_QUAD_SWARM_ROLL_PITCH_YAW_THRUST:
+ handle_message_quad_swarm_roll_pitch_yaw_thrust(msg);
+ break;
- struct optical_flow_s f;
+ case MAVLINK_MSG_ID_RADIO_STATUS:
+ handle_message_radio_status(msg);
+ break;
- f.timestamp = flow.time_usec;
- f.flow_raw_x = flow.flow_x;
- f.flow_raw_y = flow.flow_y;
- f.flow_comp_x_m = flow.flow_comp_m_x;
- f.flow_comp_y_m = flow.flow_comp_m_y;
- f.ground_distance_m = flow.ground_distance;
- f.quality = flow.quality;
- f.sensor_id = flow.sensor_id;
+ case MAVLINK_MSG_ID_MANUAL_CONTROL:
+ handle_message_manual_control(msg);
+ break;
- /* check if topic is advertised */
- if (flow_pub <= 0) {
- flow_pub = orb_advertise(ORB_ID(optical_flow), &f);
+ case MAVLINK_MSG_ID_HEARTBEAT:
+ handle_message_heartbeat(msg);
+ break;
- } else {
- /* publish */
- orb_publish(ORB_ID(optical_flow), flow_pub, &f);
- }
- }
+ case MAVLINK_MSG_ID_REQUEST_DATA_STREAM:
+ handle_message_request_data_stream(msg);
+ break;
- if (msg->msgid == MAVLINK_MSG_ID_SET_MODE) {
- /* Set mode on request */
- mavlink_set_mode_t new_mode;
- mavlink_msg_set_mode_decode(msg, &new_mode);
-
- union px4_custom_mode custom_mode;
- custom_mode.data = new_mode.custom_mode;
- /* Copy the content of mavlink_command_long_t cmd_mavlink into command_t cmd */
- vcmd.param1 = new_mode.base_mode;
- vcmd.param2 = custom_mode.main_mode;
- vcmd.param3 = 0;
- vcmd.param4 = 0;
- vcmd.param5 = 0;
- vcmd.param6 = 0;
- vcmd.param7 = 0;
- vcmd.command = VEHICLE_CMD_DO_SET_MODE;
- vcmd.target_system = new_mode.target_system;
- vcmd.target_component = MAV_COMP_ID_ALL;
- vcmd.source_system = msg->sysid;
- vcmd.source_component = msg->compid;
- vcmd.confirmation = 1;
-
- /* check if topic is advertised */
- if (cmd_pub <= 0) {
- cmd_pub = orb_advertise(ORB_ID(vehicle_command), &vcmd);
+ case MAVLINK_MSG_ID_ENCAPSULATED_DATA:
+ MavlinkFTP::getServer()->handle_message(_mavlink, msg);
+ break;
- } else {
- /* create command */
- orb_publish(ORB_ID(vehicle_command), cmd_pub, &vcmd);
- }
+ default:
+ break;
}
- /* Handle Vicon position estimates */
- if (msg->msgid == MAVLINK_MSG_ID_VICON_POSITION_ESTIMATE) {
- mavlink_vicon_position_estimate_t pos;
- mavlink_msg_vicon_position_estimate_decode(msg, &pos);
-
- vicon_position.timestamp = hrt_absolute_time();
-
- vicon_position.x = pos.x;
- vicon_position.y = pos.y;
- vicon_position.z = pos.z;
+ /*
+ * Only decode hil messages in HIL mode.
+ *
+ * The HIL mode is enabled by the HIL bit flag
+ * in the system mode. Either send a set mode
+ * COMMAND_LONG message or a SET_MODE message
+ *
+ * Accept HIL GPS messages if use_hil_gps flag is true.
+ * This allows to provide fake gps measurements to the system.
+ */
+ if (_mavlink->get_hil_enabled()) {
+ switch (msg->msgid) {
+ case MAVLINK_MSG_ID_HIL_SENSOR:
+ handle_message_hil_sensor(msg);
+ break;
+
+ case MAVLINK_MSG_ID_HIL_STATE_QUATERNION:
+ handle_message_hil_state_quaternion(msg);
+ break;
+
+ default:
+ break;
+ }
+ }
- vicon_position.roll = pos.roll;
- vicon_position.pitch = pos.pitch;
- vicon_position.yaw = pos.yaw;
- if (vicon_position_pub <= 0) {
- vicon_position_pub = orb_advertise(ORB_ID(vehicle_vicon_position), &vicon_position);
+ if (_mavlink->get_hil_enabled() || (_mavlink->get_use_hil_gps() && msg->sysid == mavlink_system.sysid)) {
+ switch (msg->msgid) {
+ case MAVLINK_MSG_ID_HIL_GPS:
+ handle_message_hil_gps(msg);
+ break;
- } else {
- orb_publish(ORB_ID(vehicle_vicon_position), vicon_position_pub, &vicon_position);
+ default:
+ break;
}
+
}
- /* Handle quadrotor motor setpoints */
+ /* If we've received a valid message, mark the flag indicating so.
+ This is used in the '-w' command-line flag. */
+ _mavlink->set_has_received_messages(true);
+}
- if (msg->msgid == MAVLINK_MSG_ID_SET_QUAD_SWARM_ROLL_PITCH_YAW_THRUST) {
- mavlink_set_quad_swarm_roll_pitch_yaw_thrust_t quad_motors_setpoint;
- mavlink_msg_set_quad_swarm_roll_pitch_yaw_thrust_decode(msg, &quad_motors_setpoint);
+void
+MavlinkReceiver::handle_message_command_long(mavlink_message_t *msg)
+{
+ /* command */
+ mavlink_command_long_t cmd_mavlink;
+ mavlink_msg_command_long_decode(msg, &cmd_mavlink);
+
+ if (cmd_mavlink.target_system == mavlink_system.sysid && ((cmd_mavlink.target_component == mavlink_system.compid)
+ || (cmd_mavlink.target_component == MAV_COMP_ID_ALL))) {
+ //check for MAVLINK terminate command
+ if (cmd_mavlink.command == MAV_CMD_PREFLIGHT_REBOOT_SHUTDOWN && ((int)cmd_mavlink.param1) == 3) {
+ /* This is the link shutdown command, terminate mavlink */
+ warnx("terminated by remote command");
+ fflush(stdout);
+ usleep(50000);
+
+ /* terminate other threads and this thread */
+ _mavlink->_task_should_exit = true;
- if (mavlink_system.sysid < 4) {
+ } else {
- /* switch to a receiving link mode */
- gcs_link = false;
+ if (msg->sysid == mavlink_system.sysid && msg->compid == mavlink_system.compid) {
+ warnx("ignoring CMD spoofed with same SYS/COMP ID");
+ return;
+ }
- /*
- * rate control mode - defined by MAVLink
- */
+ struct vehicle_command_s vcmd;
+ memset(&vcmd, 0, sizeof(vcmd));
+
+ /* Copy the content of mavlink_command_long_t cmd_mavlink into command_t cmd */
+ vcmd.param1 = cmd_mavlink.param1;
+ vcmd.param2 = cmd_mavlink.param2;
+ vcmd.param3 = cmd_mavlink.param3;
+ vcmd.param4 = cmd_mavlink.param4;
+ vcmd.param5 = cmd_mavlink.param5;
+ vcmd.param6 = cmd_mavlink.param6;
+ vcmd.param7 = cmd_mavlink.param7;
+ // XXX do proper translation
+ vcmd.command = (enum VEHICLE_CMD)cmd_mavlink.command;
+ vcmd.target_system = cmd_mavlink.target_system;
+ vcmd.target_component = cmd_mavlink.target_component;
+ vcmd.source_system = msg->sysid;
+ vcmd.source_component = msg->compid;
+ vcmd.confirmation = cmd_mavlink.confirmation;
+
+ if (_cmd_pub < 0) {
+ _cmd_pub = orb_advertise(ORB_ID(vehicle_command), &vcmd);
- uint8_t ml_mode = 0;
- bool ml_armed = false;
+ } else {
+ orb_publish(ORB_ID(vehicle_command), _cmd_pub, &vcmd);
+ }
+ }
+ }
+}
- switch (quad_motors_setpoint.mode) {
- case 0:
- ml_armed = false;
- break;
+void
+MavlinkReceiver::handle_message_optical_flow(mavlink_message_t *msg)
+{
+ /* optical flow */
+ mavlink_optical_flow_t flow;
+ mavlink_msg_optical_flow_decode(msg, &flow);
+
+ struct optical_flow_s f;
+ memset(&f, 0, sizeof(f));
+
+ f.timestamp = hrt_absolute_time();
+ f.flow_timestamp = flow.time_usec;
+ f.flow_raw_x = flow.flow_x;
+ f.flow_raw_y = flow.flow_y;
+ f.flow_comp_x_m = flow.flow_comp_m_x;
+ f.flow_comp_y_m = flow.flow_comp_m_y;
+ f.ground_distance_m = flow.ground_distance;
+ f.quality = flow.quality;
+ f.sensor_id = flow.sensor_id;
+
+ if (_flow_pub < 0) {
+ _flow_pub = orb_advertise(ORB_ID(optical_flow), &f);
+
+ } else {
+ orb_publish(ORB_ID(optical_flow), _flow_pub, &f);
+ }
+}
- case 1:
- ml_mode = OFFBOARD_CONTROL_MODE_DIRECT_RATES;
- ml_armed = true;
+void
+MavlinkReceiver::handle_message_set_mode(mavlink_message_t *msg)
+{
+ mavlink_set_mode_t new_mode;
+ mavlink_msg_set_mode_decode(msg, &new_mode);
+
+ struct vehicle_command_s vcmd;
+ memset(&vcmd, 0, sizeof(vcmd));
+
+ union px4_custom_mode custom_mode;
+ custom_mode.data = new_mode.custom_mode;
+ /* copy the content of mavlink_command_long_t cmd_mavlink into command_t cmd */
+ vcmd.param1 = new_mode.base_mode;
+ vcmd.param2 = custom_mode.main_mode;
+ vcmd.param3 = 0;
+ vcmd.param4 = 0;
+ vcmd.param5 = 0;
+ vcmd.param6 = 0;
+ vcmd.param7 = 0;
+ vcmd.command = VEHICLE_CMD_DO_SET_MODE;
+ vcmd.target_system = new_mode.target_system;
+ vcmd.target_component = MAV_COMP_ID_ALL;
+ vcmd.source_system = msg->sysid;
+ vcmd.source_component = msg->compid;
+ vcmd.confirmation = 1;
+
+ if (_cmd_pub < 0) {
+ _cmd_pub = orb_advertise(ORB_ID(vehicle_command), &vcmd);
+
+ } else {
+ orb_publish(ORB_ID(vehicle_command), _cmd_pub, &vcmd);
+ }
+}
- break;
+void
+MavlinkReceiver::handle_message_vicon_position_estimate(mavlink_message_t *msg)
+{
+ mavlink_vicon_position_estimate_t pos;
+ mavlink_msg_vicon_position_estimate_decode(msg, &pos);
- case 2:
- ml_mode = OFFBOARD_CONTROL_MODE_DIRECT_ATTITUDE;
- ml_armed = true;
+ struct vehicle_vicon_position_s vicon_position;
+ memset(&vicon_position, 0, sizeof(vicon_position));
- break;
+ vicon_position.timestamp = hrt_absolute_time();
+ vicon_position.x = pos.x;
+ vicon_position.y = pos.y;
+ vicon_position.z = pos.z;
+ vicon_position.roll = pos.roll;
+ vicon_position.pitch = pos.pitch;
+ vicon_position.yaw = pos.yaw;
- case 3:
- ml_mode = OFFBOARD_CONTROL_MODE_DIRECT_VELOCITY;
- break;
+ if (_vicon_position_pub < 0) {
+ _vicon_position_pub = orb_advertise(ORB_ID(vehicle_vicon_position), &vicon_position);
- case 4:
- ml_mode = OFFBOARD_CONTROL_MODE_DIRECT_POSITION;
- break;
- }
+ } else {
+ orb_publish(ORB_ID(vehicle_vicon_position), _vicon_position_pub, &vicon_position);
+ }
+}
- offboard_control_sp.p1 = (float)quad_motors_setpoint.roll[mavlink_system.sysid - 1] / (float)INT16_MAX;
- offboard_control_sp.p2 = (float)quad_motors_setpoint.pitch[mavlink_system.sysid - 1] / (float)INT16_MAX;
- offboard_control_sp.p3 = (float)quad_motors_setpoint.yaw[mavlink_system.sysid - 1] / (float)INT16_MAX;
- offboard_control_sp.p4 = (float)quad_motors_setpoint.thrust[mavlink_system.sysid - 1] / (float)UINT16_MAX;
+void
+MavlinkReceiver::handle_message_quad_swarm_roll_pitch_yaw_thrust(mavlink_message_t *msg)
+{
+ mavlink_set_quad_swarm_roll_pitch_yaw_thrust_t swarm_offboard_control;
+ mavlink_msg_set_quad_swarm_roll_pitch_yaw_thrust_decode(msg, &swarm_offboard_control);
- if (quad_motors_setpoint.thrust[mavlink_system.sysid - 1] == 0) {
- ml_armed = false;
- }
+ /* Only accept system IDs from 1 to 4 */
+ if (mavlink_system.sysid >= 1 && mavlink_system.sysid <= 4) {
+ struct offboard_control_setpoint_s offboard_control_sp;
+ memset(&offboard_control_sp, 0, sizeof(offboard_control_sp));
- offboard_control_sp.armed = ml_armed;
- offboard_control_sp.mode = static_cast<enum OFFBOARD_CONTROL_MODE>(ml_mode);
+ /* Convert values * 1000 back */
+ offboard_control_sp.p1 = (float)swarm_offboard_control.roll[mavlink_system.sysid - 1] / 1000.0f;
+ offboard_control_sp.p2 = (float)swarm_offboard_control.pitch[mavlink_system.sysid - 1] / 1000.0f;
+ offboard_control_sp.p3 = (float)swarm_offboard_control.yaw[mavlink_system.sysid - 1] / 1000.0f;
+ offboard_control_sp.p4 = (float)swarm_offboard_control.thrust[mavlink_system.sysid - 1] / 1000.0f;
- offboard_control_sp.timestamp = hrt_absolute_time();
+ offboard_control_sp.mode = (enum OFFBOARD_CONTROL_MODE)swarm_offboard_control.mode;
- /* check if topic has to be advertised */
- if (offboard_control_sp_pub <= 0) {
- offboard_control_sp_pub = orb_advertise(ORB_ID(offboard_control_setpoint), &offboard_control_sp);
+ offboard_control_sp.timestamp = hrt_absolute_time();
- } else {
- /* Publish */
- orb_publish(ORB_ID(offboard_control_setpoint), offboard_control_sp_pub, &offboard_control_sp);
- }
+ if (_offboard_control_sp_pub < 0) {
+ _offboard_control_sp_pub = orb_advertise(ORB_ID(offboard_control_setpoint), &offboard_control_sp);
+
+ } else {
+ orb_publish(ORB_ID(offboard_control_setpoint), _offboard_control_sp_pub, &offboard_control_sp);
}
}
+}
- /* handle status updates of the radio */
- if (msg->msgid == MAVLINK_MSG_ID_RADIO_STATUS) {
-
- struct telemetry_status_s tstatus;
-
+void
+MavlinkReceiver::handle_message_radio_status(mavlink_message_t *msg)
+{
+ /* telemetry status supported only on first TELEMETRY_STATUS_ORB_ID_NUM mavlink channels */
+ if (_mavlink->get_channel() < TELEMETRY_STATUS_ORB_ID_NUM) {
mavlink_radio_status_t rstatus;
mavlink_msg_radio_status_decode(msg, &rstatus);
- /* publish telemetry status topic */
+ struct telemetry_status_s tstatus;
+ memset(&tstatus, 0, sizeof(tstatus));
+
tstatus.timestamp = hrt_absolute_time();
+ tstatus.heartbeat_time = _telemetry_heartbeat_time;
tstatus.type = TELEMETRY_STATUS_RADIO_TYPE_3DR_RADIO;
tstatus.rssi = rstatus.rssi;
tstatus.remote_rssi = rstatus.remrssi;
@@ -352,455 +414,499 @@ handle_message(mavlink_message_t *msg)
tstatus.rxerrors = rstatus.rxerrors;
tstatus.fixed = rstatus.fixed;
- if (telemetry_status_pub == 0) {
- telemetry_status_pub = orb_advertise(ORB_ID(telemetry_status), &tstatus);
+ if (_telemetry_status_pub < 0) {
+ _telemetry_status_pub = orb_advertise(telemetry_status_orb_id[_mavlink->get_channel()], &tstatus);
} else {
- orb_publish(ORB_ID(telemetry_status), telemetry_status_pub, &tstatus);
+ orb_publish(telemetry_status_orb_id[_mavlink->get_channel()], _telemetry_status_pub, &tstatus);
}
- }
-
- /*
- * Only decode hil messages in HIL mode.
- *
- * The HIL mode is enabled by the HIL bit flag
- * in the system mode. Either send a set mode
- * COMMAND_LONG message or a SET_MODE message
- */
- if (mavlink_hil_enabled) {
-
- uint64_t timestamp = hrt_absolute_time();
-
- if (msg->msgid == MAVLINK_MSG_ID_HIL_SENSOR) {
-
- mavlink_hil_sensor_t imu;
- mavlink_msg_hil_sensor_decode(msg, &imu);
-
- /* sensors general */
- hil_sensors.timestamp = hrt_absolute_time();
-
- /* hil gyro */
- static const float mrad2rad = 1.0e-3f;
- hil_sensors.gyro_raw[0] = imu.xgyro / mrad2rad;
- hil_sensors.gyro_raw[1] = imu.ygyro / mrad2rad;
- hil_sensors.gyro_raw[2] = imu.zgyro / mrad2rad;
- hil_sensors.gyro_rad_s[0] = imu.xgyro;
- hil_sensors.gyro_rad_s[1] = imu.ygyro;
- hil_sensors.gyro_rad_s[2] = imu.zgyro;
- hil_sensors.gyro_counter = hil_counter;
-
- /* accelerometer */
- static const float mg2ms2 = 9.8f / 1000.0f;
- hil_sensors.accelerometer_raw[0] = imu.xacc / mg2ms2;
- hil_sensors.accelerometer_raw[1] = imu.yacc / mg2ms2;
- hil_sensors.accelerometer_raw[2] = imu.zacc / mg2ms2;
- hil_sensors.accelerometer_m_s2[0] = imu.xacc;
- hil_sensors.accelerometer_m_s2[1] = imu.yacc;
- hil_sensors.accelerometer_m_s2[2] = imu.zacc;
- hil_sensors.accelerometer_mode = 0; // TODO what is this?
- hil_sensors.accelerometer_range_m_s2 = 32.7f; // int16
- hil_sensors.accelerometer_counter = hil_counter;
-
- /* adc */
- hil_sensors.adc_voltage_v[0] = 0.0f;
- hil_sensors.adc_voltage_v[1] = 0.0f;
- hil_sensors.adc_voltage_v[2] = 0.0f;
-
- /* magnetometer */
- float mga2ga = 1.0e-3f;
- hil_sensors.magnetometer_raw[0] = imu.xmag / mga2ga;
- hil_sensors.magnetometer_raw[1] = imu.ymag / mga2ga;
- hil_sensors.magnetometer_raw[2] = imu.zmag / mga2ga;
- hil_sensors.magnetometer_ga[0] = imu.xmag;
- hil_sensors.magnetometer_ga[1] = imu.ymag;
- hil_sensors.magnetometer_ga[2] = imu.zmag;
- hil_sensors.magnetometer_range_ga = 32.7f; // int16
- hil_sensors.magnetometer_mode = 0; // TODO what is this
- hil_sensors.magnetometer_cuttoff_freq_hz = 50.0f;
- hil_sensors.magnetometer_counter = hil_counter;
-
- /* baro */
- hil_sensors.baro_pres_mbar = imu.abs_pressure;
- hil_sensors.baro_alt_meter = imu.pressure_alt;
- hil_sensors.baro_temp_celcius = imu.temperature;
- hil_sensors.baro_counter = hil_counter;
-
- /* differential pressure */
- hil_sensors.differential_pressure_pa = imu.diff_pressure * 1e2f; //from hPa to Pa
- hil_sensors.differential_pressure_counter = hil_counter;
-
- /* airspeed from differential pressure, ambient pressure and temp */
- struct airspeed_s airspeed;
-
- float ias = calc_indicated_airspeed(hil_sensors.differential_pressure_pa);
- // XXX need to fix this
- float tas = ias;
-
- airspeed.timestamp = hrt_absolute_time();
- airspeed.indicated_airspeed_m_s = ias;
- airspeed.true_airspeed_m_s = tas;
-
- if (pub_hil_airspeed < 0) {
- pub_hil_airspeed = orb_advertise(ORB_ID(airspeed), &airspeed);
+ /* this means that heartbeats alone won't be published to the radio status no more */
+ _radio_status_available = true;
+ }
+}
- } else {
- orb_publish(ORB_ID(airspeed), pub_hil_airspeed, &airspeed);
- }
+void
+MavlinkReceiver::handle_message_manual_control(mavlink_message_t *msg)
+{
+ mavlink_manual_control_t man;
+ mavlink_msg_manual_control_decode(msg, &man);
- //warnx("SENSOR: IAS: %6.2f TAS: %6.2f", airspeed.indicated_airspeed_m_s, airspeed.true_airspeed_m_s);
+ struct manual_control_setpoint_s manual;
+ memset(&manual, 0, sizeof(manual));
- /* individual sensor publications */
- struct gyro_report gyro;
- gyro.x_raw = imu.xgyro / mrad2rad;
- gyro.y_raw = imu.ygyro / mrad2rad;
- gyro.z_raw = imu.zgyro / mrad2rad;
- gyro.x = imu.xgyro;
- gyro.y = imu.ygyro;
- gyro.z = imu.zgyro;
- gyro.temperature = imu.temperature;
- gyro.timestamp = hrt_absolute_time();
+ manual.timestamp = hrt_absolute_time();
+ manual.x = man.x / 1000.0f;
+ manual.y = man.y / 1000.0f;
+ manual.r = man.r / 1000.0f;
+ manual.z = man.z / 1000.0f;
- if (pub_hil_gyro < 0) {
- pub_hil_gyro = orb_advertise(ORB_ID(sensor_gyro), &gyro);
+ warnx("pitch: %.2f, roll: %.2f, yaw: %.2f, throttle: %.2f", (double)manual.x, (double)manual.y, (double)manual.r, (double)manual.z);
- } else {
- orb_publish(ORB_ID(sensor_gyro), pub_hil_gyro, &gyro);
- }
+ if (_manual_pub < 0) {
+ _manual_pub = orb_advertise(ORB_ID(manual_control_setpoint), &manual);
- struct accel_report accel;
+ } else {
+ orb_publish(ORB_ID(manual_control_setpoint), _manual_pub, &manual);
+ }
+}
- accel.x_raw = imu.xacc / mg2ms2;
+void
+MavlinkReceiver::handle_message_heartbeat(mavlink_message_t *msg)
+{
+ /* telemetry status supported only on first TELEMETRY_STATUS_ORB_ID_NUM mavlink channels */
+ if (_mavlink->get_channel() < TELEMETRY_STATUS_ORB_ID_NUM) {
+ mavlink_heartbeat_t hb;
+ mavlink_msg_heartbeat_decode(msg, &hb);
- accel.y_raw = imu.yacc / mg2ms2;
+ /* ignore own heartbeats, accept only heartbeats from GCS */
+ if (msg->sysid != mavlink_system.sysid && hb.type == MAV_TYPE_GCS) {
+ _telemetry_heartbeat_time = hrt_absolute_time();
- accel.z_raw = imu.zacc / mg2ms2;
+ /* if no radio status messages arrive, lets at least publish that heartbeats were received */
+ if (!_radio_status_available) {
- accel.x = imu.xacc;
+ struct telemetry_status_s tstatus;
+ memset(&tstatus, 0, sizeof(tstatus));
- accel.y = imu.yacc;
+ tstatus.timestamp = _telemetry_heartbeat_time;
+ tstatus.heartbeat_time = _telemetry_heartbeat_time;
+ tstatus.type = TELEMETRY_STATUS_RADIO_TYPE_GENERIC;
- accel.z = imu.zacc;
+ if (_telemetry_status_pub < 0) {
+ _telemetry_status_pub = orb_advertise(telemetry_status_orb_id[_mavlink->get_channel()], &tstatus);
- accel.temperature = imu.temperature;
+ } else {
+ orb_publish(telemetry_status_orb_id[_mavlink->get_channel()], _telemetry_status_pub, &tstatus);
+ }
+ }
+ }
+ }
+}
- accel.timestamp = hrt_absolute_time();
+void
+MavlinkReceiver::handle_message_request_data_stream(mavlink_message_t *msg)
+{
+ mavlink_request_data_stream_t req;
+ mavlink_msg_request_data_stream_decode(msg, &req);
- if (pub_hil_accel < 0) {
- pub_hil_accel = orb_advertise(ORB_ID(sensor_accel), &accel);
+ if (req.target_system == mavlink_system.sysid && req.target_component == mavlink_system.compid) {
+ float rate = req.start_stop ? (1000.0f / req.req_message_rate) : 0.0f;
- } else {
- orb_publish(ORB_ID(sensor_accel), pub_hil_accel, &accel);
+ MavlinkStream *stream;
+ LL_FOREACH(_mavlink->get_streams(), stream) {
+ if (req.req_stream_id == stream->get_id()) {
+ _mavlink->configure_stream_threadsafe(stream->get_name(), rate);
}
+ }
+ }
+}
- struct mag_report mag;
-
- mag.x_raw = imu.xmag / mga2ga;
+void
+MavlinkReceiver::handle_message_hil_sensor(mavlink_message_t *msg)
+{
+ mavlink_hil_sensor_t imu;
+ mavlink_msg_hil_sensor_decode(msg, &imu);
- mag.y_raw = imu.ymag / mga2ga;
+ uint64_t timestamp = hrt_absolute_time();
- mag.z_raw = imu.zmag / mga2ga;
+ /* airspeed */
+ {
+ struct airspeed_s airspeed;
+ memset(&airspeed, 0, sizeof(airspeed));
- mag.x = imu.xmag;
+ float ias = calc_indicated_airspeed(imu.diff_pressure * 1e2f);
+ // XXX need to fix this
+ float tas = ias;
- mag.y = imu.ymag;
+ airspeed.timestamp = timestamp;
+ airspeed.indicated_airspeed_m_s = ias;
+ airspeed.true_airspeed_m_s = tas;
- mag.z = imu.zmag;
+ if (_airspeed_pub < 0) {
+ _airspeed_pub = orb_advertise(ORB_ID(airspeed), &airspeed);
- mag.timestamp = hrt_absolute_time();
+ } else {
+ orb_publish(ORB_ID(airspeed), _airspeed_pub, &airspeed);
+ }
+ }
- if (pub_hil_mag < 0) {
- pub_hil_mag = orb_advertise(ORB_ID(sensor_mag), &mag);
+ /* gyro */
+ {
+ struct gyro_report gyro;
+ memset(&gyro, 0, sizeof(gyro));
- } else {
- orb_publish(ORB_ID(sensor_mag), pub_hil_mag, &mag);
- }
+ gyro.timestamp = timestamp;
+ gyro.x_raw = imu.xgyro * 1000.0f;
+ gyro.y_raw = imu.ygyro * 1000.0f;
+ gyro.z_raw = imu.zgyro * 1000.0f;
+ gyro.x = imu.xgyro;
+ gyro.y = imu.ygyro;
+ gyro.z = imu.zgyro;
+ gyro.temperature = imu.temperature;
- struct baro_report baro;
+ if (_gyro_pub < 0) {
+ _gyro_pub = orb_advertise(ORB_ID(sensor_gyro), &gyro);
- baro.pressure = imu.abs_pressure;
+ } else {
+ orb_publish(ORB_ID(sensor_gyro), _gyro_pub, &gyro);
+ }
+ }
- baro.altitude = imu.pressure_alt;
+ /* accelerometer */
+ {
+ struct accel_report accel;
+ memset(&accel, 0, sizeof(accel));
- baro.temperature = imu.temperature;
+ accel.timestamp = timestamp;
+ accel.x_raw = imu.xacc / mg2ms2;
+ accel.y_raw = imu.yacc / mg2ms2;
+ accel.z_raw = imu.zacc / mg2ms2;
+ accel.x = imu.xacc;
+ accel.y = imu.yacc;
+ accel.z = imu.zacc;
+ accel.temperature = imu.temperature;
- baro.timestamp = hrt_absolute_time();
+ if (_accel_pub < 0) {
+ _accel_pub = orb_advertise(ORB_ID(sensor_accel), &accel);
- if (pub_hil_baro < 0) {
- pub_hil_baro = orb_advertise(ORB_ID(sensor_baro), &baro);
+ } else {
+ orb_publish(ORB_ID(sensor_accel), _accel_pub, &accel);
+ }
+ }
- } else {
- orb_publish(ORB_ID(sensor_baro), pub_hil_baro, &baro);
- }
+ /* magnetometer */
+ {
+ struct mag_report mag;
+ memset(&mag, 0, sizeof(mag));
- /* publish combined sensor topic */
- if (pub_hil_sensors > 0) {
- orb_publish(ORB_ID(sensor_combined), pub_hil_sensors, &hil_sensors);
+ mag.timestamp = timestamp;
+ mag.x_raw = imu.xmag * 1000.0f;
+ mag.y_raw = imu.ymag * 1000.0f;
+ mag.z_raw = imu.zmag * 1000.0f;
+ mag.x = imu.xmag;
+ mag.y = imu.ymag;
+ mag.z = imu.zmag;
- } else {
- pub_hil_sensors = orb_advertise(ORB_ID(sensor_combined), &hil_sensors);
- }
+ if (_mag_pub < 0) {
+ _mag_pub = orb_advertise(ORB_ID(sensor_mag), &mag);
- /* fill in HIL battery status */
- hil_battery_status.timestamp = hrt_absolute_time();
- hil_battery_status.voltage_v = 11.1f;
- hil_battery_status.current_a = 10.0f;
+ } else {
+ orb_publish(ORB_ID(sensor_mag), _mag_pub, &mag);
+ }
+ }
- /* lazily publish the battery voltage */
- if (pub_hil_battery > 0) {
- orb_publish(ORB_ID(battery_status), pub_hil_battery, &hil_battery_status);
+ /* baro */
+ {
+ struct baro_report baro;
+ memset(&baro, 0, sizeof(baro));
- } else {
- pub_hil_battery = orb_advertise(ORB_ID(battery_status), &hil_battery_status);
- }
+ baro.timestamp = timestamp;
+ baro.pressure = imu.abs_pressure;
+ baro.altitude = imu.pressure_alt;
+ baro.temperature = imu.temperature;
- // increment counters
- hil_counter++;
- hil_frames++;
+ if (_baro_pub < 0) {
+ _baro_pub = orb_advertise(ORB_ID(sensor_baro), &baro);
- // output
- if ((timestamp - old_timestamp) > 10000000) {
- printf("receiving hil sensor at %d hz\n", hil_frames / 10);
- old_timestamp = timestamp;
- hil_frames = 0;
- }
+ } else {
+ orb_publish(ORB_ID(sensor_baro), _baro_pub, &baro);
}
+ }
- if (msg->msgid == MAVLINK_MSG_ID_HIL_GPS) {
-
- mavlink_hil_gps_t gps;
- mavlink_msg_hil_gps_decode(msg, &gps);
-
- /* gps */
- hil_gps.timestamp_position = gps.time_usec;
- hil_gps.time_gps_usec = gps.time_usec;
- hil_gps.lat = gps.lat;
- hil_gps.lon = gps.lon;
- hil_gps.alt = gps.alt;
- hil_gps.eph_m = (float)gps.eph * 1e-2f; // from cm to m
- hil_gps.epv_m = (float)gps.epv * 1e-2f; // from cm to m
- hil_gps.s_variance_m_s = 5.0f;
- hil_gps.p_variance_m = hil_gps.eph_m * hil_gps.eph_m;
- hil_gps.vel_m_s = (float)gps.vel * 1e-2f; // from cm/s to m/s
-
- /* gps.cog is in degrees 0..360 * 100, heading is -PI..+PI */
- float heading_rad = gps.cog * M_DEG_TO_RAD_F * 1e-2f;
-
- /* go back to -PI..PI */
- if (heading_rad > M_PI_F)
- heading_rad -= 2.0f * M_PI_F;
-
- hil_gps.vel_n_m_s = gps.vn * 1e-2f; // from cm to m
- hil_gps.vel_e_m_s = gps.ve * 1e-2f; // from cm to m
- hil_gps.vel_d_m_s = gps.vd * 1e-2f; // from cm to m
- hil_gps.vel_ned_valid = true;
- /* COG (course over ground) is spec'ed as -PI..+PI */
- hil_gps.cog_rad = heading_rad;
- hil_gps.fix_type = gps.fix_type;
- hil_gps.satellites_visible = gps.satellites_visible;
-
- /* publish GPS measurement data */
- if (pub_hil_gps > 0) {
- orb_publish(ORB_ID(vehicle_gps_position), pub_hil_gps, &hil_gps);
-
- } else {
- pub_hil_gps = orb_advertise(ORB_ID(vehicle_gps_position), &hil_gps);
- }
+ /* sensor combined */
+ {
+ struct sensor_combined_s hil_sensors;
+ memset(&hil_sensors, 0, sizeof(hil_sensors));
+
+ hil_sensors.timestamp = timestamp;
+
+ hil_sensors.gyro_raw[0] = imu.xgyro * 1000.0f;
+ hil_sensors.gyro_raw[1] = imu.ygyro * 1000.0f;
+ hil_sensors.gyro_raw[2] = imu.zgyro * 1000.0f;
+ hil_sensors.gyro_rad_s[0] = imu.xgyro;
+ hil_sensors.gyro_rad_s[1] = imu.ygyro;
+ hil_sensors.gyro_rad_s[2] = imu.zgyro;
+
+ hil_sensors.accelerometer_raw[0] = imu.xacc / mg2ms2;
+ hil_sensors.accelerometer_raw[1] = imu.yacc / mg2ms2;
+ hil_sensors.accelerometer_raw[2] = imu.zacc / mg2ms2;
+ hil_sensors.accelerometer_m_s2[0] = imu.xacc;
+ hil_sensors.accelerometer_m_s2[1] = imu.yacc;
+ hil_sensors.accelerometer_m_s2[2] = imu.zacc;
+ hil_sensors.accelerometer_mode = 0; // TODO what is this?
+ hil_sensors.accelerometer_range_m_s2 = 32.7f; // int16
+ hil_sensors.accelerometer_timestamp = timestamp;
+
+ hil_sensors.adc_voltage_v[0] = 0.0f;
+ hil_sensors.adc_voltage_v[1] = 0.0f;
+ hil_sensors.adc_voltage_v[2] = 0.0f;
+
+ hil_sensors.magnetometer_raw[0] = imu.xmag * 1000.0f;
+ hil_sensors.magnetometer_raw[1] = imu.ymag * 1000.0f;
+ hil_sensors.magnetometer_raw[2] = imu.zmag * 1000.0f;
+ hil_sensors.magnetometer_ga[0] = imu.xmag;
+ hil_sensors.magnetometer_ga[1] = imu.ymag;
+ hil_sensors.magnetometer_ga[2] = imu.zmag;
+ hil_sensors.magnetometer_range_ga = 32.7f; // int16
+ hil_sensors.magnetometer_mode = 0; // TODO what is this
+ hil_sensors.magnetometer_cuttoff_freq_hz = 50.0f;
+ hil_sensors.magnetometer_timestamp = timestamp;
+
+ hil_sensors.baro_pres_mbar = imu.abs_pressure;
+ hil_sensors.baro_alt_meter = imu.pressure_alt;
+ hil_sensors.baro_temp_celcius = imu.temperature;
+ hil_sensors.baro_timestamp = timestamp;
+
+ hil_sensors.differential_pressure_pa = imu.diff_pressure * 1e2f; //from hPa to Pa
+ hil_sensors.differential_pressure_timestamp = timestamp;
+
+ /* publish combined sensor topic */
+ if (_sensors_pub < 0) {
+ _sensors_pub = orb_advertise(ORB_ID(sensor_combined), &hil_sensors);
+ } else {
+ orb_publish(ORB_ID(sensor_combined), _sensors_pub, &hil_sensors);
}
+ }
- if (msg->msgid == MAVLINK_MSG_ID_HIL_STATE_QUATERNION) {
-
- mavlink_hil_state_quaternion_t hil_state;
- mavlink_msg_hil_state_quaternion_decode(msg, &hil_state);
-
- struct airspeed_s airspeed;
- airspeed.timestamp = hrt_absolute_time();
- airspeed.indicated_airspeed_m_s = hil_state.ind_airspeed * 1e-2f;
- airspeed.true_airspeed_m_s = hil_state.true_airspeed * 1e-2f;
-
- if (pub_hil_airspeed < 0) {
- pub_hil_airspeed = orb_advertise(ORB_ID(airspeed), &airspeed);
+ /* battery status */
+ {
+ struct battery_status_s hil_battery_status;
+ memset(&hil_battery_status, 0, sizeof(hil_battery_status));
- } else {
- orb_publish(ORB_ID(airspeed), pub_hil_airspeed, &airspeed);
- }
+ hil_battery_status.timestamp = timestamp;
+ hil_battery_status.voltage_v = 11.1f;
+ hil_battery_status.voltage_filtered_v = 11.1f;
+ hil_battery_status.current_a = 10.0f;
+ hil_battery_status.discharged_mah = -1.0f;
- uint64_t timestamp = hrt_absolute_time();
-
- // publish global position
- if (pub_hil_global_pos > 0) {
- orb_publish(ORB_ID(vehicle_global_position), pub_hil_global_pos, &hil_global_pos);
- // global position packet
- hil_global_pos.timestamp = timestamp;
- hil_global_pos.valid = true;
- hil_global_pos.lat = hil_state.lat;
- hil_global_pos.lon = hil_state.lon;
- hil_global_pos.alt = hil_state.alt / 1000.0f;
- hil_global_pos.vx = hil_state.vx / 100.0f;
- hil_global_pos.vy = hil_state.vy / 100.0f;
- hil_global_pos.vz = hil_state.vz / 100.0f;
+ if (_battery_pub < 0) {
+ _battery_pub = orb_advertise(ORB_ID(battery_status), &hil_battery_status);
- } else {
- pub_hil_global_pos = orb_advertise(ORB_ID(vehicle_global_position), &hil_global_pos);
- }
-
- // publish local position
- if (pub_hil_local_pos > 0) {
- float x;
- float y;
- bool landed = hil_state.alt/1000.0f < (alt0 + 0.1); // XXX improve?
- double lat = hil_state.lat*1e-7;
- double lon = hil_state.lon*1e-7;
- map_projection_project(lat, lon, &x, &y);
- hil_local_pos.timestamp = timestamp;
- hil_local_pos.xy_valid = true;
- hil_local_pos.z_valid = true;
- hil_local_pos.v_xy_valid = true;
- hil_local_pos.v_z_valid = true;
- hil_local_pos.x = x;
- hil_local_pos.y = y;
- hil_local_pos.z = alt0 - hil_state.alt/1000.0f;
- hil_local_pos.vx = hil_state.vx/100.0f;
- hil_local_pos.vy = hil_state.vy/100.0f;
- hil_local_pos.vz = hil_state.vz/100.0f;
- hil_local_pos.yaw = hil_attitude.yaw;
- hil_local_pos.xy_global = true;
- hil_local_pos.z_global = true;
- hil_local_pos.ref_timestamp = timestamp;
- hil_local_pos.ref_lat = hil_state.lat;
- hil_local_pos.ref_lon = hil_state.lon;
- hil_local_pos.ref_alt = alt0;
- hil_local_pos.landed = landed;
- orb_publish(ORB_ID(vehicle_local_position), pub_hil_local_pos, &hil_local_pos);
- } else {
- pub_hil_local_pos = orb_advertise(ORB_ID(vehicle_local_position), &hil_local_pos);
- lat0 = hil_state.lat;
- lon0 = hil_state.lon;
- alt0 = hil_state.alt / 1000.0f;
- map_projection_init(hil_state.lat, hil_state.lon);
- }
+ } else {
+ orb_publish(ORB_ID(battery_status), _battery_pub, &hil_battery_status);
+ }
+ }
- /* Calculate Rotation Matrix */
- math::Quaternion q(hil_state.attitude_quaternion);
- math::Dcm C_nb(q);
- math::EulerAngles euler(C_nb);
+ /* increment counters */
+ _hil_frames++;
- /* set rotation matrix */
- for (int i = 0; i < 3; i++) for (int j = 0; j < 3; j++)
- hil_attitude.R[i][j] = C_nb(i, j);
+ /* print HIL sensors rate */
+ if ((timestamp - _old_timestamp) > 10000000) {
+ printf("receiving HIL sensors at %d hz\n", _hil_frames / 10);
+ _old_timestamp = timestamp;
+ _hil_frames = 0;
+ }
+}
- hil_attitude.R_valid = true;
+void
+MavlinkReceiver::handle_message_hil_gps(mavlink_message_t *msg)
+{
+ mavlink_hil_gps_t gps;
+ mavlink_msg_hil_gps_decode(msg, &gps);
- /* set quaternion */
- hil_attitude.q[0] = q(0);
- hil_attitude.q[1] = q(1);
- hil_attitude.q[2] = q(2);
- hil_attitude.q[3] = q(3);
- hil_attitude.q_valid = true;
+ uint64_t timestamp = hrt_absolute_time();
- hil_attitude.roll = euler.getPhi();
- hil_attitude.pitch = euler.getTheta();
- hil_attitude.yaw = euler.getPsi();
- hil_attitude.rollspeed = hil_state.rollspeed;
- hil_attitude.pitchspeed = hil_state.pitchspeed;
- hil_attitude.yawspeed = hil_state.yawspeed;
+ struct vehicle_gps_position_s hil_gps;
+ memset(&hil_gps, 0, sizeof(hil_gps));
- /* set timestamp and notify processes (broadcast) */
- hil_attitude.timestamp = hrt_absolute_time();
+ hil_gps.timestamp_time = timestamp;
+ hil_gps.time_gps_usec = gps.time_usec;
- if (pub_hil_attitude > 0) {
- orb_publish(ORB_ID(vehicle_attitude), pub_hil_attitude, &hil_attitude);
+ hil_gps.timestamp_position = timestamp;
+ hil_gps.lat = gps.lat;
+ hil_gps.lon = gps.lon;
+ hil_gps.alt = gps.alt;
+ hil_gps.eph = (float)gps.eph * 1e-2f; // from cm to m
+ hil_gps.epv = (float)gps.epv * 1e-2f; // from cm to m
- } else {
- pub_hil_attitude = orb_advertise(ORB_ID(vehicle_attitude), &hil_attitude);
- }
+ hil_gps.timestamp_variance = timestamp;
+ hil_gps.s_variance_m_s = 5.0f;
- struct accel_report accel;
+ hil_gps.timestamp_velocity = timestamp;
+ hil_gps.vel_m_s = (float)gps.vel * 1e-2f; // from cm/s to m/s
+ hil_gps.vel_n_m_s = gps.vn * 1e-2f; // from cm to m
+ hil_gps.vel_e_m_s = gps.ve * 1e-2f; // from cm to m
+ hil_gps.vel_d_m_s = gps.vd * 1e-2f; // from cm to m
+ hil_gps.vel_ned_valid = true;
+ hil_gps.cog_rad = _wrap_pi(gps.cog * M_DEG_TO_RAD_F * 1e-2f);
- accel.x_raw = hil_state.xacc / 9.81f * 1e3f;
+ hil_gps.fix_type = gps.fix_type;
+ hil_gps.satellites_used = gps.satellites_visible; //TODO: rename mavlink_hil_gps_t sats visible to used?
- accel.y_raw = hil_state.yacc / 9.81f * 1e3f;
+ if (_gps_pub < 0) {
+ _gps_pub = orb_advertise(ORB_ID(vehicle_gps_position), &hil_gps);
- accel.z_raw = hil_state.zacc / 9.81f * 1e3f;
-
- accel.x = hil_state.xacc;
-
- accel.y = hil_state.yacc;
+ } else {
+ orb_publish(ORB_ID(vehicle_gps_position), _gps_pub, &hil_gps);
+ }
+}
- accel.z = hil_state.zacc;
+void
+MavlinkReceiver::handle_message_hil_state_quaternion(mavlink_message_t *msg)
+{
+ mavlink_hil_state_quaternion_t hil_state;
+ mavlink_msg_hil_state_quaternion_decode(msg, &hil_state);
- accel.temperature = 25.0f;
+ uint64_t timestamp = hrt_absolute_time();
- accel.timestamp = hrt_absolute_time();
+ /* airspeed */
+ {
+ struct airspeed_s airspeed;
+ memset(&airspeed, 0, sizeof(airspeed));
- if (pub_hil_accel < 0) {
- pub_hil_accel = orb_advertise(ORB_ID(sensor_accel), &accel);
+ airspeed.timestamp = timestamp;
+ airspeed.indicated_airspeed_m_s = hil_state.ind_airspeed * 1e-2f;
+ airspeed.true_airspeed_m_s = hil_state.true_airspeed * 1e-2f;
- } else {
- orb_publish(ORB_ID(sensor_accel), pub_hil_accel, &accel);
- }
+ if (_airspeed_pub < 0) {
+ _airspeed_pub = orb_advertise(ORB_ID(airspeed), &airspeed);
- /* fill in HIL battery status */
- hil_battery_status.timestamp = hrt_absolute_time();
- hil_battery_status.voltage_v = 11.1f;
- hil_battery_status.current_a = 10.0f;
+ } else {
+ orb_publish(ORB_ID(airspeed), _airspeed_pub, &airspeed);
+ }
+ }
- /* lazily publish the battery voltage */
- if (pub_hil_battery > 0) {
- orb_publish(ORB_ID(battery_status), pub_hil_battery, &hil_battery_status);
+ /* attitude */
+ struct vehicle_attitude_s hil_attitude;
+ {
+ memset(&hil_attitude, 0, sizeof(hil_attitude));
+ math::Quaternion q(hil_state.attitude_quaternion);
+ math::Matrix<3, 3> C_nb = q.to_dcm();
+ math::Vector<3> euler = C_nb.to_euler();
+
+ hil_attitude.timestamp = timestamp;
+ memcpy(hil_attitude.R, C_nb.data, sizeof(hil_attitude.R));
+ hil_attitude.R_valid = true;
+
+ hil_attitude.q[0] = q(0);
+ hil_attitude.q[1] = q(1);
+ hil_attitude.q[2] = q(2);
+ hil_attitude.q[3] = q(3);
+ hil_attitude.q_valid = true;
+
+ hil_attitude.roll = euler(0);
+ hil_attitude.pitch = euler(1);
+ hil_attitude.yaw = euler(2);
+ hil_attitude.rollspeed = hil_state.rollspeed;
+ hil_attitude.pitchspeed = hil_state.pitchspeed;
+ hil_attitude.yawspeed = hil_state.yawspeed;
+
+ if (_attitude_pub < 0) {
+ _attitude_pub = orb_advertise(ORB_ID(vehicle_attitude), &hil_attitude);
- } else {
- pub_hil_battery = orb_advertise(ORB_ID(battery_status), &hil_battery_status);
- }
+ } else {
+ orb_publish(ORB_ID(vehicle_attitude), _attitude_pub, &hil_attitude);
}
+ }
- if (msg->msgid == MAVLINK_MSG_ID_MANUAL_CONTROL) {
- mavlink_manual_control_t man;
- mavlink_msg_manual_control_decode(msg, &man);
-
- struct rc_channels_s rc_hil;
- memset(&rc_hil, 0, sizeof(rc_hil));
- static orb_advert_t rc_pub = 0;
+ /* global position */
+ {
+ struct vehicle_global_position_s hil_global_pos;
+ memset(&hil_global_pos, 0, sizeof(hil_global_pos));
+
+ hil_global_pos.timestamp = timestamp;
+ hil_global_pos.lat = hil_state.lat;
+ hil_global_pos.lon = hil_state.lon;
+ hil_global_pos.alt = hil_state.alt / 1000.0f;
+ hil_global_pos.vel_n = hil_state.vx / 100.0f;
+ hil_global_pos.vel_e = hil_state.vy / 100.0f;
+ hil_global_pos.vel_d = hil_state.vz / 100.0f;
+ hil_global_pos.yaw = hil_attitude.yaw;
+ hil_global_pos.eph = 2.0f;
+ hil_global_pos.epv = 4.0f;
+
+ if (_global_pos_pub < 0) {
+ _global_pos_pub = orb_advertise(ORB_ID(vehicle_global_position), &hil_global_pos);
- rc_hil.timestamp = hrt_absolute_time();
- rc_hil.chan_count = 4;
+ } else {
+ orb_publish(ORB_ID(vehicle_global_position), _global_pos_pub, &hil_global_pos);
+ }
+ }
- rc_hil.chan[0].scaled = man.x / 1000.0f;
- rc_hil.chan[1].scaled = man.y / 1000.0f;
- rc_hil.chan[2].scaled = man.r / 1000.0f;
- rc_hil.chan[3].scaled = man.z / 1000.0f;
+ /* local position */
+ {
+ double lat = hil_state.lat * 1e-7;
+ double lon = hil_state.lon * 1e-7;
+
+ if (!_hil_local_proj_inited) {
+ _hil_local_proj_inited = true;
+ _hil_local_alt0 = hil_state.alt / 1000.0f;
+ map_projection_init(&_hil_local_proj_ref, hil_state.lat, hil_state.lon);
+ hil_local_pos.ref_timestamp = timestamp;
+ hil_local_pos.ref_lat = lat;
+ hil_local_pos.ref_lon = lon;
+ hil_local_pos.ref_alt = _hil_local_alt0;
+ }
- struct manual_control_setpoint_s mc;
- static orb_advert_t mc_pub = 0;
+ float x;
+ float y;
+ map_projection_project(&_hil_local_proj_ref, lat, lon, &x, &y);
+ hil_local_pos.timestamp = timestamp;
+ hil_local_pos.xy_valid = true;
+ hil_local_pos.z_valid = true;
+ hil_local_pos.v_xy_valid = true;
+ hil_local_pos.v_z_valid = true;
+ hil_local_pos.x = x;
+ hil_local_pos.y = y;
+ hil_local_pos.z = _hil_local_alt0 - hil_state.alt / 1000.0f;
+ hil_local_pos.vx = hil_state.vx / 100.0f;
+ hil_local_pos.vy = hil_state.vy / 100.0f;
+ hil_local_pos.vz = hil_state.vz / 100.0f;
+ hil_local_pos.yaw = hil_attitude.yaw;
+ hil_local_pos.xy_global = true;
+ hil_local_pos.z_global = true;
+
+ bool landed = (float)(hil_state.alt) / 1000.0f < (_hil_local_alt0 + 0.1f); // XXX improve?
+ hil_local_pos.landed = landed;
+
+ if (_local_pos_pub < 0) {
+ _local_pos_pub = orb_advertise(ORB_ID(vehicle_local_position), &hil_local_pos);
- int manual_sub = orb_subscribe(ORB_ID(manual_control_setpoint));
+ } else {
+ orb_publish(ORB_ID(vehicle_local_position), _local_pos_pub, &hil_local_pos);
+ }
+ }
- /* get a copy first, to prevent altering values that are not sent by the mavlink command */
- orb_copy(ORB_ID(manual_control_setpoint), manual_sub, &mc);
+ /* accelerometer */
+ {
+ struct accel_report accel;
+ memset(&accel, 0, sizeof(accel));
- mc.timestamp = rc_hil.timestamp;
- mc.roll = man.x / 1000.0f;
- mc.pitch = man.y / 1000.0f;
- mc.yaw = man.r / 1000.0f;
- mc.throttle = man.z / 1000.0f;
+ accel.timestamp = timestamp;
+ accel.x_raw = hil_state.xacc / CONSTANTS_ONE_G * 1e3f;
+ accel.y_raw = hil_state.yacc / CONSTANTS_ONE_G * 1e3f;
+ accel.z_raw = hil_state.zacc / CONSTANTS_ONE_G * 1e3f;
+ accel.x = hil_state.xacc;
+ accel.y = hil_state.yacc;
+ accel.z = hil_state.zacc;
+ accel.temperature = 25.0f;
- /* fake RC channels with manual control input from simulator */
+ if (_accel_pub < 0) {
+ _accel_pub = orb_advertise(ORB_ID(sensor_accel), &accel);
+ } else {
+ orb_publish(ORB_ID(sensor_accel), _accel_pub, &accel);
+ }
+ }
- if (rc_pub == 0) {
- rc_pub = orb_advertise(ORB_ID(rc_channels), &rc_hil);
+ /* battery status */
+ {
+ struct battery_status_s hil_battery_status;
+ memset(&hil_battery_status, 0, sizeof(hil_battery_status));
- } else {
- orb_publish(ORB_ID(rc_channels), rc_pub, &rc_hil);
- }
+ hil_battery_status.timestamp = timestamp;
+ hil_battery_status.voltage_v = 11.1f;
+ hil_battery_status.voltage_filtered_v = 11.1f;
+ hil_battery_status.current_a = 10.0f;
+ hil_battery_status.discharged_mah = -1.0f;
- if (mc_pub == 0) {
- mc_pub = orb_advertise(ORB_ID(manual_control_setpoint), &mc);
+ if (_battery_pub < 0) {
+ _battery_pub = orb_advertise(ORB_ID(battery_status), &hil_battery_status);
- } else {
- orb_publish(ORB_ID(manual_control_setpoint), mc_pub, &mc);
- }
+ } else {
+ orb_publish(ORB_ID(battery_status), _battery_pub, &hil_battery_status);
}
}
}
@@ -809,17 +915,20 @@ handle_message(mavlink_message_t *msg)
/**
* Receive data from UART.
*/
-static void *
-receive_thread(void *arg)
+void *
+MavlinkReceiver::receive_thread(void *arg)
{
- int uart_fd = *((int *)arg);
+ int uart_fd = _mavlink->get_uart_fd();
- const int timeout = 1000;
+ const int timeout = 500;
uint8_t buf[32];
mavlink_message_t msg;
- prctl(PR_SET_NAME, "mavlink_uart_rcv", getpid());
+ /* set thread name */
+ char thread_name[24];
+ sprintf(thread_name, "mavlink_rcv_if%d", _mavlink->get_instance_id());
+ prctl(PR_SET_NAME, thread_name, getpid());
struct pollfd fds[1];
fds[0].fd = uart_fd;
@@ -827,27 +936,23 @@ receive_thread(void *arg)
ssize_t nread = 0;
- while (!thread_should_exit) {
+ while (!_mavlink->_task_should_exit) {
if (poll(fds, 1, timeout) > 0) {
- if (nread < sizeof(buf)) {
+
+ /* non-blocking read. read may return negative values */
+ if ((nread = read(uart_fd, buf, sizeof(buf))) < (ssize_t)sizeof(buf)) {
/* to avoid reading very small chunks wait for data before reading */
usleep(1000);
}
- /* non-blocking read. read may return negative values */
- nread = read(uart_fd, buf, sizeof(buf));
-
/* if read failed, this loop won't execute */
for (ssize_t i = 0; i < nread; i++) {
- if (mavlink_parse_char(chan, buf[i], &msg, &status)) {
+ if (mavlink_parse_char(_mavlink->get_channel(), buf[i], &msg, &status)) {
/* handle generic messages and commands */
handle_message(&msg);
- /* handle packet with waypoint component */
- mavlink_wpm_message_handler(&msg, &global_pos, &local_pos);
-
- /* handle packet with parameter component */
- mavlink_pm_message_handler(MAVLINK_COMM_0, &msg);
+ /* handle packet with parent object */
+ _mavlink->handle_message(&msg);
}
}
}
@@ -856,25 +961,40 @@ receive_thread(void *arg)
return NULL;
}
+void MavlinkReceiver::print_status()
+{
+
+}
+
+void *MavlinkReceiver::start_helper(void *context)
+{
+ MavlinkReceiver *rcv = new MavlinkReceiver((Mavlink *)context);
+
+ rcv->receive_thread(NULL);
+
+ delete rcv;
+
+ return nullptr;
+}
+
pthread_t
-receive_start(int uart)
+MavlinkReceiver::receive_start(Mavlink *parent)
{
pthread_attr_t receiveloop_attr;
pthread_attr_init(&receiveloop_attr);
// set to non-blocking read
- int flags = fcntl(uart, F_GETFL, 0);
- fcntl(uart, F_SETFL, flags | O_NONBLOCK);
+ int flags = fcntl(parent->get_uart_fd(), F_GETFL, 0);
+ fcntl(parent->get_uart_fd(), F_SETFL, flags | O_NONBLOCK);
struct sched_param param;
(void)pthread_attr_getschedparam(&receiveloop_attr, &param);
param.sched_priority = SCHED_PRIORITY_MAX - 40;
(void)pthread_attr_setschedparam(&receiveloop_attr, &param);
- pthread_attr_setstacksize(&receiveloop_attr, 3000);
-
+ pthread_attr_setstacksize(&receiveloop_attr, 2900);
pthread_t thread;
- pthread_create(&thread, &receiveloop_attr, receive_thread, &uart);
+ pthread_create(&thread, &receiveloop_attr, MavlinkReceiver::start_helper, (void *)parent);
pthread_attr_destroy(&receiveloop_attr);
return thread;