![]() |
Position and velocity PIDControllers using same encoder
Is there any supported way to do this in wpilib? It seems you have to set an encoder to be either kRate or kPosition - what if you want it to be one for one controller, and another for a different one? I suppose you could write a wrapper class that implements PIDSource and contains the encoder, but that seems ugly.
|
Re: Position and velocity PIDControllers using same encoder
Quote:
Alternately, use Python, and pass in the function you want as the PIDSource. |
Re: Position and velocity PIDControllers using same encoder
Are you permitted to wire the encoder to two separate pairs of inputs... one to decode rate and one to decode position? |
Re: Position and velocity PIDControllers using same encoder
Quote:
Not sure whether this or the in-code wrapper is clunkier. Perhaps this is something wpilib could support in the future. |
Re: Position and velocity PIDControllers using same encoder
Quote:
|
Re: Position and velocity PIDControllers using same encoder
Quote:
|
Re: Position and velocity PIDControllers using same encoder
In your code where you set a set point on your velocity PID Controller couldn't you set the encoder to kRate mode and then in the code where you set a set point on your position PID Controller couldn't you set the encoder to kDistance mode? Unless I'm misunderstanding the use case you aren't going to be running both the position and the velocity PID Controllers at the same time so this should work.
|
Re: Position and velocity PIDControllers using same encoder
I haven't tested this, and haven't read the implementation, but you may also be able to create two encoder objects, one for position and one for rate.
Clarify: with the same port numbers. Second edit: out of curiosity, what's your use case for this? |
Re: Position and velocity PIDControllers using same encoder
Quote:
I considered constructing two encoders on the same pins, but I strongly suspect this will throw an error. Will have to check. The use case is controlling a turret - we're wondering if it's possible to do a cascading loop using the same encoder for both the external (position) loop and the internal (velocity) loop. It's probably totally unnecessary to do this (simple fixes for dealing with friction and whatnot work fine), but it's an interesting thought. |
Re: Position and velocity PIDControllers using same encoder
Creating 2 encoder objects on the same pins technically would be allowed, as long as you used the DigitalSource constructors for the encoder, rather then the int constructor, and passed in preconstructed DigitalInput objects. However, this is overkill, as you can pass the same instance of an encoder to 2 separate PIDController objects. This would be my recommended way, as constructing 2 separate encoder objects actually takes away an encoder you could use for another purpose, whereas 2 PIDController objects with the same encoder object wouldn't take any extra resources.
|
Re: Position and velocity PIDControllers using same encoder
Quote:
|
Re: Position and velocity PIDControllers using same encoder
Quote:
|
Re: Position and velocity PIDControllers using same encoder
Quote:
A custom controller that accounts for all three states (position, velocity, and acceleration) in the setpoint would be cleaner in my opinion, and the appropriate modifications to the PIDController class shouldn't be too difficult. By the way, state space controllers do this as well, but they require a reasonably accurate system model while PID does not. |
Re: Position and velocity PIDControllers using same encoder
Quote:
|
Re: Position and velocity PIDControllers using same encoder
Quote:
|
Re: Position and velocity PIDControllers using same encoder
Quote:
|
Re: Position and velocity PIDControllers using same encoder
Quote:
|
Re: Position and velocity PIDControllers using same encoder
Quote:
I thought of another option that avoids needing to do any trickery with multiple Encoder objects. You could inherit from the PIDController class with two classes called PositionPIDController and VelocityPIDController. They would both override calculate(), call m_pidInput.setPIDSourceType() with the PIDSourceType they each want to use, then call the base class calculate(). As a side note, you may need to synchronize access to the Encoder instance in calculate() since the PIDController instances are sharing it. |
Re: Position and velocity PIDControllers using same encoder
Quote:
https://github.com/team2485/frc-2016...dDistance.java |
| All times are GMT -5. The time now is 11:05. |
Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi