-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
346 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
// Copyright 2024 Syler Clayton | ||
// | ||
// This file is part of the Refloat VESC package. | ||
// | ||
// Refloat VESC package is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License as published by the | ||
// Free Software Foundation, either version 3 of the License, or (at your | ||
// option) any later version. | ||
// | ||
// Refloat VESC package is distributed in the hope that it will be useful, but | ||
// WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY | ||
// or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | ||
// more details. | ||
// | ||
// You should have received a copy of the GNU General Public License along with | ||
// this program. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
#include "haptic_feedback.h" | ||
|
||
void haptic_feedback_configure(HapticFeedback *haptic_feedback, RefloatConfig *float_conf) { | ||
haptic_feedback->duty.type = float_conf->haptic_feedback_duty; | ||
haptic_feedback->duty.voltage = float_conf->haptic_feedback_voltage; | ||
haptic_feedback->duty.freq = float_conf->haptic_feedback_duty_freq; | ||
haptic_feedback->error.type = float_conf->haptic_feedback_error; | ||
haptic_feedback->error.voltage = float_conf->haptic_feedback_voltage; | ||
haptic_feedback->error.freq = float_conf->haptic_feedback_error_freq; | ||
haptic_feedback->rumble_voltage = float_conf->haptic_feedback_rumble_voltage; | ||
haptic_feedback->rumble_freq = float_conf->haptic_feedback_rumble_freq; | ||
haptic_feedback->alt_delay = float_conf->haptic_feedback_alt_delay; | ||
} | ||
|
||
void haptic_feedback_reset(HapticFeedback *haptic_feedback, float current_time) { | ||
if (haptic_feedback->tone_in_progress) { | ||
VESC_IF->foc_stop_audio(true); | ||
} | ||
haptic_feedback->tone_in_progress = false; | ||
haptic_feedback->timer = current_time; | ||
haptic_feedback->alt_timer = current_time; | ||
haptic_feedback->alt_state = false; | ||
} | ||
|
||
void haptic_feedback_update( | ||
HapticFeedback *haptic_feedback, State *state, float current_time, float note_duration | ||
) { | ||
if (state->mode == MODE_FLYWHEEL || !(VESC_IF->foc_play_tone && VESC_IF->foc_stop_audio)) { | ||
return; | ||
} | ||
HapticFeedbackConfig haptic_feedback_state; | ||
if (state->state == STATE_RUNNING && state->sat >= SAT_PB_DUTY) { | ||
if (state->sat == SAT_PB_DUTY) { | ||
haptic_feedback_state = haptic_feedback->duty; | ||
} else if (state->sat > SAT_PB_DUTY) { | ||
haptic_feedback_state = haptic_feedback->error; | ||
} | ||
if ((haptic_feedback_state.type > HAPTIC_FEEDBACK_NONE)) { | ||
if (!haptic_feedback->tone_in_progress) { | ||
if ((haptic_feedback_state.type == HAPTIC_FEEDBACK_RUMBLE || | ||
(haptic_feedback_state.type == HAPTIC_FEEDBACK_ALTERNATING && | ||
(haptic_feedback_state.voltage == 0.0 || haptic_feedback_state.freq == 0)))) { | ||
haptic_feedback_state.freq = haptic_feedback->rumble_freq; | ||
haptic_feedback_state.voltage = haptic_feedback->rumble_voltage; | ||
haptic_feedback->alt_state = !haptic_feedback->alt_state; | ||
} | ||
if ((haptic_feedback_state.freq > 0) && (haptic_feedback_state.voltage > 0.0)) { | ||
VESC_IF->foc_play_tone( | ||
0, haptic_feedback_state.freq, haptic_feedback_state.voltage | ||
); | ||
haptic_feedback->alt_timer = current_time; | ||
haptic_feedback->tone_in_progress = true; | ||
} | ||
} | ||
haptic_feedback->timer = current_time; | ||
} | ||
} | ||
|
||
if (haptic_feedback->tone_in_progress) { | ||
if (fabsf(haptic_feedback->timer - current_time) > note_duration) { | ||
VESC_IF->foc_stop_audio(true); | ||
haptic_feedback->tone_in_progress = false; | ||
} else if ((haptic_feedback_state.type == HAPTIC_FEEDBACK_ALTERNATING) && (haptic_feedback->alt_delay > 0) && (fabsf(haptic_feedback->alt_timer - current_time) > haptic_feedback->alt_delay/1000.0)) | ||
{ | ||
haptic_feedback->alt_timer = current_time; | ||
haptic_feedback->alt_state = !haptic_feedback->alt_state; | ||
// change tone based on state | ||
haptic_feedback_state.voltage = haptic_feedback->alt_state | ||
? haptic_feedback->rumble_voltage | ||
: haptic_feedback_state.voltage; | ||
haptic_feedback_state.freq = haptic_feedback->alt_state ? haptic_feedback->rumble_freq | ||
: haptic_feedback_state.freq; | ||
if (haptic_feedback_state.voltage > 0.0 && haptic_feedback_state.freq > 0) { | ||
VESC_IF->foc_play_tone( | ||
0, haptic_feedback_state.freq, haptic_feedback_state.voltage | ||
); | ||
} else { | ||
VESC_IF->foc_stop_audio(false); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// Copyright 2024 Syler Clayton | ||
// | ||
// This file is part of the Refloat VESC package. | ||
// | ||
// Refloat VESC package is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License as published by the | ||
// Free Software Foundation, either version 3 of the License, or (at your | ||
// option) any later version. | ||
// | ||
// Refloat VESC package is distributed in the hope that it will be useful, but | ||
// WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY | ||
// or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | ||
// more details. | ||
// | ||
// You should have received a copy of the GNU General Public License along with | ||
// this program. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
#pragma once | ||
#include "conf/datatypes.h" | ||
#include "state.h" | ||
#include "utils.h" | ||
#include <math.h> | ||
|
||
typedef struct { | ||
HAPTIC_FEEDBACK_TYPE type; | ||
float voltage; | ||
int freq; | ||
} HapticFeedbackConfig; | ||
|
||
typedef struct { | ||
float timer, alt_timer; | ||
bool alt_state, tone_in_progress; | ||
HapticFeedbackConfig duty, error; | ||
float rumble_voltage; | ||
int rumble_freq, alt_delay; | ||
} HapticFeedback; | ||
|
||
void haptic_feedback_configure(HapticFeedback *haptic_feedback, RefloatConfig *float_conf); | ||
|
||
void haptic_feedback_reset(HapticFeedback *haptic_feedback, float current_time); | ||
|
||
void haptic_feedback_update( | ||
HapticFeedback *haptic_feedback, State *state, float current_time, float note_duration | ||
); |
Oops, something went wrong.