Package edu.wpi.first.wpilibj.simulation
Class DifferentialDrivetrainSim
java.lang.Object
edu.wpi.first.wpilibj.simulation.DifferentialDrivetrainSim
This class simulates the state of the drivetrain. In simulationPeriodic, users should first set
inputs from motors with
setInputs(double, double)
, call update(double)
to
update the simulation, and set estimated encoder and gyro positions, as well as estimated
odometry pose. Teams can use Field2d
to visualize
their robot on the Sim GUI's field.
Our state-space system is:
x = [[x, y, theta, vel_l, vel_r, dist_l, dist_r]]ᵀ in the field coordinate system (dist_* are wheel distances.)
u = [[voltage_l, voltage_r]]ᵀ This is typically the control input of the last timestep from a LTVDiffDriveController.
y = x
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic enum
Represents a gearing option of the Toughbox mini.static enum
Represents common motor layouts of the kit drivetrain.static enum
Represents common wheel sizes of the kit drivetrain. -
Constructor Summary
ConstructorDescriptionDifferentialDrivetrainSim
(LinearSystem<N2, N2, N2> plant, DCMotor driveMotor, double gearing, double trackWidthMeters, double wheelRadiusMeters, Matrix<N7, N1> measurementStdDevs) Creates a simulated differential drivetrain.DifferentialDrivetrainSim
(DCMotor driveMotor, double gearing, double jKgMetersSquared, double massKg, double wheelRadiusMeters, double trackWidthMeters, Matrix<N7, N1> measurementStdDevs) Creates a simulated differential drivetrain. -
Method Summary
Modifier and TypeMethodDescriptionclampInput
(Matrix<N2, N1> u) Clamp the input vector such that no element exceeds the battery voltage.static DifferentialDrivetrainSim
createKitbotSim
(DifferentialDrivetrainSim.KitbotMotor motor, DifferentialDrivetrainSim.KitbotGearing gearing, DifferentialDrivetrainSim.KitbotWheelSize wheelSize, double jKgMetersSquared, Matrix<N7, N1> measurementStdDevs) Create a sim for the standard FRC kitbot.static DifferentialDrivetrainSim
createKitbotSim
(DifferentialDrivetrainSim.KitbotMotor motor, DifferentialDrivetrainSim.KitbotGearing gearing, DifferentialDrivetrainSim.KitbotWheelSize wheelSize, Matrix<N7, N1> measurementStdDevs) Create a sim for the standard FRC kitbot.double
Get the current draw of the drivetrain.double
Get the drivetrain gearing.The differential drive dynamics function.Returns the direction the robot is pointing.double
Get the current draw of the left side of the drivetrain.double
Get the left encoder position in meters.double
Get the left encoder velocity in meters per second.getPose()
Returns the current pose.double
Get the current draw of the right side of the drivetrain.double
Get the right encoder position in meters.double
Get the right encoder velocity in meters per second.void
setCurrentGearing
(double newGearRatio) Sets the gearing reduction on the drivetrain.void
setInputs
(double leftVoltageVolts, double rightVoltageVolts) Sets the applied voltage to the drivetrain.void
Sets the system pose.void
Sets the system state.void
update
(double dtSeconds) Update the drivetrain states with the current time difference.
-
Constructor Details
-
DifferentialDrivetrainSim
public DifferentialDrivetrainSim(DCMotor driveMotor, double gearing, double jKgMetersSquared, double massKg, double wheelRadiusMeters, double trackWidthMeters, Matrix<N7, N1> measurementStdDevs) Creates a simulated differential drivetrain.- Parameters:
driveMotor
- ADCMotor
representing the left side of the drivetrain.gearing
- The gearing ratio between motor and wheel, as output over input. This must be the same ratio as the ratio used to identify or create the drivetrainPlant.jKgMetersSquared
- The moment of inertia of the drivetrain about its center.massKg
- The mass of the drivebase.wheelRadiusMeters
- The radius of the wheels on the drivetrain.trackWidthMeters
- The robot's track width, or distance between left and right wheels.measurementStdDevs
- Standard deviations for measurements, in the form [x, y, heading, left velocity, right velocity, left distance, right distance]ᵀ. Can be null if no noise is desired. Gyro standard deviations of 0.0001 radians, velocity standard deviations of 0.05 m/s, and position measurement standard deviations of 0.005 meters are a reasonable starting point.
-
DifferentialDrivetrainSim
public DifferentialDrivetrainSim(LinearSystem<N2, N2, N2> plant, DCMotor driveMotor, double gearing, double trackWidthMeters, double wheelRadiusMeters, Matrix<N7, N1> measurementStdDevs) Creates a simulated differential drivetrain.- Parameters:
plant
- TheLinearSystem
representing the robot's drivetrain. This system can be created withLinearSystemId.createDrivetrainVelocitySystem(DCMotor, double, double, double, double, double)
orLinearSystemId.identifyDrivetrainSystem(double, double, double, double)
.driveMotor
- ADCMotor
representing the drivetrain.gearing
- The gearingRatio ratio of the robot, as output over input. This must be the same ratio as the ratio used to identify or create the drivetrainPlant.trackWidthMeters
- The distance between the two sides of the drivetrain. Can be found with SysId.wheelRadiusMeters
- The radius of the wheels on the drivetrain, in meters.measurementStdDevs
- Standard deviations for measurements, in the form [x, y, heading, left velocity, right velocity, left distance, right distance]ᵀ. Can be null if no noise is desired. Gyro standard deviations of 0.0001 radians, velocity standard deviations of 0.05 m/s, and position measurement standard deviations of 0.005 meters are a reasonable starting point.
-
-
Method Details
-
setInputs
Sets the applied voltage to the drivetrain. Note that positive voltage must make that side of the drivetrain travel forward (+X).- Parameters:
leftVoltageVolts
- The left voltage.rightVoltageVolts
- The right voltage.
-
update
Update the drivetrain states with the current time difference.- Parameters:
dtSeconds
- the time difference
-
getHeading
Returns the direction the robot is pointing.Note that this angle is counterclockwise-positive, while most gyros are clockwise positive.
- Returns:
- The direction the robot is pointing.
-
getPose
Returns the current pose.- Returns:
- The current pose.
-
getRightPositionMeters
Get the right encoder position in meters.- Returns:
- The encoder position.
-
getRightVelocityMetersPerSecond
Get the right encoder velocity in meters per second.- Returns:
- The encoder velocity.
-
getLeftPositionMeters
Get the left encoder position in meters.- Returns:
- The encoder position.
-
getLeftVelocityMetersPerSecond
Get the left encoder velocity in meters per second.- Returns:
- The encoder velocity.
-
getLeftCurrentDrawAmps
Get the current draw of the left side of the drivetrain.- Returns:
- the drivetrain's left side current draw, in amps
-
getRightCurrentDrawAmps
Get the current draw of the right side of the drivetrain.- Returns:
- the drivetrain's right side current draw, in amps
-
getCurrentDrawAmps
Get the current draw of the drivetrain.- Returns:
- the current draw, in amps
-
getCurrentGearing
Get the drivetrain gearing.- Returns:
- the gearing ration
-
setCurrentGearing
Sets the gearing reduction on the drivetrain. This is commonly used for shifting drivetrains.- Parameters:
newGearRatio
- The new gear ratio, as output over input.
-
setState
Sets the system state.- Parameters:
state
- The state.
-
setPose
Sets the system pose.- Parameters:
pose
- The pose.
-
getDynamics
The differential drive dynamics function.- Parameters:
x
- The state.u
- The input.- Returns:
- The state derivative with respect to time.
-
clampInput
Clamp the input vector such that no element exceeds the battery voltage. If any does, the relative magnitudes of the input will be maintained.- Parameters:
u
- The input vector.- Returns:
- The normalized input.
-
createKitbotSim
public static DifferentialDrivetrainSim createKitbotSim(DifferentialDrivetrainSim.KitbotMotor motor, DifferentialDrivetrainSim.KitbotGearing gearing, DifferentialDrivetrainSim.KitbotWheelSize wheelSize, Matrix<N7, N1> measurementStdDevs) Create a sim for the standard FRC kitbot.- Parameters:
motor
- The motors installed in the bot.gearing
- The gearing reduction used.wheelSize
- The wheel size.measurementStdDevs
- Standard deviations for measurements, in the form [x, y, heading, left velocity, right velocity, left distance, right distance]ᵀ. Can be null if no noise is desired. Gyro standard deviations of 0.0001 radians, velocity standard deviations of 0.05 m/s, and position measurement standard deviations of 0.005 meters are a reasonable starting point.- Returns:
- A sim for the standard FRC kitbot.
-
createKitbotSim
public static DifferentialDrivetrainSim createKitbotSim(DifferentialDrivetrainSim.KitbotMotor motor, DifferentialDrivetrainSim.KitbotGearing gearing, DifferentialDrivetrainSim.KitbotWheelSize wheelSize, double jKgMetersSquared, Matrix<N7, N1> measurementStdDevs) Create a sim for the standard FRC kitbot.- Parameters:
motor
- The motors installed in the bot.gearing
- The gearing reduction used.wheelSize
- The wheel size.jKgMetersSquared
- The moment of inertia of the drivebase. This can be calculated using SysId.measurementStdDevs
- Standard deviations for measurements, in the form [x, y, heading, left velocity, right velocity, left distance, right distance]ᵀ. Can be null if no noise is desired. Gyro standard deviations of 0.0001 radians, velocity standard deviations of 0.05 m/s, and position measurement standard deviations of 0.005 meters are a reasonable starting point.- Returns:
- A sim for the standard FRC kitbot.
-