Interactive Code Playground
The MARSLib Code Playground is an interactive learning environment where you can explore real programming patterns from championship-winning FRC robots. Experiment with swerve drive control, vision alignment, PID mechanisms, and state machines - all in your browser.
How It Works
Section titled “How It Works”The Code Playground demonstrates production-ready MARSLib patterns used by FRC Team 2614. Each example includes:
✅ Real Code - Actual patterns from competition robots, not simplified examples ✅ Best Practices - Proper Command-based programming and AdvantageKit logging ✅ Safety First - Fault handling and state management built-in ✅ Performance - Zero-allocation patterns and real-time control
Featured Examples
Section titled “Featured Examples”🏎️ Swerve Drive Basics
Section titled “🏎️ Swerve Drive Basics”Learn the fundamentals of controlling a swerve drive robot using MARSLib’s abstraction layer. This example shows:
- ChassisSpeeds for robot-relative control.
- Proper Command structure with requirements.
- Real-time velocity control for competitive play.
👁️ Vision Alignment
Section titled “👁️ Vision Alignment”Explore automatic AprilTag alignment using MARSLib’s vision system. Demonstrates:
- Vision pose estimation fusion.
- PID controllers for precise positioning.
- Continuous angle handling for rotation.
⚙️ PID Elevator Control
Section titled “⚙️ PID Elevator Control”Master position control with linear mechanisms. Features:
- LinearMechanismIO abstraction layer.
- PID control with feedforward for gravity compensation.
- Proper periodic() update patterns.
🤖 State Machine Superstructure
Section titled “🤖 State Machine Superstructure”Coordinate complex mechanisms using MARSLib state machines. Shows:
- MARSStateMachine for multi-system coordination.
- Safe state transitions and behaviors.
- Emergency stop (E-Stop) setup.
Programming Patterns
Section titled “Programming Patterns”Command-Based structure
Section titled “Command-Based structure”All examples follow WPILib’s Command-based programming pattern:
public class ExampleCommand extends Command { private final Subsystem subsystem;
public ExampleCommand(Subsystem subsystem) { this.subsystem = subsystem; addRequirements(subsystem); // Declare requirements }
@Override public void execute() { // Control logic here }}IO Abstraction Layer
Section titled “IO Abstraction Layer”MARSLib separates logic from hardware through interfaces:
// Your subsystem logic works with any IO implementationpublic class MechanismSubsystem extends Subsystem { private final MechanismIO io; // Can be Real or Sim
@Override public void periodic() { // Works the same on robot and in simulation! double position = io.getPosition(); }}AdvantageKit Integration
Section titled “AdvantageKit Integration”All subsystems automatically log state for AdvantageScope:
// MARSLib handles logging automatically// No manual logging code needed in your subsystemsBest Practices
Section titled “Best Practices”✅ DO use Command-based programming for autonomous and teleop ✅ DO use MARSLib’s IO abstractions for hardware independence ✅ DO use state machines for complex mechanism coordination ✅ DO tune PID gains using SysId characterization ✅ DO implement safety mechanisms (E-Stop, limits)
❌ DON’T put hardware-specific code in your commands ❌ DON’T ignore feedback from sensors ❌ DON’T skip proper requirement management ❌ DON’T forget about fault tolerance ❌ DON’T block in execute() methods
Next Steps
Section titled “Next Steps”Explore More Examples
Section titled “Explore More Examples”- Check out the State Machine tutorial for advanced coordination.
- Learn about Vision Systems for AprilTag integration.
- Study Swerve Drive for autonomous path following.
Practice on Your Own
Section titled “Practice on Your Own”- Clone the MARSLib repository and explore the full codebase.
- Run the simulator to see these patterns in action.
- Modify the examples to learn by experimentation.
- Build your own mechanisms using MARSLib patterns.
Join the Community
Section titled “Join the Community”- GitHub: MARSProgramming/MARSLib
- Documentation: MARSProgramming.github.io/MARSLib
- Team: FRC 2614 - Mountaineer Area RoboticS.
Technical Details
Section titled “Technical Details”Supported Patterns
Section titled “Supported Patterns”- ✅ Command-based programming.
- ✅ State machines with MARSStateMachine.
- ✅ Vision-based alignment using MARSVision.
- ✅ PID control with feedforward.
- ✅ Swerve drive kinematics.
- ✅ Mechanism coordination.
structure Alignment
Section titled “structure Alignment”All examples follow MARSLib’s core structure:
📖 Further Reading & External Resources
Section titled “📖 Further Reading & External Resources”- WPILib Command-Based Programming - Official WPILib guide to Command-based programming.
- AdvantageKit Documentation - Learn about the logging framework that powers MARSLib.
- SysId Characterization - Tool for PID tuning and system identification.
- PhotonVision Documentation - AprilTag detection and vision processing.