Motors & Encoders
Description
This section covers the basics of using different kinds of motor controllers and encoders. Motors are typically controlled through either PWM or CAN. PWM is a protocol that uses a single wire to send data to the motor controller, so it cannot provide back any information or sensor data from the motor. CAN is more advanced and allows for more information to be sent to and from the motor controller, such as configuration data, encoder position, temperature, current draw, etc.
Encoder
Typically when using dedicated encoders, you can use the frc::Encoder class.
Header
#include <frc/Encoder.h>
Initialization
frc::Encoder encoder{0, 1};
Usage
encoder.Get(); // Position (in ticks)
encoder.GetRate(); // Velocity (RPM)
PWM Servo
PWM Servos can be controlled with the frc::Servo class.
Header
#include <frc/Servo.h>
Initialization
frc::Servo servo { 0 };
Percent Output
servo.Set(0.5);
CAN Spark Max
CAN Spark Max motor controllers are used to control REV motors, such as Neos or Neo 550s. They can be controlled with the rev::CANSparkMax class.
Header
#include <rev/CANSparkMax.h>
Initialization
frc::CANSparkMax motor { 0, rev::CANSparkMax::MotorType::kBrushless }; // kBrushless or kBrushed
Percent Output
motor.Set(0.5);
Idle Mode
motor.SetIdleMode(rev::CANSparkMax::IdleMode::kCoast); // kCoast or kBrake
Encoder
rev::SparkMaxRelativeEncoder encoder(motor.GetEncoder());
double position = encoder.GetPosition();
double velocity = encoder.GetVelocity();
PID Controller
rev::SparkMaxPIDController pidController = motor.GetPIDController();
// Configure the PID Controller
pidController.SetP(0.1);
pidController.SetI(0.0);
pidController.SetD(0.0);
pidController.SetFF(0.0);
pidController.SetIZone(0.0);
// Set the reference (kVelocity, kPosition, kCurrent, etc.).
pidController.SetReference(1.0, rev::ControlType::kVelocity);
CAN Talon FX
CAN Talon FX motor controllers are used to control CTRE motors, predominantly the Falcon 500. They can be controlled with the ctre::phoenix::motorcontrol::can::TalonFX class.
Header
#include <ctre/Phoenix.h>
Initialization
ctre::phoenix::motorcontrol::can::TalonFX motor { 0 };
Percent Output
motor.Set(ctre::phoenix::motorcontrol::ControlMode::PercentOutput, 0.5);
Idle Mode
motor.SetNeutralMode(ctre::phoenix::motorcontrol::NeutralMode::Coast); // Coast or Brake
Encoder
double position = motor.GetSelectedSensorPosition(0);
double velocity = motor.GetSelectedSensorVelocity(0);
PID Controller
motor.Config_kP(0, 0.1);
motor.Config_kI(0, 0.0);
motor.Config_kD(0, 0.0);
motor.Config_kF(0, 0.0);
motor.Config_IntegralZone(0, 0.0);
// Set the reference (Position, Velocity, Current, etc.).
motor.Set(ctre::phoenix::motorcontrol::TalonFXControlMode::Velocity, 1.0);
CAN Talon SRX
CAN Talon FX motor controllers are used to control CTRE motors, predominantly the Falcon 500. They can be controlled with the ctre::phoenix::motorcontrol::can::TalonSRX class.
Header
#include <ctre/Phoenix.h>
Initialization
ctre::phoenix::motorcontrol::can::TalonSRX motor { 0 };
Percent Output
motor.Set(ctre::phoenix::motorcontrol::ControlMode::PercentOutput, 0.5);
Idle Mode
motor.SetNeutralMode(ctre::phoenix::motorcontrol::NeutralMode::Coast); // Coast or Brake
Encoder
double position = motor.GetSelectedSensorPosition(0);
double velocity = motor.GetSelectedSensorVelocity(0);
PID Controller
motor.Config_kP(0, 0.1);
motor.Config_kI(0, 0.0);
motor.Config_kD(0, 0.0);
motor.Config_kF(0, 0.0);
motor.Config_IntegralZone(0, 0.0);
// Set the reference (Position, Velocity, Current, etc.).
motor.Set(ctre::phoenix::motorcontrol::TalonSRXControlMode::Velocity, 1.0);
CANCoder
The CTRE CANCoder is an absolute encoder, meaning that it’s position won’t reset when the robot is powered off. They can be controlled with the ctre::phoenix::sensors::CANCoder class.
Header
#include <ctre/Phoenix.h>
Initialization
ctre::phoenix::sensors::CANCoder canCoder { 0 };
// Signed_PlusMinus180 or Unsigned_0_to_360
canCoder.ConfigAbsoluteSensorRange(ctre::phoenix::sensors::AbsoluteSensorRange::Signed_PlusMinus180);
Absolute Position
canCoder.GetAbsolutePosition();
Spark
PWM Spark motor controllers can be controlled with the frc::Spark class.
Header
#include <frc/Spark.h>
Initialization
frc::Spark motor { 0 };
Percent Output
motor.Set(0.5);