# How do you make a braking program?

How do you make a braking program? preferably in the program.

Thank you

us If you want to brake your drive in the case of hard defense you may want a mechanical brake, but doing it with code is possible.

You will need encoders on each side of your drive train, and set up a proportional loop (or use a complete pid loop if you’re experienced) per side.

Your code, ultimately, will want your encoders at zero ticks when you hold your brake button. With a proportional loop, when an encoder begins to tick away from zero, the corresponding side of your drive will fight back resulting in a brake.

-If you need more info on the P-loop I can post again.

Here was an old post on a braking command to keep an autonomous drive function from overshooting. You might find it of interest.

Chris, Thank you for your enormous contribution to this forum. I have found your work and posts, and those of jpearman, on the PTC temperature problem very beneficial for understanding the problem and addressing it.

I read through your above-referenced blog post on the braking question. I am not quite able to decipher the physics behind your equations. More specifically, shouldn’t this be governed in part by Newton’s law of motion? Wouldn’t robot’s acceleration, drag force (though probably small here), and rolling resistance play parts in determining how much the robot would coast (without braking) before it comes to a complete stop? Wouldn’t that effect the brake calculations?

Yes to all… In the post I assumed that the braking force would be commanded by a reverse full command. I also assumed that these forces would dominate the drag and rolling resistance forces. What is left are the dynamics of an ideal motor with back emf votage feeding back to the torque calculation. This is what we call dynamic braking from a motor controller. The dynamic braking results in a first order lag response with a time constant that is the ratio of the maximum motor speed divided by the maximum motor acceleration. The derivation of the first order lag response comes from Newtons law of motion which results in a first order differential equation.

Lets review the force equation:

N_motor = number of motors
M = mass of robot
R_m = motor resistance
V_cmd = motor voltage input from the controller command
Vemf = motor back emf voltage
torq = total torque the wheels are creating from ground forces
kt = motor torque constant = torq_max/(I_max - I_free)
ke = motor speed constant = kt for most motors.
v = robot speed, dv/dt = rate of change of speed or acceleration
v_cmd = speed command equivalant to V_cmd voltage

torq = N_motorskt(V_cmd - Vemf)/R_m

dv/dt = accel = (torq/r)/M ; (a = F/M here is Newton)

Now to get this into a nice differential equation involving v we must make use of the fact that Vemf = v/r*ke.

dv/dt = N_motorskt(V_cmd - v/r*ke)/R_m/r/M

With some further manipulation this can be rewritten in terms of speed and speed command as

dv/dt = (v_cmd - v)/tau which has a first order lag response solution

v= v_init*(exp (-t/tau) + v_final*(1 - exp(-t/tau))

where tau = maximum speed/maximum acceleration.

One could also play with a for() loop. They aren’t as complex as Pid (not PID, lol) Here is an example below: (if you happen to have access to the intellitek Robotics Class curriculum, there is a section devoted to this kind of thing).

``````for ( variable = 0 ; variable <= 127 ; variable = variable + 5 ) // Change the 5 to change how increments of PWM
{
SetMotor ( 1 , (127 - variable) ) ;
Wait ( 50 ) ; // You can change this, too, to change how fast the bot slows down
// You will need to play with the commented numbers above to find
}
``````

In case you don’t know how a for() loop works (or for future viewers), a for loop defines a variable first. We defined variable as 0 (variable = 0). By the way, you don’t have to actually set that variable equal to something (if you wanted to use a Sensor Value, or something.

You then put a logical test. In this case, we said while variable is less than or equal to 127. (variable <= 127) If the logical test fails, the for loop won’t run.

Finally, when the robot reaches the end of the loop, it will run the final piece of code, where you increment the code, in this case we increment by 5 (variable = variable + 5). Overall, the syntax for the for() loop is

``````
for (initial; condition; increment)

``````

Please let me know if you have any questions.

P.S. If you are programming in ROBOTC, the for() syntax is the same, but you will have different SetMotor() and Wait() statements, fitting ROBOTC syntax. I’ll let you translate as needed.