FIRST Robotics Competition Documents

Showing results for 
Search instead for 
Did you mean: 

Basic Motor Control Tutorial




1.1 Introduction


1.2 Getting Started


1.3 Connecting the FRC Hardware


1.4 Creating the LabVIEW Project


1.5 Programming Motor Control in LabVIEW


1.6 Adding a Joystick


1.7 Conclusion




This tutorial demonstrates how to program the roboRIO for controlling a motor. It walks through the programming necessary to control a motor with a joystick, as well as covering basics such as setting up the FRC Control System, and setting up an FRC Robot Project.


Getting Started


If you haven’t already, go ahead and install the software that came in your kit. This will install a FRC specific version of LabVIEW along with the WPI Robotics Library. You will also need to have your Driver Station firmware up to date. You can refer to The Training Materials And Resources page for information and instruction.


The following is a list of the hardware we are going to be using in this tutorial:


  • NI roboRIO
  • Power supply
  • PWM Motor
  • USB Joystick


Connecting the Hardware


Every motor type will be different so we will not cover the specifics of connecting the roboRIO or the motors here. However, the LabVIEW examples of motor control (LabVIEW>> Help>> Find Examples>> FRC Robotics>> roboRIO>> Robot and Motor) often have specifics on how to connect motors. In addition, WPI has several resources that discuss the various connections needed for motor control. Wiring the 2017 FRC Control System, 2017 FRC Control System Hardware Overview.


Creating the LabVIEW Project


Now that the hardware is configured the next step is to write a software VI to control the motor. Open up LabVIEW FRC and create a new FRC roboRIO Project. Name the project, set the save path, and enter the team number (Which helps determine the IP address of the target) then click Finish.


create new.png


In the project window, right-click on Target (roboRIO – 7777 -FRC-local and select New»VI 


     new vi.png


Save the new VI as Simple Motor Control. Notice that LabVIEW places the new VI inside the roboRIO target tree, which means the VI will run on the roboRIO.




Programming Motor Control in LabVIEW


Double-click the Simple Motor Control VI to open it. Next, go to the block diagram by selecting Window»Show Block Diagram (or Ctrl+E). Now we will begin the actual coding required to get the motor moving. First create a while loop by right-clicking anywhere in the white space on the block diagram and selecting Programming»Structures»While Loop (Or Quick Drop a while loop). Click, drag, and release on the block diagram to specify the size of the while loop.


The while loop allows all code inside of it to run continuously until a condition or set of conditions are met. Right-click the conditional terminal in the bottom right corner of the while loop and select create control. Notice that a Stop button appears on the front panel.


while loop.png


Now right-click anywhere in the white space of the block diagram and navigate to the WPI Robotics Library»RobotDrive»Advanced»Motor Control palette and place the WPI_MotorControlOpen VI on the block diagram to the left of the while loop. Select your motor type from the dropdown.

Next, right-click on the PWM Channel input of the Open VI and create a constant. Set the value to PWM 1. This selects the PWM channel that our motor controller is connected to. Note that some motors that use CAN communication may not be included in the WPI VI. In these cases, the motor is likely supported by a third party motor driver which can be obtained from the motor manufacturer.  




Now place the Set Output VI from WPI Robotics Library»RobotDrive»Advanced»Motor Control in the middle of the while loop. Wire the MotorControlDevRef and error terminals of the Open Jaguar VI to the inputs of the Set Output VI. Right-click the output input of the Set Output VI and select Create»Control. You may need to select a different motor from the drop down depending on your specific motor.


This creates a control on the front panel that allows us to specify the speed of the motor. The value of this control can be between 1 and -1. Because we have the Set Output VI in a while loop, we can change the motor speed from the front panel while the motor is running. This VI will continue to run until you push the stop button.


do work.png


Finally, place the Close VI from WPI Robotics Library»RobotDrive»Advanced»Motor Control palette to the right of the while loop and then wire the reference and error outputs of the Set Output VI to the inputs of the Close VI.




Next place the Start from WPI Robotics Library»Driver Station outside of the while loop. The Generate and Wait for from Programming>>Synchronization>>Occurrences the should be added to allow the driver communication to run concurrently with the other loop. Then add a Boolean Or to stop the VI from either the stop button or an error from the motor code. Wire the stop condition to a Flat Sequence Structure (Programming>>Structures) and Stop VI from Programming>>Application Control.


These VIs are necessary to establish a connection with the roboRIO when the program starts and to close that connection when the program stops.




Now go to the front panel, save the VI, and click the run button. The VI will now deploy to the roboRIO. Once the program has finished downloading, try changing the value of the speed control from 0 to 0.1.


front pan.png


The motor should now start moving slowly.


Keep increasing this number up to 1, at which point the motor will spin at full speed. Notice that inputting negative values for speed will spin the motor in the opposite direction. Press the stop button on the front panel to stop the program.


Adding a Joystick


In order to control the motor with a joystick, first plug the joystick into a USB port 1 on the driver station and set it to Joystick 1 in the Driver Station. Then go back to the block diagram and place the joystick Open VI located at WPI Robotics Library»Driver Station»Joystick to the left of the while loop. Right-click on the joystick device input and create a constant. Select a value of USB 1.


Now place the Get Values VI from WPI Robotics Library»Driver Station»Joystick inside the while loop and connect the JoystickDevRef output of the joystick Open VI to the reference input of the Get Axis VI. Then add an Index Array VI from the Programming>>Array Palette, and wire the Axes output of the Get Values VI to the Array input of the Array Index VI. Right Click the Index 0 input terminal of the Index Array VI and create a constant and type 2 as the constant. The Joystick Get Values VI returns an array of values for the current state of the joystick, then we index that array and select the number in the array that corresponds with the axis that we are interested in. On this joystick, the second element in the array is axis three.


If you are using a different joystick, you may find that your joystick has different mappings for the axis.


Delete the speed control made earlier by clicking the control and hitting the delete key on the keyboard.




Remove the broken wires by going to Edit»Remove Broken Wires (or Crtl+B). Next, connect the value output from the index array to the output input of the PWM Set Speed VI. Finally, close the joystick reference with a Close VI.


mod finish.png


Click the run button to download the VI to the roboRIO. Once the code downloads, move the joystick forwards and backwards to move the motor with varying speed and direction. Press the stop button when finished.




Congratulations, now you’re up and running with a simple motor example. Explore the rest of the Training Material and Resources page to learn about acquiring data from sensors, taking and processi... images from the camera, and integrating the data you read from both the sensors and the camera into a project to move motors. Have fun programming and use the skills and techniques you learn in these tutorials to build a better robot!