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.

176 lines
7.4 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
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. #include "main.h"
  5. #include "project.hpp"
  6. //
  7. #include "sdk/hal/zhal.hpp"
  8. #include "sdk\components\iflytop_can_slave_v1\iflytop_can_slave.hpp"
  9. #include "sdk\components\tmc\ic\ztmc4361A.hpp"
  10. #define TAG "main"
  11. namespace iflytop {
  12. Main gmain;
  13. };
  14. using namespace iflytop;
  15. IflytopCanProtocolStackProcesser m_protocolStack;
  16. TMC4361A m_motora;
  17. TMC4361A m_motorb;
  18. ZGPIO arm_sensor1_gpio;
  19. ZGPIO arm_sensor2_gpio;
  20. ZGPIO arm_sensor3_gpio;
  21. ZGPIO arm_sensor4_gpio;
  22. ZGPIO arm_sensor5_gpio;
  23. ZGPIO arm_sensor6_gpio;
  24. ZGPIO arm_sensor7_gpio;
  25. ZGPIO arm_sensor8_gpio;
  26. ZGPIO debuglight;
  27. ZGPIO tmc_motor1_spi_select1_io;
  28. ZGPIO tmc_motor1_nfreeze_io;
  29. ZGPIO tmc_motor1_nreset_io;
  30. ZGPIO tmc_motor1_sub_ic_enn_io;
  31. ZGPIO tmc_motor2_spi_select1_io;
  32. ZGPIO tmc_motor2_nfreeze_io;
  33. ZGPIO tmc_motor2_nreset_io;
  34. ZGPIO tmc_motor2_sub_ic_enn_io;
  35. void input_sensors_init() {
  36. arm_sensor1_gpio.initAsInput(ARM_SENSOR1_GPIO, ZGPIO::kMode_nopull, ZGPIO::kIRQ_risingAndFallingIrq, true);
  37. arm_sensor2_gpio.initAsInput(ARM_SENSOR2_GPIO, ZGPIO::kMode_nopull, ZGPIO::kIRQ_risingAndFallingIrq, true);
  38. arm_sensor3_gpio.initAsInput(ARM_SENSOR3_GPIO, ZGPIO::kMode_nopull, ZGPIO::kIRQ_noIrq, false);
  39. arm_sensor4_gpio.initAsInput(ARM_SENSOR4_GPIO, ZGPIO::kMode_nopull, ZGPIO::kIRQ_noIrq, false);
  40. arm_sensor5_gpio.initAsInput(ARM_SENSOR5_GPIO, ZGPIO::kMode_nopull, ZGPIO::kIRQ_noIrq, false);
  41. arm_sensor6_gpio.initAsInput(ARM_SENSOR6_GPIO, ZGPIO::kMode_nopull, ZGPIO::kIRQ_noIrq, false);
  42. arm_sensor7_gpio.initAsInput(ARM_SENSOR7_GPIO, ZGPIO::kMode_nopull, ZGPIO::kIRQ_risingAndFallingIrq, true);
  43. arm_sensor8_gpio.initAsInput(ARM_SENSOR8_GPIO, ZGPIO::kMode_nopull, ZGPIO::kIRQ_risingAndFallingIrq, true);
  44. }
  45. uint32_t intput_sensors_get_table0() {
  46. uint32_t val = 0;
  47. val |= arm_sensor1_gpio.getStateUint32() << 1;
  48. val |= arm_sensor2_gpio.getStateUint32() << 2;
  49. val |= arm_sensor3_gpio.getStateUint32() << 3;
  50. val |= arm_sensor4_gpio.getStateUint32() << 4;
  51. val |= arm_sensor5_gpio.getStateUint32() << 5;
  52. val |= arm_sensor6_gpio.getStateUint32() << 6;
  53. val |= arm_sensor7_gpio.getStateUint32() << 7;
  54. val |= arm_sensor8_gpio.getStateUint32() << 8;
  55. return val;
  56. }
  57. ZGPIO *input_sensors_get_arm1_homegpio() { return &arm_sensor8_gpio; }
  58. ZGPIO *input_sensors_get_arm2_homegpio() { return &arm_sensor7_gpio; }
  59. icps::error_t Main::onHostRegisterWriteEvent(IflytopCanProtocolStackProcesser *processer, icps::WriteEvent *event) {}
  60. icps::error_t Main::onHostRegisterReadEvent(IflytopCanProtocolStackProcesser *processer, icps::ReadEvent *event) {}
  61. void Main::onHostRegisterReportEvent(IflytopCanProtocolStackProcesser *processer, icps::ReportEvent *event) {}
  62. void Main::run() {
  63. iflytop_no_os_cfg_t oscfg = {
  64. .delayhtim = &DELAY_US_TIMER,
  65. .debuguart = &DEBUG_UART,
  66. };
  67. iflytop_no_os_init(&oscfg);
  68. ZLOGI(TAG, "robotic_core_xy:%s", VERSION);
  69. debuglight.initAsOutput(DEBUG_LIGHT_GPIO, ZGPIO::kMode_nopull, false, false);
  70. ZHAL_CORE_REG(200, { debuglight.toggleState(); });
  71. /*******************************************************************************
  72. * GPIO输入初始化 *
  73. *******************************************************************************/
  74. input_sensors_init();
  75. intput_sensors_get_table0();
  76. /*******************************************************************************
  77. * *
  78. *******************************************************************************/
  79. tmc_motor1_spi_select1_io.initAsOutput(TMC_MOTOR1_SPI_SELECT1_IO, ZGPIO::kMode_nopull, false, true);
  80. tmc_motor1_nfreeze_io.initAsOutput(TMC_MOTOR1_nFREEZE_IO, ZGPIO::kMode_nopull, false, true);
  81. tmc_motor1_nreset_io.initAsOutput(TMC_MOTOR1_nRESET_IO, ZGPIO::kMode_nopull, false, true);
  82. tmc_motor1_sub_ic_enn_io.initAsOutput(TMC_MOTOR1_SUB_IC_ENN_IO, ZGPIO::kMode_nopull, false, true);
  83. tmc_motor2_spi_select1_io.initAsOutput(TMC_MOTOR2_SPI_SELECT1_IO, ZGPIO::kMode_nopull, false, true);
  84. tmc_motor2_nfreeze_io.initAsOutput(TMC_MOTOR2_nFREEZE_IO, ZGPIO::kMode_nopull, false, true);
  85. tmc_motor2_nreset_io.initAsOutput(TMC_MOTOR2_nRESET_IO, ZGPIO::kMode_nopull, false, true);
  86. tmc_motor2_sub_ic_enn_io.initAsOutput(TMC_MOTOR2_SUB_IC_ENN_IO, ZGPIO::kMode_nopull, false, true);
  87. {
  88. TMC4361A::cfg_t motora_cfg = {.spi = &TMC_MOTOR_SPI,
  89. .csgpio = &tmc_motor1_spi_select1_io,
  90. .resetPin = &tmc_motor1_nreset_io,
  91. .fREEZEPin = &tmc_motor1_nfreeze_io,
  92. .ennPin = NULL,
  93. .driverIC_ennPin = &tmc_motor1_sub_ic_enn_io,
  94. .driverIC_resetPin = NULL};
  95. m_motora.initialize(&motora_cfg);
  96. int32_t ic4361Version = m_motora.readICVersion();
  97. int32_t ic2160Version = m_motora.readSubICVersion();
  98. ZLOGI(TAG, "m_motora:TMC4361Version:%lx TMC2160VERSION:%lx", ic4361Version, ic2160Version);
  99. }
  100. {
  101. TMC4361A::cfg_t motorb_cfg = {.spi = &TMC_MOTOR_SPI,
  102. .csgpio = &tmc_motor2_spi_select1_io,
  103. .resetPin = &tmc_motor2_nreset_io,
  104. .fREEZEPin = &tmc_motor2_nfreeze_io,
  105. .ennPin = NULL,
  106. .driverIC_ennPin = &tmc_motor2_sub_ic_enn_io,
  107. .driverIC_resetPin = NULL};
  108. m_motorb.initialize(&motorb_cfg);
  109. int32_t ic4361Version = m_motorb.readICVersion();
  110. int32_t ic2160Version = m_motorb.readSubICVersion();
  111. ZLOGI(TAG, "m_motorb:TMC4361Version:%lx TMC2160VERSION:%lx", ic4361Version, ic2160Version);
  112. }
  113. /*******************************************************************************
  114. * *
  115. *******************************************************************************/
  116. {
  117. // static ZUART uart;
  118. // PB6.initAsOutput(STM32_GPIO::kOutput_nopull, true, false);
  119. // ZUART::cfg_t cfg = {
  120. // .name = "CODE_SCANER_UART",
  121. // .huart = &CODE_SCANER_UART,
  122. // .rxbuffersize = 300,
  123. // .rxovertime_ms = 10,
  124. // };
  125. // uart.initialize(&cfg,[](){})
  126. // uart.initialize_basic("CODE_SCANER_UART", &m_hardware, &CODE_SCANER_UART);
  127. // uart.initialize_setRxBuffer(300);
  128. // uart.initialize_setRxOvertime(33);
  129. // uart.initialize_finished();
  130. }
  131. /*******************************************************************************
  132. * *
  133. *******************************************************************************/
  134. {
  135. auto *cfg = IflytopCanProtocolStackProcesser::createDefaultConfig(DEVICE_ID, 128);
  136. m_protocolStack.initialize(cfg);
  137. m_protocolStack.setDumpPacketFlag(false);
  138. m_protocolStack.registerListener(this);
  139. m_protocolStack.activeReg(REG_GPIO_INPUT0, icps::kwr, 0);
  140. }
  141. m_motora.setIHOLD_IRUN(1, 12, 0); // 小臂
  142. m_motora.setMotorShaft(0);
  143. m_motorb.setIHOLD_IRUN(1, 12, 0); // 小臂
  144. m_motorb.setMotorShaft(0);
  145. m_motora.rotate(1200000);
  146. m_motorb.rotate(1200000);
  147. while (1) {
  148. ZHALCORE::getInstance()->loop();
  149. }
  150. }