AP_UAVCAN.cpp 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552
  1. /*
  2. * This file is free software: you can redistribute it and/or modify it
  3. * under the terms of the GNU General Public License as published by the
  4. * Free Software Foundation, either version 3 of the License, or
  5. * (at your option) any later version.
  6. *
  7. * This file is distributed in the hope that it will be useful, but
  8. * WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. * See the GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License along
  13. * with this program. If not, see <http://www.gnu.org/licenses/>.
  14. *
  15. * Author: Eugene Shamaev, Siddharth Bharat Purohit
  16. */
  17. #include <AP_Common/AP_Common.h>
  18. #include <AP_HAL/AP_HAL.h>
  19. #if HAL_WITH_UAVCAN
  20. #include "AP_UAVCAN.h"
  21. #include <GCS_MAVLink/GCS.h>
  22. #include <AP_Motors/AP_Motors.h> //电机类
  23. #include "../../ArduSub/Sub.h"
  24. #include <AP_BoardConfig/AP_BoardConfig.h>
  25. #include <AP_BoardConfig/AP_BoardConfig_CAN.h>
  26. #include <uavcan/transport/can_acceptance_filter_configurator.hpp>
  27. #include <uavcan/equipment/actuator/ArrayCommand.hpp>
  28. #include <uavcan/equipment/actuator/Command.hpp>
  29. #include <uavcan/equipment/actuator/Status.hpp>
  30. //#include <uavcan/equipment/esc/MotorCommands.hpp>
  31. #include <uavcan/equipment/esc/Motor1ResCommand.hpp>
  32. #include <uavcan/equipment/esc/SetFloatParam.hpp>
  33. #include <uavcan/equipment/esc/SingleMotorCommand.hpp>
  34. #include <uavcan/equipment/esc/GetFloatParam1.hpp>
  35. //hight volt HighVoltThrusterSetRegs.hpp
  36. #include <uavcan/equipment/esc/Bfd1.hpp>
  37. #include <uavcan/equipment/esc/Bfd2.hpp>
  38. #include <uavcan/equipment/esc/Bfd3.hpp>
  39. #include <uavcan/equipment/esc/Clear.hpp>
  40. #include <uavcan/equipment/esc/HighVoltThrusterSetRegs.hpp>
  41. #include <uavcan/equipment/esc/HighVoltThruster1GetRegs.hpp>
  42. #include <uavcan/equipment/esc/HighVoltThruster2GetRegs.hpp>
  43. #include <uavcan/equipment/esc/HighVoltThruster3GetRegs.hpp>
  44. #include <uavcan/equipment/esc/HighVoltThruster1GetRegsFujian.hpp>
  45. #include <uavcan/equipment/esc/HighVoltThruster2GetRegsFujian.hpp>
  46. #include <uavcan/equipment/esc/HighVoltThruster3GetRegsFujian.hpp>
  47. #include <uavcan/equipment/esc/SpeedCommands.hpp>
  48. #include <uavcan/equipment/esc/HighVoltThruster1timeback.hpp>
  49. #include <uavcan/equipment/esc/HighVoltThruster2timeback.hpp>
  50. #include <uavcan/equipment/esc/HighVoltThruster3timeback.hpp>
  51. //#include <uavcan/equipment/esc/HighVoltThruster1timebackST.hpp>
  52. //#include <uavcan/equipment/esc/HighVoltThruster2timebackST.hpp>
  53. //#include <uavcan/equipment/esc/HighVoltThruster3timebackST.hpp>
  54. #include <uavcan/equipment/esc/RawCommand.hpp>
  55. #include <uavcan/equipment/indication/LightsCommand.hpp>
  56. #include <uavcan/equipment/indication/SingleLightCommand.hpp>
  57. #include <uavcan/equipment/indication/BeepCommand.hpp>
  58. #include <uavcan/equipment/indication/RGB565.hpp>
  59. #include <ardupilot/indication/SafetyState.hpp>
  60. #include <ardupilot/indication/Button.hpp>
  61. #include <AP_Baro/AP_Baro_UAVCAN.h>
  62. #include <AP_RangeFinder/AP_RangeFinder_UAVCAN.h>
  63. #include <AP_GPS/AP_GPS_UAVCAN.h>
  64. #include <AP_BattMonitor/AP_BattMonitor_UAVCAN.h>
  65. #include <AP_Compass/AP_Compass_UAVCAN.h>
  66. #include <AP_Airspeed/AP_Airspeed_UAVCAN.h>
  67. #include <SRV_Channel/SRV_Channel.h>
  68. #include <AP_OpticalFlow/AP_OpticalFlow_HereFlow.h>
  69. #define LED_DELAY_US 50000
  70. extern const AP_HAL::HAL& hal;
  71. #define debug_uavcan(level_debug, fmt, args...) do { if ((level_debug) <= AP::can().get_debug_level_driver(_driver_index)) { printf(fmt, ##args); }} while (0)
  72. // Translation of all messages from UAVCAN structures into AP structures is done
  73. // in AP_UAVCAN and not in corresponding drivers.
  74. // The overhead of including definitions of DSDL is very high and it is best to
  75. // concentrate in one place.
  76. // table of user settable CAN bus parameters
  77. const AP_Param::GroupInfo AP_UAVCAN::var_info[] = {
  78. // @Param: NODE
  79. // @DisplayName: UAVCAN node that is used for this network
  80. // @Description: UAVCAN node should be set implicitly
  81. // @Range: 1 250
  82. // @User: Advanced
  83. AP_GROUPINFO("NODE", 1, AP_UAVCAN, _uavcan_node, 10),
  84. // @Param: SRV_BM
  85. // @DisplayName: RC Out channels to be transmitted as servo over UAVCAN
  86. // @Description: Bitmask with one set for channel to be transmitted as a servo command over UAVCAN
  87. // @Bitmask: 0: Servo 1, 1: Servo 2, 2: Servo 3, 3: Servo 4, 4: Servo 5, 5: Servo 6, 6: Servo 7, 7: Servo 8, 8: Servo 9, 9: Servo 10, 10: Servo 11, 11: Servo 12, 12: Servo 13, 13: Servo 14, 14: Servo 15
  88. // @User: Advanced
  89. AP_GROUPINFO("SRV_BM", 2, AP_UAVCAN, _servo_bm, 0),
  90. // @Param: ESC_BM
  91. // @DisplayName: RC Out channels to be transmitted as ESC over UAVCAN
  92. // @Description: Bitmask with one set for channel to be transmitted as a ESC command over UAVCAN
  93. // @Bitmask: 0: ESC 1, 1: ESC 2, 2: ESC 3, 3: ESC 4, 4: ESC 5, 5: ESC 6, 6: ESC 7, 7: ESC 8, 8: ESC 9, 9: ESC 10, 10: ESC 11, 11: ESC 12, 12: ESC 13, 13: ESC 14, 14: ESC 15, 15: ESC 16
  94. // @User: Advanced
  95. AP_GROUPINFO("ESC_BM", 3, AP_UAVCAN, _esc_bm, 0),
  96. // @Param: SRV_RT
  97. // @DisplayName: Servo output rate
  98. // @Description: Maximum transmit rate for servo outputs
  99. // @Range: 1 200
  100. // @Units: Hz
  101. // @User: Advanced
  102. AP_GROUPINFO("SRV_RT", 4, AP_UAVCAN, _servo_rate_hz, 50),
  103. AP_GROUPEND
  104. };
  105. // this is the timeout in milliseconds for periodic message types. We
  106. // set this to 1 to minimise resend of stale msgs
  107. #define CAN_PERIODIC_TX_TIMEOUT_MS 2
  108. //自定义stm32 参数设置协议--------
  109. static uavcan::Publisher<uavcan::equipment::esc::HighVoltThrusterSetRegs>* HVoltThtSetReg[MAX_NUMBER_OF_CAN_DRIVERS];
  110. static uavcan::Publisher<uavcan::equipment::esc::Bfd1>* esc_Bfd1[MAX_NUMBER_OF_CAN_DRIVERS];
  111. static uavcan::Publisher<uavcan::equipment::esc::Bfd2>* esc_Bfd2[MAX_NUMBER_OF_CAN_DRIVERS];
  112. static uavcan::Publisher<uavcan::equipment::esc::Bfd3>* esc_Bfd3[MAX_NUMBER_OF_CAN_DRIVERS];
  113. static uavcan::Publisher<uavcan::equipment::esc::Clear>* clearFault[MAX_NUMBER_OF_CAN_DRIVERS];
  114. //static uavcan::Publisher<uavcan::equipment::esc::MotorCommands>* esc_MotorCommands[MAX_NUMBER_OF_CAN_DRIVERS];
  115. static uavcan::Publisher<uavcan::equipment::esc::SetFloatParam>* SetFloatParam[MAX_NUMBER_OF_CAN_DRIVERS];
  116. static uavcan::Publisher<uavcan::equipment::esc::SpeedCommands>* esc_SpeedCommands[MAX_NUMBER_OF_CAN_DRIVERS];
  117. //自定义stm32 参数设置协议--------
  118. // publisher interfaces
  119. static uavcan::Publisher<uavcan::equipment::actuator::ArrayCommand>* act_out_array[MAX_NUMBER_OF_CAN_DRIVERS];
  120. static uavcan::Publisher<uavcan::equipment::esc::RawCommand>* esc_raw[MAX_NUMBER_OF_CAN_DRIVERS];
  121. static uavcan::Publisher<uavcan::equipment::indication::LightsCommand>* rgb_led[MAX_NUMBER_OF_CAN_DRIVERS];
  122. static uavcan::Publisher<uavcan::equipment::indication::BeepCommand>* buzzer[MAX_NUMBER_OF_CAN_DRIVERS];
  123. static uavcan::Publisher<ardupilot::indication::SafetyState>* safety_state[MAX_NUMBER_OF_CAN_DRIVERS];
  124. // subscribers
  125. // handler SafteyButton
  126. UC_REGISTRY_BINDER(ButtonCb, ardupilot::indication::Button);
  127. static uavcan::Subscriber<ardupilot::indication::Button, ButtonCb> *safety_button_listener[MAX_NUMBER_OF_CAN_DRIVERS];
  128. //接收局部全局变量-----------
  129. int16_t motors_receive[12]={0,0,0,0,0,0,0,0,0,0,0,0}; //STM32发来的速度
  130. uint8_t crc_motors[42];
  131. union DRIVEBOX_ABNORMAL_UNI motor_stall_state;//获取所有的堵转
  132. union PROPELLER_ABNORMAL_UNI propeller_block;//推进器缠绕等级
  133. union PROPELLER_ABNORMAL_UNI track_block;//推进器缠绕等级
  134. int16_t temperature_power = 0;//温度
  135. int16_t voltage48V= 0;//48V电源
  136. //int16_t driveleak = 0;//泄露
  137. extern mavlink_set_slave_parameter_t set_stm32_param;
  138. extern mavlink_rov_state_monitoring_t rov_message;
  139. extern mavlink_hv_reg_get_t hv_reg_get;
  140. extern mavlink_hv_reg_set_t hv_reg_set;
  141. extern mavlink_motor_speed_t mav_motor_speed;
  142. //uint8_t mode_flag = 0;
  143. uint8_t get_stm32_param_buf[7] = {0,0,0,0,0,0,0};// 板1收到的参数
  144. //hv_motor_runmode.all = 0xFFFF;
  145. uint8_t hv_motor_cmd_temp = 0;
  146. uint8_t hv_motor_cmd = 0;
  147. int16_t hv_motor_speed_32 = 0;
  148. int16_t lightstate=0;
  149. int16_t usbl_power=0;
  150. int16_t light_leak=0;
  151. #define reboot_time 3000
  152. //----------------------------------------------
  153. float getfloat(mavlink_set_slave_parameter_t par);
  154. void STM32_set_packet(uavcan::equipment::esc::SetFloatParam &_msgparam);
  155. void crc_setmotor(uavcan::equipment::esc::HighVoltThrusterSetRegs &hvmesg);
  156. void crc_ackfault(uavcan::equipment::esc::HighVoltThrusterSetRegs &hvmesg,uint8_t number);
  157. AP_UAVCAN::AP_UAVCAN() :
  158. _node_allocator()
  159. {
  160. AP_Param::setup_object_defaults(this, var_info);
  161. _singleton = this;
  162. for (uint8_t i = 0; i < UAVCAN_SRV_NUMBER; i++) {
  163. _SRV_conf[i].esc_pending = false;
  164. _SRV_conf[i].servo_pending = false;
  165. }
  166. sublightstate = 0;
  167. trackblock_flag =0;
  168. propellerblock_flag =0;
  169. motor_stall_state.all = 0;
  170. propeller_block.all = 0;
  171. track_block.all = 0;
  172. for (uint8_t cnt = 0; cnt < 12; cnt++)
  173. {
  174. motors_receive[cnt]=0;
  175. }
  176. for(uint8_t j=0;j<12;j++){
  177. motor_from_stm32[j] = 0;//读到转速
  178. }
  179. debug_uavcan(2, "AP_UAVCAN constructed\n\r");
  180. }
  181. AP_UAVCAN::~AP_UAVCAN()
  182. {
  183. }
  184. AP_UAVCAN *AP_UAVCAN::get_uavcan(uint8_t driver_index)
  185. {
  186. if (driver_index >= AP::can().get_num_drivers() ||
  187. AP::can().get_protocol_type(driver_index) != AP_BoardConfig_CAN::Protocol_Type_UAVCAN) {
  188. return nullptr;
  189. }
  190. return static_cast<AP_UAVCAN*>(AP::can().get_driver(driver_index));
  191. }
  192. //self define rec fun-------------------------------start---------------
  193. static void motor_res_st_cb(const uavcan::ReceivedDataStructure<uavcan::equipment::esc::Motor1ResCommand>& msg,uint8_t mgr )
  194. {//接收到的数据在这里处理,打印出来
  195. if (msg.motor_index == 0){
  196. //推进器0,1,2
  197. motors_receive[0] = msg.rpm0 & 0x0FFF;//推进器0
  198. propeller_block.bit.motor0 = (msg.rpm0 & 0xF000)>>12;
  199. motors_receive[1] = msg.rpm1 & 0x0FFF;//推进器1
  200. propeller_block.bit.motor1 = (msg.rpm1 & 0xF000)>>12;
  201. }
  202. else if(msg.motor_index == 3){
  203. //推进器0.1.履带左的给定
  204. int16_t rmp1 = msg.rpm0;
  205. int16_t rmp2 = msg.rpm1;
  206. int16_t rmp3 = msg.rpm2;
  207. static int16_t c2 = 0;
  208. c2++;
  209. if(c2>50){
  210. c2 =0 ;
  211. gcs().send_text(MAV_SEVERITY_WARNING, "motor_index == 3 %d %d %d.",(int)rmp1,(int)rmp2,(int)rmp3);
  212. }
  213. }
  214. else if(msg.motor_index == 6){
  215. int16_t rpm = msg.rpm0;//履带右的给定
  216. motors_receive[8] = msg.rpm2 & 0x0FFF;//履带1
  217. track_block.bit.motor0 = (msg.rpm2 & 0xF000)>>12;
  218. static int16_t c3 = 0;
  219. c3++;
  220. if(c3>50){
  221. c3 =0 ;
  222. gcs().send_text(MAV_SEVERITY_WARNING, "motor_index == 6 %d %d ",(int)rpm,motors_receive[8]);
  223. }
  224. }
  225. else if(msg.motor_index == 9){
  226. ////履带2
  227. motors_receive[9] = msg.rpm0 & 0x0FFF;//履带2速度
  228. track_block.bit.motor1 = (msg.rpm0 & 0xF000)>>12;
  229. static int16_t c4 = 0;
  230. c4++;
  231. if(c4>100){
  232. c4 =0 ;
  233. gcs().send_text(MAV_SEVERITY_WARNING, "motor_index == 9 %d.",(int)motors_receive[9]);
  234. }
  235. }
  236. else if(msg.motor_index == 12){
  237. ////
  238. voltage48V = msg.rpm0;
  239. temperature_power = msg.rpm1;
  240. motor_stall_state.all = msg.rpm2;//所有电机的堵转// 履带没有堵转了
  241. static int16_t c5 = 0;
  242. c5++;
  243. if(c5>100){
  244. c5 =0 ;
  245. //gcs().send_text(MAV_SEVERITY_WARNING, "motor_index == 12 %d %d %x.",(int)voltage48V,(int)temperature_power,motor_stall_state.all);
  246. }
  247. }
  248. else if(msg.motor_index == 15){
  249. ////履带2,毛刷1,毛刷2
  250. lightstate = msg.rpm0;
  251. usbl_power = msg.rpm1;
  252. light_leak = msg.rpm2;//灯 漏水
  253. }
  254. }
  255. static void motor_res_st_cb0(const uavcan::ReceivedDataStructure<uavcan::equipment::esc::Motor1ResCommand>& msg ){
  256. motor_res_st_cb(msg,0);
  257. }
  258. static void motor_res_st_cb1(const uavcan::ReceivedDataStructure<uavcan::equipment::esc::Motor1ResCommand>& msg ){
  259. motor_res_st_cb(msg,1);
  260. }
  261. static void (*motor_res_st_cb_arr[2])(const uavcan::ReceivedDataStructure<uavcan::equipment::esc::Motor1ResCommand>& msg )
  262. ={motor_res_st_cb0,motor_res_st_cb1};
  263. //==========================================================================================================================
  264. //获取stm32板1参数------XXXXXXXX.YY-------------------------
  265. float getfloat(mavlink_set_slave_parameter_t par){
  266. float data =(par.XX1/16)*10000000+(par.XX1%16)*1000000 +
  267. (par.XX2/16)*100000+(par.XX2%16)*10000+
  268. (par.XX3/16)*1000+(par.XX3%16)*100 +
  269. (par.XX4/16)*10 + (par.XX4%16) +
  270. (par.YY/16)*0.1 +(par.YY%16)*0.01;
  271. return data;
  272. }
  273. static void motor_par1_cb(const uavcan::ReceivedDataStructure<uavcan::equipment::esc::GetFloatParam1>& msg,uint8_t mgr ){
  274. get_stm32_param_buf[0] = msg.number;
  275. get_stm32_param_buf[1] = msg.flag;
  276. get_stm32_param_buf[2] = msg.XX1;
  277. get_stm32_param_buf[3] = msg.XX2;
  278. get_stm32_param_buf[4] = msg.XX3;
  279. get_stm32_param_buf[5] = msg.XX4;
  280. get_stm32_param_buf[6] = msg.YY;
  281. }
  282. static void motor_par1_cb0(const uavcan::ReceivedDataStructure<uavcan::equipment::esc::GetFloatParam1>& msg ){
  283. motor_par1_cb(msg,0);
  284. }
  285. static void motor_par1_cb1(const uavcan::ReceivedDataStructure<uavcan::equipment::esc::GetFloatParam1>& msg ){
  286. motor_par1_cb(msg,1);
  287. }
  288. static void (*motor_par1_cb_arr[2])(const uavcan::ReceivedDataStructure<uavcan::equipment::esc::GetFloatParam1>& msg )
  289. ={motor_par1_cb0,motor_par1_cb1};
  290. //高压电调1参数返回
  291. static void thruster1_1GetRegs_cb(const uavcan::ReceivedDataStructure<uavcan::equipment::esc::HighVoltThruster1GetRegs>& msg,uint8_t mgr ){
  292. uint8_t buffer[7]={0,0,0,0,0,0,0};
  293. for (uint8_t i = 0; i < 7; i++)
  294. {
  295. buffer[i] = msg.BUFFER[i];
  296. }
  297. hv_reg_get.aim = 3;
  298. hv_reg_get.data[0] = (uint32_t)buffer[1] + (uint32_t)(buffer[2]<<8) + (uint32_t)(buffer[3]<<16) + (uint32_t)(buffer[4]<<24);
  299. hv_reg_get.data[1] = 0;
  300. hv_reg_get.data[2] = 0;
  301. gcs().send_text(MAV_SEVERITY_WARNING, "thruster3 %x .",(uint32_t)hv_reg_get.data[0]);
  302. }
  303. static void Thruster1_1GetRegs_cb0(const uavcan::ReceivedDataStructure<uavcan::equipment::esc::HighVoltThruster1GetRegs>& msg ){
  304. thruster1_1GetRegs_cb(msg,0);
  305. }
  306. static void Thruster1_1GetRegs_cb1(const uavcan::ReceivedDataStructure<uavcan::equipment::esc::HighVoltThruster1GetRegs>& msg ){
  307. thruster1_1GetRegs_cb(msg,1);
  308. }
  309. static void (*Thruster1_1GetRegs_cb_arr[2])(const uavcan::ReceivedDataStructure<uavcan::equipment::esc::HighVoltThruster1GetRegs>& msg )
  310. ={Thruster1_1GetRegs_cb0,Thruster1_1GetRegs_cb1};
  311. //福建
  312. static void thruster1GetRegs_cb(const uavcan::ReceivedDataStructure<uavcan::equipment::esc::HighVoltThruster1GetRegsFujian>& msg,uint8_t mgr ){
  313. uint8_t buffer[7]={0,0,0,0,0,0,0};
  314. for (uint8_t i = 0; i < 7; i++)
  315. {
  316. buffer[i] = msg.BUFFER[i];
  317. }
  318. hv_reg_get.aim = 3;
  319. hv_reg_get.data[0] = (uint32_t)buffer[1] + (uint32_t)(buffer[2]<<8) + (uint32_t)(buffer[3]<<16) + (uint32_t)(buffer[4]<<24);
  320. hv_reg_get.data[1] = 0;
  321. hv_reg_get.data[2] = 0;
  322. //gcs().send_text(MAV_SEVERITY_WARNING, "thruster3 %x .",(uint32_t)hv_reg_get.data[0]);
  323. }
  324. static void Thruster1GetRegs_cb0(const uavcan::ReceivedDataStructure<uavcan::equipment::esc::HighVoltThruster1GetRegsFujian>& msg ){
  325. thruster1GetRegs_cb(msg,0);
  326. }
  327. static void Thruster1GetRegs_cb1(const uavcan::ReceivedDataStructure<uavcan::equipment::esc::HighVoltThruster1GetRegsFujian>& msg ){
  328. thruster1GetRegs_cb(msg,1);
  329. }
  330. static void (*Thruster1GetRegs_cb_arr[2])(const uavcan::ReceivedDataStructure<uavcan::equipment::esc::HighVoltThruster1GetRegsFujian>& msg )
  331. ={Thruster1GetRegs_cb0,Thruster1GetRegs_cb1};
  332. //高压电调2参数返回
  333. static void thruster2_1GetRegs_cb(const uavcan::ReceivedDataStructure<uavcan::equipment::esc::HighVoltThruster2GetRegs>& msg ,uint8_t mgr){
  334. uint8_t buffer[7]={0,0,0,0,0,0,0};
  335. for (uint8_t i = 0; i < 7; i++)
  336. {
  337. buffer[i] = msg.BUFFER[i];
  338. }
  339. hv_reg_get.aim = 4;
  340. hv_reg_get.data[0] = (uint32_t)buffer[1] + (uint32_t)(buffer[2]<<8) + (uint32_t)(buffer[3]<<16) + (uint32_t)(buffer[4]<<24);
  341. hv_reg_get.data[1] = 0;
  342. hv_reg_get.data[2] = 0;
  343. gcs().send_text(MAV_SEVERITY_WARNING, "thruster4 %x .",(uint32_t)hv_reg_get.data[0]);
  344. }
  345. static void Thruster2_1GetRegs_cb0(const uavcan::ReceivedDataStructure<uavcan::equipment::esc::HighVoltThruster2GetRegs>& msg ){
  346. thruster2_1GetRegs_cb(msg,0);
  347. }
  348. static void Thruster2_1GetRegs_cb1(const uavcan::ReceivedDataStructure<uavcan::equipment::esc::HighVoltThruster2GetRegs>& msg ){
  349. thruster2_1GetRegs_cb(msg,1);
  350. }
  351. static void (*Thruster2_1GetRegs_cb_arr[2])(const uavcan::ReceivedDataStructure<uavcan::equipment::esc::HighVoltThruster2GetRegs>& msg )
  352. ={Thruster2_1GetRegs_cb0,Thruster2_1GetRegs_cb1};
  353. //福建
  354. static void thruster2GetRegs_cb(const uavcan::ReceivedDataStructure<uavcan::equipment::esc::HighVoltThruster2GetRegsFujian>& msg ,uint8_t mgr){
  355. uint8_t buffer[7]={0,0,0,0,0,0,0};
  356. for (uint8_t i = 0; i < 7; i++)
  357. {
  358. buffer[i] = msg.BUFFER[i];
  359. }
  360. hv_reg_get.aim = 4;
  361. hv_reg_get.data[0] = (uint32_t)buffer[1] + (uint32_t)(buffer[2]<<8) + (uint32_t)(buffer[3]<<16) + (uint32_t)(buffer[4]<<24);
  362. hv_reg_get.data[1] = 0;
  363. hv_reg_get.data[2] = 0;
  364. //gcs().send_text(MAV_SEVERITY_WARNING, "thruster4 %x .",(uint32_t)hv_reg_get.data[0]);
  365. }
  366. static void Thruster2GetRegs_cb0(const uavcan::ReceivedDataStructure<uavcan::equipment::esc::HighVoltThruster2GetRegsFujian>& msg ){
  367. thruster2GetRegs_cb(msg,0);
  368. }
  369. static void Thruster2GetRegs_cb1(const uavcan::ReceivedDataStructure<uavcan::equipment::esc::HighVoltThruster2GetRegsFujian>& msg ){
  370. thruster2GetRegs_cb(msg,1);
  371. }
  372. static void (*Thruster2GetRegs_cb_arr[2])(const uavcan::ReceivedDataStructure<uavcan::equipment::esc::HighVoltThruster2GetRegsFujian>& msg )
  373. ={Thruster2GetRegs_cb0,Thruster2GetRegs_cb1};
  374. //高压电调3参数返回
  375. static void thruster3_1GetRegs_cb(const uavcan::ReceivedDataStructure<uavcan::equipment::esc::HighVoltThruster3GetRegs>& msg ,uint8_t mgr){
  376. uint8_t buffer[7]={0,0,0,0,0,0,0};
  377. for (uint8_t i = 0; i < 7; i++)
  378. {
  379. buffer[i] = msg.BUFFER[i];
  380. }
  381. hv_reg_get.aim = 5;
  382. hv_reg_get.data[0] = (uint32_t)buffer[1] + (uint32_t)(buffer[2]<<8) + (uint32_t)(buffer[3]<<16) + (uint32_t)(buffer[4]<<24);
  383. hv_reg_get.data[1] = 0;
  384. hv_reg_get.data[2] = 0;
  385. gcs().send_text(MAV_SEVERITY_WARNING, "thruster5 %x .",(uint32_t)hv_reg_get.data[0]);
  386. //gcs().send_text(MAV_SEVERITY_WARNING, "thruster3data %d %d %d %d.",(int)buffer[0],(int)buffer[1],(int)buffer[2],(int)buffer[3]);
  387. //gcs().send_text(MAV_SEVERITY_WARNING, "thruster3 %d %d %d.",hv_reg_get.aim,(int)data[0],(int)data[1]);
  388. }
  389. static void Thruster3_1GetRegs_cb0(const uavcan::ReceivedDataStructure<uavcan::equipment::esc::HighVoltThruster3GetRegs>& msg ){
  390. thruster3_1GetRegs_cb(msg,0);
  391. }
  392. static void Thruster3_1GetRegs_cb1(const uavcan::ReceivedDataStructure<uavcan::equipment::esc::HighVoltThruster3GetRegs>& msg ){
  393. thruster3_1GetRegs_cb(msg,1);
  394. }
  395. static void (*Thruster3_1GetRegs_cb_arr[2])(const uavcan::ReceivedDataStructure<uavcan::equipment::esc::HighVoltThruster3GetRegs>& msg )
  396. ={Thruster3_1GetRegs_cb0,Thruster3_1GetRegs_cb1};
  397. //福建
  398. static void thruster3GetRegs_cb(const uavcan::ReceivedDataStructure<uavcan::equipment::esc::HighVoltThruster3GetRegsFujian>& msg ,uint8_t mgr){
  399. uint8_t buffer[7]={0,0,0,0,0,0,0};
  400. for (uint8_t i = 0; i < 7; i++)
  401. {
  402. buffer[i] = msg.BUFFER[i];
  403. }
  404. hv_reg_get.aim = 5;
  405. hv_reg_get.data[0] = (uint32_t)buffer[1] + (uint32_t)(buffer[2]<<8) + (uint32_t)(buffer[3]<<16) + (uint32_t)(buffer[4]<<24);
  406. hv_reg_get.data[1] = 0;
  407. hv_reg_get.data[2] = 0;
  408. //gcs().send_text(MAV_SEVERITY_WARNING, "thruster5 %x .",(uint32_t)hv_reg_get.data[0]);
  409. //gcs().send_text(MAV_SEVERITY_WARNING, "thruster3data %d %d %d %d.",(int)buffer[0],(int)buffer[1],(int)buffer[2],(int)buffer[3]);
  410. //gcs().send_text(MAV_SEVERITY_WARNING, "thruster3 %d %d %d.",hv_reg_get.aim,(int)data[0],(int)data[1]);
  411. }
  412. static void Thruster3GetRegs_cb0(const uavcan::ReceivedDataStructure<uavcan::equipment::esc::HighVoltThruster3GetRegsFujian>& msg ){
  413. thruster3GetRegs_cb(msg,0);
  414. }
  415. static void Thruster3GetRegs_cb1(const uavcan::ReceivedDataStructure<uavcan::equipment::esc::HighVoltThruster3GetRegsFujian>& msg ){
  416. thruster3GetRegs_cb(msg,1);
  417. }
  418. static void (*Thruster3GetRegs_cb_arr[2])(const uavcan::ReceivedDataStructure<uavcan::equipment::esc::HighVoltThruster3GetRegsFujian>& msg )
  419. ={Thruster3GetRegs_cb0,Thruster3GetRegs_cb1};
  420. //高压电调1定时返回
  421. struct HVmes Thruster1={0,0,0,0,0,0};
  422. struct HVmes Thruster2={0,0,0,0,0,0};
  423. struct HVmes Thruster3={0,0,0,0,0,0};
  424. struct HVmotor haoye1={0,0,0,0,0,0};
  425. struct HVmotor haoye2={0,0,0,0,0,0};
  426. //static void Thruster1timeback_cb(const uavcan::ReceivedDataStructure<uavcan::equipment::esc::HighVoltThruster1timebackST>& msg ,uint8_t mgr){
  427. static void Thruster1timeback_cb(const uavcan::ReceivedDataStructure<uavcan::equipment::esc::HighVoltThruster1timeback>& msg ,uint8_t mgr){
  428. uint8_t buffer[6]={0,0,0,0,0,0};
  429. for (uint8_t i = 0; i < 6; i++)
  430. {
  431. buffer[i] = msg.BUFFER[i];
  432. }
  433. HVcycle(msg.CODE,buffer,Thruster1);
  434. //if(msg.CODE == 0x0A){
  435. //int16_t speedmes = buffer[4]+(buffer[5]<<8);
  436. static int16_t per = 0;
  437. per++;
  438. if (per>150)
  439. {
  440. per = 0;
  441. gcs().send_text(MAV_SEVERITY_INFO, "HV3 %d %d %d %d %d",(int)Thruster1.voltage,(int)Thruster1.speedref,(int)Thruster1.temperature,(int)Thruster1.currentlimit,(int)Thruster1.Imax/336);
  442. }
  443. //}
  444. }
  445. //static void Thruster1timeback_cb0(const uavcan::ReceivedDataStructure<uavcan::equipment::esc::HighVoltThruster1timebackST>& msg ){
  446. static void Thruster1timeback_cb0(const uavcan::ReceivedDataStructure<uavcan::equipment::esc::HighVoltThruster1timeback>& msg ){
  447. Thruster1timeback_cb(msg,0);
  448. }
  449. //static void Thruster1timeback_cb1(const uavcan::ReceivedDataStructure<uavcan::equipment::esc::HighVoltThruster1timebackST>& msg ){
  450. static void Thruster1timeback_cb1(const uavcan::ReceivedDataStructure<uavcan::equipment::esc::HighVoltThruster1timeback>& msg ){
  451. Thruster1timeback_cb(msg,1);
  452. }
  453. //static void (*Thruster1timeback_cb_arr[2])(const uavcan::ReceivedDataStructure<uavcan::equipment::esc::HighVoltThruster1timebackST>& msg )
  454. static void (*Thruster1timeback_cb_arr[2])(const uavcan::ReceivedDataStructure<uavcan::equipment::esc::HighVoltThruster1timeback>& msg )
  455. ={Thruster1timeback_cb0,Thruster1timeback_cb1};
  456. //高压电调2定时返回
  457. //static void Thruster2timeback_cb(const uavcan::ReceivedDataStructure<uavcan::equipment::esc::HighVoltThruster2timebackST>& msg ,uint8_t mgr){
  458. static void Thruster2timeback_cb(const uavcan::ReceivedDataStructure<uavcan::equipment::esc::HighVoltThruster2timeback>& msg ,uint8_t mgr){
  459. uint8_t buffer[6]={0,0,0,0,0,0};
  460. for (uint8_t i = 0; i < 6; i++)
  461. {
  462. buffer[i] = msg.BUFFER[i];
  463. }
  464. HVcycle(msg.CODE,buffer,Thruster2);
  465. //if(msg.CODE == 0x0A){
  466. //int16_t speedmes = buffer[4]+(buffer[5]<<8);
  467. static int16_t per = 0;
  468. per++;
  469. if (per>150)
  470. {
  471. per = 0;
  472. gcs().send_text(MAV_SEVERITY_INFO, "HV4 %d %d %d %d %d",(int)Thruster2.voltage,(int)Thruster2.speedref,(int)Thruster2.temperature,(int)Thruster2.currentlimit,(int)Thruster2.Imax/336);
  473. }
  474. //}
  475. }
  476. //static void Thruster2timeback_cb0(const uavcan::ReceivedDataStructure<uavcan::equipment::esc::HighVoltThruster2timebackST>& msg ){
  477. static void Thruster2timeback_cb0(const uavcan::ReceivedDataStructure<uavcan::equipment::esc::HighVoltThruster2timeback>& msg ){
  478. Thruster2timeback_cb(msg,0);
  479. }
  480. //static void Thruster2timeback_cb1(const uavcan::ReceivedDataStructure<uavcan::equipment::esc::HighVoltThruster2timebackST>& msg ){
  481. static void Thruster2timeback_cb1(const uavcan::ReceivedDataStructure<uavcan::equipment::esc::HighVoltThruster2timeback>& msg ){
  482. Thruster2timeback_cb(msg,1);
  483. }
  484. //static void (*Thruster2timeback_cb_arr[2])(const uavcan::ReceivedDataStructure<uavcan::equipment::esc::HighVoltThruster2timebackST>& msg )
  485. static void (*Thruster2timeback_cb_arr[2])(const uavcan::ReceivedDataStructure<uavcan::equipment::esc::HighVoltThruster2timeback>& msg )
  486. ={Thruster2timeback_cb0,Thruster2timeback_cb1};
  487. //高压电调2定时返回
  488. //static void Thruster3timeback_cb(const uavcan::ReceivedDataStructure<uavcan::equipment::esc::HighVoltThruster3timebackST>& msg ,uint8_t mgr){
  489. static void Thruster3timeback_cb(const uavcan::ReceivedDataStructure<uavcan::equipment::esc::HighVoltThruster3timeback>& msg ,uint8_t mgr){
  490. uint8_t buffer[6]={0,0,0,0,0,0};
  491. for (uint8_t i = 0; i < 6; i++)
  492. {
  493. buffer[i] = msg.BUFFER[i];
  494. }
  495. HVcycle(msg.CODE,buffer,Thruster3);
  496. //if(msg.CODE == 0x0A){
  497. //int16_t speedmes = buffer[4]+(buffer[5]<<8);
  498. static int16_t per = 0;
  499. per++;
  500. if (per>150)
  501. {
  502. per = 0;
  503. gcs().send_text(MAV_SEVERITY_INFO, "HV5 %d %d %d %d %d",(int)Thruster3.voltage,(int)Thruster3.speedref,(int)Thruster3.temperature,(int)Thruster3.currentlimit,(int)Thruster3.Imax/336);
  504. }
  505. //}
  506. }
  507. //static void Thruster3timeback_cb0(const uavcan::ReceivedDataStructure<uavcan::equipment::esc::HighVoltThruster3timebackST>& msg ){
  508. static void Thruster3timeback_cb0(const uavcan::ReceivedDataStructure<uavcan::equipment::esc::HighVoltThruster3timeback>& msg ){
  509. Thruster3timeback_cb(msg,0);
  510. }
  511. //static void Thruster3timeback_cb1(const uavcan::ReceivedDataStructure<uavcan::equipment::esc::HighVoltThruster3timebackST>& msg ){
  512. static void Thruster3timeback_cb1(const uavcan::ReceivedDataStructure<uavcan::equipment::esc::HighVoltThruster3timeback>& msg ){
  513. Thruster3timeback_cb(msg,1);
  514. }
  515. //static void (*Thruster3timeback_cb_arr[2])(const uavcan::ReceivedDataStructure<uavcan::equipment::esc::HighVoltThruster3timebackST>& msg )
  516. static void (*Thruster3timeback_cb_arr[2])(const uavcan::ReceivedDataStructure<uavcan::equipment::esc::HighVoltThruster3timeback>& msg )
  517. ={Thruster3timeback_cb0,Thruster3timeback_cb1};
  518. //self define rec fun-------------------------------end---------------
  519. void AP_UAVCAN::init(uint8_t driver_index, bool enable_filters)
  520. {
  521. //hal.scheduler->delay_microseconds(1000);
  522. if (_initialized) {
  523. debug_uavcan(2, "UAVCAN: init called more than once\n\r");
  524. return;
  525. }
  526. _driver_index = driver_index;
  527. AP_HAL::CANManager* can_mgr = hal.can_mgr[driver_index];
  528. if (can_mgr == nullptr) {
  529. debug_uavcan(2, "UAVCAN: init called for inexisting CAN driver\n\r");
  530. return;
  531. }
  532. if (!can_mgr->is_initialized()) {
  533. debug_uavcan(1, "UAVCAN: CAN driver not initialized\n\r");
  534. return;
  535. }
  536. uavcan::ICanDriver* driver = can_mgr->get_driver();
  537. if (driver == nullptr) {
  538. debug_uavcan(2, "UAVCAN: can't get UAVCAN interface driver\n\r");
  539. return;
  540. }
  541. _node = new uavcan::Node<0>(*driver, SystemClock::instance(), _node_allocator);
  542. if (_node == nullptr) {
  543. debug_uavcan(1, "UAVCAN: couldn't allocate node\n\r");
  544. return;
  545. }
  546. if (_node->isStarted()) {
  547. debug_uavcan(2, "UAVCAN: node was already started?\n\r");
  548. return;
  549. }
  550. uavcan::NodeID self_node_id(_uavcan_node);
  551. _node->setNodeID(self_node_id);
  552. char ndname[20];
  553. snprintf(ndname, sizeof(ndname), "org.ardupilot:%u", driver_index);
  554. uavcan::NodeStatusProvider::NodeName name(ndname);
  555. _node->setName(name);
  556. uavcan::protocol::SoftwareVersion sw_version; // Standard type uavcan.protocol.SoftwareVersion
  557. sw_version.major = AP_UAVCAN_SW_VERS_MAJOR;
  558. sw_version.minor = AP_UAVCAN_SW_VERS_MINOR;
  559. _node->setSoftwareVersion(sw_version);
  560. uavcan::protocol::HardwareVersion hw_version; // Standard type uavcan.protocol.HardwareVersion
  561. hw_version.major = AP_UAVCAN_HW_VERS_MAJOR;
  562. hw_version.minor = AP_UAVCAN_HW_VERS_MINOR;
  563. const uint8_t uid_buf_len = hw_version.unique_id.capacity();
  564. uint8_t uid_len = uid_buf_len;
  565. uint8_t unique_id[uid_buf_len];
  566. if (hal.util->get_system_id_unformatted(unique_id, uid_len)) {
  567. uavcan::copy(unique_id, unique_id + uid_len, hw_version.unique_id.begin());
  568. }
  569. _node->setHardwareVersion(hw_version);
  570. int start_res = _node->start();
  571. if (start_res < 0) {
  572. debug_uavcan(1, "UAVCAN: node start problem, error %d\n\r", start_res);
  573. return;
  574. }
  575. //Start Servers
  576. #ifdef HAS_UAVCAN_SERVERS
  577. _servers.init(*_node);
  578. #endif
  579. // Roundup all subscribers from supported drivers
  580. AP_GPS_UAVCAN::subscribe_msgs(this);
  581. AP_Compass_UAVCAN::subscribe_msgs(this);
  582. AP_Baro_UAVCAN::subscribe_msgs(this);
  583. AP_BattMonitor_UAVCAN::subscribe_msgs(this);
  584. AP_Airspeed_UAVCAN::subscribe_msgs(this);
  585. AP_OpticalFlow_HereFlow::subscribe_msgs(this);
  586. AP_RangeFinder_UAVCAN::subscribe_msgs(this);
  587. //创建设备类型------------------
  588. auto *node= get_node();
  589. uavcan::Subscriber<uavcan::equipment::esc::Motor1ResCommand>* motor_res_st;
  590. motor_res_st = new uavcan::Subscriber<uavcan::equipment::esc::Motor1ResCommand>(*node);
  591. const int motor_res_start_res = motor_res_st->start(motor_res_st_cb_arr[_driver_index]);//定义回调函数用于接收
  592. if (motor_res_start_res<0)
  593. {
  594. return;
  595. }
  596. //创建设备类型获取stm32参数1
  597. uavcan::Subscriber<uavcan::equipment::esc::GetFloatParam1>* motor_par1_st;
  598. motor_par1_st = new uavcan::Subscriber<uavcan::equipment::esc::GetFloatParam1>(*node);
  599. const int motor_par1_res = motor_par1_st->start(motor_par1_cb_arr[_driver_index]);//定义回调函数用于接收
  600. if (motor_par1_res<0)
  601. {
  602. return;
  603. }
  604. //高压电调1 参数返回------------------------------------------------------------
  605. uavcan::Subscriber<uavcan::equipment::esc::HighVoltThruster1GetRegs>* Thruster1_1GetRegs;
  606. Thruster1_1GetRegs = new uavcan::Subscriber<uavcan::equipment::esc::HighVoltThruster1GetRegs>(*node);
  607. const int Tht1_1GetReg = Thruster1_1GetRegs->start(Thruster1_1GetRegs_cb_arr[_driver_index]);//定义回调函数用于接收
  608. if (Tht1_1GetReg<0)
  609. {
  610. return;
  611. }
  612. //高压电调2 参数返回
  613. uavcan::Subscriber<uavcan::equipment::esc::HighVoltThruster2GetRegs>* Thruster2_1GetRegs;
  614. Thruster2_1GetRegs = new uavcan::Subscriber<uavcan::equipment::esc::HighVoltThruster2GetRegs>(*node);
  615. const int Tht2_1GetReg = Thruster2_1GetRegs->start(Thruster2_1GetRegs_cb_arr[_driver_index]);//定义回调函数用于接收
  616. if (Tht2_1GetReg<0)
  617. {
  618. return;
  619. }
  620. //高压电调3 参数返回
  621. uavcan::Subscriber<uavcan::equipment::esc::HighVoltThruster3GetRegs>* Thruster3_1GetRegs;
  622. Thruster3_1GetRegs = new uavcan::Subscriber<uavcan::equipment::esc::HighVoltThruster3GetRegs>(*node);
  623. const int Tht3_1GetReg = Thruster3_1GetRegs->start(Thruster3_1GetRegs_cb_arr[_driver_index]);//定义回调函数用于接收
  624. if (Tht3_1GetReg<0)
  625. {
  626. return;
  627. }//以下是福建的--------------------------
  628. //高压电调1 参数返回
  629. uavcan::Subscriber<uavcan::equipment::esc::HighVoltThruster1GetRegsFujian>* Thruster1GetRegs;
  630. Thruster1GetRegs = new uavcan::Subscriber<uavcan::equipment::esc::HighVoltThruster1GetRegsFujian>(*node);
  631. const int Tht1GetReg = Thruster1GetRegs->start(Thruster1GetRegs_cb_arr[_driver_index]);//定义回调函数用于接收
  632. if (Tht1GetReg<0)
  633. {
  634. return;
  635. }
  636. //高压电调2 参数返回
  637. uavcan::Subscriber<uavcan::equipment::esc::HighVoltThruster2GetRegsFujian>* Thruster2GetRegs;
  638. Thruster2GetRegs = new uavcan::Subscriber<uavcan::equipment::esc::HighVoltThruster2GetRegsFujian>(*node);
  639. const int Tht2GetReg = Thruster2GetRegs->start(Thruster2GetRegs_cb_arr[_driver_index]);//定义回调函数用于接收
  640. if (Tht2GetReg<0)
  641. {
  642. return;
  643. }
  644. //高压电调3 参数返回
  645. uavcan::Subscriber<uavcan::equipment::esc::HighVoltThruster3GetRegsFujian>* Thruster3GetRegs;
  646. Thruster3GetRegs = new uavcan::Subscriber<uavcan::equipment::esc::HighVoltThruster3GetRegsFujian>(*node);
  647. const int Tht3GetReg = Thruster3GetRegs->start(Thruster3GetRegs_cb_arr[_driver_index]);//定义回调函数用于接收
  648. if (Tht3GetReg<0)
  649. {
  650. return;
  651. }
  652. //高压电调1 定时参数返回
  653. uavcan::Subscriber<uavcan::equipment::esc::HighVoltThruster1timeback>* Thruster1timeback;
  654. Thruster1timeback = new uavcan::Subscriber<uavcan::equipment::esc::HighVoltThruster1timeback>(*node);
  655. const int Tht1timecb = Thruster1timeback->start(Thruster1timeback_cb_arr[_driver_index]);//定义回调函数用于接收
  656. if (Tht1timecb<0)
  657. {
  658. return;
  659. }
  660. //高压电调2 定时参数返回
  661. uavcan::Subscriber<uavcan::equipment::esc::HighVoltThruster2timeback>* Thruster2timeback;
  662. Thruster2timeback = new uavcan::Subscriber<uavcan::equipment::esc::HighVoltThruster2timeback>(*node);
  663. const int Tht2timecb = Thruster2timeback->start(Thruster2timeback_cb_arr[_driver_index]);//定义回调函数用于接收
  664. if (Tht2timecb<0)
  665. {
  666. return;
  667. }
  668. //高压电调3 定时参数返回
  669. uavcan::Subscriber<uavcan::equipment::esc::HighVoltThruster3timeback>* Thruster3timeback;
  670. Thruster3timeback = new uavcan::Subscriber<uavcan::equipment::esc::HighVoltThruster3timeback>(*node);
  671. const int Tht3timecb = Thruster3timeback->start(Thruster3timeback_cb_arr[_driver_index]);//定义回调函数用于接收
  672. if (Tht3timecb<0)
  673. {
  674. return;
  675. }
  676. //创建发送设备类型
  677. esc_SpeedCommands[driver_index] = new uavcan::Publisher<uavcan::equipment::esc::SpeedCommands>(*_node);
  678. esc_SpeedCommands[driver_index]->setTxTimeout(uavcan::MonotonicDuration::fromMSec(2));
  679. esc_SpeedCommands[driver_index]->setPriority(uavcan::TransferPriority::OneLowerThanHighest);
  680. //HVoltThtSetReg
  681. //高压电调清故障
  682. clearFault[driver_index] = new uavcan::Publisher<uavcan::equipment::esc::Clear>(*_node);
  683. clearFault[driver_index]->setTxTimeout(uavcan::MonotonicDuration::fromMSec(20));
  684. clearFault[driver_index]->setPriority(uavcan::TransferPriority::OneLowerThanHighest);
  685. //高压电调参数设置
  686. HVoltThtSetReg[driver_index] = new uavcan::Publisher<uavcan::equipment::esc::HighVoltThrusterSetRegs>(*_node);
  687. HVoltThtSetReg[driver_index]->setTxTimeout(uavcan::MonotonicDuration::fromMSec(20));
  688. HVoltThtSetReg[driver_index]->setPriority(uavcan::TransferPriority::OneLowerThanHighest);
  689. //创建发送设备类型
  690. esc_Bfd1[driver_index] = new uavcan::Publisher<uavcan::equipment::esc::Bfd1>(*_node);
  691. esc_Bfd1[driver_index]->setTxTimeout(uavcan::MonotonicDuration::fromMSec(2));
  692. esc_Bfd1[driver_index]->setPriority(uavcan::TransferPriority::OneLowerThanHighest);
  693. esc_Bfd2[driver_index] = new uavcan::Publisher<uavcan::equipment::esc::Bfd2>(*_node);
  694. esc_Bfd2[driver_index]->setTxTimeout(uavcan::MonotonicDuration::fromMSec(2));
  695. esc_Bfd2[driver_index]->setPriority(uavcan::TransferPriority::OneLowerThanHighest);
  696. esc_Bfd3[driver_index] = new uavcan::Publisher<uavcan::equipment::esc::Bfd3>(*_node);
  697. esc_Bfd3[driver_index]->setTxTimeout(uavcan::MonotonicDuration::fromMSec(2));
  698. esc_Bfd3[driver_index]->setPriority(uavcan::TransferPriority::OneLowerThanHighest);
  699. //esc_MotorCommands[driver_index] = new uavcan::Publisher<uavcan::equipment::esc::MotorCommands>(*_node);
  700. // esc_MotorCommands[driver_index]->setTxTimeout(uavcan::MonotonicDuration::fromMSec(2));
  701. // esc_MotorCommands[driver_index]->setPriority(uavcan::TransferPriority::OneLowerThanHighest);
  702. //创建参数设置类型
  703. SetFloatParam[driver_index] = new uavcan::Publisher<uavcan::equipment::esc::SetFloatParam>(*_node);
  704. SetFloatParam[driver_index]->setTxTimeout(uavcan::MonotonicDuration::fromMSec(2));
  705. SetFloatParam[driver_index]->setPriority(uavcan::TransferPriority::OneLowerThanHighest);
  706. //-----------------------------------
  707. act_out_array[driver_index] = new uavcan::Publisher<uavcan::equipment::actuator::ArrayCommand>(*_node);
  708. act_out_array[driver_index]->setTxTimeout(uavcan::MonotonicDuration::fromMSec(2));
  709. act_out_array[driver_index]->setPriority(uavcan::TransferPriority::OneLowerThanHighest);
  710. esc_raw[driver_index] = new uavcan::Publisher<uavcan::equipment::esc::RawCommand>(*_node);
  711. esc_raw[driver_index]->setTxTimeout(uavcan::MonotonicDuration::fromMSec(2));
  712. esc_raw[driver_index]->setPriority(uavcan::TransferPriority::OneLowerThanHighest);
  713. rgb_led[driver_index] = new uavcan::Publisher<uavcan::equipment::indication::LightsCommand>(*_node);
  714. rgb_led[driver_index]->setTxTimeout(uavcan::MonotonicDuration::fromMSec(20));
  715. rgb_led[driver_index]->setPriority(uavcan::TransferPriority::OneHigherThanLowest);
  716. buzzer[driver_index] = new uavcan::Publisher<uavcan::equipment::indication::BeepCommand>(*_node);
  717. buzzer[driver_index]->setTxTimeout(uavcan::MonotonicDuration::fromMSec(20));
  718. buzzer[driver_index]->setPriority(uavcan::TransferPriority::OneHigherThanLowest);
  719. safety_state[driver_index] = new uavcan::Publisher<ardupilot::indication::SafetyState>(*_node);
  720. safety_state[driver_index]->setTxTimeout(uavcan::MonotonicDuration::fromMSec(20));
  721. safety_state[driver_index]->setPriority(uavcan::TransferPriority::OneHigherThanLowest);
  722. safety_button_listener[driver_index] = new uavcan::Subscriber<ardupilot::indication::Button, ButtonCb>(*_node);
  723. if (safety_button_listener[driver_index]) {
  724. safety_button_listener[driver_index]->start(ButtonCb(this, &handle_button));
  725. }
  726. _led_conf.devices_count = 0;
  727. if (enable_filters) {
  728. configureCanAcceptanceFilters(*_node);
  729. }
  730. /*
  731. * Informing other nodes that we're ready to work.
  732. * Default mode is INITIALIZING.
  733. */
  734. _node->setModeOperational();
  735. // Spin node for device discovery
  736. _node->spin(uavcan::MonotonicDuration::fromMSec(5000));
  737. snprintf(_thread_name, sizeof(_thread_name), "uavcan_%u", driver_index);
  738. if (!hal.scheduler->thread_create(FUNCTOR_BIND_MEMBER(&AP_UAVCAN::loop, void), _thread_name, 4096, AP_HAL::Scheduler::PRIORITY_CAN, 0)) {
  739. _node->setModeOfflineAndPublish();
  740. debug_uavcan(1, "UAVCAN: couldn't create thread\n\r");
  741. return;
  742. }
  743. _initialized = true;
  744. debug_uavcan(2, "UAVCAN: init done\n\r");
  745. }
  746. void STM32_set_packet(uavcan::equipment::esc::SetFloatParam &_msgparam){
  747. _msgparam.number =set_stm32_param.number;
  748. _msgparam.flag = set_stm32_param.flag;
  749. _msgparam.XX1 = set_stm32_param.XX1;
  750. _msgparam.XX2 = set_stm32_param.XX2;
  751. _msgparam.XX3 = set_stm32_param.XX3;
  752. _msgparam.XX4 = set_stm32_param.XX4;
  753. _msgparam.YY = set_stm32_param.YY;
  754. }
  755. void crc_setmotor(uavcan::equipment::esc::HighVoltThrusterSetRegs &hvmesg){
  756. uint8_t crc_dataset[10]={0,0,0,0,0,0,0,0,0,0};
  757. hvmesg.NUMBER = hv_reg_set.aim;
  758. hvmesg.TYPE = hv_reg_set.code;
  759. hvmesg.PARNUMBER = (uint16_t)hv_reg_set.data[0] + (uint16_t)(hv_reg_set.data[1]<<8);
  760. crc_dataset[0] = hvmesg.NUMBER ;
  761. crc_dataset[1] = hvmesg.TYPE;
  762. crc_dataset[2] = hv_reg_set.data[0];
  763. crc_dataset[3] = hv_reg_set.data[1];
  764. for (uint8_t j = 0; j < 6; j++)
  765. {
  766. hvmesg.DATA[j] = hv_reg_set.data[j+2];
  767. crc_dataset[j+4] = hvmesg.DATA[j];
  768. }
  769. uint16_t crcdata = calc_crc_modbus(crc_dataset,10);
  770. hvmesg.DATA[6] = (uint8_t)crcdata;
  771. hvmesg.DATA[7] = (uint8_t)(crcdata>>8);
  772. }
  773. void crc_ackfault(uavcan::equipment::esc::HighVoltThrusterSetRegs &hvmesg,uint8_t number){
  774. uint8_t crc_dataset[10]={0,0,0,0,0,0,0,0,0,0};
  775. hvmesg.NUMBER = number;
  776. hvmesg.TYPE = 0x03;
  777. hvmesg.PARNUMBER = 0x5A5A;
  778. crc_dataset[0] = hvmesg.NUMBER ;
  779. crc_dataset[1] = hvmesg.TYPE;
  780. crc_dataset[2] = 0x5A;
  781. crc_dataset[3] = 0x5A;
  782. for (uint8_t j = 0; j < 6; j++)
  783. {
  784. hvmesg.DATA[j] = 0;
  785. crc_dataset[j+4] = hvmesg.DATA[j];
  786. }
  787. uint16_t crcdata = calc_crc_modbus(crc_dataset,10);
  788. hvmesg.DATA[6] = (uint8_t)crcdata;
  789. hvmesg.DATA[7] = (uint8_t)(crcdata>>8);
  790. }
  791. uint8_t HVreset = 0;
  792. void AP_UAVCAN::loop(void)
  793. {
  794. while (true) {
  795. if (!_initialized) {
  796. hal.scheduler->delay_microseconds(1000);
  797. continue;
  798. }
  799. const int error = _node->spin(uavcan::MonotonicDuration::fromMSec(1));
  800. if (error < 0) {
  801. hal.scheduler->delay_microseconds(100);
  802. continue;
  803. }
  804. //接收的周期性数据--------------------
  805. for(uint8_t j=0;j<12;j++){
  806. motor_from_stm32[j] = motors_receive[j];//读到转速
  807. }
  808. motor_stall_flag = motor_stall_state.all;//堵转
  809. propellerblock_flag = propeller_block.all;//推进器缠绕等级
  810. trackblock_flag = track_block.all;//推进器缠绕等级
  811. temperature_48Vpower = temperature_power;//can获取18B20温度
  812. board_voltage = voltage48V;//can获取48V电源
  813. driverleakstate = light_leak;//灯 漏水
  814. sublightstate = lightstate;
  815. HVmotor1 = Thruster1;
  816. HVmotor2 = Thruster2;
  817. HVmotor3 = Thruster3;
  818. rov_message.hvMotorMod = 0xffff;
  819. uavcan::equipment::esc::HighVoltThrusterSetRegs hvmesg;//高压电调参数设置
  820. //2-------------------aim取值0,1,2,3 高压电调设置
  821. if(hv_reg_set.aim == 0 || (hv_reg_set.aim &0x0F) !=0){
  822. crc_setmotor(hvmesg);
  823. hv_reg_set.aim = 0xF0;
  824. HVoltThtSetReg[_driver_index]->broadcast(hvmesg);//高压电调读取或者设置参数
  825. }
  826. //----------------------------------------------------------
  827. //2---------------高压电调设置end*******************************************
  828. //---------重启设置---------------------------
  829. static uint8_t stm32_rboot = 0;
  830. if((set_stm32_param.flag == 0xF0)&&(set_stm32_param.number ==12)){
  831. set_stm32_param.flag = 0;
  832. //消除缠绕
  833. stm32_rboot = 1;
  834. //高压电调消除故障
  835. HVreset = 7;
  836. uavcan::equipment::esc::Clear Clearesg;//高压电调参数设置
  837. clearFault[_driver_index]->broadcast(Clearesg);//高压电调读取或者设置参数
  838. }
  839. //-------发送数据-------------------------------------------------
  840. const AP_Motors6DOF &motors6dof = AP::motors6dof();//6自由度电机计算出来的PWM
  841. uavcan::equipment::esc::SingleMotorCommand msg;
  842. uavcan::equipment::esc::SpeedCommands msgarryspeed;
  843. uavcan::equipment::esc::SetFloatParam msgparam;
  844. uavcan::equipment::esc::Bfd1 msgbfd1;
  845. uavcan::equipment::esc::Bfd2 msgbfd2;
  846. uavcan::equipment::esc::Bfd3 msgbfd3;
  847. uint32_t nowtime = AP_HAL::micros();
  848. static uint32_t last_send_time = nowtime;
  849. if (nowtime - last_send_time >1000000UL/200 )//发送周期
  850. { last_send_time = nowtime;
  851. msgbfd1.index = 0;
  852. msgbfd1.speed1 = 0;
  853. msgbfd1.speed2 = 0;
  854. if ((HVreset&0x01)==1 )
  855. {
  856. if (HVmotor1.fault!=0)
  857. {
  858. msgbfd1.speed3 = 0;
  859. }else{
  860. msgbfd1.speed3 = motors6dof.motor_to_can[2];
  861. HVreset = HVreset&0x06;
  862. }
  863. }else{
  864. msgbfd1.speed3 = motors6dof.motor_to_can[2];
  865. }
  866. esc_Bfd1[_driver_index]->broadcast(msgbfd1);//大约100HZ 在没有设置参数的情况下 发送驱动*/
  867. //-----------------------------------------------------------------------------------------------------
  868. msgbfd2.index = 0;
  869. if ((HVreset&0x02)==2 )
  870. {
  871. if (HVmotor2.fault!=0)
  872. {
  873. msgbfd2.speed1 = 0;
  874. }else{
  875. msgbfd2.speed1 = motors6dof.motor_to_can[3];
  876. HVreset =HVreset&0x05;
  877. }
  878. }else{
  879. msgbfd2.speed1 = motors6dof.motor_to_can[3];
  880. }
  881. if ((HVreset&0x04)==4 )
  882. {
  883. if (HVmotor3.fault!=0)
  884. {
  885. msgbfd2.speed2 = 0;
  886. }else{
  887. msgbfd2.speed2 = motors6dof.motor_to_can[4];
  888. HVreset = HVreset&0x03;
  889. }
  890. }else{
  891. msgbfd2.speed2 = motors6dof.motor_to_can[4];
  892. }
  893. msgbfd2.speed3 = 0;
  894. esc_Bfd2[_driver_index]->broadcast(msgbfd2);//大约100HZ 在没有设置参数的情况下 发送驱动*/
  895. //-------------------------------------------------------------------------------------------------
  896. msgbfd3.index = 0;
  897. msgbfd3.speed1 = 0;
  898. msgbfd3.speed2 = 0;
  899. msgbfd3.speed3 = 0;
  900. esc_Bfd3[_driver_index]->broadcast(msgbfd3);//大约100HZ 在没有设置参数的情况下 发送驱动*/
  901. }
  902. uint32_t nowtime2 = AP_HAL::micros();
  903. static uint32_t last_send_time2 = nowtime2;
  904. static uint32_t lasttime_reboot = 0;
  905. if(nowtime2 - last_send_time2 >1000000UL/200 )
  906. { last_send_time2 = nowtime2;
  907. if(stm32_rboot==1)
  908. {
  909. static uint8_t reboot_cnt = 0;
  910. if((motor_from_stm32[0]!=0 || motor_from_stm32[1]!=0 ||motor_from_stm32[8]!=0 ||motor_from_stm32[9]!=0)&&(reboot_cnt<170)){
  911. msgarryspeed.rpm.push_back(0);
  912. msgarryspeed.rpm.push_back(0);
  913. msgarryspeed.rpm.push_back(0);
  914. msgarryspeed.rpm.push_back(0);
  915. msgarryspeed.rpm.push_back(0);
  916. msgarryspeed.rpm.push_back(0);
  917. reboot_cnt++;
  918. esc_SpeedCommands[_driver_index]->broadcast(msgarryspeed);//大约100HZ 在没有设置参数的情况下 发送驱动
  919. }else{
  920. stm32_rboot = 0;
  921. reboot_cnt=0;
  922. lasttime_reboot = AP_HAL::millis();
  923. }
  924. }
  925. uint32_t errortime_reboot = 0;
  926. uint32_t nowtime_restart = AP_HAL::millis();//ms
  927. errortime_reboot = nowtime_restart - lasttime_reboot;//重启时间
  928. if ((errortime_reboot > reboot_time)&&(stm32_rboot==0))
  929. {
  930. int16_t pwm ;
  931. pwm = motors6dof.motor_to_can[0];
  932. msgarryspeed.rpm.push_back(pwm);
  933. pwm = motors6dof.motor_to_can[1];
  934. msgarryspeed.rpm.push_back(pwm);
  935. pwm = motors6dof.motor_to_can[8];
  936. msgarryspeed.rpm.push_back(pwm);
  937. pwm = motors6dof.motor_to_can[9];
  938. msgarryspeed.rpm.push_back(pwm);
  939. pwm = sub.lights;
  940. msgarryspeed.rpm.push_back(pwm);
  941. pwm = sub.usblpoweroff;
  942. msgarryspeed.rpm.push_back(pwm);
  943. esc_SpeedCommands[_driver_index]->broadcast(msgarryspeed);//大约100HZ 在没有设置参数的情况下 发送驱动
  944. }
  945. //+++++++++设置stm32 参数----------------------------------------
  946. // 上位机没有发送的时候不会发送 退出初始化之后才能写参数
  947. if( !sub.motors.armed()&&(((set_stm32_param.flag == 0xF0)&&(set_stm32_param.number!=16))||(set_stm32_param.flag == 0xFF)))
  948. {
  949. STM32_set_packet(msgparam);
  950. SetFloatParam[_driver_index]->broadcast(msgparam);//广播发送
  951. set_stm32_param.flag = 0x00;
  952. gcs().send_text(MAV_SEVERITY_WARNING, "set %x%x%x%x.%x .",msgparam.XX1,msgparam.XX2,msgparam.XX3,msgparam.XX4,msgparam.YY);
  953. }
  954. }
  955. }
  956. }
  957. ///// SRV output /////
  958. void AP_UAVCAN::SRV_send_actuator(void)
  959. {
  960. uint8_t starting_servo = 0;
  961. bool repeat_send;
  962. WITH_SEMAPHORE(SRV_sem);
  963. do {
  964. repeat_send = false;
  965. uavcan::equipment::actuator::ArrayCommand msg;
  966. uint8_t i;
  967. // UAVCAN can hold maximum of 15 commands in one frame
  968. for (i = 0; starting_servo < UAVCAN_SRV_NUMBER && i < 15; starting_servo++) {
  969. uavcan::equipment::actuator::Command cmd;
  970. /*
  971. * Servo output uses a range of 1000-2000 PWM for scaling.
  972. * This converts output PWM from [1000:2000] range to [-1:1] range that
  973. * is passed to servo as unitless type via UAVCAN.
  974. * This approach allows for MIN/TRIM/MAX values to be used fully on
  975. * autopilot side and for servo it should have the setup to provide maximum
  976. * physically possible throws at [-1:1] limits.
  977. */
  978. if (_SRV_conf[starting_servo].servo_pending && ((((uint32_t) 1) << starting_servo) & _servo_bm)) {
  979. cmd.actuator_id = starting_servo + 1;
  980. // TODO: other types
  981. cmd.command_type = uavcan::equipment::actuator::Command::COMMAND_TYPE_UNITLESS;
  982. // TODO: failsafe, safety
  983. cmd.command_value = constrain_float(((float) _SRV_conf[starting_servo].pulse - 1000.0) / 500.0 - 1.0, -1.0, 1.0);
  984. msg.commands.push_back(cmd);
  985. i++;
  986. }
  987. }
  988. if (i > 0) {
  989. act_out_array[_driver_index]->broadcast(msg);
  990. if (i == 15) {
  991. repeat_send = true;
  992. }
  993. }
  994. } while (repeat_send);
  995. }
  996. void AP_UAVCAN::SRV_send_esc(void)
  997. {
  998. static const int cmd_max = uavcan::equipment::esc::RawCommand::FieldTypes::cmd::RawValueType::max();
  999. uavcan::equipment::esc::RawCommand esc_msg;
  1000. uint8_t active_esc_num = 0, max_esc_num = 0;
  1001. uint8_t k = 0;
  1002. WITH_SEMAPHORE(SRV_sem);
  1003. // find out how many esc we have enabled and if they are active at all
  1004. for (uint8_t i = 0; i < UAVCAN_SRV_NUMBER; i++) {
  1005. if ((((uint32_t) 1) << i) & _esc_bm) {
  1006. max_esc_num = i + 1;
  1007. if (_SRV_conf[i].esc_pending) {
  1008. active_esc_num++;
  1009. }
  1010. }
  1011. }
  1012. // if at least one is active (update) we need to send to all
  1013. if (active_esc_num > 0) {
  1014. k = 0;
  1015. for (uint8_t i = 0; i < max_esc_num && k < 20; i++) {
  1016. if ((((uint32_t) 1) << i) & _esc_bm) {
  1017. // TODO: ESC negative scaling for reverse thrust and reverse rotation
  1018. float scaled = cmd_max * (hal.rcout->scale_esc_to_unity(_SRV_conf[i].pulse) + 1.0) / 2.0;
  1019. scaled = constrain_float(scaled, 0, cmd_max);
  1020. esc_msg.cmd.push_back(static_cast<int>(scaled));
  1021. } else {
  1022. esc_msg.cmd.push_back(static_cast<unsigned>(0));
  1023. }
  1024. k++;
  1025. }
  1026. esc_raw[_driver_index]->broadcast(esc_msg);
  1027. }
  1028. }
  1029. void AP_UAVCAN::SRV_push_servos()
  1030. {
  1031. WITH_SEMAPHORE(SRV_sem);
  1032. for (uint8_t i = 0; i < NUM_SERVO_CHANNELS; i++) {
  1033. // Check if this channels has any function assigned
  1034. if (SRV_Channels::channel_function(i)) {
  1035. _SRV_conf[i].pulse = SRV_Channels::srv_channel(i)->get_output_pwm();
  1036. _SRV_conf[i].esc_pending = true;
  1037. _SRV_conf[i].servo_pending = true;
  1038. }
  1039. }
  1040. _SRV_armed = hal.util->safety_switch_state() != AP_HAL::Util::SAFETY_DISARMED;
  1041. }
  1042. ///// LED /////
  1043. void AP_UAVCAN::led_out_send()
  1044. {
  1045. uint64_t now = AP_HAL::micros64();
  1046. if ((now - _led_conf.last_update) < LED_DELAY_US) {
  1047. return;
  1048. }
  1049. uavcan::equipment::indication::LightsCommand msg;
  1050. {
  1051. WITH_SEMAPHORE(_led_out_sem);
  1052. if (_led_conf.devices_count == 0) {
  1053. return;
  1054. }
  1055. uavcan::equipment::indication::SingleLightCommand cmd;
  1056. for (uint8_t i = 0; i < _led_conf.devices_count; i++) {
  1057. cmd.light_id =_led_conf.devices[i].led_index;
  1058. cmd.color.red = _led_conf.devices[i].red >> 3;
  1059. cmd.color.green = _led_conf.devices[i].green >> 2;
  1060. cmd.color.blue = _led_conf.devices[i].blue >> 3;
  1061. msg.commands.push_back(cmd);
  1062. }
  1063. }
  1064. rgb_led[_driver_index]->broadcast(msg);
  1065. _led_conf.last_update = now;
  1066. }
  1067. bool AP_UAVCAN::led_write(uint8_t led_index, uint8_t red, uint8_t green, uint8_t blue)
  1068. {
  1069. if (_led_conf.devices_count >= AP_UAVCAN_MAX_LED_DEVICES) {
  1070. return false;
  1071. }
  1072. WITH_SEMAPHORE(_led_out_sem);
  1073. // check if a device instance exists. if so, break so the instance index is remembered
  1074. uint8_t instance = 0;
  1075. for (; instance < _led_conf.devices_count; instance++) {
  1076. if (_led_conf.devices[instance].led_index == led_index) {
  1077. break;
  1078. }
  1079. }
  1080. // load into the correct instance.
  1081. // if an existing instance was found in above for loop search,
  1082. // then instance value is < _led_conf.devices_count.
  1083. // otherwise a new one was just found so we increment the count.
  1084. // Either way, the correct instance is the current value of instance
  1085. _led_conf.devices[instance].led_index = led_index;
  1086. _led_conf.devices[instance].red = red;
  1087. _led_conf.devices[instance].green = green;
  1088. _led_conf.devices[instance].blue = blue;
  1089. if (instance == _led_conf.devices_count) {
  1090. _led_conf.devices_count++;
  1091. }
  1092. return true;
  1093. }
  1094. // buzzer send
  1095. void AP_UAVCAN::buzzer_send()
  1096. {
  1097. uavcan::equipment::indication::BeepCommand msg;
  1098. WITH_SEMAPHORE(_buzzer.sem);
  1099. uint8_t mask = (1U << _driver_index);
  1100. if ((_buzzer.pending_mask & mask) == 0) {
  1101. return;
  1102. }
  1103. _buzzer.pending_mask &= ~mask;
  1104. msg.frequency = _buzzer.frequency;
  1105. msg.duration = _buzzer.duration;
  1106. buzzer[_driver_index]->broadcast(msg);
  1107. }
  1108. // buzzer support
  1109. void AP_UAVCAN::set_buzzer_tone(float frequency, float duration_s)
  1110. {
  1111. WITH_SEMAPHORE(_buzzer.sem);
  1112. _buzzer.frequency = frequency;
  1113. _buzzer.duration = duration_s;
  1114. _buzzer.pending_mask = 0xFF;
  1115. }
  1116. // SafetyState send
  1117. void AP_UAVCAN::safety_state_send()
  1118. {
  1119. ardupilot::indication::SafetyState msg;
  1120. uint32_t now = AP_HAL::millis();
  1121. if (now - _last_safety_state_ms < 500) {
  1122. // update at 2Hz
  1123. return;
  1124. }
  1125. _last_safety_state_ms = now;
  1126. switch (hal.util->safety_switch_state()) {
  1127. case AP_HAL::Util::SAFETY_ARMED:
  1128. msg.status = ardupilot::indication::SafetyState::STATUS_SAFETY_OFF;
  1129. break;
  1130. case AP_HAL::Util::SAFETY_DISARMED:
  1131. msg.status = ardupilot::indication::SafetyState::STATUS_SAFETY_ON;
  1132. break;
  1133. default:
  1134. // nothing to send
  1135. return;
  1136. }
  1137. safety_state[_driver_index]->broadcast(msg);
  1138. }
  1139. /*
  1140. handle Button message
  1141. */
  1142. void AP_UAVCAN::handle_button(AP_UAVCAN* ap_uavcan, uint8_t node_id, const ButtonCb &cb)
  1143. {
  1144. switch (cb.msg->button) {
  1145. case ardupilot::indication::Button::BUTTON_SAFETY: {
  1146. AP_BoardConfig *brdconfig = AP_BoardConfig::get_singleton();
  1147. if (brdconfig && brdconfig->safety_button_handle_pressed(cb.msg->press_time)) {
  1148. AP_HAL::Util::safety_state state = hal.util->safety_switch_state();
  1149. if (state == AP_HAL::Util::SAFETY_ARMED) {
  1150. hal.rcout->force_safety_on();
  1151. } else {
  1152. hal.rcout->force_safety_off();
  1153. }
  1154. }
  1155. break;
  1156. }
  1157. }
  1158. }
  1159. AP_UAVCAN *AP_UAVCAN::_singleton;
  1160. namespace AP {
  1161. AP_UAVCAN &uavcan()
  1162. {
  1163. return *AP_UAVCAN::get_singleton();
  1164. }
  1165. };
  1166. #endif // HAL_WITH_UAVCAN