can_foc_msg.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. #include "libs/utils.h"
  2. #include "prot/can_message.h"
  3. #include "prot/can_foc_msg.h"
  4. #include "foc/commands.h"
  5. #include "foc/samples.h"
  6. #include "foc/motor/motor.h"
  7. #include "foc/core/PMSM_FOC_Core.h"
  8. void can_report_speed(u8 can, s16 rpm) {
  9. u8 data[6];
  10. encoder_can_key(data, CMD_2_CAN_KEY(Foc_Report_Speed));
  11. encode_s16(data + 2, rpm);
  12. can_send_message(get_indicator_can_id(can), data, sizeof(data), 0);
  13. }
  14. void can_report_power(u8 can) {
  15. u8 data[8];
  16. s16 rpm = (s16)motor_encoder_get_speed();
  17. float vDC = get_vbus_float();
  18. float iDC = PMSM_FOC_GetVbusCurrent();
  19. s16 v = S16Q5(vDC);
  20. s16 i = S16Q5(iDC);
  21. encoder_can_key(data, CMD_2_CAN_KEY(Foc_Report_Power));
  22. encode_s16(data + 2, ABS(rpm));
  23. encode_s16(data + 4, v);
  24. encode_s16(data + 6, i);
  25. can_send_message(get_indicator_can_id(can), data, sizeof(data), 0);
  26. }
  27. void can_report_phase_current(u8 can) {
  28. u8 data[8];
  29. encoder_can_key(data, CMD_2_CAN_KEY(Foc_Report_Phase_Current));
  30. encode_s16(data + 2, S16Q5(PMSM_FOC_Get()->in.s_iABC[0]));
  31. encode_s16(data + 4, S16Q5(PMSM_FOC_Get()->in.s_iABC[1]));
  32. encode_s16(data + 6, S16Q5(PMSM_FOC_Get()->in.s_iABC[2]));
  33. can_send_message(get_indicator_can_id(can), data, sizeof(data), 0);
  34. }
  35. void can_report_phase_voltage(u8 can) {
  36. u8 data[8];
  37. float s_vABC[3];
  38. get_phase_vols(s_vABC);
  39. encoder_can_key(data, CMD_2_CAN_KEY(Foc_Report_Phase_Vol));
  40. encode_s16(data + 2, S16Q5(s_vABC[0]));
  41. encode_s16(data + 4, S16Q5(s_vABC[1]));
  42. encode_s16(data + 6, S16Q5(s_vABC[2]));
  43. can_send_message(get_indicator_can_id(can), data, sizeof(data), 0);
  44. }
  45. void can_report_dq_current(u8 can) {
  46. u8 data[6];
  47. encoder_can_key(data, CMD_2_CAN_KEY(Foc_Report_Dq_Current));
  48. float id = PMSM_FOC_Get()->out.s_FilterIdq.d;
  49. float iq = PMSM_FOC_Get()->out.s_FilterIdq.q;
  50. encode_s16(data + 2, S16Q5(id));
  51. encode_s16(data + 4, S16Q5(iq));
  52. can_send_message(get_indicator_can_id(can), data, sizeof(data), 0);
  53. }
  54. void can_response_hall_offset(u8 can, int offset) {
  55. u8 data[7];
  56. encoder_can_key(data, CMD_2_CAN_KEY(Foc_Cali_Hall_Offset));
  57. encode_u8(data + 2, 1);
  58. encode_u32(data + 3, offset);
  59. can_send_message(get_indicator_can_id(can), data, sizeof(data), 0);
  60. }
  61. void can_report_pid_value(u8 can, u8 id) {
  62. float kp, ki, kd;
  63. PMSM_FOC_GetPid(id, &kp, &ki, &kd);
  64. u8 data[15];
  65. encoder_can_key(data, CMD_2_CAN_KEY(Foc_Report_Pid));
  66. data[2] = id;
  67. encode_float(data + 3, kp);
  68. encode_float(data + 7, ki);
  69. encode_float(data + 11, kd);
  70. can_send_message(get_indicator_can_id(can), data, sizeof(data), 0);
  71. }
  72. void can_report_foc_status(u8 can) {
  73. u8 data[16];
  74. encoder_can_key(data, CMD_2_CAN_KEY(Foc_Report_Status));
  75. mc_get_running_status(data+2);
  76. u32 errMask = mc_get_critical_error();
  77. encode_u32(data + 3, errMask);
  78. encode_s16(data + 7, get_motor_temp());
  79. encode_s16(data + 9, MAX(get_mos_temp(), get_mos_temp2()));
  80. encode_u32(data + 11, shark_get_seconds());
  81. can_send_message(get_indicator_can_id(can), data, sizeof(data), 0);
  82. }
  83. void can_report_mpta_values(u8 can) {
  84. u8 data[8];
  85. if (!PMSM_FOC_Get()->in.b_MTPA_calibrate) {
  86. return;
  87. }
  88. encoder_can_key(data, CMD_2_CAN_KEY(Foc_Report_MTPA_DQ_Angle));
  89. encode_s16(data + 2, S16Q5(PMSM_FOC_Get()->out.s_RealCurrentFiltered));
  90. encode_s16(data + 4, S16Q5(PMSM_FOC_Get()->out.s_FilterIdq.d));
  91. encode_s16(data + 6, S16Q5(PMSM_FOC_Get()->out.s_FilterIdq.q));
  92. can_send_message(get_indicator_can_id(can), data, sizeof(data), 0);
  93. }
  94. void can_report_ext_status(u8 can) {
  95. u8 data[8] = {0x0, 0x0, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
  96. data[0] = (get_vbus_int()>60?1:0) << 5;
  97. data[0] |= (mc_is_epm()?1:0) << 6;
  98. data[0] |= (mc_is_epm()?1:0) << 7;
  99. data[1] = mc_is_start()?0:1;
  100. data[1] |= (PMSM_FOC_Is_CruiseEnabled()?1:0) << 3;
  101. data[1] |= mc_get_gear() << 6;
  102. encode_s16(data + 2, ABS((s16)(motor_encoder_get_speed()/4.0f)));
  103. float vDC = get_vbus_float();
  104. encode_s16(data + 4, (s16)(vDC*10));
  105. float iDC = PMSM_FOC_GetVbusCurrent();
  106. encode_s16(data + 6, (s16)(iDC*10));
  107. shark_can0_send_ext_message(0x1A014D43, data, sizeof(data));
  108. }
  109. void can_report_plot_values(u8 can) {
  110. s16 trq_ref = eCtrl_get_RefTorque() * 10;
  111. s16 real_ref = PMSM_FOC_Get_Real_Torque() * 10;
  112. can_plot2(trq_ref, real_ref);
  113. }
  114. void can_plot1(s16 v1) {
  115. u8 data[4];
  116. encoder_can_key(data, CMD_2_CAN_KEY(Foc_Report_Plot));
  117. encode_s16(data + 2, v1);
  118. can_send_message(get_indicator_can_id(0x45), data, sizeof(data), 0);
  119. }
  120. void can_plot2(s16 v1, s16 v2) {
  121. u8 data[6];
  122. encoder_can_key(data, CMD_2_CAN_KEY(Foc_Report_Plot));
  123. encode_s16(data + 2, v1);
  124. encode_s16(data + 4, v2);
  125. can_send_message(get_indicator_can_id(0x45), data, sizeof(data), 0);
  126. }
  127. void can_plot3(s16 v1, s16 v2, s16 v3) {
  128. u8 data[8];
  129. encoder_can_key(data, CMD_2_CAN_KEY(Foc_Report_Plot));
  130. encode_s16(data + 2, v1);
  131. encode_s16(data + 4, v2);
  132. encode_s16(data + 6, v3);
  133. can_send_message(get_indicator_can_id(0x45), data, sizeof(data), 0);
  134. }