001/*
002 * Copyright (C) Cross The Road Electronics.  All rights reserved.
003 * License information can be found in CTRE_LICENSE.txt
004 * For support and suggestions contact support@ctr-electronics.com or file
005 * an issue tracker at https://github.com/CrossTheRoadElec/Phoenix-Releases
006 */
007package com.ctre.phoenix6.mechanisms;
008
009import com.ctre.phoenix6.configs.*;
010import com.ctre.phoenix6.signals.*;
011
012import edu.wpi.first.units.measure.*;
013import static edu.wpi.first.units.Units.*;
014
015public class DifferentialMotorConstants<MotorConfigsT extends ParentConfiguration> {
016    /**
017     * Sensor sources for a differential Pigeon 2.
018     */
019    public enum DifferentialPigeon2Source {
020        /**
021         * Use the yaw component of the Pigeon 2.
022         */
023        Yaw,
024        /**
025         * Use the pitch component of the Pigeon 2.
026         */
027        Pitch,
028        /**
029         * Use the roll component of the Pigeon 2.
030         */
031        Roll,
032    }
033
034    /**
035     * Sensor sources for a differential CTR Electronics' CANdi™ branded device.
036     */
037    public enum DifferentialCANdiSource {
038        /**
039         * Use a pulse-width encoder remotely attached to the Sensor Input 1 (S1IN).
040         */
041        PWM1,
042        /**
043         * Use a pulse-width encoder remotely attached to the Sensor Input 2 (S2IN).
044         */
045        PWM2,
046        /**
047         * Use a quadrature encoder remotely attached to the two Sensor Inputs.
048         */
049        Quadrature,
050    }
051
052    /**
053     * Name of the CAN bus the mechanism is on. Possible CAN bus strings are:
054     * 
055     * <ul>
056     *   <li> "rio" for the native roboRIO CAN bus
057     *   <li> CANivore name or serial number
058     *   <li> SocketCAN interface (non-FRC Linux only)
059     *   <li> "*" for any CANivore seen by the program
060     *   <li> empty string (default) to select the default for the system:
061     *   <ul>
062     *     <li> "rio" on roboRIO
063     *     <li> "can0" on Linux
064     *     <li> "*" on Windows
065     *   </ul>
066     * </ul>
067     * 
068     * Note that all devices must be on the same CAN bus.
069     */
070    public String CANBusName = "";
071    /**
072     * CAN ID of the leader motor in the differential mechanism. The leader will
073     * have the differential output added to its regular output.
074     */
075    public int LeaderId = 0;
076    /**
077     * CAN ID of the follower motor in the differential mechanism. The follower will
078     * have the differential output subtracted from its regular output.
079     */
080    public int FollowerId = 0;
081    /**
082     * The alignment of the differential leader and follower motors, ignoring the
083     * configured inverts.
084     */
085    public MotorAlignmentValue Alignment = MotorAlignmentValue.Aligned;
086    /**
087     * The ratio of sensor rotations to the differential mechanism's difference
088     * output, where a ratio greater than 1 is a reduction.
089     * <p>
090     * When not using a separate sensor on the difference axis, the sensor is
091     * considered half of the difference between the two motor controllers'
092     * mechanism positions/velocities. As a result, this should be set to the gear
093     * ratio on the difference axis in that scenario, or any gear ratio between the
094     * sensor and the mechanism differential when using another sensor source.
095     */
096    public double SensorToDifferentialRatio = 1.0;
097    /**
098     * The update rate of the closed-loop controllers. This determines the update
099     * rate of the differential leader's DifferentialOutput status signal, the
100     * follower's Position and Velocity signals, and the relevant signals for any
101     * other selected differential sensor.
102     */
103    public double ClosedLoopRate = 100;
104    /**
105     * The initial configs used to configure the differential leader. The default
106     * value is the factory-default.
107     * <p>
108     * Users may change the initial configuration as they need. Any config that's
109     * not referenced in the {@link DifferentialMotorConstants} class is available
110     * to be changed.
111     * <p>
112     * The list of configs that will be overwritten is as follows:
113     * 
114     * <ul>
115     *   <li> {@link DifferentialSensorsConfigs} (automatic based on the devices
116     *        used)
117     * </ul>
118     * 
119     */
120    public MotorConfigsT LeaderInitialConfigs = null;
121    /**
122     * The initial configs used to configure the differential follower. The default
123     * value is the factory-default.
124     * <p>
125     * Users may change the initial configuration as they need. Any config that's
126     * not referenced in the {@link DifferentialMotorConstants} class is available
127     * to be changed.
128     * <p>
129     * The list of configs that will be overwritten is as follows:
130     * 
131     * <ul>
132     *   <li> {@link DifferentialSensorsConfigs} (factory defaulted)
133     *   <li> {@link MotorOutputConfigs#Inverted} (determined from {@link
134     *        #Alignment} and the {@link #LeaderInitialConfigs} invert)
135     * </ul>
136     * 
137     * If {@link #FollowerUsesCommonLeaderConfigs} is set to true (default), the
138     * following configs are copied from {@link #LeaderInitialConfigs}:
139     * 
140     * <ul>
141     *   <li> {@link AudioConfigs}
142     *   <li> {@link CurrentLimitsConfigs}
143     *   <li> {@link MotorOutputConfigs} (except {@link
144     *        MotorOutputConfigs#Inverted})
145     *   <li> {@link TorqueCurrentConfigs}
146     *   <li> {@link VoltageConfigs}
147     * </ul>
148     * 
149     */
150    public MotorConfigsT FollowerInitialConfigs = null;
151    /**
152     * Whether the follower should overwrite some of its initial configs with common
153     * configs from the {@link #LeaderInitialConfigs}, such as current limits. The
154     * list of configs that are copied is documented in {@link
155     * #FollowerInitialConfigs}.
156     */
157    public boolean FollowerUsesCommonLeaderConfigs = true;
158    
159    /**
160     * Modifies the CANBusName parameter and returns itself.
161     * <p>
162     * Name of the CAN bus the mechanism is on. Possible CAN bus strings are:
163     * 
164     * <ul>
165     *   <li> "rio" for the native roboRIO CAN bus
166     *   <li> CANivore name or serial number
167     *   <li> SocketCAN interface (non-FRC Linux only)
168     *   <li> "*" for any CANivore seen by the program
169     *   <li> empty string (default) to select the default for the system:
170     *   <ul>
171     *     <li> "rio" on roboRIO
172     *     <li> "can0" on Linux
173     *     <li> "*" on Windows
174     *   </ul>
175     * </ul>
176     * 
177     * Note that all devices must be on the same CAN bus.
178     *
179     * @param newCANBusName Parameter to modify
180     * @return this object
181     */
182    public DifferentialMotorConstants<MotorConfigsT> withCANBusName(String newCANBusName) {
183        this.CANBusName = newCANBusName;
184        return this;
185    }
186    
187    /**
188     * Modifies the LeaderId parameter and returns itself.
189     * <p>
190     * CAN ID of the leader motor in the differential mechanism. The leader will
191     * have the differential output added to its regular output.
192     *
193     * @param newLeaderId Parameter to modify
194     * @return this object
195     */
196    public DifferentialMotorConstants<MotorConfigsT> withLeaderId(int newLeaderId) {
197        this.LeaderId = newLeaderId;
198        return this;
199    }
200    
201    /**
202     * Modifies the FollowerId parameter and returns itself.
203     * <p>
204     * CAN ID of the follower motor in the differential mechanism. The follower will
205     * have the differential output subtracted from its regular output.
206     *
207     * @param newFollowerId Parameter to modify
208     * @return this object
209     */
210    public DifferentialMotorConstants<MotorConfigsT> withFollowerId(int newFollowerId) {
211        this.FollowerId = newFollowerId;
212        return this;
213    }
214    
215    /**
216     * Modifies the Alignment parameter and returns itself.
217     * <p>
218     * The alignment of the differential leader and follower motors, ignoring the
219     * configured inverts.
220     *
221     * @param newAlignment Parameter to modify
222     * @return this object
223     */
224    public DifferentialMotorConstants<MotorConfigsT> withAlignment(MotorAlignmentValue newAlignment) {
225        this.Alignment = newAlignment;
226        return this;
227    }
228    
229    /**
230     * Modifies the SensorToDifferentialRatio parameter and returns itself.
231     * <p>
232     * The ratio of sensor rotations to the differential mechanism's difference
233     * output, where a ratio greater than 1 is a reduction.
234     * <p>
235     * When not using a separate sensor on the difference axis, the sensor is
236     * considered half of the difference between the two motor controllers'
237     * mechanism positions/velocities. As a result, this should be set to the gear
238     * ratio on the difference axis in that scenario, or any gear ratio between the
239     * sensor and the mechanism differential when using another sensor source.
240     *
241     * @param newSensorToDifferentialRatio Parameter to modify
242     * @return this object
243     */
244    public DifferentialMotorConstants<MotorConfigsT> withSensorToDifferentialRatio(double newSensorToDifferentialRatio) {
245        this.SensorToDifferentialRatio = newSensorToDifferentialRatio;
246        return this;
247    }
248    
249    /**
250     * Modifies the ClosedLoopRate parameter and returns itself.
251     * <p>
252     * The update rate of the closed-loop controllers. This determines the update
253     * rate of the differential leader's DifferentialOutput status signal, the
254     * follower's Position and Velocity signals, and the relevant signals for any
255     * other selected differential sensor.
256     *
257     * @param newClosedLoopRate Parameter to modify
258     * @return this object
259     */
260    public DifferentialMotorConstants<MotorConfigsT> withClosedLoopRate(double newClosedLoopRate) {
261        this.ClosedLoopRate = newClosedLoopRate;
262        return this;
263    }
264    
265    /**
266     * Modifies the ClosedLoopRate parameter and returns itself.
267     * <p>
268     * The update rate of the closed-loop controllers. This determines the update
269     * rate of the differential leader's DifferentialOutput status signal, the
270     * follower's Position and Velocity signals, and the relevant signals for any
271     * other selected differential sensor.
272     *
273     * @param newClosedLoopRate Parameter to modify
274     * @return this object
275     */
276    public DifferentialMotorConstants<MotorConfigsT> withClosedLoopRate(Frequency newClosedLoopRate) {
277        this.ClosedLoopRate = newClosedLoopRate.in(Hertz);
278        return this;
279    }
280    
281    /**
282     * Modifies the LeaderInitialConfigs parameter and returns itself.
283     * <p>
284     * The initial configs used to configure the differential leader. The default
285     * value is the factory-default.
286     * <p>
287     * Users may change the initial configuration as they need. Any config that's
288     * not referenced in the {@link DifferentialMotorConstants} class is available
289     * to be changed.
290     * <p>
291     * The list of configs that will be overwritten is as follows:
292     * 
293     * <ul>
294     *   <li> {@link DifferentialSensorsConfigs} (automatic based on the devices
295     *        used)
296     * </ul>
297     * 
298     *
299     * @param newLeaderInitialConfigs Parameter to modify
300     * @return this object
301     */
302    public DifferentialMotorConstants<MotorConfigsT> withLeaderInitialConfigs(MotorConfigsT newLeaderInitialConfigs) {
303        this.LeaderInitialConfigs = newLeaderInitialConfigs;
304        return this;
305    }
306    
307    /**
308     * Modifies the FollowerInitialConfigs parameter and returns itself.
309     * <p>
310     * The initial configs used to configure the differential follower. The default
311     * value is the factory-default.
312     * <p>
313     * Users may change the initial configuration as they need. Any config that's
314     * not referenced in the {@link DifferentialMotorConstants} class is available
315     * to be changed.
316     * <p>
317     * The list of configs that will be overwritten is as follows:
318     * 
319     * <ul>
320     *   <li> {@link DifferentialSensorsConfigs} (factory defaulted)
321     *   <li> {@link MotorOutputConfigs#Inverted} (determined from {@link
322     *        #Alignment} and the {@link #LeaderInitialConfigs} invert)
323     * </ul>
324     * 
325     * If {@link #FollowerUsesCommonLeaderConfigs} is set to true (default), the
326     * following configs are copied from {@link #LeaderInitialConfigs}:
327     * 
328     * <ul>
329     *   <li> {@link AudioConfigs}
330     *   <li> {@link CurrentLimitsConfigs}
331     *   <li> {@link MotorOutputConfigs} (except {@link
332     *        MotorOutputConfigs#Inverted})
333     *   <li> {@link TorqueCurrentConfigs}
334     *   <li> {@link VoltageConfigs}
335     * </ul>
336     * 
337     *
338     * @param newFollowerInitialConfigs Parameter to modify
339     * @return this object
340     */
341    public DifferentialMotorConstants<MotorConfigsT> withFollowerInitialConfigs(MotorConfigsT newFollowerInitialConfigs) {
342        this.FollowerInitialConfigs = newFollowerInitialConfigs;
343        return this;
344    }
345    
346    /**
347     * Modifies the FollowerUsesCommonLeaderConfigs parameter and returns itself.
348     * <p>
349     * Whether the follower should overwrite some of its initial configs with common
350     * configs from the {@link #LeaderInitialConfigs}, such as current limits. The
351     * list of configs that are copied is documented in {@link
352     * #FollowerInitialConfigs}.
353     *
354     * @param newFollowerUsesCommonLeaderConfigs Parameter to modify
355     * @return this object
356     */
357    public DifferentialMotorConstants<MotorConfigsT> withFollowerUsesCommonLeaderConfigs(boolean newFollowerUsesCommonLeaderConfigs) {
358        this.FollowerUsesCommonLeaderConfigs = newFollowerUsesCommonLeaderConfigs;
359        return this;
360    }
361}