7
7
import com .ctre .phoenix6 .configs .TalonFXConfiguration ;
8
8
import com .ctre .phoenix6 .controls .Follower ;
9
9
import com .ctre .phoenix6 .controls .MotionMagicVoltage ;
10
+ import com .ctre .phoenix6 .controls .VoltageOut ;
10
11
import com .ctre .phoenix6 .hardware .TalonFX ;
11
12
import com .ctre .phoenix6 .signals .*;
12
13
import edu .wpi .first .wpilibj .Servo ;
13
14
import org .team1540 .robot2024 .Constants ;
14
15
15
16
public class ElevatorIOTalonFX implements ElevatorIO {
16
-
17
- private final MotionMagicVoltage motionMagicOut = new MotionMagicVoltage (0 ).withEnableFOC (true );
18
17
private final TalonFX leader = new TalonFX (Constants .Elevator .LEADER_ID );
19
18
private final TalonFX follower = new TalonFX (Constants .Elevator .FOLLOWER_ID );
19
+
20
+ private final MotionMagicVoltage motionMagicOut = new MotionMagicVoltage (0 ).withEnableFOC (true );
21
+ private final VoltageOut voltageOut = new VoltageOut (0 ).withEnableFOC (true );
22
+ private final Follower followerReq = new Follower (leader .getDeviceID (), false );
23
+
20
24
private final Servo leftFlipper = new Servo (Constants .Elevator .LEFT_FLIPPER_ID );
21
25
private final Servo rightFlipper = new Servo (Constants .Elevator .RIGHT_FLIPPER_ID );
26
+
22
27
private final StatusSignal <Double > leaderPosition = leader .getPosition ();
23
28
private final StatusSignal <Double > leaderVelocity = leader .getVelocity ();
24
29
private final StatusSignal <Double > leaderAppliedVolts = leader .getMotorVoltage ();
30
+ private final StatusSignal <Double > followerAppliedVolts = follower .getMotorVoltage ();
25
31
private final StatusSignal <Double > leaderCurrent = leader .getStatorCurrent ();
26
32
private final StatusSignal <Double > followerCurrent = follower .getStatorCurrent ();
27
33
private final StatusSignal <Double > leaderTemp = leader .getDeviceTemp ();
28
34
private final StatusSignal <Double > followerTemp = follower .getDeviceTemp ();
29
35
private final StatusSignal <ForwardLimitValue > topLimitStatus = leader .getForwardLimit ();
30
36
private final StatusSignal <ReverseLimitValue > bottomLimitStatus = leader .getReverseLimit ();
31
- TalonFXConfiguration config ;
37
+ private final TalonFXConfiguration config ;
32
38
33
39
34
40
public ElevatorIOTalonFX () {
@@ -45,6 +51,7 @@ public ElevatorIOTalonFX() {
45
51
leaderPosition ,
46
52
leaderVelocity ,
47
53
leaderAppliedVolts ,
54
+ followerAppliedVolts ,
48
55
leaderCurrent ,
49
56
followerCurrent ,
50
57
leaderTemp ,
@@ -55,7 +62,7 @@ public ElevatorIOTalonFX() {
55
62
follower .optimizeBusUtilization ();
56
63
57
64
// setting slot 0 gains
58
- Slot0Configs slot0Configs = config . Slot0 ;
65
+ Slot0Configs slot0Configs = new Slot0Configs () ;
59
66
slot0Configs .kS = Constants .Elevator .KS ;
60
67
slot0Configs .kV = Constants .Elevator .KV ;
61
68
slot0Configs .kA = Constants .Elevator .KA ;
@@ -64,21 +71,22 @@ public ElevatorIOTalonFX() {
64
71
slot0Configs .kD = Constants .Elevator .KD ;
65
72
slot0Configs .kG = Constants .Elevator .KG ;
66
73
slot0Configs .GravityType = GravityTypeValue .Elevator_Static ;
74
+ config .Slot0 = slot0Configs ;
67
75
68
76
// setting Motion Magic Settings
69
- MotionMagicConfigs motionMagicConfigs = config . MotionMagic ;
77
+ MotionMagicConfigs motionMagicConfigs = new MotionMagicConfigs () ;
70
78
motionMagicConfigs .MotionMagicCruiseVelocity = Constants .Elevator .CRUISE_VELOCITY_MPS ;
71
79
motionMagicConfigs .MotionMagicAcceleration = Constants .Elevator .MAXIMUM_ACCELERATION_MPS2 ;
72
80
motionMagicConfigs .MotionMagicJerk = Constants .Elevator .JERK_MPS3 ;
81
+ config .MotionMagic = motionMagicConfigs ;
73
82
74
83
config .HardwareLimitSwitch .ForwardLimitEnable = true ;
75
84
config .HardwareLimitSwitch .ReverseLimitEnable = true ;
76
85
config .HardwareLimitSwitch .ReverseLimitAutosetPositionEnable = true ;
77
86
config .HardwareLimitSwitch .ReverseLimitAutosetPositionValue = 0 ;
78
87
leader .getConfigurator ().apply (config );
79
- config .MotorOutput .Inverted = InvertedValue .CounterClockwise_Positive ;
80
88
follower .getConfigurator ().apply (config );
81
- follower .setControl (new Follower ( leader . getDeviceID (), false ) );
89
+ follower .setControl (followerReq );
82
90
}
83
91
84
92
@ Override
@@ -87,6 +95,7 @@ public void updateInputs(ElevatorIOInputs inputs) {
87
95
leaderPosition ,
88
96
leaderVelocity ,
89
97
leaderAppliedVolts ,
98
+ followerAppliedVolts ,
90
99
leaderCurrent ,
91
100
followerCurrent ,
92
101
leaderTemp ,
@@ -96,7 +105,7 @@ public void updateInputs(ElevatorIOInputs inputs) {
96
105
97
106
inputs .positionMeters = leaderPosition .getValueAsDouble ();
98
107
inputs .velocityMPS = leaderVelocity .getValueAsDouble ();
99
- inputs .voltage = leaderAppliedVolts .getValueAsDouble ();
108
+ inputs .voltage = new double []{ leaderAppliedVolts .getValueAsDouble (), followerAppliedVolts . getValueAsDouble ()} ;
100
109
inputs .currentAmps = new double []{leaderCurrent .getValueAsDouble (), followerCurrent .getValueAsDouble ()};
101
110
inputs .tempCelsius = new double []{leaderTemp .getValueAsDouble (), followerTemp .getValueAsDouble ()};
102
111
inputs .atUpperLimit = topLimitStatus .getValue () == ForwardLimitValue .ClosedToGround ;
@@ -111,13 +120,14 @@ public void setSetpointMeters(double positionMeters) {
111
120
112
121
@ Override
113
122
public void setVoltage (double voltage ) {
114
- leader .set ( voltage );
123
+ leader .setControl ( voltageOut . withOutput ( voltage ) );
115
124
}
116
125
117
126
@ Override
118
127
public void setBrakeMode (boolean isBrakeMode ) {
119
128
leader .setNeutralMode (isBrakeMode ? NeutralModeValue .Brake : NeutralModeValue .Coast );
120
129
follower .setNeutralMode (isBrakeMode ? NeutralModeValue .Brake : NeutralModeValue .Coast );
130
+ follower .setControl (followerReq );
121
131
}
122
132
123
133
@ Override
@@ -134,5 +144,6 @@ public void configPID(double kP, double kI, double kD) {
134
144
configs .kI = kI ;
135
145
configs .kD = kD ;
136
146
leader .getConfigurator ().apply (configs );
147
+ follower .setControl (followerReq );
137
148
}
138
149
}
0 commit comments