| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639 |
- #include "foc/motor/motor.h"
- #include "foc/motor/current.h"
- #include "foc/foc_config.h"
- #include "foc/mc_error.h"
- #include "foc/samples.h"
- #include "math/fast_math.h"
- #include "bsp/bsp_driver.h"
- #include "libs/time_measure.h"
- #include "foc/commands.h"
- #include "libs/logger.h"
- #include "foc/samples.h"
- #include "foc/motor/motor_param.h"
- #include "foc/core/foc_observer.h"
- #include "foc/core/F_Calc.h"
- #include "foc/core/etcs.h"
- #include "app/nv_storage.h"
- #include "foc/motor/mot_params_ind.h"
- #include "foc/motor/throttle.h"
- #include "foc/limit.h"
- extern u32 enc_pwm_err_ms;
- extern s16 enc_delta_err1, enc_delta_err2;
- static bool mc_detect_hwbrake(void);
- static bool mc_is_gpio_mlock(void);
- static void _pwm_brake_prot_timer_handler(shark_timer_t *);
- static shark_timer_t _brake_prot_timer = TIMER_INIT(_brake_prot_timer, _pwm_brake_prot_timer_handler);
- static void _fan_det_timer_handler(shark_timer_t *);
- static shark_timer_t _fan_det_timer1 = TIMER_INIT(_fan_det_timer1, _fan_det_timer_handler);
- static shark_timer_t _fan_det_timer2 = TIMER_INIT(_fan_det_timer2, _fan_det_timer_handler);
- static void _encoder_zero_off_timer_handler(shark_timer_t *);
- static shark_timer_t _encoder_zero_off_timer = TIMER_INIT(_encoder_zero_off_timer, _encoder_zero_off_timer_handler);
- static void _led_off_timer_handler(shark_timer_t *);
- static shark_timer_t _led_off_timer = TIMER_INIT(_led_off_timer, _led_off_timer_handler);
- m_contrl_t motor = {
- .s_direction = POSITIVE,
- .n_gear = 0,
- .b_high_vol_mode = false,
- .mode = CTRL_MODE_OPEN,
- .mos_lim = 0,
- .motor_lim = 0,
- .b_ind_start = false,
- .s_target_speed = MAX_S16,
- .s_force_torque = MAX_S16,
- .u_set.idc_lim = IDC_USER_LIMIT_NONE,
- .u_set.rpm_lim = RPM_USER_LIMIT_NONE,
- .u_set.ebrk_lvl = 0,
- .u_set.n_brkShutPower = CONFIG_Settings_BrkShutPower,
- .u_set.b_tcs = CONFIG_Settings_TcsEnable,
- };
- /* 无感运行的挡位,限制速度,母线电流,最大扭矩 */
- static gear_t sensorless_gear = {
- .max_speed = CONFIG_SENSORLESS_MAX_SPEED,
- .max_torque = CONFIG_SENSORLESS_MAX_TORQUE,
- .max_idc = CONFIG_SENSORLESS_MAX_IDC,
- .zero_accl = 1500,
- .accl_time = 1500,
- .torque = {100, 80, 60, 50, 40, 30, 0, 0, 0, 0},
- };
- static runtime_node_t mc_error;
- static void MC_Check_MosVbusThrottle(void) {
- int count = 1000;
- float ibus_adc = 0;
- float vref_adc = 0;
- float vref_5v_adc = 0;
- gpio_phase_u_detect(true);
- while(count-- > 0) {
- task_udelay(20);
- sample_uvw_phase();
- sample_throttle();
- sample_vbus();
- vref_adc += adc_get_vref();
- vref_5v_adc += adc_get_5v_ref();
- }
- adc_set_vref_calc(vref_adc/1000.0f);
- adc_set_5vref_calc(vref_5v_adc/1000.0f);
- sys_debug("5v ref %f\n", vref_5v_adc/1000.0f);
- count = 50;
- while(count-- >0) {
- task_udelay(300);
- ibus_adc += adc_get_ibus();
- }
- u16 offset = ((float)ibus_adc)/50.0f;
- sys_debug("ibus offset %d\n", offset);
- sample_ibus_offset(offset);
- gpio_phase_u_detect(false);
- float abc[3];
- get_phase_vols_filtered(abc);
- int vbus_vol = get_vbus_int();
- if (vbus_vol > mc_conf()->c.max_dc_vol) {
- mc_set_critical_error(FOC_CRIT_OV_Vol_Err);
- }
- if (vbus_vol <= mc_conf()->c.min_dc_vol) {
- mc_set_critical_error(FOC_CRIT_UN_Vol_Err);
- }
- vbus_vol = get_acc_vol();
- sys_debug("acc vol %d\n", vbus_vol);
- if (vbus_vol >= mc_conf()->c.max_dc_vol) {
- mc_set_critical_error(FOC_CRIT_ACC_OV_Err);
- }
- if (vbus_vol <= CONFIG_ACC_MIN_VOL) {
- mc_set_critical_error(FOC_CRIT_ACC_Un_Err);
- }
- if (throttle_is_all_error()) {
- if (!motor.b_ignor_throttle) {
- mc_set_critical_error(FOC_CRIT_THRO_Err);
- mc_set_critical_error(FOC_CRIT_THRO2_Err);
- }
- }
- if (abc[0] > vbus_vol/2 || abc[1] > vbus_vol/2 || abc[2] > vbus_vol/2) {
- mc_set_critical_error(FOC_CRIT_H_MOS_Err);
- }else if (abc[0] < 0.001f){
- mc_set_critical_error(FOC_CRIT_L_MOS_Err);
- }else if ((abc[0] > 0.5f) && (abc[1] < 0.001f || abc[2] < 0.001f)) {
- mc_set_critical_error(FOC_CRIT_Phase_Conn_Err);
- }
- sys_debug("phase vol %f, %f, %f\n", abc[0], abc[1], abc[2]);
- }
- static int hw_brk_err_cnt = 0;
- static int hw_brk_no_err_cnt = 0;
- static u32 _self_check_task(void *p) {
- if (get_tick_ms() < 500) { //启动500ms内检测刹车状态是否正常,应该是没有刹车
- if (mc_detect_hwbrake() && hw_brk_err_cnt++ >= 60) {
- mc_set_critical_error(FOC_CRIT_BRK_Err);
- }
- }else {
- if (mc_critical_err_is_set(FOC_CRIT_BRK_Err) && !mc_detect_hwbrake()) {
- if (hw_brk_no_err_cnt++ >= 100) {
- mc_clr_critical_error(FOC_CRIT_BRK_Err);
- }
- }else {
- hw_brk_no_err_cnt = 0;
- }
- }
- if (ENC_Check_error()) {
- mc_crit_err_add_s16(FOC_CRIT_Encoder_Err, -1);
- mc_set_critical_error(FOC_CRIT_Encoder_Err);
- }
- if (get_tick_ms() < 5000) { //启动后5s内检测锁电机线
- if (mc_is_gpio_mlock()) {
- mc_lock_motor(true);
- }
- }
- if (motor.b_lock_motor) {
- if (!mc_is_gpio_mlock()) {
- mc_lock_motor(false);
- }
- }
- if (!motor.b_ignor_throttle) {
- if (throttle_is_all_error()) {
- mc_set_critical_error(FOC_CRIT_THRO_Err);
- mc_set_critical_error(FOC_CRIT_THRO2_Err);
- }else if (throttle1_is_error()) {
- mc_set_critical_error(FOC_CRIT_THRO_Err);
- }else if (throttle2_is_error()) {
- mc_set_critical_error(FOC_CRIT_THRO2_Err);
- }
- }
- if (fan_pwm_is_running()) {
- #ifdef GPIO_FAN1_IN_GROUP
- if ((get_delta_ms(motor.fan[0].start_ts) >= 5000) && (motor.fan[0].rpm == 0)) {
- mc_set_critical_error(FOC_CRIT_Fan_Err);
- }else if (motor.fan[0].rpm > 0) {
- mc_clr_critical_error(FOC_CRIT_Fan_Err);
- }
- #endif
- }
- return 5;
- }
- static bool mc_detect_vbus_mode(void) {
- #ifdef CONFIG_FORCE_HIGH_VOL_MODE
- motor.b_high_vol_mode = true;
- return false;
- #else
- bool is_96mode = motor.b_high_vol_mode;
- motor.b_high_vol_mode = get_vbus_int() >= CONFIG_HIGH_VOL_MODE_MIN_VOL;
- return (is_96mode != motor.b_high_vol_mode);
- #endif
- }
- static void _mc_internal_init(u8 mode, bool start) {
- motor.mode = mode;
- motor.throttle = 0;
- motor.b_start = start;
- motor.b_runStall = false;
- motor.runStall_time = 0;
- motor.b_epm = false;
- motor.b_epm_cmd_move = false;
- motor.epm_dir = EPM_Dir_None;
- motor.n_autohold_time = 0;
- motor.b_auto_hold = 0;
- motor.b_break = false;
- motor.b_wait_brk_release = false;
- motor.b_force_run = false;
- motor.b_cruise = false;
- motor.b_limit_pending = false;
- motor.f_epm_trq = 0;
- motor.f_epm_vel = 0;
- motor.s_vbus_hw_min = mc_conf()->c.min_dc_vol;
- }
- static void _led_off_timer_handler(shark_timer_t *t) {
- gpio_led_enable(false);
- }
- static void mc_gear_mode_set(void) {
- gear_t *gears = mc_gear_conf();
- if (gears != &sensorless_gear) {
- sensorless_gear.max_torque = gears->max_torque;
- }else { //slowly changed
- line_ramp_set_time(&motor.controller.vel_lim, CONFIG_SENSORLESS_RAMP_TIME);
- line_ramp_set_time(&motor.controller.dc_curr_lim, CONFIG_SENSORLESS_RAMP_TIME);
- }
- mot_contrl_set_vel_limit(&motor.controller, (float)min(gears->max_speed, motor.u_set.rpm_lim));
- mot_contrl_set_dccurr_limit(&motor.controller, (float)min(gears->max_idc, motor.u_set.idc_lim));
- mot_contrl_set_torque_limit(&motor.controller, (float)gears->max_torque);
- }
- void mc_init(void) {
- fan_pwm_init();
- adc_init();
- pwm_3phase_init();
- samples_init();
- motor_encoder_init();
- foc_command_init();
- throttle_init();
- mc_detect_vbus_mode();
- mot_contrl_init(&motor.controller);
- mc_gpio_init();
- MC_Check_MosVbusThrottle();
- sched_timer_enable(CONFIG_SPD_CTRL_US);
- shark_task_create(_self_check_task, NULL);
- pwm_up_enable(true);
- gpio_led_enable(true);
- shark_timer_post(&_led_off_timer, 5000);
- }
- m_contrl_t * mc_params(void) {
- return &motor;
- }
- gear_t *mc_gear_conf_by_gear(u8 n_gear) {
- gear_t *gears;
- if (!foc_observer_is_encoder()) { //无感模式,受限运行
- return &sensorless_gear;
- }
- if (motor.b_high_vol_mode) {
- gears = &mc_conf()->g_n[0];
- }else {
- gears = &mc_conf()->g_l[0];
- }
- return &gears[n_gear];
- }
- gear_t *mc_gear_conf(void) {
- return mc_gear_conf_by_gear(motor.n_gear);
- }
- static __INLINE float gear_rpm_2_torque(u8 torque, s16 max) {
- return (float)torque/100.0f * max;
- }
- float mc_gear_max_torque(s16 vel, u8 gear_n) {
- gear_t *gear = mc_gear_conf_by_gear(gear_n);
- if (gear == NULL) {
- return 0;
- }
- vel = ABS(vel);
- if (vel <= 1000) {
- return gear_rpm_2_torque(gear->torque[0], gear->max_torque);
- }
- int vel_idx = vel / 1000;
- if (vel_idx >= CONFIG_GEAR_SPEED_TRQ_NUM -1 ) {
- return gear_rpm_2_torque(gear->torque[CONFIG_GEAR_SPEED_TRQ_NUM-1], gear->max_torque);
- }
- float torque_1 = gear_rpm_2_torque(gear->torque[vel_idx-1], gear->max_torque);
- float min_rpm = vel_idx * 1000;
- float torque_2 = gear_rpm_2_torque(gear->torque[vel_idx], gear->max_torque);
- float max_rpm = min_rpm + 1000;
- return f_map((float)vel, min_rpm, max_rpm, torque_1, torque_2);
- }
- /* 必须立即停机 */
- bool mc_critical_need_stop(void) {
- u32 mask = FOC_Cri_Err_Mask(FOC_CRIT_IDC_OV) | FOC_Cri_Err_Mask(FOC_CRIT_Angle_Err) | FOC_Cri_Err_Mask(FOC_CRIT_Vol_HW_Err);
- u32 err = motor.n_CritiCalErrMask & mask;
- return (err != 0);
- }
- bool mc_critical_can_not_run(void) {
- u32 mask = FOC_Cri_Err_Mask(FOC_CRIT_MOTOR_TEMP_Lim) | FOC_Cri_Err_Mask(FOC_CRIT_MOS_TEMP_Lim);
- u32 err = motor.n_CritiCalErrMask & mask;
- bool crit_err = (err != 0) || mc_critical_need_stop();
- if (!motor.b_ignor_throttle) {
- crit_err = crit_err || throttle_is_all_error();
- }
- return crit_err;
- }
- bool mc_unsafe_critical_error(void) {
- u32 err = motor.n_CritiCalErrMask & (~(FOC_Cri_Err_Mask(FOC_CRIT_Fan_Err)));
- #ifdef CONFIG_DQ_STEP_RESPONSE
- sys_debug("err=0x%x\n", err);
- err = err & (~(FOC_Cri_Err_Mask(FOC_CRIT_Encoder_Err)));
- err = err & (~(FOC_Cri_Err_Mask(FOC_CRIT_THRO_Err)));
- err = err & (~(FOC_Cri_Err_Mask(FOC_CRIT_THRO2_Err)));
- sys_debug("err=0x%x\n", err);
- #else
- if (!throttle_is_all_error()) {//不是两个转把都有问题,忽略出问题的转把错误
- if (throttle1_is_error()) {
- err = err & (~(FOC_Cri_Err_Mask(FOC_CRIT_THRO_Err)));
- }else if (throttle2_is_error()) {
- err = err & (~(FOC_Cri_Err_Mask(FOC_CRIT_THRO2_Err)));
- }
- }
- #endif
- if (motor.b_ignor_throttle) {
- err = err & (~(FOC_Cri_Err_Mask(FOC_CRIT_THRO_Err)));
- err = err & (~(FOC_Cri_Err_Mask(FOC_CRIT_THRO2_Err)));
- }
- return (err != 0);
- }
- bool mc_start(u8 mode) {
- if (motor.b_start) {
- return true;
- }
- #ifdef CONFIG_DQ_STEP_RESPONSE
- mode = CTRL_MODE_CURRENT;
- #endif
- motor.s_force_torque = MAX_S16;
- mc_detect_vbus_mode();
- etcs_enable(&motor.controller.etcs, motor.u_set.b_tcs);
- if (motor.b_lock_motor) {
- mot_contrl_set_error(&motor.controller, FOC_NotAllowed);
- return false;
- }
- MC_Check_MosVbusThrottle();
- if (mc_unsafe_critical_error()) {
- mot_contrl_set_error(&motor.controller, FOC_Have_CritiCal_Err);
- return false;
- }
- if (mode > CTRL_MODE_CURRENT) {
- mot_contrl_set_error(&motor.controller, FOC_Param_Err);
- return false;
- }
- if (mot_contrl_get_speed(&motor.controller) > CONFIG_ZERO_SPEED_RPM) {
- mot_contrl_set_error(&motor.controller, FOC_NowAllowed_With_Speed);
- return false;
- }
- if (!mc_throttle_released()) {
- mot_contrl_set_error(&motor.controller, FOC_Throttle_Err);
- return false;
- }
-
- pwm_up_enable(false);
- u32 mask = cpu_enter_critical();
- _mc_internal_init(mode, true);
- throttle_torque_reset();
- motor_encoder_start(true);
- mot_contrl_start(&motor.controller, mode);
- mot_contrl_set_torque_ramp_time(&motor.controller, mc_gear_conf()->zero_accl, mc_conf()->c.thro_dec_time);
- mc_set_ebrk_level(motor.u_set.ebrk_lvl);
- mc_gear_mode_set();
- cpu_exit_critical(mask);
- pwm_turn_on_low_side();
- delay_ms(10);
- phase_current_offset_calibrate();
- pwm_start();
- delay_us(10); //wait for ebrake error
- if (mc_unsafe_critical_error()) {
- mc_stop();
- return false;
- }
- adc_start_convert();
- phase_current_calibrate_wait();
- if (phase_curr_offset_check()) {
- mot_contrl_set_error(&motor.controller, FOC_CRIT_CURR_OFF_Err);
- mc_stop();
- return false;
- }
- if (mc_detect_hwbrake()) {
- mot_contrl_set_hw_brake(&motor.controller, true);
- }
- return true;
- }
- bool mc_stop(void) {
- if (!motor.b_start) {
- return true;
- }
- if (motor.b_lock_motor) {
- mot_contrl_set_error(&motor.controller, FOC_NotAllowed);
- return false;
- }
- if (mot_contrl_get_speed(&motor.controller) > CONFIG_ZERO_SPEED_RPM) {
- mot_contrl_set_error(&motor.controller, FOC_NowAllowed_With_Speed);
- return false;
- }
- if (!mc_throttle_released()) {
- mot_contrl_set_error(&motor.controller, FOC_Throttle_Err);
- return false;
- }
- u32 mask = cpu_enter_critical();
- _mc_internal_init(CTRL_MODE_OPEN, false);
- adc_stop_convert();
- pwm_stop();
- mot_contrl_stop(&motor.controller);
- motor_encoder_start(false);
- pwm_up_enable(true);
- cpu_exit_critical(mask);
- return true;
- }
- void mc_set_mos_lim_level(u8 l) {
- if (motor.mos_lim != l) {
- mc_crit_err_add(FOC_EV_MOS_Limit_L, l, get_mos_temp_raw());
- }
- motor.mos_lim = l;
- }
- void mc_set_motor_lim_level(u8 l) {
- if (motor.motor_lim != l) {
- mc_crit_err_add(FOC_EV_MOT_Limit_L, l, get_motor_temp_raw());
- }
- motor.motor_lim = l;
- }
- bool mc_set_gear(u8 gear) {
- if (gear >= CONFIG_MAX_GEARS) {
- mot_contrl_set_error(&motor.controller, FOC_Param_Err);
- return false;
- }
- if (motor.n_gear != gear) {
- u32 mask = cpu_enter_critical();
- motor.n_gear = gear;
- mc_gear_mode_set();
- cpu_exit_critical(mask);
- }
- return true;
- }
- u8 mc_get_gear(void) {
- if (motor.n_gear == 3){
- return 0;
- }
- return motor.n_gear + 1;
- }
- u8 mc_get_internal_gear(void) {
- return motor.n_gear;
- }
- bool mc_hwbrk_can_shutpower(void) {
- if (motor.u_set.n_brkShutPower != MAX_U8) {
- return (motor.u_set.n_brkShutPower != 0);
- }
- return (mc_conf()->s.brk_shut_power != 0);
- }
- bool mc_enable_cruise(bool enable) {
- if (enable == motor.b_cruise) {
- return true;
- }
- if (mot_contrl_set_cruise(&motor.controller, enable)) {
- motor.b_cruise = enable;
- motor.cruise_time = enable?shark_get_seconds():0;
- motor.cruise_torque = 0.0f;
- return true;
- }
- return false;
- }
- bool mc_set_target_vel(s16 vel) {
- if (motor.mode != CTRL_MODE_SPD || mc_is_epm()) {
- return false;
- }
- motor.s_target_speed = vel;
- return true;
- }
- bool mc_set_force_torque(s16 torque) {
- if (motor.mode != CTRL_MODE_TRQ) {
- return false;
- }
- if (torque == MAX_S16) {
- motor.s_force_torque = MAX_S16;
- }else {
- motor.s_force_torque = fclamp(torque, -mc_gear_conf()->max_torque, mc_gear_conf()->max_torque);
- }
- return true;
- }
- bool mc_is_cruise_enabled(void) {
- return motor.b_cruise;
- }
- bool mc_set_cruise_speed(bool rpm_abs, float target_rpm) {
- bool ret;
- if (rpm_abs) {
- ret = mot_contrl_set_cruise_speed(&motor.controller, target_rpm);
- }else {
- ret = mot_contrl_set_cruise_speed(&motor.controller, mot_contrl_get_speed(&motor.controller) + target_rpm);
- }
- if (ret) {
- motor.cruise_time = shark_get_seconds();
- motor.cruise_torque = 0.0f;
- }
- return ret;
- }
- void mc_set_idc_limit(s16 limit) {
- if ((limit == motor.u_set.idc_lim) || (limit < 0)) {
- return;
- }
- motor.u_set.idc_lim = limit;
- mc_gear_mode_set();
- }
- void mc_set_rpm_limit(s16 limit) {
- if ((limit == motor.u_set.rpm_lim) || (limit < 0)) {
- return;
- }
- motor.u_set.rpm_lim = limit;
- mc_gear_mode_set();
- }
- bool mc_set_ebrk_level(u8 level) {
- if (level < CONFIG_EBRK_LVL_NUM) {
- motor.u_set.ebrk_lvl = level;
- mot_contrl_set_ebrk_time(&motor.controller, mc_get_ebrk_time());
- mot_contrl_set_ebrk_torquer(&motor.controller, mc_get_ebrk_torque());
- return true;
- }
- return false;
- }
- void mc_enable_brkshutpower(u8 shut) {
- motor.u_set.n_brkShutPower = shut;
- }
- void mc_enable_tcs(bool enable) {
- motor.u_set.b_tcs = enable;
- etcs_enable(&motor.controller.etcs, enable);
- }
- s16 mc_get_ebrk_torque(void) {
- if (motor.u_set.ebrk_lvl < CONFIG_EBRK_LVL_NUM) {
- return min(mc_conf()->e_r[motor.u_set.ebrk_lvl].torque, mc_conf()->c.max_ebrk_torque);
- }
- return 0;
- }
- u16 mc_get_ebrk_time(void) {
- if (motor.u_set.ebrk_lvl < CONFIG_EBRK_LVL_NUM) {
- return (u16)mc_conf()->e_r[motor.u_set.ebrk_lvl].time;
- }
- return 0xFFFF;
- }
- bool mc_set_foc_mode(u8 mode) {
- if (mode == motor.mode) {
- return true;
- }
- if (!motor.b_start && !motor.b_ind_start) {
- return false;
- }
- if (mc_critical_can_not_run()) {
- return false;
- }
- if ((mode == CTRL_MODE_OPEN) && (ABS(mot_contrl_get_speed(&motor.controller)) > CONFIG_ZERO_SPEED_RPM)) {
- mot_contrl_set_error(&motor.controller, FOC_NotAllowed);
- return false;
- }
- u32 mask = cpu_enter_critical();
- bool ret = false;
- if (mot_contrl_request_mode(&motor.controller, mode)) {
- motor.mode = mode;
- if ((mode == CTRL_MODE_OPEN || mode == CTRL_MODE_CURRENT) && !mot_contrl_is_start(&motor.controller)) {
- mot_contrl_start(&motor.controller, motor.mode);
- pwm_enable_channel();
- }
- ret = true;
- }
- cpu_exit_critical(mask);
- return ret;
- }
- bool mc_start_epm(bool epm) {
- if (motor.b_epm == epm) {
- return true;
- }
- if (!motor.b_start) {
- mot_contrl_set_error(&motor.controller, FOC_NotAllowed);
- return false;
- }
- if (motor_encoder_get_speed() > CONFIG_ZERO_SPEED_RPM) {
- mot_contrl_set_error(&motor.controller, FOC_NowAllowed_With_Speed);
- return false;
- }
- if (!mc_throttle_released()) {
- mot_contrl_set_error(&motor.controller, FOC_Throttle_Err);
- return false;
- }
- u32 mask = cpu_enter_critical();
- motor.b_epm = epm;
- motor.f_epm_vel = 0.0f;
- motor.f_epm_trq = 0.0f;
- motor_encoder_band_epm(epm);
- if (epm) {
- mot_contrl_set_target_vel(&motor.controller, 0);
- motor.mode = CTRL_MODE_SPD;
- motor.epm_dir = EPM_Dir_None;
- mot_contrl_set_torque_limit(&motor.controller, mc_conf()->c.max_epm_torque);
- mot_contrl_request_mode(&motor.controller, CTRL_MODE_SPD);
- }else {
- motor.epm_dir = EPM_Dir_None;
- motor.mode = CTRL_MODE_TRQ;
- motor.b_epm_cmd_move = false;
- mot_contrl_request_mode(&motor.controller, CTRL_MODE_TRQ);
- mot_contrl_set_torque_limit_rttime(&motor.controller, CONFIG_LIMIT_RAMP_TIME);
- mot_contrl_set_vel_rttime(&motor.controller, CONFIG_CRUISE_RAMP_TIME);
- mc_gear_mode_set();
- }
- cpu_exit_critical(mask);
-
- return false;
- }
- bool mc_is_epm(void) {
- return motor.b_epm;
- }
- bool mc_is_start(void) {
- return (motor.b_start || mot_contrl_is_start(&motor.controller));
- }
- bool mc_start_epm_move(epm_dir_t dir, bool is_command) {
- if (!motor.b_epm || !motor.b_start) {
- mot_contrl_set_error(&motor.controller, FOC_NotAllowed);
- return false;
- }
- if ((dir == motor.epm_dir) && (is_command == motor.b_epm_cmd_move)) {
- return true;
- }
- u32 mask = cpu_enter_critical();
- if (motor.epm_dir != dir) {
- motor.f_epm_vel = 0.0f;
- motor.f_epm_trq = 0.0f;
- }
- motor.epm_dir = dir;
- if (dir != EPM_Dir_None) {
- motor.b_epm_cmd_move = is_command;
-
- if (!mot_contrl_is_start(&motor.controller)) {
- mot_contrl_start(&motor.controller, motor.mode);
- mc_gear_mode_set();
- pwm_enable_channel();
- }else if (mot_contrl_is_auto_holdding(&motor.controller)) {
- mc_auto_hold(false);
- }
- if (dir == EPM_Dir_Back) {
- mot_contrl_velloop_params(&motor.controller, 0.2f, 7.5f);
- }else {
- mot_contrl_velloop_params(&motor.controller, 0.2f, 7.5f);
- }
- mot_contrl_set_torque_limit_rttime(&motor.controller, 2.0);
- mot_contrl_set_torque_limit(&motor.controller, motor.f_epm_trq);
- mot_contrl_set_vel_rttime(&motor.controller, 1000.0f);
- mot_contrl_set_target_vel(&motor.controller, motor.f_epm_vel);
- }else {
- motor.b_epm_cmd_move = false;
- mot_contrl_velloop_params(&motor.controller, mc_conf()->c.pid[PID_Vel_ID].kp, mc_conf()->c.pid[PID_Vel_ID].ki);
- mot_contrl_set_target_vel(&motor.controller, 0);
- }
- cpu_exit_critical(mask);
- return true;
- }
- void mc_set_fan_duty(u8 duty) {
- sys_debug("fan duty %d\n", duty);
- if (!fan_pwm_is_running() && duty > 0) {
- motor.fan[0].start_ts = get_tick_ms();
- motor.fan[1].start_ts = get_tick_ms();
- shark_timer_post(&_fan_det_timer1, 5000);
- shark_timer_post(&_fan_det_timer2, 5000);
- }else if (duty == 0) {
- shark_timer_cancel(&_fan_det_timer1);
- shark_timer_cancel(&_fan_det_timer2);
- }
- fan_set_duty(duty);
- }
- bool mc_command_epm_move(epm_dir_t dir) {
- return mc_start_epm_move(dir, true);
- }
- bool mc_throttle_epm_move(epm_dir_t dir) {
- return mc_start_epm_move(dir, false);
- }
- void mc_set_throttle_r(bool use, u8 r) {
- motor.u_throttle_ration = r;
- motor.b_ignor_throttle = use;
- if (motor.b_ignor_throttle) {
- mc_clr_critical_error(FOC_CRIT_THRO_Err);
- mc_clr_critical_error(FOC_CRIT_THRO2_Err);
- }
- }
- void mc_use_throttle(void) {
- motor.b_ignor_throttle = false;
- }
- void mc_get_running_status(u8 *data) {
- data[0] = motor.mode;
- data[0] |= (mot_contrl_is_auto_holdding(&motor.controller)?1:0) << 2;
- data[0] |= (motor.b_break?1:0) << 3;
- data[0] |= (motor.b_cruise?1:0) << 4;
- data[0] |= (motor.b_start?1:0) << 5;
- data[0] |= (mc_is_epm()?1:0) << 6;
- data[0] |= (motor.b_lock_motor) << 7; //motor locked
- }
- u16 mc_get_running_status2(void) {
- u16 data = 0;
- data = motor.b_start?1:0;
- data |= (motor.n_gear & 0x7) << 1;
- data |= (mot_contrl_is_auto_holdding(&motor.controller)?1:0) << 3;
- data |= (motor.b_break?1:0) << 4;
- data |= (motor.b_cruise?1:0) << 5;
- data |= (mc_is_epm()?1:0) << 6;
- data |= (motor.b_lock_motor) << 7; //motor locked
- data |= (mot_contrl_ebrk_is_running(&motor.controller)?1:0) << 8; //能量回收运行标志
- data |= ((motor.n_CritiCalErrMask != 0)?1:0) << 9;
- data |= (fan_pwm_is_running()?1:0) << 10; //风扇是否运行
- data |= (motor.b_runStall?1:0) << 11; //是否堵转
- data |= (mot_contrl_dccurr_is_protected(&motor.controller)?1:0) << 12; //是否欠压限制母线电流
- data |= (mot_contrl_torque_is_protected(&motor.controller)?1:0) << 13; //是否高温限扭矩
- data |= (etcs_is_running(&motor.controller.etcs)?1:0) << 14; //电子tcs是否正在工作
- data |= (throttle_not_released_err()?1:0) << 15;
- return data;
- }
- static float _force_angle = 0.0f;
- static int _force_wait = 2000;
- /* 开环,强制给定电角度和DQ的电压 */
- void mc_force_run_open(s16 vd, s16 vq, bool align) {
- if (motor.b_start || motor.b_force_run) {
- if (vd == 0 && vq == 0) {
- mot_contrl_set_vdq(&motor.controller, 0, 0);
- delay_ms(500);
- wdog_reload();
- adc_stop_convert();
- pwm_stop();
- mot_contrl_stop(&motor.controller);
- pwm_up_enable(true);
- motor.b_force_run = false;
- motor.b_ignor_throttle = false;
- }
- return;
- }
- if (vd == 0 && vq == 0) {
- return;
- }
- motor.b_ignor_throttle = true;
- MC_Check_MosVbusThrottle();
- if (mc_unsafe_critical_error()) {
- mot_contrl_set_error(&motor.controller, FOC_Have_CritiCal_Err);
- }
-
- pwm_up_enable(false);
- pwm_turn_on_low_side();
- task_udelay(500);
- mot_contrl_start(&motor.controller, CTRL_MODE_OPEN);
- phase_current_offset_calibrate();
- pwm_start();
- adc_start_convert();
- pwm_enable_channel();
- phase_current_calibrate_wait();
- mot_contrl_set_angle(&motor.controller, 0);
- mot_contrl_set_vdq(&motor.controller, (float)vd, 0);
- if (align) {
- _force_wait = 2000 + 1;
- }else {
- _force_wait = 2000;
- }
- motor.b_force_run = true;
- }
- bool mc_ind_motor_start(bool start) {
- if (start == motor.b_ind_start) {
- return true;
- }
- if (start) {
- motor.b_ignor_throttle = true;
- MC_Check_MosVbusThrottle();
- if (mc_unsafe_critical_error() || mot_params_flux_pending()) {
- mot_contrl_set_error(&motor.controller, FOC_Have_CritiCal_Err);
- return false;
- }
- pwm_up_enable(false);
- pwm_turn_on_low_side();
- task_udelay(500);
- mot_contrl_start(&motor.controller, CTRL_MODE_OPEN);
- phase_current_offset_calibrate();
- pwm_start();
- adc_start_convert();
- pwm_enable_channel();
- phase_current_calibrate_wait();
- mot_contrl_set_vdq_immediate(&motor.controller, 0, 0);
- motor.b_ind_start = start;
- }else {
- u32 mask = cpu_enter_critical();
- motor.b_ind_start = start;
- mot_contrl_set_vdq(&motor.controller, 0, 0);
- cpu_exit_critical(mask);
- delay_us(500);
- wdog_reload();
- adc_stop_convert();
- pwm_stop();
- mot_contrl_stop(&motor.controller);
- motor.mode = CTRL_MODE_OPEN;
- pwm_up_enable(true);
- motor.b_ignor_throttle = false;
- }
-
- return true;
- }
- static void _encoder_zero_off_timer_handler(shark_timer_t *t){
- if (!motor.b_calibrate) {
- return;
- }
- float enc_off = 0.0f;
- float phase = motor_encoder_zero_phase_detect(&enc_off);
- mot_contrl_set_vdq(&motor.controller, 0, 0);
- delay_ms(50);
- adc_stop_convert();
- pwm_stop();
- mot_contrl_stop(&motor.controller);
- _mc_internal_init(CTRL_MODE_OPEN, false);
- motor.b_calibrate = false;
- }
- bool mc_encoder_zero_calibrate(s16 vd) {
- if (motor.b_calibrate) {
- if (vd == 0) {
- encoder_clear_cnt_offset();
- shark_timer_cancel(&_encoder_zero_off_timer);
- mot_contrl_set_vdq(&motor.controller, 0, 0);
- delay_ms(500);
- adc_stop_convert();
- pwm_stop();
- mot_contrl_stop(&motor.controller);
- _mc_internal_init(CTRL_MODE_OPEN, false);
- motor.b_calibrate = false;
- motor.b_ignor_throttle = false;
- }
- return true;
- }
- encoder_clear_cnt_offset();
- motor.b_ignor_throttle = true;
- MC_Check_MosVbusThrottle();
- if (mc_unsafe_critical_error()) {
- mot_contrl_set_error(&motor.controller, FOC_Have_CritiCal_Err);
- return false;
- }
- _mc_internal_init(CTRL_MODE_OPEN, true);
- motor.b_calibrate = true;
- pwm_turn_on_low_side();
- task_udelay(500);
- mot_contrl_start(&motor.controller, CTRL_MODE_OPEN);
- phase_current_offset_calibrate();
- pwm_start();
- adc_start_convert();
- pwm_enable_channel();
- phase_current_calibrate_wait();
- mot_contrl_set_angle(&motor.controller, 0);
- mot_contrl_set_vdq(&motor.controller, vd, 0);
- shark_timer_post(&_encoder_zero_off_timer, 6*1000);
- return true;
- }
- bool mc_current_sensor_calibrate(float current) {
- if (!mc_start(CTRL_MODE_OPEN)) {
- return false;
- }
- phase_current_sensor_start_calibrate(current);
- phase_current_calibrate_wait();
- return true;
- }
- bool mc_lock_motor(bool lock) {
- if (motor.b_lock_motor == lock) {
- return true;
- }
- int ret = true;
- u32 mask = cpu_enter_critical();
- if (motor.b_start) {
- mot_contrl_set_error(&motor.controller, FOC_NotAllowed);
- ret = false;
- goto ml_ex_cri;
- }
- if (lock && (motor_encoder_get_speed() >= CONFIG_LOCK_MOTOR_MIN_RPM)) {
- mot_contrl_set_error(&motor.controller, FOC_NowAllowed_With_Speed);
- ret = false;
- goto ml_ex_cri;
- }
- motor.b_lock_motor = lock;
- if (lock) {
- pwm_start();
- pwm_update_duty(0, 0, 0);
- pwm_enable_channel();
- }else {
- pwm_stop();
- }
- ml_ex_cri:
- cpu_exit_critical(mask);
- return ret;
- }
- bool mc_auto_hold(bool hold) {
- if (motor.b_auto_hold == hold) {
- return true;
- }
- if (!mc_conf()->s.auto_hold) {
- mot_contrl_set_error(&motor.controller, FOC_NotAllowed);
- return false;
- }
- if (!motor.b_start) {
- mot_contrl_set_error(&motor.controller, FOC_NotAllowed);
- return false;
- }
- if (hold && !mc_throttle_released()) {
- mot_contrl_set_error(&motor.controller, FOC_Throttle_Err);
- return false;
- }
- u32 mask = cpu_enter_critical();
- motor.b_auto_hold = hold;
- if (!mot_contrl_is_start(&motor.controller)) {
- mot_contrl_start(&motor.controller, motor.mode);
- mot_contrl_set_autohold(&motor.controller, hold);
- pwm_enable_channel();
- }else {
- mot_contrl_set_autohold(&motor.controller, hold);
- }
- cpu_exit_critical(mask);
- return true;
- }
- bool mc_set_critical_error(u8 err) {
- if (mc_critical_err_is_set(err)) {
- return false;
- }
- motor.n_CritiCalErrMask |= (1u << err);
- return true;
- }
- void mc_clr_critical_error(u8 err) {
- motor.n_CritiCalErrMask &= ~(1u << err);
- }
- bool mc_critical_err_is_set(u8 err) {
- u32 mask = (1u << err);
- return (motor.n_CritiCalErrMask & mask) != 0;
- }
- u32 mc_get_critical_error(void) {
- return motor.n_CritiCalErrMask;
- }
- bool mc_throttle_released(void) {
- if (motor.s_force_torque != MAX_S16) {
- return motor.s_force_torque == 0;
- }
- if (motor.b_ignor_throttle) {
- return motor.u_throttle_ration == 0;
- }
- return throttle_is_released();
- }
- static bool mc_is_gpio_mlock(void) {
- int count = 50;
- int settimes = 0;
- while(count-- > 0) {
- bool b1 = gpio_motor_locked();
- if (b1) {
- settimes ++;
- }
- delay_us(1);
- }
- if (settimes == 0) {
- return false;
- }else if (settimes == 50) {
- return true;
- }
- //有干扰,do nothing
- return false;
- }
- static bool _mc_is_hwbrake(void) {
- int count = 50;
- int settimes = 0;
- while(count-- > 0) {
- bool b1 = mc_get_gpio_brake() || mc_get_gpio_brake1();
- if (b1) {
- settimes ++;
- }
- delay_us(1);
- }
- if (settimes == 0) {
- #if GPIO_BREAK_MODE==GPIO_LOW_BRK_MODE
- return true;
- #else
- return false;
- #endif
- }else if (settimes == 50) {
- #if GPIO_BREAK_MODE==GPIO_LOW_BRK_MODE
- return false;
- #else
- return true;
- #endif
- }
- //有干扰,do nothing
- motor.n_brake_errors++;
- return false;
- }
- static bool mc_detect_hwbrake(void) {
- motor.b_break = _mc_is_hwbrake();
- return motor.b_break;
- }
- static void _fan_det_timer_handler(shark_timer_t *t) {
- if (t == &_fan_det_timer1) {
- motor.fan[0].rpm = 0;
- motor.fan[0].det_ts = 0;
- }else {
- motor.fan[1].rpm = 0;
- motor.fan[1].det_ts = 0;
- }
- }
- void Fan_IRQHandler(int idx) {
- fan_t *fan = motor.fan + idx;
- u32 pre_ts = fan->det_ts;
- u32 delta_ts = get_delta_ms(pre_ts);
- fan->det_ts = get_tick_ms();
- float rpm = 60.0f * 1000 / (float)delta_ts;
-
- LowPass_Filter(fan->rpm, rpm, 0.1f);
- if (idx == 0) {
- shark_timer_post(&_fan_det_timer1, 100);
- }else {
- shark_timer_post(&_fan_det_timer2, 100);
- }
- }
- void MC_Brake_IRQHandler(void) {
- mc_detect_hwbrake();
- if (!motor.b_start) {
- return;
- }
- if (motor.b_break) {
- mc_enable_cruise(false);
- mot_contrl_set_hw_brake(&motor.controller, true);
- }else {
- mot_contrl_set_hw_brake(&motor.controller, false);
- }
- }
- static void _pwm_brake_prot_timer_handler(shark_timer_t *t){
- pwm_brake_enable(true);
- sys_debug("MC protect error\n");
- }
- static void mc_save_err_runtime(void) {
- mc_error.vbus_x10 = (s16)(sample_vbus_raw() * 10.0f);
- mc_error.ibus_x10 = (s16)(sample_ibus_raw() * 10.0f);
- mc_error.vacc_x10 = (s16) (sample_acc_vol_raw() * 10.0f);
- mc_error.id_ref_x10 = (s16)(motor.controller.foc.in.target_id.interpolation * 10.0f);
- mc_error.iq_ref_x10 = (s16)(motor.controller.foc.in.target_iq.interpolation * 10.0f);
- mc_error.id_x10 = (s16)(motor.controller.foc.out.curr_dq.d * 10.0f);
- mc_error.iq_x10 = (s16)(motor.controller.foc.out.curr_dq.q * 10.0f);
- mc_error.vd_x10 = (s16)(motor.controller.foc.out.vol_dq.d * 10.0f);
- mc_error.vq_x10 = (s16)(motor.controller.foc.out.vol_dq.q * 10.0f);
- mc_error.torque_ref_x10 = (s16)(motor.controller.target_torque * 10.0f);
- mc_error.run_mode = motor.controller.mode_running;
- mc_error.rpm = (s16)motor_encoder_get_speed();
- mc_error.b_sensorless = !foc_observer_is_encoder();
- mc_error.b_sensorless_stable = foc_observer_sensorless_stable();
- mc_error.mos_temp = get_mos_temp_raw();
- mc_error.mot_temp = get_motor_temp_raw();
- mc_error.enc_error = motor_encoder_may_error();
- mc_error.sensorless_rpm = (s16)foc_observer_sensorless_speed();
- mc_err_runtime_add(&mc_error);
- }
- void MC_Protect_IRQHandler(void){
- pwm_brake_enable(false);
- shark_timer_post(&_brake_prot_timer, 1000);
- if (!motor.b_start) {
- return;
- }
- mc_set_critical_error(FOC_CRIT_Phase_Err);
- mc_save_err_runtime();
- _mc_internal_init(CTRL_MODE_OPEN, false);
- adc_stop_convert();
- pwm_stop();
- mot_contrl_stop(&motor.controller);
- pwm_up_enable(true);
- }
- void motor_debug(void) {
- if (!mc_unsafe_critical_error()) {
- return;
- }
- sys_debug("err1: %f, %f, %f, %d\n", (float)mc_error.vbus_x10/10.0f, (float)mc_error.id_ref_x10/10.0f, (float)mc_error.iq_ref_x10/10.0f, mc_error.run_mode);
- sys_debug("err2: %f, %f, %f, %f\n", (float)mc_error.id_x10/10.0f, (float)mc_error.iq_x10/10.0f, (float)mc_error.vd_x10/10.0f, (float)mc_error.vq_x10/10.0f);
- sys_debug("err3: %f, %d, %d, %d, %d\n", (float)mc_error.ibus_x10/10.0f, mc_error.sensorless_rpm, mc_error.mos_temp, mc_error.mot_temp, mc_error.enc_error);
- }
- static void motor_vbus_crit_low(s16 curr_vbus) {
- static u16 _vbus_e_count = 0;
- if (curr_vbus < motor.s_vbus_hw_min) {
- _vbus_e_count ++;
- if (_vbus_e_count >= 2) {
- if (mot_contrl_is_start(&motor.controller)) {
- pwm_disable_channel();
- mc_save_err_runtime();
- mot_contrl_stop(&motor.controller);
- }
- if (mc_set_critical_error(FOC_CRIT_Vol_HW_Err)) {
- if (mot_contrl_get_speed(&motor.controller) > CONFIG_ZERO_SPEED_RPM) {
- mc_crit_err_add_s16(FOC_CRIT_Vol_HW_Err, curr_vbus);
- }
- }
- }
- }else {
- _vbus_e_count = 0;
- }
- }
- void TIMER_UP_IRQHandler(void){
- if (!motor.b_start && !mot_contrl_is_start(&motor.controller)) {
- motor_encoder_update(false);
- motor_vbus_crit_low((s16)get_vbus_int());
- }
- }
- measure_time_t g_meas_foc = {.exec_max_time = 25, .intval_max_time = 62, .intval_low_err = 0, .intval_hi_err = 0, .first = true,};
- #define TIME_MEATURE_START() time_measure_start(&g_meas_foc)
- #define TIME_MEATURE_END() time_measure_end(&g_meas_foc)
- #if (CONFIG_ENABLE_IAB_REC==1)
- #define CONFIG_IAB_REC_COUNT 1000
- static s16 ia[CONFIG_IAB_REC_COUNT], ib[CONFIG_IAB_REC_COUNT];
- static int iab_w_count = 0, iab_r_count = 0;
- static bool b_iab_rec = false;
- extern void can_plot2(s16 v1, s16 v2);
- #endif
- /*ADC 电流采集中断,调用FOC的核心处理函数*/
- void ADC_IRQHandler(void) {
- if (phase_current_offset()) {//check if is adc offset checked
- return;
- }
- if (phase_current_sensor_do_calibrate()){
- pwm_update_duty(100, FOC_PWM_Half_Period-100, 100);
- pwm_update_sample(FOC_PWM_Half_Period-1, FOC_PWM_Half_Period+1, PHASE_BC);
- return;
- }
- TIME_MEATURE_START();
- #if (CONFIG_ENABLE_IAB_REC==1)
- if (b_iab_rec && (iab_w_count < CONFIG_IAB_REC_COUNT)) {
- ia[iab_w_count] = (s16)motor.controller.foc.in.curr_abc[0];
- ib[iab_w_count] = (s16)motor.controller.foc.in.curr_abc[1];
- iab_w_count ++;
- }
- #endif
- motor_vbus_crit_low((s16)sample_vbus_raw()); //need fast detect vbus very low, to stop the motor
- float vd, vq;
- if (motor.b_ind_start) {
- mot_params_high_freq_inject();
- vd = motor.controller.foc.out.vol_dq.d;
- vq = motor.controller.foc.out.vol_dq.q;
- }
- if (!mot_contrl_update(&motor.controller)) {/* FOC 角度错误,立即停机 */
- if (mot_contrl_is_start(&motor.controller)) {
- pwm_disable_channel();
- /* 记录错误 */
- if (!foc_observer_is_force_sensorless()) {
- mc_save_err_runtime();
- }
- mot_contrl_stop(&motor.controller);
- g_meas_foc.first = true;
- if (!foc_observer_is_force_sensorless()) {
- if (mc_set_critical_error(FOC_CRIT_Angle_Err)) {
- mc_crit_err_add_s16(FOC_CRIT_Angle_Err, (s16)motor_encoder_get_speed());
- }
- if (motor_encoder_may_error() == ENCODER_PWM_ERR) {
- if (mc_set_critical_error(FOC_CRIT_Encoder_Err)) {
- mc_crit_err_add(FOC_CRIT_Encoder_Err, (s16)enc_pwm_err_ms, enc_delta_err2);
- }
- }else if (motor_encoder_may_error() == ENCODER_AB_ERR) {
- if (mc_set_critical_error(FOC_CRIT_ENC_AB_Err)) {
- mc_crit_err_add(FOC_CRIT_ENC_AB_Err, enc_delta_err1, enc_delta_err2);
- }
- }
- }
- }
- }
- if (motor.b_ind_start) {
- float id = motor.controller.foc.out.curr_dq.d;
- float iq = motor.controller.foc.out.curr_dq.q;
- mot_params_hj_sample_vi(vd, vq, id, iq);
- }
- TIME_MEATURE_END();
- }
- #if (CONFIG_ENABLE_IAB_REC==1)
- static void _iab_plot_timer_handler(shark_timer_t *t) {
- if (!b_iab_rec) {
- return;
- }
- if (iab_r_count < iab_w_count) {
- can_plot2(ia[iab_r_count], ib[iab_r_count]);
- iab_r_count ++;
- shark_timer_post(t, 10);
- }
- }
- static shark_timer_t _iab_plot_timer = TIMER_INIT(_iab_plot_timer, _iab_plot_timer_handler);
- void mc_start_current_rec(bool rec) {
- if (b_iab_rec == rec) {
- return;
- }
- if (!rec) {
- b_iab_rec = false;
- shark_timer_cancel(&_iab_plot_timer);
- return;
- }
- iab_w_count = 0;
- iab_r_count = 0;
- b_iab_rec = true;
- shark_timer_post(&_iab_plot_timer, 100);
- }
- #endif
- static bool mc_run_stall_process(u8 run_mode) {
- if ((run_mode == CTRL_MODE_TRQ || run_mode == CTRL_MODE_SPD) && !mot_contrl_is_auto_holdding(&motor.controller)) {
- //堵转判断
- if (motor.b_runStall) {
- if (!mc_throttle_released()) {
- return true;
- }
- motor.runStall_time = 0;
- motor.b_runStall = false; //转把释放,清除堵转标志
- }else if (mot_contrl_get_current_vector(&motor.controller) >= CONFIG_STALL_MAX_CURRENT){
- if (ABS(mot_contrl_get_speed(&motor.controller)) < 1.0f && (motor.runStall_time == 0)) {
- motor.runStall_time = get_tick_ms();
- motor.runStall_pos = motor_encoder_get_position();
- }
- if (motor.runStall_time > 0) {
- if (get_delta_ms(motor.runStall_time) >= CONFIG_STALL_MAX_TIME) {
- motor.b_runStall = true;
- motor.runStall_time = 0;
- mot_contrl_set_torque(&motor.controller, 0);
- throttle_torque_reset();
- return true;
- }
- if (ABS(motor.runStall_pos - motor_encoder_get_position()) >= 0.2f) {
- motor.runStall_time = 0;
- }
- }
- }else {
- motor.runStall_time = 0;
- }
- }
- return false;
- }
- static void mc_autohold_process(void) __attribute__((unused));
- static void mc_autohold_process(void) {
- if (!mc_conf()->s.auto_hold) {
- if (mot_contrl_is_auto_holdding(&motor.controller)) {
- mc_auto_hold(false);
- }
- return;
- }
- if (mot_contrl_is_auto_holdding(&motor.controller)) {
- if (!mc_throttle_released()) {
- mc_auto_hold(false);
- motor.b_wait_brk_release = false;
- }else if (!motor.b_break && motor.b_wait_brk_release) {
- motor.b_wait_brk_release = false;
- }else if (motor.b_break && !motor.b_wait_brk_release) {
- mc_auto_hold(false);
- }
- }
- if (!mot_contrl_is_auto_holdding(&motor.controller) && motor.b_break && (motor_encoder_get_speed() == 0)) {
- if (motor.n_autohold_time == 0) {
- motor.n_autohold_time = get_tick_ms();
- }else {
- if (get_delta_ms(motor.n_autohold_time) >= CONFIG_AUTOHOLD_DETECT_TIME) {
- if (mc_auto_hold(true)) {
- motor.b_wait_brk_release = true;
- }
- }
- }
- }else {
- motor.n_autohold_time = 0;
- }
- }
- static void mc_process_throttle_epm(void) __attribute__((unused));
- static void mc_process_throttle_epm(void) {
- if (!motor.b_epm_cmd_move) {//通过命令前进后退,不处理转把
- if (mc_throttle_released()) {
- mc_throttle_epm_move(EPM_Dir_None);
- }else {
- mc_throttle_epm_move(EPM_Dir_Forward);
- }
- }
- }
- static void mc_process_epm_move(void) __attribute__((unused));
- static void mc_process_epm_move(void) {
- if (!motor.b_epm || (motor.epm_dir == EPM_Dir_None)){
- return;
- }
- float target_vel = mc_conf()->c.max_epm_rpm;
- float target_trq = mc_conf()->c.max_epm_torque;
- if (motor.epm_dir == EPM_Dir_Back) {
- target_vel = -mc_conf()->c.max_epm_back_rpm;
- target_trq = mc_conf()->c.max_epm_back_torque;
- }else if (!motor.b_epm_cmd_move) {
- target_vel = throttle_vol_to_opening(throttle_get_signal()) * 2.0f * (float)target_vel;
- }
- motor.f_epm_trq = target_trq;
- motor.f_epm_vel = target_vel;
- mot_contrl_set_torque_limit(&motor.controller, motor.f_epm_trq);
- mot_contrl_set_target_vel(&motor.controller, motor.f_epm_vel);
- }
- static bool mc_process_force_running(void) {
- if (motor.b_calibrate || (motor.mode == CTRL_MODE_OPEN)) {
- if (motor.b_force_run && _force_wait <= 2000) {
- if (_force_wait > 0) {
- --_force_wait;
- }else {
- _force_angle += 1.5f;
- rand_angle(_force_angle);
- mot_contrl_set_angle(&motor.controller, _force_angle);
- }
- }
- return true;
- }
- return false;
- }
- static void mc_process_brake_light(void) {
- bool can_lighting = false;
- if (motor.b_break || motor.b_auto_hold || mot_contrl_ebrk_is_running(&motor.controller) || ((!mc_critical_can_not_run()) && motor_encoder_get_speed() > 2000)) {
- can_lighting = true;
- }
- gpio_brk_light_enable(can_lighting);
- }
- #ifdef CONFIG_CRUISE_ENABLE_ACCL
- static void mc_process_curise(void) {
- static bool can_pause_resume = false;
- if (motor.b_cruise) {
- if (mot_contrl_get_speed(&motor.controller) < CONFIG_CRUISE_EXIT_RPM) {
- mot_contrl_set_cruise(&motor.controller, false);
- return;
- }
- /* 定速巡航模式下,必须转把归位后才能开始通过拧动转把加速 */
- if (mc_throttle_released() && !can_pause_resume) {
- can_pause_resume = true;
- }
- if (!can_pause_resume) {
- return;
- }
- if (mot_contrl_is_cruise_enabled(&motor.controller)) {
- u32 cruise_time = shark_get_seconds() - motor.cruise_time;
- if ((cruise_time >= 3) && motor.cruise_torque == 0.0f) {
- motor.cruise_torque = motor.controller.target_torque;
- }else if ((cruise_time >= 3) && (motor.cruise_torque > 0.0f)){
- float trq_req = get_user_request_torque();
- if (trq_req > motor.cruise_torque * 1.2f) {
- mot_contrl_pause_cruise(&motor.controller); //需要加速,暂停定速巡航
- }
- }
- }else {
- float trq_req = get_user_request_torque();
- if (trq_req <= motor.cruise_torque * 1.1f) {
- mot_contrl_resume_cruise(&motor.controller); //重新开始定速巡航,巡航速度还是前一次定速巡航给的速度
- }
- }
- }else {
- mot_contrl_set_cruise(&motor.controller, false);
- can_pause_resume = false;
- }
- }
- #endif
- static bool mc_can_stop_foc(void) {
- if (mc_critical_need_stop()) {
- return true;
- }
- if (motor.mode == CTRL_MODE_CURRENT) {
- return false;
- }
- if (!motor.b_cruise && !motor.b_epm && motor.mode == CTRL_MODE_SPD) {
- if (motor.s_target_speed != MAX_S16 && motor.s_target_speed != 0) {
- return false;
- }else {
- return true;
- }
- }
- if (mc_throttle_released() && mot_contrl_get_speed(&motor.controller) == 0.0f) {
- if (!mot_contrl_is_auto_holdding(&motor.controller) && motor.epm_dir == EPM_Dir_None) {
- return true;
- }
- }
- /* 启用无感观测器,但是观测器未稳定,关闭输出,滑行 */
- if (!foc_observer_is_encoder() && !foc_observer_sensorless_stable()) {
- return true;
- }
- return false;
- }
- static bool mc_can_restart_foc(void) {
- bool can_start = (!mc_throttle_released() || (motor.epm_dir != EPM_Dir_None)) && (!mc_critical_can_not_run());
- if (!foc_observer_is_encoder() && !foc_observer_sensorless_stable()){
- return false;
- }
- if ((motor.s_target_speed != MAX_S16 && motor.s_target_speed != 0) && (!mc_critical_can_not_run()) && motor.mode == CTRL_MODE_SPD) {
- return true;
- }
- return can_start;
- }
- static void mc_motor_runstop(void) __attribute__((unused));
- static void mc_motor_runstop(void) {
- u32 mask;
- if (mot_contrl_is_start(&motor.controller) && mc_can_stop_foc()) {
- mask = cpu_enter_critical();
- mot_contrl_stop(&motor.controller);
- pwm_disable_channel();
- g_meas_foc.first = true;
- cpu_exit_critical(mask);
- }
- if (!mot_contrl_is_start(&motor.controller) && mc_can_restart_foc()) {
- mask = cpu_enter_critical();
- mot_contrl_start(&motor.controller, motor.mode);
- mc_gear_mode_set();
- throttle_torque_reset();
- pwm_enable_channel();
- g_meas_foc.first = true;
- cpu_exit_critical(mask);
- }
- }
- static void mc_process_throttle_torque(float vol) __attribute__((unused));
- static void mc_process_throttle_torque(float vol) {
- float torque = throttle_get_torque(&motor.controller, vol);
- if (mc_throttle_released()) {
- #ifdef CONFIG_CRUISE_ENABLE_ACCL
- if (mc_is_cruise_enabled()) {
- return;
- }
- #endif
- if (mot_contrl_energy_recovery(&motor.controller, true)) {
- return;
- }
- }
- if (motor.controller.mode_running == CTRL_MODE_TRQ) {
- throttle_set_torque(&motor.controller, torque);
- }else if (motor.controller.mode_running == CTRL_MODE_SPD) {
- if (!mc_is_cruise_enabled()) {
- float vel_ref = motor.controller.userlim.mot_vel * throttle_get_open_ration_filted();
- mot_contrl_set_target_vel(&motor.controller, vel_ref);
- }
- }else if (motor.controller.mode_running == CTRL_MODE_EBRAKE){
- float vel = mot_contrl_get_speed(&motor.controller);
- float ebrk_trq = motor_get_ebreak_toruqe(vel);
-
- if (ebrk_trq >= -mot_contrl_get_ebrk_torque(&motor.controller)/2){
- mot_contrl_set_ebrk_time(&motor.controller, 1);
- }
- if (ebrk_trq != 0) {
- mot_contrl_set_torque(&motor.controller, ebrk_trq);
- }
- if ((mot_contrl_get_final_torque(&motor.controller) < 0.0001f && vel < CONFIG_MIN_RPM_EXIT_EBRAKE) ||
- (!mc_throttle_released() || (mc_throttle_released() && (vel == 0.0f)))) {
- mot_contrl_energy_recovery(&motor.controller, false);
- throttle_torque_reset();
- }
- }
- }
- /*FOC 的部分处理,比如速度环,状态机,转把采集等*/
- measure_time_t g_meas_MCTask = {.exec_max_time = 100, .intval_max_time = 1000, .intval_low_err = 0, .intval_hi_err = 0, .first = true,};
- #define mc_TaskStart time_measure_start(&g_meas_MCTask)
- #define mc_TaskEnd time_measure_end(&g_meas_MCTask)
- void Sched_MC_mTask(void) {
- static int vbus_err_cnt = 0;
- static bool _sensorless_run = false;
- mc_TaskStart;
- adc_vref_filter();
- throttle_detect(motor.b_start);
- F_all_Calc();
- #ifdef CONFIG_CRUISE_ENABLE_ACCL
- mc_process_curise();
- #endif
- u8 runMode = mot_contrl_mode(&motor.controller);
- /*保护功能*/
- u8 limted = mot_contrl_protect(&motor.controller);
- /* 母线电流,实际采集的相电流矢量大小的计算 */
- mot_contrl_calc_current(&motor.controller);
- if ((mot_contrl_get_dc_current(&motor.controller) > (CONFIG_HW_MAX_DC_CURRENT * 1.1f)) || (mot_contrl_get_dc_current(&motor.controller) < CONFIG_HW_MAX_CHRG_CURRENT)) {
- vbus_err_cnt ++;
- if (vbus_err_cnt >= 5) {
- if (mc_set_critical_error(FOC_CRIT_IDC_OV)) {
- mc_crit_err_add(FOC_CRIT_IDC_OV, (s16)sample_vbus_raw(), (s16)sample_ibus_raw());
- mc_save_err_runtime();
- }
- }
- }else {
- vbus_err_cnt = 0;
- }
- if (mc_process_force_running()) {
- mc_TaskEnd;
- return;
- }
- bool sensor_less = !foc_observer_is_encoder();
- if (mc_detect_vbus_mode() || (limted == FOC_LIM_CHANGE_L) || (_sensorless_run != sensor_less)) {
- mc_gear_mode_set();
- if (sensor_less && foc_observer_sensorless_stable()) {//unstable 记录在ADC中断处理中
- if (motor_encoder_may_error() == ENCODER_PWM_ERR) {
- mc_set_critical_error(FOC_CRIT_Encoder_Err);
- mc_crit_err_add(FOC_CRIT_Encoder_Err, (s16)enc_pwm_err_ms, enc_delta_err2);
- }else if (motor_encoder_may_error() == ENCODER_AB_ERR) {
- mc_set_critical_error(FOC_CRIT_ENC_AB_Err);
- mc_crit_err_add(FOC_CRIT_ENC_AB_Err, enc_delta_err1, enc_delta_err2);
- }
- }
- motor.b_limit_pending = false;
- }else if (limted == FOC_LIM_CHANGE_H) {
- motor.b_limit_pending = true;
- }
- _sensorless_run = sensor_less;
- /* 如果取消高温,欠压等限流需要释放转把后才生效,确保不会突然加速 */
- if (motor.b_limit_pending && mc_throttle_released()) {
- motor.b_limit_pending = false;
- mc_gear_mode_set();
- }
- /* 堵转处理 */
- if (mc_run_stall_process(runMode) || (motor.mode == CTRL_MODE_CURRENT)) {
- #ifndef CONFIG_DQ_STEP_RESPONSE
- mot_contrl_slow_task(&motor.controller);
- mc_motor_runstop();
- #endif
- if (motor.b_ind_start) {
- mot_params_flux_stop();
- }
- mc_TaskEnd;
- return;
- }
- mc_process_brake_light();
- if ((runMode != CTRL_MODE_OPEN) || (motor.mode != CTRL_MODE_OPEN)) {
- #ifndef CONFIG_DQ_STEP_RESPONSE
- mc_autohold_process();
- if (motor.mode != CTRL_MODE_OPEN) {
- mc_motor_runstop();
- }
- if (runMode != CTRL_MODE_OPEN) {
- if (runMode == CTRL_MODE_SPD) {
- if (mc_is_epm()) {
- mc_process_throttle_epm();
- mc_process_epm_move();
- }else if (motor.s_target_speed != MAX_S16) {
- mot_contrl_set_target_vel(&motor.controller, motor.s_target_speed);
- }
- }else {
- if (motor.s_force_torque != MAX_S16) {
- mot_contrl_set_force_torque(&motor.controller, motor.s_force_torque);
- }else {
- float thro = throttle_get_signal();
- if (motor.b_ignor_throttle) {
- float r = (float)motor.u_throttle_ration/100.0f;
- thro = throttle_opening_to_vol(r);
- }
- mc_process_throttle_torque(thro);
- }
- }
- mot_contrl_slow_task(&motor.controller);
- }
- #endif
- }
- mc_TaskEnd;
- }
|