# Motion Profiling

I’ve been starting to research into some more advanced techniques for robot controlling and I stumbled upon what’s called motion profiling. I looked a little more on this on the internet and I found out a little about the trapezoidal and s-curve profiles. However all I know is the kinematic relationships (velocity, acceleration…) and I’m confused about how to take this to the useful level. Could the awesome robotics community out there help me in learning the basics of motion profiling and how to implement it into a robot design - especially a drive system?

3 Likes

FYI, 254’s code uses trapezoidal motion profiles for all drivetrain moves. If you are willing to dredge through the code, you can see how a team used them last year.

I like to conceptually visualize motion profiles as a filter (even though it is not LTI, so this is an abuse of terminology to me). You tell the filter that you want to go to position “10”, and it tells you what to do for the next time step to get closer to “10” while only moving with a maximum acceleration and velocity that you configure the filter with. This is essentially how we implemented it.

I would start by writing code on your local machine to generate motion profiles and debug it by plotting the output to see if it works or not. It is quite hard to debug this type of stuff on a running robot.

To echo what Austin said: This is a great example of something you can develop and test without a running robot. Write the code to generate a trapezoidal motion profile using J2SE or on a desktop using C++, debug using Excel, Matlab, or Gnu plots, and you will be able to port it to your robot code with ease.

Of course, once you are generating your trapezoidal motion profiles, you will need a controller that makes sure the bot actually follows it! Here are a few ideas on how you could do this:

1. Use a PID loop on your drive motors (in distance mode). Use the speed output of your motion profile to limit the maximum command that PID is allowed to send to your motors.

2. Use a PID loop on your drive motors (in speed mode). As long as your speed loop has an integral term (the I gain is nonzero), this should get you where you are going.

3. Use both (1) and (2) to control both speed and distance; there is also a specialized “PIV” controller used in industrial servos that mixes both speed and distance to command the motors.

4. Just use the speed command you generated to drive the motors directly (open loop), but switch to a PID controller when you get close to the goal to go the last few inches.

5. Make a full-state controller using control theory and simulation; if you go this route you are either insane or 254

3 Likes

Good point Jared. Once you have a profile, you need to follow it.

My favorite controller for following trajectories is as follows (I think this is a modification of 3). I used it before I got tired of hand-tuning the PD for the drivetrain and switched to state feedback.

pwm = P * error + D * ((error - prev_error) / dt - goal_velocity) + Kv * goal_velocity + Ka * goal_acceleration

This is close to a PD controller, but with some feed forward terms. The idea behind feed forwards is that if you have a pretty good idea about how much power it will take to do something, go ahead and add it in. The control loop will take up the slop from there when you are wrong, or someone bumped the bot.

The D term is special in that you subtract off the goal velocity. This falls out from the state feedback controllers. Think about it this way. If you are at the goal, and moving at the right speed, you don’t want to apply corrective power to decelerate the robot (This is what D would do if you weren’t trying to move but were moving and at the goal.)

The Kv and Ka feed forwards terms make a lot of sense. It takes power to move at a velocity, so add that power on… Same for acceleration. It takes power to accelerate, so just add the power right on. You can get the goal velocity and acceleration from your profile.

A properly tuned controller with motion profiles will produce some very nice and smooth motion.

1 Like

Hi,

Realise this is an old thread, but hopefully you will pick this up…

I have been working with PID

controllers for a while now, but have never looked at adding feed forward terms. This looks really interesting for a hydraulic system I’m currently working on.

I have a few questions with regards to your controller above:

1. What is your (dt) term within the D part of your controller.

2. Do you determine the goal_velocity and goal_acceleration from the goal position within the controller, or do you send these in from your trajectroy planner?

3. Can you point me to a code example of this controller?

I have a bunch of other questions but figured I would start with this…

Matt.

DT is the period of your loop.

Acceleration, velocity and position are from the trajectory planner.

254’s 2011 code has this as part of the drive train autonomous code. It should be available in the whitepapers on this site.

Austin

Great thanks, yes I should have known dt would be discrete time! I checked out the code and the trajectory planner was a great help! Imported the ContinuousAccelFilter class into my borland builder test app, works really well! Thanks for the pointers.

So on a side note, if you were sending just position information to the PID

controller, and did not have velocity and acceleration data, could one derive the velocity and acceleration by differentiating the position data with the previous position… I guess this would not produce a true goal velocity, but may give some feed forward gain??

Matt.

You can, if and only if that signal is twice differentiable. If not, then you’ll get bad spikes in your data – if you have spots that are not twice differentiable, you’ll need to differentiate on each side then use each section as a separate input, essentially “cutting out” the bad spots. This is much more useful if the position trajectory is derived via some analytical or numerical process – you’ll likely have a ton of noise if you try to do this with real sensor data.

Also, with full PID

, having a feedforward on the velocity target will cause overshoot, just FYI.* If you’re doing PID
for position control, then having an acceleration feedforward should be fine; a velocity feedforward would be beneficial if you’re doing PD.

If anyone disagrees with any of the statements in this post, please correct me – this is operating at the edge of my knowledge of control theory.

• This statement is incorrect, please see AustinSchuh’s post below.

Matt,

The problem with this method is that discrete differentiation induces noise into the control loop. By using the path planning algorithm you have a pure, non noisy, velocity and acceleration profile.

Paul

I disagree with this statement. I’ll give you a fuzzy explanation now, and try to find time to prove it for real with the math later. I’ll probably cheat and give you the continuous time proof…

You can model the velocity feed forward as a disturbance. It adds a power to the motors that the control loop is not expecting. This power is constant (at least at constant velocity), so, since the loop is stable, it will stabilize out and converge. This is very similar from the loop’s perspective to changing the goal, and since both of them are happening at the same time, it shouldn’t have much effect.

Differentiating the goal signal will also introduce delay into it. This won’t have a huge effect on the system since the time constant of a robot is very slow compared to a good loop frequency, but this can become more and more pronounced as your system’s time constant gets closer to the period of your loop.

I agree with Austin on this one. Velocity feed forward (and acceleration FF, and current FF) all work to stabilize the PID loop. Instead of your proportional gain having to do all the heavy lifting, a small understanding of PWM in value vs actual rotational speed will decrease the proportional gain required. At full voltage the PID is ideally only dealing with disturbances. As the battery dies, the P will have to do a little more work since the FF mapping is no longer valid (but better than 0).

In our FRC auton application it is a huge advantage to use FF gain since you are presumably using a fully charged battery.

Put another way, the P gain is a multiplier. The smaller that multiplier is allowed to be, the less oscillation and general “what the heck just happened” sessions will happen.

1 Like

I’m a bit lost as to why a trapezoidal profile would be better than a well tuned PID

. It seems to me that you want to accelerate as quickly as possible for as long as possible, then decelerate as quickly as possible and stop at your location.

How does a ‘profile’ do that any better than PID

?

That profile actually gives you a predictable and repeatable motion path almost regardless of battery voltage (I say almost because a dead battery is a dead battery). The PID should only be used for disturbance correction. All high end industrial robots that need ridiculously good repeatability use the motion planning method. FANUC uses trapezoidal Acceleration profile and triangular acceleration profile (for really short motions) to control motion. The biggest issue fighting repeatability is Jerk (the derivative of Acceleration) and the trapezoidal acceleration profile helps fight this.

Most wafer robots used in the silicon industry use triangular aceeleration, but the loads are a bit higher with a tradeoff of shorter time to target.

In general, it is good practice to do some sort of motion planning to increase reliability. Most of us FRC people are just too lazy to actually do it and use PID to control everything.

1 Like

I think you’re right – I was making an assumption that I did not state in my post. I was assuming that the velocity feedforward gain (your Kv) is sufficient to cause essentially zero steady-state error without an integral component – if it is less, than it is possible to have no overshoot. Also, I was not claiming that it would be unstable – it would overshoot once then return to the setpoint in a perfectly stable manner.

Here’s where I’m coming from, assuming Kv is as described above (i.e. would cause approximately 0 steady state error with no integral component):

Say you’re moving steadily (tracking a target trajectory) at a velocity of 1 (arbitrary units) – your P, I, and D terms are all approximately zero, since you are at steady state. The Kv term is the majority of your control signal.

Now suppose the target trajectory changes to a velocity of 2 – this could happen either smoothly or instantaneously. Due to the system’s inertia, it will find itself at less than the desired position. Therefore, the control system will correct, trying to bring the state towards the new setpoint.

During this time, the previously-small I term will be growing as the errors accumulate. However, due to the Kv term, the necessary I term to maintain zero steady state error at the setpoint is approximately zero. Since the I term will not begin decreasing until it has overshot, it is guaranteed to overshoot.

This overshoot may or may not be acceptable. Additionally, if Kv is less than the value I’ve described, then it would only decrease the Ki value necessary to prevent overshoot, and decrease the rise time of the controller, which may be desirable.

Ok. After reading a bit more online, I understand some of the terms. Jerk is caused by the transition between acceleration / velocity, velocity / deceleration, or acceleration / deceleration. Those are the ‘sharp’ points on the graph.

To create a motion profile utilizing acceleration and deceleration, don’t you have experimentally measure these to accurately determine what your robot is capable of?

From what I understand, motion profiling would seem to get you to your position point more quickly that PID. With PID, you have your drive values taper off as you get closer to your setpoint. With motion profiling, you would stay at your maximum velocity until it’s time to go into maximum deceleration.

Is this accurate?

Technically, it’s just the derivative of acceleration, but yes, that is effectively correct.

To create a motion profile utilizing acceleration and deceleration, don’t you have experimentally measure these to accurately determine what your robot is capable of?

You just need to get them within the robot’s capabilities – if you’re too close to your limits, then you’ll get wheel slippage anyway, throwing off your distance measurements. You could probably get close enough just by looking at your wheel’s CoF.

From what I understand, motion profiling would seem to get you to your position point more quickly that PID

. With PID
, you have your drive values taper off as you get closer to your setpoint. With motion profiling, you would stay at your maximum velocity until it’s time to go into maximum deceleration.

I think that motion profiling would be slower than directly doing PID

or PD on position, since motion profiling usually has a more gentle acceleration. One tradeoff, however, is that where pure PID
/PD would spin the wheels on acceleration, motion profiling should prevent wheel slippage.

To everyone: Please let me know if I’ve made any mistakes in my interpretation of “motion profiling” – I personally see several ways to do this, so I hope I am responding with the correct interpretation in mind.

A perfect motion profiler (and appropriate controller) will get you to your goal at least as fast as any other method. If the acceleration limit in your profiler is right on the cusp of the stick-slip point for the wheels, you have essentially implemented a form of traction control for your robot.

Of course, the stick-slip point depends on a lot of things (tread wear, carpet irregularities, the amount of weight being carried by each wheel instantaneously, etc.), so in practice you usually want to back off on the acceleration limit by a safety margin. The extra repeatability is worth the handful of milliseconds longer it takes for your robot to get to its goal.

In general, if all your feedback controller is doing is removing noise/error from your planned trajectory, you’re going to have a good time. This is why feedforward gains are so powerful - in the absence of any disturbances, you theoretically will get exactly where you want to go without needing ANY feedback!

Tom,

I am sitting in the HK airport so I have some time to kill. I will try to go through a simple double linear filter motion profile scheme. This is the fastest way, computationally, to do real time motion profiling. However, for FRC autonomous mode applications, the real time filtering really isn’t required.

Some definitiions:

itp = iteration time (loop time)
T1 = Time, in ms for the first filter
T2 = Time, in ms for the second filter
FL1 = Filter 1’s length, unitless. Must be an integer. FL1=RoundUp(T1/itp)
FL2 = Filter 1’s length, unitless. Must be an integer. FL2=RoundUp(T2/itp)

Vprog = Desired Max Speed, ft/sec (can be any units you desire, just be consistent)

Dist = Desired travel distance, ft (can be any units …)

T4 = Time, in ms, to get to destination if always at Vprog. T4 = Dist / Vprog
N = Total number of inputs to the filter, Integer. N = RoundUp (T4/itp)

That is really all you need to do the filtering so here is an example with numbers:

Vprog = 10 ft/sec
Dist = 4 ft
itp = 50ms (doing this to make the math short and easy)
T1 = 200 ms
T2 = 100 ms (this makes it an even trapezoid, as this number increases, it becomes a more traingular accel profile)

T4 = 4/10 *1000 = 400
FL1 = 200/50 = 4
FL2 = 100/50 = 2
N = 400/50 = 8

Ok, now time to fill the filters. How this works is simple. FIlter 1 has FL1 number of boxes, Filter 2 has FL2 # of boxes, and your inputs are N # of 1s until all filters are cleared.

Step # Time Input FL1 FL2 Output (Vel)
1 0 0 0 0 0 0 0 0 0 * Vprog
2 .05 1 1 0 0 0 0 0 1/6 * Vprog
3 .10 1 1 1 0 0 0 0 1/3 * Vprog
4 .15 1 1 1 1 0 0 0 1/2 * Vprog
5 .20 1 1 1 1 1 0 0 2/3 * Vprog
6 .25 1 1 1 1 1 1 0 5/6 * Vprog
7 .30 1 1 1 1 1 1 1 1 * Vprog
8 .35 1 1 1 1 1 1 1 1 * Vprog
9 .40 1 1 1 1 1 1 1 1 * Vprog
10 .45 0 0 1 1 1 1 1 5/6 * Vprog
11 .50 0 0 0 1 1 1 1 2/3 * Vprog
12 .55 0 0 0 0 1 1 1 1/2 * Vprog
13 .60 0 0 0 0 0 1 1 1/3 * Vprog
14 .65 0 0 0 0 0 0 1 1/6 * Vprog
15 .70 0 0 0 0 0 0 0 0 * Vprog

This is now your velocity command. Some interesting statistics:

Total time to end point = (N + FL1 + FL2)*itp
Total time to Max Speed = (FL1 + FL2)itp
Theoretical time to end point = N
itp. This now corresponds to the time when decel starts.

In theory, if your itp time is short enough, then you can simply do a velocity PI routine on each of these commands in the Jag and get great position control. In addition, you can manipulate the ratio between T1 and T2 to get different Velocity trajectories based on your robot’s capabilities. At FANUC, the T1 = 2 * T2 was pretty much a golden rule, but I violated it once or twice on specific painting robot models.

Let me know if you have any questions.

Paul

Filter Motion Profile.xlsx (11.7 KB)

Filter Motion Profile.xlsx (11.7 KB)

1 Like

Paul,

Thanks for sharing that. That’s a very elegant way to do it.

You say about T2, “this makes it an even trapezoid, as this number increases, it becomes a more traingular accel profile”. I don’t follow. In your spreadsheet I can’t see the difference between the two filters (to me it looks like one filter of length FL1+FL2).

Jared,

Graph acceleration vs. time and you will see what I mean. Now I did just do that from memory while sitting in an airport so there is a chance I missed a step. When I get back to my notes I will make sure what I put in this post is correct.

Edit:
Yep, I missed a step. After filter 1 you are supposed to sum filter 1 and divide by the number of steps in filter 1 (in our case, 4) and use that as the input to filter 2. Everything else stays the same. I attached a new Excel file. The resolution of this example stinks so that is why you get the chop in the accel curve.

Paul

Filter Motion Profile.xlsx (15.2 KB)

Filter Motion Profile.xlsx (15.2 KB)