|
|
|
![]() |
|
|||||||
|
||||||||
![]() |
|
|
Thread Tools | Rate Thread | Display Modes |
|
|
|
#1
|
|||
|
|||
|
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.
|
|
#2
|
||||
|
||||
|
Re: Position and velocity PIDControllers using same encoder
Quote:
Alternately, use Python, and pass in the function you want as the PIDSource. Last edited by nickbrickmaster : 14-10-2016 at 20:11. |
|
#3
|
||||
|
||||
|
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? |
|
#4
|
|||
|
|||
|
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. |
|
#5
|
||||
|
||||
|
Re: Position and velocity PIDControllers using same encoder
Do you want the FPGA to compute your instantaneous rate and position? Does the wrapper approach accomplish that?
|
|
#6
|
|||
|
|||
|
Re: Position and velocity PIDControllers using same encoder
Wrapper should accomplish this, as encoder does have both a getPosition and getRate method - it's just that you can only set it to have PIDGet() call one of them or the other.
|
|
#7
|
||||
|
||||
|
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.
|
|
#8
|
||||
|
||||
|
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? Last edited by nickbrickmaster : 14-10-2016 at 21:44. |
|
#9
|
|||
|
|||
|
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. |
|
#10
|
||||
|
||||
|
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.
|
|
#11
|
|||
|
|||
|
Re: Position and velocity PIDControllers using same encoder
Quote:
|
|
#12
|
||||
|
||||
|
Re: Position and velocity PIDControllers using same encoder
Oh I guess thats true. A small wrapper class would allow that however, and that class should be pretty thin.
|
|
#13
|
||||||
|
||||||
|
Re: Position and velocity PIDControllers using same encoder
Quote:
https://github.com/team2485/frc-2016...dDistance.java |
|
#14
|
||||
|
||||
|
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. |
|
#15
|
|||
|
|||
|
Re: Position and velocity PIDControllers using same encoder
This is the idea. As I said, it's probably completely unnecessary - but I didn't see any reason why it couldn't be done, and thought it might be worth playing with.
|
![]() |
| Thread Tools | |
| Display Modes | Rate This Thread |
|
|