usr_motor.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707
  1. /*
  2. * usr_gpio.c
  3. *
  4. * Created on: 2022年4月11日
  5. * Author: amos
  6. */
  7. #include "usr_motor.h"
  8. #define BOUNCE_THRESHOLD 2 // 800ms长按阈值
  9. #define LONG_PRESS_THRESHOLD 80 // 800ms长按阈值
  10. #define CONTINUOUS_INTERVAL 40 // 300ms内算连续按键
  11. #define USER_STOP_TIMER 200 // 长按2s不放,停止混匀
  12. #define SUPER_LONG_PRESS_TIMER 3000 // 超长按30s(10ms单位),触发SmartConfig配网
  13. #define FIND_OPTIC_SPEED 50 // 找光开速度
  14. #define FIND_OPTIC_BACK_SPEED 43 // 过光开回退速度
  15. #define FIND_OPTIC_BACK_GAP 2 // 过光开回退速度
  16. #define MAX_RUNCNT 4 // 找光开速度
  17. typedef enum swing_state {
  18. SWING_IDLE,
  19. SWING_START,
  20. SWING_LOOP,
  21. SWING_END,
  22. LOOP_START,
  23. LOOP_END,
  24. EMERGENCY_END,
  25. FIND_GAPNUM,
  26. FIND_HOMEGAP,
  27. SWING_ERR,
  28. } swing_state_def;
  29. typedef enum {
  30. KEY_IDLE, // 空闲状态
  31. KEY_SHORT, // 短按
  32. KEY_CONTINUE, // 连续按
  33. KEY_LONG, // 空闲状态,长按抬起,复位设备
  34. KEY_END //混匀状态,长按不释放,停止混匀
  35. } KeyState;
  36. volatile
  37. struct {
  38. KeyState state;
  39. uint32_t press_tick; // 按下时刻(10ms单位)
  40. uint32_t release_tick; // 当前释放时刻
  41. uint32_t last_release_tick; // 新增:上一次释放时刻
  42. uint32_t click_count; // 连续按键计数
  43. uint32_t last_level;
  44. uint32_t key_valid_flag; // 连续按键标志
  45. } key_ctrl;
  46. static uint8_t key_superlong_flag = 0; // 30s超长按已触发SmartConfig配网
  47. swing_state_def sw_st, last_st;
  48. unsigned int keyState = 0;
  49. unsigned int MotorOptState = 0;
  50. extern uint32_t conter100ms;
  51. uint32_t conter10ms;
  52. int32_t runLoopTime = 600;
  53. int32_t runLoopSpeed = 80;
  54. uint32_t curtimeinner;
  55. uint32_t timeinner = 6000;
  56. uint32_t swingtime = 100;
  57. uint32_t swingtimeRight = 40;
  58. uint32_t swingtimePause = 100;
  59. int32_t backOptFlag = 0;
  60. int32_t startLoopConter = 0;
  61. int32_t swingspeed = 60;
  62. int32_t stateFistin;
  63. int32_t REVORFWD;
  64. //extern uint8_t error_set(uint8_t error);
  65. uint32_t timerBuf[16];
  66. uint32_t timerconter;
  67. uint32_t intoGap;
  68. uint32_t OutGap;
  69. uint32_t lastGap;
  70. uint32_t judgeGap;
  71. uint32_t curGap;
  72. uint32_t checkPWM;
  73. MOTOR_DIR findhomedir;
  74. uint32_t intoGapFlag;
  75. uint32_t resetFromSwingFlag;
  76. uint32_t swingLoopHigtTime = 0;
  77. uint32_t swingHigStopflage = 0;
  78. uint32_t swingStoptime = 0;
  79. uint32_t startFindHometime = 0;
  80. uint32_t findHome = 0;
  81. extern void DelayMS(uint32_t ms);
  82. //冒泡法排序,排序结果有小到大
  83. void Bubble_Sort(unsigned int *Array,unsigned int n)
  84. {
  85. unsigned char i, j;
  86. unsigned int temp;
  87. for(i=0;i<n-1;i++)
  88. {
  89. for(j=i+1;j<n;j++)
  90. {
  91. if(Array[i] > Array[j])
  92. {
  93. temp = Array[i];
  94. Array[i] = Array[j];
  95. Array[j] = temp;
  96. }
  97. }
  98. }
  99. }
  100. uint32_t Read_Motor(void) {
  101. return (HAL_GPIO_ReadPin(MINI7_V2_REF_SW2_GPIO_Port,MINI7_V2_REF_SW2_Pin)) ? 1 : 0; // 检查PF5引脚值
  102. }
  103. uint32_t Read_key(void) {
  104. return (HAL_GPIO_ReadPin(MINI7_V2_KEY_GPIO_Port,MINI7_V2_KEY_Pin)) ? 1 : 0; // 检查PF5引脚值
  105. }
  106. uint32_t TIMER_GetCounter(void) {
  107. return htim4.Instance->CNT;
  108. }
  109. /**
  110. * @brief SetMotorPwm
  111. *
  112. * @param value between 0-100
  113. *
  114. * @return None
  115. *
  116. * @details
  117. */
  118. void SetMotorPwm(int dir, int32_t value) {
  119. /* Start PWM counter */
  120. unsigned int DutyCycle;
  121. if (value > 100) {
  122. value = 100;
  123. } else if (value < 0) {
  124. value = 0;
  125. DutyCycle = 0;
  126. }
  127. DutyCycle = value*10;
  128. if (dir) {
  129. __HAL_TIM_SET_COMPARE(&htim3, TIM_CHANNEL_2, 0);
  130. __HAL_TIM_SET_COMPARE(&htim3, TIM_CHANNEL_3, DutyCycle);
  131. } else {
  132. __HAL_TIM_SET_COMPARE(&htim3, TIM_CHANNEL_2, DutyCycle);
  133. __HAL_TIM_SET_COMPARE(&htim3, TIM_CHANNEL_3, 0);
  134. }
  135. }
  136. /**
  137. * @brief GPIO PA/PB/PG/PH IRQ
  138. *
  139. * @param None
  140. *
  141. * @return None
  142. *
  143. * @details The PA/PB/PG/PH default IRQ, declared in startup_M031Series.s.
  144. */
  145. void KYE_IRQHandler(void) {
  146. volatile uint32_t temp;
  147. conter10ms = sysinf.conter10ms;
  148. /* To check if PB.2 interrupt occurred */
  149. {
  150. uint8_t curr_level = Read_key();
  151. if (curr_level != key_ctrl.last_level) {
  152. key_ctrl.last_level = curr_level;
  153. if (curr_level == 0) { // 下降沿(按下)
  154. key_ctrl.press_tick = conter10ms;
  155. if (key_ctrl.state == KEY_SHORT) {
  156. key_ctrl.last_release_tick =
  157. key_ctrl.release_tick; // 更新上次释放时间
  158. } else {
  159. key_ctrl.last_release_tick = 0;
  160. }
  161. } else { // 上升沿(释放)
  162. key_ctrl.release_tick = conter10ms;
  163. if (key_superlong_flag) { // 30s超长按已触发SmartConfig,释放不产生按键事件
  164. key_superlong_flag = 0;
  165. key_ctrl.press_tick = 0;
  166. key_ctrl.last_release_tick = 0;
  167. key_ctrl.state = KEY_IDLE;
  168. key_ctrl.key_valid_flag = 0;
  169. return;
  170. }
  171. uint32_t press_duration = 0;
  172. if(key_ctrl.press_tick){
  173. press_duration = key_ctrl.release_tick - key_ctrl.press_tick;
  174. }
  175. key_ctrl.press_tick = 0;
  176. // 长按判定
  177. if (press_duration <= BOUNCE_THRESHOLD) {
  178. key_ctrl.state = KEY_IDLE;
  179. key_ctrl.last_release_tick = 0;
  180. } else if (press_duration >= LONG_PRESS_THRESHOLD) {
  181. key_ctrl.state = KEY_LONG;
  182. key_ctrl.last_release_tick = 0; // 更新上次释放时间
  183. key_ctrl.key_valid_flag = 1;
  184. }
  185. // 短按判定
  186. else {
  187. key_ctrl.click_count = 1;
  188. key_ctrl.state = KEY_SHORT;
  189. if ((key_ctrl.release_tick - key_ctrl.last_release_tick) <=
  190. CONTINUOUS_INTERVAL) {
  191. key_ctrl.key_valid_flag = 1;
  192. key_ctrl.click_count++;
  193. key_ctrl.state = KEY_CONTINUE;
  194. }
  195. }
  196. }
  197. }
  198. }
  199. }
  200. void check_keyShortValid(void) {
  201. volatile uint32_t temp;
  202. /* To check if PB.2 interrupt occurred */
  203. if (key_ctrl.state == KEY_SHORT) {
  204. if ((conter10ms - key_ctrl.release_tick) > CONTINUOUS_INTERVAL * 2) {
  205. key_ctrl.key_valid_flag = 1;
  206. }
  207. }
  208. if (sw_st != SWING_IDLE && key_ctrl.press_tick) {
  209. if ((conter10ms - key_ctrl.press_tick) > USER_STOP_TIMER) {
  210. key_ctrl.state = KEY_END;
  211. key_ctrl.key_valid_flag = 1;
  212. key_ctrl.press_tick = 0;
  213. }
  214. }
  215. // 超长按(≥30s,仅空闲状态能持续按住)触发ESP32 SmartConfig配网
  216. if (!key_superlong_flag && key_ctrl.press_tick && !WIFI_provision_active()) {
  217. if ((sysinf.conter10ms - key_ctrl.press_tick) >= SUPER_LONG_PRESS_TIMER) {
  218. key_superlong_flag = 1;
  219. key_ctrl.press_tick = 0; // 清除按压计时,释放时不再触发长按复位
  220. key_ctrl.release_tick = 0;
  221. key_ctrl.last_release_tick = 0;
  222. Van_Device_Printf(VAN_LOG_NOTICE, "\n[key] 30s pressed, start smartconfig");
  223. WIFI_provision_request();
  224. }
  225. }
  226. }
  227. /**
  228. * @brief GPIO PC/PD/PE/PF IRQ
  229. *
  230. * @param None
  231. *
  232. * @return None
  233. *
  234. * @details The PC/PD/PE/PF default IRQ, declared in startup_M031Series.s.
  235. */
  236. void OPTO_IRQHandler(void) {
  237. //
  238. // volatile unsigned int temp;
  239. /* To check if PB.2 interrupt occurred */
  240. {
  241. conter10ms = sysinf.conter10ms;
  242. switch(sw_st){
  243. case FIND_GAPNUM:{
  244. if (Read_Motor()){
  245. intoGap = TIMER_GetCounter();
  246. }else{
  247. OutGap = TIMER_GetCounter();
  248. if((conter10ms - startFindHometime) > MOTOR_LOOP_TIM){
  249. if(MAX_RUNCNT > timerconter){
  250. if(OutGap > intoGap){
  251. timerBuf[timerconter++] = OutGap - intoGap;
  252. }else{
  253. timerBuf[timerconter++] = OutGap + 0x10000 - intoGap;
  254. }
  255. if(MAX_RUNCNT == timerconter){
  256. Bubble_Sort(timerBuf,MAX_RUNCNT);
  257. judgeGap = 0;
  258. if(timerBuf[0]*1.25<timerBuf[1]){
  259. judgeGap = timerBuf[0]+timerBuf[0];
  260. }else{
  261. judgeGap = timerBuf[1]+timerBuf[0];
  262. }
  263. // for(unsigned int i =0;i<(MAX_RUNCNT/2);i++){
  264. // judgeGap += timerBuf[i];
  265. // }
  266. judgeGap =judgeGap*0.625;
  267. timerBuf[timerconter++] = judgeGap ;
  268. /*判决门限为最低两个值均值的1.5倍*/
  269. }
  270. }else{
  271. if(OutGap > intoGap){
  272. curGap = OutGap - intoGap;
  273. }else{
  274. curGap = OutGap + 0x10000 - intoGap;
  275. }
  276. timerBuf[timerconter++] = curGap ;
  277. if(curGap > judgeGap){
  278. sw_st = FIND_HOMEGAP;
  279. SetMotorPwm(MOTOR_REV, 0);
  280. SetMotorPwm(MOTOR_FWD, 0);
  281. stateFistin = 1;
  282. checkPWM = checkPWM*0.8; /*降速便于复位*/
  283. findhomedir = (MOTOR_DIR)!findhomedir;
  284. }
  285. if(timerconter==sizeof(timerBuf)){
  286. SetMotorPwm(MOTOR_REV, 0);
  287. SetMotorPwm(MOTOR_FWD, 0);
  288. timerconter = 0;
  289. }
  290. }
  291. }
  292. }
  293. }
  294. break;
  295. case FIND_HOMEGAP:{
  296. /*找远点阶段,光开亮停止*/
  297. if (Read_Motor()){
  298. SetMotorPwm(MOTOR_REV, 0);
  299. SetMotorPwm(MOTOR_FWD, 0);
  300. intoGapFlag = 1;
  301. intoGap = TIMER_GetCounter();
  302. }
  303. }
  304. break;
  305. case SWING_LOOP:{
  306. /*找远点阶段,光开亮停止*/
  307. if(startLoopConter > 0){
  308. startLoopConter --;
  309. }else{
  310. if (Read_Motor()){
  311. SetMotorPwm(MOTOR_REV, 0);
  312. SetMotorPwm(MOTOR_FWD, 0);
  313. intoGapFlag = 1;
  314. intoGap = TIMER_GetCounter();
  315. }
  316. }
  317. }break;
  318. default:{
  319. SetMotorPwm(MOTOR_REV, 0);
  320. SetMotorPwm(MOTOR_FWD, 0);
  321. // sw_st = FIND_GAPNUM;
  322. // stateFistin = 1;
  323. }break;
  324. }
  325. }
  326. }
  327. /**
  328. * @brief Motor_Init
  329. *
  330. * @param init key and optic input
  331. *
  332. * @return None
  333. *
  334. * @details
  335. */
  336. void Motor_Init()
  337. {
  338. key_ctrl.last_level = 1;
  339. timeinner = sysinf.timeinner;
  340. swingspeed = sysinf.swingspeed;
  341. swingtime =sysinf.swingtime;
  342. // swingtimeRight =sysinf.swingtimeRight;
  343. // swingtimePause =sysinf.swingtimePause;
  344. // runLoopTime = sysinf.runLoopTime;
  345. // runLoopSpeed =sysinf.runLoopSpeed;
  346. }
  347. /**
  348. * @brief Motor_mix
  349. *
  350. * @param init key and optic input
  351. *
  352. * @return 0正常,1没离开光开,2,没回到光开
  353. *
  354. * @details 设置夹爪转动参数。
  355. */
  356. uint32_t Motor_mix(uint32_t time, uint32_t speed, uint32_t swing,
  357. uint32_t timeRight, uint32_t timePause) {
  358. timeinner = time;
  359. swingtime = swing;
  360. swingspeed = speed;
  361. // swingtimeRight = timeRight;
  362. // swingtimePause = timePause;
  363. // timeinner = time - time % (swing + timeRight);
  364. // curtimeinner = timeinner;
  365. if (speed == 0) {
  366. return 0;
  367. }
  368. curtimeinner = conter10ms;
  369. sw_st = SWING_LOOP;
  370. startLoopConter = 2;
  371. stateFistin = 1;
  372. backOptFlag = 1;
  373. findhomedir = MOTOR_REV;
  374. return 0;
  375. }
  376. /**
  377. * @brief Motor_deal_key
  378. *
  379. * @param 按键处理,短按,左右摇;连续点击,转圈摇;长按,复位;
  380. *
  381. * @return 0正常,1没离开光开,2,没回到光开
  382. *
  383. * @details 夹爪根据。
  384. */
  385. uint32_t Motor_deal_key(swing_state_def st, KeyState key) {
  386. if (st == SWING_IDLE) {
  387. switch (key) {
  388. case KEY_SHORT:
  389. curtimeinner = conter10ms;
  390. sw_st = SWING_LOOP;
  391. startLoopConter = 2;
  392. stateFistin = 1;
  393. backOptFlag = 1;
  394. findhomedir = MOTOR_REV;
  395. break;
  396. // case KEY_CONTINUE:
  397. // curtimeinner = conter10ms;
  398. // sw_st = LOOP_START;
  399. // stateFistin = 1;
  400. // break;
  401. case KEY_LONG:
  402. curtimeinner = conter10ms;
  403. sw_st = FIND_GAPNUM;
  404. stateFistin = 1;
  405. break;
  406. default:
  407. break;
  408. }
  409. }
  410. if (key == KEY_LONG || key == KEY_END ) {
  411. sw_st = FIND_GAPNUM;
  412. stateFistin = 1;
  413. resetFromSwingFlag = 0;
  414. }
  415. return 0;
  416. }
  417. void Motor_reset(void) {
  418. curtimeinner = conter10ms;
  419. sw_st = FIND_GAPNUM;
  420. stateFistin = 1;
  421. resetFromSwingFlag = 0;
  422. }
  423. /**
  424. * @brief Motor_mix_work
  425. *
  426. * @param init key and optic input
  427. *
  428. * @return 0正常,1没离开光开,2,没回到光开
  429. *
  430. * @details 夹爪根据。
  431. */
  432. uint32_t Motor_mix_work(void) {
  433. static uint32_t last10ms;
  434. static uint32_t firstINGap;
  435. static uint32_t powerOnFlag = 1;
  436. check_keyShortValid();
  437. if(powerOnFlag){
  438. powerOnFlag = 0;
  439. // Motor_Check(0);
  440. resetFromSwingFlag = 0;
  441. sw_st = FIND_GAPNUM;
  442. stateFistin = 1;
  443. }
  444. conter10ms = sysinf.conter10ms;
  445. if (key_ctrl.key_valid_flag) {
  446. key_ctrl.key_valid_flag = 0;
  447. if (!WIFI_provision_active()) { // SmartConfig配网期间屏蔽按键动作
  448. Motor_deal_key(sw_st, key_ctrl.state);
  449. }
  450. key_ctrl.state = KEY_IDLE;
  451. }
  452. if(sw_st == SWING_ERR){
  453. /*电机故障,不执行操作*/
  454. return 0;
  455. }
  456. switch (sw_st) {
  457. case SWING_IDLE: {
  458. SetMotorPwm(MOTOR_REV, 0);
  459. SetMotorPwm(MOTOR_FWD, 0);
  460. } break;
  461. case FIND_GAPNUM: {
  462. if (stateFistin) {
  463. timerconter = 0;
  464. intoGap = 0;
  465. OutGap = 0;
  466. lastGap = 0;
  467. judgeGap = 0;
  468. HAL_TIM_Base_Stop(&htim4);
  469. HAL_TIM_Base_Start(&htim4);
  470. last10ms = conter10ms;
  471. stateFistin = 0;
  472. startFindHometime = conter10ms;
  473. findhomedir = MOTOR_REV;
  474. checkPWM = swingspeed;
  475. SetMotorPwm(findhomedir, checkPWM);
  476. } else {
  477. if (((conter10ms - last10ms) > MOTOR_LOOP_TIM*4 )&&(timerconter < MAX_RUNCNT)) {
  478. /*电机不动,返回报错*/
  479. SetMotorPwm(MOTOR_REV, 0);
  480. sw_st = SWING_ERR;
  481. error_set(ERROR_MOTOR_FIALED,0);
  482. error_set(ERROR_MOTOR_FIALED,1);
  483. updateAmplifState(SYS_STATE_ERROR_HW,0);
  484. updateAmplifState(SYS_STATE_ERROR_HW,1);
  485. return 1;
  486. }
  487. }
  488. } break;
  489. case FIND_HOMEGAP: {
  490. if (stateFistin) {
  491. last10ms = conter10ms;
  492. stateFistin = 0;
  493. intoGapFlag = 0;
  494. firstINGap = 0;
  495. SetMotorPwm(findhomedir, checkPWM);
  496. } else {
  497. if (!intoGapFlag && (conter10ms - last10ms) > MOTOR_LOOP_TIM) {
  498. /*电机来回不动,返回报错*/
  499. stateFistin = 1;
  500. checkPWM = checkPWM/0.85; /*降速便于复位*/
  501. }
  502. if(intoGapFlag){
  503. if(!firstINGap){
  504. /*进入缝隙,置位,延时判读*/
  505. firstINGap = 1;
  506. last10ms = conter10ms;
  507. }else{
  508. if((conter10ms - last10ms) > MOTOR_MOVEFORM_OPTIC_TIM){
  509. if(Read_Motor()){
  510. sw_st = SWING_IDLE;
  511. stateFistin = 1;
  512. findHome = 1;
  513. }
  514. else{
  515. sw_st = FIND_HOMEGAP;
  516. stateFistin = 1;
  517. findhomedir = (MOTOR_DIR)!findhomedir;
  518. checkPWM = checkPWM*0.9; /*降速便于复位*/
  519. }
  520. }
  521. }
  522. }
  523. }
  524. } break;
  525. case LOOP_START: {
  526. if (stateFistin) {
  527. SetMotorPwm(MOTOR_REV, runLoopSpeed);
  528. last10ms = conter10ms;
  529. stateFistin = 0;
  530. } else {
  531. if ((conter10ms - last10ms) == (runLoopTime)) {
  532. SetMotorPwm(MOTOR_REV, 0);
  533. SetMotorPwm(MOTOR_FWD, 0);
  534. }
  535. if ((conter10ms - last10ms) >= (runLoopTime + swingtimePause)) {
  536. sw_st = LOOP_END;
  537. stateFistin = 1;
  538. REVORFWD = 0;
  539. }
  540. }
  541. } break;
  542. case LOOP_END: {
  543. sw_st = FIND_GAPNUM;
  544. stateFistin = 1;
  545. REVORFWD = 1;
  546. } break;
  547. case SWING_LOOP: {
  548. if (stateFistin ) {
  549. /**/
  550. if(backOptFlag){
  551. if(swingtime< (conter10ms - swingStoptime)){
  552. checkPWM = swingspeed;
  553. backOptFlag = 0;
  554. SetMotorPwm(findhomedir, checkPWM);
  555. stateFistin = 0;
  556. last10ms = conter10ms;
  557. swingHigStopflage = 0;
  558. }
  559. }else{
  560. SetMotorPwm(findhomedir, checkPWM);
  561. stateFistin = 0;
  562. last10ms = conter10ms;
  563. swingHigStopflage = 0;
  564. }
  565. intoGapFlag = 0;
  566. firstINGap = 0;
  567. } else{
  568. if (!intoGapFlag && (conter10ms - last10ms) > MOTOR_LOOP_TIM) {
  569. /*速度过底,电机回不来*/
  570. backOptFlag = 1;
  571. swingStoptime = conter10ms;
  572. findhomedir = MOTOR_REV;
  573. stateFistin = 1;
  574. }
  575. if(intoGapFlag){
  576. if(!firstINGap){
  577. /*进入缝隙,置位,延时判读*/
  578. firstINGap = 1;
  579. last10ms = conter10ms;
  580. }else{
  581. if((conter10ms - last10ms) > MOTOR_MOVEFORM_OPTIC_TIM){
  582. stateFistin = 1;
  583. if(Read_Motor()){
  584. backOptFlag = 1;
  585. swingStoptime = conter10ms;
  586. findhomedir = MOTOR_REV;
  587. }
  588. else{
  589. findhomedir = (MOTOR_DIR)!findhomedir;
  590. checkPWM = checkPWM*0.9; /*降速便于复位*/
  591. }
  592. }
  593. }
  594. }
  595. if ((conter10ms - curtimeinner) > (timeinner)){
  596. resetFromSwingFlag = 0;
  597. __HAL_TIM_SET_COMPARE(&htim3, TIM_CHANNEL_2, 0);
  598. __HAL_TIM_SET_COMPARE(&htim3, TIM_CHANNEL_2, 0);
  599. timerconter = 0;
  600. memset(timerBuf,0,sizeof(timerBuf));
  601. stateFistin = 1;
  602. sw_st = FIND_GAPNUM;
  603. }
  604. }
  605. } break;
  606. default:
  607. sw_st = SWING_ERR;
  608. break;
  609. }
  610. return 0;
  611. }
  612. /**
  613. * @brief Motor_mix
  614. *
  615. * @param init key and optic input
  616. *
  617. * @return 空闲返回1 其他返回0
  618. *
  619. * @details 设置夹爪转动参数。
  620. */
  621. uint32_t Motor_idle() {
  622. return(sw_st == SWING_IDLE);
  623. }
  624. uint32_t print_home_time() {
  625. if(findHome){
  626. findHome = 0;
  627. Van_Device_Printf(VAN_LOG_NOTICE,"gap:%d ",timerconter);
  628. if(timerconter > sizeof(timerBuf)){
  629. timerconter = sizeof(timerBuf);
  630. }
  631. for(unsigned int i=0;i<timerconter;i++){
  632. Van_Device_Printf(VAN_LOG_NOTICE," %d ",timerBuf[i]);
  633. }
  634. Van_Device_Printf(VAN_LOG_NOTICE,"\n");
  635. }
  636. return 0;
  637. }