Skip to content

Commit 4271bfe

Browse files
committed
logging setup
1 parent 4e7a5a3 commit 4271bfe

File tree

5 files changed

+72
-47
lines changed

5 files changed

+72
-47
lines changed

Template/src/main/java/frc/robot/RobotContainer.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,15 @@ public RobotContainer() {
1717
DriverStation.silenceJoystickConnectionWarning(true);
1818
DriverStation.removeRefreshedDataEventHandle(44000);
1919

20+
// data logs
2021
DataLogManager.start();
2122
DataLogManager.logNetworkTables(true);
2223
DriverStation.startDataLog(DataLogManager.getLog(), true);
2324

25+
// When to go into brownout protection
2426
RobotController.setBrownoutVoltage(7.0);
2527

28+
// robot setup
2629
JoystickIO.getButtonBindings();
2730
AutoUtils.initAuto();
2831
}

Template/src/main/java/frc/robot/commands/auto/AutoController.java

Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,8 @@ public class AutoController {
1414
private static PIDController yController = new PIDController(AutoConstants.kPDrive, 0, AutoConstants.kDDrive);
1515
private static PIDController turnController = new PIDController(AutoConstants.kPTurn, 0, AutoConstants.kDTurn);
1616

17-
private static ShuffleData<Double[]> setpointPositionLog = new ShuffleData<Double[]>(
18-
"Auto",
19-
"setpoint position",
20-
new Double[] { 0.0, 0.0, 0.0 });
21-
private static ShuffleData<Double[]> setpointVelocityLog = new ShuffleData<Double[]>(
22-
"Auto",
23-
"setpoint velocity",
24-
new Double[] { 0.0, 0.0, 0.0 });
25-
private static ShuffleData<Double[]> setpointAccelerationLog = new ShuffleData<Double[]>(
26-
"Auto",
27-
"setpoint acceleration",
28-
new Double[] { 0.0, 0.0, 0.0 });
29-
3017
public static void choreoController(Pose2d curPose, SwerveSample sample) {
31-
logSetpoints(sample);
18+
Robot.swerve.logSetpoints(sample);
3219
ChassisSpeeds speeds = ChassisSpeeds.fromFieldRelativeSpeeds(
3320
new ChassisSpeeds(
3421
xController.calculate(curPose.getX(), sample.x) + sample.vx,
@@ -39,17 +26,4 @@ public static void choreoController(Pose2d curPose, SwerveSample sample) {
3926
Robot.swerve.setChassisSpeeds(speeds);
4027
}
4128

42-
private static void logSetpoints(SwerveSample sample) {
43-
Double[] positions = new Double[] { sample.x, sample.y, sample.heading };
44-
positions[2] = Units.radiansToDegrees(positions[2]);
45-
setpointPositionLog.set(positions);
46-
47-
Double[] velocities = new Double[] { sample.vx, sample.vy, sample.omega };
48-
velocities[2] = Units.radiansToDegrees(velocities[2]);
49-
setpointVelocityLog.set(velocities);
50-
51-
Double[] accelerations = new Double[] { sample.ax, sample.ay, sample.alpha };
52-
accelerations[2] = Units.radiansToDegrees(accelerations[2]);
53-
setpointAccelerationLog.set(accelerations);
54-
}
5529
}

Template/src/main/java/frc/robot/commands/auto/AutoUtils.java

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
import choreo.Choreo;
44
import choreo.auto.AutoChooser;
55
import choreo.auto.AutoFactory;
6+
import choreo.auto.AutoTrajectory;
67
import choreo.auto.AutoFactory.AutoBindings;
78
import choreo.trajectory.SwerveSample;
89
import edu.wpi.first.math.geometry.Pose2d;
10+
import edu.wpi.first.wpilibj2.command.Command;
911
import edu.wpi.first.wpilibj2.command.Commands;
1012
import frc.robot.Robot;
1113
import frc.robot.utils.UtilityFunctions;
@@ -24,6 +26,23 @@ public static void initAuto() {
2426

2527
}
2628

29+
public static AutoChooser getChooser() {
30+
return chooser;
31+
}
32+
33+
public static Command makeStartingTrajectoryCommand(AutoTrajectory trajectory) {
34+
return Commands.runOnce(
35+
() -> Robot.swerve.setOdometry(trajectory.getInitialPose().orElseGet(() -> Robot.swerve.getPose())))
36+
.andThen(trajectory.cmd());
37+
}
38+
39+
public static Command addResetLoggingCommand(Command cmd) {
40+
return cmd.andThen(
41+
Commands.runOnce(() -> Robot.swerve.logSetpoints(
42+
new SwerveSample(-100, 0, -100, 0, 0, 0, 0, 0,
43+
0, 0, new double[] { 0.0, 0.0, 0.0, 0.0 }, new double[] { 0.0, 0.0, 0.0, 0.0 }))));
44+
}
45+
2746
private static void setupFactory() {
2847
// what commands run on what markers
2948
AutoBindings bindings = new AutoFactory.AutoBindings();
@@ -50,10 +69,4 @@ private static void setupChooser() {
5069

5170
}
5271

53-
54-
55-
public static AutoChooser getChooser() {
56-
return chooser;
57-
}
58-
5972
}

Template/src/main/java/frc/robot/commands/auto/Autos.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
import choreo.auto.AutoFactory;
55
import choreo.auto.AutoLoop;
66
import choreo.auto.AutoTrajectory;
7+
import choreo.trajectory.SwerveSample;
78
import frc.robot.Robot;
9+
import frc.robot.subsystems.swerve.Swerve;
810
import edu.wpi.first.wpilibj2.command.Command;
911
import edu.wpi.first.wpilibj2.command.Commands;
1012

@@ -21,15 +23,14 @@ public static Command getMyRoutine(AutoFactory factory) {
2123
// instaniate our auto loop and trajectories
2224
AutoLoop loop = factory.newLoop("auto");
2325
AutoTrajectory trajectory = factory.trajectory("trajectoryName", loop);
24-
25-
// create our trajectory commands, setting odometry if needed
26-
Command trajectoryCommand = Commands.runOnce(
27-
() -> Robot.swerve.setOdometry(trajectory.getInitialPose().orElseGet(() -> Robot.swerve.getPose())));
28-
trajectoryCommand = trajectoryCommand.andThen(trajectory.cmd());
26+
27+
// create our trajectory commands, setting odometry and resetting logging when finished
28+
Command trajectoryCommand = AutoUtils.addResetLoggingCommand(
29+
AutoUtils.makeStartingTrajectoryCommand(trajectory));
2930

3031
// set the command to begin when the loop enables
3132
loop.enabled().onTrue(trajectoryCommand);
32-
33+
3334
// our final, total command
3435
Command cmd;
3536

Template/src/main/java/frc/robot/subsystems/swerve/Swerve.java

Lines changed: 42 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
package frc.robot.subsystems.swerve;
66

7+
import choreo.trajectory.SwerveSample;
78
import edu.wpi.first.math.MathUtil;
89
import edu.wpi.first.math.VecBuilder;
910
import edu.wpi.first.math.estimator.SwerveDrivePoseEstimator;
@@ -44,6 +45,7 @@ public class Swerve extends SubsystemBase {
4445
// equivilant to a odometer, but also intakes vision
4546
private SwerveDrivePoseEstimator swerveDrivePoseEstimator;
4647

48+
// Logging
4749
private ShuffleData<Double[]> odometryLog = new ShuffleData<Double[]>(
4850
this.getName(),
4951
"odometry",
@@ -98,6 +100,19 @@ public class Swerve extends SubsystemBase {
98100
"heading",
99101
0.0);
100102

103+
private ShuffleData<Double[]> setpointPositionLog = new ShuffleData<Double[]>(
104+
this.getName(),
105+
"setpoint position",
106+
new Double[] { 0.0, 0.0, 0.0 });
107+
private ShuffleData<Double[]> setpointVelocityLog = new ShuffleData<Double[]>(
108+
this.getName(),
109+
"setpoint velocity",
110+
new Double[] { 0.0, 0.0, 0.0 });
111+
private ShuffleData<Double[]> setpointAccelerationLog = new ShuffleData<Double[]>(
112+
this.getName(),
113+
"setpoint acceleration",
114+
new Double[] { 0.0, 0.0, 0.0 });
115+
101116
public Swerve() {
102117

103118
// if simulation
@@ -320,16 +335,22 @@ public void resetGyro() {
320335
}
321336
}
322337

323-
@Override
324-
public void periodic() {
325-
gyro.updateData(gyroData);
326-
updateOdometry();
338+
public void logSetpoints(SwerveSample sample) {
339+
// setpoint logging for automated driving
340+
Double[] positions = new Double[] { sample.x, sample.y, sample.heading };
341+
positions[2] = Units.radiansToDegrees(positions[2]);
342+
setpointPositionLog.set(positions);
327343

328-
// periodic method for individual modules
329-
for (int i = 0; i < 4; i++) {
330-
modules[i].periodic();
331-
}
344+
Double[] velocities = new Double[] { sample.vx, sample.vy, sample.omega };
345+
velocities[2] = Units.radiansToDegrees(velocities[2]);
346+
setpointVelocityLog.set(velocities);
332347

348+
Double[] accelerations = new Double[] { sample.ax, sample.ay, sample.alpha };
349+
accelerations[2] = Units.radiansToDegrees(accelerations[2]);
350+
setpointAccelerationLog.set(accelerations);
351+
}
352+
353+
private void logData() {
333354
// logging of our module states
334355
Double[] realStates = {
335356
modules[0].getState().angle.getDegrees(),
@@ -378,6 +399,19 @@ public void periodic() {
378399
velocityLog.set(robotVelocity);
379400
accelerationLog.set((robotVelocity - prevVelocity) / .02);
380401
prevVelocity = robotVelocity;
402+
}
403+
404+
@Override
405+
public void periodic() {
406+
gyro.updateData(gyroData);
407+
updateOdometry();
408+
409+
// periodic method for individual modules
410+
for (int i = 0; i < 4; i++) {
411+
modules[i].periodic();
412+
}
413+
414+
logData();
381415

382416
}
383417

0 commit comments

Comments
 (0)