|
|
|
![]() |
|
|||||||
|
||||||||
![]() |
| Thread Tools | Rate Thread | Display Modes |
|
#16
|
||||
|
||||
|
Re: Using encoder with talon SRX
If you are using RobotBuilder then you can to a "whenPressed" action...
https://wpilib.screenstepslive.com/s...e-to-a-command If you are using command classes yourself there is a Button class you could wire into the scheduler. If you are just writing straight java, I usually just copy/paste the following... Code:
//instance variables
boolean [] btns = new boolean [] {false,false,false,false,false};
boolean [] last = new boolean [] {false,false,false,false,false};
Code:
//get latest
public void teleopPeriodic() {
btns[1] = _joy.getRawButton(1);
btns[2] = _joy.getRawButton(2);
btns[3] = _joy.getRawButton(3);
btns[4] = _joy.getRawButton(4);
Code:
if(!last[3] && btns[3]){
//btn3 just pressed, do something
}
Code:
// bottom of teleop save all btns into last for(i=0;i<last.length;++i) last[i] = btns[i]; |
|
#17
|
|||
|
|||
|
Re: Using encoder with talon SRX
As for the self-test and PID things - I had already verified the encoder position is increasing when the talon flashes green so we're good there. The pid values were just ones that were in example earlier in the thread. Honestly I have a bit of a mental block on PID. I must have read the tutorials a dozen times. I'll take a look at it in just a moment. Thankfully I've got my first decent stretch of time with a test rig of my own so I can use my usual tactic of sheer stubbornness to get this down pat. Thanks for all the help everyone.
|
|
#18
|
|||
|
|||
|
Re: Using encoder with talon SRX
I think I had found my problem - I am using the 2016 version of Robotbuilder I built from source (yay talon srx now works!) - I hadn't noticed when I threw the button in that it defaulted to while pressed not when pressed.
Quote:
|
|
#19
|
|||
|
|||
|
Re: Using encoder with talon SRX
Quote:
I want to travel 10' on one wheel. 1: Asuming a 6" wheel diameter I get a circumfrence of 18.84 inches I believe this means I will need to travel 6.36 revolutions. 2: I am using the e4p-360 encoder so its 360 ticks per revolution, except that its quadrature so I multiply that by 4 meaning we really see 1440 counts per revolution. Based on that I think I want a target of +9158.4 (or negative if I'm going backwards. 3: (a little hazier here) - Do I need to include current position or is that implied? My concern is I don't want to do whatever it takes to get to count 5000, I want to add or subtract 5000 to where I am now - or do I need to reset the counter to 0 first? Code:
int currentPosition = talon.getEncPosition(); talon.set((currentPosition + 9158)); 'I' is the Integral - a smaller value gives faster control? - Is there a range to work within or a rough rule of thumb? 'D' is the derivative which if I understood correctly helps prevent overshoot? Higher value reacts faster? I've tried the numbers from both previous examples and I'm going to continue to try them, but what I don't understand is how they were chosen. Also in all of this is there a way to indicate an acceptable error margin? Like +/- 5 inches or something? Finally - when I call this command is there a way for me to set how fast I want to travel (like 1/2 speed or something?) Last edited by riftware : 11-24-2015 at 09:31 PM. |
|
#20
|
|||
|
|||
|
Re: Using encoder with talon SRX
I may have just got it to work (perhaps)!
I think my problem was I was calling talon.set(9158) instead of setPosition(9158) I say perhaps because the selftest screen shows much different numbers than talon.getEncPosition() ...still working on it but I may be making progress. |
|
#21
|
|||
|
|||
|
Re: Using encoder with talon SRX
Ok - I seem to have gotten it working. Understanding is coming slowly. By calling set or setPosition (either seems to work?) I am basically telling the system the encoder current position is 9158 away from a 0 error state, at which point the PID system works to try and correct that. These values seem to work for me, not really sure why (placebo effect maybe?) but the longer version of setPid really seemed to cut down on the overshoot issue. The following code isn't pretty and I'm definitely cleaning it up but it gives me repeatable results. If anyone has any comments or wants to help me know why the values I've chosen seem to work well I'll be happy but I'm no longer banging my head against the wall. Thank you to everyone who patiently helped out. I'm excited that we may have a more intelligent autonomous mode than previous years (drive forward 4 seconds to get into next zone).
Code:
protected void execute() {
if(busy) {return;}
busy = true;
finished = false;
System.out.println("Start of Execute");
RobotDrive drive = RobotMap.driveSystemdrive;
CANTalon talon = RobotMap.driveSystemCANTalon2;
talon.changeControlMode(ControlMode.Position); //Change control mode of talon, default is PercentVbus (-1.0 to 1.0)
talon.setFeedbackDevice(FeedbackDevice.QuadEncoder); //Set the feedback device that is hooked up to the talon
talon.setPID(0.5, 0.001, 0.00, 0.00, 360, 36, 0); //Set the PID constants (p, i, d)
talon.enableControl(); //Enable PID control on the talon
int currentPosition = talon.getEncPosition();
System.out.println(currentPosition);
talon.setPosition( 9158);
}
// Make this return true when this Command no longer needs to run execute()
protected boolean isFinished() {
if(RobotMap.driveSystemCANTalon2.getPosition() < 80){
finished = true;
busy = false;
}
return finished;
}
|
|
#22
|
||||
|
||||
|
Re: Using encoder with talon SRX
set() and setPosition() do not do the same thing. set() changes the setpoint of the motor (make it go to 7530 counts, etc.), while setPosition() "zeroes" the encoder, keeping the setpoint the same. For example, setPosition(0) redefines the current encoder position to be 0. However, if the previous setpoint was 7350 before zeroing, it will auto-correct to the new 7350 (7350 counts ahead of where the old 7350 used to be).
|
![]() |
| Thread Tools | |
| Display Modes | Rate This Thread |
|
|