AdvantageScope
AdvantageScope is a data visualization tool that can be used to view data from the robot in real-time. It is a powerful tool for debugging and tuning the robot, and it can be used to view a wide variety of data, such as:
- Sensor readings
- Motor outputs
- PID controller state
- Autonomous path data
AdvantageScope works by reading log files generated by the robot code. WPILib's DataLog class provides an easy way to create these log files (.wpilog).
For more information, see the AdvantageScope documentation: https://github.com/Mechanical-Advantage/AdvantageScope/blob/main/docs/README.md
Example: Logging Subsystem State
Here is an example of a shooter subsystem that logs its velocity, setpoint, and motor output. This data can then be graphed in AdvantageScope to tune the PID controller.
import commands2
import wpilib
import wpimath.controller
import wpimath.log
from util.simtalon import Talon # Assuming a simulated Talon for example
class Shooter(commands2.SubsystemBase):
def __init__(self):
super().__init__()
self.motor = Talon(3)
self.encoder = wpilib.Encoder(2, 3)
self.controller = wpimath.controller.PIDController(0.1, 0, 0)
# Start data logging by creating a DataLog object.
# This will create a .wpilog file in a USB stick if connected, or in /home/lvuser
self.data_log = wpilib.DataLogManager.getLog()
# Create log entries for the setpoint, measurement, and output
self.setpoint_log = wpimath.log.DoubleLogEntry(self.data_log, "/shooter/setpoint")
self.measurement_log = wpimath.log.DoubleLogEntry(self.data_log, "/shooter/velocity")
self.output_log = wpimath.log.DoubleLogEntry(self.data_log, "/shooter/output")
def set_velocity(self, velocity):
output = self.controller.calculate(self.encoder.getRate(), velocity)
self.motor.set(output)
# Log the data at each timestamp
self.setpoint_log.append(velocity)
self.measurement_log.append(self.encoder.getRate())
self.output_log.append(output)
def stop(self):
self.motor.set(0)