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.

390 lines
12 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
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. CHECK_ARGC(2);
  145. int motorid = atoi(argv[1]);
  146. int pos = atoi(argv[2]);
  147. if (motorid < 1 || motorid > 6) {
  148. ZLOGE(TAG, "motorid out of range");
  149. return;
  150. }
  151. g_step_motor45[motorid].moveTo(pos);
  152. });
  153. cmdScheduler.registerCmd("stepmotor45_moveBy", [](int argc, char** argv, CmdScheduler::CmdProcessContext* context) {
  154. CHECK_ARGC(2);
  155. int motorid = atoi(argv[1]);
  156. int pos = atoi(argv[2]);
  157. if (motorid < 1 || motorid > 6) {
  158. ZLOGE(TAG, "motorid out of range");
  159. return;
  160. }
  161. g_step_motor45[motorid].moveBy(pos);
  162. });
  163. cmdScheduler.registerCmd("stepmotor45_stop", [](int argc, char** argv, CmdScheduler::CmdProcessContext* context) {
  164. CHECK_ARGC(1);
  165. int motorid = atoi(argv[1]);
  166. if (motorid < 1 || motorid > 6) {
  167. ZLOGE(TAG, "motorid out of range");
  168. return;
  169. }
  170. g_step_motor45[motorid].stop();
  171. });
  172. cmdScheduler.registerCmd("stepmotor45_setSpeed", [](int argc, char** argv, CmdScheduler::CmdProcessContext* context) {
  173. CHECK_ARGC(1);
  174. int motorid = atoi(argv[1]);
  175. int speed = atoi(argv[2]);
  176. if (motorid < 1 || motorid > 6) {
  177. ZLOGE(TAG, "motorid out of range");
  178. return;
  179. }
  180. g_step_motor45[motorid].setDefaultSpeed(speed);
  181. });
  182. /*******************************************************************************
  183. * *
  184. *******************************************************************************/
  185. #define GET_MOTOR(motor) \
  186. { \
  187. int motorid = atoi(argv[1]); \
  188. motor = &g_step_motor[motorid]; \
  189. if (motorid >= 6 || motorid < 1) { \
  190. ZLOGE(TAG, "motor %d not found", motorid); \
  191. context->breakflag = true; \
  192. return; \
  193. } \
  194. }
  195. /**
  196. * @brief STEPMOTOR
  197. */
  198. cmdScheduler.registerCmd("step_motor_setvelocity", [](int argc, char** argv, CmdScheduler::CmdProcessContext* context) {
  199. CHECK_ARGC(2);
  200. StepMotor* motor = NULL;
  201. GET_MOTOR(motor);
  202. motor->setVelocity(atoi(argv[2]));
  203. });
  204. cmdScheduler.registerCmd("step_motor_set_acc", [](int argc, char** argv, CmdScheduler::CmdProcessContext* context) {
  205. CHECK_ARGC(2);
  206. StepMotor* motor = NULL;
  207. GET_MOTOR(motor);
  208. motor->setAcc(atoi(argv[2]));
  209. });
  210. cmdScheduler.registerCmd("step_motor_set_dec", [](int argc, char** argv, CmdScheduler::CmdProcessContext* context) {
  211. CHECK_ARGC(2);
  212. StepMotor* motor = NULL;
  213. GET_MOTOR(motor);
  214. motor->setDec(atoi(argv[2]));
  215. });
  216. cmdScheduler.registerCmd("step_motor_moveto", [](int argc, char** argv, CmdScheduler::CmdProcessContext* context) {
  217. CHECK_ARGC(2);
  218. StepMotor* motor = NULL;
  219. GET_MOTOR(motor);
  220. motor->moveTo(atoi(argv[2]));
  221. });
  222. cmdScheduler.registerCmd("step_motor_moveby", [](int argc, char** argv, CmdScheduler::CmdProcessContext* context) {
  223. CHECK_ARGC(2);
  224. StepMotor* motor = NULL;
  225. GET_MOTOR(motor);
  226. motor->moveBy(atoi(argv[2]));
  227. });
  228. cmdScheduler.registerCmd("step_motor_rotate", [](int argc, char** argv, CmdScheduler::CmdProcessContext* context) {
  229. CHECK_ARGC(2);
  230. StepMotor* motor = NULL;
  231. GET_MOTOR(motor);
  232. motor->rotate(atoi(argv[2]));
  233. });
  234. cmdScheduler.registerCmd("step_motor_movetozero", [](int argc, char** argv, CmdScheduler::CmdProcessContext* context) {
  235. CHECK_ARGC(1);
  236. StepMotor* motor = NULL;
  237. GET_MOTOR(motor);
  238. motor->moveToZero();
  239. });
  240. cmdScheduler.registerCmd("step_motor_wait_for_idle", [](int argc, char** argv, CmdScheduler::CmdProcessContext* context) {
  241. CHECK_ARGC(1);
  242. StepMotor* motor = NULL;
  243. GET_MOTOR(motor);
  244. HAL_Delay(100);
  245. while (!motor->isIdleState()) {
  246. HAL_Delay(300);
  247. ZLOGI(TAG, "step_motor_wait_for_idle %d", atoi(argv[1]));
  248. }
  249. });
  250. cmdScheduler.registerCmd("step_motor_clear_exception", [](int argc, char** argv, CmdScheduler::CmdProcessContext* context) {
  251. CHECK_ARGC(1);
  252. StepMotor* motor = NULL;
  253. GET_MOTOR(motor);
  254. motor->clearException();
  255. });
  256. cmdScheduler.registerCmd("step_motor_moveto_mm", [](int argc, char** argv, CmdScheduler::CmdProcessContext* context) {
  257. CHECK_ARGC(2);
  258. StepMotor* motor = NULL;
  259. GET_MOTOR(motor);
  260. int32_t step;
  261. if (!distance_mm_to_step(atoi(argv[1]), atof(argv[2]), &step)) {
  262. ZLOGE(TAG, "step_motor_moveto_mm %d %f failed", atoi(argv[1]), atof(argv[2]));
  263. context->breakflag = true;
  264. return;
  265. }
  266. motor->moveTo(step);
  267. });
  268. cmdScheduler.registerCmd("sleep_ms", [](int argc, char** argv, CmdScheduler::CmdProcessContext* context) {
  269. CHECK_ARGC(1);
  270. HAL_Delay(atoi(argv[1]));
  271. });
  272. }
  273. void Main::run() {
  274. /*******************************************************************************
  275. * ϵͳʼ *
  276. *******************************************************************************/
  277. chip_init(&chipcfg);
  278. zos_cfg_t zoscfg;
  279. zos_init(&zoscfg);
  280. auto config = m_IflytopCanMaster.createDefaultConfig(1);
  281. m_IflytopCanMaster.initialize(config);
  282. int i = 1;
  283. g_step_motor[i++].initialize(11, 10000, &m_IflytopCanMaster);
  284. g_step_motor[i++].initialize(12, 10000, &m_IflytopCanMaster);
  285. g_step_motor[i++].initialize(13, 10000, &m_IflytopCanMaster);
  286. g_step_motor[i++].initialize(14, 10000, &m_IflytopCanMaster);
  287. g_step_motor[i++].initialize(15, 10000, &m_IflytopCanMaster);
  288. g_step_motor[i++].initialize(16, 10000, &m_IflytopCanMaster);
  289. // g_step_motor45[0].initialize(cfg1);
  290. g_step_motor45[1].initialize(cfg1);
  291. g_step_motor45[2].initialize(cfg2);
  292. g_step_motor45[3].initialize(cfg3);
  293. g_step_motor45[4].initialize(cfg4);
  294. g_step_motor45[5].initialize(cfg5);
  295. g_step_motor45[6].initialize(cfg6);
  296. StepMotor45Scheduler step_motor45_scheduler;
  297. step_motor45_scheduler.initialize(&htim10, 1000);
  298. step_motor45_scheduler.addMotor(&g_step_motor45[1]);
  299. step_motor45_scheduler.addMotor(&g_step_motor45[2]);
  300. step_motor45_scheduler.addMotor(&g_step_motor45[3]);
  301. step_motor45_scheduler.addMotor(&g_step_motor45[4]);
  302. step_motor45_scheduler.addMotor(&g_step_motor45[5]);
  303. step_motor45_scheduler.addMotor(&g_step_motor45[6]);
  304. // g_step_motor45_1.rotate(true, 1000);
  305. step_motor45_scheduler.start();
  306. cmdScheduler.initialize(&DEBUG_UART, 1000);
  307. regfn();
  308. while (true) {
  309. OSDefaultSchduler::getInstance()->loop();
  310. cmdScheduler.schedule();
  311. m_IflytopCanMaster.periodicJob();
  312. // m_IflytopCanMaster.writeReg(1,1,1,10);
  313. osDelay(1);
  314. }
  315. }