[robotpy][examples] Split examples and snippets (#8944)

This also updates the bazel scripts to behave more like the C++ and Java
examples, and updates the copybara scripts to be able to sync up
`mostrobotpy`
This commit is contained in:
PJ Reiniger
2026-06-03 22:43:16 -04:00
committed by GitHub
parent a734275cc5
commit dca59147e1
134 changed files with 111 additions and 80 deletions

View File

@@ -0,0 +1,98 @@
#
# Copyright (c) FIRST and other WPILib contributors.
# Open Source Software; you can modify and/or share it under the terms of
# the WPILib BSD license file in the root directory of this project.
#
import wpilib
import enum
class ExampleSmartMotorController(wpilib.MotorController):
"""A simplified stub class that simulates the API of a common "smart" motor controller.
Has no actual functionality.
"""
class PIDMode(enum.Enum):
kPosition = enum.auto()
kVelocity = enum.auto()
kMovementWitchcraft = enum.auto()
def __init__(self, port: int) -> None:
"""Creates a new ExampleSmartMotorController.
Args:
port: The port for the controller.
"""
super().__init__()
self._velocity = 0.0
self._inverted = False
self._leader = None
def setPID(self, kp: float, ki: float, kd: float) -> None:
"""Example method for setting the PID gains of the smart controller.
Args:
kp: The proportional gain.
ki: The integral gain.
kd: The derivative gain.
"""
pass
def setSetPoint(
self, mode: PIDMode, setpoint: float, arbfeedforward: float
) -> None:
"""Example method for setting the setpoint of the smart controller in PID mode.
Args:
mode: The mode of the PID controller.
setpoint: The controller setpoint.
arbfeedforward: An arbitrary feedforward output (from -1 to 1).
"""
pass
def follow(self, leader: "ExampleSmartMotorController") -> None:
"""Places this motor controller in follower mode.
Args:
leader: The leader to follow.
"""
self._leader = leader
def getEncoderDistance(self) -> float:
"""Returns the encoder distance.
Returns:
The current encoder distance.
"""
return 0
def getEncoderRate(self) -> float:
"""Returns the encoder rate.
Returns:
The current encoder rate.
"""
return 0
def resetEncoder(self) -> None:
"""Resets the encoder to zero distance."""
pass
def set(self, velocity: float) -> None:
self._velocity = -velocity if self._inverted else velocity
def get(self) -> float:
return self._velocity
def setInverted(self, isInverted: bool) -> None:
self._inverted = isInverted
def getInverted(self) -> bool:
return self._inverted
def disable(self) -> None:
self._velocity = 0.0
def stopMotor(self) -> None:
self._velocity = 0.0

View File

@@ -0,0 +1,50 @@
#!/usr/bin/env python3
#
# Copyright (c) FIRST and other WPILib contributors.
# Open Source Software; you can modify and/or share it under the terms of
# the WPILib BSD license file in the root directory of this project.
#
import wpilib
from wpimath import TrapezoidProfile
import examplesmartmotorcontroller
import wpimath
class MyRobot(wpilib.TimedRobot):
kDt = 0.02
def __init__(self):
super().__init__()
self.joystick = wpilib.Joystick(1)
self.motor = examplesmartmotorcontroller.ExampleSmartMotorController(1)
# Note: These gains are fake, and will have to be tuned for your robot.
self.feedforward = wpimath.SimpleMotorFeedforwardMeters(1, 1.5)
# Create a motion profile with the given maximum velocity and maximum
# acceleration constraints for the next setpoint.
self.profile = TrapezoidProfile(TrapezoidProfile.Constraints(1.75, 0.75))
self.goal = TrapezoidProfile.State()
self.setpoint = TrapezoidProfile.State()
# Note: These gains are fake, and will have to be tuned for your robot.
self.motor.setPID(1.3, 0.0, 0.7)
def teleopPeriodic(self):
if self.joystick.getRawButtonPressed(2):
self.goal = TrapezoidProfile.State(5, 0)
elif self.joystick.getRawButtonPressed(3):
self.goal = TrapezoidProfile.State(0, 0)
# Retrieve the profiled setpoint for the next timestep. This setpoint moves
# toward the goal while obeying the constraints.
self.setpoint = self.profile.calculate(self.kDt, self.setpoint, self.goal)
# Send setpoint to offboard controller PID
self.motor.setSetPoint(
examplesmartmotorcontroller.ExampleSmartMotorController.PIDMode.kPosition,
self.setpoint.position,
self.feedforward.calculate(self.setpoint.velocity) / 12,
)