Skip to content

Commit 9e7a7d4

Browse files
Added creating and running an idenfication routine
1 parent e4f67a0 commit 9e7a7d4

1 file changed

Lines changed: 103 additions & 1 deletion

File tree

docs/section-7/feedforward-control.md

Lines changed: 103 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,109 @@ motor.setVoltage(feedforwardVolts);
5050
!!! warning
5151
The code excerpt is only meant to show how feedforward works. This is not how we actually implement feedforward, but should give you a better idea of the inner workings of feedforward.
5252

53-
## System Idenfication
53+
## Tuning and System Idenfication
54+
Similar to PID, you can tune values by manually guessing and checking.
55+
56+
[Click here to try tuning a feedforward controller. SCROLL TO "PURE FEEDFORWARD CONTROL", SKIP EVERYTHING ELSE](https://docs.wpilib.org/en/stable/docs/software/advanced-controls/introduction/tuning-flywheel.html)
57+
58+
Follow the instructions and see if you can get the optimal tuning solution. The model simulates a flywheel shooter mechanism and halfway through the simulation it shoots a ball. **DO NOT SKIP THIS PRACTICE**
59+
60+
While manual tuning works, WPILIB provides a way to generate kS, kV, and kA, called System Identification, or SysID for short.
61+
62+
!!! warning
63+
Do not move on if you don't know how [lambdas/consumers](https://docs.wpilib.org/en/stable/docs/software/basic-programming/functions-as-data.html) work and the [Java Unit library](https://docs.wpilib.org/en/stable/docs/software/basic-programming/java-units.html).
64+
65+
System Identification is the process of determining a mathematical model for the behavior of a system through statistical anaylsis of its inputs and outputs. SysID has a process to determine kS, kV, and kA for the motor, so you don't have to do any tuning! They also provide PID values, but treat them as a "starting point" for further tuning.
66+
67+
Read the following WPILIB articles:
68+
69+
- [Creating an Identification Routine](https://docs.wpilib.org/en/stable/docs/software/advanced-controls/system-identification/creating-routine.html)
70+
- [Running the Identification Routine](https://docs.wpilib.org/en/stable/docs/software/advanced-controls/system-identification/running-routine.html)
71+
72+
<hr>
73+
74+
For those that want a quick summary:
75+
76+
The SysID tool runs two types of tests:
77+
78+
- Quasistatic: mechanism is gradually sped up (determines amount of voltage needed for velocity)
79+
- Dynamic: mechanism is given a constant 'step voltage' (determines amount of voltage needed for acceleration)
80+
81+
These tests can be run backwards and forwards. To create the test, you have to write create a SysIdRoutine object:
82+
```java
83+
public class Shooter extends SubsystemBase {
84+
85+
private final CANSparkMax motor =
86+
MotorControllerFactory.createSparkMax(0, MotorConfig.NEO);
87+
88+
private final SysIdRoutine sysIdRoutine =
89+
new SysIdRoutine(
90+
new SysIdRoutine.Config(),
91+
new SysIdRoutine.Mechanism(
92+
this::voltageDrive,
93+
this::logMotors,
94+
this
95+
)
96+
);
97+
}
98+
```
99+
100+
The `SysIdRoutine.Config()` is where you specify custom parameters for the quasistatic and dynamic tests.
101+
For example if you write:
102+
```java
103+
// ramp rate of 3 volts per second and step voltage of 8 volts
104+
new SysIdRoutine.Config(3, 8);
105+
```
106+
By default the ramp rate is 1 volt per second and the step voltage is 7 volts. The reason why you may want to lower the ramp rate is so it doesn't run too fast and smash into the wall. Typically the default works fine.
107+
The `Config` object also accepts a timeout and callback (function that is called when the test is over).
108+
109+
Now that you have set the parameters, you need to specify which motors receive voltage and how you will log the data for analysis. In this example we will be controlling a single motor. The `Mechanism` object accepts those two functions:
110+
```java
111+
new SysIdRoutine.Mechanism(this::driveMotor, this::logMotor, this)
112+
```
113+
114+
Here are the two functions that drive and log the motor voltage:
115+
```java
116+
// Mutable holder for unit-safe voltage values, persisted to avoid reallocation.
117+
private final MutableMeasure<Voltage> voltage = mutable(Volts.of(0));
118+
119+
public void driveMotor(Measure<Voltage> volts) {
120+
motor.setVoltage(volts.in(Volts));
121+
}
122+
123+
public void logMotor(SysIdRoutineLog log) {
124+
log.motor("shooter-motor")
125+
.voltage(voltage.mut_replace(
126+
motor.get() * RobotController.getBatteryVoltage(),
127+
Volts
128+
));
129+
}
130+
```
131+
Note that [SysIdRoutineLog](https://github.wpilib.org/allwpilib/docs/release/java/edu/wpi/first/wpilibj/sysid/SysIdRoutineLog.html) has a handy `motor` method which returns a `SysIdRoutineLog.MotorLog` object used to log voltage, linear position, velocity, and more. [Take a look at all the properties the object logs](https://github.wpilib.org/allwpilib/docs/release/java/edu/wpi/first/wpilibj/sysid/SysIdRoutineLog.MotorLog.html).
132+
133+
You may also notice that the only values that the logger logs are of instances that must be of `MutableMeasure<(insert measure)>`. This records the values along with its units. You can't just log a value.
134+
135+
After you set up the testing parameters and mechanism to test, the SysIdRoutine provides functions that return a command to run the test.
136+
137+
```java
138+
public Command sysIdQuasistatic(SysIdRoutine.Direction direction) {
139+
return routine.quasistatic(direction);
140+
}
141+
142+
public Command sysIdDynamic(SysIdRoutine.Direction direction) {
143+
return routine.dynamic(direction);
144+
}
145+
```
146+
147+
Typically it is recommended to bind these commands to controller buttons or an autonomous routine. If using a controller, it is recommended to bind them like this:
148+
149+
```java
150+
GenericHID controller = new GenericHID(0);
151+
Shooter shooter = new Shooter();
152+
153+
new JoystickButton(controller, Button.kY.value).whileTrue(shooter.sysIdQuasistatic(SysIdRoutine.Direction.kForward));
154+
```
155+
Now you are ready to enable and run the tests! Typically the longer you run them, the more data you get which will lead to more accurate calculations. However, keep in mind not to run too long for safety purposes. After all four tests have been run, use the [DataLogTool](https://docs.wpilib.org/en/stable/docs/software/telemetry/datalog-download.html) to get the files.
54156

55157
## Implementation
56158

0 commit comments

Comments
 (0)