You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

375 lines
11 KiB

2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
  1. #include "main.hpp"
  2. #include <stddef.h>
  3. #include <stdio.h>
  4. //
  5. #include "sdk/os/zos.hpp"
  6. #include "sdk\components\cmdscheduler\cmd_scheduler.hpp"
  7. #include "sdk\components\iflytop_can_slave_module_master_end\stepmotor.hpp"
  8. #include "sdk\components\iflytop_can_slave_v1\iflytop_can_master.hpp"
  9. #include "sdk\components\step_motor_45\step_motor_45.hpp"
  10. #include "sdk\components\step_motor_45\step_motor_45_scheduler.hpp"
  11. #define TAG "main"
  12. namespace iflytop {
  13. Main gmain;
  14. };
  15. using namespace iflytop;
  16. using namespace std;
  17. #define CHECK_ARGC(n) \
  18. if (argc != (n + 1)) { \
  19. ZLOGE(TAG, "argc != %d", n); \
  20. context->breakflag = true; \
  21. return; \
  22. }
  23. extern "C" {
  24. void StartDefaultTask(void const* argument) { umain(); }
  25. }
  26. /*******************************************************************************
  27. * *
  28. *******************************************************************************/
  29. static chip_cfg_t chipcfg = {
  30. .us_dleay_tim = &DELAY_US_TIMER,
  31. .tim_irq_scheduler_tim = &TIM_IRQ_SCHEDULER_TIMER,
  32. .huart = &DEBUG_UART,
  33. .debuglight = DEBUG_LIGHT_GPIO,
  34. };
  35. static StepMotor45::cfg_t cfg1 = {
  36. .max_pos = -1,
  37. .enable_zero_limit = false,
  38. .enable_max_pos_limit = false,
  39. .mirror = false,
  40. .zeroPin = PinNull,
  41. .zeroPinMirror = false,
  42. .driverPin = {PB15, PD11, PD12, PD13},
  43. .driverPinMirror = false,
  44. };
  45. static StepMotor45::cfg_t cfg2 = {
  46. .max_pos = -1,
  47. .enable_zero_limit = false,
  48. .enable_max_pos_limit = false,
  49. .mirror = false,
  50. .zeroPin = PinNull,
  51. .zeroPinMirror = false,
  52. .driverPin = {PG2, PG3, PG4, PG5},
  53. .driverPinMirror = false,
  54. };
  55. static StepMotor45::cfg_t cfg3 = {
  56. .max_pos = -1,
  57. .enable_zero_limit = false,
  58. .enable_max_pos_limit = false,
  59. .mirror = false,
  60. .zeroPin = PinNull,
  61. .zeroPinMirror = false,
  62. .driverPin = {PG6, PG7, PG8, PC6},
  63. .driverPinMirror = false,
  64. };
  65. static StepMotor45::cfg_t cfg4 = {
  66. .max_pos = -1,
  67. .enable_zero_limit = false,
  68. .enable_max_pos_limit = false,
  69. .mirror = false,
  70. .zeroPin = PinNull,
  71. .zeroPinMirror = false,
  72. .driverPin = {PC7, PC8, PC9, PA8},
  73. .driverPinMirror = false,
  74. };
  75. static StepMotor45::cfg_t cfg5 = {
  76. .max_pos = -1,
  77. .enable_zero_limit = false,
  78. .enable_max_pos_limit = false,
  79. .mirror = false,
  80. .zeroPin = PinNull,
  81. .zeroPinMirror = false,
  82. .driverPin = {PA13, PA14, PA15, PC10},
  83. .driverPinMirror = false,
  84. };
  85. static StepMotor45::cfg_t cfg6 = {
  86. .max_pos = -1,
  87. .enable_zero_limit = false,
  88. .enable_max_pos_limit = false,
  89. .mirror = false,
  90. .zeroPin = PinNull,
  91. .zeroPinMirror = false,
  92. .driverPin = {PC12, PD3, PD5, PD7},
  93. .driverPinMirror = false,
  94. };
  95. map<int, float> screw_lead = {
  96. {1, 10.0}, //
  97. {2, 10.0}, //
  98. {3, 10.0}, //
  99. {4, 10.0}, //
  100. {5, 10.0}, //
  101. {6, 10.0}, //
  102. };
  103. /*******************************************************************************
  104. * *
  105. *******************************************************************************/
  106. static StepMotor45 g_step_motor45[7];
  107. StepMotor g_step_motor[10];
  108. IflytopCanMaster m_IflytopCanMaster;
  109. static CmdScheduler cmdScheduler;
  110. bool distance_mm_to_step(int motorid, float distance_mm, int32_t* distance) {
  111. if (screw_lead.find(motorid) == screw_lead.end()) {
  112. return false;
  113. }
  114. float lead = screw_lead[motorid];
  115. *distance = distance_mm / lead * 51200;
  116. return true;
  117. }
  118. void regfn() {
  119. cmdScheduler.registerCmd("help", //
  120. [](int argc, char** argv, CmdScheduler::CmdProcessContext* context) { ZLOGI(TAG, "do_help"); });
  121. // stepmotor45
  122. cmdScheduler.registerCmd("stepmotor45_rotate", [](int argc, char** argv, CmdScheduler::CmdProcessContext* context) {
  123. // stepmotor45_rotate motorid direction
  124. CHECK_ARGC(2);
  125. int motorid = atoi(argv[1]);
  126. bool direction = atoi(argv[2]);
  127. if (motorid < 1 || motorid > 6) {
  128. ZLOGE(TAG, "motorid out of range");
  129. return;
  130. }
  131. g_step_motor45[motorid].rotate(direction);
  132. });
  133. cmdScheduler.registerCmd("stepmotor45_readPos", [](int argc, char** argv, CmdScheduler::CmdProcessContext* context) {
  134. // stepmotor45_rotate motorid direction
  135. CHECK_ARGC(2);
  136. int motorid = atoi(argv[1]);
  137. if (motorid < 1 || motorid > 6) {
  138. ZLOGE(TAG, "motorid out of range");
  139. return;
  140. }
  141. ZLOGI(TAG, "motorid %d pos %d", motorid, g_step_motor45[motorid].getPos());
  142. });
  143. cmdScheduler.registerCmd("stepmotor45_moveTo", [](int argc, char** argv, CmdScheduler::CmdProcessContext* context) {
  144. // stepmotor45_rotate motorid pos
  145. CHECK_ARGC(2);
  146. int motorid = atoi(argv[1]);
  147. int pos = atoi(argv[2]);
  148. if (motorid < 1 || motorid > 6) {
  149. ZLOGE(TAG, "motorid out of range");
  150. return;
  151. }
  152. g_step_motor45[motorid].moveTo(pos);
  153. });
  154. cmdScheduler.registerCmd("stepmotor45_moveBy", [](int argc, char** argv, CmdScheduler::CmdProcessContext* context) {
  155. // stepmotor45_rotate motorid pos
  156. CHECK_ARGC(2);
  157. int motorid = atoi(argv[1]);
  158. int pos = atoi(argv[2]);
  159. if (motorid < 1 || motorid > 6) {
  160. ZLOGE(TAG, "motorid out of range");
  161. return;
  162. }
  163. g_step_motor45[motorid].moveBy(pos);
  164. });
  165. cmdScheduler.registerCmd("stepmotor45_stop", [](int argc, char** argv, CmdScheduler::CmdProcessContext* context) {
  166. // stepmotor45_rotate motorid pos
  167. CHECK_ARGC(1);
  168. int motorid = atoi(argv[1]);
  169. if (motorid < 1 || motorid > 6) {
  170. ZLOGE(TAG, "motorid out of range");
  171. return;
  172. }
  173. g_step_motor45[motorid].stop();
  174. });
  175. /*******************************************************************************
  176. * *
  177. *******************************************************************************/
  178. #define GET_MOTOR(motor) \
  179. { \
  180. int motorid = atoi(argv[1]); \
  181. motor = &g_step_motor[motorid]; \
  182. if (motorid >= 6 || motorid < 1) { \
  183. ZLOGE(TAG, "motor %d not found", motorid); \
  184. context->breakflag = true; \
  185. return; \
  186. } \
  187. }
  188. /**
  189. * @brief STEPMOTOR
  190. */
  191. cmdScheduler.registerCmd("step_motor_setvelocity", [](int argc, char** argv, CmdScheduler::CmdProcessContext* context) {
  192. CHECK_ARGC(2);
  193. StepMotor* motor = NULL;
  194. GET_MOTOR(motor);
  195. motor->setVelocity(atoi(argv[2]));
  196. });
  197. cmdScheduler.registerCmd("step_motor_set_acc", [](int argc, char** argv, CmdScheduler::CmdProcessContext* context) {
  198. CHECK_ARGC(2);
  199. StepMotor* motor = NULL;
  200. GET_MOTOR(motor);
  201. motor->setAcc(atoi(argv[2]));
  202. });
  203. cmdScheduler.registerCmd("step_motor_set_dec", [](int argc, char** argv, CmdScheduler::CmdProcessContext* context) {
  204. CHECK_ARGC(2);
  205. StepMotor* motor = NULL;
  206. GET_MOTOR(motor);
  207. motor->setDec(atoi(argv[2]));
  208. });
  209. cmdScheduler.registerCmd("step_motor_moveto", [](int argc, char** argv, CmdScheduler::CmdProcessContext* context) {
  210. CHECK_ARGC(2);
  211. StepMotor* motor = NULL;
  212. GET_MOTOR(motor);
  213. motor->moveTo(atoi(argv[2]));
  214. });
  215. cmdScheduler.registerCmd("step_motor_moveby", [](int argc, char** argv, CmdScheduler::CmdProcessContext* context) {
  216. CHECK_ARGC(2);
  217. StepMotor* motor = NULL;
  218. GET_MOTOR(motor);
  219. motor->moveBy(atoi(argv[2]));
  220. });
  221. cmdScheduler.registerCmd("step_motor_rotate", [](int argc, char** argv, CmdScheduler::CmdProcessContext* context) {
  222. CHECK_ARGC(2);
  223. StepMotor* motor = NULL;
  224. GET_MOTOR(motor);
  225. motor->rotate(atoi(argv[2]));
  226. });
  227. cmdScheduler.registerCmd("step_motor_movetozero", [](int argc, char** argv, CmdScheduler::CmdProcessContext* context) {
  228. CHECK_ARGC(1);
  229. StepMotor* motor = NULL;
  230. GET_MOTOR(motor);
  231. motor->moveToZero();
  232. });
  233. cmdScheduler.registerCmd("step_motor_wait_for_idle", [](int argc, char** argv, CmdScheduler::CmdProcessContext* context) {
  234. CHECK_ARGC(1);
  235. StepMotor* motor = NULL;
  236. GET_MOTOR(motor);
  237. HAL_Delay(100);
  238. while (!motor->isIdleState()) {
  239. HAL_Delay(300);
  240. ZLOGI(TAG, "step_motor_wait_for_idle %d", atoi(argv[1]));
  241. }
  242. });
  243. cmdScheduler.registerCmd("step_motor_clear_exception", [](int argc, char** argv, CmdScheduler::CmdProcessContext* context) {
  244. CHECK_ARGC(1);
  245. StepMotor* motor = NULL;
  246. GET_MOTOR(motor);
  247. motor->clearException();
  248. });
  249. cmdScheduler.registerCmd("step_motor_moveto_mm", [](int argc, char** argv, CmdScheduler::CmdProcessContext* context) {
  250. CHECK_ARGC(2);
  251. StepMotor* motor = NULL;
  252. GET_MOTOR(motor);
  253. int32_t step;
  254. if (!distance_mm_to_step(atoi(argv[1]), atof(argv[2]), &step)) {
  255. ZLOGE(TAG, "step_motor_moveto_mm %d %f failed", atoi(argv[1]), atof(argv[2]));
  256. context->breakflag = true;
  257. return;
  258. }
  259. motor->moveTo(step);
  260. });
  261. }
  262. void Main::run() {
  263. /*******************************************************************************
  264. * ϵͳʼ *
  265. *******************************************************************************/
  266. chip_init(&chipcfg);
  267. zos_cfg_t zoscfg;
  268. zos_init(&zoscfg);
  269. auto config = m_IflytopCanMaster.createDefaultConfig(1);
  270. m_IflytopCanMaster.initialize(config);
  271. int i = 1;
  272. g_step_motor[i++].initialize(11, 10000, &m_IflytopCanMaster);
  273. g_step_motor[i++].initialize(12, 10000, &m_IflytopCanMaster);
  274. g_step_motor[i++].initialize(13, 10000, &m_IflytopCanMaster);
  275. g_step_motor[i++].initialize(14, 10000, &m_IflytopCanMaster);
  276. g_step_motor[i++].initialize(15, 10000, &m_IflytopCanMaster);
  277. g_step_motor[i++].initialize(16, 10000, &m_IflytopCanMaster);
  278. // g_step_motor45[0].initialize(cfg1);
  279. g_step_motor45[1].initialize(cfg1);
  280. g_step_motor45[2].initialize(cfg2);
  281. g_step_motor45[3].initialize(cfg3);
  282. g_step_motor45[4].initialize(cfg4);
  283. g_step_motor45[5].initialize(cfg5);
  284. g_step_motor45[6].initialize(cfg6);
  285. StepMotor45Scheduler step_motor45_scheduler;
  286. step_motor45_scheduler.initialize(&htim10, 1000);
  287. step_motor45_scheduler.addMotor(&g_step_motor45[1]);
  288. step_motor45_scheduler.addMotor(&g_step_motor45[2]);
  289. step_motor45_scheduler.addMotor(&g_step_motor45[3]);
  290. step_motor45_scheduler.addMotor(&g_step_motor45[4]);
  291. step_motor45_scheduler.addMotor(&g_step_motor45[5]);
  292. step_motor45_scheduler.addMotor(&g_step_motor45[6]);
  293. // g_step_motor45_1.rotate(true, 1000);
  294. step_motor45_scheduler.start();
  295. cmdScheduler.initialize(&DEBUG_UART, 1000);
  296. regfn();
  297. while (true) {
  298. OSDefaultSchduler::getInstance()->loop();
  299. cmdScheduler.schedule();
  300. m_IflytopCanMaster.periodicJob();
  301. osDelay(1);
  302. }
  303. }