Skip to content

Commit 33f4c31

Browse files
authored
Add REV CANSPARKMAX instructions to ZTR (#2152)
* Add REV CANSPARKMAX instructions to ZTR * Remove reference to LiveWIndow, no longer in example
1 parent cc7ed43 commit 33f4c31

File tree

1 file changed

+70
-3
lines changed

1 file changed

+70
-3
lines changed

source/docs/zero-to-robot/step-4/creating-benchtop-test-program-cpp-java.rst

Lines changed: 70 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ Creating your Benchtop Test Program (C++/Java)
33

44
Once everything is installed, we're ready to create a robot program. WPILib comes with several templates for robot programs. Use of these templates is highly recommended for new users; however, advanced users are free to write their own robot code from scratch. This article walks through creating a project from one of the provided examples which has some code already written to drive a basic robot.
55

6-
.. important:: This guide includes code examples that involve vendor hardware for the convenience of the user. In this document, PWM refers to the motor controller included in the KOP. The CTRE tab references the Talon FX motor controller (Falcon 500 motor), but usage is similar for TalonSRX and VictorSPX. There is an assumption that the user has already installed the required :doc:`vendordeps </docs/software/vscode-overview/3rd-party-libraries>` and configured the device(s) (update firmware, assign CAN IDs, etc) according to the manufacturer documentation (`CTRE <https://docs.ctr-electronics.com/>`__). REV examples will be coming soon.
6+
.. important:: This guide includes code examples that involve vendor hardware for the convenience of the user. In this document, PWM refers to the motor controller included in the KOP. The CTRE tab references the Talon FX motor controller (Falcon 500 motor), but usage is similar for TalonSRX and VictorSPX. The REV tab references the CAN SPARK MAX controlling a brushless motor, but it's similar for brushed motor. There is an assumption that the user has already installed the required :doc:`vendordeps </docs/software/vscode-overview/3rd-party-libraries>` and configured the device(s) (update firmware, assign CAN IDs, etc) according to the manufacturer documentation (`CTRE <https://docs.ctr-electronics.com/>`__ `REV <https://docs.revrobotics.com/sparkmax/gs-sm>`__).
77

88
Creating a New WPILib Project
99
-----------------------------
@@ -108,7 +108,35 @@ Imports/Includes
108108
#include <frc/drive/DifferentialDrive.h>
109109
#include <ctre/phoenix/motorcontrol/can/WPI_TalonFX.h>
110110
111-
Our code needs to reference the components of WPILib that are used. In C++ this is accomplished using ``#include`` statements; in Java it is done with ``import`` statements. The program references classes for ``Joystick`` (for driving), ``PWMSparkMax`` / ``WPI_TalonFX`` (for controlling motors), ``TimedRobot`` (the base class used for the example), ``Timer`` (used for autonomous), ``DifferentialDrive`` (for connecting the joystick control to the motors), and ``LiveWindow`` (C++ only).
111+
.. group-tab:: REV
112+
113+
.. tabs::
114+
115+
.. group-tab:: Java
116+
117+
.. code-block:: java
118+
119+
import com.revrobotics.CANSparkMax;
120+
import com.revrobotics.CANSparkMaxLowLevel.MotorType;
121+
122+
import edu.wpi.first.wpilibj.TimedRobot;
123+
import edu.wpi.first.wpilibj.Timer;
124+
import edu.wpi.first.wpilibj.XboxController;
125+
import edu.wpi.first.wpilibj.drive.DifferentialDrive;
126+
127+
.. group-tab:: C++
128+
129+
.. code-block:: cpp
130+
131+
#include <frc/TimedRobot.h>
132+
#include <frc/Timer.h>
133+
#include <frc/XboxController.h>
134+
#include <frc/drive/DifferentialDrive.h>
135+
#include <frc/motorcontrol/PWMSparkMax.h>
136+
137+
#include <rev/CANSparkMax.h>
138+
139+
Our code needs to reference the components of WPILib that are used. In C++ this is accomplished using ``#include`` statements; in Java it is done with ``import`` statements. The program references classes for ``Joystick`` (for driving), ``PWMSparkMax`` / ``WPI_TalonFX`` / ``CANSparkMax (for controlling motors), ``TimedRobot`` (the base class used for the example), ``Timer`` (used for autonomous), and ``DifferentialDrive`` (for connecting the joystick control to the motors).
112140

113141
Defining the variables for our sample robot
114142
-------------------------------------------
@@ -156,7 +184,7 @@ Defining the variables for our sample robot
156184
private final Joystick m_stick = new Joystick(0);
157185
private final Timer m_timer = new Timer();
158186
159-
.. group-tab:: C++ (Header)
187+
.. group-tab:: C++
160188
161189
.. code-block:: cpp
162190
@@ -181,6 +209,45 @@ Defining the variables for our sample robot
181209
frc::Joystick m_stick{0};
182210
frc::Timer m_timer;
183211
212+
.. group-tab:: REV
213+
214+
.. tabs::
215+
216+
.. group-tab:: Java
217+
218+
.. code-block:: java
219+
220+
public class Robot extends TimedRobot {
221+
private final CANSparkMax m_leftDrive = new CANSparkMax(1, MotorType.kBrushless);
222+
private final CANSparkMax m_rightDrive = new CANSparkMax(2, MotorType.kBrushless);
223+
private final DifferentialDrive m_robotDrive = new DifferentialDrive(m_leftDrive, m_rightDrive);
224+
private final XboxController m_controller = new XboxController(0);
225+
private final Timer m_timer = new Timer();
226+
227+
.. group-tab:: C++
228+
229+
.. code-block:: cpp
230+
231+
Robot() {
232+
// We need to invert one side of the drivetrain so that positive voltages
233+
// result in both sides moving forward. Depending on how your robot's
234+
// gearbox is constructed, you might have to invert the left side instead.
235+
m_right.SetInverted(true);
236+
m_robotDrive.SetExpiration(100_ms);
237+
m_timer.Start();
238+
}
239+
240+
.. code-block:: cpp
241+
242+
private:
243+
// Robot drive system
244+
rev::CANSparkMax m_left{1, rev::CANSparkMax::MotorType::kBrushless};
245+
rev::CANSparkMax m_right{2, rev::CANSparkMax::MotorType::kBrushless};
246+
frc::DifferentialDrive m_robotDrive{m_left, m_right};
247+
248+
frc::XboxController m_controller{0};
249+
frc::Timer m_timer;
250+
184251
The sample robot in our examples will have a joystick on USB port 0 for arcade drive and two motors on PWM ports 0 and 1 (Vendor examples use CAN with IDs 1 and 2). Here we create objects of type DifferentialDrive (m_robotDrive), Joystick (m_stick) and Timer (m_timer). This section of the code does three things:
185252
186253
1. Defines the variables as members of our Robot class.

0 commit comments

Comments
 (0)