Skip to content

Commit

Permalink
motor: add short brake to reduce the distance to full stop
Browse files Browse the repository at this point in the history
Result with USE_SHORT_BRAKE
    MOTOR1 SPEED   0.46 m/s   4.13 rad/s STOP  0.014 m
Result without USE_SHORT_BRAKE
    MOTOR1 SPEED   0.46 m/s   4.10 rad/s STOP  0.135 m
You can see that the distance to full stop is much longer
without USE_SHORT_BRAKE. The robot might collide. And it might slide
on a hill as motors are turned off. The short brake mode (aka slow decay)
also provides better speed control. More details on the adafruit.
https://learn.adafruit.com/improve-brushed-dc-motor-performance/current-decay-mode

Signed-off-by: Thomas Chou <[email protected]>
  • Loading branch information
hippo5329 committed Feb 5, 2024
1 parent 1de6dc5 commit 1ba8117
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions firmware/lib/motor/default_motor.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ class Generic2: public MotorInterface
{
if (in_a_pin_ < 0) return;
analogWrite(pwm_pin_, 0);
#ifdef USE_SHORT_BRAKE
digitalWrite(in_a_pin_, HIGH); // short brake
digitalWrite(in_b_pin_, HIGH);
#endif
}
};

Expand Down Expand Up @@ -135,20 +139,31 @@ class BTS7960: public MotorInterface
private:
int in_a_pin_;
int in_b_pin_;
int pwm_max_;

protected:
void forward(int pwm) override
{
if (in_a_pin_ < 0) return;
#ifdef USE_SHORT_BRAKE
analogWrite(in_a_pin_, pwm_max_ - abs(pwm));
analogWrite(in_b_pin_, pwm_max_); // short brake
#else
analogWrite(in_a_pin_, 0);
analogWrite(in_b_pin_, abs(pwm));
#endif
}

void reverse(int pwm) override
{
if (in_a_pin_ < 0) return;
#ifdef USE_SHORT_BRAKE
analogWrite(in_b_pin_, pwm_max_ - abs(pwm));
analogWrite(in_a_pin_, pwm_max_); // short brake
#else
analogWrite(in_b_pin_, 0);
analogWrite(in_a_pin_, abs(pwm));
#endif
}

public:
Expand All @@ -158,6 +173,7 @@ class BTS7960: public MotorInterface
in_b_pin_(in_b_pin)
{
if (in_a_pin_ < 0) return;
pwm_max_ = (1 << pwm_bits) - 1;
pinMode(in_a_pin_, OUTPUT);
pinMode(in_b_pin_, OUTPUT);

Expand All @@ -180,6 +196,7 @@ class BTS7960: public MotorInterface
in_b_pin_(in_b_pin)
{
if (in_a_pin_ < 0) return;
pwm_max_ = (1 << pwm_bits) - 1;
pinMode(in_a_pin_, OUTPUT);
pinMode(in_b_pin_, OUTPUT);

Expand All @@ -199,8 +216,13 @@ class BTS7960: public MotorInterface
void brake() override
{
if (in_a_pin_ < 0) return;
#ifdef USE_SHORT_BRAKE
analogWrite(in_a_pin_, pwm_max_);
analogWrite(in_b_pin_, pwm_max_); // short brake
#else
analogWrite(in_b_pin_, 0);
analogWrite(in_a_pin_, 0);
#endif
}
};

Expand Down

0 comments on commit 1ba8117

Please sign in to comment.