AP_UAVCAN.cpparm! 47 KB

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