soft_motor_output.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971
  1. #include "soft_motor_output.h"
  2. #include "hpm_math.h"
  3. #include "auto_pilot.h"
  4. #include "bsp_V8M_YY_pwm.h"
  5. #include "bsp_V8M_pwm.h"
  6. #include "control_rate.h"
  7. #include "control_throttle.h"
  8. #include "dcm.h"
  9. #include "helpler_funtions.h"
  10. #include "matrix.h"
  11. #include "my_math.h"
  12. #include "params.h"
  13. #include "quaternion.h"
  14. #include "soft_flash.h"
  15. #include "soft_gs.h"
  16. #include "soft_imu.h"
  17. #include "soft_rc_input.h"
  18. #include "soft_time.h"
  19. #include "soft_timer.h"
  20. #include "ver_config.h"
  21. /*-------------------- Macros definition -------------------------------------*/
  22. /* 控制量混控 */
  23. #define PIDMIX(R, P, Y, T) ((pid_thr - 1250) * T + 1250 + pid_roll * R + pid_pitch * P + pid_yaw * Y)
  24. #define PIDMIX_NOYAW(R, P, T) ((pid_thr - 1250) * T + 1250 + pid_roll * R + pid_pitch * P)
  25. #define Min_PWM_Out (conf_par.idle_speed) // us
  26. #define Max_PWM_Out 2000 // us
  27. /*-------------------- Variables definition ----------------------------------*/
  28. /* pid 控制量 */
  29. float pid_roll = 0.0f, pid_pitch = 0.0f, pid_thr = 0.0f, pid_yaw = 0.0f;
  30. /* 8 个电机量 */
  31. uint16_t motor[8] = {1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000};
  32. uint16_t can_motor[8] = {1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000};
  33. /* PWM超限后yaw能给出的最大值 */
  34. static short yaw_limit = 0;
  35. /* PWM饱和后触发航线限制 */
  36. char yaw_output_restriciton = 0;
  37. /* 电机输出是否平衡 */
  38. bool motor_output_unbanlace = false;
  39. /* 电机平滑输出用到的中间变量 */
  40. static float pre_motor[8] = {1000.0f, 1000.0f, 1000.0f, 1000.0f,
  41. 1000.0f, 1000.0f, 1000.0f, 1000.0f};
  42. static float temp_motor[8] = {1000.0f, 1000.0f, 1000.0f, 1000.0f,
  43. 1000.0f, 1000.0f, 1000.0f, 1000.0f};
  44. /* 电机检测通道号和开始时间 */
  45. static uint8_t motcheck_num = 0;
  46. static uint32_t motcheck_starttime = 0;
  47. /* 电机动力故障序号 */
  48. static uint8_t _motor_failsafe_num = 0;
  49. /*--------------------- Functions definition ---------------------------------*/
  50. static void
  51. set_motor_noyaw(uint16_t *p_motor, unsigned char motor_num);
  52. static void get_max_yaw_value(uint16_t *p_motor, unsigned char motor_num);
  53. /**
  54. * @brief 电机 PWM 输出初始化
  55. *
  56. */
  57. void motor_output_initial(void)
  58. {
  59. switch (ver_par.hardware_id)
  60. {
  61. case HW_V8M_YY:
  62. Bsp_V8M_YY_PwmInit();
  63. break;
  64. default:
  65. break;
  66. }
  67. }
  68. /**
  69. * @brief 设置电机输出
  70. *
  71. * @param s_ch 通道号 1 ~ 10
  72. * @param s_pwm 输出值
  73. */
  74. void set_motor_pwm(uint8_t s_ch, uint16_t s_pwm)
  75. {
  76. /* 根据不同的硬件选择不同的输出接口 */
  77. switch (ver_par.hardware_id)
  78. {
  79. case HW_V8M_YY:
  80. Bsp_V8M_YY_PwmSetCHValue(s_ch, s_pwm);
  81. break;
  82. default:
  83. break;
  84. }
  85. }
  86. /**
  87. * @brief 获取电机输出
  88. *
  89. * @param s_ch 通道号 1 ~ 10
  90. * @retval uint16_t 通道值
  91. */
  92. uint16_t get_motor_pwm(uint8_t s_ch)
  93. {
  94. uint16_t m_value = 0;
  95. /* 根据不同的硬件选择不同的输出接口 */
  96. switch (ver_par.hardware_id)
  97. {
  98. case HW_V8M_YY:
  99. m_value = Bsp_V8M_YY_PwmGetCHValue(s_ch);
  100. break;
  101. default:
  102. break;
  103. }
  104. return m_value;
  105. }
  106. /**
  107. * @brief 电调校准
  108. *
  109. */
  110. void esc_calibrate_enable(void)
  111. {
  112. /* 开始电调校准时间 */
  113. static uint32_t start_esc_clb_time = 0;
  114. char i = 0;
  115. // 获取校准之后的遥控器的值,好处是如果有的遥控器没经过校准时无法进入电调校准的
  116. get_rc_value();
  117. // 带油门,带横滚上电进入电调校准程序。防止有时油门反向直接进入了电调校准。
  118. if ((rc_in[RC_CH3] > 1950) && (rc_in[RC_CH1] > 1950))
  119. {
  120. // 电调校准模式下,不允许电机输出部分更新TIMx_CCRx的值。
  121. // 有一次出现没法进入校准,波形一直变化,是因为没有屏蔽掉定时器中断中电机输出部分的更新
  122. pilot_mode = PILOT_ESC_CLB;
  123. start_esc_clb_time = micros();
  124. // 带油门&横滚
  125. // 上电,说明用户希望初始化电调,保持一段时间的高PWM值,避免时间太短无法进入电调校准程序
  126. while (micros() - start_esc_clb_time < 0.5f * 1000000)
  127. {
  128. if (hz_50_flag == true)
  129. {
  130. hz_50_flag = false;
  131. // 根据机型,全部输出CH3油门量。
  132. for (i = MOTOR1; i <= conf_par.jixing / 10; i++)
  133. {
  134. // 测试发现,校准电调时电调自己预留的10%的最小油门量,确保小油门能停转。(所以再用原始值来校准导致有些预留量大的电调1140无法转起来)
  135. set_motor_pwm(i, rc_in[RC_CH3]);
  136. }
  137. }
  138. }
  139. }
  140. }
  141. /**
  142. * @brief 设置电机检测的电机号
  143. *
  144. * @param checkChNum
  145. */
  146. void MotorCheck_SetCheckNum(uint8_t checkChNum)
  147. {
  148. if (checkChNum >= MOTOR1 && checkChNum <= conf_par.jixing / 10)
  149. {
  150. motcheck_num = checkChNum;
  151. motcheck_starttime = micros();
  152. }
  153. }
  154. /**
  155. * @brief 未起飞前电机平滑输出
  156. *
  157. * @param num 电机各数
  158. * @param max_add 每步平滑限制幅度
  159. */
  160. void ground_motor_slow_launch(uint8_t num, float max_add)
  161. {
  162. if (ground_air_status == ON_GROUND)
  163. {
  164. for (uint8_t i = 0; i < num; i++)
  165. {
  166. temp_motor[i] = pre_motor[i] + constrain_float((motor[i] - pre_motor[i]), -max_add, max_add);
  167. pre_motor[i] = temp_motor[i];
  168. motor[i] = (int16_t)temp_motor[i];
  169. }
  170. }
  171. }
  172. /**
  173. * @brief 清零电机平滑输出用到的中间控制量
  174. *
  175. */
  176. void reset_pre_motor(void)
  177. {
  178. uint8_t i;
  179. for (i = 0; i < 8; i++)
  180. {
  181. pre_motor[i] = 1000.0f;
  182. temp_motor[i] = 1000.0f;
  183. }
  184. }
  185. /**
  186. * @brief 上锁状态下的电机输出
  187. *
  188. */
  189. void locked_motor_output(void)
  190. {
  191. if (thr_lock_status != LOCKED)
  192. {
  193. return;
  194. }
  195. // motcheck_value[0]无用
  196. static uint16_t motcheck_value[9] = {1000};
  197. uint8_t i = 0;
  198. // 每个电机检测 2 s
  199. if ((motcheck_num != 0) && (micros() - motcheck_starttime > 2 * 1000000))
  200. {
  201. motcheck_num = 0;
  202. }
  203. // 有电机需要检测时把需要检测的电机的PWM值增加,其他的电机PWM值赋值1000
  204. if (motcheck_num != 0)
  205. {
  206. for (i = 0; i <= 8; i++)
  207. {
  208. motcheck_value[i] = 1000;
  209. }
  210. // 直接输出怠速
  211. motcheck_value[motcheck_num] = conf_par.idle_speed;
  212. }
  213. else
  214. {
  215. for (i = 0; i <= 8; i++)
  216. {
  217. motcheck_value[i] = 1000;
  218. }
  219. }
  220. // 检测电机时根据设置的机型来输出响应的值
  221. for (i = 1; i <= (conf_par.jixing / 10); i++)
  222. {
  223. set_motor_pwm(i, motcheck_value[i]);
  224. can_motor[i - 1] = motcheck_value[i];
  225. }
  226. // 上锁时清零电机平滑用到的中间变量
  227. reset_pre_motor();
  228. }
  229. static void _I6_motor_output_mix(uint8_t failsafe_motor_num, uint16_t *motor_value)
  230. {
  231. uint16_t _motor_tmp[6] = {1000, 1000, 1000, 1000, 1000, 1000};
  232. switch (failsafe_motor_num)
  233. {
  234. case 1:
  235. case 4:
  236. /* 停掉失效的侧桨叶 */
  237. motor_value[0] = Min_PWM_Out;
  238. motor_value[3] = Min_PWM_Out;
  239. motor_value[1] = PIDMIX_NOYAW(0.866f, -1.5f, 1.2f);
  240. motor_value[2] = PIDMIX_NOYAW(0.866f, 1.5f, 1.2f);
  241. motor_value[4] = PIDMIX_NOYAW(-0.866f, 1.5f, 1.2f);
  242. motor_value[5] = PIDMIX_NOYAW(-0.866f, -1.5f, 1.2f);
  243. set_motor_noyaw(motor_value, 6);
  244. motor_value[0] = 1000;
  245. motor_value[3] = 1000;
  246. break;
  247. case 2:
  248. case 5:
  249. /* 停掉失效的侧桨叶 */
  250. motor_value[1] = Min_PWM_Out;
  251. motor_value[4] = Min_PWM_Out;
  252. motor_value[0] = PIDMIX_NOYAW(0.866f, -1.5f, 1.2f);
  253. motor_value[2] = PIDMIX_NOYAW(1.732f, 0.f, 1.2f);
  254. motor_value[3] = PIDMIX_NOYAW(-0.866f, 1.5, 1.2f);
  255. motor_value[5] = PIDMIX_NOYAW(-1.732f, 0.0f, 1.2f);
  256. set_motor_noyaw(motor_value, 6);
  257. motor_value[1] = 1000;
  258. motor_value[4] = 1000;
  259. break;
  260. case 3:
  261. case 6: /* 停掉失效的侧桨叶 */
  262. motor_value[2] = Min_PWM_Out;
  263. motor_value[5] = Min_PWM_Out;
  264. motor_value[0] = PIDMIX_NOYAW(-0.866f, -1.5f, 1.2f);
  265. motor_value[1] = PIDMIX_NOYAW(1.732f, 0, 1.2f);
  266. motor_value[3] = PIDMIX_NOYAW(0.866f, 1.5f, 1.2f);
  267. motor_value[4] = PIDMIX_NOYAW(-1.732f, 0, 1.2f);
  268. set_motor_noyaw(motor_value, 6);
  269. motor_value[2] = 1000;
  270. motor_value[5] = 1000;
  271. break;
  272. default:
  273. {
  274. const float yaw_ratio[6] = {-1.f, 1.f, -1.f, 1.f, -1.f, 1.f};
  275. motor_value[0] = PIDMIX_NOYAW(-0.0f, -1.0f, 1);
  276. motor_value[1] = PIDMIX_NOYAW(+7.0f / 8.0f, -1.0f / 2.0f, 1);
  277. motor_value[2] = PIDMIX_NOYAW(+7.0f / 8.0f, +1.0f / 2.0f, 1);
  278. motor_value[3] = PIDMIX_NOYAW(+0.0f, +1.0f, 1);
  279. motor_value[4] = PIDMIX_NOYAW(-7.0f / 8.0f, +1.0f / 2.0f, 1);
  280. motor_value[5] = PIDMIX_NOYAW(-7.0f / 8.0f, -1.0f / 2.0f, 1);
  281. set_motor_noyaw(motor_value, 6);
  282. for (uint8_t i = 0; i < 6; ++i)
  283. {
  284. _motor_tmp[i] = motor_value[i] + pid_yaw * yaw_ratio[i];
  285. }
  286. get_max_yaw_value(_motor_tmp, 6);
  287. for (uint8_t i = 0; i < 6; ++i)
  288. {
  289. motor_value[i] += yaw_limit * yaw_ratio[i];
  290. }
  291. }
  292. break;
  293. }
  294. }
  295. static void _X4_motor_output_mix(uint8_t failsafe_motor_num, uint16_t *motor_value)
  296. {
  297. uint16_t _motor_tmp[4];
  298. switch (failsafe_motor_num)
  299. {
  300. default:
  301. motor_value[0] = PIDMIX_NOYAW(-1.0f, -1.0f, 1);
  302. motor_value[1] = PIDMIX_NOYAW(+1.0f, -1.0f, 1);
  303. motor_value[2] = PIDMIX_NOYAW(+1.0f, +1.0f, 1);
  304. motor_value[3] = PIDMIX_NOYAW(-1.0f, +1.0f, 1);
  305. set_motor_noyaw(motor_value, 4);
  306. const float yaw_ratio[4] = {-1.f, 1.f, -1.f, 1.f};
  307. for (int i = 0; i < 4; ++i)
  308. {
  309. _motor_tmp[i] = motor_value[i] + pid_yaw * yaw_ratio[i];
  310. }
  311. get_max_yaw_value(_motor_tmp, 4);
  312. for (int i = 0; i < 4; ++i)
  313. {
  314. motor_value[i] = motor_value[i] + yaw_limit * yaw_ratio[i];
  315. }
  316. break;
  317. }
  318. }
  319. static void _I4_motor_output_mix(uint8_t failsafe_motor_num, uint16_t *motor_value)
  320. {
  321. uint16_t _motor_tmp[4];
  322. switch (failsafe_motor_num)
  323. {
  324. default:
  325. {
  326. motor_value[0] = PIDMIX_NOYAW(0, -1.0f, 1);
  327. motor_value[1] = PIDMIX_NOYAW(+1.0f, 0, 1);
  328. motor_value[2] = PIDMIX_NOYAW(0, +1.0f, 1);
  329. motor_value[3] = PIDMIX_NOYAW(-1.0f, 0, 1);
  330. set_motor_noyaw(motor_value, 4);
  331. const float yaw_ratio[4] = {-1.f, 1.f, -1.f, 1.f};
  332. for (int i = 0; i < 4; ++i)
  333. {
  334. _motor_tmp[i] = motor_value[i] + pid_yaw * yaw_ratio[i];
  335. }
  336. get_max_yaw_value(_motor_tmp, 4);
  337. for (int i = 0; i < 4; ++i)
  338. {
  339. motor_value[i] = motor_value[i] + yaw_limit * yaw_ratio[i];
  340. }
  341. }
  342. break;
  343. }
  344. }
  345. static void _X6_motor_output_mix(uint8_t failsafe_motor_num, uint16_t *motor_value)
  346. {
  347. uint16_t _motor_tmp[6] = {1000, 1000, 1000, 1000, 1000, 1000};
  348. switch (failsafe_motor_num)
  349. {
  350. case 1:
  351. case 4:
  352. /* 停掉失效的侧桨叶 */
  353. motor_value[3] = Min_PWM_Out;
  354. motor_value[0] = Min_PWM_Out;
  355. motor_value[1] = PIDMIX_NOYAW(0.0f, -1.732f, 1.2f);
  356. motor_value[2] = PIDMIX_NOYAW(1.5f, 0.866f, 1.2f);
  357. motor_value[4] = PIDMIX_NOYAW(0.0f, 1.732f, 1.2f);
  358. motor_value[5] = PIDMIX_NOYAW(-1.5f, -0.866f, 1.2f);
  359. set_motor_noyaw(motor_value, 6);
  360. motor_value[3] = 1000;
  361. motor_value[0] = 1000;
  362. break;
  363. case 2:
  364. case 5:
  365. /* 停掉失效的侧桨叶 */
  366. motor_value[1] = Min_PWM_Out;
  367. motor_value[4] = Min_PWM_Out;
  368. motor_value[0] = PIDMIX_NOYAW(0.f, -1.732f, 1.2f);
  369. motor_value[2] = PIDMIX_NOYAW(1.5f, -0.866f, 1.2f);
  370. motor_value[3] = PIDMIX_NOYAW(0.f, 1.732f, 1.2f);
  371. motor_value[5] = PIDMIX_NOYAW(-1.5f, 0.866f, 1.2f);
  372. set_motor_noyaw(motor_value, 6);
  373. motor_value[1] = 1000;
  374. motor_value[4] = 1000;
  375. break;
  376. case 3:
  377. case 6:
  378. /* 停掉失效的侧桨叶 */
  379. motor_value[2] = Min_PWM_Out;
  380. motor_value[5] = Min_PWM_Out;
  381. motor_value[0] = PIDMIX_NOYAW(-1.5f, -0.866f, 1.2f);
  382. motor_value[1] = PIDMIX_NOYAW(1.5f, -0.866f, 1.2f);
  383. motor_value[3] = PIDMIX_NOYAW(1.5f, 0.866f, 1.2f);
  384. motor_value[4] = PIDMIX_NOYAW(-1.5f, 0.866f, 1.2f);
  385. set_motor_noyaw(motor_value, 6);
  386. motor_value[2] = 1000;
  387. motor_value[5] = 1000;
  388. break;
  389. default:
  390. {
  391. const float yaw_ratio[6] = {-1.f, 1.f, -1.f, 1.f, -1.f, 1.f};
  392. motor_value[0] = PIDMIX_NOYAW(-0.5f, -0.866f, 1);
  393. motor_value[1] = PIDMIX_NOYAW(0.5f, -0.866f, 1);
  394. motor_value[2] = PIDMIX_NOYAW(1.0f, 0.0f, 1);
  395. motor_value[3] = PIDMIX_NOYAW(0.5f, 0.866f, 1);
  396. motor_value[4] = PIDMIX_NOYAW(-0.5f, 0.866f, 1);
  397. motor_value[5] = PIDMIX_NOYAW(-1.0f, 0.0f, 1);
  398. set_motor_noyaw(motor_value, 6);
  399. for (uint8_t i = 0; i < 6; ++i)
  400. {
  401. _motor_tmp[i] = motor_value[i] + pid_yaw * yaw_ratio[i];
  402. }
  403. get_max_yaw_value(_motor_tmp, 6);
  404. for (uint8_t i = 0; i < 6; ++i)
  405. {
  406. motor_value[i] += yaw_limit * yaw_ratio[i];
  407. }
  408. }
  409. break;
  410. }
  411. }
  412. static void _H6_motor_output_mix(uint8_t failsafe_motor_num, uint16_t *motor_value)
  413. {
  414. uint16_t _motor_tmp[6] = {1000, 1000, 1000, 1000, 1000, 1000};
  415. switch (failsafe_motor_num)
  416. {
  417. default:
  418. {
  419. motor_value[0] = PIDMIX_NOYAW(-0.5f, -0.866f, 1);
  420. motor_value[1] = PIDMIX_NOYAW(0.5f, -0.866f, 1);
  421. motor_value[2] = PIDMIX_NOYAW(2.0f, 0.0f, 1);
  422. motor_value[3] = PIDMIX_NOYAW(0.5f, 0.866f, 1);
  423. motor_value[4] = PIDMIX_NOYAW(-0.5f, 0.866f, 1);
  424. motor_value[5] = PIDMIX_NOYAW(-2.0f, 0.0f, 1);
  425. set_motor_noyaw(motor_value, 6);
  426. const float yaw_ratio[6] = {-1.f, 1.f, -2.f, 1.f, -1.f, 2.f};
  427. for (uint8_t i = 0; i < 6; ++i)
  428. {
  429. _motor_tmp[i] = motor_value[i] + pid_yaw * yaw_ratio[i];
  430. }
  431. get_max_yaw_value(_motor_tmp, 6);
  432. for (uint8_t i = 0; i < 6; ++i)
  433. {
  434. motor_value[i] += yaw_limit * yaw_ratio[i];
  435. }
  436. }
  437. break;
  438. }
  439. }
  440. void unlocked_motor_output(void)
  441. {
  442. if (thr_lock_status != UNLOCKED)
  443. {
  444. return;
  445. }
  446. uint16_t limit_motor[8] = {1000, 1000, 1000, 1000,
  447. 1000, 1000, 1000, 1000};
  448. if (ground_air_status == ON_GROUND)
  449. {
  450. // 在地上的时候积分不参与运算
  451. clear_rate_i_item(&pid_m_roll);
  452. clear_rate_i_item(&pid_m_pitch);
  453. clear_rate_i_item(&pid_m_yaw);
  454. if (rc_in[RC_CH3] < 1200)
  455. {
  456. pid_roll = 0.0f;
  457. pid_pitch = 0.0f;
  458. pid_yaw = 0.0f;
  459. }
  460. }
  461. switch (conf_par.jixing)
  462. {
  463. case FOUR_I4:
  464. _I4_motor_output_mix(_motor_failsafe_num, motor);
  465. break;
  466. case FOUR_X4:
  467. _X4_motor_output_mix(_motor_failsafe_num, motor);
  468. break;
  469. case THREE_Y6D:
  470. motor[0] = PIDMIX_NOYAW(-0.0f, +1.0f, 1); // REAR
  471. motor[1] = PIDMIX_NOYAW(-1.0f, -1.0f, 1); // RIGHT
  472. motor[2] = PIDMIX_NOYAW(+1.0f, -1.0f, 1); // LEFT
  473. motor[3] = PIDMIX_NOYAW(+0.0f, +1.0f, 1); // UNDER_REAR
  474. motor[4] = PIDMIX_NOYAW(-1.0f, -1.0f, 1); // UNDER_RIGHT
  475. motor[5] = PIDMIX_NOYAW(+1.0f, -1.0f, 1); // UNDER_LEFT
  476. set_motor_noyaw(motor, 6);
  477. limit_motor[0] = motor[0] + (short)(pid_yaw * -1.0f); // REAR 后尾电机
  478. limit_motor[1] = motor[1] + (short)(pid_yaw * -1.0f); // RIGHT 右边电机
  479. limit_motor[2] = motor[2] + (short)(pid_yaw * -1.0f); // LEFT 左边电机
  480. limit_motor[3] = motor[3] + (short)(pid_yaw * 1.0f); // FRONT 前面电机
  481. limit_motor[4] = motor[4] + (short)(pid_yaw * 1.0f); // LEFT 左边电机
  482. limit_motor[5] = motor[5] + (short)(pid_yaw * 1.0f); // FRONT 前面电机
  483. get_max_yaw_value(limit_motor, 6);
  484. motor[0] += (yaw_limit * -1); // REAR 后尾电机
  485. motor[1] += (yaw_limit * -1); // RIGHT 右边电机
  486. motor[2] += (yaw_limit * -1); // LEFT 左边电机
  487. motor[3] += (yaw_limit * 1); // FRONT 前面电机
  488. motor[4] += (yaw_limit * 1); // LEFT 左边电机
  489. motor[5] += (yaw_limit * 1); // FRONT 前面电机
  490. break;
  491. case THREE_YI6D:
  492. motor[0] = PIDMIX_NOYAW(-0.0f, -1.0f, 1); // REAR
  493. motor[1] = PIDMIX_NOYAW(+1.0f, +1.0f, 1); // RIGHT
  494. motor[2] = PIDMIX_NOYAW(-1.0f, +1.0f, 1); // LEFT
  495. motor[3] = PIDMIX_NOYAW(+0.0f, -1.0f, 1); // UNDER_REAR
  496. motor[4] = PIDMIX_NOYAW(+1.0f, +1.0f, 1); // UNDER_RIGHT
  497. motor[5] = PIDMIX_NOYAW(-1.0f, +1.0f, 1); // UNDER_LEFT
  498. set_motor_noyaw(motor, 6);
  499. limit_motor[0] = motor[0] + (short)(pid_yaw * -1.0f); // REAR 后尾电机
  500. limit_motor[1] = motor[1] + (short)(pid_yaw * -1.0f); // RIGHT 右边电机
  501. limit_motor[2] = motor[2] + (short)(pid_yaw * -1.0f); // LEFT 左边电机
  502. limit_motor[3] = motor[3] + (short)(pid_yaw * 1.0f); // FRONT 前面电机
  503. limit_motor[4] = motor[4] + (short)(pid_yaw * 1.0f); // LEFT 左边电机
  504. limit_motor[5] = motor[5] + (short)(pid_yaw * 1.0f); // FRONT 前面电机
  505. get_max_yaw_value(limit_motor, 6);
  506. motor[0] += (yaw_limit * -1); // REAR 后尾电机
  507. motor[1] += (yaw_limit * -1); // RIGHT 右边电机
  508. motor[2] += (yaw_limit * -1); // LEFT 左边电机
  509. motor[3] += (yaw_limit * 1); // FRONT 前面电机
  510. motor[4] += (yaw_limit * 1); // LEFT 左边电机
  511. motor[5] += (yaw_limit * 1); // FRONT 前面电机
  512. break;
  513. case SIX_I6:
  514. _I6_motor_output_mix(_motor_failsafe_num, motor);
  515. break;
  516. case SIX_X6:
  517. _X6_motor_output_mix(_motor_failsafe_num, motor);
  518. break;
  519. case SIX_H6:
  520. _H6_motor_output_mix(_motor_failsafe_num, motor);
  521. break;
  522. // 上全正,下全反,跟DJI一样
  523. case FOUR_X8D:
  524. motor[0] = PIDMIX_NOYAW(-1.0f, -1.0f, 1);
  525. motor[1] = PIDMIX_NOYAW(+1.0f, -1.0f, 1);
  526. motor[2] = PIDMIX_NOYAW(+1.0f, +1.0f, 1);
  527. motor[3] = PIDMIX_NOYAW(-1.0f, +1.0f, 1);
  528. motor[4] = PIDMIX_NOYAW(-1.0f, -1.0f, 1);
  529. motor[5] = PIDMIX_NOYAW(+1.0f, -1.0f, 1);
  530. motor[6] = PIDMIX_NOYAW(+1.0f, +1.0f, 1);
  531. motor[7] = PIDMIX_NOYAW(-1.0f, +1.0f, 1);
  532. set_motor_noyaw(motor, 8);
  533. limit_motor[0] = motor[0] + (short)(pid_yaw * -1.0f); // REAR 后尾电机
  534. limit_motor[1] = motor[1] + (short)(pid_yaw * -1.0f); // RIGHT 右边电机
  535. limit_motor[2] = motor[2] + (short)(pid_yaw * -1.0f); // LEFT 左边电机
  536. limit_motor[3] = motor[3] + (short)(pid_yaw * -1.0f); // FRONT 前面电机
  537. limit_motor[4] = motor[4] + (short)(pid_yaw * 1.0f); // LEFT 左边电机
  538. limit_motor[5] = motor[5] + (short)(pid_yaw * 1.0f); // FRONT 前面电机
  539. limit_motor[6] = motor[6] + (short)(pid_yaw * 1.0f); // LEFT 左边电机
  540. limit_motor[7] = motor[7] + (short)(pid_yaw * 1.0f); // FRONT 前面电机
  541. get_max_yaw_value(limit_motor, 8);
  542. motor[0] += (yaw_limit * -1); // REAR 后尾电机
  543. motor[1] += (yaw_limit * -1); // RIGHT 右边电机
  544. motor[2] += (yaw_limit * -1); // LEFT 左边电机
  545. motor[3] += (yaw_limit * -1); // FRONT 前面电机
  546. motor[4] += (yaw_limit * 1); // LEFT 左边电机
  547. motor[5] += (yaw_limit * 1); // FRONT 前面电机
  548. motor[6] += (yaw_limit * 1); // LEFT 左边电机
  549. motor[7] += (yaw_limit * 1); // FRONT 前面电机
  550. break;
  551. // 上跟四轴X一样,下正好跟上相反
  552. case FOUR_X8M:
  553. motor[0] = PIDMIX_NOYAW(-1.0f, -1.0f, 1);
  554. motor[1] = PIDMIX_NOYAW(+1.0f, -1.0f, 1);
  555. motor[2] = PIDMIX_NOYAW(+1.0f, +1.0f, 1);
  556. motor[3] = PIDMIX_NOYAW(-1.0f, +1.0f, 1);
  557. motor[4] = PIDMIX_NOYAW(-1.0f, -1.0f, 1);
  558. motor[5] = PIDMIX_NOYAW(+1.0f, -1.0f, 1);
  559. motor[6] = PIDMIX_NOYAW(+1.0f, +1.0f, 1);
  560. motor[7] = PIDMIX_NOYAW(-1.0f, +1.0f, 1);
  561. set_motor_noyaw(motor, 8);
  562. limit_motor[0] = motor[0] + (short)(pid_yaw * -1.0f); // REAR 后尾电机
  563. limit_motor[1] = motor[1] + (short)(pid_yaw * 1.0f); // RIGHT 右边电机
  564. limit_motor[2] = motor[2] + (short)(pid_yaw * -1.0f); // LEFT 左边电机
  565. limit_motor[3] = motor[3] + (short)(pid_yaw * 1.0f); // FRONT 前面电机
  566. limit_motor[4] = motor[4] + (short)(pid_yaw * 1.0f); // LEFT 左边电机
  567. limit_motor[5] = motor[5] + (short)(pid_yaw * -1.0f); // FRONT 前面电机
  568. limit_motor[6] = motor[6] + (short)(pid_yaw * 1.0f); // LEFT 左边电机
  569. limit_motor[7] = motor[7] + (short)(pid_yaw * -1.0f); // FRONT 前面电机
  570. get_max_yaw_value(limit_motor, 8);
  571. motor[0] += (yaw_limit * -1); // REAR 后尾电机
  572. motor[1] += (yaw_limit * 1); // RIGHT 右边电机
  573. motor[2] += (yaw_limit * -1); // LEFT 左边电机
  574. motor[3] += (yaw_limit * 1); // FRONT 前面电机
  575. motor[4] += (yaw_limit * 1); // LEFT 左边电机
  576. motor[5] += (yaw_limit * -1); // FRONT 前面电机
  577. motor[6] += (yaw_limit * 1); // LEFT 左边电机
  578. motor[7] += (yaw_limit * -1); // FRONT 前面电机
  579. break;
  580. // 和 x8m 相反
  581. case FOUR_X8MR:
  582. motor[0] = PIDMIX_NOYAW(-1.0f, -1.0f, 1);
  583. motor[1] = PIDMIX_NOYAW(+1.0f, -1.0f, 1);
  584. motor[2] = PIDMIX_NOYAW(+1.0f, +1.0f, 1);
  585. motor[3] = PIDMIX_NOYAW(-1.0f, +1.0f, 1);
  586. motor[4] = PIDMIX_NOYAW(-1.0f, -1.0f, 1);
  587. motor[5] = PIDMIX_NOYAW(+1.0f, -1.0f, 1);
  588. motor[6] = PIDMIX_NOYAW(+1.0f, +1.0f, 1);
  589. motor[7] = PIDMIX_NOYAW(-1.0f, +1.0f, 1);
  590. set_motor_noyaw(motor, 8);
  591. limit_motor[0] = motor[0] + (short)(pid_yaw * 1.0f); // REAR 后尾电机
  592. limit_motor[1] = motor[1] + (short)(pid_yaw * -1.0f); // RIGHT 右边电机
  593. limit_motor[2] = motor[2] + (short)(pid_yaw * 1.0f); // LEFT 左边电机
  594. limit_motor[3] = motor[3] + (short)(pid_yaw * -1.0f); // FRONT 前面电机
  595. limit_motor[4] = motor[4] + (short)(pid_yaw * -1.0f); // LEFT 左边电机
  596. limit_motor[5] = motor[5] + (short)(pid_yaw * 1.0f); // FRONT 前面电机
  597. limit_motor[6] = motor[6] + (short)(pid_yaw * -1.0f); // LEFT 左边电机
  598. limit_motor[7] = motor[7] + (short)(pid_yaw * 1.0f); // FRONT 前面电机
  599. get_max_yaw_value(limit_motor, 8);
  600. motor[0] += (yaw_limit * 1); // REAR 后尾电机
  601. motor[1] += (yaw_limit * -1); // RIGHT 右边电机
  602. motor[2] += (yaw_limit * 1); // LEFT 左边电机
  603. motor[3] += (yaw_limit * -1); // FRONT 前面电机
  604. motor[4] += (yaw_limit * -1); // LEFT 左边电机
  605. motor[5] += (yaw_limit * 1); // FRONT 前面电机
  606. motor[6] += (yaw_limit * -1); // LEFT 左边电机
  607. motor[7] += (yaw_limit * 1); // FRONT 前面电机
  608. break;
  609. // 上全反,下全正,跟DJI一样
  610. case FOUR_X8DR:
  611. motor[0] = PIDMIX_NOYAW(-1.0f, -1.0f, 1);
  612. motor[1] = PIDMIX_NOYAW(+1.0f, -1.0f, 1);
  613. motor[2] = PIDMIX_NOYAW(+1.0f, +1.0f, 1);
  614. motor[3] = PIDMIX_NOYAW(-1.0f, +1.0f, 1);
  615. motor[4] = PIDMIX_NOYAW(-1.0f, -1.0f, 1);
  616. motor[5] = PIDMIX_NOYAW(+1.0f, -1.0f, 1);
  617. motor[6] = PIDMIX_NOYAW(+1.0f, +1.0f, 1);
  618. motor[7] = PIDMIX_NOYAW(-1.0f, +1.0f, 1);
  619. set_motor_noyaw(motor, 8);
  620. limit_motor[0] = motor[0] + (short)(pid_yaw * 1.0f); // REAR 后尾电机
  621. limit_motor[1] = motor[1] + (short)(pid_yaw * 1.0f); // RIGHT 右边电机
  622. limit_motor[2] = motor[2] + (short)(pid_yaw * 1.0f); // LEFT 左边电机
  623. limit_motor[3] = motor[3] + (short)(pid_yaw * 1.0f); // FRONT 前面电机
  624. limit_motor[4] = motor[4] + (short)(pid_yaw * -1.0f); // LEFT 左边电机
  625. limit_motor[5] = motor[5] + (short)(pid_yaw * -1.0f); // FRONT 前面电机
  626. limit_motor[6] = motor[6] + (short)(pid_yaw * -1.0f); // LEFT 左边电机
  627. limit_motor[7] = motor[7] + (short)(pid_yaw * -1.0f); // FRONT 前面电机
  628. get_max_yaw_value(limit_motor, 8);
  629. motor[0] += (yaw_limit * 1); // REAR 后尾电机
  630. motor[1] += (yaw_limit * 1); // RIGHT 右边电机
  631. motor[2] += (yaw_limit * 1); // LEFT 左边电机
  632. motor[3] += (yaw_limit * 1); // FRONT 前面电机
  633. motor[4] += (yaw_limit * -1); // LEFT 左边电机
  634. motor[5] += (yaw_limit * -1); // FRONT 前面电机
  635. motor[6] += (yaw_limit * -1); // LEFT 左边电机
  636. motor[7] += (yaw_limit * -1); // FRONT 前面电机
  637. break;
  638. case EIGHT_I8:
  639. motor[0] = PIDMIX_NOYAW(-0.0f, -1.0f, 1); // REAR_R
  640. motor[1] = PIDMIX_NOYAW(+7.0f / 10.0f, -7.0f / 10.0f, 1); // FRONT_R
  641. motor[2] = PIDMIX_NOYAW(+1.0f, -0.0f, 1); // REAR_L
  642. motor[3] = PIDMIX_NOYAW(+7.0f / 10.0f, +7.0f / 10.0f, 1); // FRONT_L
  643. motor[4] = PIDMIX_NOYAW(+0.0f, +1.0f, 1); // UNDER_REAR_R
  644. motor[5] = PIDMIX_NOYAW(-7.0f / 10.0f, +7.0f / 10.0f, 1); // UNDER_FRONT_R
  645. motor[6] = PIDMIX_NOYAW(-1.0f, +0.0f, 1); // UNDER_REAR_L
  646. motor[7] = PIDMIX_NOYAW(-7.0f / 10.0f, -7.0f / 10.0f, 1); // UNDER_FRONT_L
  647. set_motor_noyaw(motor, 8);
  648. limit_motor[0] = motor[0] + (short)(pid_yaw * -1.0f); // REAR 后尾电机
  649. limit_motor[1] = motor[1] + (short)(pid_yaw * 1.0f); // RIGHT 右边电机
  650. limit_motor[2] = motor[2] + (short)(pid_yaw * -1.0f); // LEFT 左边电机
  651. limit_motor[3] = motor[3] + (short)(pid_yaw * 1.0f); // FRONT 前面电机
  652. limit_motor[4] = motor[4] + (short)(pid_yaw * -1.0f); // LEFT 左边电机
  653. limit_motor[5] = motor[5] + (short)(pid_yaw * 1.0f); // FRONT 前面电机
  654. limit_motor[6] = motor[6] + (short)(pid_yaw * -1.0f); // LEFT 左边电机
  655. limit_motor[7] = motor[7] + (short)(pid_yaw * 1.0f); // FRONT 前面电机
  656. get_max_yaw_value(limit_motor, 8);
  657. motor[0] += (yaw_limit * -1); // REAR 后尾电机
  658. motor[1] += (yaw_limit * 1); // RIGHT 右边电机
  659. motor[2] += (yaw_limit * -1); // LEFT 左边电机
  660. motor[3] += (yaw_limit * 1); // FRONT 前面电机
  661. motor[4] += (yaw_limit * -1); // LEFT 左边电机
  662. motor[5] += (yaw_limit * 1); // FRONT 前面电机
  663. motor[6] += (yaw_limit * -1); // LEFT 左边电机
  664. motor[7] += (yaw_limit * 1); // FRONT 前面电机
  665. break;
  666. case EIGHT_X8:
  667. motor[0] = PIDMIX_NOYAW(-1.0f / 2.0f, -1.0f, 1); // REAR_R
  668. motor[1] = PIDMIX_NOYAW(+1.0f / 2.0f, -1.0f, 1); // FRONT_R
  669. motor[2] = PIDMIX_NOYAW(+1.0f, -1.0f / 2.0f, 1); // REAR_L
  670. motor[3] = PIDMIX_NOYAW(+1.0f, +1.0f / 2.0f, 1); // FRONT_L
  671. motor[4] = PIDMIX_NOYAW(+1.0f / 2.0f, +1.0f, 1); // UNDER_REAR_R
  672. motor[5] = PIDMIX_NOYAW(-1.0f / 2.0f, +1.0f, 1); // UNDER_FRONT_R
  673. motor[6] = PIDMIX_NOYAW(-1.0f, +1.0f / 2.0f, 1); // UNDER_REAR_L
  674. motor[7] = PIDMIX_NOYAW(-1.0f, -1.0f / 2.0f, 1); // UNDER_FRONT_L
  675. set_motor_noyaw(motor, 8);
  676. limit_motor[0] = motor[0] + (short)(pid_yaw * -1.0f); // REAR 后尾电机
  677. limit_motor[1] = motor[1] + (short)(pid_yaw * 1.0f); // RIGHT 右边电机
  678. limit_motor[2] = motor[2] + (short)(pid_yaw * -1.0f); // LEFT 左边电机
  679. limit_motor[3] = motor[3] + (short)(pid_yaw * 1.0f); // FRONT 前面电机
  680. limit_motor[4] = motor[4] + (short)(pid_yaw * -1.0f); // LEFT 左边电机
  681. limit_motor[5] = motor[5] + (short)(pid_yaw * 1.0f); // FRONT 前面电机
  682. limit_motor[6] = motor[6] + (short)(pid_yaw * -1.0f); // LEFT 左边电机
  683. limit_motor[7] = motor[7] + (short)(pid_yaw * 1.0f); // FRONT 前面电机
  684. get_max_yaw_value(limit_motor, 8);
  685. motor[0] += (yaw_limit * -1); // REAR 后尾电机
  686. motor[1] += (yaw_limit * 1); // RIGHT 右边电机
  687. motor[2] += (yaw_limit * -1); // LEFT 左边电机
  688. motor[3] += (yaw_limit * 1); // FRONT 前面电机
  689. motor[4] += (yaw_limit * -1); // LEFT 左边电机
  690. motor[5] += (yaw_limit * 1); // FRONT 前面电机
  691. motor[6] += (yaw_limit * -1); // LEFT 左边电机
  692. motor[7] += (yaw_limit * 1); // FRONT 前面电机
  693. break;
  694. default:
  695. break;
  696. }
  697. ground_motor_slow_launch(conf_par.jixing / 10, 0.6f);
  698. for (uint8_t chNum = MOTOR1; chNum <= conf_par.jixing / 10; chNum++)
  699. {
  700. set_motor_pwm(chNum, motor[chNum - 1]);
  701. can_motor[chNum - 1] = motor[chNum - 1];
  702. }
  703. }
  704. /**
  705. * @brief 电机输出饱和保护,牺牲高度控制
  706. *
  707. * @param p_motor 电机输出量
  708. * @param motor_num 电机个数
  709. */
  710. void set_motor_noyaw(uint16_t *p_motor, unsigned char motor_num)
  711. {
  712. short min_pwm_out = 2000, max_pwm_out = 1000;
  713. short pwm_out_error = 0;
  714. unsigned char i = 0;
  715. // 起飞后最低转速保护1050
  716. short min_thr_constrain = Min_PWM_Out;
  717. for (i = 0; i < motor_num; i++)
  718. {
  719. // 找出电机输出的最大值和最小值
  720. if (p_motor[i] > max_pwm_out)
  721. {
  722. max_pwm_out = p_motor[i];
  723. }
  724. if (p_motor[i] < min_pwm_out)
  725. {
  726. min_pwm_out = p_motor[i];
  727. }
  728. }
  729. // 如果最大油门大于2000,则牺牲油门控制量
  730. if (max_pwm_out > Max_PWM_Out)
  731. {
  732. pwm_out_error = max_pwm_out - Max_PWM_Out;
  733. }
  734. if (min_pwm_out < min_thr_constrain)
  735. {
  736. pwm_out_error = min_pwm_out - min_thr_constrain;
  737. }
  738. // 将所有电机量同时减掉pwm_out_error
  739. if (pwm_out_error != 0)
  740. {
  741. for (i = 0; i < motor_num; i++)
  742. {
  743. p_motor[i] -= pwm_out_error;
  744. if (p_motor[i] > Max_PWM_Out)
  745. {
  746. p_motor[i] = Max_PWM_Out;
  747. }
  748. else if (p_motor[i] < min_thr_constrain)
  749. {
  750. p_motor[i] = min_thr_constrain;
  751. }
  752. }
  753. }
  754. }
  755. /**
  756. * @brief 电机输出饱和保护,牺牲航向
  757. *
  758. * @param p_motor 电机值
  759. * @param motor_num 电机个数
  760. */
  761. void get_max_yaw_value(uint16_t *p_motor, unsigned char motor_num)
  762. {
  763. static unsigned int motor_restriction_time = 0;
  764. short min_pwm_out = 2000, max_pwm_out = 1000;
  765. unsigned char i = 0;
  766. short pwm_out_error = 0;
  767. // 起飞后最低转速保护1050
  768. short min_thr_constrain = Min_PWM_Out;
  769. if (ground_air_status == IN_AIR)
  770. {
  771. min_thr_constrain = 1050;
  772. }
  773. else
  774. {
  775. min_thr_constrain = Min_PWM_Out;
  776. }
  777. for (i = 0; i < motor_num; i++)
  778. {
  779. // 找出电机输出的最大值和最小值
  780. if (p_motor[i] > max_pwm_out)
  781. {
  782. max_pwm_out = p_motor[i];
  783. }
  784. if (p_motor[i] < min_pwm_out)
  785. {
  786. min_pwm_out = p_motor[i];
  787. }
  788. }
  789. // 如果最大油门大于2000,则牺牲航向控制
  790. if (max_pwm_out > Max_PWM_Out)
  791. {
  792. pwm_out_error = max_pwm_out - Max_PWM_Out;
  793. }
  794. if (min_pwm_out < min_thr_constrain)
  795. {
  796. pwm_out_error = min_thr_constrain - min_pwm_out;
  797. }
  798. // 如果输出油门到达限幅,则触发航向只做减速控制
  799. if (max_pwm_out > Max_PWM_Out || min_pwm_out < min_thr_constrain)
  800. {
  801. if (micros() - motor_restriction_time > 500000)
  802. {
  803. yaw_output_restriciton = 1;
  804. pid_m_yaw.angle_i_item = 0.0f;
  805. }
  806. }
  807. else
  808. {
  809. yaw_output_restriciton = 0;
  810. motor_restriction_time = micros();
  811. }
  812. if (pid_yaw >= 0.0f)
  813. yaw_limit = (short)pid_yaw - pwm_out_error;
  814. else
  815. yaw_limit = (short)pid_yaw + pwm_out_error;
  816. // 如果飞机倾斜角度大于 35 度,则不加入航向控制量
  817. float tilt_angle = acosf(cosf(pid_m_roll.angle_c * DEG_TO_RAD) *
  818. cosf(pid_m_pitch.angle_c * DEG_TO_RAD)) * RAD_TO_DEG;
  819. if (fabsf(tilt_angle) > 35.0f)
  820. {
  821. yaw_limit = 0.0f;
  822. pid_m_yaw.angle_i_item = 0.0f;
  823. }
  824. }
  825. /**
  826. * @brief 获取航向输出控制受限标志
  827. *
  828. * @return uint8_t
  829. */
  830. uint8_t MotorOutput_GetYawRestrictionStatus(void)
  831. {
  832. return yaw_output_restriciton;
  833. }
  834. uint8_t Motor_GetFailsafeNum(void)
  835. {
  836. return _motor_failsafe_num;
  837. }