Skip to content

Commit 05f2f79

Browse files
committed
Added somewhat thorough documentation to lib2813
1 parent 0674d33 commit 05f2f79

67 files changed

Lines changed: 5867 additions & 1755 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

lib/src/main/java/com/team2813/lib2813/control/ControlMode.java

Lines changed: 99 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,106 @@
22

33
import com.revrobotics.spark.SparkBase.ControlType;
44

5+
/**
6+
* Enumeration defining standardized motor control modes for the team's control system.
7+
*
8+
* <p>This enum provides a vendor-neutral abstraction over different motor controller
9+
* control modes while maintaining compatibility with specific hardware implementations.
10+
* Each control mode maps to the appropriate vendor-specific control type for seamless
11+
* integration with different motor controller families.
12+
*
13+
* <p>The enum currently includes mappings for REV Robotics SPARK controllers, with
14+
* each mode corresponding to a specific {@link ControlType} from the SPARK API.
15+
* Additional vendor mappings can be added as needed.
16+
*
17+
* <p>Supported control modes:
18+
* <ul>
19+
* <li><b>DUTY_CYCLE:</b> Open-loop duty cycle control (percentage output)</li>
20+
* <li><b>VELOCITY:</b> Closed-loop velocity control with PID feedback</li>
21+
* <li><b>MOTION_MAGIC:</b> Closed-loop position control with motion profiling</li>
22+
* <li><b>VOLTAGE:</b> Open-loop voltage control</li>
23+
* </ul>
24+
*
25+
* @author Team 2813
26+
* @since 1.0
27+
*/
528
public enum ControlMode {
6-
DUTY_CYCLE(ControlType.kDutyCycle),
7-
VELOCITY(ControlType.kVelocity),
8-
MOTION_MAGIC(ControlType.kPosition),
9-
VOLTAGE(ControlType.kVoltage);
29+
30+
/**
31+
* Open-loop duty cycle control mode.
32+
*
33+
* <p>Controls the motor by setting the duty cycle (percentage of full power) directly.
34+
* The motor output is proportional to the demand value, typically ranging from
35+
* -1.0 (full reverse) to +1.0 (full forward). This is the most basic control mode
36+
* and does not use feedback control.
37+
*
38+
* <p>Maps to {@link ControlType#kDutyCycle} for SPARK controllers.
39+
*/
40+
DUTY_CYCLE(ControlType.kDutyCycle),
41+
42+
/**
43+
* Closed-loop velocity control mode.
44+
*
45+
* <p>Controls the motor to maintain a specific velocity using PID feedback control.
46+
* The controller continuously adjusts the motor output to minimize the error between
47+
* the demanded velocity and the actual measured velocity from the encoder. This mode
48+
* is ideal for applications requiring consistent speed regardless of load variations.
49+
*
50+
* <p>Maps to {@link ControlType#kVelocity} for SPARK controllers.
51+
*/
52+
VELOCITY(ControlType.kVelocity),
53+
54+
/**
55+
* Closed-loop position control mode with motion profiling.
56+
*
57+
* <p>Controls the motor to reach a specific position using advanced motion profiling
58+
* algorithms. The controller generates smooth velocity and acceleration profiles to
59+
* move the mechanism to the target position while respecting configured motion
60+
* constraints (max velocity, max acceleration). This mode provides the smoothest
61+
* and most controlled movement for precise positioning applications.
62+
*
63+
* <p>Maps to {@link ControlType#kPosition} for SPARK controllers.
64+
*
65+
* <p><b>Note:</b> Despite the name "MOTION_MAGIC," this maps to position control
66+
* type for SPARK controllers, as the motion profiling is handled internally.
67+
*/
68+
MOTION_MAGIC(ControlType.kPosition),
69+
70+
/**
71+
* Open-loop voltage control mode.
72+
*
73+
* <p>Controls the motor by applying a specific voltage directly to the motor terminals.
74+
* Unlike duty cycle control, voltage control compensates for battery voltage variations
75+
* to provide more consistent motor behavior. The demand value represents the desired
76+
* voltage to apply to the motor.
77+
*
78+
* <p>Maps to {@link ControlType#kVoltage} for SPARK controllers.
79+
*/
80+
VOLTAGE(ControlType.kVoltage);
1081

11-
private final ControlType sparkMode;
82+
/** The corresponding SPARK controller control type for this mode */
83+
private final ControlType sparkMode;
1284

13-
ControlMode(ControlType sparkMode) {
14-
this.sparkMode = sparkMode;
15-
}
85+
/**
86+
* Creates a ControlMode with the specified SPARK controller mapping.
87+
*
88+
* @param sparkMode the corresponding {@link ControlType} for SPARK controllers
89+
*/
90+
ControlMode(ControlType sparkMode) {
91+
this.sparkMode = sparkMode;
92+
}
1693

17-
public ControlType getSparkMode() {
18-
return sparkMode;
19-
}
20-
}
94+
/**
95+
* Gets the corresponding SPARK controller control type for this mode.
96+
*
97+
* <p>This method provides the mapping from the vendor-neutral ControlMode
98+
* to the specific {@link ControlType} required by REV Robotics SPARK
99+
* controllers. This abstraction allows the same control mode enum to be
100+
* used across different motor controller implementations.
101+
*
102+
* @return the corresponding {@link ControlType} for SPARK controllers
103+
*/
104+
public ControlType getSparkMode() {
105+
return sparkMode;
106+
}
107+
}

lib/src/main/java/com/team2813/lib2813/control/DeviceInformation.java

Lines changed: 138 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -3,59 +3,142 @@
33
import com.team2813.lib2813.util.InputValidation;
44
import java.util.Optional;
55

6+
/**
7+
* Immutable value class representing the identifying information for a CAN device.
8+
*
9+
* <p>This class encapsulates the essential information needed to uniquely identify
10+
* a device on the CAN bus network: the CAN ID and the specific CAN bus the device
11+
* is connected to. It provides a standardized way to represent device identity
12+
* across the team's control system architecture.
13+
*
14+
* <p>The class distinguishes between devices on the RoboRIO's built-in CAN bus
15+
* (represented by an empty Optional for the canbus) and devices on named CAN buses
16+
* such as CANivore or other CAN bus interfaces.
17+
*
18+
* <p>Key features:
19+
* <ul>
20+
* <li>Immutable design for thread safety and reliable identity</li>
21+
* <li>Input validation for CAN ID range [0, 62]</li>
22+
* <li>Proper equals() and hashCode() implementation for use in collections</li>
23+
* <li>Optional-based canbus representation for null safety</li>
24+
* </ul>
25+
*
26+
* <p>This class is commonly used as a key in device registries and for comparing
27+
* device instances to determine if they represent the same physical hardware.
28+
*
29+
* @author Team 2813
30+
* @since 1.0
31+
*/
632
public final class DeviceInformation {
7-
private int id;
8-
private Optional<String> canbus;
9-
10-
/**
11-
* Creates a DeviceInformation for a device on the RoboRIO can loop
12-
*
13-
* @param id the can ID
14-
*/
15-
public DeviceInformation(int id) {
16-
this(id, null);
17-
}
18-
19-
/**
20-
* Creates a DeviceInformation with a canbus string. If {@code canbus} is {@code null}, method
21-
* acts like {@link #DeviceInformation(int)} was called
22-
*
23-
* @param id the CAN id
24-
* @param canbus the canbus string
25-
*/
26-
public DeviceInformation(int id, String canbus) {
27-
this.id = InputValidation.checkCanId(id);
28-
this.canbus = Optional.ofNullable(canbus);
29-
}
30-
31-
/**
32-
* Gets the can id of this device
33-
*
34-
* @return the can id of the device
35-
*/
36-
public int id() {
37-
return id;
38-
}
39-
40-
/**
41-
* Returns the canbus that this device is on, or {@link Optional#empty()} if it is on the RoboRIO
42-
* can loop
43-
*
44-
* @return the canbus that the device is on
45-
*/
46-
public Optional<String> canbus() {
47-
return canbus;
48-
}
49-
50-
@Override
51-
public boolean equals(Object o) {
52-
if (!(o instanceof DeviceInformation)) return false;
53-
DeviceInformation other = (DeviceInformation) o;
54-
return other.id == id && other.canbus.equals(canbus);
55-
}
56-
57-
@Override
58-
public int hashCode() {
59-
return id * 31 + canbus.hashCode();
60-
}
61-
}
33+
34+
/** The CAN ID of the device, validated to be in range [0, 62] */
35+
private int id;
36+
37+
/** The CAN bus name, empty if on the RoboRIO's default CAN bus */
38+
private Optional<String> canbus;
39+
40+
/**
41+
* Creates DeviceInformation for a device on the RoboRIO's default CAN bus.
42+
*
43+
* <p>This constructor is used for devices connected directly to the RoboRIO's
44+
* built-in CAN bus interface. The canbus will be represented as an empty
45+
* Optional to indicate the default bus.
46+
*
47+
* @param id the CAN ID of the device, must be in range [0, 62]
48+
* @throws com.team2813.lib2813.util.InvalidCanIdException if the CAN ID is outside the valid range
49+
*/
50+
public DeviceInformation(int id) {
51+
this(id, null);
52+
}
53+
54+
/**
55+
* Creates DeviceInformation with a specific CAN bus name.
56+
*
57+
* <p>This constructor supports devices on named CAN buses such as CANivore
58+
* devices or other CAN bus interfaces. If {@code canbus} is {@code null},
59+
* this method behaves identically to {@link #DeviceInformation(int)}.
60+
*
61+
* <p>Examples of valid canbus names:
62+
* <ul>
63+
* <li>{@code "canivore"} - For CANivore devices</li>
64+
* <li>{@code "CANivore_12345"} - For CANivore with specific serial number</li>
65+
* <li>{@code null} - For RoboRIO default CAN bus</li>
66+
* </ul>
67+
*
68+
* @param id the CAN ID of the device, must be in range [0, 62]
69+
* @param canbus the CAN bus name, or {@code null} for the RoboRIO default bus
70+
* @throws com.team2813.lib2813.util.InvalidCanIdException if the CAN ID is outside the valid range
71+
*/
72+
public DeviceInformation(int id, String canbus) {
73+
this.id = InputValidation.checkCanId(id);
74+
this.canbus = Optional.ofNullable(canbus);
75+
}
76+
77+
/**
78+
* Gets the CAN ID of this device.
79+
*
80+
* <p>The CAN ID is guaranteed to be in the valid range [0, 62] due to
81+
* validation performed during construction.
82+
*
83+
* @return the CAN ID of the device
84+
*/
85+
public int id() {
86+
return id;
87+
}
88+
89+
/**
90+
* Returns the CAN bus that this device is connected to.
91+
*
92+
* <p>The return value interpretation:
93+
* <ul>
94+
* <li>{@link Optional#empty()} - Device is on the RoboRIO's default CAN bus</li>
95+
* <li>{@code Optional.of("busname")} - Device is on the named CAN bus</li>
96+
* </ul>
97+
*
98+
* <p>This Optional-based approach provides null safety and makes the
99+
* distinction between default and named buses explicit in the API.
100+
*
101+
* @return an Optional containing the CAN bus name, or empty if on the RoboRIO CAN bus
102+
*/
103+
public Optional<String> canbus() {
104+
return canbus;
105+
}
106+
107+
/**
108+
* Determines whether this DeviceInformation is equal to another object.
109+
*
110+
* <p>Two DeviceInformation instances are considered equal if and only if
111+
* they have the same CAN ID and are on the same CAN bus. This allows
112+
* DeviceInformation objects to be used reliably as keys in hash-based
113+
* collections and for device identity comparisons.
114+
*
115+
* @param o the object to compare against
116+
* @return {@code true} if the objects represent the same device,
117+
* {@code false} otherwise
118+
*/
119+
@Override
120+
public boolean equals(Object o) {
121+
if (!(o instanceof DeviceInformation)) return false;
122+
DeviceInformation other = (DeviceInformation) o;
123+
return other.id == id && other.canbus.equals(canbus);
124+
}
125+
126+
/**
127+
* Returns a hash code value for this DeviceInformation.
128+
*
129+
* <p>The hash code is computed from both the CAN ID and the CAN bus
130+
* information, ensuring that equal DeviceInformation objects have
131+
* equal hash codes. This implementation supports the use of
132+
* DeviceInformation objects in hash-based collections such as
133+
* HashMap and HashSet.
134+
*
135+
* <p>The hash code formula combines the ID and canbus hash using
136+
* a prime multiplier for good distribution properties.
137+
*
138+
* @return a hash code value for this object
139+
*/
140+
@Override
141+
public int hashCode() {
142+
return id * 31 + canbus.hashCode();
143+
}
144+
}

0 commit comments

Comments
 (0)