| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134 |
- #include "foc/core/torque.h"
- #include "foc/foc_config.h"
- #include "foc/motor/motor.h"
- #include "foc/motor/motor_param.h"
- #include "foc/core/e_ctrl.h"
- #include "foc/core/PMSM_FOC_Core.h"
- #include "app/nv_storage.h"
- #include "libs/logger.h"
- #include "prot/can_foc_msg.h"
- /*
- 通过查表获取对应扭矩和速度时的Id和IQ的分配
- */
- static torque_lut_t *_trq_tbl = NULL;
- static torque_manager_t g_trq_mn;
- void torque_init(void) {
- _trq_tbl = nv_get_trq_tlb();
- torque_reset();
- }
- void torque_reset(void) {
- memset(&g_trq_mn, 0, sizeof(g_trq_mn));
- }
- void torque_get_idq(float torque, float rpm, DQ_t *dq_out) {
- if ((_trq_tbl == NULL) || (torque < 0 || rpm < 0)) {
- dq_out->d = 0;
- dq_out->q = torque;
- return;
- }
- int trq_idx = (int)torque / TBL_TRQ_INTVAL;
- int rpm_idx = (int)rpm / TBL_SPD_INTVAL;
- if (trq_idx >= MAX_TRQ_POINTS) {
- trq_idx = MAX_TRQ_POINTS -1;
- }
- if (rpm_idx >= MAX_SPD_POINTS) {
- rpm_idx = MAX_SPD_POINTS -1;
- }
- s16 d = _trq_tbl->dq[trq_idx][rpm_idx].d;
- s16 q = _trq_tbl->dq[trq_idx][rpm_idx].q;
- if (trq_idx < MAX_TRQ_POINTS - 1) {
- trq_idx += 1;
- }
- if (rpm_idx < MAX_SPD_POINTS - 1) {
- rpm_idx += 1;
- }
- s16 d_delta = _trq_tbl->dq[trq_idx][rpm_idx].d - d;
- s16 q_delta = _trq_tbl->dq[trq_idx][rpm_idx].q - q;
- float comp_ceof = 0.5f * ((torque - torque/TBL_TRQ_INTVAL*TBL_TRQ_INTVAL)/(float)TBL_TRQ_INTVAL + (rpm - rpm/TBL_SPD_INTVAL*TBL_SPD_INTVAL)/(float)TBL_SPD_INTVAL);
- dq_out->d = d + d_delta * comp_ceof;
- dq_out->q = q + q_delta * comp_ceof;
- }
- /* 获取油门开度 */
- static float throttle_ration(float f_throttle) {
- if (f_throttle <= nv_get_foc_params()->n_minThroVol) {
- return 0;
- }
- float delta = f_throttle - (nv_get_foc_params()->n_minThroVol);
- int ration = (delta * 1000.0f) / (nv_get_foc_params()->n_maxThroVol - nv_get_foc_params()->n_minThroVol);
-
- return ((float)ration)/1000.0f;
- }
- float throttle_to_speed(float f_throttle) {
- return (PMSM_FOC_GetSpeedLimit() * throttle_ration(f_throttle));
- }
- float throttle_to_torque(float f_throttle) {
- return PMSM_FOC_GetTorqueLimit() * throttle_ration(f_throttle);
- }
- #define REAL_DQ_CEOF 0.9f
- #define FINAL_DQ_CEFO 1.1F
- void request_torque(float thro_ration) {
- float curr_rpm = PMSM_FOC_GetSpeed();
- if (curr_rpm == 0) {
- g_trq_mn.spd_filted = 0;
- }else {
- LowPass_Filter(g_trq_mn.spd_filted, curr_rpm, 0.5f);
- }
- float trq_map = (float)get_max_torque_for_rpm((s16)g_trq_mn.spd_filted);
- float trq_lim = PMSM_FOC_GetTorqueLimit();
- float max_trq = min(trq_map, trq_lim);
- float ref_trq = max_trq * thro_ration;
- if ((mc_throttle_released()) && eCtrl_enable_eBrake(true)) {
- g_trq_mn.thro_ration = 0;
- return;
- }
-
- if (!mc_throttle_released()) {
- if (curr_rpm <= CONFIG_ZERO_SPEED_RPM) {
- ref_trq = MAX(eCtrl_get_FinalTorque() * FINAL_DQ_CEFO, ref_trq );
- }
- PMSM_FOC_Set_Torque(ref_trq );
- }else if (mc_throttle_released() && eCtrl_get_FinalTorque() != 0){
- float real_trq = PMSM_FOC_Get_Real_Torque() * REAL_DQ_CEOF;
- float ref_now = min(real_trq, eCtrl_get_RefTorque());
- eCtrl_reset_Torque(ref_now);
- PMSM_FOC_Set_Torque(0);
- g_trq_mn.thro_ration = 0;
- }
- }
- void request_speed(float thro_ration) {
- float speed_Ref = PMSM_FOC_GetSpeedLimit() * thro_ration;
- PMSM_FOC_Set_Speed(speed_Ref);
- }
- #define THRO_REF_LP_CEOF 0.2f
- void throttle_process(u8 run_mode, float f_throttle) {
- float thro_value = throttle_ration(f_throttle);
- g_trq_mn.thro_ration = LowPass_Filter(g_trq_mn.thro_ration, thro_value, THRO_REF_LP_CEOF);
- if (run_mode == CTRL_MODE_TRQ) {
- request_torque(g_trq_mn.thro_ration);
- }else if (run_mode == CTRL_MODE_SPD) {
- request_speed(g_trq_mn.thro_ration);
- }else if (run_mode == CTRL_MODE_CURRENT_BRK) {
- eCtrl_reset_Torque(0);
- if (eCtrl_get_FinalCurrent() < 0.0001f && PMSM_FOC_GetSpeed() < CONFIG_MIN_RPM_EXIT_EBRAKE) {
- eCtrl_enable_eBrake(false);
- }
- if (!mc_throttle_released() || (mc_throttle_released() && (PMSM_FOC_GetSpeed() == 0.0f))) {
- eCtrl_enable_eBrake(false);
- }
- }
- }
|