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.

224 lines
7.3 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
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 "one_dimensional_code_laser_scanner.hpp"
  8. // #include "sdk/components/single_axis_motor_control_v2/single_axis_motor_control_v2.hpp"
  9. #include "sdk/components/iflytop_can_slave_modules/idcard_reader_service.hpp"
  10. #include "sdk/components/single_axis_motor_control/single_axis_motor_control.hpp"
  11. #include "sdk/hal/zhal.hpp"
  12. #include "sdk\components\iflytop_can_slave_modules\io_control_service.hpp"
  13. #include "sdk\components\iflytop_can_slave_v1\iflytop_can_slave.hpp"
  14. #include "sdk\components\m3078\m3078_code_scaner.hpp"
  15. #include "sdk\components\tmc\ic\ztmc4361A.hpp"
  16. #include "sdk\components\tmc\ic\ztmc5130.hpp"
  17. //
  18. #include "sdk\components\huacheng_sensor\dp600_pressure_sensor.hpp"
  19. #include "sdk\components\zcan_module\huacheng_pressure_sensor.hpp"
  20. #include "sdk\components\zcan_module\zcan_basic_order_module.hpp"
  21. #include "sdk\components\zcan_module\zcan_pump_ctrl_module.hpp"
  22. #include "sdk\components\zcan_module\zcan_trigle_warning_light_ctl_module.hpp"
  23. #define TAG "main"
  24. namespace iflytop {
  25. Main gmain;
  26. };
  27. using namespace iflytop;
  28. IflytopCanProtocolStackProcesser m_protocolStack;
  29. TMC5130 m_motor1;
  30. TMC5130 m_motor2;
  31. ZGPIO debuglight;
  32. ZGPIO triLight_R;
  33. ZGPIO triLight_G;
  34. ZGPIO triLight_B;
  35. ZGPIO triLight_BEEP;
  36. ZGPIO m_input1;
  37. ZGPIO m_input2;
  38. ZGPIO m_input3;
  39. ZGPIO m_input4;
  40. ZGPIO m_input5;
  41. ZGPIO output1;
  42. ZGPIO output2;
  43. ZCanReceiver m_canReceiver;
  44. ZCanBasicOrderModule m_basicOrderModule;
  45. ZCanPumpCtrlModule m_pumpCtrlModule;
  46. ZCanTrigleWarningLightCtlModule m_warningLightCtlModule;
  47. HuachengPressureSensor m_huachengPressureSensor;
  48. void setmotor(TMC5130 *motor, int16_t acc_rpm2, int16_t rpm, int16_t idlepower, int16_t power) {
  49. int32_t ppm = rpm / 60.0 * 51200;
  50. int32_t acc = acc_rpm2 / 60.0 * 51200;
  51. int16_t _idlepower = 1;
  52. int16_t _power = 31;
  53. if (idlepower > 0 && idlepower < 31) {
  54. _idlepower = idlepower;
  55. }
  56. if (power > 0 && power < 31) {
  57. _power = power;
  58. }
  59. motor->setIHOLD_IRUN(_idlepower, _power, 10); // 5W
  60. motor->setAcceleration(acc);
  61. motor->setDeceleration(acc);
  62. motor->rotate(ppm);
  63. }
  64. void Main::onRceivePacket(CanPacketRxBuffer *rxbuf, uint8_t *packet, size_t len) {
  65. ZLOGI(TAG, "onRceivePacket from %d %d", rxbuf->id, len);
  66. for (size_t i = 0; i < len; i++) {
  67. printf("%02X ", packet[i]);
  68. }
  69. printf("\n");
  70. }
  71. void Main::run() {
  72. ZHALCORE::cfg_t oscfg = {
  73. .delayhtim = &DELAY_US_TIMER,
  74. .debuguart = &DEBUG_UART,
  75. };
  76. ZHALCORE::getInstance()->initialize(oscfg);
  77. ZLOGI(TAG, "zapp:%s", VERSION);
  78. printf("int32_t %d int %d longint %d\n", sizeof(int32_t), sizeof(int), sizeof(long int));
  79. debuglight.initAsOutput(DEBUG_LIGHT_GPIO, ZGPIO::kMode_nopull, false, false);
  80. ZHAL_CORE_REG(200, { debuglight.toggleState(); });
  81. ZCanReceiver::CFG *cfg = m_canReceiver.createCFG(DEVICE_ID);
  82. m_canReceiver.init(cfg);
  83. m_canReceiver.registerListener(this);
  84. /**
  85. * @brief
  86. */
  87. m_input1.initAsInput(PD11, ZGPIO::kMode_nopull, ZGPIO::kIRQ_noIrq, true /*mirror*/);
  88. m_input2.initAsInput(PC5, ZGPIO::kMode_nopull, ZGPIO::kIRQ_noIrq, true /*mirror*/);
  89. m_input3.initAsInput(PD12, ZGPIO::kMode_nopull, ZGPIO::kIRQ_noIrq, true /*mirror*/);
  90. m_input4.initAsInput(PD13, ZGPIO::kMode_nopull, ZGPIO::kIRQ_noIrq, true /*mirror*/);
  91. m_input5.initAsInput(PC6, ZGPIO::kMode_nopull, ZGPIO::kIRQ_noIrq, true /*mirror*/);
  92. m_basicOrderModule.initialize(&m_canReceiver);
  93. m_basicOrderModule.regInputCtl([this](uint8_t id, bool &val) {
  94. if (id == 1) {
  95. val = m_input1.getState();
  96. return true;
  97. }
  98. if (id == 2) {
  99. val = m_input2.getState();
  100. return true;
  101. }
  102. if (id == 3) {
  103. val = m_input3.getState();
  104. return true;
  105. }
  106. if (id == 4) {
  107. val = m_input4.getState();
  108. return true;
  109. }
  110. if (id == 5) {
  111. val = m_input5.getState();
  112. return true;
  113. }
  114. return false;
  115. });
  116. // output1.reg
  117. // output2
  118. m_basicOrderModule.regOutCtl([this](uint8_t id, bool val) { return false; });
  119. ZHAL_CORE_REG(3000, {
  120. // ZLOGI(TAG, "IO1:%d IO2:%d", m_input1.getState(), m_input2.getState());
  121. ZLOGI(TAG, "IO1:%d IO2:%d IO3:%d IO4:%d IO5:%d", m_input1.getState(), m_input2.getState(), m_input3.getState(), m_input4.getState(), m_input5.getState());
  122. });
  123. /*******************************************************************************
  124. * *
  125. *******************************************************************************/
  126. {
  127. TMC5130::cfg_t cfg = {.hspi = &MOTOR_SPI, .enn_pin = MOTOR1_ENN, .csn_pin = MOTOR1_CSN};
  128. m_motor1.initialize(&cfg);
  129. int32_t chipv = m_motor1.readChipVERSION();
  130. ZLOGI(TAG, "m_motor1:%lx", chipv);
  131. m_motor1.setIHOLD_IRUN(1, 20, 0);
  132. m_motor1.setMotorShaft(true);
  133. m_motor1.setAcceleration(300000);
  134. m_motor1.setDeceleration(300000);
  135. // m_motor1.rotate(1000000);
  136. }
  137. {
  138. TMC5130::cfg_t cfg = {.hspi = &MOTOR_SPI, .enn_pin = MOTOR2_ENN, .csn_pin = MOTOR2_CSN};
  139. m_motor2.initialize(&cfg);
  140. int32_t chipv = m_motor2.readChipVERSION();
  141. ZLOGI(TAG, "m_motor2:%lx", chipv);
  142. m_motor2.setIHOLD_IRUN(1, 20, 0); // 5W
  143. m_motor2.setMotorShaft(true);
  144. m_motor2.setAcceleration(300000);
  145. m_motor2.setDeceleration(300000);
  146. // m_motor1.rotate(1000000);
  147. }
  148. m_pumpCtrlModule.initialize(&m_canReceiver);
  149. m_pumpCtrlModule.regSubmodule(1, [&](int16_t acc_rpm2, int16_t rpm, int16_t idlepower, int16_t power) {
  150. ZLOGI(TAG, "pump1 acc_rpm2:%d rpm:%d", acc_rpm2, rpm);
  151. setmotor(&m_motor1, acc_rpm2, rpm, idlepower, power);
  152. });
  153. m_pumpCtrlModule.regSubmodule(2, [&](int16_t acc_rpm2, int16_t rpm, int16_t idlepower, int16_t power) {
  154. ZLOGI(TAG, "pump2 acc:%d rpm:%d", acc_rpm2, rpm);
  155. setmotor(&m_motor2, acc_rpm2, rpm, idlepower, power);
  156. });
  157. /*******************************************************************************
  158. * *
  159. *******************************************************************************/
  160. {
  161. triLight_R.initAsOutput(PD8, ZGPIO::kMode_nopull, false, false);
  162. triLight_G.initAsOutput(PD7, ZGPIO::kMode_nopull, false, false);
  163. triLight_B.initAsOutput(PD9, ZGPIO::kMode_nopull, false, false);
  164. triLight_BEEP.initAsOutput(PD10, ZGPIO::kMode_nopull, false, false);
  165. m_warningLightCtlModule.initialize(&m_canReceiver);
  166. m_warningLightCtlModule.regSubmodule(1, [&](uint8_t r, uint8_t g, uint8_t b, uint8_t beep) {
  167. ZLOGI(TAG, "warningLightCtlModule r:%d g:%d b:%d beep:%d", r, g, b, beep);
  168. triLight_R.setState(r != 0);
  169. triLight_G.setState(g != 0);
  170. triLight_B.setState(b != 0);
  171. triLight_BEEP.setState(beep != 0);
  172. });
  173. }
  174. /*******************************************************************************
  175. * *
  176. *******************************************************************************/
  177. {
  178. m_huachengPressureSensor.initialize(&m_canReceiver);
  179. m_huachengPressureSensor.regSubmodule(1, &huart3, 1);
  180. m_huachengPressureSensor.regSubmodule(2, &huart3, 2);
  181. m_huachengPressureSensor.regSubmodule(3, &huart3, 3);
  182. m_huachengPressureSensor.regSubmodule(4, &huart3, 4);
  183. }
  184. ZLOGI(TAG, "init done");
  185. while (1) {
  186. ZHALCORE::getInstance()->loop();
  187. }
  188. }