app.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. #include "bsp/bsp_driver.h"
  2. #include "app/app.h"
  3. #include "os/os_task.h"
  4. #include "libs/logger.h"
  5. #include "libs/utils.h"
  6. #include "foc/motor/motor.h"
  7. #include "foc/motor/current.h"
  8. #include "foc/core/foc_observer.h"
  9. #include "foc/core/ladrc_observer.h"
  10. #include "foc/samples.h"
  11. #include "prot/can_foc_msg.h"
  12. #include "prot/can_message.h"
  13. #include "libs/time_measure.h"
  14. #include "app/nv_storage.h"
  15. #include "foc/commands.h"
  16. #include "foc/core/F_Calc.h"
  17. #include "foc/motor/motor_param.h"
  18. #include "foc/motor/mot_params_ind.h"
  19. #include "foc/limit.h"
  20. #include "foc/mc_error.h"
  21. #include "foc/mc_config.h"
  22. #include "foc/motor/throttle.h"
  23. static u32 app_low_task(void *args);
  24. static u32 app_report_task(void *args);
  25. static u32 app_plot_task(void *args);
  26. //static u32 _app_trq_test_task(void *args);
  27. extern void PMSM_FOC_LogDebug(void);
  28. extern void mc_err_code_log(void);
  29. extern void encoder_log(void);
  30. extern void sample_log(void);
  31. extern void throttle_log(void);
  32. extern bool can_is_connect_pc(void);
  33. extern measure_time_t g_meas_hall;
  34. extern measure_time_t g_meas_foc;
  35. extern measure_time_t g_meas_MCTask;
  36. void app_start(void){
  37. can_debug(true);
  38. can_message_init();
  39. nv_storage_init();
  40. mc_conf_init();
  41. mc_err_block_init(FOC_Reset_Reson);
  42. mc_init();
  43. shark_task_create(app_low_task, NULL);
  44. shark_task_create(app_report_task, NULL);
  45. shark_task_create(app_plot_task, NULL);
  46. sys_debug("mc start\n");
  47. shark_task_run();
  48. }
  49. #if 0
  50. static void test_motor_temp(void) {
  51. static s16 temp = 100;
  52. static bool add = true;
  53. sys_debug("mot: %d->%f\n", temp, motor_temp_high_limit_test(temp));
  54. if (temp > 90 && temp != 300) {
  55. temp += add?1:-1;
  56. if (temp == 133) {
  57. add = false;
  58. }
  59. }else {
  60. temp = 300;
  61. }
  62. }
  63. static void test_mos_temp(void) {
  64. static s16 temp = 88;
  65. static bool add = true;
  66. sys_debug("mos: %d->%f\n", temp, mos_temp_high_limit_test(temp));
  67. if (temp > 85 && temp != -40) {
  68. temp += add?1:-1;
  69. if (temp == 123) {
  70. add = false;
  71. }
  72. }else {
  73. temp = -40;
  74. }
  75. }
  76. static void test_vbus_vol(void) {
  77. static s16 vol = 65;
  78. static bool add = false;
  79. sys_debug("vbus: %d->%d\n", vol, vbus_voltage_low_limit_test(vol));
  80. vol += add?1:-1;
  81. if (vol < 40) {
  82. add = true;
  83. }else if (vol >= 65) {
  84. add = false;
  85. }
  86. }
  87. #endif
  88. static void app_print_log(void) {
  89. //sys_debug("Slow: %d - %d, err:%d, %d\n", g_meas_MCTask.intval_time, g_meas_MCTask.exec_time, g_meas_MCTask.exec_max_error_time, g_meas_MCTask.exec_time_error);
  90. sys_debug("Fast: %d - %d, err: %d-%d-%d\n", g_meas_foc.intval_time, g_meas_foc.exec_time, g_meas_foc.intval_hi_err, g_meas_foc.intval_low_err, g_meas_foc.exec_max_error_time);
  91. sys_debug("FOC time err %d %d %d %d\n", g_meas_foc.intval_time_h_error, g_meas_foc.intval_time_l_error, g_meas_foc.exec_max_error_time, g_meas_foc.exec_time_error);
  92. sys_debug("acc vol %d, bid %d\n", get_acc_vol(), gpio_board_id());
  93. //sys_debug("throttle %f\n", get_throttle_float());
  94. sys_debug("ADC Vref %f, %f\n", get_adc_vref(), adc_5vref_compesion());
  95. //sys_debug("dead time 0x%x\n", get_deadtime());
  96. //thro_torque_log();
  97. //sys_debug("_>%f, %f, %f\n", ladrc_observer_get()->ld, ladrc_observer_get()->lq, ladrc_observer_get()->poles);
  98. //encoder_log();
  99. //motor_debug();
  100. //sample_log();
  101. //throttle_log();
  102. //sys_debug("Trq: %f-%f-%f\n", motor.controller.ramp_input_torque.target, motor.controller.ramp_input_torque.interpolation, motor.controller.ramp_input_torque.step);
  103. sys_debug("FOC is %s, %d,%d\n", mot_contrl_is_start(mot_contrl())?"start":"stop", motor.foc_start_cnt, motor.foc_stop_cnt);
  104. //F_debug();
  105. //eCtrl_debug_log();
  106. //sys_debug("enc err %d, %d\n", foc_observer_enc_errcount(), foc_observer_sensorless_stable());
  107. //mc_err_code_log();
  108. //sys_debug("=====\n");
  109. //test_mos_temp();
  110. //test_mos_temp();
  111. //test_vbus_vol();
  112. }
  113. static u32 app_report_task(void *p) {
  114. static u32 loop = 0;
  115. can_report_ext_status(0x43);
  116. can_mcast_foc_status2();
  117. if (!can_is_connect_pc()) {
  118. return 200;
  119. }
  120. can_report_power(0x45);
  121. can_report_foc_status(0x45);
  122. can_report_dq_voltage(0X45);
  123. if (motor.controller.b_mtpa_calibrate) {
  124. can_report_mpta_values(0x45);
  125. }else {
  126. can_report_dq_current(0x45);
  127. }
  128. if (mot_params_rs_ested()) {
  129. can_report_mot_params_ested(mot_params_get_est_rs(), R_TYPE);
  130. }
  131. if (mot_params_ld_ested()) {
  132. can_report_mot_params_ested(mot_params_get_est_ld(), L_TYPE_D);
  133. }
  134. if (mot_params_lq_ested()) {
  135. can_report_mot_params_ested(mot_params_get_est_lq(), L_TYPE_Q);
  136. }
  137. if (mot_params_flux_ested()) {
  138. can_report_mot_params_ested(mot_params_get_est_flux(), FLUX_TYPE);
  139. }
  140. if (++loop % 10 == 0) {
  141. app_print_log();
  142. }
  143. return 200;
  144. }
  145. int plot_type = 0;
  146. static void plot_smo_angle(void) {
  147. u32 mask = cpu_enter_critical();
  148. float smo_angle = foc_observer_sensorless_angle();
  149. float mot_angle = foc()->in.mot_angle;
  150. cpu_exit_critical(mask);
  151. float delta = smo_angle - mot_angle;
  152. float s, c;
  153. arm_sin_cos(delta, &s, &c);
  154. delta = fast_atan2(s, c)/PI*180.0f;
  155. can_plot3(mot_angle, smo_angle, delta);
  156. }
  157. static u32 app_plot_task(void * args) {
  158. if (plot_type == 1) {
  159. s16 plot_arg1 = (s16)foc_observer_sensorless_speed();
  160. s16 plot_arg2 = (s16)mot_contrl_get_speed(&motor.controller);
  161. if (mot_contrl()->mode_running == CTRL_MODE_SPD) {
  162. if (mc_is_cruise_enabled()) {
  163. plot_arg1 = mot_contrl()->ramp_cruise_vel.target;
  164. }else {
  165. plot_arg1 = mot_contrl()->ramp_target_vel.target;
  166. }
  167. }
  168. can_plot2(plot_arg1, plot_arg2);
  169. }else if (plot_type == 2) {
  170. can_plot2(mot_contrl_get_final_torque(&motor.controller), mot_contrl()->target_torque);
  171. }else if (plot_type == 3) {
  172. plot_smo_angle();
  173. }else if (plot_type == 4) {
  174. can_plot2((s16)foc()->out.curr_dq.d, (s16)foc()->out.curr_dq.q);
  175. }else if (plot_type == 5) {
  176. can_plot2((s16)foc()->in.target_id.interpolation , (s16)foc()->in.target_iq.interpolation);
  177. }else if (plot_type == 6) {
  178. //do it in other place
  179. }else if (plot_type == 7) {
  180. can_plot2((s16)(foc()->in.target_id.target*10.0f), (s16)(foc()->out.curr_dq.d * 10.0f));
  181. }else if (plot_type == 8) {
  182. can_plot2((s16)(foc()->in.target_iq.target*10.0f), (s16)(foc()->out.curr_dq.q * 10.0f));
  183. }else if (plot_type == 9) {
  184. s16 thro_v = get_throttle_float() * 100.0f;
  185. s16 thro2_v = get_throttle2_float() * 100.0f;
  186. s16 sig = throttle_get_signal();
  187. can_plot3(sig, thro_v, thro2_v);
  188. }else if (plot_type == 10) {
  189. s16 duty = (s16)(motor.controller.duty_raw * 100);
  190. s16 duty_filterd = (s16)(motor.controller.duty_filterd * 100);
  191. can_plot2(duty, duty_filterd);
  192. }
  193. return 20;
  194. }
  195. static u32 app_low_task(void *args) {
  196. wdog_reload();
  197. if (!mot_contrl_is_start(&motor.controller)) {
  198. mc_err_block_save();
  199. }
  200. return 1;
  201. }