Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Path: blob/master/Rover/GCS_MAVLink_Rover.cpp
Views: 1798
#include "Rover.h"12#include "GCS_MAVLink_Rover.h"34#include <AP_RPM/AP_RPM_config.h>5#include <AP_RangeFinder/AP_RangeFinder_Backend.h>6#include <AP_EFI/AP_EFI_config.h>7#include <AC_Avoidance/AP_OADatabase.h>89MAV_TYPE GCS_Rover::frame_type() const10{11if (rover.is_boat()) {12return MAV_TYPE_SURFACE_BOAT;13}14return MAV_TYPE_GROUND_ROVER;15}1617MAV_MODE GCS_MAVLINK_Rover::base_mode() const18{19uint8_t _base_mode = MAV_MODE_FLAG_CUSTOM_MODE_ENABLED;2021// work out the base_mode. This value is not very useful22// for APM, but we calculate it as best we can so a generic23// MAVLink enabled ground station can work out something about24// what the MAV is up to. The actual bit values are highly25// ambiguous for most of the APM flight modes. In practice, you26// only get useful information from the custom_mode, which maps to27// the APM flight mode and has a well defined meaning in the28// ArduPlane documentation29if (rover.control_mode->has_manual_input()) {30_base_mode |= MAV_MODE_FLAG_MANUAL_INPUT_ENABLED;31}3233if (rover.control_mode->is_autopilot_mode()) {34_base_mode |= MAV_MODE_FLAG_GUIDED_ENABLED;35}3637if (rover.g2.stick_mixing > 0 && rover.control_mode != &rover.mode_initializing) {38// all modes except INITIALISING have some form of manual39// override if stick mixing is enabled40_base_mode |= MAV_MODE_FLAG_MANUAL_INPUT_ENABLED;41}4243// we are armed if we are not initialising44if (rover.control_mode != &rover.mode_initializing && rover.arming.is_armed()) {45_base_mode |= MAV_MODE_FLAG_SAFETY_ARMED;46}4748// indicate we have set a custom mode49_base_mode |= MAV_MODE_FLAG_CUSTOM_MODE_ENABLED;5051return (MAV_MODE)_base_mode;52}5354uint32_t GCS_Rover::custom_mode() const55{56return (uint32_t)rover.control_mode->mode_number();57}5859MAV_STATE GCS_MAVLINK_Rover::vehicle_system_status() const60{61if ((rover.failsafe.triggered != 0) || rover.failsafe.ekf) {62return MAV_STATE_CRITICAL;63}64if (rover.control_mode == &rover.mode_initializing) {65return MAV_STATE_CALIBRATING;66}67if (rover.control_mode == &rover.mode_hold) {68return MAV_STATE_STANDBY;69}7071return MAV_STATE_ACTIVE;72}7374void GCS_MAVLINK_Rover::send_position_target_global_int()75{76Location target;77if (!rover.control_mode->get_desired_location(target)) {78return;79}80static constexpr uint16_t POSITION_TARGET_TYPEMASK_LAST_BYTE = 0xF000;81static constexpr uint16_t TYPE_MASK = POSITION_TARGET_TYPEMASK_VX_IGNORE | POSITION_TARGET_TYPEMASK_VY_IGNORE | POSITION_TARGET_TYPEMASK_VZ_IGNORE |82POSITION_TARGET_TYPEMASK_AX_IGNORE | POSITION_TARGET_TYPEMASK_AY_IGNORE | POSITION_TARGET_TYPEMASK_AZ_IGNORE |83POSITION_TARGET_TYPEMASK_YAW_IGNORE | POSITION_TARGET_TYPEMASK_YAW_RATE_IGNORE | POSITION_TARGET_TYPEMASK_LAST_BYTE;84mavlink_msg_position_target_global_int_send(85chan,86AP_HAL::millis(), // time_boot_ms87MAV_FRAME_GLOBAL, // targets are always global altitude88TYPE_MASK, // ignore everything except the x/y/z components89target.lat, // latitude as 1e790target.lng, // longitude as 1e791target.alt * 0.01f, // altitude is sent as a float920.0f, // vx930.0f, // vy940.0f, // vz950.0f, // afx960.0f, // afy970.0f, // afz980.0f, // yaw990.0f); // yaw_rate100}101102void GCS_MAVLINK_Rover::send_nav_controller_output() const103{104if (!rover.control_mode->is_autopilot_mode()) {105return;106}107108const Mode *control_mode = rover.control_mode;109110mavlink_msg_nav_controller_output_send(111chan,1120, // roll113degrees(rover.g2.attitude_control.get_desired_pitch()),114control_mode->nav_bearing(),115control_mode->wp_bearing(),116MIN(control_mode->get_distance_to_destination(), UINT16_MAX),1170,118control_mode->speed_error(),119control_mode->crosstrack_error());120}121122void GCS_MAVLINK_Rover::send_servo_out()123{124float motor1, motor3;125if (rover.g2.motors.have_skid_steering()) {126motor1 = 10000 * (SRV_Channels::get_output_scaled(SRV_Channel::k_throttleLeft) * 0.001f);127motor3 = 10000 * (SRV_Channels::get_output_scaled(SRV_Channel::k_throttleRight) * 0.001f);128} else {129motor1 = 10000 * (SRV_Channels::get_output_scaled(SRV_Channel::k_steering) / 4500.0f);130motor3 = 10000 * (SRV_Channels::get_output_scaled(SRV_Channel::k_throttle) * 0.01f);131}132mavlink_msg_rc_channels_scaled_send(133chan,134millis(),1350, // port 0136motor1,1370,138motor3,1390,1400,1410,1420,1430,144#if AP_RSSI_ENABLED145receiver_rssi()146#else147255148#endif149);150}151152int16_t GCS_MAVLINK_Rover::vfr_hud_throttle() const153{154return rover.g2.motors.get_throttle();155}156157#if AP_RANGEFINDER_ENABLED158void GCS_MAVLINK_Rover::send_rangefinder() const159{160float distance = 0;161float voltage = 0;162bool got_one = false;163164// report smaller distance of all rangefinders165for (uint8_t i=0; i<rover.rangefinder.num_sensors(); i++) {166AP_RangeFinder_Backend *s = rover.rangefinder.get_backend(i);167if (s == nullptr) {168continue;169}170if (!got_one ||171s->distance() < distance) {172distance = s->distance();173voltage = s->voltage_mv();174got_one = true;175}176}177if (!got_one) {178// no relevant data found179return;180}181182mavlink_msg_rangefinder_send(183chan,184distance,185voltage);186}187188void GCS_MAVLINK_Rover::send_water_depth()189{190if (!HAVE_PAYLOAD_SPACE(chan, WATER_DEPTH)) {191return;192}193194// only send for boats:195if (!rover.is_boat()) {196return;197}198199RangeFinder *rangefinder = RangeFinder::get_singleton();200201if (rangefinder == nullptr) {202return;203}204205// depth can only be measured by a downward-facing rangefinder:206if (!rangefinder->has_orientation(ROTATION_PITCH_270)) {207return;208}209210// get position211const AP_AHRS &ahrs = AP::ahrs();212Location loc;213IGNORE_RETURN(ahrs.get_location(loc));214215const auto num_sensors = rangefinder->num_sensors();216for (uint8_t i=0; i<num_sensors; i++) {217last_WATER_DEPTH_index += 1;218if (last_WATER_DEPTH_index >= num_sensors) {219last_WATER_DEPTH_index = 0;220}221222const AP_RangeFinder_Backend *s = rangefinder->get_backend(last_WATER_DEPTH_index);223224if (s == nullptr || s->orientation() != ROTATION_PITCH_270 || !s->has_data()) {225continue;226}227228// get temperature229float temp_C;230if (!s->get_temp(temp_C)) {231temp_C = 0.0f;232}233234const bool sensor_healthy = (s->status() == RangeFinder::Status::Good);235236mavlink_msg_water_depth_send(237chan,238AP_HAL::millis(), // time since system boot TODO: take time of measurement239last_WATER_DEPTH_index, // rangefinder instance240sensor_healthy, // sensor healthy241loc.lat, // latitude of vehicle242loc.lng, // longitude of vehicle243loc.alt * 0.01f, // altitude of vehicle (MSL)244ahrs.get_roll(), // roll in radians245ahrs.get_pitch(), // pitch in radians246ahrs.get_yaw(), // yaw in radians247s->distance(), // distance in meters248temp_C); // temperature in degC249250break; // only send one WATER_DEPTH message per loop251}252253}254#endif // AP_RANGEFINDER_ENABLED255256/*257send PID tuning message258*/259void GCS_MAVLINK_Rover::send_pid_tuning()260{261Parameters &g = rover.g;262ParametersG2 &g2 = rover.g2;263264const AP_PIDInfo *pid_info;265266// steering PID267if (g.gcs_pid_mask & 1) {268pid_info = &g2.attitude_control.get_steering_rate_pid().get_pid_info();269mavlink_msg_pid_tuning_send(chan, PID_TUNING_STEER,270degrees(pid_info->target),271degrees(pid_info->actual),272pid_info->FF,273pid_info->P,274pid_info->I,275pid_info->D,276pid_info->slew_rate,277pid_info->Dmod);278if (!HAVE_PAYLOAD_SPACE(chan, PID_TUNING)) {279return;280}281}282283// speed to throttle PID284if (g.gcs_pid_mask & 2) {285pid_info = &g2.attitude_control.get_throttle_speed_pid_info();286mavlink_msg_pid_tuning_send(chan, PID_TUNING_ACCZ,287pid_info->target,288pid_info->actual,289pid_info->FF,290pid_info->P,291pid_info->I,292pid_info->D,293pid_info->slew_rate,294pid_info->Dmod);295if (!HAVE_PAYLOAD_SPACE(chan, PID_TUNING)) {296return;297}298}299300// pitch to throttle pid301if (g.gcs_pid_mask & 4) {302pid_info = &g2.attitude_control.get_pitch_to_throttle_pid().get_pid_info();303mavlink_msg_pid_tuning_send(chan, PID_TUNING_PITCH,304degrees(pid_info->target),305degrees(pid_info->actual),306pid_info->FF,307pid_info->P,308pid_info->I,309pid_info->D,310pid_info->slew_rate,311pid_info->Dmod);312if (!HAVE_PAYLOAD_SPACE(chan, PID_TUNING)) {313return;314}315}316317// left wheel rate control pid318if (g.gcs_pid_mask & 8) {319pid_info = &g2.wheel_rate_control.get_pid(0).get_pid_info();320mavlink_msg_pid_tuning_send(chan, 7,321pid_info->target,322pid_info->actual,323pid_info->FF,324pid_info->P,325pid_info->I,326pid_info->D,327pid_info->slew_rate,328pid_info->Dmod);329if (!HAVE_PAYLOAD_SPACE(chan, PID_TUNING)) {330return;331}332}333334// right wheel rate control pid335if (g.gcs_pid_mask & 16) {336pid_info = &g2.wheel_rate_control.get_pid(1).get_pid_info();337mavlink_msg_pid_tuning_send(chan, 8,338pid_info->target,339pid_info->actual,340pid_info->FF,341pid_info->P,342pid_info->I,343pid_info->D,344pid_info->slew_rate,345pid_info->Dmod);346if (!HAVE_PAYLOAD_SPACE(chan, PID_TUNING)) {347return;348}349}350351// sailboat heel to mainsail pid352if (g.gcs_pid_mask & 32) {353pid_info = &g2.attitude_control.get_sailboat_heel_pid().get_pid_info();354mavlink_msg_pid_tuning_send(chan, 9,355pid_info->target,356pid_info->actual,357pid_info->FF,358pid_info->P,359pid_info->I,360pid_info->D,361pid_info->slew_rate,362pid_info->Dmod);363if (!HAVE_PAYLOAD_SPACE(chan, PID_TUNING)) {364return;365}366}367368// Position Controller Velocity North PID369if (g.gcs_pid_mask & 64) {370pid_info = &g2.pos_control.get_vel_pid().get_pid_info_x();371mavlink_msg_pid_tuning_send(chan, 10,372pid_info->target,373pid_info->actual,374pid_info->FF,375pid_info->P,376pid_info->I,377pid_info->D,378pid_info->slew_rate,379pid_info->Dmod);380if (!HAVE_PAYLOAD_SPACE(chan, PID_TUNING)) {381return;382}383}384385// Position Controller Velocity East PID386if (g.gcs_pid_mask & 128) {387pid_info = &g2.pos_control.get_vel_pid().get_pid_info_y();388mavlink_msg_pid_tuning_send(chan, 11,389pid_info->target,390pid_info->actual,391pid_info->FF,392pid_info->P,393pid_info->I,394pid_info->D,395pid_info->slew_rate,396pid_info->Dmod);397if (!HAVE_PAYLOAD_SPACE(chan, PID_TUNING)) {398return;399}400}401}402403void Rover::send_wheel_encoder_distance(const mavlink_channel_t chan)404{405// send wheel encoder data using wheel_distance message406if (g2.wheel_encoder.num_sensors() > 0) {407double distances[MAVLINK_MSG_WHEEL_DISTANCE_FIELD_DISTANCE_LEN] {};408for (uint8_t i = 0; i < g2.wheel_encoder.num_sensors(); i++) {409distances[i] = wheel_encoder_last_distance_m[i];410}411mavlink_msg_wheel_distance_send(chan, 1000UL * AP_HAL::millis(), g2.wheel_encoder.num_sensors(), distances);412}413}414415uint8_t GCS_MAVLINK_Rover::sysid_my_gcs() const416{417return rover.g.sysid_my_gcs;418}419420bool GCS_MAVLINK_Rover::sysid_enforce() const421{422return rover.g2.sysid_enforce;423}424425uint32_t GCS_MAVLINK_Rover::telem_delay() const426{427return static_cast<uint32_t>(rover.g.telem_delay);428}429430bool GCS_Rover::vehicle_initialised() const431{432return rover.control_mode != &rover.mode_initializing;433}434435// try to send a message, return false if it won't fit in the serial tx buffer436bool GCS_MAVLINK_Rover::try_send_message(enum ap_message id)437{438switch (id) {439440case MSG_SERVO_OUT:441CHECK_PAYLOAD_SIZE(RC_CHANNELS_SCALED);442send_servo_out();443break;444445case MSG_WHEEL_DISTANCE:446CHECK_PAYLOAD_SIZE(WHEEL_DISTANCE);447rover.send_wheel_encoder_distance(chan);448break;449450case MSG_WIND:451CHECK_PAYLOAD_SIZE(WIND);452rover.g2.windvane.send_wind(chan);453break;454455#if AP_OADATABASE_ENABLED456case MSG_ADSB_VEHICLE: {457AP_OADatabase *oadb = AP::oadatabase();458if (oadb != nullptr) {459CHECK_PAYLOAD_SIZE(ADSB_VEHICLE);460uint16_t interval_ms = 0;461if (get_ap_message_interval(id, interval_ms)) {462oadb->send_adsb_vehicle(chan, interval_ms);463}464}465break;466}467#endif468469#if AP_RANGEFINDER_ENABLED470case MSG_WATER_DEPTH:471CHECK_PAYLOAD_SIZE(WATER_DEPTH);472send_water_depth();473break;474#endif // AP_RANGEFINDER_ENABLED475476default:477return GCS_MAVLINK::try_send_message(id);478}479return true;480}481482void GCS_MAVLINK_Rover::packetReceived(const mavlink_status_t &status, const mavlink_message_t &msg)483{484#if AP_FOLLOW_ENABLED485// pass message to follow library486rover.g2.follow.handle_msg(msg);487#endif488GCS_MAVLINK::packetReceived(status, msg);489}490491/*492default stream rates to 1Hz493*/494const AP_Param::GroupInfo GCS_MAVLINK_Parameters::var_info[] = {495// @Param: RAW_SENS496// @DisplayName: Raw sensor stream rate497// @Description: MAVLink Stream rate of RAW_IMU, SCALED_IMU2, SCALED_IMU3, SCALED_PRESSURE, SCALED_PRESSURE2, SCALED_PRESSURE3 and AIRSPEED498// @Units: Hz499// @Range: 0 50500// @Increment: 1501// @RebootRequired: True502// @User: Advanced503AP_GROUPINFO("RAW_SENS", 0, GCS_MAVLINK_Parameters, streamRates[0], 1),504505// @Param: EXT_STAT506// @DisplayName: Extended status stream rate507// @Description: MAVLink Stream rate of SYS_STATUS, POWER_STATUS, MCU_STATUS, MEMINFO, CURRENT_WAYPOINT, GPS_RAW_INT, GPS_RTK (if available), GPS2_RAW_INT (if available), GPS2_RTK (if available), NAV_CONTROLLER_OUTPUT, FENCE_STATUS, and GLOBAL_TARGET_POS_INT508// @Units: Hz509// @Range: 0 50510// @Increment: 1511// @RebootRequired: True512// @User: Advanced513AP_GROUPINFO("EXT_STAT", 1, GCS_MAVLINK_Parameters, streamRates[1], 1),514515// @Param: RC_CHAN516// @DisplayName: RC Channel stream rate517// @Description: MAVLink Stream rate of SERVO_OUTPUT_RAW and RC_CHANNELS518// @Units: Hz519// @Range: 0 50520// @Increment: 1521// @RebootRequired: True522// @User: Advanced523AP_GROUPINFO("RC_CHAN", 2, GCS_MAVLINK_Parameters, streamRates[2], 1),524525// @Param: RAW_CTRL526// @DisplayName: Raw Control stream rate527// @Description: MAVLink Raw Control stream rate of SERVO_OUT528// @Units: Hz529// @Range: 0 50530// @Increment: 1531// @RebootRequired: True532// @User: Advanced533AP_GROUPINFO("RAW_CTRL", 3, GCS_MAVLINK_Parameters, streamRates[3], 1),534535// @Param: POSITION536// @DisplayName: Position stream rate537// @Description: MAVLink Stream rate of GLOBAL_POSITION_INT and LOCAL_POSITION_NED538// @Units: Hz539// @Range: 0 50540// @Increment: 1541// @RebootRequired: True542// @User: Advanced543AP_GROUPINFO("POSITION", 4, GCS_MAVLINK_Parameters, streamRates[4], 1),544545// @Param: EXTRA1546// @DisplayName: Extra data type 1 stream rate to ground station547// @Description: MAVLink Stream rate of ATTITUDE, SIMSTATE (SIM only), AHRS2 and PID_TUNING548// @Units: Hz549// @Range: 0 50550// @Increment: 1551// @RebootRequired: True552// @User: Advanced553AP_GROUPINFO("EXTRA1", 5, GCS_MAVLINK_Parameters, streamRates[5], 1),554555// @Param: EXTRA2556// @DisplayName: Extra data type 2 stream rate557// @Description: MAVLink Stream rate of VFR_HUD558// @Units: Hz559// @Range: 0 50560// @Increment: 1561// @RebootRequired: True562// @User: Advanced563AP_GROUPINFO("EXTRA2", 6, GCS_MAVLINK_Parameters, streamRates[6], 1),564565// @Param: EXTRA3566// @DisplayName: Extra data type 3 stream rate567// @Description: MAVLink Stream rate of AHRS, SYSTEM_TIME, WIND, RANGEFINDER, DISTANCE_SENSOR, BATTERY2, BATTERY_STATUS, GIMBAL_DEVICE_ATTITUDE_STATUS, OPTICAL_FLOW, MAG_CAL_REPORT, MAG_CAL_PROGRESS, EKF_STATUS_REPORT, VIBRATION, RPM, ESC TELEMETRY, and WHEEL_DISTANCE568// @Units: Hz569// @Range: 0 50570// @Increment: 1571// @RebootRequired: True572// @User: Advanced573AP_GROUPINFO("EXTRA3", 7, GCS_MAVLINK_Parameters, streamRates[7], 1),574575// @Param: PARAMS576// @DisplayName: Parameter stream rate577// @Description: MAVLink Stream rate of PARAM_VALUE578// @Units: Hz579// @Range: 0 50580// @Increment: 1581// @RebootRequired: True582// @User: Advanced583AP_GROUPINFO("PARAMS", 8, GCS_MAVLINK_Parameters, streamRates[8], 10),584585// @Param: ADSB586// @DisplayName: ADSB stream rate587// @Description: MAVLink ADSB (AIS) stream rate588// @Units: Hz589// @Range: 0 50590// @Increment: 1591// @RebootRequired: True592// @User: Advanced593AP_GROUPINFO("ADSB", 9, GCS_MAVLINK_Parameters, streamRates[9], 0),594595AP_GROUPEND596};597598static const ap_message STREAM_RAW_SENSORS_msgs[] = {599MSG_RAW_IMU,600MSG_SCALED_IMU2,601MSG_SCALED_IMU3,602MSG_SCALED_PRESSURE,603MSG_SCALED_PRESSURE2,604MSG_SCALED_PRESSURE3,605#if AP_AIRSPEED_ENABLED606MSG_AIRSPEED,607#endif608};609static const ap_message STREAM_EXTENDED_STATUS_msgs[] = {610MSG_SYS_STATUS,611MSG_POWER_STATUS,612#if HAL_WITH_MCU_MONITORING613MSG_MCU_STATUS,614#endif615MSG_MEMINFO,616MSG_CURRENT_WAYPOINT,617#if AP_GPS_GPS_RAW_INT_SENDING_ENABLED618MSG_GPS_RAW,619#endif620#if AP_GPS_GPS_RTK_SENDING_ENABLED621MSG_GPS_RTK,622#endif623#if AP_GPS_GPS2_RAW_SENDING_ENABLED624MSG_GPS2_RAW,625#endif626#if AP_GPS_GPS2_RTK_SENDING_ENABLED627MSG_GPS2_RTK,628#endif629MSG_NAV_CONTROLLER_OUTPUT,630#if AP_FENCE_ENABLED631MSG_FENCE_STATUS,632#endif633MSG_POSITION_TARGET_GLOBAL_INT,634};635static const ap_message STREAM_POSITION_msgs[] = {636MSG_LOCATION,637MSG_LOCAL_POSITION638};639static const ap_message STREAM_RAW_CONTROLLER_msgs[] = {640MSG_SERVO_OUT,641};642static const ap_message STREAM_RC_CHANNELS_msgs[] = {643MSG_SERVO_OUTPUT_RAW,644MSG_RC_CHANNELS,645#if AP_MAVLINK_MSG_RC_CHANNELS_RAW_ENABLED646MSG_RC_CHANNELS_RAW, // only sent on a mavlink1 connection647#endif648};649static const ap_message STREAM_EXTRA1_msgs[] = {650MSG_ATTITUDE,651#if AP_SIM_ENABLED652MSG_SIMSTATE,653#endif654MSG_AHRS2,655MSG_PID_TUNING,656};657static const ap_message STREAM_EXTRA2_msgs[] = {658MSG_VFR_HUD659};660static const ap_message STREAM_EXTRA3_msgs[] = {661MSG_AHRS,662MSG_WIND,663#if AP_RANGEFINDER_ENABLED664MSG_RANGEFINDER,665MSG_WATER_DEPTH,666#endif667MSG_DISTANCE_SENSOR,668MSG_SYSTEM_TIME,669#if AP_BATTERY_ENABLED670MSG_BATTERY_STATUS,671#endif672#if HAL_MOUNT_ENABLED673MSG_GIMBAL_DEVICE_ATTITUDE_STATUS,674#endif675#if AP_OPTICALFLOW_ENABLED676MSG_OPTICAL_FLOW,677#endif678#if COMPASS_CAL_ENABLED679MSG_MAG_CAL_REPORT,680MSG_MAG_CAL_PROGRESS,681#endif682MSG_EKF_STATUS_REPORT,683MSG_VIBRATION,684#if AP_RPM_ENABLED685MSG_RPM,686#endif687MSG_WHEEL_DISTANCE,688#if HAL_WITH_ESC_TELEM689MSG_ESC_TELEMETRY,690#endif691#if HAL_EFI_ENABLED692MSG_EFI_STATUS,693#endif694};695static const ap_message STREAM_PARAMS_msgs[] = {696MSG_NEXT_PARAM,697MSG_AVAILABLE_MODES698};699static const ap_message STREAM_ADSB_msgs[] = {700MSG_ADSB_VEHICLE,701#if AP_AIS_ENABLED702MSG_AIS_VESSEL,703#endif704};705706const struct GCS_MAVLINK::stream_entries GCS_MAVLINK::all_stream_entries[] = {707MAV_STREAM_ENTRY(STREAM_RAW_SENSORS),708MAV_STREAM_ENTRY(STREAM_EXTENDED_STATUS),709MAV_STREAM_ENTRY(STREAM_POSITION),710MAV_STREAM_ENTRY(STREAM_RAW_CONTROLLER),711MAV_STREAM_ENTRY(STREAM_RC_CHANNELS),712MAV_STREAM_ENTRY(STREAM_EXTRA1),713MAV_STREAM_ENTRY(STREAM_EXTRA2),714MAV_STREAM_ENTRY(STREAM_EXTRA3),715MAV_STREAM_ENTRY(STREAM_ADSB),716MAV_STREAM_ENTRY(STREAM_PARAMS),717MAV_STREAM_TERMINATOR // must have this at end of stream_entries718};719720bool GCS_MAVLINK_Rover::handle_guided_request(AP_Mission::Mission_Command &cmd)721{722if (!rover.control_mode->in_guided_mode()) {723// only accept position updates when in GUIDED mode724return false;725}726727// make any new wp uploaded instant (in case we are already in Guided mode)728return rover.mode_guided.set_desired_location(cmd.content.location);729}730731MAV_RESULT GCS_MAVLINK_Rover::_handle_command_preflight_calibration(const mavlink_command_int_t &packet, const mavlink_message_t &msg)732{733if (packet.y == 1) {734if (rover.g2.windvane.start_direction_calibration()) {735return MAV_RESULT_ACCEPTED;736} else {737return MAV_RESULT_FAILED;738}739} else if (packet.y == 2) {740if (rover.g2.windvane.start_speed_calibration()) {741return MAV_RESULT_ACCEPTED;742} else {743return MAV_RESULT_FAILED;744}745}746747return GCS_MAVLINK::_handle_command_preflight_calibration(packet, msg);748}749750MAV_RESULT GCS_MAVLINK_Rover::handle_command_int_packet(const mavlink_command_int_t &packet, const mavlink_message_t &msg)751{752switch (packet.command) {753754case MAV_CMD_DO_CHANGE_SPEED:755// param1 : unused756// param2 : new speed in m/s757if (!rover.control_mode->set_desired_speed(packet.param2)) {758return MAV_RESULT_FAILED;759}760return MAV_RESULT_ACCEPTED;761762case MAV_CMD_DO_REPOSITION:763return handle_command_int_do_reposition(packet);764765case MAV_CMD_DO_SET_REVERSE:766// param1 : Direction (0=Forward, 1=Reverse)767rover.control_mode->set_reversed(is_equal(packet.param1,1.0f));768return MAV_RESULT_ACCEPTED;769770case MAV_CMD_NAV_RETURN_TO_LAUNCH:771if (rover.set_mode(rover.mode_rtl, ModeReason::GCS_COMMAND)) {772return MAV_RESULT_ACCEPTED;773}774return MAV_RESULT_FAILED;775776case MAV_CMD_DO_MOTOR_TEST:777// param1 : motor sequence number (a number from 1 to max number of motors on the vehicle)778// param2 : throttle type (0=throttle percentage, 1=PWM, 2=pilot throttle channel pass-through. See MOTOR_TEST_THROTTLE_TYPE enum)779// param3 : throttle (range depends upon param2)780// param4 : timeout (in seconds)781return rover.mavlink_motor_test_start(*this,782(AP_MotorsUGV::motor_test_order)packet.param1,783static_cast<uint8_t>(packet.param2),784static_cast<int16_t>(packet.param3),785packet.param4);786787case MAV_CMD_MISSION_START:788if (!is_zero(packet.param1) || !is_zero(packet.param2)) {789// first-item/last item not supported790return MAV_RESULT_DENIED;791}792if (rover.set_mode(rover.mode_auto, ModeReason::GCS_COMMAND)) {793return MAV_RESULT_ACCEPTED;794}795return MAV_RESULT_FAILED;796797#if AP_MAVLINK_MAV_CMD_NAV_SET_YAW_SPEED_ENABLED798case MAV_CMD_NAV_SET_YAW_SPEED:799send_received_message_deprecation_warning("MAV_CMD_NAV_SET_YAW_SPEED");800return handle_command_nav_set_yaw_speed(packet, msg);801#endif802803default:804return GCS_MAVLINK::handle_command_int_packet(packet, msg);805}806}807808#if AP_MAVLINK_MAV_CMD_NAV_SET_YAW_SPEED_ENABLED809MAV_RESULT GCS_MAVLINK_Rover::handle_command_nav_set_yaw_speed(const mavlink_command_int_t &packet, const mavlink_message_t &msg)810{811// param1 : yaw angle (may be absolute or relative)812// param2 : Speed - in metres/second813// param3 : 0 = param1 is absolute, 1 = param1 is relative814815// exit if vehicle is not in Guided mode816if (!rover.control_mode->in_guided_mode()) {817return MAV_RESULT_FAILED;818}819820// get final angle, 1 = Relative, 0 = Absolute821if (packet.param3 > 0) {822// relative angle823rover.mode_guided.set_desired_heading_delta_and_speed(packet.param1 * 100.0f, packet.param2);824} else {825// absolute angle826rover.mode_guided.set_desired_heading_and_speed(packet.param1 * 100.0f, packet.param2);827}828return MAV_RESULT_ACCEPTED;829}830#endif831832MAV_RESULT GCS_MAVLINK_Rover::handle_command_int_do_reposition(const mavlink_command_int_t &packet)833{834const bool change_modes = ((int32_t)packet.param2 & MAV_DO_REPOSITION_FLAGS_CHANGE_MODE) == MAV_DO_REPOSITION_FLAGS_CHANGE_MODE;835if (!rover.control_mode->in_guided_mode() && !change_modes) {836return MAV_RESULT_DENIED;837}838839// sanity check location840if (!check_latlng(packet.x, packet.y)) {841return MAV_RESULT_DENIED;842}843if (packet.x == 0 && packet.y == 0) {844return MAV_RESULT_DENIED;845}846847Location requested_location {};848if (!location_from_command_t(packet, requested_location)) {849return MAV_RESULT_DENIED;850}851852if (!rover.control_mode->in_guided_mode()) {853if (!rover.set_mode(Mode::Number::GUIDED, ModeReason::GCS_COMMAND)) {854return MAV_RESULT_FAILED;855}856}857858if (is_positive(packet.param1)) {859if (!rover.control_mode->set_desired_speed(packet.param1)) {860return MAV_RESULT_FAILED;861}862}863864// set the destination865if (!rover.mode_guided.set_desired_location(requested_location)) {866return MAV_RESULT_FAILED;867}868869return MAV_RESULT_ACCEPTED;870}871872void GCS_MAVLINK_Rover::handle_message(const mavlink_message_t &msg)873{874switch (msg.msgid) {875876case MAVLINK_MSG_ID_SET_ATTITUDE_TARGET:877handle_set_attitude_target(msg);878break;879880case MAVLINK_MSG_ID_SET_POSITION_TARGET_LOCAL_NED:881handle_set_position_target_local_ned(msg);882break;883884case MAVLINK_MSG_ID_SET_POSITION_TARGET_GLOBAL_INT:885handle_set_position_target_global_int(msg);886break;887888default:889GCS_MAVLINK::handle_message(msg);890break;891}892}893894void GCS_MAVLINK_Rover::handle_manual_control_axes(const mavlink_manual_control_t &packet, const uint32_t tnow)895{896manual_override(rover.channel_steer, packet.y, 1000, 2000, tnow);897manual_override(rover.channel_throttle, packet.z, 1000, 2000, tnow);898}899900void GCS_MAVLINK_Rover::handle_set_attitude_target(const mavlink_message_t &msg)901{902// decode packet903mavlink_set_attitude_target_t packet;904mavlink_msg_set_attitude_target_decode(&msg, &packet);905906// exit if vehicle is not in Guided mode907if (!rover.control_mode->in_guided_mode()) {908return;909}910911// ensure type_mask specifies to use thrust912if ((packet.type_mask & MAVLINK_SET_ATT_TYPE_MASK_THROTTLE_IGNORE) != 0) {913return;914}915916// convert thrust to ground speed917packet.thrust = constrain_float(packet.thrust, -1.0f, 1.0f);918const float target_speed = rover.control_mode->get_speed_default() * packet.thrust;919920// if the body_yaw_rate field is ignored, convert quaternion to heading921if ((packet.type_mask & MAVLINK_SET_ATT_TYPE_MASK_YAW_RATE_IGNORE) != 0) {922// convert quaternion to heading923float target_heading_cd = degrees(Quaternion(packet.q[0], packet.q[1], packet.q[2], packet.q[3]).get_euler_yaw()) * 100.0f;924rover.mode_guided.set_desired_heading_and_speed(target_heading_cd, target_speed);925} else {926// use body_yaw_rate field927rover.mode_guided.set_desired_turn_rate_and_speed((RAD_TO_DEG * packet.body_yaw_rate) * 100.0f, target_speed);928}929}930931void GCS_MAVLINK_Rover::handle_set_position_target_local_ned(const mavlink_message_t &msg)932{933// decode packet934mavlink_set_position_target_local_ned_t packet;935mavlink_msg_set_position_target_local_ned_decode(&msg, &packet);936937// exit if vehicle is not in Guided mode938if (!rover.control_mode->in_guided_mode()) {939return;940}941942// need ekf origin943Location ekf_origin;944if (!rover.ahrs.get_origin(ekf_origin)) {945return;946}947948// check for supported coordinate frames949switch (packet.coordinate_frame) {950case MAV_FRAME_LOCAL_NED:951case MAV_FRAME_LOCAL_OFFSET_NED:952case MAV_FRAME_BODY_NED:953case MAV_FRAME_BODY_OFFSET_NED:954break;955956default:957return;958}959960bool pos_ignore = packet.type_mask & MAVLINK_SET_POS_TYPE_MASK_POS_IGNORE;961bool vel_ignore = packet.type_mask & MAVLINK_SET_POS_TYPE_MASK_VEL_IGNORE;962bool acc_ignore = packet.type_mask & MAVLINK_SET_POS_TYPE_MASK_ACC_IGNORE;963bool yaw_ignore = packet.type_mask & MAVLINK_SET_POS_TYPE_MASK_YAW_IGNORE;964bool yaw_rate_ignore = packet.type_mask & MAVLINK_SET_POS_TYPE_MASK_YAW_RATE_IGNORE;965966// prepare target position967Location target_loc = rover.current_loc;968if (!pos_ignore) {969switch (packet.coordinate_frame) {970case MAV_FRAME_BODY_NED:971case MAV_FRAME_BODY_OFFSET_NED: {972// rotate from body-frame to NE frame973const float ne_x = packet.x * rover.ahrs.cos_yaw() - packet.y * rover.ahrs.sin_yaw();974const float ne_y = packet.x * rover.ahrs.sin_yaw() + packet.y * rover.ahrs.cos_yaw();975// add offset to current location976target_loc.offset(ne_x, ne_y);977}978break;979980case MAV_FRAME_LOCAL_OFFSET_NED:981// add offset to current location982target_loc.offset(packet.x, packet.y);983break;984985case MAV_FRAME_LOCAL_NED:986default:987// MAV_FRAME_LOCAL_NED is interpreted as an offset from EKF origin988target_loc = ekf_origin;989target_loc.offset(packet.x, packet.y);990break;991}992}993994float target_speed = 0.0f;995float target_yaw_cd = 0.0f;996997// consume velocity and convert to target speed and heading998if (!vel_ignore) {999const float speed_max = rover.control_mode->get_speed_default();1000// convert vector length into a speed1001target_speed = constrain_float(safe_sqrt(sq(packet.vx) + sq(packet.vy)), -speed_max, speed_max);1002// convert vector direction to target yaw1003target_yaw_cd = degrees(atan2f(packet.vy, packet.vx)) * 100.0f;10041005// rotate target yaw if provided in body-frame1006if (packet.coordinate_frame == MAV_FRAME_BODY_NED || packet.coordinate_frame == MAV_FRAME_BODY_OFFSET_NED) {1007target_yaw_cd = wrap_180_cd(target_yaw_cd + rover.ahrs.yaw_sensor);1008}1009}10101011// consume yaw heading1012if (!yaw_ignore) {1013target_yaw_cd = ToDeg(packet.yaw) * 100.0f;1014// rotate target yaw if provided in body-frame1015if (packet.coordinate_frame == MAV_FRAME_BODY_NED || packet.coordinate_frame == MAV_FRAME_BODY_OFFSET_NED) {1016target_yaw_cd = wrap_180_cd(target_yaw_cd + rover.ahrs.yaw_sensor);1017}1018}1019// consume yaw rate1020float target_turn_rate_cds = 0.0f;1021if (!yaw_rate_ignore) {1022target_turn_rate_cds = ToDeg(packet.yaw_rate) * 100.0f;1023}10241025// handling case when both velocity and either yaw or yaw-rate are provided1026// by default, we consider that the rover will drive forward1027float speed_dir = 1.0f;1028if (!vel_ignore && (!yaw_ignore || !yaw_rate_ignore)) {1029// Note: we are using the x-axis velocity to determine direction even though1030// the frame may have been provided in MAV_FRAME_LOCAL_OFFSET_NED or MAV_FRAME_LOCAL_NED1031if (is_negative(packet.vx)) {1032speed_dir = -1.0f;1033}1034}10351036// set guided mode targets1037if (!pos_ignore) {1038// consume position target1039if (!rover.mode_guided.set_desired_location(target_loc)) {1040// GCS will need to monitor desired location to1041// see if they are having an effect.1042}1043} else if (!vel_ignore && acc_ignore && yaw_ignore && yaw_rate_ignore) {1044// consume velocity1045rover.mode_guided.set_desired_heading_and_speed(target_yaw_cd, speed_dir * target_speed);1046} else if (!vel_ignore && acc_ignore && yaw_ignore && !yaw_rate_ignore) {1047// consume velocity and turn rate1048rover.mode_guided.set_desired_turn_rate_and_speed(target_turn_rate_cds, speed_dir * target_speed);1049} else if (!vel_ignore && acc_ignore && !yaw_ignore && yaw_rate_ignore) {1050// consume velocity and heading1051rover.mode_guided.set_desired_heading_and_speed(target_yaw_cd, speed_dir * target_speed);1052} else if (vel_ignore && acc_ignore && !yaw_ignore && yaw_rate_ignore) {1053// consume just target heading (probably only skid steering vehicles can do this)1054rover.mode_guided.set_desired_heading_and_speed(target_yaw_cd, 0.0f);1055} else if (vel_ignore && acc_ignore && yaw_ignore && !yaw_rate_ignore) {1056// consume just turn rate (probably only skid steering vehicles can do this)1057rover.mode_guided.set_desired_turn_rate_and_speed(target_turn_rate_cds, 0.0f);1058}1059}10601061void GCS_MAVLINK_Rover::handle_set_position_target_global_int(const mavlink_message_t &msg)1062{1063// decode packet1064mavlink_set_position_target_global_int_t packet;1065mavlink_msg_set_position_target_global_int_decode(&msg, &packet);10661067// exit if vehicle is not in Guided mode1068if (!rover.control_mode->in_guided_mode()) {1069return;1070}1071// check for supported coordinate frames1072switch (packet.coordinate_frame) {1073case MAV_FRAME_GLOBAL:1074case MAV_FRAME_GLOBAL_INT:1075case MAV_FRAME_GLOBAL_RELATIVE_ALT:1076case MAV_FRAME_GLOBAL_RELATIVE_ALT_INT:1077case MAV_FRAME_GLOBAL_TERRAIN_ALT:1078case MAV_FRAME_GLOBAL_TERRAIN_ALT_INT:1079break;10801081default:1082return;1083}10841085bool pos_ignore = packet.type_mask & MAVLINK_SET_POS_TYPE_MASK_POS_IGNORE;1086bool vel_ignore = packet.type_mask & MAVLINK_SET_POS_TYPE_MASK_VEL_IGNORE;1087bool acc_ignore = packet.type_mask & MAVLINK_SET_POS_TYPE_MASK_ACC_IGNORE;1088bool yaw_ignore = packet.type_mask & MAVLINK_SET_POS_TYPE_MASK_YAW_IGNORE;1089bool yaw_rate_ignore = packet.type_mask & MAVLINK_SET_POS_TYPE_MASK_YAW_RATE_IGNORE;10901091// prepare target position1092Location target_loc = rover.current_loc;1093if (!pos_ignore) {1094// sanity check location1095if (!check_latlng(packet.lat_int, packet.lon_int)) {1096// result = MAV_RESULT_FAILED;1097return;1098}1099target_loc.lat = packet.lat_int;1100target_loc.lng = packet.lon_int;1101}11021103float target_speed = 0.0f;1104float target_yaw_cd = 0.0f;11051106// consume velocity and convert to target speed and heading1107if (!vel_ignore) {1108const float speed_max = rover.control_mode->get_speed_default();1109// convert vector length into a speed1110target_speed = constrain_float(safe_sqrt(sq(packet.vx) + sq(packet.vy)), -speed_max, speed_max);1111// convert vector direction to target yaw1112target_yaw_cd = degrees(atan2f(packet.vy, packet.vx)) * 100.0f;1113}11141115// consume yaw heading1116if (!yaw_ignore) {1117target_yaw_cd = ToDeg(packet.yaw) * 100.0f;1118}11191120// consume yaw rate1121float target_turn_rate_cds = 0.0f;1122if (!yaw_rate_ignore) {1123target_turn_rate_cds = ToDeg(packet.yaw_rate) * 100.0f;1124}11251126// handling case when both velocity and either yaw or yaw-rate are provided1127// by default, we consider that the rover will drive forward1128float speed_dir = 1.0f;1129if (!vel_ignore && (!yaw_ignore || !yaw_rate_ignore)) {1130// Note: we are using the x-axis velocity to determine direction even though1131// the frame is provided in MAV_FRAME_GLOBAL_xxx1132if (is_negative(packet.vx)) {1133speed_dir = -1.0f;1134}1135}11361137// set guided mode targets1138if (!pos_ignore) {1139// consume position target1140if (!rover.mode_guided.set_desired_location(target_loc)) {1141// GCS will just need to look at desired location1142// outputs to see if it having an effect.1143}1144} else if (!vel_ignore && acc_ignore && yaw_ignore && yaw_rate_ignore) {1145// consume velocity1146rover.mode_guided.set_desired_heading_and_speed(target_yaw_cd, speed_dir * target_speed);1147} else if (!vel_ignore && acc_ignore && yaw_ignore && !yaw_rate_ignore) {1148// consume velocity and turn rate1149rover.mode_guided.set_desired_turn_rate_and_speed(target_turn_rate_cds, speed_dir * target_speed);1150} else if (!vel_ignore && acc_ignore && !yaw_ignore && yaw_rate_ignore) {1151// consume velocity1152rover.mode_guided.set_desired_heading_and_speed(target_yaw_cd, speed_dir * target_speed);1153} else if (vel_ignore && acc_ignore && !yaw_ignore && yaw_rate_ignore) {1154// consume just target heading (probably only skid steering vehicles can do this)1155rover.mode_guided.set_desired_heading_and_speed(target_yaw_cd, 0.0f);1156} else if (vel_ignore && acc_ignore && yaw_ignore && !yaw_rate_ignore) {1157// consume just turn rate(probably only skid steering vehicles can do this)1158rover.mode_guided.set_desired_turn_rate_and_speed(target_turn_rate_cds, 0.0f);1159}1160}11611162/*1163handle a LANDING_TARGET command. The timestamp has been jitter corrected1164*/1165void GCS_MAVLINK_Rover::handle_landing_target(const mavlink_landing_target_t &packet, uint32_t timestamp_ms)1166{1167#if AC_PRECLAND_ENABLED1168rover.precland.handle_msg(packet, timestamp_ms);1169#endif1170}11711172uint64_t GCS_MAVLINK_Rover::capabilities() const1173{1174return (MAV_PROTOCOL_CAPABILITY_MISSION_FLOAT |1175MAV_PROTOCOL_CAPABILITY_MISSION_INT |1176MAV_PROTOCOL_CAPABILITY_COMMAND_INT |1177MAV_PROTOCOL_CAPABILITY_SET_POSITION_TARGET_LOCAL_NED |1178MAV_PROTOCOL_CAPABILITY_SET_POSITION_TARGET_GLOBAL_INT |1179MAV_PROTOCOL_CAPABILITY_SET_ATTITUDE_TARGET |1180GCS_MAVLINK::capabilities());1181}11821183#if HAL_HIGH_LATENCY2_ENABLED1184uint8_t GCS_MAVLINK_Rover::high_latency_tgt_heading() const1185{1186const Mode *control_mode = rover.control_mode;1187if (rover.control_mode->is_autopilot_mode()) {1188// need to convert -180->180 to 0->360/21189return wrap_360(control_mode->wp_bearing()) / 2;1190}1191return 0;1192}11931194uint16_t GCS_MAVLINK_Rover::high_latency_tgt_dist() const1195{1196const Mode *control_mode = rover.control_mode;1197if (rover.control_mode->is_autopilot_mode()) {1198// return units are dm1199return MIN((control_mode->get_distance_to_destination()) / 10, UINT16_MAX);1200}1201return 0;1202}12031204uint8_t GCS_MAVLINK_Rover::high_latency_tgt_airspeed() const1205{1206const Mode *control_mode = rover.control_mode;1207if (rover.control_mode->is_autopilot_mode()) {1208// return units are m/s*51209return MIN((vfr_hud_airspeed() - control_mode->speed_error()) * 5, UINT8_MAX);1210}1211return 0;1212}12131214uint8_t GCS_MAVLINK_Rover::high_latency_wind_speed() const1215{1216if (rover.g2.windvane.enabled()) {1217// return units are m/s*51218return MIN(rover.g2.windvane.get_true_wind_speed() * 5, UINT8_MAX);1219}1220return 0;1221}12221223uint8_t GCS_MAVLINK_Rover::high_latency_wind_direction() const1224{1225if (rover.g2.windvane.enabled()) {1226// return units are deg/21227return wrap_360(degrees(rover.g2.windvane.get_true_wind_direction_rad())) / 2;1228}1229return 0;1230}1231#endif // HAL_HIGH_LATENCY2_ENABLED12321233// Send the mode with the given index (not mode number!) return the total number of modes1234// Index starts at 11235uint8_t GCS_MAVLINK_Rover::send_available_mode(uint8_t index) const1236{1237const Mode* modes[] {1238&rover.mode_manual,1239&rover.mode_acro,1240&rover.mode_steering,1241&rover.mode_hold,1242&rover.mode_loiter,1243#if MODE_FOLLOW_ENABLED1244&rover.mode_follow,1245#endif1246&rover.mode_simple,1247&rover.g2.mode_circle,1248&rover.mode_auto,1249&rover.mode_rtl,1250&rover.mode_smartrtl,1251&rover.mode_guided,1252&rover.mode_initializing,1253#if MODE_DOCK_ENABLED1254(Mode *)rover.g2.mode_dock_ptr,1255#endif1256};12571258const uint8_t mode_count = ARRAY_SIZE(modes);12591260// Convert to zero indexed1261const uint8_t index_zero = index - 1;1262if (index_zero >= mode_count) {1263// Mode does not exist!?1264return mode_count;1265}12661267// Ask the mode for its name and number1268const char* name = modes[index_zero]->name4();1269const uint8_t mode_number = (uint8_t)modes[index_zero]->mode_number();12701271mavlink_msg_available_modes_send(1272chan,1273mode_count,1274index,1275MAV_STANDARD_MODE::MAV_STANDARD_MODE_NON_STANDARD,1276mode_number,12770, // MAV_MODE_PROPERTY bitmask1278name1279);12801281return mode_count;1282}128312841285