Skip to content

Commit

Permalink
implement throttle speed override flag #109
Browse files Browse the repository at this point in the history
  • Loading branch information
danielnilsson9 committed Apr 13, 2023
1 parent f54d2b0 commit 7dcda82
Show file tree
Hide file tree
Showing 7 changed files with 122 additions and 41 deletions.
41 changes: 28 additions & 13 deletions src/firmware/app.c
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ void apply_pas_torque(uint8_t* target_current);
#endif

void apply_cruise(uint8_t* target_current, uint8_t throttle_percent);
void apply_throttle(uint8_t* target_current, uint8_t* target_cadence, uint8_t throttle_percent);
bool apply_throttle(uint8_t* target_current, uint8_t throttle_percent);
void apply_current_ramp_up(uint8_t* target_current);
void apply_current_ramp_down(uint8_t* target_current);
void apply_speed_limit(uint8_t* target_current);
Expand Down Expand Up @@ -112,7 +112,7 @@ void app_init()
void app_process()
{
uint8_t target_current = 0;
uint8_t target_cadence = assist_level_data.level.max_cadence_percent;
bool throttle_override = false;

if (assist_level == ASSIST_PUSH && g_config.use_push_walk)
{
Expand All @@ -132,19 +132,37 @@ void app_process()
// order is important, ramp up shall not affect throttle
apply_current_ramp_up(&target_current);

apply_throttle(&target_current, &target_cadence, throttle_percent);
throttle_override = apply_throttle(&target_current, throttle_percent);
}

apply_current_ramp_down(&target_current);

apply_speed_limit(&target_current);
// do not apply speed limit if throttle speed override active
if (!throttle_override ||
!(assist_level_data.level.flags & ASSIST_FLAG_PAS) ||
!(assist_level_data.level.flags & ASSIST_FLAG_OVERRIDE_SPEED))
{
apply_speed_limit(&target_current);
}

apply_thermal_limit(&target_current);
apply_low_voltage_limit(&target_current);
#if HAS_SHIFT_SENSOR_SUPPORT
apply_shift_sensor_interrupt(&target_current);
#endif

motor_set_target_speed(target_cadence);
// override target cadence if configured in assist level
if (throttle_override &&
(assist_level_data.level.flags & ASSIST_FLAG_PAS) &&
(assist_level_data.level.flags & ASSIST_FLAG_OVERRIDE_CADENCE))
{
motor_set_target_speed(THROTTLE_CADENCE_OVERRIDE_PERCENT);
}
else
{
motor_set_target_speed(assist_level_data.level.max_cadence_percent);
}

motor_set_target_current(target_current);

if (target_current > 0 && !brake_is_activated())
Expand Down Expand Up @@ -442,23 +460,20 @@ void apply_cruise(uint8_t* target_current, uint8_t throttle_percent)
}
}

void apply_throttle(uint8_t* target_current, uint8_t* target_cadence, uint8_t throttle_percent)
bool apply_throttle(uint8_t* target_current, uint8_t throttle_percent)
{
if ((assist_level_data.level.flags & ASSIST_FLAG_THROTTLE) && throttle_percent > 0 && throttle_ok())
{
uint8_t current = (uint8_t)MAP16(throttle_percent, 0, 100, g_config.throttle_start_percent, assist_level_data.level.max_throttle_current_percent);
if (current >= *target_current)
{
*target_current = current;

// override target cadence if configured in assist level
if ((assist_level_data.level.flags & ASSIST_FLAG_PAS) &&
(assist_level_data.level.flags & ASSIST_FLAG_OVERRIDE_CADENCE))
{
*target_cadence = THROTTLE_CADENCE_OVERRIDE_PERCENT;
}

return true; // return true if overrides previous set target current
}
}

return false;
}

void apply_current_ramp_up(uint8_t* target_current)
Expand Down
1 change: 1 addition & 0 deletions src/firmware/cfgstore.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#define ASSIST_FLAG_PAS_VARIABLE 0x08 // pas mode using throttle to set power level
#define ASSIST_FLAG_PAS_TORQUE 0x10 // pas mode using torque sensor reading
#define ASSIST_FLAG_OVERRIDE_CADENCE 0x20 // pas option where max cadence is set to 100% when throttle overrides pas
#define ASSIST_FLAG_OVERRIDE_SPEED 0x40 // pas option where max speed is set to 100% when throttle overrides pas

#define ASSIST_MODE_SELECT_OFF 0x00
#define ASSIST_MODE_SELECT_STANDARD 0x01
Expand Down
4 changes: 2 additions & 2 deletions src/firmware/version.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
#define _VERSION_H_

#define VERSION_MAJOR 1
#define VERSION_MINOR 3
#define VERSION_PATCH 1
#define VERSION_MINOR 4
#define VERSION_PATCH 0


#if defined(BBSHD)
Expand Down
3 changes: 2 additions & 1 deletion src/tool/Model/Configuration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ public enum AssistFlagsType : byte

PasVariable = 0x08,
PasTorque = 0x10,
CadenceOverride = 0x20
CadenceOverride = 0x20,
SpeedOverride = 0x40
};

public enum TemperatureSensor
Expand Down
72 changes: 51 additions & 21 deletions src/tool/View/AssistLevelPasView.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@


<TextBlock Grid.Column="0" Grid.Row="5" Text="Enable Throttle:" VerticalAlignment="Center" Margin="0 16 0 0">
<TextBlock.ToolTip>
<TextBlock Width="300" TextWrapping="Wrap">
Allow the use of throttle while in this assist level.
</TextBlock>
</TextBlock.ToolTip>
<TextBlock.Style>
<Style TargetType="{x:Type TextBlock}">
<Style.Triggers>
Expand All @@ -85,13 +90,7 @@
</CheckBox.Style>
</CheckBox>

<TextBlock Grid.Column="0" Grid.Row="6" Text="Cadence Override:" VerticalAlignment="Center" Margin="0 8 0 0">
<TextBlock.ToolTip>
<TextBlock Width="300" TextWrapping="Wrap">
Override configured assist level cadence to 100% when power
requested by throttle action surpasses power set by pedal assist level.
</TextBlock>
</TextBlock.ToolTip>
<TextBlock Grid.Column="0" Grid.Row="6" Text="Throttle Overrides:" VerticalAlignment="Center" Margin="0 8 0 0">
<TextBlock.Style>
<Style TargetType="{x:Type TextBlock}">
<Style.Triggers>
Expand All @@ -102,21 +101,52 @@
</Style>
</TextBlock.Style>
</TextBlock>
<CheckBox Grid.Column="1" Grid.Row="6" IsChecked="{Binding IsThrottleCadenceOverrideEnabled}" Margin="9 8 0 0">
<CheckBox.Style>
<Style TargetType="{x:Type CheckBox}">
<Style.Triggers>
<DataTrigger Binding="{Binding IsThrottleEnabled}" Value="False">
<Setter Property="IsEnabled" Value="False" />
</DataTrigger>
<DataTrigger Binding="{Binding IsPasAssistVariableVariant}" Value="True">
<Setter Property="Visibility" Value="Collapsed" />
</DataTrigger>
</Style.Triggers>
</Style>
</CheckBox.Style>
</CheckBox>

<StackPanel Orientation="Horizontal" Grid.Column="1" Grid.Row="6" >
<CheckBox Content="Cadence" IsChecked="{Binding IsThrottleCadenceOverrideEnabled}" Margin="9 8 0 0">
<CheckBox.ToolTip>
<TextBlock Width="300" TextWrapping="Wrap">
Override configured assist level cadence limit to 100% when power
requested by throttle action surpasses power set by pedal assist level.
</TextBlock>
</CheckBox.ToolTip>
<CheckBox.Style>
<Style TargetType="{x:Type CheckBox}">
<Style.Triggers>
<DataTrigger Binding="{Binding IsThrottleEnabled}" Value="False">
<Setter Property="IsEnabled" Value="False" />
</DataTrigger>
<DataTrigger Binding="{Binding IsPasAssistVariableVariant}" Value="True">
<Setter Property="Visibility" Value="Collapsed" />
</DataTrigger>
</Style.Triggers>
</Style>
</CheckBox.Style>
</CheckBox>

<CheckBox Content="Speed" IsChecked="{Binding IsThrottleSpeedOverrideEnabled}" Margin="16 8 0 0">
<CheckBox.ToolTip>
<TextBlock Width="300" TextWrapping="Wrap">
Override configured assist level speed limit to 100% when power
requested by throttle action surpasses power set by pedal assist level.
</TextBlock>
</CheckBox.ToolTip>
<CheckBox.Style>
<Style TargetType="{x:Type CheckBox}">
<Style.Triggers>
<DataTrigger Binding="{Binding IsThrottleEnabled}" Value="False">
<Setter Property="IsEnabled" Value="False" />
</DataTrigger>
<DataTrigger Binding="{Binding IsPasAssistVariableVariant}" Value="True">
<Setter Property="Visibility" Value="Collapsed" />
</DataTrigger>
</Style.Triggers>
</Style>
</CheckBox.Style>
</CheckBox>
</StackPanel>


<TextBlock Grid.Column="0" Grid.Row="7" Text="Max Throttle Current (%):" VerticalAlignment="Center" Margin="0 8 0 0">
<TextBlock.Style>
<Style TargetType="{x:Type TextBlock}">
Expand Down
38 changes: 36 additions & 2 deletions src/tool/ViewModel/AssistLevelViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,15 +102,17 @@ public ValueItemViewModel<AssistBaseType> SelectedType
MaxSpeedPercent = 0;
TorqueAmplificationFactor = 0;
_level.Type = ClearThrottleFlag(_level.Type);
_level.Type = ClearThrottleCadenceOverrideFlag(_level.Type);
_level.Type = ClearPasVariantFlag(_level.Type);
IsThrottleCadenceOverrideEnabled = false;
IsThrottleSpeedOverrideEnabled = false;
break;
case AssistBaseType.Throttle:
TargetCurrentPercent = 0;
TorqueAmplificationFactor = 0;
TargetCurrentPercent = 0;
_level.Type = ClearPasVariantFlag(_level.Type);
_level.Type = ClearThrottleCadenceOverrideFlag(_level.Type);
IsThrottleCadenceOverrideEnabled = false;
IsThrottleSpeedOverrideEnabled = false;
break;
case AssistBaseType.Pas:
MaxThrottlePercent = 0;
Expand Down Expand Up @@ -152,6 +154,7 @@ public ValueItemViewModel<AssistPasVariant> SelectedPasVariant
TorqueAmplificationFactor = 0;
IsThrottleEnabled = false;
IsThrottleCadenceOverrideEnabled = false;
IsThrottleSpeedOverrideEnabled = false;
MaxThrottlePercent = 0;
break;
case AssistPasVariant.Cadence:
Expand Down Expand Up @@ -188,6 +191,18 @@ public bool IsThrottleCadenceOverrideEnabled
}
}

public bool IsThrottleSpeedOverrideEnabled
{
get { return _level.Type.HasFlag(Configuration.AssistFlagsType.SpeedOverride); }
set
{
if (value != IsThrottleSpeedOverrideEnabled)
{
_level.Type = ApplyThrottleSpeedOverrideFlag(value, _level.Type);
OnPropertyChanged(nameof(IsThrottleSpeedOverrideEnabled));
}
}
}

public bool IsPasAssistVariableVariant
{
Expand Down Expand Up @@ -359,5 +374,24 @@ private static Configuration.AssistFlagsType ApplyThrottleCadenceOverrideFlag(bo
return result;
}

private static Configuration.AssistFlagsType ClearThrottleSpeedOverrideFlag(Configuration.AssistFlagsType flags)
{
byte f = (byte)flags;
f &= (byte)~(Configuration.AssistFlagsType.SpeedOverride);

return (Configuration.AssistFlagsType)f;
}

private static Configuration.AssistFlagsType ApplyThrottleSpeedOverrideFlag(bool enabled, Configuration.AssistFlagsType flags)
{
var result = ClearThrottleSpeedOverrideFlag(flags);
if (enabled)
{
result |= Configuration.AssistFlagsType.SpeedOverride;
}

return result;
}

}
}
4 changes: 2 additions & 2 deletions src/tool/bbs-fw-tool.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
<UseWPF>true</UseWPF>
<AssemblyName>BBSFWTool</AssemblyName>
<Authors>Daniel Nilsson</Authors>
<AssemblyVersion>1.3.1.0</AssemblyVersion>
<FileVersion>1.3.1.0</FileVersion>
<AssemblyVersion>1.4.0.0</AssemblyVersion>
<FileVersion>1.4.0.0</FileVersion>
</PropertyGroup>

<ItemGroup>
Expand Down

0 comments on commit 7dcda82

Please sign in to comment.