Skip to content

Commit

Permalink
introduce logging & tuning, pretty much finish sim docs
Browse files Browse the repository at this point in the history
  • Loading branch information
Yxhej authored and sigalrmp committed Sep 4, 2024
1 parent d1a0bb8 commit dff614b
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 8 deletions.
69 changes: 64 additions & 5 deletions BasicArmBot.md
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ While we're here, let's also set the real robot's encoder conversion factor.

REV encoders' default measurements are rotations, which should be converted to radians or degrees for use on the robot. This can be easily done with Java's Units library.

Use the predetermined `POSITION_FACTOR` constant in `ArmConstants` as the base, and convert that into your unit of choice.
Use the predetermined `POSITION_FACTOR` constant in `ArmConstants` as the base, and convert that into your angular unit of choice.

```java
public RealArm() {
Expand All @@ -372,23 +372,82 @@ Use the predetermined `POSITION_FACTOR` constant in `ArmConstants` as the base,
}
```

### Logging

Before we can actually see your widget, we need to send it to [NetworkTables](https://docs.wpilib.org/en/stable/docs/software/networktables/networktables-intro.html). Using [Monologue](https://github.com/shueja/Monologue/wiki), this can easily be done using the `@Log.NT` annotation, like so:

```java
@Log.NT private final Mechanism2d mech = new Mechanism2d(2, 2);
// like other Java annotations, can be placed next to or above
```

Using this, you can also log other useful data, including your PID object, and even return values from methods. Be aware though; only primitive types and classes implementing `Sendable` can be sent over NetworkTables.

For more in-depth information, we highly encourage you to read our [telemetry doc](/Telemetry.md).

### Button & subsystem bindings

From this point, you can actually see the widget in action in the sim GUI. Follow [these directions](https://docs.wpilib.org/en/stable/docs/software/dashboards/glass/mech2d-widget.html#viewing-the-mechanism2d-in-glass).
From this point, you can actually see the widget in action in the sim GUI.

However, you'll notice it just... sitting there. Which makes sense... we haven't given it any commands.

Like you've done before, create your subsystem objects with `create()`, and use the `CommandXboxController` in `Robot.java` to run your subsystem commands with it.
Like you've done before, create your subsystem objects with `create()`, and use the operator `CommandXboxController` in `Robot.java` to run your subsystem commands with it.

They might look something like this:

```java
operator.x().onTrue(arm.moveTo(x));
operator.x().onTrue(arm.moveTo(your-setpoint-here));
```

Don't forget to add your [default command]()!

### The payoff

Open up the sim GUI. Display the widget on your screen. Reminders on how to do that here.
Open up the [sim GUI](https://docs.wpilib.org/en/stable/docs/software/wpilib-tools/robot-simulation/simulation-gui.html).

[Display the widget]((https://docs.wpilib.org/en/stable/docs/software/dashboards/glass/mech2d-widget.html#viewing-the-mechanism2d-in-glass)) on your screen. Make sure your [joystick inputs](https://docs.wpilib.org/en/stable/docs/software/wpilib-tools/robot-simulation/simulation-gui.html#adding-a-system-joystick-to-joysticks) are correct, keyboard and the actual selection.

Hit one of the button axes (you'll know when the yellow square lights up) and that should run your command.

Congrats! You've successfully simulated an arm. Play around with it, set it to different angles, have fun with it!

Of course, this isn't the end.

### Tuning your simulated arm

You might have noticed that your arm does not consistently reach its setpoint, especially if the arm's setpoint is in a different quadrant.

This is the result of those constants being poorly tuned for that range of motion; it'll be up to you to tune the closed-loop control system.

Current sim behavior[^2]

[^2] As of the 24-25 school year.

One way we can make this job much easier is with the use of our `Tuning.java` utility class, which uses WPILIB's `DoubleEntry`.

```java
private final DoubleEntry p = Tuning.entry("/Robot/arm/P", kP);
private final DoubleEntry i = Tuning.entry("/Robot/arm/I", kI);
private final DoubleEntry d = Tuning.entry("/Robot/arm/D", kD);
// feel free to copy the above into Arm.java
```

These values can be modified during runtime, allowing for code modification and tuning without needing to redeploy code to test each new value.

In your `periodic()` method, update your PID constants using the values above with the `DoubleEntry.get()` method.

```java
@Override
public void periodic() {
pid.setP(p.get());
// include the rest
}
```

In the sim GUI, you can click on the values of the DoubleEntry above to set new values.

In AdvantageScope, tuning mode can be turned on [like so](https://github.com/Mechanical-Advantage/AdvantageScope/blob/main/docs/OPEN-LIVE.md#tuning-mode).

Note that the constants themselves will not change in code, only for the simulation. Be sure to note down what values worked and update your constants with those correctly tuned values!

For guidance on tuning your arm control, consult the [WPILIB docs](https://docs.wpilib.org/en/stable/docs/software/advanced-controls/introduction/tuning-vertical-arm.html).
4 changes: 2 additions & 2 deletions Telemetry.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

In testing (real or simulated), it is common to want to directly observe robot measurements and values in order to tune your systems and debug. Rather than spamming hundreds of print statements, WPILIB provides interfaces allowing certain information to be logged while the robot runs.

It sends data to [NetworkTables](https://docs.wpilib.org/en/stable/docs/software/networktables/networktables-intro.html), which can be read [while running](#dashboards) or later on in [log files](#log-viewers).
Loggers send data to [NetworkTables](https://docs.wpilib.org/en/stable/docs/software/networktables/networktables-intro.html), which can be read [while running](#dashboards) or stored for later viewing in [log files](#log-viewers).

Only classes that implement `Sendable` can be sent over NetworkTables; see [here](https://docs.wpilib.org/en/stable/docs/software/telemetry/3rd-party-libraries.html) for more details.
Only classes that implement `Sendable` can be sent over NetworkTables; see [here](https://docs.wpilib.org/en/stable/docs/software/telemetry/robot-telemetry-with-sendable.html#what-is-sendable) for more details.

For more in-depth information on the inner workings, visit [the docs](https://docs.wpilib.org/en/stable/docs/software/telemetry/telemetry.html).

Expand Down
2 changes: 1 addition & 1 deletion archive/Simulation.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Robot Simulation

Simulation in WPILib allows for code and logic to be tested onboard your computer, rather than physical hardware. It's exceptionally useful for when construction has not built the robot, but you have code to run!
Simulation in WPILib allows for code and logic to be tested onboard your computer, rather than physical hardware. It's exceptionally useful for when construction and electronics have not finished the robot, but you have code to run!

There are a few different facets of simulation to take note of before you can start, including:

Expand Down

0 comments on commit dff614b

Please sign in to comment.