soft_motor_output.c 32 KB

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