| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596 |
- #include "foc/core/torque.h"
- #include "foc/foc_config.h"
- #include "foc/motor/motor.h"
- #include "foc/core/e_ctrl.h"
- #include "foc/core/PMSM_FOC_Core.h"
- #include "app/nv_storage.h"
- #include "libs/logger.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();
- 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;
- }
- float throttle_to_speed(float f_throttle) {
- if (f_throttle <= nv_get_foc_params()->n_minThroVol) {
- return 0;
- }
- float delta = f_throttle - (nv_get_foc_params()->n_minThroVol);
- float ration = delta / (nv_get_foc_params()->n_maxThroVol - nv_get_foc_params()->n_minThroVol);
- return (PMSM_FOC_GetSpeedLimit() * ration);
- }
- float throttle_to_torque(float f_throttle) {
- if (f_throttle <= (nv_get_foc_params()->n_minThroVol)) {
- return 0;
- }
- float delta = f_throttle - (nv_get_foc_params()->n_minThroVol);
- float ration = delta / (nv_get_foc_params()->n_maxThroVol - nv_get_foc_params()->n_minThroVol);
- int torque = PMSM_FOC_GetTorqueLimit() * ration * 10.0f;
- return ((float)torque / 10.0f);
- }
- void torque_manager(u8 run_mode, float f_throttle) {
- if ((g_trq_mn.count++ % 20) != 0) {
- return;
- }
- if (run_mode == CTRL_MODE_SPD) {
- float speed_Ref = throttle_to_speed(f_throttle);
- PMSM_FOC_Set_Speed(speed_Ref);
- }else if (run_mode == CTRL_MODE_TRQ) {
- if (mc_throttle_released()) {
- eCtrl_enable_eBrake(true);
- PMSM_FOC_Set_Torque(0);
- g_trq_mn.torque_prev = 0;
- }else {
- float torque = throttle_to_torque(f_throttle);
- eCtrl_enable_eBrake(false);
- PMSM_FOC_Set_Torque(torque);
- g_trq_mn.torque_prev = torque;
- }
- }else if (run_mode == CTRL_MODE_CURRENT_BRK) {
- if (!mc_throttle_released() || (mc_throttle_released() && (PMSM_FOC_GetSpeed() == 0.0f))) {
- eCtrl_enable_eBrake(false);
- }
- }
- }
|