Skip to content

Basic Programming

Haowei Zheng edited this page Jan 16, 2016 · 12 revisions

By: Haowei Zheng (hw2zheng) ##Loading Sample Code

For basic testing, sample code classes are given out for beginning programmers to learn basic programming. These are useful because they all come fully functional, and can be sent to the robot and used with a few basic adjustments.

##Loading sample code is very simple. First, open Eclipse, and go to File->New->Project

##Then go to the folder on the bottom, which is named "WPILIB Robot Development"

##Expand this and double click "Example Robot C++ Project"

##From this list, you may choose any sample code to load. The best example code for beginners is "Arcade Drive", at the top of the list. Click finish once you have selected it

##Click this button to expand it into programmer mode

##Right-click "ArcadeDrive" on the left. Rename it into anything you wish, such as "Your_Name_Drive"

##After renaming, you can now open the source file. It is under "src", and named "Robot.cpp"

##If there are any build errors, right-click the Project and go to Index-Rebuild

##The Four Functions

There are four different functions in this file, each of which corresponds to a different Driver Station mode. When the robot is being used, there will be four modes to choose from. It is up to the programmers to put the appropriate code in the appropriate functions.

void OperatorControl() is a function, which is activated when Teleoperated mode is enabled by the driver. It is used for the driving portion of the competition.

void Autonomous() is another function, activated when Autonomous is enabled by the driver. It is used at the beginning of the competition. void Test() is used for testing practice code void Practice() is another practice function

##Custom Classes

Instead of making the programmer control each individual action of the robot, FIRST has made it much easier to work with by giving all programmers the WPILIB library, which includes many custom classes and functions which control the robot for you. For example, if you wanted to make a motor spin you'd have to tell the roboRIO exactly where to send exactly how many volts of electricity, as well as when to stop.

However, using WPILIB, I would simply declare an instance of the included Talon class, which is a type of motor controller, and tell the roboRIO that it is connected to Port 0. Before programming, it is necessary to check the port number of your motor controllers to ensure that the roboRIO is controlling the correct motors.

In order to control it, I would simply use the function motor_name.Set(speed), in which motor_name is the name of the motor controller, Set is the function, and speed is the desired speed, between -1 and 1. In this case, I used speed 0.5, which means half speed forward. -1 is full speed backwards, and 1 is full speed forwards. A speed of 0 would stop the motor.

This code would make the motor go full speed forwards, for as long as the Teleoperated mode is activated on the Driver Station. One problem with this code is that there is no way to change the motor speed without changing the code, loading it onto the robot, and waiting for the robot to process the code. This ties into the next section.

##Joystick

Another very important custom class is the Joystick class, which is used to get input from the user and use it to control the robot. There are two main functions that the Joystick class can use, which are GetRawAxis() and GetRawButton().

GetRawAxis() is a function which returns the value of an axis. An axis is a horizontal or vertical line, such as those found on a joystick. For example, on the joystick of a video game controller, there would be two axes: X and Y. Moving the joystick would change the position of the stick in relation to the axis. Each axis is numbered, and in order to implement GetRawAxis() you would need to put the axis number in the parentheses.

GetRawAxis() returns the position of the joystick, which ranges from -1 to 1. For example, if the joystick was pressed all the way left then GetRawAxis(0) [0 is the number of the x axis] would return -1, because it's all the way in one direction. All the way left would return 1, because it is all the way in the other direction. However, GetRawAxis(1) [1 is the number of the Y axis] would return 0, because the joystick has not moved up/down the Y-axis.

This can be used to control the motor dynamically, as the motor could get faster/slower as you move the stick up and down the axis. In this example code, the motor would change speeds as you moved the stick up and down the Y-axis.

Another function is GetRawButton(). Since a button can only be pressed or not pressed, the function GetRawButton() can only return two values: true or false, in which true is pressed and false is not pressed. Each button is numbered as well, so one must put the button number in parameters.

Implementation is shown below. The motor will move if the button is pressed.

##List of Classes

This isn't the end! There are so many more classes to use, including Ultrasonic Sensor and many others. They can be found in the WPILIB class declaration, which can be found by highlighting WPILIB.h and hitting F3.