[C++] how to brake a motor?

how is it possible by [C++] to brake a motor?
IIUC, using
set_motor_power(int x)
and passing -128 will make the motor float (no rotation resistance or torque),
but how to apply a certain brake power without making the motor go forward or reverse?

A hammer will brake most things if used correctly :wink:

the function set_motor_position will hold the axle at that position. but as stated earlier if you then reset the motor encoder count then the axle will be at different count so the brickpi will correct this to rotate the axle till it is at the correct count.

no, I do not want to set a position, I want to stop by a brake, the encoders are not affected.
e.g., when a mobile robot is running fast and an obstacle is detected very/quite close then the robot has to be stopped by braking the speed to zero - not immediately by brute force, but quick and smooth though, like a brake of a car.
In NXC we had both Off(port) == stop by brake and additionally Coast(port).
coast is like set_motor_power(-128), i.e. switch voltage off and let it roll on -
but how works the brake thing for BrickPi3?

(BTW, different H-Bridges provide this feature by setting various input and enable pins to high or low to rotate, coast, or brake)

I will be interested in what Matt replies.

But you may have to write your own routine to do this that slows the DPS then uses set_motor_position once the speed has slowed enough.

If you are a really good coder you could write an ABS braking system for max performance.

When my computer vision robot started to get so fast it was sliding out on the corners it was suggested to me that I could write a traction control routine.

yes, we will probably wait for what matt replies.
Actually the brake thing is a hardware thing, not software, because the H-Bridges itself provide this feature.
e.g., for a L293/298 H-Bridge type, it’s like

pind1 = HIGH
pind2 = HIGH
pinen = PWM // e.g., 0...255 for brake power

other H-Bridges like MC33926 have for

enable = HIGH
dir = LOW // forward+brake
dir = HIGH // reverse+brake
pwm = PWM // e.g., 1...255 for power by pwm

and BRAKE-LOW (outputs to GND) by
enable = HIGH
dir = (X)
pwm = LOW

so it’s a feature which has to be enabled on the slave boards by the SAMD-pins for local H-Bridge control, as far as technically possible.

Ok my hardware understanding is limited.

I though a H bridge is basically 4 switches that can allow you to reverse polarity so you can run motors forward or backwards.

no, actually most H-Bridges have at least 3 input pins each (some even more than that), for enable, direction, and pwm, and optionally monitoring pins, by different logical states, and however they are coded internally.
This e.g., is a logic table for the MC33926 (2 more pins for monitoring additionally):

Inputs                             Outputs
EN      DIR        PWM        MxA       MxB       operating mode
1        0         PWM      PWM(H/L)     L        forward/brake at speed PWM %
1        1         PWM         L      PWM(H/L)    reverse/brake at speed PWM %
1        X          0          L         L        brake low (outputs shorted to ground)
0        X          X          Z         Z        coast (outputs off)

The L293/L298 types look different of course:

D1       D2        EN              operating mode
1        0         PWM          forward at speed PWM %
0        1         PWM          reverse at speed PWM %
1        1         PWM          brake at PWM %
0        0          0           coast (outputs off)

Those things are crucial to know for Arduino- and native GPIO-pin programmers :sunglasses:

Not sure which type is on the BrickPi3s though.

After reading your post about H bridge being able to hardware brake I googled it.

Seems close S1 and S4 it runs one way or close S2 and S3 and it goes the other which I knew
But if you close S2 and S4 or S1 and S3 it acts like a brake. I am still to understand why this works, Hmmm something to think about

as I stated, the logical pin coding depends on the actual H-Bridge type, they vary a lot.
And as stated, not sure which coding type is on the BrickPi3s though.

But anyway, this is performed by the SAMD MCUs and is supposed to be enabled then by a high-level API command via SPI and the Raspi-C-libs.

Ok I am doing some reading. The braking effect works by you short across the 2 terminals and the spinning motor works like a generator and creates a current that tries to turn the axle in the opposite direction causing a braking effect. The faster the axle is spinning the more the generator effect so the more the braking.

As you stated this is a standard feature in a micro controlled solid state H bridge.

I would be interested to hear if BrickPi has this functionality.

Seems I learn something new everyday :slight_smile:

Use set_motor_power to set the power to 0. That’s basically shorting the pins of the motor together so that it’s hard to rotate (inactive braking).

ok, thanks, so
set_motor_power(0) is brake low
set_motor_power(-128) is coast.

thank you!

To summarize, here are the three braking options:

  • You can use set_motor_power to float the motors by setting the power to < -100 or > 100 (you can use MOTOR_FLOAT which is defined as -128). The motor rotates freely as if it was unplugged.

  • You can use set_motor_power to do an inactive brake by setting the power to 0. The motor is difficult to rotate, as the pins are essentially shorted together.

  • You can use set_motor_position to do an active brake by setting the target position to the current position. The motor will be powered as necessary to actively try to stay at it’s target position.