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.

177 lines
7.6 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
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) { return icps::kSuccess; }
  60. icps::error_t Main::onHostRegisterReadEvent(IflytopCanProtocolStackProcesser *processer, icps::ReadEvent *event) { return icps::kSuccess; }
  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. printf("int32_t %d int %d longint %d\n", sizeof(int32_t), sizeof(int), sizeof(long int));
  70. debuglight.initAsOutput(DEBUG_LIGHT_GPIO, ZGPIO::kMode_nopull, false, false);
  71. ZHAL_CORE_REG(200, { debuglight.toggleState(); });
  72. /*******************************************************************************
  73. * GPIO输入初始化 *
  74. *******************************************************************************/
  75. input_sensors_init();
  76. intput_sensors_get_table0();
  77. /*******************************************************************************
  78. * *
  79. *******************************************************************************/
  80. tmc_motor1_spi_select1_io.initAsOutput(TMC_MOTOR1_SPI_SELECT1_IO, ZGPIO::kMode_nopull, false, true);
  81. tmc_motor1_nfreeze_io.initAsOutput(TMC_MOTOR1_nFREEZE_IO, ZGPIO::kMode_nopull, false, true);
  82. tmc_motor1_nreset_io.initAsOutput(TMC_MOTOR1_nRESET_IO, ZGPIO::kMode_nopull, false, true);
  83. tmc_motor1_sub_ic_enn_io.initAsOutput(TMC_MOTOR1_SUB_IC_ENN_IO, ZGPIO::kMode_nopull, false, true);
  84. tmc_motor2_spi_select1_io.initAsOutput(TMC_MOTOR2_SPI_SELECT1_IO, ZGPIO::kMode_nopull, false, true);
  85. tmc_motor2_nfreeze_io.initAsOutput(TMC_MOTOR2_nFREEZE_IO, ZGPIO::kMode_nopull, false, true);
  86. tmc_motor2_nreset_io.initAsOutput(TMC_MOTOR2_nRESET_IO, ZGPIO::kMode_nopull, false, true);
  87. tmc_motor2_sub_ic_enn_io.initAsOutput(TMC_MOTOR2_SUB_IC_ENN_IO, ZGPIO::kMode_nopull, false, true);
  88. {
  89. TMC4361A::cfg_t motora_cfg = {.spi = &TMC_MOTOR_SPI,
  90. .csgpio = &tmc_motor1_spi_select1_io,
  91. .resetPin = &tmc_motor1_nreset_io,
  92. .fREEZEPin = &tmc_motor1_nfreeze_io,
  93. .ennPin = NULL,
  94. .driverIC_ennPin = &tmc_motor1_sub_ic_enn_io,
  95. .driverIC_resetPin = NULL};
  96. m_motora.initialize(&motora_cfg);
  97. int32_t ic4361Version = m_motora.readICVersion();
  98. int32_t ic2160Version = m_motora.readSubICVersion();
  99. ZLOGI(TAG, "m_motora:TMC4361Version:%lx TMC2160VERSION:%lx", ic4361Version, ic2160Version);
  100. }
  101. {
  102. TMC4361A::cfg_t motorb_cfg = {.spi = &TMC_MOTOR_SPI,
  103. .csgpio = &tmc_motor2_spi_select1_io,
  104. .resetPin = &tmc_motor2_nreset_io,
  105. .fREEZEPin = &tmc_motor2_nfreeze_io,
  106. .ennPin = NULL,
  107. .driverIC_ennPin = &tmc_motor2_sub_ic_enn_io,
  108. .driverIC_resetPin = NULL};
  109. m_motorb.initialize(&motorb_cfg);
  110. int32_t ic4361Version = m_motorb.readICVersion();
  111. int32_t ic2160Version = m_motorb.readSubICVersion();
  112. ZLOGI(TAG, "m_motorb:TMC4361Version:%lx TMC2160VERSION:%lx", ic4361Version, ic2160Version);
  113. }
  114. /*******************************************************************************
  115. * *
  116. *******************************************************************************/
  117. {
  118. // static ZUART uart;
  119. // PB6.initAsOutput(STM32_GPIO::kOutput_nopull, true, false);
  120. // ZUART::cfg_t cfg = {
  121. // .name = "CODE_SCANER_UART",
  122. // .huart = &CODE_SCANER_UART,
  123. // .rxbuffersize = 300,
  124. // .rxovertime_ms = 10,
  125. // };
  126. // uart.initialize(&cfg,[](){})
  127. // uart.initialize_basic("CODE_SCANER_UART", &m_hardware, &CODE_SCANER_UART);
  128. // uart.initialize_setRxBuffer(300);
  129. // uart.initialize_setRxOvertime(33);
  130. // uart.initialize_finished();
  131. }
  132. /*******************************************************************************
  133. * *
  134. *******************************************************************************/
  135. {
  136. auto *cfg = IflytopCanProtocolStackProcesser::createDefaultConfig(DEVICE_ID, 128);
  137. m_protocolStack.initialize(cfg);
  138. m_protocolStack.setDumpPacketFlag(false);
  139. m_protocolStack.registerListener(this);
  140. m_protocolStack.activeReg(REG_GPIO_INPUT0, icps::kwr, 0);
  141. }
  142. m_motora.setIHOLD_IRUN(1, 12, 0); // 小臂
  143. m_motora.setMotorShaft(0);
  144. m_motorb.setIHOLD_IRUN(1, 12, 0); // 小臂
  145. m_motorb.setMotorShaft(0);
  146. m_motora.rotate(1200000);
  147. m_motorb.rotate(1200000);
  148. while (1) {
  149. ZHALCORE::getInstance()->loop();
  150. }
  151. }