Skip to content

Commit f7bfc04

Browse files
jasondamingkatzuvclaudesciencewhiz
authored
Improve motor controller code examples for clarity (#3144)
* Improve motor controller code examples for clarity - Changed confusing variable names (Spark spark -> intakeMotor) - Used different PWM ports (0 and 1) to avoid confusion - Added descriptive comments for each line - Added 'Basic Usage' section header - Added important note explaining set() range (-1.0 to 1.0) - Added 'Where to Put This Code' section with examples for: - Command-Based programs (subsystem class) - Timed Robot programs (Robot class with robotInit/teleopPeriodic) - Added 'Common Use Cases' section listing real mechanisms: - Intakes, shooters, conveyors, arms/elevators, climbers - Links to drivetrain classes and WPILib examples Fixes #1385 * Update source/docs/software/hardware-apis/motors/using-motor-controllers.rst Co-authored-by: Dan Katzuv <[email protected]> * Address review feedback on motor controller examples Changes per reviewer feedback: 1. Removed confusing line about PWM controllers being controlled the same as CAN controllers (sciencewhiz comment on line 7) 2. Deleted entire "Basic Usage" section as it was redundant with the "Where to Put This Code" section (sciencewhiz comment on line 11). The Basic Usage examples didn't add value since the Command-Based and Timed Robot examples show declaration and usage more clearly. 3. Moved the set() range explanation (important box) up to right after the note about Spark/VictorSP, so it's not lost with the deletion of Basic Usage section. 4. Fixed C++ motor controller declaration in Timed Robot example (sciencewhiz comment on line 162): - Changed from std::unique_ptr with make_unique in RobotInit() - To direct member variable initialized in initializer list - This matches the recommended pattern used in Command-Based example - Removed RobotInit() method as it's no longer needed 5. Updated CAN motor controllers vendors list (sciencewhiz comment on line 195): - Added specific examples (Talon FX, SPARK MAX/FLEX) - Added Redux Robotics - Changed "handful" to just listing vendors with "and others" 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]> * Fix Java joystick declaration bug in TimedRobot example Added missing joystick declaration and initialization in Java example. The code was referencing m_joystick.getRawButton(1) but never declared or initialized the joystick object, which would cause a compilation error. Added: - private Joystick m_joystick; member variable - m_joystick = new Joystick(0); initialization in robotInit() This matches the C++ and Python examples which already had the joystick properly declared and initialized. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]> * Remove Redux --------- Co-authored-by: Dan Katzuv <[email protected]> Co-authored-by: Claude <[email protected]> Co-authored-by: sciencewhiz <[email protected]>
1 parent 465aa23 commit f7bfc04

File tree

1 file changed

+125
-14
lines changed

1 file changed

+125
-14
lines changed

source/docs/software/hardware-apis/motors/using-motor-controllers.rst

Lines changed: 125 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,33 +4,144 @@ Motor controllers come in two main flavors: :term:`CAN` and :term:`PWM`. A CAN c
44

55
## Using PWM Motor Controllers
66

7-
PWM motor controllers can be controlled in the same way as a CAN motor controller. For a more detailed background on *how* they work, see :doc:`pwm-controllers`. To use a PWM motor controller, simply use the appropriate motor controller class provided by WPILib and supply it the port the motor controller(s) are plugged into on the roboRIO. All approved motor controllers have WPILib classes provided for them.
7+
For a more detailed background on *how* PWM motor controllers work, see :doc:`pwm-controllers`. To use a PWM motor controller, simply use the appropriate motor controller class provided by WPILib and supply it the port the motor controller(s) are plugged into on the roboRIO. All approved motor controllers have WPILib classes provided for them.
88

99
.. note:: The ``Spark`` and ``VictorSP`` classes are used here as an example; other PWM motor controller classes have exactly the same API.
1010

11+
.. important:: The ``set()`` method accepts values from **-1.0 to 1.0**, where:
12+
13+
- **-1.0** = full speed reverse
14+
- **0.0** = stopped
15+
- **1.0** = full speed forward
16+
17+
### Where to Put This Code
18+
19+
**In Command-Based programs:**
20+
21+
Motor controllers should be declared as member variables in your subsystem class. Create them in the subsystem constructor, and use them in command methods.
22+
23+
.. tab-set-code::
24+
25+
```java
26+
public class IntakeSubsystem extends SubsystemBase {
27+
private final Spark m_motor = new Spark(0);
28+
29+
public void runIntake() {
30+
m_motor.set(0.8);
31+
}
32+
33+
public void stopIntake() {
34+
m_motor.set(0.0);
35+
}
36+
}
37+
```
38+
39+
```c++
40+
class IntakeSubsystem : public frc2::SubsystemBase {
41+
public:
42+
IntakeSubsystem() : m_motor{0} {}
43+
44+
void RunIntake() {
45+
m_motor.Set(0.8);
46+
}
47+
48+
void StopIntake() {
49+
m_motor.Set(0.0);
50+
}
51+
52+
private:
53+
frc::Spark m_motor;
54+
};
55+
```
56+
57+
```python
58+
class IntakeSubsystem(commands2.SubsystemBase):
59+
def __init__(self):
60+
super().__init__()
61+
self.motor = wpilib.Spark(0)
62+
63+
def run_intake(self):
64+
self.motor.set(0.8)
65+
66+
def stop_intake(self):
67+
self.motor.set(0.0)
68+
```
69+
70+
**In Timed Robot programs:**
71+
72+
Motor controllers should be declared as member variables in your ``Robot`` class. Create them in ``robotInit()``, and use them in periodic methods or autonomous/teleop methods.
73+
1174
.. tab-set-code::
1275

1376
```java
14-
Spark spark = new Spark(0); // 0 is the RIO PWM port this is connected to
15-
spark.set(-0.75); // the % output of the motor, between -1 and 1
16-
VictorSP victor = new VictorSP(0); // 0 is the RIO PWM port this is connected to
17-
victor.set(0.6); // the % output of the motor, between -1 and 1
77+
public class Robot extends TimedRobot {
78+
private Spark m_intakeMotor;
79+
private Joystick m_joystick;
80+
81+
@Override
82+
public void robotInit() {
83+
m_intakeMotor = new Spark(0);
84+
m_joystick = new Joystick(0);
85+
}
86+
87+
@Override
88+
public void teleopPeriodic() {
89+
// Run intake when button is pressed
90+
if (m_joystick.getRawButton(1)) {
91+
m_intakeMotor.set(0.8);
92+
} else {
93+
m_intakeMotor.set(0.0);
94+
}
95+
}
96+
}
1897
```
1998

2099
```c++
21-
frc::Spark spark{0}; // 0 is the RIO PWM port this is connected to
22-
spark.Set(-0.75); // the % output of the motor, between -1 and 1
23-
frc::VictorSP victor{0}; // 0 is the RIO PWM port this is connected to
24-
victor.Set(0.6); // the % output of the motor, between -1 and 1
100+
class Robot : public frc::TimedRobot {
101+
public:
102+
Robot() : m_intakeMotor{0}, m_joystick{0} {}
103+
104+
void TeleopPeriodic() override {
105+
// Run intake when button is pressed
106+
if (m_joystick.GetRawButton(1)) {
107+
m_intakeMotor.Set(0.8);
108+
} else {
109+
m_intakeMotor.Set(0.0);
110+
}
111+
}
112+
113+
private:
114+
frc::Spark m_intakeMotor;
115+
frc::Joystick m_joystick;
116+
};
25117
```
26118

27119
```python
28-
spark = wpilib.Spark(0) # 0 is the RIO PWM port this is connected to
29-
spark.set(-0.75) # the % output of the motor, between -1 and 1
30-
victor = wpilib.VictorSP(0) # 0 is the RIO PWM port this is connected to
31-
victor.set(0.6) # the % output of the motor, between -1 and 1
120+
class MyRobot(wpilib.TimedRobot):
121+
def robotInit(self):
122+
self.intake_motor = wpilib.Spark(0)
123+
self.joystick = wpilib.Joystick(0)
124+
125+
def teleopPeriodic(self):
126+
# Run intake when button is pressed
127+
if self.joystick.getRawButton(1):
128+
self.intake_motor.set(0.8)
129+
else:
130+
self.intake_motor.set(0.0)
32131
```
33132

133+
### Common Use Cases
134+
135+
Motor controllers are used throughout the robot for many mechanisms:
136+
137+
- **Intakes**: Spin wheels to collect game pieces
138+
- **Shooters**: Spin flywheels to launch game pieces
139+
- **Conveyors**: Move game pieces through the robot
140+
- **Arms/Elevators**: Raise and lower mechanisms (often with position control)
141+
- **Climbers**: Extend or retract climbing mechanisms
142+
143+
For drivetrain usage, see :doc:`wpi-drive-classes`. For more complete examples, see :doc:`/docs/software/examples-tutorials/wpilib-examples`.
144+
34145
## CAN Motor Controllers
35146

36-
A handful of CAN motor controllers are available through vendors such as CTR Electronics, REV Robotics, and Playing with Fusion. See :doc:`/docs/software/can-devices/third-party-devices`, :doc:`/docs/software/vscode-overview/3rd-party-libraries`, and :doc:`/docs/software/examples-tutorials/third-party-examples` for more information.
147+
CAN motor controllers are available through vendors such as CTR Electronics (Talon FX), REV Robotics (SPARK MAX/FLEX), and others. See :doc:`/docs/software/can-devices/third-party-devices`, :doc:`/docs/software/vscode-overview/3rd-party-libraries`, and :doc:`/docs/software/examples-tutorials/third-party-examples` for more information.

0 commit comments

Comments
 (0)