Skip to content

Commit

Permalink
Fix reverse motor stuttering.
Browse files Browse the repository at this point in the history
  • Loading branch information
andyblarblar committed Feb 14, 2024
1 parent 1ad4f02 commit 8796282
Showing 1 changed file with 17 additions and 10 deletions.
27 changes: 17 additions & 10 deletions lib/tinykart/esc.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ class ESC {
int max_pwm_duty;
/// Max PWM period in ms
float period;
/// Tracks if we were moving forward, for clearing locks
bool was_forward = false;

public:
/// Initializes an ESC.
Expand All @@ -30,7 +32,7 @@ class ESC {
}

/// Arms the ESC. Set to neutral after exiting.
void arm() const {
void arm() {
this->set_reverse_raw(1.0);
delay(int(period * 2));

Expand All @@ -42,23 +44,25 @@ class ESC {
}

/// Sets the motor to neutral.
void set_neutral() const {
void set_neutral() {
// Neutral is 1.5ms periods
auto value = uint16_t((1.5 / period) * max_pwm_duty);
analogWrite(pwm_pin, value);
}

/// Sets the esc to power forward with some 0.0-1.0 percent.
void set_forward(float power) const {
void set_forward(float power) {
assert(power <= 1.0 && power >= 0.0);

this->was_forward = true;

// Forward is 1.5-2.0ms periods
auto value = uint16_t(((1.5 + power * 0.5) / period) * max_pwm_duty);
analogWrite(pwm_pin, value);
}

/// Forces the ESC to move in reverse. May not work without clearing the lockout.
void set_reverse_raw(float power) const {
void set_reverse_raw(float power) {
assert(power <= 1.0 && power >= 0.0);

// Reverse is 1.0-1.5ms periods
Expand All @@ -67,15 +71,18 @@ class ESC {
}

/// Sets the esc to power reverse with some 0.0-1.0 percent.
void set_reverse(float power) const {
void set_reverse(float power) {
assert(power <= 1.0 && power >= 0.0);

this->set_reverse_raw(0.4);
delay(uint16_t(period * 2));

// Clear lockout
this->set_neutral();
delay(uint16_t(period * 4));
if (this->was_forward) {
this->set_reverse_raw(0.4);
delay(uint16_t(period * 2));

this->set_neutral();
delay(uint16_t(period * 4));
this->was_forward = false;
}

this->set_reverse_raw(power);
}
Expand Down

0 comments on commit 8796282

Please sign in to comment.