factory.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. #include "factory.h"
  2. #include "bsp/bsp_driver.h"
  3. #include "prot/can_message.h"
  4. #include "prot/can_foc_msg.h"
  5. #include "foc/samples.h"
  6. #include "foc/motor/current.h"
  7. #include "foc/motor/motor.h"
  8. #include "libs/utils.h"
  9. #include "libs/logger.h"
  10. #include "os/os_task.h"
  11. static u8 factory_mode = 0;
  12. static void stop_pwm_adc(void);
  13. static bool phase_adc_start = false;
  14. static bool start_pwm_adc(void) {
  15. if (phase_adc_start) {
  16. return true;
  17. }
  18. pwm_turn_on_low_side();
  19. delay_ms(10);
  20. phase_current_offset_calibrate();
  21. pwm_start();
  22. delay_us(10);
  23. get_motor()->b_start = true;
  24. adc_start_convert();
  25. phase_current_calibrate_wait();
  26. if (phase_curr_offset_check()) {
  27. stop_pwm_adc();
  28. return false;
  29. }
  30. phase_adc_start = true;
  31. return true;
  32. }
  33. static void stop_pwm_adc(void) {
  34. if (!phase_adc_start) {
  35. return;
  36. }
  37. phase_adc_start = false;
  38. u32 mask = cpu_enter_critical();
  39. adc_stop_convert();
  40. pwm_stop();
  41. pwm_up_enable(true);
  42. get_motor()->b_start = false;
  43. cpu_exit_critical(mask);
  44. }
  45. void can_process_factory_message(can_message_t *can_message){
  46. uint8_t response[32];
  47. uint8_t rsplen;
  48. encoder_can_key(response, can_message->key);
  49. response[2] = 0;
  50. rsplen = 3;
  51. switch(can_message->key) {
  52. case BUILD_CMD_KEY(0xE0):
  53. factory_mode = decode_u8(can_message->data);
  54. break;
  55. case BUILD_CMD_KEY(0xE1):
  56. {
  57. if (!factory_is_running()) {
  58. response[2] = 1;
  59. break;
  60. }
  61. u8 item = decode_u8(can_message->data);
  62. if (item == 1) { //3相驱动测试
  63. u8 duty = decode_u8((u8 *)can_message->data + 1);
  64. if (duty != 0) {
  65. pwm_3phase_test();
  66. pwm_start();
  67. u16 duty_time = (u16)((float)duty * FOC_PWM_Half_Period / 100.0f);
  68. pwm_update_duty(duty_time, duty_time, duty_time);
  69. }else {
  70. pwm_stop();
  71. pwm_3phase_init();
  72. }
  73. sys_debug("phase test duty %d\n", duty);
  74. }else if (item == 2) {//获取所有电压的采集值
  75. can_response_vols(can_message->src, can_message->key);
  76. return;
  77. }else if (item == 3) { //读取gpio状态
  78. encode_u16(response + 3, gpio_get_pin_values());
  79. rsplen += 2;
  80. }else if (item == 4) { // u phase detect
  81. int count = 200;
  82. float uvw[3] = {0, 0, 0};
  83. s16 uvw_total[3] = {0, 0, 0};
  84. u8 detect = decode_u8((u8 *)can_message->data + 1);
  85. gpio_phase_u_detect(detect?true:false);
  86. delay_ms(50);
  87. while(count-- > 0) {
  88. delay_us(100);
  89. get_uvw_phases_raw(uvw);
  90. uvw_total[0] += S16Q5(uvw[0]);
  91. uvw_total[1] += S16Q5(uvw[1]);
  92. uvw_total[2] += S16Q5(uvw[2]);
  93. }
  94. encode_s16(response + 3, uvw_total[0]/200);
  95. encode_s16(response + 5, uvw_total[1]/200);
  96. encode_s16(response + 7, uvw_total[2]/200);
  97. gpio_phase_u_detect(false);
  98. rsplen += 6;
  99. }else if (item == 5) { //phase current test
  100. u8 start = decode_u8((u8 *)can_message->data + 1);
  101. if (start == 1) {
  102. pwm_3phase_test(); //use pwm output, disable timer break in
  103. if (!start_pwm_adc()) {
  104. response[2] = 1;
  105. break;
  106. }
  107. }else if (start == 0){
  108. stop_pwm_adc();
  109. pwm_3phase_init();
  110. }else {
  111. s16 ia = S16Q5(foc()->in.curr_abc[0]);
  112. s16 ib = S16Q5(foc()->in.curr_abc[1]);
  113. s16 ic = S16Q5(foc()->in.curr_abc[2]);
  114. encode_s16(response + 3, ia);
  115. encode_s16(response + 5, ib);
  116. encode_s16(response + 7, ic);
  117. rsplen += 6;
  118. }
  119. }
  120. break;
  121. }
  122. default:
  123. rsplen = 0;
  124. break;
  125. }
  126. if (rsplen > 0) {
  127. can_send_response(can_message->src, response, rsplen);
  128. }
  129. }
  130. bool factory_is_running(void) {
  131. return (factory_mode == 0x5A);
  132. }