Offseason · Week 4 of 8
Motors & Sensors
SparkMax, TalonFX, encoders, gyroscopes, and SmartDashboard.
Motor Controllers
2974 primarily uses REV SparkMax (brushless) and CTRE TalonFX (Falcon 500). Both have similar APIs but different setup steps and vendor library imports.
java — SparkMax setup
import com.revrobotics.CANSparkMax; import com.revrobotics.CANSparkLowLevel.MotorType; CANSparkMax motor = new CANSparkMax(1, MotorType.kBrushless); motor.restoreFactoryDefaults(); // always do this first motor.setInverted(false); motor.setIdleMode(CANSparkMax.IdleMode.kBrake); // Set speed: -1.0 (full reverse) to 1.0 (full forward) motor.set(0.5);
Encoders
Encoders measure rotation — how far a mechanism has moved, how fast it's going. SparkMax has a built-in encoder on NEO motors. Always set conversion factors to get meaningful units.
java — reading encoder values
RelativeEncoder encoder = motor.getEncoder(); // Convert rotations to inches: circumference / gear ratio double wheelCircumference = 2 * Math.PI * 3.0; // 3" radius wheel encoder.setPositionConversionFactor(wheelCircumference / GEAR_RATIO); encoder.setVelocityConversionFactor(wheelCircumference / GEAR_RATIO / 60.0); double positionInches = encoder.getPosition(); // inches from start double speedInchPerSec = encoder.getVelocity(); // inches/sec encoder.setPosition(0); // reset to zero
Gyroscope (NavX / Pigeon)
java — NavX basics
import com.kauailabs.navx.frc.AHRS; import edu.wpi.first.wpilibj.SPI; AHRS gyro = new AHRS(SPI.Port.kMXP); double heading = gyro.getAngle(); // degrees, accumulates past 360 double yaw = gyro.getYaw(); // -180 to 180 gyro.reset(); // zero the heading
SmartDashboard
java — logging to Shuffleboard
import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard; // Put in periodic() for live updates SmartDashboard.putNumber("Shooter RPM", encoder.getVelocity()); SmartDashboard.putBoolean("At Speed", isAtSpeed); SmartDashboard.putString("Robot State", state.toString());