Introduction
Robots, from the Mars Rover to the Roomba, have one thing in common, a CPU. These machines can perform a wide range of automated tasks, but they all are dependent on programmers—robots are especially configured to work the way they do. This is a result of a complex system of inputs and outputs. A robot’s onboard computer constantly gets input from sources such as sensors and motors, and the computer analyzes this information to determine what the robot needs to do. This input can be information such as position, time, and a host of other possibilities. These variables can in turn trigger motors, lights, or other onboard components.
Many robots are programmed from scratch, and others have a preexisting framework. To give you a basic example of robot programming, we will the latter, and show you how one can rearrange preexisting code to make a robot perform customized tasks. This tutorial is based on the FIRST Robotics Competition’s default code. This code used and altered by thousands of high school teams across the world. Each team receives the same framework (which is described in this article) and their programmers edit the code to achieve their team’s goals. IHOT team 1414’s lead programmer, Adrien Pellerin, wrote this article.
The Controller
This introduction to programming is an overview of the necessities of programming the FRC (FIRST Robotics Competition) Robot Controller.
By the end of this article you should have a better understanding of what programming the robot takes and how it is done. The Robot Controller, manufactured by Innovation First (www.innovationfirst.com), is the heart of the robot. It is the machine that executes your code. It gets information from the rest of the robot through its many analog and digital inputs, and it sends information back through its many outputs. Basically there are 16 each of Analog Inputs, Digital In/Outputs, and Motor (PWM) Outputs. Digital inputs can be used for various sensors like limit switches and light sensors. Being digital they send a ‘1’ (true, or high) or a ‘0’ (false, or low) back to the controller depending on how they’re being used. Analog Inputs can be used for sensors like potentiometers where the return value can’t just be true or false. Finally, motor outputs are where the motors are plugged in to so they can be told how fast to turn.
The Code
Since 2004, FRC robots have been programmed in the C language, or if teams wish, Assembly language. 
Before 2004 PBASIC was used. The code is compiled with Microchip’s C18 compiler. FIRST provides this along with Microchip’s MPLAB IDE to FRC teams.
The structure of the Default Code (provided to FRC teams) can be seen in MPLAB IDE. The various files in the project are categorized under “Source Files”, “Header Files”, “Library Files”, or “Linker Scripts”. Source files are those with actual code which will be executed. Header files contain variable declarations and definitions that are used throughout the program. Library files cannot be read because they have already been compiled. They contain more information like the header files. Linker Scripts tell the compiler how to link all of these files together to make one seamless program. The most important file to the programmer is ‘user_routines.c’. It contains a function called ‘Default_Routine()’. This function runs every 26.2 ms on the robot controller. This means that it will run approximately 38 times per second. The reason for this time is the controller can only get new inputs and put new outputs every 26.2 ms.

Modifying the Default Code
Motors: pwmx (x = 01 – 16)
Digital inputs: rc_dig_inx (x = 01 – 16)
Analog inputs: rc_ana_inx (x = 01 – 16)
Joysticks:
• p1_x (X axis of joystick 1)
• p3_y (Y axis of joystick 3)
• p2_sw_trig (Trigger of joystick 2 [0 = up, 1 = pressed])
• p4_sw_top (Top button of joystick 4)
Using these variables, you can edit ‘Default_Routine()’ to suit your robot. All variables are stored as “unsigned char”, so they are one byte each. This means that they can have values of 0-255 (digital variables only use 1 and 0). In pwm’s, 0 is full reverse, 255 is full forward, and 127 is neutral, or no movement. On joystick axes, 0 is full back, 255 is full forward, and 127 is middle. This means that you can easily map a pwm to a joystick with this line:
pwm01 = p1_y; //Motor 1 gets the speed of Y axis of Joystick 1
Here are some more code samples:
if(rc_dig_in05 == 0) //If Digital input 5 is giving '0'
{
pwm01 = pwm02 = ((p1_y + p2_y) / 2); //motors 1 & 2 get the average of p1_y and p2_y
}
int timer = 0; //Times processor cycles
/*
timer will count how many times the code has run.
The code runs every 26.2 ms. 38 times per second.
This can be used to time certain events
*/void Default_Routine(void)
{
if(timer < 38) //For the first second...
{
pwm01 = pwm02 = 255; //Both motors at full speed
}
else if(timer < 114) //For the next 2 seconds...
{
pwm01 = 0; //motor one full reverse
pwm02 = 255; //motor two full forward
}
else if(timer < 133) //For the next 1/2 secoond...
{
pwm01 = pwm02 = 0; //Both motors full reverse
}
else
{
pwm01 = pwm02 = 127; //When finished, stop both motors
}
}
if(p1_y >= 127) //If p1_y is pushed forward or at rest...
{
pwm01 = ((((p1_y - 127) * (p1_y - 127)) / 127) + 127); //Curve the speed
}
else //If p1_y is pulled back
{
pwm01 = (127 - (((127 - p1_y) * (127 - p1_y)) / 127)); //Curve the speed
}
Cite
Our Experiments & Research
If you have used any of this information or any of these images
please go ahead and cite them in your bibliography. For your convenience,
this is what the citation would look like in MLA format:
Last Name, First Name. “An Introduction to Robot Programming.” February 23, 2006 Mad Physics. dd mmm. yyyy †
<http://www.madphysics.com/robotics/an_introduction_to_robot_programming.htm">
We are glad to share our knowledge with you as long as you cite all of our info, and contact us before you use anything for non-educational purposes (commercial, etc.).
† In the bibliography you must insert the day you visited the site (this is relevant because the site could change at some point), therefore, in the bibliography above replace dd with the day you visited, mmm with the abreviated month, and yyyy with the year (ex: dd mmm. yyyy becomes 23 Dec. 2004).