PID Question

programming
java
pid

#1

Is it possible to use PID with two sensors? E.g. a gyro and an encoder? I can’t find any documentation on having two feedback sensors for PID, or how to architect it. The reason I want to know is we’re using PID to get to a certain location for our robot, but I also want the robot to have a straight heading… We’re trying to use swerve. Anyone got suggestions?


#2

You could use a PID for heading and use the result of that to adjust for the setpoint of your position PID


#3

What you could try is using two separate control loops. If you command your swerve using the forward and strafe components of the velocity combined with a yaw term: drive(forward, strafe, yaw), a position loop will control your forward and strafe while a separate control loop maintains your heading.

For yaw control, I’d recommend only using a P term.

yawError = targetHeading - gyro.getAngle();
yaw = kP * yawError

Then distance can be done the same way (I’d recommend looking into motion profiles).


#4

Hmmm okay. I’ll see if I could test this out with the team.


#5

The term I think you are looking for is ‘cascading PID’. Use that term to search CD for threads on this. That is what I did a few years ago to learn how to do this same thing. There are many ways to do this. Some teams use the more advanced control modes available on talons to this same thing. Pick what you and your team are most comfortable with.

In the past we used two WPILib PidControllers. One used a gyro for input to keep straight. Second used encoders to measure distance. Neither of the PIDs controlled motors directly. We took the output from each PID controller and combined it, then fed it to talons to make the robot move. We used talons and ran them in velocity mode(which is PID but running on talon). So I guess you can say we combined two PIDs which then cascaded to PIDs running on talons. I’m not a controls expert/engineer. Never took a class on it. Everything I learned about control theory has been thru CD and FRC so take my definition/explanation with a grain of salt. If you search on CD you’ll find many more posts from people who know what they are talking about(and alot more math).

You can look at our source code from 2017 to see how we did this one year(it’s not pretty). Most of what would answer your question is in:

The part where PID outputs are combined is here:

The command that uses the PIDs is here:

Let me know if you have any questions… and good luck!


#6

Thank you for the reply, I’ll definitely check it out and get back to you when questions come up. thanks again!