if (((ui16_aca_flags & TQ_SENSOR_MODE) != TQ_SENSOR_MODE)) {
// add dynamic assist level based on past throttle input
ui8_temp = ui8_assist_percent_actual;
if ((ui16_aca_flags & DYNAMIC_ASSIST_LEVEL) == DYNAMIC_ASSIST_LEVEL) {
ui8_temp += ui8_assist_dynamic_percent_addon;
}
if (ui16_time_ticks_between_pas_interrupt_smoothed > ui16_s_ramp_end) {
//if you are pedaling slower than defined ramp end
//or not pedalling at all
//current is proportional to cadence
uint32_current_target = (ui8_temp * (ui16_battery_current_max_value) / 100);
float_temp = 1.0 - (((float) (ui16_time_ticks_between_pas_interrupt_smoothed - ui16_s_ramp_end)) / ((float) (ui16_s_ramp_start - ui16_s_ramp_end)));
uint32_current_target = ((uint16_t) (uint32_current_target)*(uint16_t) (float_temp * 100.0)) / 100 + ui16_current_cal_b;
controll_state_temp += 1;
//in you are pedaling faster than in ramp end defined, desired battery current level is set,
} else {
uint32_current_target = (ui8_temp * (ui16_battery_current_max_value) / 100 + ui16_current_cal_b);
controll_state_temp += 2;
}
} else { // torque sensor mode
//erst mal alles aufmultiplizieren, damit beim Teilen was über 1 übrig bleibt. Bitte mal überschlagen, ob die int32 da nicht überlaufen kann...
uint32_temp = ui16_sum_torque;
uint32_temp *= ui8_assist_percent_actual;
uint32_temp *= ui16_battery_current_max_value;
uint32_temp *= uint32_torquesensorCalibration;
uint32_temp /= ui16_time_ticks_between_pas_interrupt_smoothed; // hier lässt sich die geteilt-Operation nicht vermeiden :-(
if(PAS_is_active)
uint32_current_target = (uint32_temp >>8) + (uint32_t) ui16_current_cal_b; //right shift 15 fasst die Operationen /100 (annähernd >>7) aus der assist_percent und /255 ( >>8) aus dem battery_current max zusammen, ist nicht ganz korrekt, ggf. nur >>14 nehmen -->/(256*128) vs. /(256*64)
else uint32_current_target =(uint32_t) ui16_current_cal_b;
controll_state_temp += 4;
}
float_temp = 0.0;
// throttle / torquesensor override following
if (((ui16_aca_flags & TQ_SENSOR_MODE) != TQ_SENSOR_MODE)) {
if (ui8_speedlimit_kph > 1){
// do not apply throttle at very low speed limits (technical restriction, speelimit can and should never be lover than 1)
float_temp = (float) ui16_sum_throttle;
}
} else {
float_temp = (float) ui16_momentary_throttle; // or ui16_sum_throttle
//float_temp *= (1 - (float) ui16_virtual_erps_speed / 2 / (float) (ui16_speed_kph_to_erps_ratio * ((float) ui8_speedlimit_kph))); //ramp down linear with speed. Risk: Value is getting negative if speed>2*speedlimit
}
// map curret target to assist level, not to maximum value
if ((ui16_aca_flags & ASSIST_LVL_AFFECTS_THROTTLE) == ASSIST_LVL_AFFECTS_THROTTLE) {
float_temp *= ((float) ui8_assist_percent_actual / 100.0);
controll_state_temp += 8;
}
float_temp = float_temp * (float) (ui16_battery_current_max_value) / 255.0 + (float) ui16_current_cal_b; //calculate current target
if ((uint32_t) float_temp > uint32_current_target) {
//override current target with throttle
uint32_current_target = (uint32_t) float_temp;
controll_state_temp += 16;
}
// check for overspeed
uint32_temp = uint32_current_target;
uint32_current_target = CheckSpeed((uint16_t) uint32_current_target, (uint16_t) ui16_virtual_erps_speed, (ui16_speed_kph_to_erps_ratio * ((uint16_t) ui8_speedlimit_actual_kph)) / 100, (ui16_speed_kph_to_erps_ratio * ((uint16_t) (ui8_speedlimit_actual_kph + 2))) / 100); //limit speed
if (uint32_temp != uint32_current_target) {
controll_state_temp += 32;
}
if (uint32_current_target > ui16_battery_current_max_value + ui16_current_cal_b) {
uint32_current_target = ui16_battery_current_max_value + ui16_current_cal_b;
controll_state_temp += 64;
}
//phase current limiting
if (setpoint_old > 0 && (uint32_current_target - ui16_current_cal_b)*255 / setpoint_old > PHASE_CURRENT_MAX_VALUE) { // limit phase current according to Phase Current = battery current/duty cycle
uint32_current_target = (PHASE_CURRENT_MAX_VALUE) * setpoint_old / 255 + ui16_current_cal_b;
controll_state_temp += 128;
}
// control power instead of current
if ((ui16_aca_flags & POWER_BASED_CONTROL) == POWER_BASED_CONTROL) {
// nominal voltage based on limits
ui8_temp = ((ui8_s_battery_voltage_max - ui8_s_battery_voltage_min)>>1)+ui8_s_battery_voltage_min;
uint32_current_target*=ui8_temp/ui8_BatteryVoltage;
}
if ((ui16_aca_experimental_flags & DC_STATIC_ZERO) == DC_STATIC_ZERO) {
ui32_dutycycle = 0;
controll_state_temp += 256;
}else if (!checkUnderVoltageOverride() && !checkMaxErpsOverride()){
if (ui8_walk_assist) uint32_current_target = 10 + ui16_current_cal_b;
//send current target to PI-controller
ui32_dutycycle = PI_control(ui16_BatteryCurrent, uint32_current_target,uint_PWM_Enable);
}
if ((ui16_aca_experimental_flags & PWM_AUTO_OFF) == PWM_AUTO_OFF) {
controll_state_temp += 512;
//disable PWM if enabled and no power is wanted
if (uint_PWM_Enable && ui32_erps_filtered == 0 && uint32_current_target == ui16_current_cal_b) {
TIM1_CtrlPWMOutputs(DISABLE);
uint_PWM_Enable = 0;
}
//enable PWM if disabled and voltage is 6.25% higher than min, some hysteresis and power is wanted
if (!uint_PWM_Enable && ui8_BatteryVoltage > (ui8_s_battery_voltage_min + (ui8_s_battery_voltage_min >>4)) && (uint32_current_target != ui16_current_cal_b)){
TIM1_CtrlPWMOutputs(ENABLE);
uint_PWM_Enable = 1;
}
}else{
//enable PWM if disabled and voltage is 6.25% higher than min, some hysteresis
if (!uint_PWM_Enable && ui8_BatteryVoltage > (ui8_s_battery_voltage_min + (ui8_s_battery_voltage_min >>4))) {
TIM1_CtrlPWMOutputs(ENABLE);
uint_PWM_Enable = 1;
}
}
}
ui16_control_state = controll_state_temp;
return cutoffSetpoint(ui32_dutycycle);
}