Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improvements to HV behavior #30

Open
wants to merge 2 commits into
base: v1.1-preview2-devel1
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 16 additions & 6 deletions src/haptic_feedback.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,9 @@ static HapticFeedbackType state_to_haptic_type(const State *state) {
case SAT_PB_TEMPERATURE:
return HAPTIC_FEEDBACK_ERROR_TEMPERATURE;
case SAT_PB_LOW_VOLTAGE:
return HAPTIC_FEEDBACK_ERROR_LO_VOLTAGE;
case SAT_PB_HIGH_VOLTAGE:
return HAPTIC_FEEDBACK_ERROR_VOLTAGE;
return HAPTIC_FEEDBACK_ERROR_HI_VOLTAGE;
default:
return HAPTIC_FEEDBACK_NONE;
}
Expand All @@ -64,7 +65,8 @@ static uint8_t get_beats(HapticFeedbackType type) {
return 0;
case HAPTIC_FEEDBACK_ERROR_TEMPERATURE:
return 6;
case HAPTIC_FEEDBACK_ERROR_VOLTAGE:
case HAPTIC_FEEDBACK_ERROR_LO_VOLTAGE:
case HAPTIC_FEEDBACK_ERROR_HI_VOLTAGE:
return 8;
case HAPTIC_FEEDBACK_NONE:
break;
Expand All @@ -79,7 +81,8 @@ static const CfgHapticTone *get_haptic_tone(const HapticFeedback *hf) {
case HAPTIC_FEEDBACK_DUTY_CONTINUOUS:
return &hf->cfg->duty;
case HAPTIC_FEEDBACK_ERROR_TEMPERATURE:
case HAPTIC_FEEDBACK_ERROR_VOLTAGE:
case HAPTIC_FEEDBACK_ERROR_LO_VOLTAGE:
case HAPTIC_FEEDBACK_ERROR_HI_VOLTAGE:
return &hf->cfg->error;
case HAPTIC_FEEDBACK_NONE:
break;
Expand Down Expand Up @@ -145,9 +148,16 @@ void haptic_feedback_update(
float speed = VESC_IF->mc_get_speed();
const CfgHapticTone *tone = get_haptic_tone(hf);
if (tone->strength > 0.0f) {
VESC_IF->foc_play_tone(
0, tone->frequency, scale_strength(tone->strength, hf->cfg, speed)
);
float strength = scale_strength(tone->strength, hf->cfg, speed);
if (type_to_play == HAPTIC_FEEDBACK_ERROR_HI_VOLTAGE) {
float duration = current_time - hf->start_time;
if (duration > 3) {
// more than 3 seconds in HV? Start scaling up for next 2 seconds
// up to 50% louder
strength = strength * fminf(1.5, duration * 0.25 + 0.25);
}
}
VESC_IF->foc_play_tone(0, tone->frequency, strength);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a second special-case being added 🥲.

Also, this raises the strength above the user-set threshold, and by as much as 50%. Doesn't seem like a good thing to do.

If I was to contrive a disaster scenario, someone configures their haptic feedback and tests it at the configured levels (no way to easily know what this +50% strength will do and even that it can actually happen on HV). Then, months later, they trigger HV on a steep downhill. They don't react in time, the loud haptics mess up the imu and make the nose go higher, they go into taildrag and crash.

}

if (hf->cfg->vibrate.strength > 0.0f) {
Expand Down
3 changes: 2 additions & 1 deletion src/haptic_feedback.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ typedef enum {
HAPTIC_FEEDBACK_DUTY,
HAPTIC_FEEDBACK_DUTY_CONTINUOUS,
HAPTIC_FEEDBACK_ERROR_TEMPERATURE,
HAPTIC_FEEDBACK_ERROR_VOLTAGE,
HAPTIC_FEEDBACK_ERROR_LO_VOLTAGE,
HAPTIC_FEEDBACK_ERROR_HI_VOLTAGE,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think the one or two extra chars will change anything, so I'd prefer the HIGH and LOW spelling.

} HapticFeedbackType;

typedef struct {
Expand Down
12 changes: 6 additions & 6 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -755,20 +755,20 @@ static void calculate_setpoint_target(data *d) {
} else if (d->motor.duty_cycle > 0.05 && input_voltage > d->float_conf.tiltback_hv) {
d->beep_reason = BEEP_HV;
beep_alert(d, 3, false);
if (((d->current_time - d->tb_highvoltage_timer) > .5) ||
(input_voltage > d->float_conf.tiltback_hv + 1)) {
// 500ms have passed or voltage is another volt higher, time for some tiltback
if ((d->current_time - d->tb_highvoltage_timer) > 5) {
// It is assumed that haptic feedback is enabled!
// 5s have passed or voltage is another volt higher, time for some tiltback
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You've removed the "another volt higher" part of the condition. A case in point with these comments, self-explanatory code is better.

But more importantly, at this stage we can't make that assumption, which in my eyes just invalidates the PR.

if (d->motor.erpm > 0) {
d->setpoint_target = d->float_conf.tiltback_hv_angle;
} else {
d->setpoint_target = -d->float_conf.tiltback_hv_angle;
}

d->state.sat = SAT_PB_HIGH_VOLTAGE;
} else {
// The rider has 500ms to react to the triple-beep, or maybe it was just a short spike
d->state.sat = SAT_NONE;
d->setpoint_target = 0;
}
// setting the state regardless to ensure haptic buzz starts right away
d->state.sat = SAT_PB_HIGH_VOLTAGE;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not super major, but for awareness: This function is bound for a refactor, I'd like to change it so that only the state.sat is set based on the conditions, and then there'll be a function that sets setpoint_target according to sat (haven't fully thought through some of the cases but that's the rough idea).

This special-case throws a wrench into that plan, and is not pretty as it is. Obviously if this behavior is desired it can be worked out and it should be prettier after the refactor... But it's better to not have special cases.

} else if (VESC_IF->mc_temp_fet_filtered() > d->mc_max_temp_fet) {
// Use the angle from Low-Voltage tiltback, but slower speed from High-Voltage tiltback
beep_alert(d, 3, true);
Expand Down
Loading