-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGyroIOPigeon2.java
More file actions
executable file
·68 lines (58 loc) · 2.59 KB
/
GyroIOPigeon2.java
File metadata and controls
executable file
·68 lines (58 loc) · 2.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// Copyright 2021-2025 FRC 6328
// http://github.com/Mechanical-Advantage
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// version 3 as published by the Free Software Foundation or
// available in the root directory of this project.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
package frc.robot.subsystems.drive;
import java.util.Queue;
import com.ctre.phoenix6.BaseStatusSignal;
import com.ctre.phoenix6.CANBus;
import com.ctre.phoenix6.StatusCode;
import com.ctre.phoenix6.StatusSignal;
import com.ctre.phoenix6.configs.GyroTrimConfigs;
import com.ctre.phoenix6.configs.Pigeon2Configuration;
import com.ctre.phoenix6.hardware.Pigeon2;
import edu.wpi.first.math.geometry.Rotation2d;
import edu.wpi.first.units.measure.Angle;
/** IO implementation for Pigeon 2. */
public class GyroIOPigeon2 implements GyroIO {
private final Pigeon2 pigeon;
private final Queue<Double> yawPositionQueue;
private final Queue<Double> yawTimestampQueue;
private final StatusSignal<Angle> yaw;
public GyroIOPigeon2(int Pigeon2Id, CANBus bus) {
// Init Pigeon and Statuses
pigeon = new Pigeon2(Pigeon2Id, bus);
yaw = pigeon.getYaw();
pigeon.getConfigurator().apply(new Pigeon2Configuration());
pigeon.getConfigurator().setYaw(0.0);
yaw.setUpdateFrequency(Drive.ODOMETRY_FREQUENCY);
pigeon.optimizeBusUtilization();
yawTimestampQueue = PhoenixOdometryThread.getInstance().makeTimestampQueue();
yawPositionQueue = PhoenixOdometryThread.getInstance().registerSignal(pigeon.getYaw());
// Improve pigeon's yaw accuracy using the Gyroscope Sensitivity Calibration procedure
GyroTrimConfigs trimConfigs = new GyroTrimConfigs();
trimConfigs.GyroScalarZ = Drive.GYRO_YAW_DEG_PER_ROT_CORRECTION;
pigeon.getConfigurator().apply(trimConfigs);
}
@Override
public void updateInputs(GyroIOInputs inputs) {
inputs.connected = BaseStatusSignal.refreshAll(yaw).equals(StatusCode.OK);
inputs.yawPosition = Rotation2d.fromDegrees(yaw.getValueAsDouble());
inputs.odometryYawTimestamps =
yawTimestampQueue.stream().mapToDouble((Double value) -> value).toArray();
inputs.odometryYawPositions =
yawPositionQueue.stream()
.map((Double value) -> Rotation2d.fromDegrees(value))
.toArray(Rotation2d[]::new);
yawTimestampQueue.clear();
yawPositionQueue.clear();
}
}