soft_motor_output.c 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584
  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. #include "test.h"
  22. /*-------------------- Macros definition -------------------------------------*/
  23. /* 控制量混控 */
  24. #define PIDMIX(R, P, Y, T) ((pid_thr - 1250) * T + 1250 + pid_roll * R + pid_pitch * P + pid_yaw * Y)
  25. #define PIDMIX_NOYAW(R, P, T) ((pid_thr - 1250) * T + 1250 + pid_roll * R + pid_pitch * P)
  26. #define Min_PWM_Out (conf_par.idle_speed) // us
  27. #define Max_PWM_Out 2000 // us
  28. /*-------------------- Variables definition ----------------------------------*/
  29. /* pid 控制量 */
  30. float pid_roll = 0.0f, pid_pitch = 0.0f, pid_thr = 0.0f, pid_yaw = 0.0f;
  31. /* 8 个电机量 */
  32. uint16_t motor[8] = {1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000};
  33. uint16_t can_motor[8] = {1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000};
  34. /* PWM超限后yaw能给出的最大值 */
  35. static short yaw_limit = 0;
  36. /* PWM饱和后触发航线限制 */
  37. char yaw_output_restriciton = 0;
  38. /* 电机输出是否平衡 */
  39. bool motor_output_unbanlace = false;
  40. /* 电机平滑输出用到的中间变量 */
  41. static float pre_motor[8] = {1000.0f, 1000.0f, 1000.0f, 1000.0f,
  42. 1000.0f, 1000.0f, 1000.0f, 1000.0f};
  43. static float temp_motor[8] = {1000.0f, 1000.0f, 1000.0f, 1000.0f,
  44. 1000.0f, 1000.0f, 1000.0f, 1000.0f};
  45. /* 电机检测通道号和开始时间 */
  46. static uint8_t motcheck_num = 0;
  47. static uint32_t motcheck_starttime = 0;
  48. /* 电机动力故障序号 */
  49. static uint8_t _motor_failsafe_num = 0;
  50. /*--------------------- Functions definition ---------------------------------*/
  51. static void
  52. set_motor_noyaw(uint16_t *p_motor, unsigned char motor_num);
  53. static void get_max_yaw_value(uint16_t *p_motor, unsigned char motor_num);
  54. /**
  55. * @brief 电机 PWM 输出初始化
  56. *
  57. */
  58. void motor_output_initial(void)
  59. {
  60. switch (ver_par.hardware_id)
  61. {
  62. case HW_V8M_YY:
  63. Bsp_V8M_YY_PwmInit();
  64. break;
  65. default:
  66. break;
  67. }
  68. }
  69. /**
  70. * @brief 设置电机输出
  71. *
  72. * @param s_ch 通道号 1 ~ 10
  73. * @param s_pwm 输出值
  74. */
  75. void set_motor_pwm(uint8_t s_ch, uint16_t s_pwm)
  76. {
  77. /* 根据不同的硬件选择不同的输出接口 */
  78. switch (ver_par.hardware_id)
  79. {
  80. case HW_V8M_YY:
  81. Bsp_V8M_YY_PwmSetCHValue(s_ch, s_pwm);
  82. break;
  83. default:
  84. break;
  85. }
  86. }
  87. /**
  88. * @brief 获取电机输出
  89. *
  90. * @param s_ch 通道号 1 ~ 10
  91. * @retval uint16_t 通道值
  92. */
  93. uint16_t get_motor_pwm(uint8_t s_ch)
  94. {
  95. uint16_t m_value = 0;
  96. /* 根据不同的硬件选择不同的输出接口 */
  97. switch (ver_par.hardware_id)
  98. {
  99. case HW_V8M_YY:
  100. m_value = Bsp_V8M_YY_PwmGetCHValue(s_ch);
  101. break;
  102. default:
  103. break;
  104. }
  105. return m_value;
  106. }
  107. /**
  108. * @brief 电调校准
  109. *
  110. */
  111. void esc_calibrate_enable(void)
  112. {
  113. /* 开始电调校准时间 */
  114. static uint32_t start_esc_clb_time = 0;
  115. char i = 0;
  116. // 获取校准之后的遥控器的值,好处是如果有的遥控器没经过校准时无法进入电调校准的
  117. get_rc_value();
  118. // 带油门,带横滚上电进入电调校准程序。防止有时油门反向直接进入了电调校准。
  119. if ((rc_in[RC_CH3] > 1950) && (rc_in[RC_CH1] > 1950))
  120. {
  121. // 电调校准模式下,不允许电机输出部分更新TIMx_CCRx的值。
  122. // 有一次出现没法进入校准,波形一直变化,是因为没有屏蔽掉定时器中断中电机输出部分的更新
  123. pilot_mode = PILOT_ESC_CLB;
  124. start_esc_clb_time = micros();
  125. // 带油门&横滚
  126. // 上电,说明用户希望初始化电调,保持一段时间的高PWM值,避免时间太短无法进入电调校准程序
  127. while (micros() - start_esc_clb_time < 0.5f * 1000000)
  128. {
  129. if (hz_50_flag == true)
  130. {
  131. hz_50_flag = false;
  132. // 根据机型,全部输出CH3油门量。
  133. for (i = MOTOR1; i <= conf_par.jixing / 10; i++)
  134. {
  135. // 测试发现,校准电调时电调自己预留的10%的最小油门量,确保小油门能停转。(所以再用原始值来校准导致有些预留量大的电调1140无法转起来)
  136. set_motor_pwm(i, rc_in[RC_CH3]);
  137. }
  138. }
  139. }
  140. }
  141. }
  142. /**
  143. * @brief 设置电机检测的电机号
  144. *
  145. * @param checkChNum
  146. */
  147. void MotorCheck_SetCheckNum(uint8_t checkChNum)
  148. {
  149. if (checkChNum >= MOTOR1 && checkChNum <= conf_par.jixing / 10)
  150. {
  151. motcheck_num = checkChNum;
  152. motcheck_starttime = micros();
  153. }
  154. }
  155. /**
  156. * @brief 未起飞前电机平滑输出
  157. *
  158. * @param num 电机各数
  159. * @param max_add 每步平滑限制幅度
  160. */
  161. void ground_motor_slow_launch(uint8_t num, float max_add)
  162. {
  163. if (ground_air_status == ON_GROUND)
  164. {
  165. for (uint8_t i = 0; i < num; i++)
  166. {
  167. temp_motor[i] = pre_motor[i] + constrain_float((motor[i] - pre_motor[i]), -max_add, max_add);
  168. pre_motor[i] = temp_motor[i];
  169. motor[i] = (int16_t)temp_motor[i];
  170. }
  171. }
  172. }
  173. /**
  174. * @brief 清零电机平滑输出用到的中间控制量
  175. *
  176. */
  177. void reset_pre_motor(void)
  178. {
  179. uint8_t i;
  180. for (i = 0; i < 8; i++)
  181. {
  182. pre_motor[i] = 1000.0f;
  183. temp_motor[i] = 1000.0f;
  184. }
  185. }
  186. /**
  187. * @brief 上锁状态下的电机输出
  188. *
  189. */
  190. void locked_motor_output(void)
  191. {
  192. if (thr_lock_status != LOCKED)
  193. {
  194. return;
  195. }
  196. // motcheck_value[0]无用
  197. static uint16_t motcheck_value[9] = {1000};
  198. uint8_t i = 0;
  199. // 每个电机检测 2 s
  200. if ((motcheck_num != 0) && (micros() - motcheck_starttime > 2 * 1000000))
  201. {
  202. motcheck_num = 0;
  203. }
  204. // 有电机需要检测时把需要检测的电机的PWM值增加,其他的电机PWM值赋值1000
  205. if (motcheck_num != 0)
  206. {
  207. for (i = 0; i <= 8; i++)
  208. {
  209. motcheck_value[i] = 1000;
  210. }
  211. // 直接输出怠速
  212. motcheck_value[motcheck_num] = conf_par.idle_speed;
  213. }
  214. else
  215. {
  216. for (i = 0; i <= 8; i++)
  217. {
  218. motcheck_value[i] = 1000;
  219. }
  220. }
  221. // 检测电机时根据设置的机型来输出响应的值
  222. for (i = 1; i <= (conf_par.jixing / 10); i++)
  223. {
  224. set_motor_pwm(i, motcheck_value[i]);
  225. can_motor[i - 1] = motcheck_value[i];
  226. }
  227. // 上锁时清零电机平滑用到的中间变量
  228. reset_pre_motor();
  229. }
  230. static void _I6_motor_output_mix(uint8_t failsafe_motor_num, uint16_t *motor_value)
  231. {
  232. uint16_t _motor_tmp[6] = {1000, 1000, 1000, 1000, 1000, 1000};
  233. switch (failsafe_motor_num)
  234. {
  235. case 1:
  236. case 4:
  237. /* 停掉失效的侧桨叶 */
  238. motor_value[0] = Min_PWM_Out;
  239. motor_value[3] = Min_PWM_Out;
  240. motor_value[1] = PIDMIX_NOYAW(0.866f, -1.5f, 1.2f);
  241. motor_value[2] = PIDMIX_NOYAW(0.866f, 1.5f, 1.2f);
  242. motor_value[4] = PIDMIX_NOYAW(-0.866f, 1.5f, 1.2f);
  243. motor_value[5] = PIDMIX_NOYAW(-0.866f, -1.5f, 1.2f);
  244. set_motor_noyaw(motor_value, 6);
  245. motor_value[0] = 1000;
  246. motor_value[3] = 1000;
  247. break;
  248. case 2:
  249. case 5:
  250. /* 停掉失效的侧桨叶 */
  251. motor_value[1] = Min_PWM_Out;
  252. motor_value[4] = Min_PWM_Out;
  253. motor_value[0] = PIDMIX_NOYAW(0.866f, -1.5f, 1.2f);
  254. motor_value[2] = PIDMIX_NOYAW(1.732f, 0.f, 1.2f);
  255. motor_value[3] = PIDMIX_NOYAW(-0.866f, 1.5, 1.2f);
  256. motor_value[5] = PIDMIX_NOYAW(-1.732f, 0.0f, 1.2f);
  257. set_motor_noyaw(motor_value, 6);
  258. motor_value[1] = 1000;
  259. motor_value[4] = 1000;
  260. break;
  261. case 3:
  262. case 6: /* 停掉失效的侧桨叶 */
  263. motor_value[2] = Min_PWM_Out;
  264. motor_value[5] = Min_PWM_Out;
  265. motor_value[0] = PIDMIX_NOYAW(-0.866f, -1.5f, 1.2f);
  266. motor_value[1] = PIDMIX_NOYAW(1.732f, 0, 1.2f);
  267. motor_value[3] = PIDMIX_NOYAW(0.866f, 1.5f, 1.2f);
  268. motor_value[4] = PIDMIX_NOYAW(-1.732f, 0, 1.2f);
  269. set_motor_noyaw(motor_value, 6);
  270. motor_value[2] = 1000;
  271. motor_value[5] = 1000;
  272. break;
  273. default:
  274. {
  275. const float yaw_ratio[6] = {-1.f, 1.f, -1.f, 1.f, -1.f, 1.f};
  276. motor_value[0] = PIDMIX_NOYAW(-0.0f, -1.0f, 1);
  277. motor_value[1] = PIDMIX_NOYAW(+7.0f / 8.0f, -1.0f / 2.0f, 1);
  278. motor_value[2] = PIDMIX_NOYAW(+7.0f / 8.0f, +1.0f / 2.0f, 1);
  279. motor_value[3] = PIDMIX_NOYAW(+0.0f, +1.0f, 1);
  280. motor_value[4] = PIDMIX_NOYAW(-7.0f / 8.0f, +1.0f / 2.0f, 1);
  281. motor_value[5] = PIDMIX_NOYAW(-7.0f / 8.0f, -1.0f / 2.0f, 1);
  282. set_motor_noyaw(motor_value, 6);
  283. for (uint8_t i = 0; i < 6; ++i)
  284. {
  285. _motor_tmp[i] = motor_value[i] + pid_yaw * yaw_ratio[i];
  286. }
  287. get_max_yaw_value(_motor_tmp, 6);
  288. for (uint8_t i = 0; i < 6; ++i)
  289. {
  290. motor_value[i] += yaw_limit * yaw_ratio[i];
  291. }
  292. }
  293. break;
  294. }
  295. }
  296. static void _X4_motor_output_mix(uint8_t failsafe_motor_num, uint16_t *motor_value)
  297. {
  298. uint16_t _motor_tmp[4];
  299. switch (failsafe_motor_num)
  300. {
  301. default:
  302. motor_value[0] = PIDMIX_NOYAW(-1.0f, -1.0f, 1);
  303. motor_value[1] = PIDMIX_NOYAW(+1.0f, -1.0f, 1);
  304. motor_value[2] = PIDMIX_NOYAW(+1.0f, +1.0f, 1);
  305. motor_value[3] = PIDMIX_NOYAW(-1.0f, +1.0f, 1);
  306. set_motor_noyaw(motor_value, 4);
  307. const float yaw_ratio[4] = {-1.f, 1.f, -1.f, 1.f};
  308. for (int i = 0; i < 4; ++i)
  309. {
  310. _motor_tmp[i] = motor_value[i] + pid_yaw * yaw_ratio[i];
  311. }
  312. get_max_yaw_value(_motor_tmp, 4);
  313. for (int i = 0; i < 4; ++i)
  314. {
  315. motor_value[i] = motor_value[i] + yaw_limit * yaw_ratio[i];
  316. }
  317. break;
  318. }
  319. }
  320. static void _I4_motor_output_mix(uint8_t failsafe_motor_num, uint16_t *motor_value)
  321. {
  322. uint16_t _motor_tmp[4];
  323. switch (failsafe_motor_num)
  324. {
  325. default:
  326. {
  327. motor_value[0] = PIDMIX_NOYAW(0, -1.0f, 1);
  328. motor_value[1] = PIDMIX_NOYAW(+1.0f, 0, 1);
  329. motor_value[2] = PIDMIX_NOYAW(0, +1.0f, 1);
  330. motor_value[3] = PIDMIX_NOYAW(-1.0f, 0, 1);
  331. set_motor_noyaw(motor_value, 4);
  332. const float yaw_ratio[4] = {-1.f, 1.f, -1.f, 1.f};
  333. for (int i = 0; i < 4; ++i)
  334. {
  335. _motor_tmp[i] = motor_value[i] + pid_yaw * yaw_ratio[i];
  336. }
  337. get_max_yaw_value(_motor_tmp, 4);
  338. for (int i = 0; i < 4; ++i)
  339. {
  340. motor_value[i] = motor_value[i] + yaw_limit * yaw_ratio[i];
  341. }
  342. }
  343. break;
  344. }
  345. }
  346. static void _X6_motor_output_mix(uint8_t failsafe_motor_num, uint16_t *motor_value)
  347. {
  348. uint16_t _motor_tmp[6] = {1000, 1000, 1000, 1000, 1000, 1000};
  349. switch (failsafe_motor_num)
  350. {
  351. case 1:
  352. case 4:
  353. /* 停掉失效的侧桨叶 */
  354. motor_value[3] = Min_PWM_Out;
  355. motor_value[0] = Min_PWM_Out;
  356. motor_value[1] = PIDMIX_NOYAW(0.0f, -1.732f, 1.2f);
  357. motor_value[2] = PIDMIX_NOYAW(1.5f, 0.866f, 1.2f);
  358. motor_value[4] = PIDMIX_NOYAW(0.0f, 1.732f, 1.2f);
  359. motor_value[5] = PIDMIX_NOYAW(-1.5f, -0.866f, 1.2f);
  360. set_motor_noyaw(motor_value, 6);
  361. motor_value[3] = 1000;
  362. motor_value[0] = 1000;
  363. break;
  364. case 2:
  365. case 5:
  366. /* 停掉失效的侧桨叶 */
  367. motor_value[1] = Min_PWM_Out;
  368. motor_value[4] = Min_PWM_Out;
  369. motor_value[0] = PIDMIX_NOYAW(0.f, -1.732f, 1.2f);
  370. motor_value[2] = PIDMIX_NOYAW(1.5f, -0.866f, 1.2f);
  371. motor_value[3] = PIDMIX_NOYAW(0.f, 1.732f, 1.2f);
  372. motor_value[5] = PIDMIX_NOYAW(-1.5f, 0.866f, 1.2f);
  373. set_motor_noyaw(motor_value, 6);
  374. motor_value[1] = 1000;
  375. motor_value[4] = 1000;
  376. break;
  377. case 3:
  378. case 6:
  379. /* 停掉失效的侧桨叶 */
  380. motor_value[2] = Min_PWM_Out;
  381. motor_value[5] = Min_PWM_Out;
  382. motor_value[0] = PIDMIX_NOYAW(-1.5f, -0.866f, 1.2f);
  383. motor_value[1] = PIDMIX_NOYAW(1.5f, -0.866f, 1.2f);
  384. motor_value[3] = PIDMIX_NOYAW(1.5f, 0.866f, 1.2f);
  385. motor_value[4] = PIDMIX_NOYAW(-1.5f, 0.866f, 1.2f);
  386. set_motor_noyaw(motor_value, 6);
  387. motor_value[2] = 1000;
  388. motor_value[5] = 1000;
  389. break;
  390. default:
  391. {
  392. const float yaw_ratio[6] = {-1.f, 1.f, -1.f, 1.f, -1.f, 1.f};
  393. motor_value[0] = PIDMIX_NOYAW(-0.5f, -0.866f, 1);
  394. motor_value[1] = PIDMIX_NOYAW(0.5f, -0.866f, 1);
  395. motor_value[2] = PIDMIX_NOYAW(1.0f, 0.0f, 1);
  396. motor_value[3] = PIDMIX_NOYAW(0.5f, 0.866f, 1);
  397. motor_value[4] = PIDMIX_NOYAW(-0.5f, 0.866f, 1);
  398. motor_value[5] = PIDMIX_NOYAW(-1.0f, 0.0f, 1);
  399. set_motor_noyaw(motor_value, 6);
  400. for (uint8_t i = 0; i < 6; ++i)
  401. {
  402. _motor_tmp[i] = motor_value[i] + pid_yaw * yaw_ratio[i];
  403. }
  404. get_max_yaw_value(_motor_tmp, 6);
  405. for (uint8_t i = 0; i < 6; ++i)
  406. {
  407. motor_value[i] += yaw_limit * yaw_ratio[i];
  408. }
  409. }
  410. break;
  411. }
  412. }
  413. static void _H6_motor_output_mix(uint8_t failsafe_motor_num, uint16_t *motor_value)
  414. {
  415. uint16_t _motor_tmp[6] = {1000, 1000, 1000, 1000, 1000, 1000};
  416. switch (failsafe_motor_num)
  417. {
  418. default:
  419. {
  420. motor_value[0] = PIDMIX_NOYAW(-0.5f, -0.866f, 1);
  421. motor_value[1] = PIDMIX_NOYAW(0.5f, -0.866f, 1);
  422. motor_value[2] = PIDMIX_NOYAW(2.0f, 0.0f, 1);
  423. motor_value[3] = PIDMIX_NOYAW(0.5f, 0.866f, 1);
  424. motor_value[4] = PIDMIX_NOYAW(-0.5f, 0.866f, 1);
  425. motor_value[5] = PIDMIX_NOYAW(-2.0f, 0.0f, 1);
  426. set_motor_noyaw(motor_value, 6);
  427. const float yaw_ratio[6] = {-1.f, 1.f, -2.f, 1.f, -1.f, 2.f};
  428. for (uint8_t i = 0; i < 6; ++i)
  429. {
  430. _motor_tmp[i] = motor_value[i] + pid_yaw * yaw_ratio[i];
  431. }
  432. get_max_yaw_value(_motor_tmp, 6);
  433. for (uint8_t i = 0; i < 6; ++i)
  434. {
  435. motor_value[i] += yaw_limit * yaw_ratio[i];
  436. }
  437. }
  438. break;
  439. }
  440. }
  441. void unlocked_motor_output(void)
  442. {
  443. if (thr_lock_status != UNLOCKED)
  444. {
  445. return;
  446. }
  447. uint16_t limit_motor[8] = {1000, 1000, 1000, 1000,
  448. 1000, 1000, 1000, 1000};
  449. if (ground_air_status == ON_GROUND)
  450. {
  451. // 在地上的时候积分不参与运算
  452. clear_rate_i_item(&pid_m_roll);
  453. clear_rate_i_item(&pid_m_pitch);
  454. clear_rate_i_item(&pid_m_yaw);
  455. if (rc_in[RC_CH3] < 1200)
  456. {
  457. pid_roll = 0.0f;
  458. pid_pitch = 0.0f;
  459. pid_yaw = 0.0f;
  460. }
  461. }
  462. switch (conf_par.jixing)
  463. {
  464. case FOUR_I4:
  465. _I4_motor_output_mix(_motor_failsafe_num, motor);
  466. break;
  467. case FOUR_X4:
  468. _X4_motor_output_mix(_motor_failsafe_num, motor);
  469. break;
  470. case THREE_Y6D:
  471. motor[0] = PIDMIX_NOYAW(-0.0f, +1.0f, 1); // REAR
  472. motor[1] = PIDMIX_NOYAW(-1.0f, -1.0f, 1); // RIGHT
  473. motor[2] = PIDMIX_NOYAW(+1.0f, -1.0f, 1); // LEFT
  474. motor[3] = PIDMIX_NOYAW(+0.0f, +1.0f, 1); // UNDER_REAR
  475. motor[4] = PIDMIX_NOYAW(-1.0f, -1.0f, 1); // UNDER_RIGHT
  476. motor[5] = PIDMIX_NOYAW(+1.0f, -1.0f, 1); // UNDER_LEFT
  477. set_motor_noyaw(motor, 6);
  478. limit_motor[0] = motor[0] + (short)(pid_yaw * -1.0f); // REAR 后尾电机
  479. limit_motor[1] = motor[1] + (short)(pid_yaw * -1.0f); // RIGHT 右边电机
  480. limit_motor[2] = motor[2] + (short)(pid_yaw * -1.0f); // LEFT 左边电机
  481. limit_motor[3] = motor[3] + (short)(pid_yaw * 1.0f); // FRONT 前面电机
  482. limit_motor[4] = motor[4] + (short)(pid_yaw * 1.0f); // LEFT 左边电机
  483. limit_motor[5] = motor[5] + (short)(pid_yaw * 1.0f); // FRONT 前面电机
  484. get_max_yaw_value(limit_motor, 6);
  485. motor[0] += (yaw_limit * -1); // REAR 后尾电机
  486. motor[1] += (yaw_limit * -1); // RIGHT 右边电机
  487. motor[2] += (yaw_limit * -1); // LEFT 左边电机
  488. motor[3] += (yaw_limit * 1); // FRONT 前面电机
  489. motor[4] += (yaw_limit * 1); // LEFT 左边电机
  490. motor[5] += (yaw_limit * 1); // FRONT 前面电机
  491. break;
  492. case THREE_YI6D:
  493. motor[0] = PIDMIX_NOYAW(-0.0f, -1.0f, 1); // REAR
  494. motor[1] = PIDMIX_NOYAW(+1.0f, +1.0f, 1); // RIGHT
  495. motor[2] = PIDMIX_NOYAW(-1.0f, +1.0f, 1); // LEFT
  496. motor[3] = PIDMIX_NOYAW(+0.0f, -1.0f, 1); // UNDER_REAR
  497. motor[4] = PIDMIX_NOYAW(+1.0f, +1.0f, 1); // UNDER_RIGHT
  498. motor[5] = PIDMIX_NOYAW(-1.0f, +1.0f, 1); // UNDER_LEFT
  499. set_motor_noyaw(motor, 6);
  500. limit_motor[0] = motor[0] + (short)(pid_yaw * -1.0f); // REAR 后尾电机
  501. limit_motor[1] = motor[1] + (short)(pid_yaw * -1.0f); // RIGHT 右边电机
  502. limit_motor[2] = motor[2] + (short)(pid_yaw * -1.0f); // LEFT 左边电机
  503. limit_motor[3] = motor[3] + (short)(pid_yaw * 1.0f); // FRONT 前面电机
  504. limit_motor[4] = motor[4] + (short)(pid_yaw * 1.0f); // LEFT 左边电机
  505. limit_motor[5] = motor[5] + (short)(pid_yaw * 1.0f); // FRONT 前面电机
  506. get_max_yaw_value(limit_motor, 6);
  507. motor[0] += (yaw_limit * -1); // REAR 后尾电机
  508. motor[1] += (yaw_limit * -1); // RIGHT 右边电机
  509. motor[2] += (yaw_limit * -1); // LEFT 左边电机
  510. motor[3] += (yaw_limit * 1); // FRONT 前面电机
  511. motor[4] += (yaw_limit * 1); // LEFT 左边电机
  512. motor[5] += (yaw_limit * 1); // FRONT 前面电机
  513. break;
  514. case SIX_I6:
  515. _I6_motor_output_mix(_motor_failsafe_num, motor);
  516. break;
  517. case SIX_X6:
  518. _X6_motor_output_mix(_motor_failsafe_num, motor);
  519. break;
  520. case SIX_H6:
  521. _H6_motor_output_mix(_motor_failsafe_num, motor);
  522. break;
  523. // 上全正,下全反,跟DJI一样
  524. case FOUR_X8D:
  525. motor[0] = PIDMIX_NOYAW(-1.0f, -1.0f, 1);
  526. motor[1] = PIDMIX_NOYAW(+1.0f, -1.0f, 1);
  527. motor[2] = PIDMIX_NOYAW(+1.0f, +1.0f, 1);
  528. motor[3] = PIDMIX_NOYAW(-1.0f, +1.0f, 1);
  529. motor[4] = PIDMIX_NOYAW(-1.0f, -1.0f, 1);
  530. motor[5] = PIDMIX_NOYAW(+1.0f, -1.0f, 1);
  531. motor[6] = PIDMIX_NOYAW(+1.0f, +1.0f, 1);
  532. motor[7] = PIDMIX_NOYAW(-1.0f, +1.0f, 1);
  533. set_motor_noyaw(motor, 8);
  534. limit_motor[0] = motor[0] + (short)(pid_yaw * -1.0f); // REAR 后尾电机
  535. limit_motor[1] = motor[1] + (short)(pid_yaw * -1.0f); // RIGHT 右边电机
  536. limit_motor[2] = motor[2] + (short)(pid_yaw * -1.0f); // LEFT 左边电机
  537. limit_motor[3] = motor[3] + (short)(pid_yaw * -1.0f); // FRONT 前面电机
  538. limit_motor[4] = motor[4] + (short)(pid_yaw * 1.0f); // LEFT 左边电机
  539. limit_motor[5] = motor[5] + (short)(pid_yaw * 1.0f); // FRONT 前面电机
  540. limit_motor[6] = motor[6] + (short)(pid_yaw * 1.0f); // LEFT 左边电机
  541. limit_motor[7] = motor[7] + (short)(pid_yaw * 1.0f); // FRONT 前面电机
  542. get_max_yaw_value(limit_motor, 8);
  543. motor[0] += (yaw_limit * -1); // REAR 后尾电机
  544. motor[1] += (yaw_limit * -1); // RIGHT 右边电机
  545. motor[2] += (yaw_limit * -1); // LEFT 左边电机
  546. motor[3] += (yaw_limit * -1); // FRONT 前面电机
  547. motor[4] += (yaw_limit * 1); // LEFT 左边电机
  548. motor[5] += (yaw_limit * 1); // FRONT 前面电机
  549. motor[6] += (yaw_limit * 1); // LEFT 左边电机
  550. motor[7] += (yaw_limit * 1); // FRONT 前面电机
  551. break;
  552. // 上跟四轴X一样,下正好跟上相反
  553. case FOUR_X8M:
  554. motor[0] = PIDMIX_NOYAW(-1.0f, -1.0f, 1);
  555. motor[1] = PIDMIX_NOYAW(+1.0f, -1.0f, 1);
  556. motor[2] = PIDMIX_NOYAW(+1.0f, +1.0f, 1);
  557. motor[3] = PIDMIX_NOYAW(-1.0f, +1.0f, 1);
  558. motor[4] = PIDMIX_NOYAW(-1.0f, -1.0f, 1);
  559. motor[5] = PIDMIX_NOYAW(+1.0f, -1.0f, 1);
  560. motor[6] = PIDMIX_NOYAW(+1.0f, +1.0f, 1);
  561. motor[7] = PIDMIX_NOYAW(-1.0f, +1.0f, 1);
  562. set_motor_noyaw(motor, 8);
  563. limit_motor[0] = motor[0] + (short)(pid_yaw * -1.0f); // REAR 后尾电机
  564. limit_motor[1] = motor[1] + (short)(pid_yaw * 1.0f); // RIGHT 右边电机
  565. limit_motor[2] = motor[2] + (short)(pid_yaw * -1.0f); // LEFT 左边电机
  566. limit_motor[3] = motor[3] + (short)(pid_yaw * 1.0f); // FRONT 前面电机
  567. limit_motor[4] = motor[4] + (short)(pid_yaw * 1.0f); // LEFT 左边电机
  568. limit_motor[5] = motor[5] + (short)(pid_yaw * -1.0f); // FRONT 前面电机
  569. limit_motor[6] = motor[6] + (short)(pid_yaw * 1.0f); // LEFT 左边电机
  570. limit_motor[7] = motor[7] + (short)(pid_yaw * -1.0f); // FRONT 前面电机
  571. get_max_yaw_value(limit_motor, 8);
  572. motor[0] += (yaw_limit * -1); // REAR 后尾电机
  573. motor[1] += (yaw_limit * 1); // RIGHT 右边电机
  574. motor[2] += (yaw_limit * -1); // LEFT 左边电机
  575. motor[3] += (yaw_limit * 1); // FRONT 前面电机
  576. motor[4] += (yaw_limit * 1); // LEFT 左边电机
  577. motor[5] += (yaw_limit * -1); // FRONT 前面电机
  578. motor[6] += (yaw_limit * 1); // LEFT 左边电机
  579. motor[7] += (yaw_limit * -1); // FRONT 前面电机
  580. break;
  581. // 和 x8m 相反
  582. case FOUR_X8MR:
  583. motor[0] = PIDMIX_NOYAW(-1.0f, -1.0f, 1);
  584. motor[1] = PIDMIX_NOYAW(+1.0f, -1.0f, 1);
  585. motor[2] = PIDMIX_NOYAW(+1.0f, +1.0f, 1);
  586. motor[3] = PIDMIX_NOYAW(-1.0f, +1.0f, 1);
  587. motor[4] = PIDMIX_NOYAW(-1.0f, -1.0f, 1);
  588. motor[5] = PIDMIX_NOYAW(+1.0f, -1.0f, 1);
  589. motor[6] = PIDMIX_NOYAW(+1.0f, +1.0f, 1);
  590. motor[7] = PIDMIX_NOYAW(-1.0f, +1.0f, 1);
  591. set_motor_noyaw(motor, 8);
  592. limit_motor[0] = motor[0] + (short)(pid_yaw * 1.0f); // REAR 后尾电机
  593. limit_motor[1] = motor[1] + (short)(pid_yaw * -1.0f); // RIGHT 右边电机
  594. limit_motor[2] = motor[2] + (short)(pid_yaw * 1.0f); // LEFT 左边电机
  595. limit_motor[3] = motor[3] + (short)(pid_yaw * -1.0f); // FRONT 前面电机
  596. limit_motor[4] = motor[4] + (short)(pid_yaw * -1.0f); // LEFT 左边电机
  597. limit_motor[5] = motor[5] + (short)(pid_yaw * 1.0f); // FRONT 前面电机
  598. limit_motor[6] = motor[6] + (short)(pid_yaw * -1.0f); // LEFT 左边电机
  599. limit_motor[7] = motor[7] + (short)(pid_yaw * 1.0f); // FRONT 前面电机
  600. get_max_yaw_value(limit_motor, 8);
  601. motor[0] += (yaw_limit * 1); // REAR 后尾电机
  602. motor[1] += (yaw_limit * -1); // RIGHT 右边电机
  603. motor[2] += (yaw_limit * 1); // LEFT 左边电机
  604. motor[3] += (yaw_limit * -1); // FRONT 前面电机
  605. motor[4] += (yaw_limit * -1); // LEFT 左边电机
  606. motor[5] += (yaw_limit * 1); // FRONT 前面电机
  607. motor[6] += (yaw_limit * -1); // LEFT 左边电机
  608. motor[7] += (yaw_limit * 1); // FRONT 前面电机
  609. break;
  610. // 上全反,下全正,跟DJI一样
  611. case FOUR_X8DR:
  612. motor[0] = PIDMIX_NOYAW(-1.0f, -1.0f, 1);
  613. motor[1] = PIDMIX_NOYAW(+1.0f, -1.0f, 1);
  614. motor[2] = PIDMIX_NOYAW(+1.0f, +1.0f, 1);
  615. motor[3] = PIDMIX_NOYAW(-1.0f, +1.0f, 1);
  616. motor[4] = PIDMIX_NOYAW(-1.0f, -1.0f, 1);
  617. motor[5] = PIDMIX_NOYAW(+1.0f, -1.0f, 1);
  618. motor[6] = PIDMIX_NOYAW(+1.0f, +1.0f, 1);
  619. motor[7] = PIDMIX_NOYAW(-1.0f, +1.0f, 1);
  620. set_motor_noyaw(motor, 8);
  621. limit_motor[0] = motor[0] + (short)(pid_yaw * 1.0f); // REAR 后尾电机
  622. limit_motor[1] = motor[1] + (short)(pid_yaw * 1.0f); // RIGHT 右边电机
  623. limit_motor[2] = motor[2] + (short)(pid_yaw * 1.0f); // LEFT 左边电机
  624. limit_motor[3] = motor[3] + (short)(pid_yaw * 1.0f); // FRONT 前面电机
  625. limit_motor[4] = motor[4] + (short)(pid_yaw * -1.0f); // LEFT 左边电机
  626. limit_motor[5] = motor[5] + (short)(pid_yaw * -1.0f); // FRONT 前面电机
  627. limit_motor[6] = motor[6] + (short)(pid_yaw * -1.0f); // LEFT 左边电机
  628. limit_motor[7] = motor[7] + (short)(pid_yaw * -1.0f); // FRONT 前面电机
  629. get_max_yaw_value(limit_motor, 8);
  630. motor[0] += (yaw_limit * 1); // REAR 后尾电机
  631. motor[1] += (yaw_limit * 1); // RIGHT 右边电机
  632. motor[2] += (yaw_limit * 1); // LEFT 左边电机
  633. motor[3] += (yaw_limit * 1); // FRONT 前面电机
  634. motor[4] += (yaw_limit * -1); // LEFT 左边电机
  635. motor[5] += (yaw_limit * -1); // FRONT 前面电机
  636. motor[6] += (yaw_limit * -1); // LEFT 左边电机
  637. motor[7] += (yaw_limit * -1); // FRONT 前面电机
  638. break;
  639. case EIGHT_I8:
  640. motor[0] = PIDMIX_NOYAW(-0.0f, -1.0f, 1); // REAR_R
  641. motor[1] = PIDMIX_NOYAW(+7.0f / 10.0f, -7.0f / 10.0f, 1); // FRONT_R
  642. motor[2] = PIDMIX_NOYAW(+1.0f, -0.0f, 1); // REAR_L
  643. motor[3] = PIDMIX_NOYAW(+7.0f / 10.0f, +7.0f / 10.0f, 1); // FRONT_L
  644. motor[4] = PIDMIX_NOYAW(+0.0f, +1.0f, 1); // UNDER_REAR_R
  645. motor[5] = PIDMIX_NOYAW(-7.0f / 10.0f, +7.0f / 10.0f, 1); // UNDER_FRONT_R
  646. motor[6] = PIDMIX_NOYAW(-1.0f, +0.0f, 1); // UNDER_REAR_L
  647. motor[7] = PIDMIX_NOYAW(-7.0f / 10.0f, -7.0f / 10.0f, 1); // UNDER_FRONT_L
  648. set_motor_noyaw(motor, 8);
  649. limit_motor[0] = motor[0] + (short)(pid_yaw * -1.0f); // REAR 后尾电机
  650. limit_motor[1] = motor[1] + (short)(pid_yaw * 1.0f); // RIGHT 右边电机
  651. limit_motor[2] = motor[2] + (short)(pid_yaw * -1.0f); // LEFT 左边电机
  652. limit_motor[3] = motor[3] + (short)(pid_yaw * 1.0f); // FRONT 前面电机
  653. limit_motor[4] = motor[4] + (short)(pid_yaw * -1.0f); // LEFT 左边电机
  654. limit_motor[5] = motor[5] + (short)(pid_yaw * 1.0f); // FRONT 前面电机
  655. limit_motor[6] = motor[6] + (short)(pid_yaw * -1.0f); // LEFT 左边电机
  656. limit_motor[7] = motor[7] + (short)(pid_yaw * 1.0f); // FRONT 前面电机
  657. get_max_yaw_value(limit_motor, 8);
  658. motor[0] += (yaw_limit * -1); // REAR 后尾电机
  659. motor[1] += (yaw_limit * 1); // RIGHT 右边电机
  660. motor[2] += (yaw_limit * -1); // LEFT 左边电机
  661. motor[3] += (yaw_limit * 1); // FRONT 前面电机
  662. motor[4] += (yaw_limit * -1); // LEFT 左边电机
  663. motor[5] += (yaw_limit * 1); // FRONT 前面电机
  664. motor[6] += (yaw_limit * -1); // LEFT 左边电机
  665. motor[7] += (yaw_limit * 1); // FRONT 前面电机
  666. break;
  667. case EIGHT_X8:
  668. motor[0] = PIDMIX_NOYAW(-1.0f / 2.0f, -1.0f, 1); // REAR_R
  669. motor[1] = PIDMIX_NOYAW(+1.0f / 2.0f, -1.0f, 1); // FRONT_R
  670. motor[2] = PIDMIX_NOYAW(+1.0f, -1.0f / 2.0f, 1); // REAR_L
  671. motor[3] = PIDMIX_NOYAW(+1.0f, +1.0f / 2.0f, 1); // FRONT_L
  672. motor[4] = PIDMIX_NOYAW(+1.0f / 2.0f, +1.0f, 1); // UNDER_REAR_R
  673. motor[5] = PIDMIX_NOYAW(-1.0f / 2.0f, +1.0f, 1); // UNDER_FRONT_R
  674. motor[6] = PIDMIX_NOYAW(-1.0f, +1.0f / 2.0f, 1); // UNDER_REAR_L
  675. motor[7] = PIDMIX_NOYAW(-1.0f, -1.0f / 2.0f, 1); // UNDER_FRONT_L
  676. set_motor_noyaw(motor, 8);
  677. limit_motor[0] = motor[0] + (short)(pid_yaw * -1.0f); // REAR 后尾电机
  678. limit_motor[1] = motor[1] + (short)(pid_yaw * 1.0f); // RIGHT 右边电机
  679. limit_motor[2] = motor[2] + (short)(pid_yaw * -1.0f); // LEFT 左边电机
  680. limit_motor[3] = motor[3] + (short)(pid_yaw * 1.0f); // FRONT 前面电机
  681. limit_motor[4] = motor[4] + (short)(pid_yaw * -1.0f); // LEFT 左边电机
  682. limit_motor[5] = motor[5] + (short)(pid_yaw * 1.0f); // FRONT 前面电机
  683. limit_motor[6] = motor[6] + (short)(pid_yaw * -1.0f); // LEFT 左边电机
  684. limit_motor[7] = motor[7] + (short)(pid_yaw * 1.0f); // FRONT 前面电机
  685. get_max_yaw_value(limit_motor, 8);
  686. motor[0] += (yaw_limit * -1); // REAR 后尾电机
  687. motor[1] += (yaw_limit * 1); // RIGHT 右边电机
  688. motor[2] += (yaw_limit * -1); // LEFT 左边电机
  689. motor[3] += (yaw_limit * 1); // FRONT 前面电机
  690. motor[4] += (yaw_limit * -1); // LEFT 左边电机
  691. motor[5] += (yaw_limit * 1); // FRONT 前面电机
  692. motor[6] += (yaw_limit * -1); // LEFT 左边电机
  693. motor[7] += (yaw_limit * 1); // FRONT 前面电机
  694. break;
  695. default:
  696. break;
  697. }
  698. ground_motor_slow_launch(conf_par.jixing / 10, 0.6f);
  699. for (uint8_t chNum = MOTOR1; chNum <= conf_par.jixing / 10; chNum++)
  700. {
  701. set_motor_pwm(chNum, motor[chNum - 1]);
  702. can_motor[chNum - 1] = motor[chNum - 1];
  703. }
  704. }
  705. /**
  706. * @brief 电机输出饱和保护,牺牲高度控制
  707. *
  708. * @param p_motor 电机输出量
  709. * @param motor_num 电机个数
  710. */
  711. void set_motor_noyaw(uint16_t *p_motor, unsigned char motor_num)
  712. {
  713. short min_pwm_out = 2000, max_pwm_out = 1000;
  714. short pwm_out_error = 0;
  715. unsigned char i = 0;
  716. // 起飞后最低转速保护1050
  717. short min_thr_constrain = Min_PWM_Out;
  718. for (i = 0; i < motor_num; i++)
  719. {
  720. // 找出电机输出的最大值和最小值
  721. if (p_motor[i] > max_pwm_out)
  722. {
  723. max_pwm_out = p_motor[i];
  724. }
  725. if (p_motor[i] < min_pwm_out)
  726. {
  727. min_pwm_out = p_motor[i];
  728. }
  729. }
  730. // 如果最大油门大于2000,则牺牲油门控制量
  731. if (max_pwm_out > Max_PWM_Out)
  732. {
  733. pwm_out_error = max_pwm_out - Max_PWM_Out;
  734. }
  735. if (min_pwm_out < min_thr_constrain)
  736. {
  737. pwm_out_error = min_pwm_out - min_thr_constrain;
  738. }
  739. // 将所有电机量同时减掉pwm_out_error
  740. if (pwm_out_error != 0)
  741. {
  742. for (i = 0; i < motor_num; i++)
  743. {
  744. p_motor[i] -= pwm_out_error;
  745. if (p_motor[i] > Max_PWM_Out)
  746. {
  747. p_motor[i] = Max_PWM_Out;
  748. }
  749. else if (p_motor[i] < min_thr_constrain)
  750. {
  751. p_motor[i] = min_thr_constrain;
  752. }
  753. }
  754. }
  755. }
  756. /**
  757. * @brief 电机输出饱和保护,牺牲航向
  758. *
  759. * @param p_motor 电机值
  760. * @param motor_num 电机个数
  761. */
  762. void get_max_yaw_value(uint16_t *p_motor, unsigned char motor_num)
  763. {
  764. static unsigned int motor_restriction_time = 0;
  765. short min_pwm_out = 2000, max_pwm_out = 1000;
  766. unsigned char i = 0;
  767. short pwm_out_error = 0;
  768. // 起飞后最低转速保护1050
  769. short min_thr_constrain = Min_PWM_Out;
  770. if (ground_air_status == IN_AIR)
  771. {
  772. min_thr_constrain = 1050;
  773. }
  774. else
  775. {
  776. min_thr_constrain = Min_PWM_Out;
  777. }
  778. for (i = 0; i < motor_num; i++)
  779. {
  780. // 找出电机输出的最大值和最小值
  781. if (p_motor[i] > max_pwm_out)
  782. {
  783. max_pwm_out = p_motor[i];
  784. }
  785. if (p_motor[i] < min_pwm_out)
  786. {
  787. min_pwm_out = p_motor[i];
  788. }
  789. }
  790. // 如果最大油门大于2000,则牺牲航向控制
  791. if (max_pwm_out > Max_PWM_Out)
  792. {
  793. pwm_out_error = max_pwm_out - Max_PWM_Out;
  794. }
  795. if (min_pwm_out < min_thr_constrain)
  796. {
  797. pwm_out_error = min_thr_constrain - min_pwm_out;
  798. }
  799. // 如果输出油门到达限幅,则触发航向只做减速控制
  800. if (max_pwm_out > Max_PWM_Out || min_pwm_out < min_thr_constrain)
  801. {
  802. if (micros() - motor_restriction_time > 500000)
  803. {
  804. yaw_output_restriciton = 1;
  805. pid_m_yaw.angle_i_item = 0.0f;
  806. }
  807. }
  808. else
  809. {
  810. yaw_output_restriciton = 0;
  811. motor_restriction_time = micros();
  812. }
  813. if (pid_yaw >= 0.0f)
  814. yaw_limit = (short)pid_yaw - pwm_out_error;
  815. else
  816. yaw_limit = (short)pid_yaw + pwm_out_error;
  817. // 如果飞机倾斜角度大于 35 度,则不加入航向控制量
  818. float tilt_angle = acosf(cosf(pid_m_roll.angle_c * DEG_TO_RAD) *
  819. cosf(pid_m_pitch.angle_c * DEG_TO_RAD)) * RAD_TO_DEG;
  820. if (fabsf(tilt_angle) > 35.0f)
  821. {
  822. yaw_limit = 0.0f;
  823. pid_m_yaw.angle_i_item = 0.0f;
  824. }
  825. }
  826. /**
  827. * @brief 获取航向输出控制受限标志
  828. *
  829. * @return uint8_t
  830. */
  831. uint8_t MotorOutput_GetYawRestrictionStatus(void)
  832. {
  833. return yaw_output_restriciton;
  834. }
  835. uint8_t Motor_GetFailsafeNum(void)
  836. {
  837. return _motor_failsafe_num;
  838. }
  839. #ifdef SOFT_MOTOR_TEST
  840. //============================================================================
  841. // 测试配置
  842. //============================================================================
  843. #define TEST_MOTOR_COUNT 4 // 四轴电机数量
  844. #define TEST_DURATION_MS 5000 // 每个测试持续时间 5秒
  845. #define TEST_STEP_DELAY_MS 2000 // 测试步骤间隔 2秒
  846. // 测试模式
  847. typedef enum {
  848. TEST_MODE_IDLE = 0, // 怠速测试
  849. TEST_MODE_HOVER, // 悬停测试
  850. TEST_MODE_ROLL, // 横滚测试
  851. TEST_MODE_PITCH, // 俯仰测试
  852. TEST_MODE_YAW, // 偏航测试
  853. TEST_MODE_THROTTLE, // 油门测试
  854. TEST_MODE_SATURATION, // 饱和保护测试
  855. TEST_MODE_END // 结束
  856. } test_mode_t;
  857. // 测试结果
  858. typedef struct {
  859. uint16_t motor_values[8]; // 电机输出值
  860. float pid_roll; // 横滚PID输出
  861. float pid_pitch; // 俯仰PID输出
  862. float pid_yaw; // 偏航PID输出
  863. float pid_thr; // 油门PID输出
  864. uint8_t yaw_restricted; // 偏航受限标志
  865. uint32_t timestamp; // 时间戳
  866. } motor_test_data_t;
  867. //============================================================================
  868. // 全局测试变量
  869. //============================================================================
  870. static motor_test_data_t g_test_data[100]; // 存储测试数据
  871. static uint16_t g_data_index = 0;
  872. static bool g_test_running = false;
  873. //============================================================================
  874. // 辅助函数
  875. //============================================================================
  876. /**
  877. * @brief 打印电机输出值
  878. */
  879. static void print_motor_values(uint16_t *motors, uint8_t count, const char* title)
  880. {
  881. printf("%s: ", title);
  882. for (int i = 0; i < count; i++) {
  883. printf("M%d=%d ", i+1, motors[i]);
  884. }
  885. printf("\n");
  886. }
  887. /**
  888. * @brief 记录测试数据
  889. */
  890. static void record_test_data(void)
  891. {
  892. if (g_data_index < sizeof(g_test_data)/sizeof(g_test_data[0])) {
  893. g_test_data[g_data_index].timestamp = micros();
  894. // 读取当前电机输出
  895. for (int i = 0; i < TEST_MOTOR_COUNT; i++) {
  896. g_test_data[g_data_index].motor_values[i] = get_motor_pwm(i+1);
  897. }
  898. // 记录PID值(从全局变量获取)
  899. extern float pid_roll, pid_pitch, pid_yaw, pid_thr;
  900. g_test_data[g_data_index].pid_roll = pid_roll;
  901. g_test_data[g_data_index].pid_pitch = pid_pitch;
  902. g_test_data[g_data_index].pid_yaw = pid_yaw;
  903. g_test_data[g_data_index].pid_thr = pid_thr;
  904. // 记录偏航受限标志
  905. g_test_data[g_data_index].yaw_restricted = MotorOutput_GetYawRestrictionStatus();
  906. g_data_index++;
  907. }
  908. }
  909. /**
  910. * @brief 打印测试总结
  911. */
  912. static void print_test_summary(void)
  913. {
  914. printf("\n========== Test Summary ==========\n");
  915. printf("Total records: %d\n", g_data_index);
  916. if (g_data_index > 0) {
  917. printf("\nLast record:\n");
  918. printf(" Time: %lu us\n", g_test_data[g_data_index-1].timestamp);
  919. printf(" Motors: ");
  920. for (int i = 0; i < TEST_MOTOR_COUNT; i++) {
  921. printf("%d ", g_test_data[g_data_index-1].motor_values[i]);
  922. }
  923. printf("\n");
  924. printf(" PID: roll=%.2f, pitch=%.2f, yaw=%.2f, thr=%.2f\n",
  925. g_test_data[g_data_index-1].pid_roll,
  926. g_test_data[g_data_index-1].pid_pitch,
  927. g_test_data[g_data_index-1].pid_yaw,
  928. g_test_data[g_data_index-1].pid_thr);
  929. printf(" Yaw restricted: %d\n", g_test_data[g_data_index-1].yaw_restricted);
  930. }
  931. printf("==================================\n");
  932. }
  933. //============================================================================
  934. // 测试函数
  935. //============================================================================
  936. /**
  937. * @brief 测试1: 怠速输出测试
  938. * @note 测试电机在锁状态下的怠速输出
  939. */
  940. void test_motor_idle_output(void)
  941. {
  942. printf("\n=== Test 1: Idle Output Test ===\n");
  943. printf("Expected: All motors output idle speed (%d us)\n", Min_PWM_Out);
  944. // 确保处于锁定状态
  945. thr_lock_status = LOCKED;
  946. // 运行电机输出函数
  947. for (int i = 0; i < 100; i++) {
  948. locked_motor_output();
  949. delay_ms(10);
  950. if (i % 20 == 0) {
  951. uint16_t motors[4];
  952. for (int j = 0; j < TEST_MOTOR_COUNT; j++) {
  953. motors[j] = get_motor_pwm(j+1);
  954. }
  955. print_motor_values(motors, TEST_MOTOR_COUNT, " Motors");
  956. }
  957. }
  958. printf("Idle output test completed\n");
  959. }
  960. /**
  961. * @brief 测试2: 悬停状态混控测试
  962. * @note 模拟悬停状态,测试电机混控输出
  963. */
  964. void test_motor_hover_mix(void)
  965. {
  966. printf("\n=== Test 2: Hover Mix Test ===\n");
  967. printf("Setting: roll=0, pitch=0, yaw=0, throttle=middle\n");
  968. // 设置悬停状态
  969. extern float pid_roll, pid_pitch, pid_yaw, pid_thr;
  970. pid_roll = 0.0f;
  971. pid_pitch = 0.0f;
  972. pid_yaw = 0.0f;
  973. pid_thr = 1500.0f; // 悬停油门
  974. // 设置解锁定状态
  975. thr_lock_status = UNLOCKED;
  976. ground_air_status = IN_AIR;
  977. // 运行电机输出
  978. for (int i = 0; i < 50; i++) {
  979. unlocked_motor_output();
  980. delay_ms(20);
  981. if (i % 10 == 0) {
  982. uint16_t motors[4];
  983. for (int j = 0; j < TEST_MOTOR_COUNT; j++) {
  984. motors[j] = get_motor_pwm(j+1);
  985. }
  986. print_motor_values(motors, TEST_MOTOR_COUNT, " Hover");
  987. }
  988. }
  989. printf("Hover mix test completed\n");
  990. }
  991. /**
  992. * @brief 测试3: 横滚响应测试
  993. * @note 测试横滚控制时的电机响应
  994. */
  995. void test_motor_roll_response(void)
  996. {
  997. printf("\n=== Test 3: Roll Response Test ===\n");
  998. extern float pid_roll, pid_pitch, pid_yaw, pid_thr;
  999. pid_pitch = 0.0f;
  1000. pid_yaw = 0.0f;
  1001. pid_thr = 1500.0f;
  1002. thr_lock_status = UNLOCKED;
  1003. ground_air_status = IN_AIR;
  1004. // 测试正横滚
  1005. printf("Positive roll (+100):\n");
  1006. pid_roll = 100.0f;
  1007. for (int i = 0; i < 25; i++) {
  1008. unlocked_motor_output();
  1009. delay_ms(20);
  1010. if (i % 10 == 0) {
  1011. uint16_t motors[4];
  1012. for (int j = 0; j < TEST_MOTOR_COUNT; j++) {
  1013. motors[j] = get_motor_pwm(j+1);
  1014. }
  1015. print_motor_values(motors, TEST_MOTOR_COUNT, " Motors");
  1016. }
  1017. }
  1018. delay_ms(1000);
  1019. // 测试负横滚
  1020. printf("Negative roll (-100):\n");
  1021. pid_roll = -100.0f;
  1022. for (int i = 0; i < 25; i++) {
  1023. unlocked_motor_output();
  1024. delay_ms(20);
  1025. if (i % 10 == 0) {
  1026. uint16_t motors[4];
  1027. for (int j = 0; j < TEST_MOTOR_COUNT; j++) {
  1028. motors[j] = get_motor_pwm(j+1);
  1029. }
  1030. print_motor_values(motors, TEST_MOTOR_COUNT, " Motors");
  1031. }
  1032. }
  1033. printf("Roll response test completed\n");
  1034. }
  1035. /**
  1036. * @brief 测试4: 俯仰响应测试
  1037. */
  1038. void test_motor_pitch_response(void)
  1039. {
  1040. printf("\n=== Test 4: Pitch Response Test ===\n");
  1041. extern float pid_roll, pid_pitch, pid_yaw, pid_thr;
  1042. pid_roll = 0.0f;
  1043. pid_yaw = 0.0f;
  1044. pid_thr = 1500.0f;
  1045. thr_lock_status = UNLOCKED;
  1046. ground_air_status = IN_AIR;
  1047. // 测试正俯仰
  1048. printf("Positive pitch (+100):\n");
  1049. pid_pitch = 100.0f;
  1050. for (int i = 0; i < 25; i++) {
  1051. unlocked_motor_output();
  1052. delay_ms(20);
  1053. if (i % 10 == 0) {
  1054. uint16_t motors[4];
  1055. for (int j = 0; j < TEST_MOTOR_COUNT; j++) {
  1056. motors[j] = get_motor_pwm(j+1);
  1057. }
  1058. print_motor_values(motors, TEST_MOTOR_COUNT, " Motors");
  1059. }
  1060. }
  1061. delay_ms(1000);
  1062. // 测试负俯仰
  1063. printf("Negative pitch (-100):\n");
  1064. pid_pitch = -100.0f;
  1065. for (int i = 0; i < 25; i++) {
  1066. unlocked_motor_output();
  1067. delay_ms(20);
  1068. if (i % 10 == 0) {
  1069. uint16_t motors[4];
  1070. for (int j = 0; j < TEST_MOTOR_COUNT; j++) {
  1071. motors[j] = get_motor_pwm(j+1);
  1072. }
  1073. print_motor_values(motors, TEST_MOTOR_COUNT, " Motors");
  1074. }
  1075. }
  1076. printf("Pitch response test completed\n");
  1077. }
  1078. /**
  1079. * @brief 测试5: 偏航响应测试
  1080. */
  1081. void test_motor_yaw_response(void)
  1082. {
  1083. printf("\n=== Test 5: Yaw Response Test ===\n");
  1084. extern float pid_roll, pid_pitch, pid_yaw, pid_thr;
  1085. pid_roll = 0.0f;
  1086. pid_pitch = 0.0f;
  1087. pid_thr = 1500.0f;
  1088. thr_lock_status = UNLOCKED;
  1089. ground_air_status = IN_AIR;
  1090. // 测试正偏航
  1091. printf("Positive yaw (+100):\n");
  1092. pid_yaw = 100.0f;
  1093. for (int i = 0; i < 25; i++) {
  1094. unlocked_motor_output();
  1095. delay_ms(20);
  1096. if (i % 10 == 0) {
  1097. uint16_t motors[4];
  1098. for (int j = 0; j < TEST_MOTOR_COUNT; j++) {
  1099. motors[j] = get_motor_pwm(j+1);
  1100. }
  1101. print_motor_values(motors, TEST_MOTOR_COUNT, " Motors");
  1102. }
  1103. }
  1104. delay_ms(1000);
  1105. // 测试负偏航
  1106. printf("Negative yaw (-100):\n");
  1107. pid_yaw = -100.0f;
  1108. for (int i = 0; i < 25; i++) {
  1109. unlocked_motor_output();
  1110. delay_ms(20);
  1111. if (i % 10 == 0) {
  1112. uint16_t motors[4];
  1113. for (int j = 0; j < TEST_MOTOR_COUNT; j++) {
  1114. motors[j] = get_motor_pwm(j+1);
  1115. }
  1116. print_motor_values(motors, TEST_MOTOR_COUNT, " Motors");
  1117. }
  1118. }
  1119. printf("Yaw response test completed\n");
  1120. }
  1121. /**
  1122. * @brief 测试6: 油门响应测试
  1123. */
  1124. void test_motor_throttle_response(void)
  1125. {
  1126. printf("\n=== Test 6: Throttle Response Test ===\n");
  1127. extern float pid_roll, pid_pitch, pid_yaw, pid_thr;
  1128. pid_roll = 0.0f;
  1129. pid_pitch = 0.0f;
  1130. pid_yaw = 0.0f;
  1131. thr_lock_status = UNLOCKED;
  1132. ground_air_status = IN_AIR;
  1133. // 测试低油门
  1134. printf("Low throttle (1200):\n");
  1135. pid_thr = 1200.0f;
  1136. for (int i = 0; i < 25; i++) {
  1137. unlocked_motor_output();
  1138. delay_ms(20);
  1139. if (i % 10 == 0) {
  1140. uint16_t motors[4];
  1141. for (int j = 0; j < TEST_MOTOR_COUNT; j++) {
  1142. motors[j] = get_motor_pwm(j+1);
  1143. }
  1144. print_motor_values(motors, TEST_MOTOR_COUNT, " Motors");
  1145. }
  1146. }
  1147. delay_ms(1000);
  1148. // 测试中油门
  1149. printf("Mid throttle (1500):\n");
  1150. pid_thr = 1500.0f;
  1151. for (int i = 0; i < 25; i++) {
  1152. unlocked_motor_output();
  1153. delay_ms(20);
  1154. if (i % 10 == 0) {
  1155. uint16_t motors[4];
  1156. for (int j = 0; j < TEST_MOTOR_COUNT; j++) {
  1157. motors[j] = get_motor_pwm(j+1);
  1158. }
  1159. print_motor_values(motors, TEST_MOTOR_COUNT, " Motors");
  1160. }
  1161. }
  1162. delay_ms(1000);
  1163. // 测试高油门
  1164. printf("High throttle (1800):\n");
  1165. pid_thr = 1800.0f;
  1166. for (int i = 0; i < 25; i++) {
  1167. unlocked_motor_output();
  1168. delay_ms(20);
  1169. if (i % 10 == 0) {
  1170. uint16_t motors[4];
  1171. for (int j = 0; j < TEST_MOTOR_COUNT; j++) {
  1172. motors[j] = get_motor_pwm(j+1);
  1173. }
  1174. print_motor_values(motors, TEST_MOTOR_COUNT, " Motors");
  1175. }
  1176. }
  1177. printf("Throttle response test completed\n");
  1178. }
  1179. /**
  1180. * @brief 测试7: 饱和保护测试
  1181. * @note 测试当电机输出达到极限时的饱和保护机制
  1182. */
  1183. void test_motor_saturation_protection(void)
  1184. {
  1185. printf("\n=== Test 7: Saturation Protection Test ===\n");
  1186. extern float pid_roll, pid_pitch, pid_yaw, pid_thr;
  1187. thr_lock_status = UNLOCKED;
  1188. ground_air_status = IN_AIR;
  1189. // 设置高油门和大横滚,触发饱和
  1190. printf("Testing saturation (high throttle + large roll):\n");
  1191. pid_thr = 1900.0f;
  1192. pid_roll = 300.0f;
  1193. pid_pitch = 0.0f;
  1194. pid_yaw = 0.0f;
  1195. for (int i = 0; i < 50; i++) {
  1196. unlocked_motor_output();
  1197. delay_ms(20);
  1198. if (i % 10 == 0) {
  1199. uint16_t motors[4];
  1200. for (int j = 0; j < TEST_MOTOR_COUNT; j++) {
  1201. motors[j] = get_motor_pwm(j+1);
  1202. }
  1203. print_motor_values(motors, TEST_MOTOR_COUNT, " Motors");
  1204. printf(" Yaw restricted: %d\n", MotorOutput_GetYawRestrictionStatus());
  1205. }
  1206. }
  1207. printf("Saturation protection test completed\n");
  1208. }
  1209. /**
  1210. * @brief 测试8: 电机平滑输出测试
  1211. */
  1212. void test_motor_smoothing(void)
  1213. {
  1214. printf("\n=== Test 8: Motor Smoothing Test ===\n");
  1215. extern float pid_roll, pid_pitch, pid_yaw, pid_thr;
  1216. thr_lock_status = UNLOCKED;
  1217. ground_air_status = ON_GROUND; // 地面状态启用平滑
  1218. printf("Testing motor output smoothing (ground mode):\n");
  1219. // 突然改变PID值,观察平滑效果
  1220. pid_roll = 0.0f;
  1221. pid_pitch = 0.0f;
  1222. pid_yaw = 0.0f;
  1223. pid_thr = 1500.0f;
  1224. for (int i = 0; i < 30; i++) {
  1225. unlocked_motor_output();
  1226. delay_ms(50);
  1227. if (i == 10) {
  1228. printf(" Step change: roll=200\n");
  1229. pid_roll = 200.0f;
  1230. }
  1231. if (i % 5 == 0) {
  1232. uint16_t motors[4];
  1233. for (int j = 0; j < TEST_MOTOR_COUNT; j++) {
  1234. motors[j] = get_motor_pwm(j+1);
  1235. }
  1236. print_motor_values(motors, TEST_MOTOR_COUNT, " Motors");
  1237. }
  1238. }
  1239. printf("Motor smoothing test completed\n");
  1240. }
  1241. //============================================================================
  1242. // 主测试函数
  1243. //============================================================================
  1244. /**
  1245. * @brief 运行完整的电机测试
  1246. */
  1247. void run_motor_output_test(void)
  1248. {
  1249. printf("\n");
  1250. printf("╔═══════════════════════════════════════════════════════════╗\n");
  1251. printf("║ Quadcopter Motor Output Test Suite ║\n");
  1252. printf("╚═══════════════════════════════════════════════════════════╝\n");
  1253. // 初始化
  1254. motor_output_initial();
  1255. board_delay_ms(100);
  1256. printf("\nMotor output initialized\n");
  1257. printf("Motor count: %d\n", conf_par.jixing / 10);
  1258. printf("Idle speed: %d us\n", Min_PWM_Out);
  1259. printf("Max PWM: %d us\n", Max_PWM_Out);
  1260. g_data_index = 0;
  1261. g_test_running = true;
  1262. // 运行测试
  1263. test_motor_idle_output();
  1264. board_delay_ms(1000);
  1265. test_motor_hover_mix();
  1266. board_delay_ms(1000);
  1267. test_motor_roll_response();
  1268. board_delay_ms(1000);
  1269. test_motor_pitch_response();
  1270. board_delay_ms(1000);
  1271. test_motor_yaw_response();
  1272. board_delay_ms(1000);
  1273. test_motor_throttle_response();
  1274. board_delay_ms(1000);
  1275. test_motor_saturation_protection();
  1276. board_delay_ms(1000);
  1277. test_motor_smoothing();
  1278. // 打印总结
  1279. print_test_summary();
  1280. // 恢复安全状态
  1281. thr_lock_status = LOCKED;
  1282. locked_motor_output();
  1283. printf("\nAll tests completed!\n");
  1284. g_test_running = false;
  1285. }
  1286. /**
  1287. * @brief 简单的单步测试
  1288. * @param step 测试步骤: 1-怠速, 2-悬停, 3-横滚, 4-俯仰, 5-偏航
  1289. */
  1290. void run_motor_simple_test(uint8_t step)
  1291. {
  1292. extern float pid_roll, pid_pitch, pid_yaw, pid_thr;
  1293. motor_output_initial();
  1294. board_delay_ms(100);
  1295. switch(step) {
  1296. case 1:
  1297. printf("Idle test\n");
  1298. thr_lock_status = LOCKED;
  1299. for (int i = 0; i < 100; i++) {
  1300. locked_motor_output();
  1301. board_delay_ms(10);
  1302. }
  1303. break;
  1304. case 2:
  1305. printf("Hover test\n");
  1306. thr_lock_status = UNLOCKED;
  1307. ground_air_status = IN_AIR;
  1308. pid_roll = 0; pid_pitch = 0; pid_yaw = 0; pid_thr = 1500;
  1309. for (int i = 0; i < 100; i++) {
  1310. unlocked_motor_output();
  1311. board_delay_ms(10);
  1312. }
  1313. break;
  1314. case 3:
  1315. printf("Roll test\n");
  1316. thr_lock_status = UNLOCKED;
  1317. ground_air_status = IN_AIR;
  1318. pid_roll = 100; pid_pitch = 0; pid_yaw = 0; pid_thr = 1500;
  1319. for (int i = 0; i < 100; i++) {
  1320. unlocked_motor_output();
  1321. board_delay_ms(10);
  1322. }
  1323. break;
  1324. case 4:
  1325. printf("Pitch test\n");
  1326. thr_lock_status = UNLOCKED;
  1327. ground_air_status = IN_AIR;
  1328. pid_roll = 0; pid_pitch = 100; pid_yaw = 0; pid_thr = 1500;
  1329. for (int i = 0; i < 100; i++) {
  1330. unlocked_motor_output();
  1331. board_delay_ms(10);
  1332. }
  1333. break;
  1334. case 5:
  1335. printf("Yaw test\n");
  1336. thr_lock_status = UNLOCKED;
  1337. ground_air_status = IN_AIR;
  1338. pid_roll = 0; pid_pitch = 0; pid_yaw = 100; pid_thr = 1500;
  1339. for (int i = 0; i < 100; i++) {
  1340. unlocked_motor_output();
  1341. board_delay_ms(10);
  1342. }
  1343. break;
  1344. default:
  1345. printf("Invalid test step\n");
  1346. break;
  1347. }
  1348. // 恢复锁定
  1349. thr_lock_status = LOCKED;
  1350. locked_motor_output();
  1351. printf("Test completed\n");
  1352. }
  1353. #endif