Permalink
Find file
3ac250f Jan 11, 2017
87 lines (79 sloc) 3.12 KB
/**
* Example demonstrating the velocity closed-loop servo.
* Tested with Logitech F350 USB Gamepad inserted into Driver Station]
*
* Be sure to select the correct feedback sensor using SetFeedbackDevice() below.
*
* After deploying/debugging this to your RIO, first use the left Y-stick
* to throttle the Talon manually. This will confirm your hardware setup.
* Be sure to confirm that when the Talon is driving forward (green) the
* position sensor is moving in a positive direction. If this is not the cause
* flip the boolena input to the SetSensorDirection() call below.
*
* Once you've ensured your feedback device is in-phase with the motor,
* use the button shortcuts to servo to target velocity.
*
* Tweak the PID gains accordingly.
*/
package org.usfirst.frc.team469.robot;
import com.ctre.CANTalon;
import edu.wpi.first.wpilibj.IterativeRobot;
import edu.wpi.first.wpilibj.Joystick;
import edu.wpi.first.wpilibj.Joystick.AxisType;
import com.ctre.CANTalon.FeedbackDevice;
import com.ctre.CANTalon.TalonControlMode;
public class Robot extends IterativeRobot {
CANTalon _talon = new CANTalon(0);
Joystick _joy = new Joystick(0);
StringBuilder _sb = new StringBuilder();
int _loops = 0;
public void robotInit() {
/* first choose the sensor */
_talon.setFeedbackDevice(FeedbackDevice.CtreMagEncoder_Relative);
_talon.reverseSensor(false);
//_talon.configEncoderCodesPerRev(XXX), // if using FeedbackDevice.QuadEncoder
//_talon.configPotentiometerTurns(XXX), // if using FeedbackDevice.AnalogEncoder or AnalogPot
/* set the peak and nominal outputs, 12V means full */
_talon.configNominalOutputVoltage(+0.0f, -0.0f);
_talon.configPeakOutputVoltage(+12.0f, 0.0f);
/* set closed loop gains in slot0 */
_talon.setProfile(0);
_talon.setF(0.1097);
_talon.setP(0.22);
_talon.setI(0);
_talon.setD(0);
}
/**
* This function is called periodically during operator control
*/
public void teleopPeriodic() {
/* get gamepad axis */
double leftYstick = _joy.getAxis(AxisType.kY);
double motorOutput = _talon.getOutputVoltage() / _talon.getBusVoltage();
/* prepare line to print */
_sb.append("\tout:");
_sb.append(motorOutput);
_sb.append("\tspd:");
_sb.append(_talon.getSpeed() );
if(_joy.getRawButton(1)){
/* Speed mode */
double targetSpeed = leftYstick * 1500.0; /* 1500 RPM in either direction */
_talon.changeControlMode(TalonControlMode.Speed);
_talon.set(targetSpeed); /* 1500 RPM in either direction */
/* append more signals to print when in speed mode. */
_sb.append("\terr:");
_sb.append(_talon.getClosedLoopError());
_sb.append("\ttrg:");
_sb.append(targetSpeed);
} else {
/* Percent voltage mode */
_talon.changeControlMode(TalonControlMode.PercentVbus);
_talon.set(leftYstick);
}
if(++_loops >= 10) {
_loops = 0;
System.out.println(_sb.toString());
}
_sb.setLength(0);
}
}