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_DroneCAN/AP_DroneCAN.h
Views: 1798
/*1* This file is free software: you can redistribute it and/or modify it2* under the terms of the GNU General Public License as published by the3* Free Software Foundation, either version 3 of the License, or4* (at your option) any later version.5*6* This file is distributed in the hope that it will be useful, but7* WITHOUT ANY WARRANTY; without even the implied warranty of8* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.9* See the GNU General Public License for more details.10*11* You should have received a copy of the GNU General Public License along12* with this program. If not, see <http://www.gnu.org/licenses/>.13*14* Author: Eugene Shamaev, Siddharth Bharat Purohit15*/16#pragma once1718#include <AP_HAL/AP_HAL.h>1920#if HAL_ENABLE_DRONECAN_DRIVERS2122#include "AP_Canard_iface.h"23#include <AP_CANManager/AP_CANManager.h>24#include <AP_HAL/Semaphores.h>25#include <AP_Param/AP_Param.h>26#include <AP_ESC_Telem/AP_ESC_Telem_Backend.h>27#include <SRV_Channel/SRV_Channel_config.h>28#include <canard/publisher.h>29#include <canard/subscriber.h>30#include <canard/service_client.h>31#include <canard/service_server.h>32#include <stdio.h>33#include "AP_DroneCAN_DNA_Server.h"34#include <canard.h>35#include <dronecan_msgs.h>36#include <AP_SerialManager/AP_SerialManager_config.h>37#include <AP_Relay/AP_Relay_config.h>38#include <AP_Servo_Telem/AP_Servo_Telem_config.h>3940#ifndef DRONECAN_SRV_NUMBER41#define DRONECAN_SRV_NUMBER NUM_SERVO_CHANNELS42#endif4344#ifndef AP_DRONECAN_SEND_GPS45#define AP_DRONECAN_SEND_GPS (BOARD_FLASH_SIZE > 1024)46#endif4748#define AP_DRONECAN_SW_VERS_MAJOR 149#define AP_DRONECAN_SW_VERS_MINOR 05051#define AP_DRONECAN_HW_VERS_MAJOR 152#define AP_DRONECAN_HW_VERS_MINOR 0535455#ifndef AP_DRONECAN_HOBBYWING_ESC_SUPPORT56#define AP_DRONECAN_HOBBYWING_ESC_SUPPORT (BOARD_FLASH_SIZE>1024)57#endif5859#ifndef AP_DRONECAN_HIMARK_SERVO_SUPPORT60#define AP_DRONECAN_HIMARK_SERVO_SUPPORT (BOARD_FLASH_SIZE>1024)61#endif6263#ifndef AP_DRONECAN_SERIAL_ENABLED64#define AP_DRONECAN_SERIAL_ENABLED AP_SERIALMANAGER_REGISTER_ENABLED && (BOARD_FLASH_SIZE>1024)65#endif6667#ifndef AP_DRONECAN_VOLZ_FEEDBACK_ENABLED68#define AP_DRONECAN_VOLZ_FEEDBACK_ENABLED 069#endif7071#if AP_DRONECAN_SERIAL_ENABLED72#include "AP_DroneCAN_serial.h"73#endif7475// fwd-declare callback classes76class AP_DroneCAN_DNA_Server;77class CANSensor;7879class AP_DroneCAN : public AP_CANDriver, public AP_ESC_Telem_Backend {80friend class AP_DroneCAN_DNA_Server;81public:82AP_DroneCAN(const int driver_index);83~AP_DroneCAN();8485static const struct AP_Param::GroupInfo var_info[];8687// Return uavcan from @driver_index or nullptr if it's not ready or doesn't exist88static AP_DroneCAN *get_dronecan(uint8_t driver_index);89bool prearm_check(char* fail_msg, uint8_t fail_msg_len) const;9091void init(uint8_t driver_index, bool enable_filters) override;92bool add_interface(AP_HAL::CANIface* can_iface) override;9394// add an 11 bit auxillary driver95bool add_11bit_driver(CANSensor *sensor) override;9697// handler for outgoing frames for auxillary drivers98bool write_aux_frame(AP_HAL::CANFrame &out_frame, const uint32_t timeout_us) override;99100uint8_t get_driver_index() const { return _driver_index; }101102// define string with length structure103struct string { uint8_t len; uint8_t data[128]; };104105FUNCTOR_TYPEDEF(ParamGetSetIntCb, bool, AP_DroneCAN*, const uint8_t, const char*, int32_t &);106FUNCTOR_TYPEDEF(ParamGetSetFloatCb, bool, AP_DroneCAN*, const uint8_t, const char*, float &);107FUNCTOR_TYPEDEF(ParamGetSetStringCb, bool, AP_DroneCAN*, const uint8_t, const char*, string &);108FUNCTOR_TYPEDEF(ParamSaveCb, void, AP_DroneCAN*, const uint8_t, bool);109110void send_node_status();111112///// SRV output /////113void SRV_push_servos(void);114115///// LED /////116bool led_write(uint8_t led_index, uint8_t red, uint8_t green, uint8_t blue);117118// buzzer119void set_buzzer_tone(float frequency, float duration_s);120121// Send Reboot command122// Note: Do not call this from outside UAVCAN thread context,123// you can call this from dronecan callbacks and handlers.124// THIS IS NOT A THREAD SAFE API!125void send_reboot_request(uint8_t node_id);126127// get or set param value128// returns true on success, false on failure129// failures occur when waiting on node to respond to previous get or set request130bool set_parameter_on_node(uint8_t node_id, const char *name, float value, ParamGetSetFloatCb *cb);131bool set_parameter_on_node(uint8_t node_id, const char *name, int32_t value, ParamGetSetIntCb *cb);132bool set_parameter_on_node(uint8_t node_id, const char *name, const string &value, ParamGetSetStringCb *cb);133bool get_parameter_on_node(uint8_t node_id, const char *name, ParamGetSetFloatCb *cb);134bool get_parameter_on_node(uint8_t node_id, const char *name, ParamGetSetIntCb *cb);135bool get_parameter_on_node(uint8_t node_id, const char *name, ParamGetSetStringCb *cb);136137// Save parameters138bool save_parameters_on_node(uint8_t node_id, ParamSaveCb *cb);139140// options bitmask141enum class Options : uint16_t {142DNA_CLEAR_DATABASE = (1U<<0),143DNA_IGNORE_DUPLICATE_NODE = (1U<<1),144CANFD_ENABLED = (1U<<2),145DNA_IGNORE_UNHEALTHY_NODE = (1U<<3),146USE_ACTUATOR_PWM = (1U<<4),147SEND_GNSS = (1U<<5),148USE_HIMARK_SERVO = (1U<<6),149USE_HOBBYWING_ESC = (1U<<7),150ENABLE_STATS = (1U<<8),151ENABLE_FLEX_DEBUG = (1U<<9),152};153154// check if a option is set155bool option_is_set(Options option) const {156return (uint16_t(_options.get()) & uint16_t(option)) != 0;157}158159// check if a option is set and if it is then reset it to160// 0. return true if it was set161bool check_and_reset_option(Options option);162163CanardInterface& get_canard_iface() { return canard_iface; }164165Canard::Publisher<uavcan_equipment_indication_LightsCommand> rgb_led{canard_iface};166Canard::Publisher<uavcan_equipment_indication_BeepCommand> buzzer{canard_iface};167Canard::Publisher<uavcan_equipment_gnss_RTCMStream> rtcm_stream{canard_iface};168169// xacti specific publishers170Canard::Publisher<com_xacti_CopterAttStatus> xacti_copter_att_status{canard_iface};171Canard::Publisher<com_xacti_GimbalControlData> xacti_gimbal_control_data{canard_iface};172Canard::Publisher<com_xacti_GnssStatus> xacti_gnss_status{canard_iface};173174#if AP_RELAY_DRONECAN_ENABLED175// Hardpoint for relay176// Needs to be public so relay can edge trigger as well as streaming177Canard::Publisher<uavcan_equipment_hardpoint_Command> relay_hardpoint{canard_iface};178#endif179180#if AP_SCRIPTING_ENABLED181bool get_FlexDebug(uint8_t node_id, uint16_t msg_id, uint32_t ×tamp_us, dronecan_protocol_FlexDebug &msg) const;182#endif183184private:185void loop(void);186187///// SRV output /////188void SRV_send_actuator();189void SRV_send_esc();190#if AP_DRONECAN_HIMARK_SERVO_SUPPORT191void SRV_send_himark();192#endif193194//scale servo output appropriately before sending195int16_t scale_esc_output(uint8_t idx);196197// SafetyState198void safety_state_send();199200// send notify vehicle state201void notify_state_send();202203// check for parameter get/set response timeout204void check_parameter_callback_timeout();205206// send queued parameter get/set request. called from loop207void send_parameter_request();208209// send queued parameter save request. called from loop210void send_parameter_save_request();211212// periodic logging213void logging();214215// get parameter on a node216ParamGetSetIntCb *param_int_cb; // latest get param request callback function (for integers)217ParamGetSetFloatCb *param_float_cb; // latest get param request callback function (for floats)218ParamGetSetStringCb *param_string_cb; // latest get param request callback function (for strings)219bool param_request_sent = true; // true after a param request has been sent, false when queued to be sent220uint32_t param_request_sent_ms; // system time that get param request was sent221HAL_Semaphore _param_sem; // semaphore protecting this block of variables222uint8_t param_request_node_id; // node id of most recent get param request223224// save parameters on a node225ParamSaveCb *save_param_cb; // latest save param request callback function226bool param_save_request_sent = true; // true after a save param request has been sent, false when queued to be sent227HAL_Semaphore _param_save_sem; // semaphore protecting this block of variables228uint8_t param_save_request_node_id; // node id of most recent save param request229230// UAVCAN parameters231AP_Int8 _dronecan_node;232AP_Int32 _servo_bm;233AP_Int32 _esc_bm;234AP_Int8 _esc_offset;235AP_Int16 _servo_rate_hz;236AP_Int16 _options;237AP_Int16 _notify_state_hz;238AP_Int16 _pool_size;239AP_Int32 _esc_rv;240241uint32_t *mem_pool;242243uint8_t _driver_index;244245CanardInterface canard_iface;246247AP_DroneCAN_DNA_Server _dna_server;248249char _thread_name[13];250bool _initialized;251///// SRV output /////252struct {253uint16_t pulse;254bool esc_pending;255bool servo_pending;256} _SRV_conf[DRONECAN_SRV_NUMBER];257258uint32_t _esc_send_count;259uint32_t _srv_send_count;260uint32_t _fail_send_count;261262uint32_t _SRV_armed_mask; // mask of servo outputs that are active263uint32_t _ESC_armed_mask; // mask of ESC outputs that are active264uint32_t _SRV_last_send_us;265HAL_Semaphore SRV_sem;266267// last log time268uint32_t last_log_ms;269270#if AP_DRONECAN_SEND_GPS271// send GNSS Fix and yaw, same thing AP_GPS_DroneCAN would receive272void gnss_send_fix();273void gnss_send_yaw();274275// GNSS Fix and Status276struct {277uint32_t last_gps_lib_fix_ms;278uint32_t last_send_status_ms;279uint32_t last_lib_yaw_time_ms;280} _gnss;281#endif282283// node status send284uint32_t _node_status_last_send_ms;285286// safety status send state287uint32_t _last_safety_state_ms;288289// notify vehicle state290uint32_t _last_notify_state_ms;291uavcan_protocol_NodeStatus node_status_msg;292293#if AP_RELAY_DRONECAN_ENABLED294void relay_hardpoint_send();295struct {296AP_Int16 rate_hz;297uint32_t last_send_ms;298uint8_t last_index;299} _relay;300#endif301302#if AP_DRONECAN_SERIAL_ENABLED303AP_DroneCAN_Serial serial;304#endif305306Canard::Publisher<uavcan_protocol_NodeStatus> node_status{canard_iface};307Canard::Publisher<dronecan_protocol_CanStats> can_stats{canard_iface};308Canard::Publisher<dronecan_protocol_Stats> protocol_stats{canard_iface};309Canard::Publisher<uavcan_equipment_actuator_ArrayCommand> act_out_array{canard_iface};310Canard::Publisher<uavcan_equipment_esc_RawCommand> esc_raw{canard_iface};311Canard::Publisher<ardupilot_indication_SafetyState> safety_state{canard_iface};312Canard::Publisher<uavcan_equipment_safety_ArmingStatus> arming_status{canard_iface};313Canard::Publisher<ardupilot_indication_NotifyState> notify_state{canard_iface};314315#if AP_DRONECAN_HIMARK_SERVO_SUPPORT316Canard::Publisher<com_himark_servo_ServoCmd> himark_out{canard_iface};317#endif318319#if AP_DRONECAN_SEND_GPS320Canard::Publisher<uavcan_equipment_gnss_Fix2> gnss_fix2{canard_iface};321Canard::Publisher<uavcan_equipment_gnss_Auxiliary> gnss_auxiliary{canard_iface};322Canard::Publisher<ardupilot_gnss_Heading> gnss_heading{canard_iface};323Canard::Publisher<ardupilot_gnss_Status> gnss_status{canard_iface};324#endif325// incoming messages326Canard::ObjCallback<AP_DroneCAN, ardupilot_indication_Button> safety_button_cb{this, &AP_DroneCAN::handle_button};327Canard::Subscriber<ardupilot_indication_Button> safety_button_listener{safety_button_cb, _driver_index};328329Canard::ObjCallback<AP_DroneCAN, ardupilot_equipment_trafficmonitor_TrafficReport> traffic_report_cb{this, &AP_DroneCAN::handle_traffic_report};330Canard::Subscriber<ardupilot_equipment_trafficmonitor_TrafficReport> traffic_report_listener{traffic_report_cb, _driver_index};331332#if AP_SERVO_TELEM_ENABLED333Canard::ObjCallback<AP_DroneCAN, uavcan_equipment_actuator_Status> actuator_status_cb{this, &AP_DroneCAN::handle_actuator_status};334Canard::Subscriber<uavcan_equipment_actuator_Status> actuator_status_listener{actuator_status_cb, _driver_index};335#endif336337Canard::ObjCallback<AP_DroneCAN, uavcan_equipment_esc_Status> esc_status_cb{this, &AP_DroneCAN::handle_ESC_status};338Canard::Subscriber<uavcan_equipment_esc_Status> esc_status_listener{esc_status_cb, _driver_index};339340#if AP_EXTENDED_ESC_TELEM_ENABLED341Canard::ObjCallback<AP_DroneCAN, uavcan_equipment_esc_StatusExtended> esc_status_extended_cb{this, &AP_DroneCAN::handle_esc_ext_status};342Canard::Subscriber<uavcan_equipment_esc_StatusExtended> esc_status_extended_listener{esc_status_extended_cb, _driver_index};343#endif344345Canard::ObjCallback<AP_DroneCAN, uavcan_protocol_debug_LogMessage> debug_cb{this, &AP_DroneCAN::handle_debug};346Canard::Subscriber<uavcan_protocol_debug_LogMessage> debug_listener{debug_cb, _driver_index};347348#if AP_DRONECAN_HIMARK_SERVO_SUPPORT && AP_SERVO_TELEM_ENABLED349Canard::ObjCallback<AP_DroneCAN, com_himark_servo_ServoInfo> himark_servo_ServoInfo_cb{this, &AP_DroneCAN::handle_himark_servoinfo};350Canard::Subscriber<com_himark_servo_ServoInfo> himark_servo_ServoInfo_cb_listener{himark_servo_ServoInfo_cb, _driver_index};351#endif352#if AP_DRONECAN_VOLZ_FEEDBACK_ENABLED353Canard::ObjCallback<AP_DroneCAN, com_volz_servo_ActuatorStatus> volz_servo_ActuatorStatus_cb{this, &AP_DroneCAN::handle_actuator_status_Volz};354Canard::Subscriber<com_volz_servo_ActuatorStatus> volz_servo_ActuatorStatus_listener{volz_servo_ActuatorStatus_cb, _driver_index};355#endif356357// param client358Canard::ObjCallback<AP_DroneCAN, uavcan_protocol_param_GetSetResponse> param_get_set_res_cb{this, &AP_DroneCAN::handle_param_get_set_response};359Canard::Client<uavcan_protocol_param_GetSetResponse> param_get_set_client{canard_iface, param_get_set_res_cb};360361Canard::ObjCallback<AP_DroneCAN, uavcan_protocol_param_ExecuteOpcodeResponse> param_save_res_cb{this, &AP_DroneCAN::handle_param_save_response};362Canard::Client<uavcan_protocol_param_ExecuteOpcodeResponse> param_save_client{canard_iface, param_save_res_cb};363364// reboot client365void handle_restart_node_response(const CanardRxTransfer& transfer, const uavcan_protocol_RestartNodeResponse& msg) {}366Canard::ObjCallback<AP_DroneCAN, uavcan_protocol_RestartNodeResponse> restart_node_res_cb{this, &AP_DroneCAN::handle_restart_node_response};367Canard::Client<uavcan_protocol_RestartNodeResponse> restart_node_client{canard_iface, restart_node_res_cb};368369uavcan_protocol_param_ExecuteOpcodeRequest param_save_req;370uavcan_protocol_param_GetSetRequest param_getset_req;371372// Node Info Server373Canard::ObjCallback<AP_DroneCAN, uavcan_protocol_GetNodeInfoRequest> node_info_req_cb{this, &AP_DroneCAN::handle_node_info_request};374Canard::Server<uavcan_protocol_GetNodeInfoRequest> node_info_server{canard_iface, node_info_req_cb};375uavcan_protocol_GetNodeInfoResponse node_info_rsp;376377#if AP_SCRIPTING_ENABLED378Canard::ObjCallback<AP_DroneCAN, dronecan_protocol_FlexDebug> FlexDebug_cb{this, &AP_DroneCAN::handle_FlexDebug};379Canard::Subscriber<dronecan_protocol_FlexDebug> FlexDebug_listener{FlexDebug_cb, _driver_index};380#endif381382#if AP_DRONECAN_HOBBYWING_ESC_SUPPORT383/*384Hobbywing ESC support. Note that we need additional meta-data as385the status messages do not have an ESC ID in them, so we need a386mapping from node ID387*/388#define HOBBYWING_MAX_ESC 8389struct {390uint32_t last_GetId_send_ms;391uint8_t thr_chan[HOBBYWING_MAX_ESC];392} hobbywing;393void hobbywing_ESC_update();394395void SRV_send_esc_hobbywing();396Canard::Publisher<com_hobbywing_esc_RawCommand> esc_hobbywing_raw{canard_iface};397Canard::Publisher<com_hobbywing_esc_GetEscID> esc_hobbywing_GetEscID{canard_iface};398Canard::ObjCallback<AP_DroneCAN, com_hobbywing_esc_GetEscID> esc_hobbywing_GetEscID_cb{this, &AP_DroneCAN::handle_hobbywing_GetEscID};399Canard::Subscriber<com_hobbywing_esc_GetEscID> esc_hobbywing_GetEscID_listener{esc_hobbywing_GetEscID_cb, _driver_index};400Canard::ObjCallback<AP_DroneCAN, com_hobbywing_esc_StatusMsg1> esc_hobbywing_StatusMSG1_cb{this, &AP_DroneCAN::handle_hobbywing_StatusMsg1};401Canard::Subscriber<com_hobbywing_esc_StatusMsg1> esc_hobbywing_StatusMSG1_listener{esc_hobbywing_StatusMSG1_cb, _driver_index};402Canard::ObjCallback<AP_DroneCAN, com_hobbywing_esc_StatusMsg2> esc_hobbywing_StatusMSG2_cb{this, &AP_DroneCAN::handle_hobbywing_StatusMsg2};403Canard::Subscriber<com_hobbywing_esc_StatusMsg2> esc_hobbywing_StatusMSG2_listener{esc_hobbywing_StatusMSG2_cb, _driver_index};404bool hobbywing_find_esc_index(uint8_t node_id, uint8_t &esc_index) const;405void handle_hobbywing_GetEscID(const CanardRxTransfer& transfer, const com_hobbywing_esc_GetEscID& msg);406void handle_hobbywing_StatusMsg1(const CanardRxTransfer& transfer, const com_hobbywing_esc_StatusMsg1& msg);407void handle_hobbywing_StatusMsg2(const CanardRxTransfer& transfer, const com_hobbywing_esc_StatusMsg2& msg);408#endif // AP_DRONECAN_HOBBYWING_ESC_SUPPORT409410#if AP_DRONECAN_HIMARK_SERVO_SUPPORT && AP_SERVO_TELEM_ENABLED411void handle_himark_servoinfo(const CanardRxTransfer& transfer, const com_himark_servo_ServoInfo &msg);412#endif413414// incoming button handling415void handle_button(const CanardRxTransfer& transfer, const ardupilot_indication_Button& msg);416void handle_traffic_report(const CanardRxTransfer& transfer, const ardupilot_equipment_trafficmonitor_TrafficReport& msg);417#if AP_SERVO_TELEM_ENABLED418void handle_actuator_status(const CanardRxTransfer& transfer, const uavcan_equipment_actuator_Status& msg);419#endif420#if AP_DRONECAN_VOLZ_FEEDBACK_ENABLED421void handle_actuator_status_Volz(const CanardRxTransfer& transfer, const com_volz_servo_ActuatorStatus& msg);422#endif423void handle_ESC_status(const CanardRxTransfer& transfer, const uavcan_equipment_esc_Status& msg);424#if AP_EXTENDED_ESC_TELEM_ENABLED425void handle_esc_ext_status(const CanardRxTransfer& transfer, const uavcan_equipment_esc_StatusExtended& msg);426#endif427static bool is_esc_data_index_valid(const uint8_t index);428void handle_debug(const CanardRxTransfer& transfer, const uavcan_protocol_debug_LogMessage& msg);429void handle_param_get_set_response(const CanardRxTransfer& transfer, const uavcan_protocol_param_GetSetResponse& rsp);430void handle_param_save_response(const CanardRxTransfer& transfer, const uavcan_protocol_param_ExecuteOpcodeResponse& rsp);431void handle_node_info_request(const CanardRxTransfer& transfer, const uavcan_protocol_GetNodeInfoRequest& req);432433#if AP_SCRIPTING_ENABLED434void handle_FlexDebug(const CanardRxTransfer& transfer, const dronecan_protocol_FlexDebug& msg);435struct FlexDebug {436struct FlexDebug *next;437uint32_t timestamp_us;438uint8_t node_id;439dronecan_protocol_FlexDebug msg;440} *flexDebug_list;441#endif442};443444#endif // #if HAL_ENABLE_DRONECAN_DRIVERS445446447