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/libraries/AP_Frsky_Telem/AP_Frsky_MAVliteMsgHandler.cpp
Views: 1798
#include "AP_Frsky_MAVliteMsgHandler.h"12#include <AC_Fence/AC_Fence.h>3#include <AP_Vehicle/AP_Vehicle.h>4#include <GCS_MAVLink/GCS.h>56extern const AP_HAL::HAL& hal;78#if HAL_WITH_FRSKY_TELEM_BIDIRECTIONAL9/*10* Handle the COMMAND_LONG mavlite message11* for FrSky SPort Passthrough (OpenTX) protocol (X-receivers)12*/1314void AP_Frsky_MAVliteMsgHandler::handle_command_long(const AP_Frsky_MAVlite_Message &rxmsg)15{16mavlink_command_long_t mav_command_long {};1718uint8_t cmd_options;19float params[7] {};2021if (!rxmsg.get_uint16(mav_command_long.command, 0)) {22return;23}24if (!rxmsg.get_uint8(cmd_options, 2)) {25return;26}27uint8_t param_count = AP_Frsky_MAVlite_Message::bit8_unpack(cmd_options, 3, 0); // first 3 bits2829for (uint8_t cmd_idx=0; cmd_idx<param_count; cmd_idx++) {30// base offset is 3, relative offset is 4*cmd_idx31if (!rxmsg.get_float(params[cmd_idx], 3+(4*cmd_idx))) {32return;33}34}3536mav_command_long.param1 = params[0];37mav_command_long.param2 = params[1];38mav_command_long.param3 = params[2];39mav_command_long.param4 = params[3];40mav_command_long.param5 = params[4];41mav_command_long.param6 = params[5];42mav_command_long.param7 = params[6];4344const MAV_RESULT mav_result = handle_command(mav_command_long);45send_command_ack(mav_result, mav_command_long.command);46}4748MAV_RESULT AP_Frsky_MAVliteMsgHandler::handle_command(const mavlink_command_long_t &mav_command_long)49{50// filter allowed commands51switch (mav_command_long.command) {52//case MAV_CMD_ACCELCAL_VEHICLE_POS:53case MAV_CMD_DO_SET_MODE:54return handle_command_do_set_mode(mav_command_long);55//case MAV_CMD_DO_SET_HOME:56#if AP_FENCE_ENABLED57case MAV_CMD_DO_FENCE_ENABLE:58return handle_command_do_fence_enable(mav_command_long);59#endif60case MAV_CMD_PREFLIGHT_REBOOT_SHUTDOWN:61return handle_command_preflight_reboot(mav_command_long);62//case MAV_CMD_DO_START_MAG_CAL:63//case MAV_CMD_DO_ACCEPT_MAG_CAL:64//case MAV_CMD_DO_CANCEL_MAG_CAL:65//case MAV_CMD_START_RX_PAIR:66//case MAV_CMD_DO_DIGICAM_CONFIGURE:67//case MAV_CMD_DO_DIGICAM_CONTROL:68//case MAV_CMD_DO_SET_CAM_TRIGG_DIST:69//case MAV_CMD_DO_GRIPPER:70//case MAV_CMD_DO_MOUNT_CONFIGURE:71//case MAV_CMD_DO_MOUNT_CONTROL:72//case MAV_CMD_REQUEST_AUTOPILOT_CAPABILITIES:73//case MAV_CMD_DO_SET_ROI_SYSID:74//case MAV_CMD_DO_SET_ROI_LOCATION:75//case MAV_CMD_DO_SET_ROI:76case MAV_CMD_PREFLIGHT_CALIBRATION:77return handle_command_preflight_calibration_baro(mav_command_long);78//case MAV_CMD_BATTERY_RESET:79//case MAV_CMD_PREFLIGHT_UAVCAN:80//case MAV_CMD_FLASH_BOOTLOADER:81//case MAV_CMD_PREFLIGHT_SET_SENSOR_OFFSETS:82//case MAV_CMD_GET_HOME_POSITION:83//case MAV_CMD_PREFLIGHT_STORAGE:84//case MAV_CMD_SET_MESSAGE_INTERVAL:85//case MAV_CMD_GET_MESSAGE_INTERVAL:86//case MAV_CMD_REQUEST_MESSAGE:87//case MAV_CMD_DO_SET_SERVO:88//case MAV_CMD_DO_REPEAT_SERVO:89//case MAV_CMD_DO_SET_RELAY:90//case MAV_CMD_DO_REPEAT_RELAY:91//case MAV_CMD_DO_FLIGHTTERMINATION:92//case MAV_CMD_COMPONENT_ARM_DISARM:93//case MAV_CMD_FIXED_MAG_CAL_YAW:94default:95return MAV_RESULT_UNSUPPORTED;96}97return MAV_RESULT_UNSUPPORTED;98}99100MAV_RESULT AP_Frsky_MAVliteMsgHandler::handle_command_do_set_mode(const mavlink_command_long_t &mav_command_long)101{102if (AP::vehicle()->set_mode(mav_command_long.param1, ModeReason::FRSKY_COMMAND)) {103return MAV_RESULT_ACCEPTED;104}105return MAV_RESULT_FAILED;106}107108void AP_Frsky_MAVliteMsgHandler::send_command_ack(const MAV_RESULT mav_result, const uint16_t cmdid)109{110AP_Frsky_MAVlite_Message txmsg;111txmsg.msgid = MAVLINK_MSG_ID_COMMAND_ACK;112if (!txmsg.set_uint16(cmdid, 0)) {113return;114}115if (!txmsg.set_uint8((uint8_t)mav_result, 2)) {116return;117}118send_message(txmsg);119}120121MAV_RESULT AP_Frsky_MAVliteMsgHandler::handle_command_preflight_calibration_baro(const mavlink_command_long_t &mav_command_long)122{123if (!(is_equal(mav_command_long.param1,0.0f) && is_equal(mav_command_long.param2,0.0f) && is_equal(mav_command_long.param3,1.0f))) {124return MAV_RESULT_FAILED;125}126127if (hal.util->get_soft_armed()) {128return MAV_RESULT_DENIED;129}130// fast barometer calibration131GCS_SEND_TEXT(MAV_SEVERITY_INFO, "Updating barometer calibration");132AP::baro().update_calibration();133GCS_SEND_TEXT(MAV_SEVERITY_INFO, "Barometer calibration complete");134135#if AP_AIRSPEED_ENABLED136AP_Airspeed *airspeed = AP_Airspeed::get_singleton();137if (airspeed != nullptr) {138airspeed->calibrate(false);139}140#endif141142return MAV_RESULT_ACCEPTED;143}144145#if AP_FENCE_ENABLED146MAV_RESULT AP_Frsky_MAVliteMsgHandler::handle_command_do_fence_enable(const mavlink_command_long_t &mav_command_long)147{148AC_Fence *fence = AP::fence();149if (fence == nullptr) {150return MAV_RESULT_UNSUPPORTED;151}152153switch ((uint16_t)mav_command_long.param1) {154case 0:155fence->enable_configured(false);156return MAV_RESULT_ACCEPTED;157case 1:158fence->enable_configured(true);159return MAV_RESULT_ACCEPTED;160default:161return MAV_RESULT_FAILED;162}163}164#endif // AP_FENCE_ENABLED165166/*167* Handle the PARAM_REQUEST_READ mavlite message168* for FrSky SPort Passthrough (OpenTX) protocol (X-receivers)169*/170void AP_Frsky_MAVliteMsgHandler::handle_param_request_read(const AP_Frsky_MAVlite_Message &rxmsg)171{172float param_value;173char param_name[AP_MAX_NAME_SIZE+1];174if (!rxmsg.get_string(param_name, 0)) {175return;176}177// find existing param178if (!AP_Param::get(param_name,param_value)) {179GCS_SEND_TEXT(MAV_SEVERITY_WARNING, "Param read failed (%s)", param_name);180return;181}182AP_Frsky_MAVlite_Message txmsg;183txmsg.msgid = MAVLINK_MSG_ID_PARAM_VALUE;184if (!txmsg.set_float(param_value, 0)) {185return;186}187if (!txmsg.set_string(param_name, 4)) {188return;189}190send_message(txmsg);191}192193/*194* Handle the PARAM_SET mavlite message195* for FrSky SPort Passthrough (OpenTX) protocol (X-receivers)196*/197void AP_Frsky_MAVliteMsgHandler::handle_param_set(const AP_Frsky_MAVlite_Message &rxmsg)198{199float param_value;200char param_name[AP_MAX_NAME_SIZE+1];201// populate packet with mavlite payload202if (!rxmsg.get_float(param_value, 0)) {203return;204}205if (!rxmsg.get_string(param_name, 4)) {206return;207}208// find existing param so we can get the old value209enum ap_var_type var_type;210// set parameter211AP_Param *vp;212uint16_t parameter_flags = 0;213vp = AP_Param::find(param_name, &var_type, ¶meter_flags);214if (vp == nullptr || isnan(param_value) || isinf(param_value)) {215return;216}217if (parameter_flags & AP_PARAM_FLAG_INTERNAL_USE_ONLY) {218// the user can set BRD_OPTIONS to enable set of internal219// parameters, for developer testing or unusual use cases220if (AP_BoardConfig::allow_set_internal_parameters()) {221parameter_flags &= ~AP_PARAM_FLAG_INTERNAL_USE_ONLY;222}223}224if ((parameter_flags & AP_PARAM_FLAG_INTERNAL_USE_ONLY) || vp->is_read_only()) {225GCS_SEND_TEXT(MAV_SEVERITY_WARNING, "Param write denied (%s)", param_name);226} else if (!AP_Param::set_and_save_by_name(param_name, param_value)) {227GCS_SEND_TEXT(MAV_SEVERITY_WARNING, "Param write failed (%s)", param_name);228}229// let's read back the last value, either the readonly one or the updated one230if (!AP_Param::get(param_name,param_value)) {231GCS_SEND_TEXT(MAV_SEVERITY_WARNING, "Param read failed (%s)", param_name);232return;233}234AP_Frsky_MAVlite_Message txmsg;235txmsg.msgid = MAVLINK_MSG_ID_PARAM_VALUE;236if (!txmsg.set_float(param_value, 0)) {237return;238}239if (!txmsg.set_string(param_name, 4)) {240return;241}242send_message(txmsg);243}244245/*246Handle a MAV_CMD_PREFLIGHT_REBOOT_SHUTDOWN command247for FrSky SPort Passthrough (OpenTX) protocol (X-receivers)248Optionally disable PX4IO overrides. This is done for quadplanes to249prevent the mixer running while rebooting which can start the VTOL250motors. That can be dangerous when a preflight reboot is done with251the pilot close to the aircraft and can also damage the aircraft252*/253MAV_RESULT AP_Frsky_MAVliteMsgHandler::handle_command_preflight_reboot(const mavlink_command_long_t &mav_command_long)254{255if (!(is_equal(mav_command_long.param1,1.0f) && is_zero(mav_command_long.param2))) {256return MAV_RESULT_FAILED;257}258if (hal.util->get_soft_armed()) {259// refuse reboot when armed260return MAV_RESULT_FAILED;261}262263AP::vehicle()->reboot(false);264265return MAV_RESULT_FAILED;266}267268/*269* Process an incoming mavlite message270* for FrSky SPort Passthrough (OpenTX) protocol (X-receivers)271*/272void AP_Frsky_MAVliteMsgHandler::process_message(const AP_Frsky_MAVlite_Message &rxmsg)273{274switch (rxmsg.msgid) {275case MAVLINK_MSG_ID_PARAM_REQUEST_READ:276handle_param_request_read(rxmsg);277break;278case MAVLINK_MSG_ID_PARAM_SET:279handle_param_set(rxmsg);280break;281case MAVLINK_MSG_ID_COMMAND_LONG:282handle_command_long(rxmsg);283break;284}285}286287/*288* Send a mavlite message289*/290bool AP_Frsky_MAVliteMsgHandler::send_message(AP_Frsky_MAVlite_Message &txmsg)291{292return _send_fn(txmsg);293}294#endif295296297