<commit_before>/****************************************************************************
 *
 *   Copyright (c) 2012-2015 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
 * 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_receiver.cpp
 * MAVLink protocol message receive and dispatch
 *
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 */

/* XXX trim includes */
#include <nuttx/config.h>
#include <unistd.h>
#include <pthread.h>
#include <stdio.h>
#include <math.h>
#include <stdbool.h>
#include <fcntl.h>
#include <mqueue.h>
#include <string.h>
#include <drivers/drv_hrt.h>
#include <drivers/drv_accel.h>
#include <drivers/drv_gyro.h>
#include <drivers/drv_mag.h>
#include <drivers/drv_baro.h>
#include <drivers/drv_range_finder.h>
#include <time.h>
#include <float.h>
#include <unistd.h>
#include <nuttx/sched.h>
#include <sys/prctl.h>
#include <termios.h>
#include <errno.h>
#include <stdlib.h>
#include <poll.h>

#include <mathlib/mathlib.h>

#include <conversion/rotation.h>

#include <systemlib/param/param.h>
#include <systemlib/systemlib.h>
#include <systemlib/err.h>
#include <systemlib/airspeed.h>
#include <mavlink/mavlink_log.h>
#include <commander/px4_custom_mode.h>
#include <geo/geo.h>

__BEGIN_DECLS

#include "mavlink_bridge_header.h"
#include "mavlink_receiver.h"
#include "mavlink_main.h"

__END_DECLS

static const float mg2ms2 = CONSTANTS_ONE_G / 1000.0f;

MavlinkReceiver::MavlinkReceiver(Mavlink *parent) :
	_mavlink(parent),
	status{},
	hil_local_pos{},
	hil_land_detector{},
	_control_mode{},
	_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),
	_range_pub(-1),
	_offboard_control_sp_pub(-1),
	_global_vel_sp_pub(-1),
	_att_sp_pub(-1),
	_rates_sp_pub(-1),
	_force_sp_pub(-1),
	_pos_sp_triplet_pub(-1),
	_vicon_position_pub(-1),
	_vision_position_pub(-1),
	_telemetry_status_pub(-1),
	_rc_pub(-1),
	_manual_pub(-1),
	_land_detector_pub(-1),
	_control_mode_sub(orb_subscribe(ORB_ID(vehicle_control_mode))),
	_hil_frames(0),
	_old_timestamp(0),
	_hil_local_proj_inited(0),
	_hil_local_alt0(0.0f),
	_hil_local_proj_ref{},
	_time_offset_avg_alpha(0.6),
	_time_offset(0)
{

	// make sure the FTP server is started
	(void)MavlinkFTP::get_server();
}

MavlinkReceiver::~MavlinkReceiver()
{
}

void
MavlinkReceiver::handle_message(mavlink_message_t *msg)
{
	switch (msg->msgid) {
	case MAVLINK_MSG_ID_COMMAND_LONG:
		handle_message_command_long(msg);
		break;

	case MAVLINK_MSG_ID_COMMAND_INT:
		handle_message_command_int(msg);
		break;

	case MAVLINK_MSG_ID_OPTICAL_FLOW_RAD:
		handle_message_optical_flow_rad(msg);
		break;

	case MAVLINK_MSG_ID_SET_MODE:
		handle_message_set_mode(msg);
		break;

	case MAVLINK_MSG_ID_VICON_POSITION_ESTIMATE:
		handle_message_vicon_position_estimate(msg);
		break;

	case MAVLINK_MSG_ID_SET_POSITION_TARGET_LOCAL_NED:
		handle_message_set_position_target_local_ned(msg);
		break;

	case MAVLINK_MSG_ID_SET_ATTITUDE_TARGET:
		handle_message_set_attitude_target(msg);
		break;

	case MAVLINK_MSG_ID_VISION_POSITION_ESTIMATE:
		handle_message_vision_position_estimate(msg);
		break;

	case MAVLINK_MSG_ID_RADIO_STATUS:
		handle_message_radio_status(msg);
		break;

	case MAVLINK_MSG_ID_MANUAL_CONTROL:
		handle_message_manual_control(msg);
		break;

	case MAVLINK_MSG_ID_HEARTBEAT:
		handle_message_heartbeat(msg);
		break;

	case MAVLINK_MSG_ID_REQUEST_DATA_STREAM:
		handle_message_request_data_stream(msg);
		break;

	case MAVLINK_MSG_ID_FILE_TRANSFER_PROTOCOL:
		MavlinkFTP::get_server()->handle_message(_mavlink, msg);
		break;

	case MAVLINK_MSG_ID_SYSTEM_TIME:
		handle_message_system_time(msg);
		break;

	case MAVLINK_MSG_ID_TIMESYNC:
		handle_message_timesync(msg);
		break;

	default:
		break;
	}

	/*
	 * 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;

		case MAVLINK_MSG_ID_HIL_OPTICAL_FLOW:
			handle_message_hil_optical_flow(msg);
			break;

		default:
			break;
		}
	}


   	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;

		default:
			break;
		}

	}

    /* 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);
}

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");
			fflush(stdout);
			usleep(50000);

			/* terminate other threads and this thread */
			_mavlink->_task_should_exit = true;

		} else {

			if (msg->sysid == mavlink_system.sysid && msg->compid == mavlink_system.compid) {
				warnx("ignoring CMD with same SYS/COMP (%d/%d) ID",
				      mavlink_system.sysid, mavlink_system.compid);
				return;
			}

			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);

			} else {
				orb_publish(ORB_ID(vehicle_command), _cmd_pub, &vcmd);
			}
		}
	}
}

void
MavlinkReceiver::handle_message_command_int(mavlink_message_t *msg)
{
	/* command */
	mavlink_command_int_t cmd_mavlink;
	mavlink_msg_command_int_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");
			fflush(stdout);
			usleep(50000);

			/* terminate other threads and this thread */
			_mavlink->_task_should_exit = true;

		} else {

			if (msg->sysid == mavlink_system.sysid && msg->compid == mavlink_system.compid) {
				warnx("ignoring CMD with same SYS/COMP (%d/%d) ID",
				      mavlink_system.sysid, mavlink_system.compid);
				return;
			}

			struct vehicle_command_s vcmd;
			memset(&vcmd, 0, sizeof(vcmd));

			/* Copy the content of mavlink_command_int_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;
			/* these are coordinates as 1e7 scaled integers to work around the 32 bit floating point limits */
			vcmd.param5 = ((double)cmd_mavlink.x) / 1e7;
			vcmd.param6 = ((double)cmd_mavlink.y) / 1e7;
			vcmd.param7 = cmd_mavlink.z;
			// 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;

			if (_cmd_pub < 0) {
				_cmd_pub = orb_advertise(ORB_ID(vehicle_command), &vcmd);

			} else {
				orb_publish(ORB_ID(vehicle_command), _cmd_pub, &vcmd);
			}
		}
	}
}

void
MavlinkReceiver::handle_message_optical_flow_rad(mavlink_message_t *msg)
{
	/* optical flow */
	mavlink_optical_flow_rad_t flow;
	mavlink_msg_optical_flow_rad_decode(msg, &flow);

	enum Rotation flow_rot;
	param_get(param_find("SENS_FLOW_ROT"),&flow_rot);

	struct optical_flow_s f;
	memset(&f, 0, sizeof(f));

	f.timestamp = flow.time_usec;
	f.integration_timespan = flow.integration_time_us;
	f.pixel_flow_x_integral = flow.integrated_x;
	f.pixel_flow_y_integral = flow.integrated_y;
	f.gyro_x_rate_integral = flow.integrated_xgyro;
	f.gyro_y_rate_integral = flow.integrated_ygyro;
	f.gyro_z_rate_integral = flow.integrated_zgyro;
	f.time_since_last_sonar_update = flow.time_delta_distance_us;
	f.ground_distance_m = flow.distance;
	f.quality = flow.quality;
	f.sensor_id = flow.sensor_id;
	f.gyro_temperature = flow.temperature;

	/* rotate measurements according to parameter */
	float zeroval = 0.0f;
	rotate_3f(flow_rot, f.pixel_flow_x_integral, f.pixel_flow_y_integral, zeroval);

	if (_flow_pub < 0) {
		_flow_pub = orb_advertise(ORB_ID(optical_flow), &f);

	} else {
		orb_publish(ORB_ID(optical_flow), _flow_pub, &f);
	}
}

void
MavlinkReceiver::handle_message_hil_optical_flow(mavlink_message_t *msg)
{
	/* optical flow */
	mavlink_hil_optical_flow_t flow;
	mavlink_msg_hil_optical_flow_decode(msg, &flow);

	struct optical_flow_s f;
	memset(&f, 0, sizeof(f));

	f.timestamp = hrt_absolute_time(); // XXX we rely on the system time for now and not flow.time_usec;
	f.integration_timespan = flow.integration_time_us;
	f.pixel_flow_x_integral = flow.integrated_x;
	f.pixel_flow_y_integral = flow.integrated_y;
	f.gyro_x_rate_integral = flow.integrated_xgyro;
	f.gyro_y_rate_integral = flow.integrated_ygyro;
	f.gyro_z_rate_integral = flow.integrated_zgyro;
	f.time_since_last_sonar_update = flow.time_delta_distance_us;
	f.ground_distance_m = flow.distance;
	f.quality = flow.quality;
	f.sensor_id = flow.sensor_id;
	f.gyro_temperature = flow.temperature;

	if (_flow_pub < 0) {
		_flow_pub = orb_advertise(ORB_ID(optical_flow), &f);

	} else {
		orb_publish(ORB_ID(optical_flow), _flow_pub, &f);
	}

	/* Use distance value for range finder report */
	struct range_finder_report r;
	memset(&r, 0, sizeof(r));

	r.timestamp = hrt_absolute_time();
	r.error_count = 0;
	r.type = RANGE_FINDER_TYPE_LASER;
	r.distance = flow.distance;
	r.minimum_distance = 0.0f;
	r.maximum_distance = 40.0f; // this is set to match the typical range of real sensors, could be made configurable
	r.valid = (r.distance > r.minimum_distance) && (r.distance < r.maximum_distance);

	if (_range_pub < 0) {
		_range_pub = orb_advertise(ORB_ID(sensor_range_finder), &r);
	} else {
		orb_publish(ORB_ID(sensor_range_finder), _range_pub, &r);
	}
}

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);
	}
}

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);

	struct vehicle_vicon_position_s vicon_position;
	memset(&vicon_position, 0, sizeof(vicon_position));

	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;

	if (_vicon_position_pub < 0) {
		_vicon_position_pub = orb_advertise(ORB_ID(vehicle_vicon_position), &vicon_position);

	} else {
		orb_publish(ORB_ID(vehicle_vicon_position), _vicon_position_pub, &vicon_position);
	}
}

void
MavlinkReceiver::handle_message_set_position_target_local_ned(mavlink_message_t *msg)
{
	mavlink_set_position_target_local_ned_t set_position_target_local_ned;
	mavlink_msg_set_position_target_local_ned_decode(msg, &set_position_target_local_ned);

	struct offboard_control_setpoint_s offboard_control_sp;
	memset(&offboard_control_sp, 0, sizeof(offboard_control_sp));//XXX breaks compatibility with multiple setpoints

	/* Only accept messages which are intended for this system */
	if ((mavlink_system.sysid == set_position_target_local_ned.target_system ||
				set_position_target_local_ned.target_system == 0) &&
			(mavlink_system.compid == set_position_target_local_ned.target_component ||
			 set_position_target_local_ned.target_component == 0)) {

		/* convert mavlink type (local, NED) to uORB offboard control struct */
		switch (set_position_target_local_ned.coordinate_frame) {
			case MAV_FRAME_LOCAL_NED:
				offboard_control_sp.mode = OFFBOARD_CONTROL_MODE_DIRECT_LOCAL_NED;
				break;
			case MAV_FRAME_LOCAL_OFFSET_NED:
				offboard_control_sp.mode = OFFBOARD_CONTROL_MODE_DIRECT_LOCAL_OFFSET_NED;
				break;
			case MAV_FRAME_BODY_NED:
				offboard_control_sp.mode = OFFBOARD_CONTROL_MODE_DIRECT_BODY_NED;
				break;
			case MAV_FRAME_BODY_OFFSET_NED:
				offboard_control_sp.mode = OFFBOARD_CONTROL_MODE_DIRECT_BODY_OFFSET_NED;
				break;
			default:
				/* invalid setpoint, avoid publishing */
				return;
		}
		offboard_control_sp.position[0] = set_position_target_local_ned.x;
		offboard_control_sp.position[1] = set_position_target_local_ned.y;
		offboard_control_sp.position[2] = set_position_target_local_ned.z;
		offboard_control_sp.velocity[0] = set_position_target_local_ned.vx;
		offboard_control_sp.velocity[1] = set_position_target_local_ned.vy;
		offboard_control_sp.velocity[2] = set_position_target_local_ned.vz;
		offboard_control_sp.acceleration[0] = set_position_target_local_ned.afx;
		offboard_control_sp.acceleration[1] = set_position_target_local_ned.afy;
		offboard_control_sp.acceleration[2] = set_position_target_local_ned.afz;
		offboard_control_sp.yaw = set_position_target_local_ned.yaw;
		offboard_control_sp.yaw_rate = set_position_target_local_ned.yaw_rate;
		offboard_control_sp.isForceSetpoint = (bool)(set_position_target_local_ned.type_mask & (1 << 9));

		/* If we are in force control mode, for now set offboard mode to force control */
		if (offboard_control_sp.isForceSetpoint) {
			offboard_control_sp.mode = OFFBOARD_CONTROL_MODE_DIRECT_FORCE;
		}

		/* set ignore flags */
		for (int i = 0; i < 9; i++) {
			offboard_control_sp.ignore &=  ~(1 << i);
			offboard_control_sp.ignore |=  (set_position_target_local_ned.type_mask & (1 << i));
		}

		offboard_control_sp.ignore &=  ~(1 << OFB_IGN_BIT_YAW);
		if (set_position_target_local_ned.type_mask & (1 << 10)) {
			offboard_control_sp.ignore |=  (1 << OFB_IGN_BIT_YAW);
		}

		offboard_control_sp.ignore &=  ~(1 << OFB_IGN_BIT_YAWRATE);
		if (set_position_target_local_ned.type_mask & (1 << 11)) {
			offboard_control_sp.ignore |=  (1 << OFB_IGN_BIT_YAWRATE);
		}

		offboard_control_sp.timestamp = hrt_absolute_time();

		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);
		}

		/* If we are in offboard control mode and offboard control loop through is enabled
		 * also publish the setpoint topic which is read by the controller */
		if (_mavlink->get_forward_externalsp()) {
			bool updated;
			orb_check(_control_mode_sub, &updated);
			if (updated) {
				orb_copy(ORB_ID(vehicle_control_mode), _control_mode_sub, &_control_mode);
			}
			if (_control_mode.flag_control_offboard_enabled) {
				if (offboard_control_sp.isForceSetpoint &&
						offboard_control_sp_ignore_position_all(offboard_control_sp) &&
						offboard_control_sp_ignore_velocity_all(offboard_control_sp)) {
					/* The offboard setpoint is a force setpoint only, directly writing to the force
					 * setpoint topic and not publishing the setpoint triplet topic */
					struct vehicle_force_setpoint_s	force_sp;
					force_sp.x = offboard_control_sp.acceleration[0];
					force_sp.y = offboard_control_sp.acceleration[1];
					force_sp.z = offboard_control_sp.acceleration[2];
					//XXX: yaw
					if (_force_sp_pub < 0) {
						_force_sp_pub = orb_advertise(ORB_ID(vehicle_force_setpoint), &force_sp);
					} else {
						orb_publish(ORB_ID(vehicle_force_setpoint), _force_sp_pub, &force_sp);
					}
				} else {
					/* It's not a pure force setpoint: publish to setpoint triplet  topic */
					struct position_setpoint_triplet_s pos_sp_triplet;
					pos_sp_triplet.previous.valid = false;
					pos_sp_triplet.next.valid = false;
					pos_sp_triplet.current.valid = true;
					pos_sp_triplet.current.type = position_setpoint_s::SETPOINT_TYPE_POSITION; //XXX support others

					/* set the local pos values if the setpoint type is 'local pos' and none
					 * of the local pos fields is set to 'ignore' */
					if (offboard_control_sp.mode == OFFBOARD_CONTROL_MODE_DIRECT_LOCAL_NED &&
							!offboard_control_sp_ignore_position_some(offboard_control_sp)) {
					pos_sp_triplet.current.position_valid = true;
					pos_sp_triplet.current.x = offboard_control_sp.position[0];
					pos_sp_triplet.current.y = offboard_control_sp.position[1];
					pos_sp_triplet.current.z = offboard_control_sp.position[2];
					} else {
						pos_sp_triplet.current.position_valid = false;
					}

					/* set the local vel values if the setpoint type is 'local pos' and none
					 * of the local vel fields is set to 'ignore' */
					if (offboard_control_sp.mode == OFFBOARD_CONTROL_MODE_DIRECT_LOCAL_NED &&
							!offboard_control_sp_ignore_velocity_some(offboard_control_sp)) {
						pos_sp_triplet.current.velocity_valid = true;
						pos_sp_triplet.current.vx = offboard_control_sp.velocity[0];
						pos_sp_triplet.current.vy = offboard_control_sp.velocity[1];
						pos_sp_triplet.current.vz = offboard_control_sp.velocity[2];
					} else {
						pos_sp_triplet.current.velocity_valid = false;
					}

					/* set the local acceleration values if the setpoint type is 'local pos' and none
					 * of the accelerations fields is set to 'ignore' */
					if (offboard_control_sp.mode == OFFBOARD_CONTROL_MODE_DIRECT_LOCAL_NED &&
							!offboard_control_sp_ignore_acceleration_some(offboard_control_sp)) {
						pos_sp_triplet.current.acceleration_valid = true;
						pos_sp_triplet.current.a_x = offboard_control_sp.acceleration[0];
						pos_sp_triplet.current.a_y = offboard_control_sp.acceleration[1];
						pos_sp_triplet.current.a_z = offboard_control_sp.acceleration[2];
						pos_sp_triplet.current.acceleration_is_force =
						offboard_control_sp.isForceSetpoint;

					} else {
						pos_sp_triplet.current.acceleration_valid = false;
					}

					/* set the yaw sp value if the setpoint type is 'local pos' and the yaw
					 * field is not set to 'ignore' */
					if (offboard_control_sp.mode == OFFBOARD_CONTROL_MODE_DIRECT_LOCAL_NED &&
							!offboard_control_sp_ignore_yaw(offboard_control_sp)) {
						pos_sp_triplet.current.yaw_valid = true;
						pos_sp_triplet.current.yaw = offboard_control_sp.yaw;

					} else {
						pos_sp_triplet.current.yaw_valid = false;
					}

					/* set the yawrate sp value if the setpoint type is 'local pos' and the yawrate
					 * field is not set to 'ignore' */
					if (offboard_control_sp.mode == OFFBOARD_CONTROL_MODE_DIRECT_LOCAL_NED &&
							!offboard_control_sp_ignore_yawrate(offboard_control_sp)) {
						pos_sp_triplet.current.yawspeed_valid = true;
						pos_sp_triplet.current.yawspeed = offboard_control_sp.yaw_rate;

					} else {
						pos_sp_triplet.current.yawspeed_valid = false;
					}
					//XXX handle global pos setpoints (different MAV frames)


					if (_pos_sp_triplet_pub < 0) {
						_pos_sp_triplet_pub = orb_advertise(ORB_ID(position_setpoint_triplet),
								&pos_sp_triplet);
					} else {
						orb_publish(ORB_ID(position_setpoint_triplet), _pos_sp_triplet_pub,
								&pos_sp_triplet);
					}

				}

			}

		}
	}
}

void
MavlinkReceiver::handle_message_vision_position_estimate(mavlink_message_t *msg)
{
	mavlink_vision_position_estimate_t pos;
	mavlink_msg_vision_position_estimate_decode(msg, &pos);

	struct vision_position_estimate vision_position;
	memset(&vision_position, 0, sizeof(vision_position));

	// Use the component ID to identify the vision sensor
	vision_position.id = msg->compid;

	vision_position.timestamp_boot = to_hrt(pos.usec); // Synced time
	vision_position.timestamp_computer = pos.usec;
	vision_position.x = pos.x;
	vision_position.y = pos.y;
	vision_position.z = pos.z;

	// XXX fix this
	vision_position.vx = 0.0f;
	vision_position.vy = 0.0f;
	vision_position.vz = 0.0f;

	math::Quaternion q;
	q.from_euler(pos.roll, pos.pitch, pos.yaw);

	vision_position.q[0] = q(0);
	vision_position.q[1] = q(1);
	vision_position.q[2] = q(2);
	vision_position.q[3] = q(3);

	if (_vision_position_pub < 0) {
		_vision_position_pub = orb_advertise(ORB_ID(vision_position_estimate), &vision_position);

	} else {
		orb_publish(ORB_ID(vision_position_estimate), _vision_position_pub, &vision_position);
	}
}

void
MavlinkReceiver::handle_message_set_attitude_target(mavlink_message_t *msg)
{
	mavlink_set_attitude_target_t set_attitude_target;
	mavlink_msg_set_attitude_target_decode(msg, &set_attitude_target);

	struct offboard_control_setpoint_s offboard_control_sp;
	memset(&offboard_control_sp, 0, sizeof(offboard_control_sp)); //XXX breaks compatibility with multiple setpoints

	/* Only accept messages which are intended for this system */
	if ((mavlink_system.sysid == set_attitude_target.target_system ||
				set_attitude_target.target_system == 0) &&
			(mavlink_system.compid == set_attitude_target.target_component ||
			 set_attitude_target.target_component == 0)) {
		for (int i = 0; i < 4; i++) {
			offboard_control_sp.attitude[i] = set_attitude_target.q[i];
		}
		offboard_control_sp.attitude_rate[0] = set_attitude_target.body_roll_rate;
		offboard_control_sp.attitude_rate[1] = set_attitude_target.body_pitch_rate;
		offboard_control_sp.attitude_rate[2] = set_attitude_target.body_yaw_rate;

		/* set correct ignore flags for body rate fields: copy from mavlink message */
		for (int i = 0; i < 3; i++) {
			offboard_control_sp.ignore &=  ~(1 << (i + OFB_IGN_BIT_BODYRATE_X));
			offboard_control_sp.ignore |=  (set_attitude_target.type_mask & (1 << i)) << OFB_IGN_BIT_BODYRATE_X;
		}
		/* set correct ignore flags for thrust field: copy from mavlink message */
		offboard_control_sp.ignore &= ~(1 << OFB_IGN_BIT_THRUST);
		offboard_control_sp.ignore |= ((set_attitude_target.type_mask & (1 << 6)) << OFB_IGN_BIT_THRUST);
		/* set correct ignore flags for attitude field: copy from mavlink message */
		offboard_control_sp.ignore &= ~(1 << OFB_IGN_BIT_ATT);
		offboard_control_sp.ignore |= ((set_attitude_target.type_mask & (1 << 7)) << OFB_IGN_BIT_ATT);


		offboard_control_sp.timestamp = hrt_absolute_time();
		offboard_control_sp.mode =OFFBOARD_CONTROL_MODE_DIRECT_ATTITUDE; //XXX handle rate control mode

		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);
		}

		/* If we are in offboard control mode and offboard control loop through is enabled
		 * also publish the setpoint topic which is read by the controller */
		if (_mavlink->get_forward_externalsp()) {
			bool updated;
			orb_check(_control_mode_sub, &updated);
			if (updated) {
				orb_copy(ORB_ID(vehicle_control_mode), _control_mode_sub, &_control_mode);
			}

			if (_control_mode.flag_control_offboard_enabled) {

				/* Publish attitude setpoint if attitude and thrust ignore bits are not set */
				if (!(offboard_control_sp_ignore_attitude(offboard_control_sp) ||
							offboard_control_sp_ignore_thrust(offboard_control_sp))) {
					struct vehicle_attitude_setpoint_s att_sp;
					att_sp.timestamp = hrt_absolute_time();
					mavlink_quaternion_to_euler(set_attitude_target.q,
							&att_sp.roll_body, &att_sp.pitch_body, &att_sp.yaw_body);
					mavlink_quaternion_to_dcm(set_attitude_target.q, (float(*)[3])att_sp.R_body);
					att_sp.R_valid = true;
					att_sp.thrust = set_attitude_target.thrust;
					memcpy(att_sp.q_d, set_attitude_target.q, sizeof(att_sp.q_d));
					att_sp.q_d_valid = true;
					if (_att_sp_pub < 0) {
						_att_sp_pub = orb_advertise(ORB_ID(vehicle_attitude_setpoint), &att_sp);
					} else {
						orb_publish(ORB_ID(vehicle_attitude_setpoint), _att_sp_pub, &att_sp);
					}
				}

				/* Publish attitude rate setpoint if bodyrate and thrust ignore bits are not set */
				///XXX add support for ignoring individual axes
				if (!(offboard_control_sp_ignore_bodyrates_some(offboard_control_sp) ||
							offboard_control_sp_ignore_thrust(offboard_control_sp))) {
					struct vehicle_rates_setpoint_s rates_sp;
					rates_sp.timestamp = hrt_absolute_time();
					rates_sp.roll = set_attitude_target.body_roll_rate;
					rates_sp.pitch = set_attitude_target.body_pitch_rate;
					rates_sp.yaw = set_attitude_target.body_yaw_rate;
					rates_sp.thrust = set_attitude_target.thrust;

					if (_att_sp_pub < 0) {
						_rates_sp_pub = orb_advertise(ORB_ID(vehicle_rates_setpoint), &rates_sp);
					} else {
						orb_publish(ORB_ID(vehicle_rates_setpoint), _rates_sp_pub, &rates_sp);
					}
				}
			}

		}
	}<commit_msg>fixed 'NaN' yaw setpoint in offboard mode<commit_after>/****************************************************************************
 *
 *   Copyright (c) 2012-2015 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
 * 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_receiver.cpp
 * MAVLink protocol message receive and dispatch
 *
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 */

/* XXX trim includes */
#include <nuttx/config.h>
#include <unistd.h>
#include <pthread.h>
#include <stdio.h>
#include <math.h>
#include <stdbool.h>
#include <fcntl.h>
#include <mqueue.h>
#include <string.h>
#include <drivers/drv_hrt.h>
#include <drivers/drv_accel.h>
#include <drivers/drv_gyro.h>
#include <drivers/drv_mag.h>
#include <drivers/drv_baro.h>
#include <drivers/drv_range_finder.h>
#include <time.h>
#include <float.h>
#include <unistd.h>
#include <nuttx/sched.h>
#include <sys/prctl.h>
#include <termios.h>
#include <errno.h>
#include <stdlib.h>
#include <poll.h>

#include <mathlib/mathlib.h>

#include <conversion/rotation.h>

#include <systemlib/param/param.h>
#include <systemlib/systemlib.h>
#include <systemlib/err.h>
#include <systemlib/airspeed.h>
#include <mavlink/mavlink_log.h>
#include <commander/px4_custom_mode.h>
#include <geo/geo.h>

__BEGIN_DECLS

#include "mavlink_bridge_header.h"
#include "mavlink_receiver.h"
#include "mavlink_main.h"

__END_DECLS

static const float mg2ms2 = CONSTANTS_ONE_G / 1000.0f;

MavlinkReceiver::MavlinkReceiver(Mavlink *parent) :
	_mavlink(parent),
	status{},
	hil_local_pos{},
	hil_land_detector{},
	_control_mode{},
	_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),
	_range_pub(-1),
	_offboard_control_sp_pub(-1),
	_global_vel_sp_pub(-1),
	_att_sp_pub(-1),
	_rates_sp_pub(-1),
	_force_sp_pub(-1),
	_pos_sp_triplet_pub(-1),
	_vicon_position_pub(-1),
	_vision_position_pub(-1),
	_telemetry_status_pub(-1),
	_rc_pub(-1),
	_manual_pub(-1),
	_land_detector_pub(-1),
	_control_mode_sub(orb_subscribe(ORB_ID(vehicle_control_mode))),
	_hil_frames(0),
	_old_timestamp(0),
	_hil_local_proj_inited(0),
	_hil_local_alt0(0.0f),
	_hil_local_proj_ref{},
	_time_offset_avg_alpha(0.6),
	_time_offset(0)
{

	// make sure the FTP server is started
	(void)MavlinkFTP::get_server();
}

MavlinkReceiver::~MavlinkReceiver()
{
}

void
MavlinkReceiver::handle_message(mavlink_message_t *msg)
{
	switch (msg->msgid) {
	case MAVLINK_MSG_ID_COMMAND_LONG:
		handle_message_command_long(msg);
		break;

	case MAVLINK_MSG_ID_COMMAND_INT:
		handle_message_command_int(msg);
		break;

	case MAVLINK_MSG_ID_OPTICAL_FLOW_RAD:
		handle_message_optical_flow_rad(msg);
		break;

	case MAVLINK_MSG_ID_SET_MODE:
		handle_message_set_mode(msg);
		break;

	case MAVLINK_MSG_ID_VICON_POSITION_ESTIMATE:
		handle_message_vicon_position_estimate(msg);
		break;

	case MAVLINK_MSG_ID_SET_POSITION_TARGET_LOCAL_NED:
		handle_message_set_position_target_local_ned(msg);
		break;

	case MAVLINK_MSG_ID_SET_ATTITUDE_TARGET:
		handle_message_set_attitude_target(msg);
		break;

	case MAVLINK_MSG_ID_VISION_POSITION_ESTIMATE:
		handle_message_vision_position_estimate(msg);
		break;

	case MAVLINK_MSG_ID_RADIO_STATUS:
		handle_message_radio_status(msg);
		break;

	case MAVLINK_MSG_ID_MANUAL_CONTROL:
		handle_message_manual_control(msg);
		break;

	case MAVLINK_MSG_ID_HEARTBEAT:
		handle_message_heartbeat(msg);
		break;

	case MAVLINK_MSG_ID_REQUEST_DATA_STREAM:
		handle_message_request_data_stream(msg);
		break;

	case MAVLINK_MSG_ID_FILE_TRANSFER_PROTOCOL:
		MavlinkFTP::get_server()->handle_message(_mavlink, msg);
		break;

	case MAVLINK_MSG_ID_SYSTEM_TIME:
		handle_message_system_time(msg);
		break;

	case MAVLINK_MSG_ID_TIMESYNC:
		handle_message_timesync(msg);
		break;

	default:
		break;
	}

	/*
	 * 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;

		case MAVLINK_MSG_ID_HIL_OPTICAL_FLOW:
			handle_message_hil_optical_flow(msg);
			break;

		default:
			break;
		}
	}


   	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;

		default:
			break;
		}

	}

    /* 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);
}

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");
			fflush(stdout);
			usleep(50000);

			/* terminate other threads and this thread */
			_mavlink->_task_should_exit = true;

		} else {

			if (msg->sysid == mavlink_system.sysid && msg->compid == mavlink_system.compid) {
				warnx("ignoring CMD with same SYS/COMP (%d/%d) ID",
				      mavlink_system.sysid, mavlink_system.compid);
				return;
			}

			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);

			} else {
				orb_publish(ORB_ID(vehicle_command), _cmd_pub, &vcmd);
			}
		}
	}
}

void
MavlinkReceiver::handle_message_command_int(mavlink_message_t *msg)
{
	/* command */
	mavlink_command_int_t cmd_mavlink;
	mavlink_msg_command_int_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");
			fflush(stdout);
			usleep(50000);

			/* terminate other threads and this thread */
			_mavlink->_task_should_exit = true;

		} else {

			if (msg->sysid == mavlink_system.sysid && msg->compid == mavlink_system.compid) {
				warnx("ignoring CMD with same SYS/COMP (%d/%d) ID",
				      mavlink_system.sysid, mavlink_system.compid);
				return;
			}

			struct vehicle_command_s vcmd;
			memset(&vcmd, 0, sizeof(vcmd));

			/* Copy the content of mavlink_command_int_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;
			/* these are coordinates as 1e7 scaled integers to work around the 32 bit floating point limits */
			vcmd.param5 = ((double)cmd_mavlink.x) / 1e7;
			vcmd.param6 = ((double)cmd_mavlink.y) / 1e7;
			vcmd.param7 = cmd_mavlink.z;
			// 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;

			if (_cmd_pub < 0) {
				_cmd_pub = orb_advertise(ORB_ID(vehicle_command), &vcmd);

			} else {
				orb_publish(ORB_ID(vehicle_command), _cmd_pub, &vcmd);
			}
		}
	}
}

void
MavlinkReceiver::handle_message_optical_flow_rad(mavlink_message_t *msg)
{
	/* optical flow */
	mavlink_optical_flow_rad_t flow;
	mavlink_msg_optical_flow_rad_decode(msg, &flow);

	enum Rotation flow_rot;
	param_get(param_find("SENS_FLOW_ROT"),&flow_rot);

	struct optical_flow_s f;
	memset(&f, 0, sizeof(f));

	f.timestamp = flow.time_usec;
	f.integration_timespan = flow.integration_time_us;
	f.pixel_flow_x_integral = flow.integrated_x;
	f.pixel_flow_y_integral = flow.integrated_y;
	f.gyro_x_rate_integral = flow.integrated_xgyro;
	f.gyro_y_rate_integral = flow.integrated_ygyro;
	f.gyro_z_rate_integral = flow.integrated_zgyro;
	f.time_since_last_sonar_update = flow.time_delta_distance_us;
	f.ground_distance_m = flow.distance;
	f.quality = flow.quality;
	f.sensor_id = flow.sensor_id;
	f.gyro_temperature = flow.temperature;

	/* rotate measurements according to parameter */
	float zeroval = 0.0f;
	rotate_3f(flow_rot, f.pixel_flow_x_integral, f.pixel_flow_y_integral, zeroval);

	if (_flow_pub < 0) {
		_flow_pub = orb_advertise(ORB_ID(optical_flow), &f);

	} else {
		orb_publish(ORB_ID(optical_flow), _flow_pub, &f);
	}
}

void
MavlinkReceiver::handle_message_hil_optical_flow(mavlink_message_t *msg)
{
	/* optical flow */
	mavlink_hil_optical_flow_t flow;
	mavlink_msg_hil_optical_flow_decode(msg, &flow);

	struct optical_flow_s f;
	memset(&f, 0, sizeof(f));

	f.timestamp = hrt_absolute_time(); // XXX we rely on the system time for now and not flow.time_usec;
	f.integration_timespan = flow.integration_time_us;
	f.pixel_flow_x_integral = flow.integrated_x;
	f.pixel_flow_y_integral = flow.integrated_y;
	f.gyro_x_rate_integral = flow.integrated_xgyro;
	f.gyro_y_rate_integral = flow.integrated_ygyro;
	f.gyro_z_rate_integral = flow.integrated_zgyro;
	f.time_since_last_sonar_update = flow.time_delta_distance_us;
	f.ground_distance_m = flow.distance;
	f.quality = flow.quality;
	f.sensor_id = flow.sensor_id;
	f.gyro_temperature = flow.temperature;

	if (_flow_pub < 0) {
		_flow_pub = orb_advertise(ORB_ID(optical_flow), &f);

	} else {
		orb_publish(ORB_ID(optical_flow), _flow_pub, &f);
	}

	/* Use distance value for range finder report */
	struct range_finder_report r;
	memset(&r, 0, sizeof(r));

	r.timestamp = hrt_absolute_time();
	r.error_count = 0;
	r.type = RANGE_FINDER_TYPE_LASER;
	r.distance = flow.distance;
	r.minimum_distance = 0.0f;
	r.maximum_distance = 40.0f; // this is set to match the typical range of real sensors, could be made configurable
	r.valid = (r.distance > r.minimum_distance) && (r.distance < r.maximum_distance);

	if (_range_pub < 0) {
		_range_pub = orb_advertise(ORB_ID(sensor_range_finder), &r);
	} else {
		orb_publish(ORB_ID(sensor_range_finder), _range_pub, &r);
	}
}

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);
	}
}

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);

	struct vehicle_vicon_position_s vicon_position;
	memset(&vicon_position, 0, sizeof(vicon_position));

	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;

	if (_vicon_position_pub < 0) {
		_vicon_position_pub = orb_advertise(ORB_ID(vehicle_vicon_position), &vicon_position);

	} else {
		orb_publish(ORB_ID(vehicle_vicon_position), _vicon_position_pub, &vicon_position);
	}
}

void
MavlinkReceiver::handle_message_set_position_target_local_ned(mavlink_message_t *msg)
{
	mavlink_set_position_target_local_ned_t set_position_target_local_ned;
	mavlink_msg_set_position_target_local_ned_decode(msg, &set_position_target_local_ned);

	struct offboard_control_setpoint_s offboard_control_sp;
	memset(&offboard_control_sp, 0, sizeof(offboard_control_sp));//XXX breaks compatibility with multiple setpoints

	/* Only accept messages which are intended for this system */
	if ((mavlink_system.sysid == set_position_target_local_ned.target_system ||
				set_position_target_local_ned.target_system == 0) &&
			(mavlink_system.compid == set_position_target_local_ned.target_component ||
			 set_position_target_local_ned.target_component == 0)) {

		/* convert mavlink type (local, NED) to uORB offboard control struct */
		switch (set_position_target_local_ned.coordinate_frame) {
			case MAV_FRAME_LOCAL_NED:
				offboard_control_sp.mode = OFFBOARD_CONTROL_MODE_DIRECT_LOCAL_NED;
				break;
			case MAV_FRAME_LOCAL_OFFSET_NED:
				offboard_control_sp.mode = OFFBOARD_CONTROL_MODE_DIRECT_LOCAL_OFFSET_NED;
				break;
			case MAV_FRAME_BODY_NED:
				offboard_control_sp.mode = OFFBOARD_CONTROL_MODE_DIRECT_BODY_NED;
				break;
			case MAV_FRAME_BODY_OFFSET_NED:
				offboard_control_sp.mode = OFFBOARD_CONTROL_MODE_DIRECT_BODY_OFFSET_NED;
				break;
			default:
				/* invalid setpoint, avoid publishing */
				return;
		}
		offboard_control_sp.position[0] = set_position_target_local_ned.x;
		offboard_control_sp.position[1] = set_position_target_local_ned.y;
		offboard_control_sp.position[2] = set_position_target_local_ned.z;
		offboard_control_sp.velocity[0] = set_position_target_local_ned.vx;
		offboard_control_sp.velocity[1] = set_position_target_local_ned.vy;
		offboard_control_sp.velocity[2] = set_position_target_local_ned.vz;
		offboard_control_sp.acceleration[0] = set_position_target_local_ned.afx;
		offboard_control_sp.acceleration[1] = set_position_target_local_ned.afy;
		offboard_control_sp.acceleration[2] = set_position_target_local_ned.afz;
		offboard_control_sp.yaw = set_position_target_local_ned.yaw;
		offboard_control_sp.yaw_rate = set_position_target_local_ned.yaw_rate;
		offboard_control_sp.isForceSetpoint = (bool)(set_position_target_local_ned.type_mask & (1 << 9));

		/* If we are in force control mode, for now set offboard mode to force control */
		if (offboard_control_sp.isForceSetpoint) {
			offboard_control_sp.mode = OFFBOARD_CONTROL_MODE_DIRECT_FORCE;
		}

		/* set ignore flags */
		for (int i = 0; i < 9; i++) {
			offboard_control_sp.ignore &=  ~(1 << i);
			offboard_control_sp.ignore |=  (set_position_target_local_ned.type_mask & (1 << i));
		}

		offboard_control_sp.ignore &=  ~(1 << OFB_IGN_BIT_YAW);
		if (set_position_target_local_ned.type_mask & (1 << 10)) {
			offboard_control_sp.ignore |=  (1 << OFB_IGN_BIT_YAW);
		}

		offboard_control_sp.ignore &=  ~(1 << OFB_IGN_BIT_YAWRATE);
		if (set_position_target_local_ned.type_mask & (1 << 11)) {
			offboard_control_sp.ignore |=  (1 << OFB_IGN_BIT_YAWRATE);
		}

		offboard_control_sp.timestamp = hrt_absolute_time();

		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);
		}

		/* If we are in offboard control mode and offboard control loop through is enabled
		 * also publish the setpoint topic which is read by the controller */
		if (_mavlink->get_forward_externalsp()) {
			bool updated;
			orb_check(_control_mode_sub, &updated);
			if (updated) {
				orb_copy(ORB_ID(vehicle_control_mode), _control_mode_sub, &_control_mode);
			}
			if (_control_mode.flag_control_offboard_enabled) {
				if (offboard_control_sp.isForceSetpoint &&
						offboard_control_sp_ignore_position_all(offboard_control_sp) &&
						offboard_control_sp_ignore_velocity_all(offboard_control_sp)) {
					/* The offboard setpoint is a force setpoint only, directly writing to the force
					 * setpoint topic and not publishing the setpoint triplet topic */
					struct vehicle_force_setpoint_s	force_sp;
					force_sp.x = offboard_control_sp.acceleration[0];
					force_sp.y = offboard_control_sp.acceleration[1];
					force_sp.z = offboard_control_sp.acceleration[2];
					//XXX: yaw
					if (_force_sp_pub < 0) {
						_force_sp_pub = orb_advertise(ORB_ID(vehicle_force_setpoint), &force_sp);
					} else {
						orb_publish(ORB_ID(vehicle_force_setpoint), _force_sp_pub, &force_sp);
					}
				} else {
					/* It's not a pure force setpoint: publish to setpoint triplet  topic */
					struct position_setpoint_triplet_s pos_sp_triplet;
					pos_sp_triplet.previous.valid = false;
					pos_sp_triplet.next.valid = false;
					pos_sp_triplet.current.valid = true;
					pos_sp_triplet.current.type = position_setpoint_s::SETPOINT_TYPE_POSITION; //XXX support others

					/* set the local pos values if the setpoint type is 'local pos' and none
					 * of the local pos fields is set to 'ignore' */
					if (offboard_control_sp.mode == OFFBOARD_CONTROL_MODE_DIRECT_LOCAL_NED &&
							!offboard_control_sp_ignore_position_some(offboard_control_sp)) {
					pos_sp_triplet.current.position_valid = true;
					pos_sp_triplet.current.x = offboard_control_sp.position[0];
					pos_sp_triplet.current.y = offboard_control_sp.position[1];
					pos_sp_triplet.current.z = offboard_control_sp.position[2];
					} else {
						pos_sp_triplet.current.position_valid = false;
					}

					/* set the local vel values if the setpoint type is 'local pos' and none
					 * of the local vel fields is set to 'ignore' */
					if (offboard_control_sp.mode == OFFBOARD_CONTROL_MODE_DIRECT_LOCAL_NED &&
							!offboard_control_sp_ignore_velocity_some(offboard_control_sp)) {
						pos_sp_triplet.current.velocity_valid = true;
						pos_sp_triplet.current.vx = offboard_control_sp.velocity[0];
						pos_sp_triplet.current.vy = offboard_control_sp.velocity[1];
						pos_sp_triplet.current.vz = offboard_control_sp.velocity[2];
					} else {
						pos_sp_triplet.current.velocity_valid = false;
					}

					/* set the local acceleration values if the setpoint type is 'local pos' and none
					 * of the accelerations fields is set to 'ignore' */
					if (offboard_control_sp.mode == OFFBOARD_CONTROL_MODE_DIRECT_LOCAL_NED &&
							!offboard_control_sp_ignore_acceleration_some(offboard_control_sp)) {
						pos_sp_triplet.current.acceleration_valid = true;
						pos_sp_triplet.current.a_x = offboard_control_sp.acceleration[0];
						pos_sp_triplet.current.a_y = offboard_control_sp.acceleration[1];
						pos_sp_triplet.current.a_z = offboard_control_sp.acceleration[2];
						pos_sp_triplet.current.acceleration_is_force =
						offboard_control_sp.isForceSetpoint;

					} else {
						pos_sp_triplet.current.acceleration_valid = false;
					}

					/* set the yaw sp value if the setpoint type is 'local pos' and the yaw
					 * field is not set to 'ignore' */
					if (offboard_control_sp.mode == OFFBOARD_CONTROL_MODE_DIRECT_LOCAL_NED &&
							!offboard_control_sp_ignore_yaw(offboard_control_sp)) {
						pos_sp_triplet.current.yaw_valid = true;
						pos_sp_triplet.current.yaw = offboard_control_sp.yaw;

					} else {
						pos_sp_triplet.current.yaw_valid = false;
					}

					/* set the yawrate sp value if the setpoint type is 'local pos' and the yawrate
					 * field is not set to 'ignore' */
					if (offboard_control_sp.mode == OFFBOARD_CONTROL_MODE_DIRECT_LOCAL_NED &&
							!offboard_control_sp_ignore_yawrate(offboard_control_sp)) {
						pos_sp_triplet.current.yawspeed_valid = true;
						pos_sp_triplet.current.yawspeed = offboard_control_sp.yaw_rate;

					} else {
						pos_sp_triplet.current.yawspeed_valid = false;
					}
					//XXX handle global pos setpoints (different MAV frames)


					if (_pos_sp_triplet_pub < 0) {
						_pos_sp_triplet_pub = orb_advertise(ORB_ID(position_setpoint_triplet),
								&pos_sp_triplet);
					} else {
						orb_publish(ORB_ID(position_setpoint_triplet), _pos_sp_triplet_pub,
								&pos_sp_triplet);
					}

				}

			}

		}
	}
}

void
MavlinkReceiver::handle_message_vision_position_estimate(mavlink_message_t *msg)
{
	mavlink_vision_position_estimate_t pos;
	mavlink_msg_vision_position_estimate_decode(msg, &pos);

	struct vision_position_estimate vision_position;
	memset(&vision_position, 0, sizeof(vision_position));

	// Use the component ID to identify the vision sensor
	vision_position.id = msg->compid;

	vision_position.timestamp_boot = to_hrt(pos.usec); // Synced time
	vision_position.timestamp_computer = pos.usec;
	vision_position.x = pos.x;
	vision_position.y = pos.y;
	vision_position.z = pos.z;

	// XXX fix this
	vision_position.vx = 0.0f;
	vision_position.vy = 0.0f;
	vision_position.vz = 0.0f;

	math::Quaternion q;
	q.from_euler(pos.roll, pos.pitch, pos.yaw);

	vision_position.q[0] = q(0);
	vision_position.q[1] = q(1);
	vision_position.q[2] = q(2);
	vision_position.q[3] = q(3);

	if (_vision_position_pub < 0) {
		_vision_position_pub = orb_advertise(ORB_ID(vision_position_estimate), &vision_position);

	} else {
		orb_publish(ORB_ID(vision_position_estimate), _vision_position_pub, &vision_position);
	}
}

void
MavlinkReceiver::handle_message_set_attitude_target(mavlink_message_t *msg)
{
	mavlink_set_attitude_target_t set_attitude_target;
	mavlink_msg_set_attitude_target_decode(msg, &set_attitude_target);

	struct offboard_control_setpoint_s offboard_control_sp;
	memset(&offboard_control_sp, 0, sizeof(offboard_control_sp)); //XXX breaks compatibility with multiple setpoints

	/* Only accept messages which are intended for this system */
	if ((mavlink_system.sysid == set_attitude_target.target_system ||
				set_attitude_target.target_system == 0) &&
			(mavlink_system.compid == set_attitude_target.target_component ||
			 set_attitude_target.target_component == 0)) {
		for (int i = 0; i < 4; i++) {
			offboard_control_sp.attitude[i] = set_attitude_target.q[i];
		}
		offboard_control_sp.attitude_rate[0] = set_attitude_target.body_roll_rate;
		offboard_control_sp.attitude_rate[1] = set_attitude_target.body_pitch_rate;
		offboard_control_sp.attitude_rate[2] = set_attitude_target.body_yaw_rate;

		/* set correct ignore flags for body rate fields: copy from mavlink message */
		for (int i = 0; i < 3; i++) {
			offboard_control_sp.ignore &=  ~(1 << (i + OFB_IGN_BIT_BODYRATE_X));
			offboard_control_sp.ignore |=  (set_attitude_target.type_mask & (1 << i)) << OFB_IGN_BIT_BODYRATE_X;
		}
		/* set correct ignore flags for thrust field: copy from mavlink message */
		offboard_control_sp.ignore &= ~(1 << OFB_IGN_BIT_THRUST);
		offboard_control_sp.ignore |= ((set_attitude_target.type_mask & (1 << 6)) << OFB_IGN_BIT_THRUST);
		/* set correct ignore flags for attitude field: copy from mavlink message */
		offboard_control_sp.ignore &= ~(1 << OFB_IGN_BIT_ATT);
		offboard_control_sp.ignore |= ((set_attitude_target.type_mask & (1 << 7)) << OFB_IGN_BIT_ATT);


		offboard_control_sp.timestamp = hrt_absolute_time();
		offboard_control_sp.mode =OFFBOARD_CONTROL_MODE_DIRECT_ATTITUDE; //XXX handle rate control mode

		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);
		}

		/* If we are in offboard control mode and offboard control loop through is enabled
		 * also publish the setpoint topic which is read by the controller */
		if (_mavlink->get_forward_externalsp()) {
			bool updated;
			orb_check(_control_mode_sub, &updated);
			if (updated) {
				orb_copy(ORB_ID(vehicle_control_mode), _control_mode_sub, &_control_mode);
			}

			if (_control_mode.flag_control_offboard_enabled) {

				/* Publish attitude setpoint if attitude and thrust ignore bits are not set */
				if (!(offboard_control_sp_ignore_attitude(offboard_control_sp) ||
							offboard_control_sp_ignore_thrust(offboard_control_sp))) {
					struct vehicle_attitude_setpoint_s att_sp;
					att_sp.timestamp = hrt_absolute_time();
					mavlink_quaternion_to_euler(set_attitude_target.q,
							&att_sp.roll_body, &att_sp.pitch_body, &att_sp.yaw_body);
					mavlink_quaternion_to_dcm(set_attitude_target.q, (float(*)[3])att_sp.R_body);
					att_sp.R_valid = true;
					att_sp.thrust = set_attitude_target.thrust;
					att_sp.yaw_sp_move_rate = 0.0;
					memcpy(att_sp.q_d, set_attitude_target.q, sizeof(att_sp.q_d));
					att_sp.q_d_valid = true;
					if (_att_sp_pub < 0) {
						_att_sp_pub = orb_advertise(ORB_ID(vehicle_attitude_setpoint), &att_sp);
					} else {
						orb_publish(ORB_ID(vehicle_attitude_setpoint), _att_sp_pub, &att_sp);
					}
				}

				/* Publish attitude rate setpoint if bodyrate and thrust ignore bits are not set */
				///XXX add support for ignoring individual axes
				if (!(offboard_control_sp_ignore_bodyrates_some(offboard_control_sp) ||
							offboard_control_sp_ignore_thrust(offboard_control_sp))) {
					struct vehicle_rates_setpoint_s rates_sp;
					rates_sp.timestamp = hrt_absolute_time();
					rates_sp.roll = set_attitude_target.body_roll_rate;
					rates_sp.pitch = set_attitude_target.body_pitch_rate;
					rates_sp.yaw = set_attitude_target.body_yaw_rate;
					rates_sp.thrust = set_attitude_target.thrust;

					if (_att_sp_pub < 0) {
						_rates_sp_pub = orb_advertise(ORB_ID(vehicle_rates_setpoint), &rates_sp);
					} else {
						orb_publish(ORB_ID(vehicle_rates_setpoint), _rates_sp_pub, &rates_sp);
					}
				}
			}

		}
	}
{'fraction_non_alphanumeric': 0.10418430708439176, 'fraction_numerical': 0.007832345469940728, 'mean_word_length': 4.886096978506905, 'pattern_counts': {'":': 0, '<': 195, '<?xml version=': 0, '>': 137, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3212099', 'n_tokens_mistral': 23486, 'n_tokens_neox': 20799, 'n_words': 3780}