app.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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/thro_torque.h"
  17. #include "foc/core/F_Calc.h"
  18. #include "foc/motor/motor_param.h"
  19. #include "foc/motor/mot_params_ind.h"
  20. #include "foc/limit.h"
  21. #include "foc/mc_error.h"
  22. #include "foc/mc_config.h"
  23. #include "foc/motor/throttle.h"
  24. #ifdef CONFIG_DQ_STEP_RESPONSE
  25. extern float target_d;
  26. extern float target_q;
  27. #endif
  28. static u32 _app_low_task(void *args);
  29. static u32 _app_report_task(void *args);
  30. static u32 _app_plot_task(void *args);
  31. //static u32 _app_trq_test_task(void *args);
  32. extern void PMSM_FOC_LogDebug(void);
  33. extern void mc_err_code_log(void);
  34. extern void encoder_log(void);
  35. extern void sample_log(void);
  36. extern void thro_torque_log(void);
  37. extern void eCtrl_debug_log(void);
  38. extern bool can_is_connect_pc(void);
  39. extern measure_time_t g_meas_hall;
  40. extern measure_time_t g_meas_foc;
  41. extern measure_time_t g_meas_MCTask;
  42. #if 0
  43. static s16 test_rpm = 0000;
  44. static float test_trq = 0;
  45. static float test_id = 0;
  46. static float test_iq = 0;
  47. static u32 _app_trq_test_task(void *args) {
  48. DQ_t dq;
  49. motor_mpta_fw_lookup(test_rpm, test_trq, &dq);
  50. test_id = dq.d;
  51. test_iq = dq.q;
  52. sys_debug("lookup: %d, %f, %f, %f\n", test_rpm, test_trq, test_id, test_iq);
  53. test_rpm = 8000;
  54. test_trq += 5.0f;
  55. return 500;
  56. }
  57. #endif
  58. void app_start(void){
  59. can_debug(true);
  60. can_message_init();
  61. nv_storage_init();
  62. mc_conf_init();
  63. mc_err_block_init();
  64. mc_init();
  65. shark_task_create(_app_low_task, NULL);
  66. shark_task_create(_app_report_task, NULL);
  67. shark_task_create(_app_plot_task, NULL);
  68. //shark_task_create(_app_trq_test_task, NULL);
  69. sys_debug("mc start\n");
  70. shark_task_run();
  71. }
  72. static u32 _app_report_task(void *p) {
  73. static u32 loop = 0;
  74. can_report_ext_status(0x43);
  75. can_mcast_foc_status2();
  76. if (!can_is_connect_pc()) {
  77. return 200;
  78. }
  79. can_report_power(0x45);
  80. can_report_dq_current(0x45);
  81. can_report_foc_status(0x45);
  82. can_report_phase_voltage(0x45);
  83. can_report_mpta_values(0x45);
  84. can_report_phase_current(0x45);
  85. if (mot_params_rs_ested()) {
  86. can_report_motparam(mot_params_get_est_rs(), R_TYPE);
  87. }
  88. if (mot_params_ld_ested()) {
  89. can_report_motparam(mot_params_get_est_ld(), L_TYPE_D);
  90. }
  91. if (mot_params_lq_ested()) {
  92. can_report_motparam(mot_params_get_est_lq(), L_TYPE_Q);
  93. }
  94. if (mot_params_flux_ested()) {
  95. can_report_motparam(mot_params_get_est_flux(), FLUX_TYPE);
  96. }
  97. if (++loop % 5 == 0) {
  98. //sys_debug("rst 0x%x\n", get_mcu_reset_source());
  99. //sys_debug("Slow: %d - %d\n", g_meas_MCTask.intval_time, g_meas_MCTask.exec_time);
  100. //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);
  101. //sys_debug("FOC time err %d %d\n", g_meas_foc.intval_time_h_error, g_meas_foc.intval_time_l_error);
  102. //sys_debug("acc vol %d, bid %d\n", get_acc_vol(), gpio_board_id());
  103. //sys_debug("throttle %f\n", get_throttle_float());
  104. //sys_debug("ADC Vref %f, %f\n", get_adc_vref(), adc_5vref_compesion());
  105. //sys_debug("dead time %d\n", get_deadtime());
  106. //thro_torque_log();
  107. //sys_debug("_>%f, %f, %f\n", ladrc_observer_get()->ld, ladrc_observer_get()->lq, ladrc_observer_get()->poles);
  108. encoder_log();
  109. //motor_debug();
  110. sample_log();
  111. //PMSM_FOC_LogDebug();
  112. //F_debug();
  113. //eCtrl_debug_log();
  114. //sys_debug("enc err %d, %d\n", foc_observer_enc_errcount(), foc_observer_sensorless_stable());
  115. //mc_err_code_log();
  116. //sys_debug("=====\n");
  117. }
  118. return 200;
  119. }
  120. int plot_type = 0;
  121. static void plot_smo_angle(void) {
  122. #if 1
  123. float smo_angle = foc_observer_sensorless_angle();
  124. float delta = smo_angle - PMSM_FOC_Get()->in.s_motAngle;
  125. float s, c;
  126. arm_sin_cos_f32(delta, &s, &c);
  127. delta = fast_atan2(s, c)/PI*180.0f;
  128. can_plot3(PMSM_FOC_Get()->in.s_motAngle, smo_angle, delta);
  129. #else
  130. can_plot2((s16)(foc_observer_sensorless_diff() * 100.0f), (s16)(foc_observer_sensorless_ration() * 100.0f));
  131. #endif
  132. }
  133. static u32 _app_plot_task(void * args) {
  134. if (plot_type == 1) {
  135. can_plot2((s16)foc_observer_sensorless_speed(), (s16)PMSM_FOC_GetSpeed());
  136. //can_plot3((s16)PMSM_FOC_GetSpeed() + 1000, (s16)PMSM_FOC_GetSpeed(), (s16)PMSM_FOC_GetSpeed() - 1000);
  137. }else if (plot_type == 2) {
  138. can_plot2(eCtrl_get_FinalTorque(), PMSM_FOC_Get()->in.s_targetTorque);
  139. }else if (plot_type == 3) {
  140. plot_smo_angle();
  141. }else if (plot_type == 4) {
  142. can_plot2((s16)PMSM_FOC_Get()->out.s_RealIdq.d, (s16)PMSM_FOC_Get()->out.s_RealIdq.q);
  143. }else if (plot_type == 5) {
  144. can_plot2((s16)PMSM_FOC_Get()->idq_ctl[0].s_Cp, (s16)PMSM_FOC_Get()->idq_ctl[1].s_Cp);
  145. }else if (plot_type == 6) {
  146. //do it in other place
  147. }else if (plot_type == 7) {
  148. #ifdef CONFIG_DQ_STEP_RESPONSE
  149. can_plot2((s16)(target_d*10.0f), (s16)(PMSM_FOC_Get()->out.s_RealIdq.d * 10.0f));
  150. #endif
  151. }else if (plot_type == 8) {
  152. #ifdef CONFIG_DQ_STEP_RESPONSE
  153. can_plot2((s16)(target_q*10.0f), (s16)(PMSM_FOC_Get()->out.s_RealIdq.q * 10.0f));
  154. #endif
  155. }else if (plot_type == 9) {
  156. s16 thro_v = get_throttle_float() * 100.0f;
  157. s16 thro2_v = get_throttle2_float() * 100.0f;
  158. s16 sig = throttle_get_signal();
  159. can_plot3(sig, thro_v, thro2_v);
  160. }
  161. return 20;
  162. }
  163. static u32 _app_low_task(void *args) {
  164. wdog_reload();
  165. if (!PMSM_FOC_Is_Start()) {
  166. mc_err_block_save();
  167. }
  168. return 1;
  169. }