AM32 电调学习--setInput()函数解析

AM32 电调学习–setInput()函数解析

#define INTERVAL_TIMER TIM2 ; 过零时间测量
TIM_InitStruct.Prescaler = 31; 0.5us
#define TEN_KHZ_TIMER TIM6 ;tenKhzRoutine frq=1M load=1M/20k
*** TIM_InitStruct.Prescaler = 63 1us ***
#define UTILITY_TIMER TIM17
TIM_InitStruct.Prescaler = 63; 1us
#define COM_TIMER TIM14 //PeriodElapsedCallback 过零后等待时间中断
TIM_InitStruct.Prescaler = 31; 0.5us
TIM3: ***TIM_InitStruct.Prescaler = 0 ***

一、常用变量及其范围

  1. bi_direction:3d开关设置参数,非互补pwm时为0
  2. dshot: 输入方式设置
  3. RC_CAR_REVERSE: 设置标志
  4. uint16_t newinput = 0; 读取到的输入值
  5. forward:目前转动方向
  6. uint16_t adjusted_input = 0; 经处理 newinput 后的输入值
bi_direction==1:
   非dshot输入时:
        if (newinput > (1000 + (servo_dead_band << 1)))   
              adjusted_input = map(newinput, 1000 + (servo_dead_band << 1), 2000, 47, 2047);
        if (newinput < (1000 - (servo_dead_band << 1))) 
            adjusted_input = map(newinput, 0, 1000 - (servo_dead_band << 1), 2047, 47);
   dshot输入时:
       if (newinput > 1047)
            adjusted_input = ((newinput - 1048) * 2 + 47) - reversing_dead_band;
       if (newinput <= 1047 && newinput > 47)
            adjusted_input = ((newinput - 48) * 2 + 47) - reversing_dead_band;
bi_direction==0:
          adjusted_input = newinput; 
  1. char prop_brake_active = 0; 刹车标志,有引标志后不再换相
  2. char return_to_center = 0; 刹车结束标志
  3. char old_routine = 1; 盲启动标志
  4. bemf_timeout_happened 堵转记数
    if (INTERVAL_TIMER_COUNT > 45000 && running == 1) bemf_timeout_happened++;
  5. char bemf_timeout = 10; 堵转最大尝试次数,若超过,则停机
  6. use_sin_start 设置标志
  7. uint16_t input = 0; 经处理 adjusted_input 后的输入值
use_sin_start:
             if (adjusted_input < 30) { // dead band ?
                input = 0;
            }
            if (adjusted_input > 30 && adjusted_input < (sine_mode_changeover_thottle_level * 20)) {
                input = map(adjusted_input, 30,
                    (sine_mode_changeover_thottle_level * 20), 47, 160);
            }
            if (adjusted_input >= (sine_mode_changeover_thottle_level * 20)) {
                input = map(adjusted_input, (sine_mode_changeover_thottle_level * 20),
                    2047, 160, 2047);
            }
!use_sin_start:
use_speed_control_loop:
                     input = (uint16_t)input_override; // speed control pid override
                    if (input_override > 2047) {
                        input = 2047;
                    }
                    if (input_override < 48) {
                        input = 48;
                    }
else:
     input = adjusted_input;            
  1. float input_override = 0;
    input_override += doPidCalculations(&speedPid, e_com_time, target_e_com_time) / 10000;

  2. char armed = 0; 正常识别电机标志。

 if (armed_timeout_count > LOOP_FREQUENCY_HZ) { // one second
                        if (zero_input_count > 30) {
                            armed = 1;
  1. uint16_t last_duty_cycle = 0; 上一次输出占空比
  2. uint8_t max_duty_cycle_change = 2; 占空比最大允许改变
           if (last_duty_cycle < 150) {
                max_duty_cycle_change = RAMP_SPEED_STARTUP;//2
            } else {
                if (average_interval > 500) {
                    max_duty_cycle_change = RAMP_SPEED_LOW_RPM;  //10
                } else {
                    max_duty_cycle_change = RAMP_SPEED_HIGH_RPM;//40
                }
            }
  1. uint16_t duty_cycle_setpoint = 0; 根据input计算出的占空比
           if (use_sin_start) {
                duty_cycle_setpoint = map(input, 137, 2047, minimum_duty_cycle, TIMER1_MAX_ARR);
            } else {
                duty_cycle_setpoint = map(input, 47, 2047, minimum_duty_cycle, TIMER1_MAX_ARR);
            }
  1. int16_t phase_A_position;
  2. int16_t phase_B_position;
  3. int16_t phase_C_position;
phase_A_position = ((step - 1) * 60) + enter_sine_angle;
                if (phase_A_position > 359) {
                    phase_A_position -= 360;
                }
                phase_B_position = phase_A_position + 119;
                if (phase_B_position > 359) {
                    phase_B_position -= 360;
                }
                phase_C_position = phase_A_position + 239;
                if (phase_C_position > 359) {
                    phase_C_position -= 360;
                }
  1. uint16_t min_startup_duty = 120;
  2. uint16_t startup_max_duty_cycle = 300 + DEAD_TIME; 启动时最大值

二、函数流程分析

  1. newinput转换到adjusted_input
  2. 如果堵转尝试次数到,则停机,否则
  3. adjusted_input转换到input
  4. if (input >= 47 + (80 * use_sin_start) && armed) 则计算占空比duty_cycle_setpoint
  5. if (input < 47 + (80 * use_sin_start)) ,则命令处理
  6. 若盲启阶段,if (input >= 47 && (zero_crosses < (20 >> stall_protection)))
                if (input >= 47 && (zero_crosses < (20 >> stall_protection))) {
                if (duty_cycle_setpoint < min_startup_duty) {
                    duty_cycle_setpoint = min_startup_duty;
                }
                if (duty_cycle_setpoint > startup_max_duty_cycle) {
                    duty_cycle_setpoint = startup_max_duty_cycle;
                }
            }
  1. 履带车低速加油门处理
            if (stall_protection_adjust > 0 && input > 47) {

                duty_cycle_setpoint = duty_cycle_setpoint + (uint16_t)stall_protection_adjust;
            }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值