I am using an h-bridge to drive two dc gear motors and I'm not sure how to lock the motors to prevent momentum. Currently I use the following code to control the motor speed and when I stop I pass 0 but this causes a slow stop with significant momentum:
//
// Set the speed of the motor.
//
analogWrite(motor1EnablePin,leftSpeed);
analogWrite(motor2EnablePin,rightSpeed);
I saw there is a Robot library included with the Arduino IDE that has a method called motorsStop() which is supposed to stop motors stiff versus using Robot.motorsWrite(0,0) which stops slowly. I am not using this library but my goal is to achieve the motorsStop() behavior. How do I do this?
You need another sensor before the zero point so you can apply the brakes in time for the motor to stop at the correct place.
More complex systems use a "deceleration profile".
DeviantSpark:
I saw there is a Robot library included with the Arduino IDE that has a method called motorsStop() which is supposed to stop motors stiff versus using Robot.motorsWrite(0,0) which stops slowly. I am not using this library but my goal is to achieve the motorsStop() behavior. How do I do this?
It all depends on what sort of h-bridge you have.
For example the inexpensive L298N h-bridges have two direction pins for each motor. When one of these pins is set high and one low the enable pin can be controlled with a PWM signal. When the enable pin is high the motor is powered, when the enable pin is low, the motor coasts.
If instead of one direction pin being set high and one low, they are both set low, then when the enable pin is high, the motor will dynamically brake instead of coasting.
Many of the L298P boards use an inverter on one of the direction pins. This allows the direction to be controlled with a single I/O pin. In this situation a separate control pin is used to set the h-bridge in brake mode.
A fun experiment is to take any DC motor (including brushless, but without the controller) and try to spin it with the power leads connected to each other or floating free. It's much more difficult to spin when the leads are shorted together. That's what you want to achieve with your H-bridge.
Hi,
Depending on the motor size and the amount of INERTIA from the load, you will want to check on how much current is going to flow when you do a BRAKE with a short through your H-Bridge, you may need a brake resistor.
What is your application, you may ultimately need a mechanical brake on the motor, or as suggested a deceleration profile, usually meaning a pre-contact switch or a position sensor.
since your request was to not use a mechanical brake, and you have motor control, you can use the motor controller to control speed and should be able to use dynamic braking . much faster than a mechanical brake and zero maintenance !
in the industrial world, they use a servo, but relationship to the toy servos uses in the hobby arena to an industrial servo is similar to that of a Thomas The Tank Engine to a locomotive. the concept is similar, the parts are just different.
in an industrial servo, you use a simple 2 wire DC motor of hp requirements of your needs. fractional up to multiple HP.
add an encoder to the motor shaft. (usually less than $100)
add a servo driver (usually less than $200 for motors up to a few HP)
and your power supply.
this mimics the ability of a stepper to move to a specific location due to the feedback of the encoder.
ramp speed, number of steps, braking, all of it become possible.
For the Arduino world, you can print black and white lines on paper and use an opto to see them for the encoder
you can use the Arduino with this encoder feedback and control your H-Bridge and mimic the driver.
if you use a gray scale encoder pattern, a bit more complicated, but very possible, you can achieve positional awareness of the motor/gears/platform, etc.
Hi,
What is the motor driving, is the gearbox output shaft running faster or slower than the motor shaft?
Does the motor have to stop in the same place(s) each time, or does the stop position move depending on conditions?
How accurate does the stop point have to be?
Hi Tom, this is a 6-12 303 rpm gear motor that is driving my robot. The stop point is not critical as I drive this with my smart phone using bluetooth and just need it to stop quickly rather than coast. I think what DuaneDegn described is what I want to do, but I'm concerned if this will cause damage since if I understand correctly it causes a short circuit? I'm guessing I just leave both pins LOW for a short period of time perhaps. The h-bridge I am using is a L298: http://blog.pennybuying.com/down/f/F815A.pdf
DeviantSpark:
. . . need it to stop quickly rather than coast. I think what DuaneDegn described is what I want to do, but I'm concerned if this will cause damage since if I understand correctly it causes a short circuit?
Dynamic braking is a common method of controlling motors. You could pulse the direction pin low rather than just setting it low but either way should be fine.
I made a tutorial of sorts about using inexpensive L298N boards found on ebay. These boards can be purchased for about $3. You can see at the end of the video, I used two L298N boards to control the motors of my Mecanum wheeled robot.
I personally haven't needed dynamic braking in any of my robot projects. There enough friction in the gearboxes I use to stop a motor relatively quickly by just setting the power to the motors to zero.
You might be able to power the Arduino from the L298N board's 5V regulator. There's a "5V" output on one of the screw terminals. The 5V regulator can't power a Raspberry Pi, but microcontrollers don't need nearly as much current as a Raspberry Pi and I've found the 5V output from the L298N boards I'm using work great when powering microcontrollers.
The motor power supply (labelled "VMS" on your board) should be at least 7.5V in order to meet the L298N chip's specs (motor supply should be 2.5V higher than the control logic voltage).
Thanks DuaneDegn, I used your suggestion for dynamic breaking and it works great. It's still not an instant stop but its definitely good enough. I made all the control pins go low and set the enable pins to 255 for 1 second which brings it to a stop and then I set the control pins for going forward and set the speed to zero.
DeviantSpark:
Thanks DuaneDegn, I used your suggestion for dynamic breaking and it works great. It's still not an instant stop but its definitely good enough. I made all the control pins go low and set the enable pins to 255 for 1 second which brings it to a stop . . .
You're very welcome. It's good to hear it worked well enough for your purposes.
Setting both direction pins high will also dynamical brake the motor. I think the two techniques (setting the direction pins high and setting the direction pins low) can be used to stop a motor but I think there are differences which make one technique more appropriate in some applications than stopping a motor using the other technique.