Programming the Titan
With the 2.0.0 WPILib release a lot of new changes resulted in breaking changes for previous code. Note the examples below for how to do new things.
Adding the Library​
- Java
- C++
import com.studica.frc.Titan;
#include "studica/Titan.h"
Objects and Instances​
- Java
- C++
/**
* Declare the main Titan object
*/
private Titan m_titan0;
private Titan m_titan1;
/**
* Declare the motors of each Titan
* Each object conforms with WPILib SpeedController class for integration with built-in libs
*/
private Titan.Motor m_leftMotor0;
private Titan.Motor m_leftMotor1;
private Titan.Motor m_rightMotor0;
private Titan.Motor m_rightMotor1;
private Titan.Motor m_armMotor0;
private Titan.Motor m_armMotor1;
private Titan.Motor m_armMotor2;
private Titan.Motor m_armMotor3;
/**
* Declare the encoders of each Titan
* Each object conforms with WPILib Encoder class for integration with built-in libs
*/
private Titan.Encoder m_leftEncoder0;
private Titan.Encoder m_leftEncoder1;
private Titan.Encoder m_rightEncoder0;
private Titan.Encoder m_rightEncoder1;
/**
* Constructor
*/
public DriveTrain()
{
/**
* Create the main Titan instances
*/
m_titan0 = new Titan(42);
m_titan1 = new Titan(25);
/**
* Get motor instances and assign to objects
*/
m_leftMotor0 = m_titan0.getMotor(0);
m_leftMotor1 = m_titan0.getMotor(1);
m_rightMotor0 = m_titan0.getMotor(2);
m_rightMotor1 = m_titan0.getMotor(3);
m_armMotor0 = m_titan1.getMotor(0);
m_armMotor1 = m_titan1.getMotor(1);
m_armMotor2 = m_titan1.getMotor(2);
m_armMotor3 = m_titan1.getMotor(3);
/**
* Get encoder instances and assign to objects
*/
m_leftEncoder0 = m_titan0.getEncoder(0, kDistPerTick);
m_leftEncoder1 = m_titan0.getEncoder(1, kDistPerTick);
m_rightEncoder0 = m_titan0.getEncoder(2, kDistPerTick);
m_rightEncoder1 = m_titan0.getEncoder(3, kDistPerTick);
}
Header​
class DriveTrain : public frc2::SubsystemBase
{
public:
DriveTrain();
private:
/**
* Declare the main Titan objects
*/
studica::Titan m_titan0{42};
studica::Titan m_titan1{25};
/**
* Declare the motors of each Titan
* Each object conforms with WPILib SpeedController class for integration with built-in libs
*/
studica::Titan::Motor& m_leftMotor0;
studica::Titan::Motor& m_leftMotor1;
studica::Titan::Motor& m_rightMotor0;
studica::Titan::Motor& m_rightMotor1;
studica::Titan::Motor& m_armMotor0;
studica::Titan::Motor& m_armMotor1;
studica::Titan::Motor& m_armMotor2;
studica::Titan::Motor& m_armMotor3;
/**
* Declare the encoders of each Titan
* Each object conforms with WPILib Encoder Class for integration with built-in libs
*/
studica::Titan::Encoder& m_leftEncoder0;
studica::Titan::Encoder& m_leftEncoder1;
studica::Titan::Encoder& m_rightEncoder0;
studica::Titan::Encoder& m_rightEncoder1;
};
Source​
/**
* Assign reference instances to the objects
*/
DriveTrain::DriveTrain()
: m_leftMotor0 (m_titan0.GetMotor(0))
, m_leftMotor1 (m_titan0.GetMotor(1))
, m_rightMotor0 (m_titan0.GetMotor(2))
, m_rightMotor1 (m_titan0.GetMotor(3))
, m_armMotor0 (m_titan1.GetMotor(0))
, m_armMotor1 (m_titan1.GetMotor(1))
, m_armMotor2 (m_titan1.GetMotor(2))
, m_armMotor3 (m_titan1.GetMotor(3))
, m_leftEncoder0(m_titan0.GetEncoder(0, kDistPerTick))
, m_leftEncoder1(m_titan0.GetEncoder(1, kDistPerTick))
, m_rightEncoder0(m_titan0.GetEncoder(2, kDistPerTick))
, m_rightEncoder1(m_titan0.GetEncoder(3, kDistPerTick))
{
}
Motor Outputs​
Set()​
Set a proportional speed from -1.0 to 1.0, where 0.0 stops the motor.
- Java
- C++
m_leftMotor0.set(speed);
m_leftMotor0.Set(speed);
SetTargetVelocity()​
Set the internal S-Curve algorithm to control the output of the motor. The algorithm will attempt to hit the desired speed with the configured smoothness settings.
- Java
- C++
m_leftMotor0.setTargetVelocity((short)80.0); // Target 80 rpm
m_leftMotor0.SetTargetVelocity(80); // Target 80 rpm
SetTargetAngle()​
When using the Cypher Max as an encoder input, the absolute feature allows direct angle targeting. Set the desired angle for the motor to turn to.
- Java
- C++
m_leftMotor0.setTargetAngle(90.0); // 90 degrees
m_leftMotor0.SetTargetAngle(90.0); // 90 Degrees
Motor Settings​
SetPIDType()​
Sets the PID to Manual configured in the App or using S-Curve.
- Java
m_leftMotor0.setPIDType(0); // Set to Manual Configured
Use Manual Type: 0 when using Set(). When using SetTargetVelocity() or SetTargetAngle() use Type: 1.
SetPositionHold()​
When enabled the controller actively maintains the last commanded position.
- Java
- C++
m_leftMotor0.setPositionHold(true); // use false to release
m_leftMotor0.SetPositionHold(true); // use false to release
SetSensitivity()​
Control the S-Curve velocity control smoothness. Higher values increase smoothness. Range 0 (off) - 10 (full smoothness).
- Java
- C++
m_leftMotor0.setSensitivity(10);
m_leftMotor0.SetSensitivity(10);
At full sensitivity, the acceleration and deceleration become hyper smooth, which can cause problems such as reduced distance control accuracy.
SetCurrentLimit()​
Sometimes you just want to control the current going to a motor.
- Java
- C++
m_leftMotor0.setCurrentLimit(20); // in Amps
m_leftMotor0.SetCurrentLimit(20); // In Amps
SetCurrentLimitMode()​
Enables or disables the current limit.
- Java
- C++
m_leftMotor0.setCurrentLimitMode(0); // disabled, 1 for enabled
m_leftMotor0.SetCurrentLimitMode(0); // disabled, 1 for enabled
InvertRPM()​
Inverts the RPM readings.
- Java
- C++
m_leftMotor0.invertRPM();
m_leftMotor0.InvertRPM();
Motor Inputs​
GetRPM()​
Gets the current RPM reported by the Titan.
- Java
- C++
m_leftMotor0.getRPM();
m_leftMotor0.GetRPM();
GetLimitSwitch()​
Gets the value of the limit switch inputs on the Titan.
These inputs support normal digital input operation. Just disable the limit switch function in SHM.
- Java
- C++
m_leftMotor0.getLimitSwitch(false); // false for forward limit and true for reverse limit
m_leftMotor0.GetLimitSwitch(false); // false for forward, true for reverse limit
GetCypherPosition()​
Get the current angle reported by a Cypher Max.
- Java
- C++
m_leftMotor0.getCypherPosition();
m_leftMotor0.GetCypherPosition();
Encoder Inputs​
GetDistance()​
Reports the distance traveled by the encoder. Set kDistPerTick correctly before using this feature.
- Java
- C++
m_leftEncoder0.getDistance();
m_leftEncoder0.GetEncoderDistance();
GetRaw()​
Reports the raw count from the Titan.
- Java
- C++
m_leftEncoder0.getRaw();
m_leftEncoder0.GetRaw();
GetCount()​
Same as GetRaw() but provides higher accuracy because it avoids double conversion.
- Java
- C++
m_leftEncoder0.getCount();
m_leftEncoder0.GetCount();
GetSpeed()​
Same as GetRPM(), but needed for encoder interface.
- Java
- C++
m_leftEncoder0.getRPM();
m_leftEncoder0.GetSpeed();
Encoder Settings​
InvertSpeed()​
Same as InvertRPM(), needed for encoder interface.
- Java
- C++
m_leftEncoder0.invertSpeed();
m_leftEncoder0.InvertSpeed();
SetReverseDirection()​
Invert the count direction.
- Java
- C++
m_leftEncoder0.setReverseDirection();
m_leftEncoder0.SetReverseDirection();
Reset()​
Resets the encoder count back to 0.
- Java
- C++
m_leftEncoder0.reset();
m_leftEncoder0.Reset();