MOTOR_STEP_SPEED_T_C_ATY.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706
  1. /**
  2. * @file MOTOR_STEP_SPEED_T_C_ATY.c
  3. *
  4. * @param Project DEVICE_GENERAL_ATY_LIB
  5. *
  6. * @author ATY
  7. *
  8. * @copyright
  9. * - Copyright 2017 - 2026 MZ-ATY
  10. * - This code follows:
  11. * - MZ-ATY Various Contents Joint Statement -
  12. * <a href="https://mengze.top/MZ-ATY_VCJS">
  13. * https://mengze.top/MZ-ATY_VCJS</a>
  14. * - CC 4.0 BY-NC-SA -
  15. * <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/">
  16. * https://creativecommons.org/licenses/by-nc-sa/4.0/</a>
  17. * - Your use will be deemed to have accepted the terms of this statement.
  18. *
  19. * @brief Trapezoidal acceleration and deceleration control implementation for stepper motor
  20. *
  21. * @note import angle to use COMPENSATE
  22. *
  23. * @version
  24. * - 1_00_260119 > ATY
  25. * -# Initial version
  26. ********************************************************************************
  27. */
  28. #ifndef __MOTOR_STEP_SPEED_T_C_ATY_C
  29. #define __MOTOR_STEP_SPEED_T_C_ATY_C
  30. #include "MOTOR_STEP_SPEED_T_C_ATY.h"
  31. #include <math.h>
  32. #define MOTOR_STEP_SPEED_T_C_ATY_TAG "\r\n[MOTOR_STEP_SPEED_T_C_ATY] "
  33. /******************************* For user *************************************/
  34. /******************************************************************************/
  35. uint8_t MSSTC_PulseToggle = 0; // pulse toggle counter (for complete pulse)
  36. int32_t MSSTC_CelCount = 0; // ac/deceleration step counter
  37. uint32_t MSSTC_FastStepDelay = 0; // minimum delay at fullSpeed
  38. int32_t MSSTC_StepDelay = 0; // current step delay (timer ticks)
  39. uint32_t MSSTC_NewStepDelay = 0; // calculated step delay for next step from last step
  40. int32_t MSSTC_Rest = 0; // Remainder for precision
  41. int32_t MSSTC_LastDelay = 0; // Last acceleration delay
  42. // for angle compensation ------------------------------------------------------
  43. float MSSTC_StartAngle = 0;
  44. uint32_t MSSTC_CurrentTurn = 0;
  45. float MSSTC_CurrentAngle = 0;
  46. uint32_t MSSTC_TargetTurn = 0;
  47. float MSSTC_TargetAngle = 0;
  48. float MSSTC_AnglePerStep = 0;
  49. float MSSTC_TargetAngleL = 0;
  50. float MSSTC_TargetAngleH = 0;
  51. float MSSTC_AngleDiff = 0;
  52. int32_t MSSTC_StepDiff = 0;
  53. uint8_t MSSTC_CelCountErrCount = 0;
  54. float K = 0.676;
  55. /**
  56. * @brief Move motor with trapezoidal dev->fullSpeed profile
  57. * @param dev Pointer to motor device structure
  58. * @param steps Number of steps to move (positive=CW, negative=CCW)
  59. * @return 0=success, 1=busy, 2=invalid parameters
  60. */
  61. uint8_t MSSTC_Move(struct MOTOR_STEP_SPEED_T_C_ATY_Dev* dev,
  62. uint32_t steps, float startAngle){
  63. if(MSSTC_IsRunning(dev) == 1){
  64. return 1;
  65. }
  66. if(steps == 0 || dev->fullSpeed == 0){
  67. dev->stop();
  68. return 0;
  69. }
  70. if(dev->unitDivision == 0)
  71. dev->unitDivision = 1;
  72. if(dev->powerOnlyRun == 1){
  73. dev->enable = 1;
  74. }
  75. dev->enSet(dev->enable);
  76. dev->dirSet(dev->direction);
  77. /* Handle single step */
  78. if(steps == 1){
  79. dev->runState = MSSTC_STATE_DECEL;
  80. MSSTC_CelCount = -1;
  81. MSSTC_StepDelay = 1000;
  82. dev->start();
  83. return 0;
  84. }
  85. /* Calculate motor parameters */
  86. // alpha = 2 * PI / stepsPerRevolution [rad/step]
  87. float alpha = 2.0 * 3.14159265359 / (dev->fullSteps * dev->microstepping);
  88. /* Calculate minimum delay (maximum dev->fullSpeed) */
  89. // wmax = fullSpeed [rad/sec]
  90. // tmin = alpha / wmax [sec/step] (t = distance / velocity)
  91. // fastStepDelay = cmin = tmin * timerFreq = alpha * timerFreq / fullSpeed
  92. MSSTC_FastStepDelay = (uint32_t)(alpha * dev->timerFreq * dev->unitDivision / dev->fullSpeed);
  93. if(MSSTC_FastStepDelay <= 1){
  94. MSSTC_FastStepDelay = 2;
  95. }
  96. /* Calculate initial step delay (first step) */
  97. // t0 = sqrt(2 * alpha / (acceleration / unit)) = sqrt(2 * alpha * unit / acceleration)
  98. // stepDelay = K * timerFreq * t0
  99. if(dev->acceleration != 0){
  100. MSSTC_StepDelay = (uint32_t)(K * dev->timerFreq
  101. * sqrt(2.0 * alpha * dev->unitDivision / dev->acceleration));
  102. }
  103. if(dev->acceleration == 0){
  104. // no acceleration and no deceleration
  105. MSSTC_StepDelay = MSSTC_FastStepDelay;
  106. }
  107. /* Calculate steps to reach fullSpeed */
  108. // accelSteps = (fullSpeed / unit)^2 / (2 * alpha * (acceleration / unit))
  109. // accelSteps = fullSpeed^2 / (2 * alpha * acceleration * unit)
  110. dev->accelSteps = 1;
  111. if(dev->acceleration != 0){
  112. dev->accelSteps = (uint32_t)((double)dev->fullSpeed
  113. / (2.0 * alpha * dev->acceleration * dev->unitDivision)
  114. * dev->fullSpeed);
  115. }
  116. if(dev->accelSteps == 0 || dev->acceleration == 0){
  117. dev->accelSteps = 1;
  118. }
  119. /* Calculate steps before deceleration */
  120. dev->accelLimit = 1;
  121. if(dev->acceleration != 0 && dev->deceleration != 0){
  122. dev->accelLimit = (uint32_t)(steps * dev->deceleration
  123. / (dev->acceleration + dev->deceleration));
  124. }
  125. else if(dev->deceleration == 0){
  126. dev->accelLimit = steps - 1;
  127. }
  128. if(dev->accelLimit == 0 || dev->acceleration == 0){
  129. dev->accelLimit = 1;
  130. }
  131. /* Calculate steps deceleration */
  132. dev->decelSteps = 1;
  133. if(dev->acceleration != 0 && dev->deceleration != 0){
  134. if(dev->accelLimit <= dev->accelSteps){
  135. // not to fullSpeed
  136. dev->decelSteps = steps - dev->accelLimit;
  137. dev->accelSteps = dev->accelLimit;
  138. }
  139. else{
  140. // accelerate to fullSpeed
  141. dev->decelSteps = (uint32_t)(dev->accelSteps
  142. * dev->acceleration / dev->deceleration);
  143. }
  144. }
  145. else if(dev->acceleration == 0 && dev->deceleration != 0){
  146. dev->decelSteps = (uint32_t)((double)dev->fullSpeed
  147. / (2.0 * alpha * dev->deceleration * dev->unitDivision)
  148. * dev->fullSpeed);
  149. if(dev->decelSteps >= steps - 1){
  150. dev->decelSteps = steps - 1;
  151. }
  152. }
  153. if(dev->decelSteps == 0 || dev->deceleration == 0){
  154. dev->decelSteps = 1;
  155. }
  156. /* Calculate deceleration start position */
  157. dev->decelStart = steps - dev->decelSteps;
  158. if(dev->mode == MSSTC_MODE_SPEED_OPEN){
  159. dev->decelSteps = (uint32_t)((double)dev->fullSpeed
  160. / (2.0 * alpha * dev->deceleration * dev->unitDivision)
  161. * dev->fullSpeed);
  162. }
  163. /* Set initial state */
  164. if(MSSTC_StepDelay <= MSSTC_FastStepDelay){
  165. // fullSpeed is very low, no acceleration
  166. MSSTC_StepDelay = MSSTC_FastStepDelay;
  167. dev->runState = MSSTC_STATE_FULL;
  168. }
  169. else{
  170. dev->runState = MSSTC_STATE_ACCEL;
  171. }
  172. /* Reset counters */
  173. dev->stepCountCurrent = 0;
  174. MSSTC_PulseToggle = 0;
  175. MSSTC_CelCount = 0;
  176. MSSTC_NewStepDelay = 0;
  177. MSSTC_Rest = 0;
  178. MSSTC_LastDelay = MSSTC_StepDelay;
  179. MSSTC_CelCountErrCount = 0;
  180. MSSTC_StartAngle = startAngle;
  181. MSSTC_CurrentTurn = 0;
  182. MSSTC_CurrentAngle = startAngle;
  183. MSSTC_TargetTurn = steps / (dev->fullSteps * dev->microstepping);
  184. MSSTC_TargetAngle = ((steps % (dev->fullSteps * dev->microstepping)) * 360.0)
  185. / (dev->fullSteps * dev->microstepping) + startAngle;
  186. while(MSSTC_TargetAngle >= 360.0){
  187. MSSTC_TargetAngle -= 360.0;
  188. MSSTC_TargetTurn++;
  189. }
  190. MSSTC_AnglePerStep = 360.0 / (dev->fullSteps * dev->microstepping);
  191. MSSTC_TargetAngleL = MSSTC_TargetAngle - MSSTC_AnglePerStep;
  192. MSSTC_TargetAngleH = MSSTC_TargetAngle + MSSTC_AnglePerStep;
  193. MSSTC_AngleDiff = 0;
  194. MSSTC_StepDiff = 0;
  195. /* Start PWM and timer */
  196. dev->setTimerCompare(MSSTC_StepDelay);
  197. dev->start();
  198. return 0;
  199. }
  200. /**
  201. * @brief Timer interrupt handler - dev->fullSpeed decision algorithm
  202. * @param dev Pointer to motor device structure
  203. * @note Call this function in timer compare interrupt
  204. */
  205. void MSSTC_IRQHandler(struct MOTOR_STEP_SPEED_T_C_ATY_Dev* dev){
  206. /* Update timer compare value */
  207. dev->setTimerCompare(MSSTC_StepDelay);
  208. /* Toggle counter for complete pulse generation */
  209. MSSTC_PulseToggle++;
  210. if(MSSTC_PulseToggle >= 2){
  211. MSSTC_PulseToggle = 0;
  212. /* State machine for dev->fullSpeed control */
  213. switch(dev->runState){
  214. case MSSTC_STATE_STOP:
  215. {
  216. dev->stop();
  217. if(dev->mode == MSSTC_MODE_DISTANCE_CLOSE){
  218. dev->runState = MSSTC_STATE_COMPENSATE;
  219. }
  220. else{
  221. dev->runState = MSSTC_STATE_STANDBY;
  222. if(dev->powerOnlyRun == 1){
  223. dev->enable = 0;
  224. dev->enSet(dev->enable);
  225. }
  226. }
  227. break;
  228. }
  229. case MSSTC_STATE_ACCEL:
  230. {
  231. dev->stepCountCurrent++;
  232. MSSTC_CelCount++;
  233. /* Calculate new step delay */
  234. MSSTC_NewStepDelay = MSSTC_StepDelay -
  235. ((2 * MSSTC_StepDelay + MSSTC_Rest) /
  236. (4 * MSSTC_CelCount + 1));
  237. MSSTC_Rest = ((2 * MSSTC_StepDelay + MSSTC_Rest) %
  238. (4 * MSSTC_CelCount + 1));
  239. /* Check if should start deceleration */
  240. if(dev->mode != MSSTC_MODE_SPEED_OPEN
  241. && dev->stepCountCurrent >= dev->decelStart){
  242. MSSTC_PulseToggle = 1;
  243. MSSTC_CelCount = -1 * (int32_t)dev->decelSteps;
  244. dev->runState = MSSTC_STATE_DECEL;
  245. }
  246. /* Check if reached maximum dev->fullSpeed */
  247. else if(MSSTC_NewStepDelay <= MSSTC_FastStepDelay){
  248. MSSTC_LastDelay = MSSTC_NewStepDelay;
  249. MSSTC_CelCount = -1 * (int32_t)dev->decelSteps;
  250. dev->runState = MSSTC_STATE_FULL;
  251. }
  252. break;
  253. }
  254. case MSSTC_STATE_FULL:
  255. {
  256. if(dev->mode == MSSTC_MODE_SPEED_OPEN){
  257. dev->stepCountCurrent = 0;
  258. }
  259. else{
  260. dev->stepCountCurrent++;
  261. }
  262. MSSTC_NewStepDelay = MSSTC_FastStepDelay;
  263. // MSSTC_LastDelay = MSSTC_NewStepDelay;
  264. /* Check if should start deceleration */
  265. if(dev->stepCountCurrent >= dev->decelStart){
  266. MSSTC_PulseToggle = 1;
  267. MSSTC_Rest = 0;
  268. MSSTC_CelCount = -1 * (int32_t)dev->decelSteps;
  269. MSSTC_NewStepDelay = MSSTC_LastDelay;
  270. dev->runState = MSSTC_STATE_DECEL;
  271. }
  272. break;
  273. }
  274. case MSSTC_STATE_DECEL:
  275. {
  276. dev->stepCountCurrent++;
  277. MSSTC_CelCount++;
  278. /* Check if this is the last step */
  279. if(MSSTC_CelCount >= 0){
  280. dev->runState = MSSTC_STATE_STOP;
  281. break;
  282. }
  283. if(MSSTC_StepDiff == 0){
  284. MSSTC_NewStepDelay = MSSTC_StepDelay -
  285. ((2 * MSSTC_StepDelay + MSSTC_Rest) /
  286. (4 * MSSTC_CelCount + 1));
  287. MSSTC_Rest = ((2 * MSSTC_StepDelay + MSSTC_Rest) %
  288. (4 * MSSTC_CelCount + 1));
  289. MSSTC_LastDelay = MSSTC_NewStepDelay;
  290. }
  291. break;
  292. }
  293. default:
  294. {
  295. if(dev->powerOnlyRun == 1){
  296. dev->enable = 0;
  297. dev->enSet(dev->enable);
  298. }
  299. break;
  300. }
  301. }
  302. if(MSSTC_NewStepDelay <= 1){
  303. MSSTC_NewStepDelay = 2;
  304. }
  305. MSSTC_StepDelay = MSSTC_NewStepDelay;
  306. }
  307. }
  308. /**
  309. * @brief Stop motor immediately but not clear params data
  310. * @param dev Pointer to motor device structure
  311. */
  312. void MSSTC_Scram(struct MOTOR_STEP_SPEED_T_C_ATY_Dev* dev){
  313. dev->runState = MSSTC_STATE_STANDBY;
  314. if(dev->powerOnlyRun == 1){
  315. dev->enable = 0;
  316. dev->enSet(dev->enable);
  317. }
  318. dev->stop();
  319. }
  320. void MSSTC_Stop(struct MOTOR_STEP_SPEED_T_C_ATY_Dev* dev){
  321. MSSTC_CelCount = -1 * (int32_t)dev->decelSteps;
  322. dev->runState = MSSTC_STATE_DECEL;
  323. dev->start();
  324. MSSTC_IRQHandler(dev);
  325. }
  326. void MSSTC_UpdateFullSpeed(struct MOTOR_STEP_SPEED_T_C_ATY_Dev* dev){
  327. float alpha = 2.0 * 3.14159265359 / (dev->fullSteps * dev->microstepping);
  328. MSSTC_FastStepDelay = (uint32_t)(alpha * dev->timerFreq * dev->unitDivision / dev->fullSpeed);
  329. dev->setTimerCompare(MSSTC_FastStepDelay);
  330. }
  331. /**
  332. * @brief Check if motor is running
  333. * @param dev Pointer to motor device structure
  334. * @return 1=running, 0=stopped
  335. */
  336. uint8_t MSSTC_IsRunning(struct MOTOR_STEP_SPEED_T_C_ATY_Dev* dev){
  337. return (dev->runState == MSSTC_STATE_STANDBY) ? 0 : 1;
  338. }
  339. /**
  340. * @brief MSSTC_Compensate
  341. *
  342. * @param dev
  343. */
  344. void MSSTC_Compensate(struct MOTOR_STEP_SPEED_T_C_ATY_Dev* dev){
  345. if(dev->runState != MSSTC_STATE_COMPENSATE){
  346. return;
  347. }
  348. uint32_t publicTurns = ((MSSTC_TargetTurn < MSSTC_CurrentTurn) ? MSSTC_TargetTurn : MSSTC_CurrentTurn);
  349. float targetAngleTotal = 360.0 * (MSSTC_TargetTurn - publicTurns) + MSSTC_TargetAngle;
  350. float currentAngleTotal = 360.0 * (MSSTC_CurrentTurn - publicTurns) + MSSTC_CurrentAngle;
  351. MSSTC_StepDiff = (int32_t)((targetAngleTotal - currentAngleTotal) / MSSTC_AnglePerStep);
  352. if(MSSTC_StepDiff > 1 || MSSTC_StepDiff < -1){
  353. if(MSSTC_StepDiff < 0){
  354. if(dev->turnBackAllowed == 1){
  355. MSSTC_CelCount = MSSTC_StepDiff;
  356. dev->dirSet(dev->direction ^ 0x01);
  357. }
  358. else{
  359. dev->runState = MSSTC_STATE_STANDBY;
  360. if(dev->powerOnlyRun == 1){
  361. dev->enable = 0;
  362. dev->enSet(dev->enable);
  363. }
  364. }
  365. }
  366. else{
  367. dev->dirSet(dev->direction);
  368. MSSTC_CelCount = -1 * MSSTC_StepDiff;
  369. }
  370. dev->runState = MSSTC_STATE_DECEL;
  371. MSSTC_CelCountErrCount++;
  372. if(MSSTC_CelCountErrCount > 5){
  373. dev->runState = MSSTC_STATE_STANDBY;
  374. if(dev->powerOnlyRun == 1){
  375. dev->enable = 0;
  376. dev->enSet(dev->enable);
  377. }
  378. }
  379. else{
  380. dev->start();
  381. }
  382. }
  383. else{
  384. dev->runState = MSSTC_STATE_STANDBY;
  385. if(dev->powerOnlyRun == 1){
  386. dev->enable = 0;
  387. dev->enSet(dev->enable);
  388. }
  389. }
  390. }
  391. /**
  392. * @brief Update current angle
  393. * @param dev Pointer to motor device structure
  394. * @param turn Turn count
  395. * @param angle Current angle
  396. */
  397. void MSSTC_UpdateAngle(struct MOTOR_STEP_SPEED_T_C_ATY_Dev* dev,
  398. uint32_t turn, float angle){
  399. MSSTC_CurrentTurn = turn;
  400. MSSTC_CurrentAngle = angle;
  401. MSSTC_Compensate(dev);
  402. }
  403. #endif /* __MOTOR_STEP_SPEED_T_C_ATY_C */
  404. /************************************ etc *************************************/
  405. /* init
  406. #define MS_1_TIM htim1
  407. #define MS_1_TIM_CHANNEL TIM_CHANNEL_1
  408. #define MS_1_TIM_IT_CC TIM_IT_CC1
  409. // MSSTC -----------------------------------------------------------------------
  410. #include "MOTOR_STEP_SPEED_T_C_ATY.h"
  411. uint8_t MSSTC_1_EnSet(uint8_t en){
  412. HAL_GPIO_WritePin(MS_EN_GPIO_Port, MS_EN_Pin,
  413. (en == 0) ? GPIO_PIN_SET : GPIO_PIN_RESET);
  414. return 0;
  415. }
  416. uint8_t MSSTC_1_DirSet(uint8_t dir){
  417. HAL_GPIO_WritePin(MS_DIR_GPIO_Port, MS_DIR_Pin,
  418. (dir == __ATY_PN_P) ? GPIO_PIN_SET : GPIO_PIN_RESET);
  419. return 0;
  420. }
  421. void MSSTC_1_SetTimerCompare(uint32_t value){
  422. __HAL_TIM_SET_COMPARE(&MS_1_TIM, MS_1_TIM_CHANNEL, value);
  423. __HAL_TIM_SET_COUNTER(&MS_1_TIM, 0);
  424. }
  425. void MSSTC_1_Start(void){
  426. HAL_TIM_OC_Start_IT(&MS_1_TIM, MS_1_TIM_CHANNEL);
  427. }
  428. void MSSTC_1_Stop(void){
  429. HAL_TIM_OC_Stop_IT(&MS_1_TIM, MS_1_TIM_CHANNEL);
  430. }
  431. struct MOTOR_STEP_SPEED_T_C_ATY_Dev MSSTC_Dev_1 = {
  432. .enSet = MSSTC_1_EnSet,
  433. .dirSet = MSSTC_1_DirSet,
  434. .start = MSSTC_1_Start,
  435. .stop = MSSTC_1_Stop,
  436. .setTimerCompare = MSSTC_1_SetTimerCompare,
  437. .timerFreq = 1000000,
  438. .fullSteps = 200,
  439. .microstepping = 8,
  440. .enable = 0,
  441. .direction = __ATY_PN_P,
  442. .fullSpeed = 20,
  443. .unitDivision = 1,
  444. .acceleration = 100,
  445. .deceleration = 200,
  446. .mode = 0,
  447. .angleLock = 0,
  448. .powerOnlyRun = 1,
  449. .runState = MSSTC_STATE_STANDBY,
  450. .lock = __ATY_UNLOCKED
  451. };
  452. void HAL_TIM_OC_DelayElapsedCallback(TIM_HandleTypeDef* htim){
  453. if(htim->Instance == TIM1 && htim->Channel == HAL_TIM_ACTIVE_CHANNEL_1){
  454. if(__HAL_TIM_GET_IT_SOURCE(&MS_1_TIM, MS_1_TIM_IT_CC) != RESET){
  455. __HAL_TIM_CLEAR_IT(&MS_1_TIM, MS_1_TIM_IT_CC);
  456. MSSTC_IRQHandler(&MSSTC_Dev_1);
  457. }
  458. }
  459. }
  460. void MSSTC_1_Init(void){
  461. HAL_GPIO_WritePin(MS_SP_GPIO_Port, MS_SP_Pin, GPIO_PIN_SET);
  462. HAL_GPIO_WritePin(MS_MS1_GPIO_Port, MS_MS1_Pin, GPIO_PIN_RESET);
  463. HAL_GPIO_WritePin(MS_MS2_GPIO_Port, MS_MS2_Pin, GPIO_PIN_RESET);
  464. HAL_GPIO_WritePin(MS_EN_GPIO_Port, MS_EN_Pin, GPIO_PIN_SET);
  465. RGF_MOTOR_FREQUENCY = (1600.0 / 60.0);
  466. RGF_MOTOR_DUTY_CYCLE = 50.0;
  467. RGF_MOTOR_FULL_STEPS = MSSTC_Dev_1.fullSteps;
  468. RGF_MOTOR_MICROSTEPPING = MSSTC_Dev_1.microstepping;
  469. RGF_MOTOR_ENABLE = MSSTC_Dev_1.enable;
  470. RGF_MOTOR_DIRECTION = MSSTC_Dev_1.direction;
  471. RGF_MOTOR_SPEED = MSSTC_Dev_1.fullSpeed;
  472. RGF_MOTOR_UNIT_DIVISION = MSSTC_Dev_1.unitDivision;
  473. RGF_MOTOR_ACCELERATION = MSSTC_Dev_1.acceleration;
  474. RGF_MOTOR_DECELERATION = MSSTC_Dev_1.deceleration;
  475. RGF_MOTOR_MODE = MSSTC_Dev_1.mode;
  476. RGF_MOTOR_ANGLE_LOCK_TARGET = MSSTC_Dev_1.angleLock;
  477. RGF_MOTOR_AUTO_POWER = MSSTC_Dev_1.powerOnlyRun;
  478. RGF_MOTOR_RUN_STATE = MSSTC_Dev_1.runState;
  479. RGF_MOTOR_RUN = 1;
  480. RGF_MOTOR_STEP_COUNT = 1600;
  481. }
  482. void MSSTC_1_Cycle(void){
  483. if(MSSTC_IsRunning(&MSSTC_Dev_1) != 1){
  484. MSSTC_Dev_1.fullSteps = (uint16_t)RGF_MOTOR_FULL_STEPS;
  485. if(MSSTC_Dev_1.microstepping != (uint16_t)RGF_MOTOR_MICROSTEPPING){
  486. MSSTC_Dev_1.microstepping = (uint16_t)RGF_MOTOR_MICROSTEPPING;
  487. MSSTC_Dev_1.enSet(0);
  488. if(MSSTC_Dev_1.microstepping == 8){
  489. HAL_GPIO_WritePin(MS_MS1_GPIO_Port, MS_MS1_Pin, GPIO_PIN_RESET);
  490. HAL_GPIO_WritePin(MS_MS2_GPIO_Port, MS_MS2_Pin, GPIO_PIN_RESET);
  491. }
  492. else if(MSSTC_Dev_1.microstepping == 16){
  493. HAL_GPIO_WritePin(MS_MS1_GPIO_Port, MS_MS1_Pin, GPIO_PIN_SET);
  494. HAL_GPIO_WritePin(MS_MS2_GPIO_Port, MS_MS2_Pin, GPIO_PIN_SET);
  495. }
  496. else if(MSSTC_Dev_1.microstepping == 32){
  497. HAL_GPIO_WritePin(MS_MS1_GPIO_Port, MS_MS1_Pin, GPIO_PIN_RESET);
  498. HAL_GPIO_WritePin(MS_MS2_GPIO_Port, MS_MS2_Pin, GPIO_PIN_SET);
  499. }
  500. else if(MSSTC_Dev_1.microstepping == 64){
  501. HAL_GPIO_WritePin(MS_MS1_GPIO_Port, MS_MS1_Pin, GPIO_PIN_SET);
  502. HAL_GPIO_WritePin(MS_MS2_GPIO_Port, MS_MS2_Pin, GPIO_PIN_RESET);
  503. }
  504. if(MSSTC_Dev_1.powerOnlyRun == 0){
  505. MSSTC_Dev_1.enSet(MSSTC_Dev_1.enable);
  506. }
  507. }
  508. if(MSSTC_Dev_1.enable != (uint8_t)RGF_MOTOR_ENABLE){
  509. MSSTC_Dev_1.enable = (uint8_t)RGF_MOTOR_ENABLE;
  510. if(MSSTC_Dev_1.powerOnlyRun == 0){
  511. MSSTC_Dev_1.enSet(MSSTC_Dev_1.enable);
  512. }
  513. }
  514. if(MSSTC_Dev_1.direction != (uint8_t)RGF_MOTOR_DIRECTION){
  515. MSSTC_Dev_1.direction = (uint8_t)RGF_MOTOR_DIRECTION;
  516. MSSTC_Dev_1.dirSet(MSSTC_Dev_1.direction);
  517. }
  518. MSSTC_Dev_1.fullSpeed = (uint16_t)RGF_MOTOR_SPEED;
  519. MSSTC_Dev_1.unitDivision = (uint8_t)RGF_MOTOR_UNIT_DIVISION;
  520. MSSTC_Dev_1.acceleration = (uint16_t)RGF_MOTOR_ACCELERATION;
  521. MSSTC_Dev_1.deceleration = (uint16_t)RGF_MOTOR_DECELERATION;
  522. MSSTC_Dev_1.mode = (uint8_t)RGF_MOTOR_MODE;
  523. if(MSSTC_Dev_1.angleLock != (uint8_t)RGF_MOTOR_ANGLE_LOCK_TARGET){
  524. MSSTC_Dev_1.angleLock = (uint8_t)RGF_MOTOR_ANGLE_LOCK_TARGET;
  525. if(MSSTC_Dev_1.mode == 1){
  526. }
  527. }
  528. if(MSSTC_Dev_1.powerOnlyRun != (uint8_t)RGF_MOTOR_AUTO_POWER){
  529. MSSTC_Dev_1.powerOnlyRun = (uint8_t)RGF_MOTOR_AUTO_POWER;
  530. if(MSSTC_Dev_1.powerOnlyRun == 0){
  531. MSSTC_Dev_1.enSet(MSSTC_Dev_1.enable);
  532. }
  533. else{
  534. MSSTC_Dev_1.enSet(0);
  535. }
  536. }
  537. }
  538. MT6816_1_UpdateCycle();
  539. MSSTC_UpdateAngle(&MSSTC_Dev_1,
  540. MT6816_ATY_Dev_1.turnCount,
  541. MT6816_ATY_Dev_1.angleTotal + MT6816_ATY_Dev_1.angleStart);
  542. if((uint8_t)RGF_MOTOR_RUN == 1){
  543. MT6816_TurnCountOffsetStart(&MT6816_ATY_Dev_1, MSSTC_Dev_1.direction);
  544. MSSTC_Move(&MSSTC_Dev_1, RGF_MOTOR_STEP_COUNT, MT6816_ATY_Dev_1.angleStart);
  545. }
  546. else if((uint8_t)RGF_MOTOR_RUN == 2){
  547. MSSTC_Scram(&MSSTC_Dev_1);
  548. }
  549. RGF_MOTOR_RUN = 0;
  550. if(MSSTC_Dev_1.runState == MSSTC_STATE_STANDBY
  551. && MT6816_ATY_Dev_1.start == 1){
  552. MT6816_TurnCountOffsetStop(&MT6816_ATY_Dev_1);
  553. }
  554. RGF_MOTOR_ENABLE = MSSTC_Dev_1.enable;
  555. RGF_MOTOR_DIRECTION = MSSTC_Dev_1.direction;
  556. RGF_MOTOR_RUN_STATE = MSSTC_Dev_1.runState;
  557. RGF_MOTOR_CURRENT_STEPS = MSSTC_Dev_1.stepCountCurrent;
  558. RGF_MOTOR_ACCEL_STEPS = MSSTC_Dev_1.accelSteps;
  559. RGF_MOTOR_ACCEL_LIMIT = MSSTC_Dev_1.accelLimit;
  560. RGF_MOTOR_DECEL_STEPS = MSSTC_Dev_1.decelSteps;
  561. RGF_MOTOR_DECEL_START = MSSTC_Dev_1.decelStart;
  562. }
  563. */
  564. /* use
  565. MSSTC_1_Init();
  566. MSSTC_1_Cycle();
  567. // Reg -------------------------------------------------------------------------
  568. // motor fan
  569. #define RGF_MOTOR_FAN_EN rgf[20]
  570. #define RGF_MOTOR_FAN_BASE rgf[21]
  571. #define RGF_MOTOR_FAN_LOW_T rgf[22]
  572. #define RGF_MOTOR_FAN_HIGH_T rgf[23]
  573. #define RGF_MOTOR_FAN_T_SPACE rgf[24]
  574. // motor angle base
  575. #define RGF_MOTOR_ANGLE_SET_ZERO rgf[25]
  576. #define RGF_MOTOR_ANGLE_ZERO rgf[26]
  577. #define RGF_MOTOR_ANGLE rgf[27]
  578. #define RGF_MOTOR_ANGLE_ERR rgf[28]
  579. // motor angle correction
  580. #define RGF_MOTOR_ANGLE_ANGLE_START rgf[30]
  581. #define RGF_MOTOR_ANGLE_TURN_COUNT rgf[31]
  582. #define RGF_MOTOR_ANGLE_ANGLE_TOTAL rgf[32]
  583. #define RGF_MOTOR_ANGLE_START rgf[33]
  584. // useless motor params
  585. #define RGF_MOTOR_FREQUENCY rgf[35]
  586. #define RGF_MOTOR_DUTY_CYCLE rgf[36]
  587. #define RGF_MOTOR_FULL_STEPS rgf[38]
  588. #define RGF_MOTOR_MICROSTEPPING rgf[39]
  589. // motor base
  590. #define RGF_MOTOR_ENABLE rgf[40]
  591. #define RGF_MOTOR_DIRECTION rgf[41]
  592. #define RGF_MOTOR_SPEED rgf[42]
  593. #define RGF_MOTOR_UNIT_DIVISION rgf[43]
  594. #define RGF_MOTOR_ACCELERATION rgf[44]
  595. #define RGF_MOTOR_DECELERATION rgf[45]
  596. #define RGF_MOTOR_MODE rgf[46]
  597. #define RGF_MOTOR_ANGLE_LOCK_TARGET rgf[47]
  598. #define RGF_MOTOR_AUTO_POWER rgf[48]
  599. #define RGF_MOTOR_RUN_STATE rgf[49]
  600. #define RGF_MOTOR_RUN rgf[50]
  601. #define RGF_MOTOR_STEP_COUNT rgf[51]
  602. #define RGF_MOTOR_CURRENT_STEPS rgf[52]
  603. #define RGF_MOTOR_ACCEL_STEPS rgf[53]
  604. #define RGF_MOTOR_ACCEL_LIMIT rgf[54]
  605. #define RGF_MOTOR_DECEL_STEPS rgf[55]
  606. #define RGF_MOTOR_DECEL_START rgf[56]
  607. // FAN -------------------------------------------------------------------------
  608. uint8_t fanLastLevel = 0;
  609. void FAN_ControlCycle(void){
  610. if((uint8_t)RGF_MOTOR_FAN_EN == 1 && RGF_BOARD_TEMP > RGF_MOTOR_FAN_LOW_T){
  611. if((uint8_t)((RGF_BOARD_TEMP - RGF_MOTOR_FAN_LOW_T) / RGF_MOTOR_FAN_T_SPACE) + 1 != fanLastLevel){
  612. fanLastLevel = (uint8_t)((RGF_BOARD_TEMP - RGF_MOTOR_FAN_LOW_T) / RGF_MOTOR_FAN_T_SPACE) + 1;
  613. HAL_TIM_PWM_Stop(&htim3, TIM_CHANNEL_1);
  614. __HAL_TIM_SET_COMPARE(&htim3, TIM_CHANNEL_1,
  615. RGF_MOTOR_FAN_BASE + ((1000 - RGF_MOTOR_FAN_BASE) * fanLastLevel
  616. / ((RGF_MOTOR_FAN_HIGH_T - RGF_MOTOR_FAN_LOW_T) / RGF_MOTOR_FAN_T_SPACE)));
  617. HAL_TIM_PWM_Start(&htim3, TIM_CHANNEL_1);
  618. }
  619. }
  620. else{
  621. __HAL_TIM_SET_COMPARE(&htim3, TIM_CHANNEL_1, 0);
  622. HAL_TIM_PWM_Stop(&htim3, TIM_CHANNEL_1);
  623. fanLastLevel = 0;
  624. }
  625. }
  626. void FAN_Init(void){
  627. RGF_MOTOR_FAN_EN = 1;
  628. RGF_MOTOR_FAN_BASE = 200;
  629. RGF_MOTOR_FAN_LOW_T = 50;
  630. RGF_MOTOR_FAN_HIGH_T = 80;
  631. RGF_MOTOR_FAN_T_SPACE = 5;
  632. }
  633. */
  634. /******************************************************************************/
  635. /******************************** End Of File *********************************/