Skip to content

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

FRC TrainingCamp - Teaching Examples

A collection of minimal, well-documented example code for learning FRC robot programming. Each example focuses on one concept with extensive comments explaining why code is structured the way it is.

Each example is in its own git feature branch - keeping code minimal and focused!

๐ŸŒฟ Branch Structure

main branch (documentation, all examples)
  โ”œโ”€โ”€ feature/button-print              Simplest - Button input only
  โ”œโ”€โ”€ feature/duty-cycle-motor          Basic motor control
  โ”œโ”€โ”€ feature/position-control-motor    Encoder + PID feedback
  โ”œโ”€โ”€ feature/velocity-control-motor    RPM control
  โ””โ”€โ”€ feature/dual-velocity-motor       Dual motor coordination

To get an example:

git checkout feature/button-print              # Get that example
git checkout feature/duty-cycle-motor          # Change to different example
git checkout main                              # Back to all documentation

Overview

This project contains 5 teaching examples, each with:

  • Minimal code - Only what's needed for the concept
  • Own git branch - Each example isolated and focused
  • Annotated source - Comments explaining design decisions
  • README guides - Why you'd use this pattern (on main)
  • Practical hints - How to extend each example
  • Logging - SmartDashboard output for debugging

Perfect for students learning:

  • How subsystems work
  • How commands work
  • Motor control patterns
  • Button input handling
  • Code organization

The 5 Teaching Examples

1. ๐Ÿ“Œ Duty Cycle Motor Control

File: DutyCycleMotor_README.md

The simplest way to control a motor - just set its speed.

Button Press โ†’ Motor Spins at 50% โ†’ Button Release โ†’ Motor Stops

Concepts:

  • SparkMax motor controller setup
  • Voltage compensation and current limiting
  • Simple command structure
  • Console/SmartDashboard logging

Best For Learning:

  • Hardware initialization
  • Basic command lifecycle
  • Motor configuration patterns

Code Files:

  • SimpleMotorSubsystem.java - Motor hardware control
  • SimpleMotorCommand.java - Motor action command
  • RobotContainer.java - Button binding example

Test It:

  1. Deploy code
  2. Press A button on Xbox controller
  3. Motor spins forward
  4. Release button
  5. Motor stops

2. ๐Ÿ“ Position Control Motor

File: PositionControl_README.md

Tell a motor to move to an exact location and stop there.

Button Press โ†’ Motor Moves to 10 Rotations โ†’ Motor Stops Automatically

Concepts:

  • Encoder position feedback
  • Closed-loop PID control
  • Target-based commands
  • Command auto-completion

Best For Learning:

  • How encoders work
  • Closed-loop feedback concepts
  • PID basics
  • Why position control is more reliable

Code Files:

  • PositionControlMotorSubsystem.java - Position feedback and PID
  • PositionControlCommand.java - Target-based movement
  • RobotContainer_PositionControl.java - Button binding

Test It:

  1. Deploy code
  2. Press A button
  3. Motor moves to target position
  4. Motor stops automatically
  5. Watch SmartDashboard for position feedback

3. โšก Velocity Control Motor

File: VelocityControl_README.md

Keep a motor spinning at a constant speed, even if load changes.

Button Press โ†’ Motor Spins at 5000 RPM โ†’ Maintains Speed Under Load โ†’ Release to Stop

Concepts:

  • RPM (velocity) measurement
  • Velocity-specific PID tuning
  • Feed-forward control
  • Load compensation

Best For Learning:

  • RPM vs position vs duty cycle
  • Different PID tuning strategies
  • Intake/shooter motor patterns
  • Why constant speed matters

Code Files:

  • VelocityControlMotorSubsystem.java - RPM-based control
  • VelocityControlCommand.java - Continuous velocity command
  • RobotContainer_VelocityControl.java - Button binding

Test It:

  1. Deploy code
  2. Press A button
  3. Motor accelerates to 5000 RPM
  4. Motor maintains speed (try blocking it!)
  5. Release button to stop
  6. Watch RPM value in SmartDashboard

4. ๐Ÿ”„ Dual Motor Velocity Control

File: DualMotor_README.md

Control two motors working together using leader-follower pattern.

Button Press โ†’ Leader Motor Runs with Velocity Control โ†’ Follower Motor Copies Output โ†’ Both Stop

Concepts:

  • Leader-follower motor pattern
  • Motor synchronization
  • Multiple CAN devices
  • Copying motor outputs

Best For Learning:

  • Dual intake rollers
  • Elevator motors
  • Climber coordination
  • Motor troubleshooting

Code Files:

  • DualMotorVelocitySubsystem.java - Two-motor coordination
  • DualMotorVelocityCommand.java - Dual motor command
  • RobotContainer_DualMotor.java - Button binding

Test It:

  1. Deploy code
  2. Press A button
  3. Both motors spin at same velocity
  4. Check SmartDashboard - outputs should match
  5. Try blocking one motor
  6. Release to stop both

5. ๐ŸŽฎ Button Press & Console Output

File: ButtonPrint_README.md

The simplest example - press a button, see a message.

Button Press โ†’ Print Message to Console โ†’ See Output

Concepts:

  • Xbox controller input
  • Button binding syntax
  • Command lifecycle
  • Debug output patterns

Best For Learning:

  • How controller input works
  • Button binding patterns
  • Different trigger types (.onTrue, .whileTrue, .onFalse)
  • Basic debugging

Code Files:

  • PrintButtonCommand.java - Simple print-only command
  • RobotContainer_ButtonPrint.java - Multiple button examples
  • No subsystems needed!

Test It:

  1. Deploy code
  2. Press different buttons
  3. Watch console output
  4. See what happens with .onTrue vs .whileTrue
  5. Try holding buttons

Quick Comparison

Feature Duty Cycle Position Velocity Dual Motor Button
Complexity โญ Simple โญโญ Medium โญโญ Medium โญโญโญ Complex โญ Simple
Motor Control โœ“ Yes โœ“ Yes โœ“ Yes โœ“โœ“ Yes (ร—2) โœ— No
Uses Encoder โœ— No โœ“ Yes โœ“ Yes โœ“ Yes (leader) โœ— No
Uses PID โœ— No โœ“ Yes โœ“ Yes โœ“ Yes โœ— No
Best For Simple spin Arm/elevator Intake/shooter Dual roller Testing

Recommended Learning Path

Start Here
    โ†“
buttonPrint_README.md
    โ†“
DutyCycleMotor_README.md
    โ†“
PositionControl_README.md
    โ†“
VelocityControl_README.md
    โ†“
DualMotor_README.md
    โ†“
Combine Concepts

File Organization

Each example is completely isolated in its own git branch:

TrainingCamp/
โ”œโ”€โ”€ main branch (start here!)
โ”‚   โ”œโ”€โ”€ README.md              โ† Overview of all examples
โ”‚   โ”œโ”€โ”€ QUICKSTART.md          โ† How to get and learn each example
โ”‚   โ”œโ”€โ”€ DutyCycleMotor_README.md      (for feature/duty-cycle-motor)
โ”‚   โ”œโ”€โ”€ PositionControl_README.md     (for feature/position-control-motor)
โ”‚   โ”œโ”€โ”€ VelocityControl_README.md     (for feature/velocity-control-motor)
โ”‚   โ”œโ”€โ”€ DualMotor_README.md           (for feature/dual-velocity-motor)
โ”‚   โ”œโ”€โ”€ ButtonPrint_README.md         (for feature/button-print)
โ”‚   โ”‚
โ”‚   โ”œโ”€โ”€ build.gradle & settings.gradle (shared across all branches)
โ”‚   โ”‚
โ”‚   โ””โ”€โ”€ src/main/java/frc/robot/
โ”‚       โ”œโ”€โ”€ Main.java, Robot.java, Constants.java (shared)
โ”‚       โ”œโ”€โ”€ RobotContainer.java (shared base)
โ”‚       โ”œโ”€โ”€ subsystems/ (all examples here)
โ”‚       โ””โ”€โ”€ commands/ (all examples here)
โ”‚
โ””โ”€โ”€ feature/ branches (check them out!)
    โ”œโ”€โ”€ feature/button-print
    โ”‚   โ””โ”€โ”€ Contains ONLY: PrintButtonCommand.java
    โ”‚
    โ”œโ”€โ”€ feature/duty-cycle-motor
    โ”‚   โ””โ”€โ”€ Contains ONLY: SimpleMotorSubsystem.java, SimpleMotorCommand.java
    โ”‚
    โ”œโ”€โ”€ feature/position-control-motor
    โ”‚   โ””โ”€โ”€ Contains ONLY: PositionControlMotorSubsystem.java, PositionControlCommand.java
    โ”‚
    โ”œโ”€โ”€ feature/velocity-control-motor
    โ”‚   โ””โ”€โ”€ Contains ONLY: VelocityControlMotorSubsystem.java, VelocityControlCommand.java
    โ”‚
    โ””โ”€โ”€ feature/dual-velocity-motor
        โ””โ”€โ”€ Contains ONLY: DualMotorVelocitySubsystem.java, DualMotorVelocityCommand.java

How to Use Each Example

  1. Read the guide on main:

    git checkout main
    # Read ButtonPrint_README.md, DutyCycleMotor_README.md, etc.
    
  2. Switch to the feature branch:

    git checkout feature/duty-cycle-motor
    # Now you have ONLY the minimal code for duty cycle control
    
  3. Deploy and test:

    ./gradlew deploy
    
  4. Study the code:

    • Source files are heavily commented
    • Read why each component exists
    • Notice the pattern repeats across examples
  5. Switch to next example:

    git checkout feature/position-control-motor
    # Different example, same base files, new subsystem+command
    

Key Design Patterns Used

Subsystem Pattern

All motor examples follow this structure:

Subsystem (Hardware) โ†’ Command (Action) โ†’ RobotContainer (Binding)
  • Subsystem: Encapsulates hardware, provides control methods
  • Command: Encapsulates an action, uses subsystem's methods
  • RobotContainer: Binds commands to buttons

Why This Matters

  • Each piece has one job (single responsibility principle)
  • Easy to test independently
  • Prevents hardware conflicts (only one command uses subsystem at once)
  • Code is reusable and composable

Important Constants

All motor values are in Constants.java:

MOTOR_ID = 10;           // CAN ID of motor
MOTOR_FOLLOWER_ID = 11;  // For dual motor example
MOTOR_CURRENT_LIMIT = 60; // Amps - protects motor
MOTOR_VOLTAGE_COMPENSATION = 10; // Volts - consistent speed
POSITION_TARGET = 10.0;   // Rotations for position control
VELOCITY_TARGET = 5000.0; // RPM for velocity control

Console Output & Debugging

All examples include logging to help you debug:

Console Output

  • Motor subsystem initialization
  • Command lifecycle (start, execute, end)
  • Motor state (speed, position, velocity)

SmartDashboard

  • Real-time motor values
  • Target vs actual
  • Motor output percentage

How to View

  • Simulator: Terminal where you ran gradlew
  • RoboRIO: Check Driver Station output or SSH logs
  • SmartDashboard: Open Shuffleboard app

Building & Deploying

Choose Your Example First

# See all available branches
git branch -a

# Pick one and switch to it
git checkout feature/duty-cycle-motor

Simulator (PC)

./gradlew simulateJava

RoboRIO (Real Robot)

./gradlew deploy

All branches use the same build.gradle, so deployment works the same way!

Troubleshooting

Motor doesn't move:

  • Check motor CAN ID in Constants
  • Check motor is powered (battery connected)
  • Check motor connector (SparkMax pins)
  • Check current limit isn't too low

Console shows errors:

  • Read the error message carefully
  • Check motor class names match imports
  • Verify Constants values are reasonable
  • Check RobotContainer has correct subsystem

Button doesn't work:

  • Check controller is connected (Driver Station shows it)
  • Verify button binding exists in configureBindings()
  • Check console for button press output
  • Try different button to verify controller works

Next Steps for Students

After understanding each example:

  1. Learn all examples:

    • Study each feature branch one at a time
    • Notice the patterns that repeat
  2. Combine Examples

    • Go back to main branch
    • Copy code from multiple feature branches
    • Button Press + Duty Cycle โ†’ Use button to spin motor
    • Duty Cycle + Position Control โ†’ Use button to move motor to position
    • Velocity Control + Dual Motor โ†’ Two motors at same speed
  3. Build Your Own Mechanisms Using these examples as templates:

    • Intake mechanism (dual motor)
    • Elevator (position control)
    • Shooter (velocity control)
    • Climber (dual motor + buttons)
  4. Real Robot Code

    • Add swerve drive code
    • Add vision-based targeting
    • Add complex autonomy
    • Mix control strategies

Getting Started

Start here:

  1. Read QUICKSTART.md
  2. Understand the git branch structure
  3. git checkout feature/button-print to get first example
  4. Study the code with the README guides
  5. Deploy and test
  6. git checkout feature/duty-cycle-motor for next example

Come back to main branch to read documentation:

  • DutyCycleMotor_README.md
  • PositionControl_README.md
  • VelocityControl_README.md
  • DualMotor_README.md
  • ButtonPrint_README.md

Resources

Questions to Ask While Learning

  • Why is this a subsystem instead of a command? โ†’ Subsystems own hardware, commands are actions using that hardware

  • Why does the command look like this? โ†’ Follows standard pattern: initialize โ†’ execute โ†’ isFinished โ†’ end

  • Why these PID values? โ†’ Based on motor characteristics, tuned for smooth response

  • Why is this logged to console? โ†’ Debugging! See what code is actually doing vs what you expect

  • Can I combine examples? โ†’ YES! That's the whole point - modular, reusable code

Credits

Written for Team 4931 - Teaching Robot Programming

Designed to be:

  • โœ“ Minimal but complete
  • โœ“ Well commented
  • โœ“ Focused on one concept each
  • โœ“ Production-quality code
  • โœ“ Easy to extend

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages