app.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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 void hall_debug_log(void);
  34. extern measure_time_t g_meas_hall;
  35. extern measure_time_t g_meas_foc;
  36. extern measure_time_t g_meas_MCTask;
  37. void app_start(void){
  38. can_debug(true);
  39. can_message_init();
  40. nv_storage_init();
  41. mc_conf_init();
  42. mc_err_block_init(FOC_Reset_Reson);
  43. mc_init();
  44. shark_task_create(app_low_task, NULL);
  45. shark_task_create(app_report_task, NULL);
  46. shark_task_create(app_plot_task, NULL);
  47. sys_debug("mc start\n");
  48. shark_task_run();
  49. }
  50. #if 0
  51. static void test_motor_temp(void) {
  52. static s16 temp = 100;
  53. static bool add = true;
  54. sys_debug("mot: %d->%f\n", temp, motor_temp_high_limit_test(temp));
  55. if (temp > 90 && temp != 300) {
  56. temp += add?1:-1;
  57. if (temp == 133) {
  58. add = false;
  59. }
  60. }else {
  61. temp = 300;
  62. }
  63. }
  64. static void test_mos_temp(void) {
  65. static s16 temp = 88;
  66. static bool add = true;
  67. sys_debug("mos: %d->%f\n", temp, mos_temp_high_limit_test(temp));
  68. if (temp > 85 && temp != -40) {
  69. temp += add?1:-1;
  70. if (temp == 123) {
  71. add = false;
  72. }
  73. }else {
  74. temp = -40;
  75. }
  76. }
  77. static void test_vbus_vol(void) {
  78. static s16 vol = 65;
  79. static bool add = false;
  80. sys_debug("vbus: %d->%d\n", vol, vbus_voltage_low_limit_test(vol));
  81. vol += add?1:-1;
  82. if (vol < 40) {
  83. add = true;
  84. }else if (vol >= 65) {
  85. add = false;
  86. }
  87. }
  88. #endif
  89. static void app_print_log(void) {
  90. //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);
  91. 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);
  92. 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);
  93. //sys_debug("acc vol %d, bid %d\n", get_acc_vol(), gpio_board_id());
  94. //sys_debug("throttle %f\n", get_throttle_float());
  95. //sys_debug("ADC Vref %f, %f\n", get_adc_vref(), adc_5vref_compesion());
  96. //sys_debug("curr A:%f,B:%f,C:%f\n", foc()->in.curr_abc[0], foc()->in.curr_abc[1], foc()->in.curr_abc[2]);
  97. //sys_debug("dead time 0x%x\n", get_deadtime());
  98. //thro_torque_log();
  99. //sys_debug("_>%f, %f, %f\n", ladrc_observer_get()->ld, ladrc_observer_get()->lq, ladrc_observer_get()->poles);
  100. //encoder_log();
  101. hall_debug_log();
  102. //motor_debug();
  103. //sample_log();
  104. //throttle_log();
  105. //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);
  106. sys_debug("FOC is %s, %d,%d, pwm:%s\n", mot_contrl_is_start(mot_contrl())?"start":"stop", motor.foc_start_cnt, motor.foc_stop_cnt, pwm_channel_is_enabled()?"start":"stop");
  107. sys_debug("curr: %f,%f\n", mot_contrl()->dc_curr_calc, mot_contrl()->dc_curr_filted);
  108. float phase_vol[3];
  109. get_uvw_phases_raw(phase_vol);
  110. sys_debug("phvol: %f,%f,%f\n", phase_vol[0], phase_vol[1], phase_vol[2]);
  111. //F_debug();
  112. //eCtrl_debug_log();
  113. //sys_debug("enc err %d, %d\n", foc_observer_enc_errcount(), foc_observer_sensorless_stable());
  114. //mc_err_code_log();
  115. //sys_debug("=====\n");
  116. //test_mos_temp();
  117. //test_mos_temp();
  118. //test_vbus_vol();
  119. }
  120. static u32 app_report_task(void *p) {
  121. static u32 loop = 0;
  122. can_report_ext_status(0x43);
  123. can_mcast_foc_status2();
  124. if (!can_is_connect_pc()) {
  125. return 200;
  126. }
  127. can_report_power(0x45);
  128. can_report_foc_status(0x45);
  129. can_report_dq_voltage(0X45);
  130. if (motor.controller.b_mtpa_calibrate) {
  131. can_report_mpta_values(0x45);
  132. }else {
  133. can_report_dq_current(0x45);
  134. }
  135. if (mot_params_rs_ested()) {
  136. can_report_mot_params_ested(mot_params_get_est_rs(), R_TYPE);
  137. }
  138. if (mot_params_ld_ested()) {
  139. can_report_mot_params_ested(mot_params_get_est_ld(), L_TYPE_D);
  140. }
  141. if (mot_params_lq_ested()) {
  142. can_report_mot_params_ested(mot_params_get_est_lq(), L_TYPE_Q);
  143. }
  144. if (mot_params_flux_ested()) {
  145. can_report_mot_params_ested(mot_params_get_est_flux(), FLUX_TYPE);
  146. }
  147. if (++loop % 10 == 0) {
  148. app_print_log();
  149. }
  150. return 200;
  151. }
  152. int plot_type = 0;
  153. static void plot_smo_angle(void) {
  154. u32 mask = cpu_enter_critical();
  155. float smo_angle = foc_observer_sensorless_angle();
  156. float mot_angle = foc()->in.mot_angle;
  157. cpu_exit_critical(mask);
  158. float delta = smo_angle - mot_angle;
  159. float s, c;
  160. arm_sin_cos(delta, &s, &c);
  161. delta = fast_atan2(s, c)/PI*180.0f;
  162. can_plot3(mot_angle, smo_angle, delta);
  163. }
  164. #ifdef CONFIG_USE_ENCODER_HALL
  165. static void plot_hall_angle(void) {
  166. u32 mask = cpu_enter_critical();
  167. float hall_angle = hall_get_elec_angle();
  168. float mot_angle = foc()->in.mot_angle;
  169. if (mot_contrl()->if_ctl.b_ena) {
  170. mot_angle += 90;
  171. }
  172. norm_angle_deg(mot_angle);
  173. cpu_exit_critical(mask);
  174. float delta = hall_angle - mot_angle;
  175. float s, c;
  176. arm_sin_cos(delta, &s, &c);
  177. delta = fast_atan2(s, c)/PI*180.0f;
  178. can_plot3(mot_angle, hall_angle, delta);
  179. }
  180. #endif
  181. static u32 app_plot_task(void * args) {
  182. if (plot_type == 1) {
  183. s16 plot_arg1 = (s16)foc_observer_sensorless_speed();
  184. s16 plot_arg2 = (s16)mot_contrl_get_speed(&motor.controller);
  185. if (mot_contrl()->mode_running == CTRL_MODE_SPD) {
  186. if (mc_is_cruise_enabled()) {
  187. plot_arg1 = mot_contrl()->ramp_cruise_vel.target;
  188. }else {
  189. plot_arg1 = mot_contrl()->ramp_target_vel.target;
  190. }
  191. }
  192. can_plot2(plot_arg1, plot_arg2);
  193. }else if (plot_type == 2) {
  194. #ifdef CONFIG_USE_ENCODER_HALL
  195. if (mot_contrl()->if_ctl.b_ena || motor.b_force_run) {
  196. plot_hall_angle();
  197. }else {
  198. can_plot2(mot_contrl_get_final_torque(&motor.controller), mot_contrl()->target_torque);
  199. }
  200. #else
  201. can_plot2(mot_contrl_get_final_torque(&motor.controller), mot_contrl()->target_torque);
  202. #endif
  203. }else if (plot_type == 3) {
  204. plot_smo_angle();
  205. }else if (plot_type == 4) {
  206. can_plot2((s16)foc()->out.curr_dq.d, (s16)foc()->out.curr_dq.q);
  207. }else if (plot_type == 5) {
  208. can_plot2((s16)foc()->in.target_id.interpolation , (s16)foc()->in.target_iq.interpolation);
  209. }else if (plot_type == 6) {
  210. //do it in other place
  211. }else if (plot_type == 7) {
  212. can_plot2((s16)(foc()->in.target_id.target*10.0f), (s16)(foc()->out.curr_dq.d * 10.0f));
  213. }else if (plot_type == 8) {
  214. can_plot2((s16)(foc()->in.target_iq.target*10.0f), (s16)(foc()->out.curr_dq.q * 10.0f));
  215. }else if (plot_type == 9) {
  216. s16 thro_v = get_throttle_float() * 100.0f;
  217. s16 thro2_v = get_throttle2_float() * 100.0f;
  218. s16 sig = throttle_get_signal();
  219. can_plot3(sig, thro_v, thro2_v);
  220. }else if (plot_type == 10) {
  221. s16 duty = (s16)(motor.controller.duty_raw * 100);
  222. s16 duty_filterd = (s16)(motor.controller.duty_filterd * 100);
  223. can_plot2(duty, duty_filterd);
  224. }
  225. return 10;
  226. }
  227. static u32 app_low_task(void *args) {
  228. wdog_reload();
  229. if (!mot_contrl_is_start(&motor.controller)) {
  230. mc_err_block_save();
  231. }
  232. return 1;
  233. }