Default code (2004)
From FIRSTwiki
The default code provides pre-programmed functionality to the Robot Controller. This can allow teams to spend more time on fabricating their robot, so long as they don't need more complicated control. But the default code also provides the starting point for programming the FRC, and it is necessary to understand the code before you decide to change it.
InnovationFIRST provides a detailed description of the pre-programmed functionality of the default code (see Default Code Reference Guide). This article does not strive to repeat that information here. Rather, the article will attempt to explain the important features of the default code, and, to a degree, how a team might go about customizing the code for its own needs (for more on this, see programming the Robot Controller).
The default code consists of 12 header and c program files, only 3 of which IFI recommends the user edit (although you are certainly allowed to create your own files). Generally speaking, it is wise to stay within these guidelines. Only edit files like main.c or printf_lib.c if you really know what you are doing (and only edit files like ifi_startup.c if you really, really know what you are doing). Consider yourself warned.
The default code comes with the following files:
| C files (.c extension) | Header files (.h extension) |
| ifi_startup.c | ifi_aliases.h |
| ifi_utilities.c | ifi_default.h |
| main.c | ifi_picdefs.h |
| printf_lib.c | ifi_utilities.h |
| user_routines.c | printf_lib.h |
| user_routines_fast.c | user_routines.h |
ifi_aliases.h provides some useful aliases, and is worth looking at. printf_lib.c and printf_lib.h provide a stripped down printf function, and are useful for debugging purposes. Other than that, you should only need to be concerned with user_routines.c, user_routines.h, and user_routines_fast.c. This is the heart of the program -- the other files do bookkeeping, and set things up for you.
Before explaining these individual files, it is helpful to examine the control flow of the program. The program starts out by doing two initialization routines, one from IFI and a custom one for the user. It then enters into the main loop (which happens to be infinite). Basically, the loop involves reading data, processing it, and then outputting data -- and then starting all over again. In the 2004 Programming Reference Guide, IFI provides a visual representation of this.
Contents |
user_routines.h
This is a header file, which is an appropriate place for macro declarations, including aliases and constants, typedef declarations, and function prototypes. Nothing eventful happens in this file in the default code, though it is a useful file to edit.
user_routines.c
This is a c file, which is the heart of the normal operations. It contains the default mappings of inputs to outputs on the RC. It defines useful functions, such as Limit_Mix () which keeps the joystick control in the correct range. This is where the action begins to happen.
User_Initialization
This function starts out by setting which I/O pins will be used as inputs and which will be used as outputs. It initializes the output, and sets the PWM values to neutral (127). It then tells the processor to take control of PWMs 13-16 (this can be changed to provide more customization). The function then intializes the serial communications and tells the master processor that it is ready.
Process_Data_From_Master_uP
This function is your connection to the main control loop. It runs every 26.2ms. The function gets fresh data, then sends it off to Default_Routine, where the main action of the default code takes place. It then does some important behind-the-scenes work that shouldn't be tampered with.
Default Routine
And now we get to the meat of the program. First, the function maps analog inputs to PWM outputs. So if you had a potentiometer connected to p4_wheel, for instance, it would control pwm12, which might be connected to a speed controller and therefore control a motor. By default, port 1 is assumed to have a joystick connected to it, and pwm13-16 are assumed to control the drive motors. The function then maps the joystick buttons to specific relay outputs. It causes certain PWM outputs to be limited by limit switches. And finally, it controls the robot feedback LEDs on the OI.
user_routines_fast.c
This is another C file, which is the heart of the autonomous operations. It also contains a place to put code that must be executed every loop, hence the name user_routines_fast. It includes a section to define custom variables.
Interrupts
The file begins by providing the framework from which to build interrupts. These are not used by default, however, and require special care and advanced knowledge of how the processor works.
User_Autonomous_Code
This is the function that executes when the robot is in autonomous mode. It starts by initializing all PWMS and relays. Then, it sets up a loop for you to add the autonomous code. There is no autonomous action by default.
Process_Data_From_Local_IO
There is nothing at all in this function in the default code. It is useful for code that must execute every loop and does not need to have fresh data every time through.
See also: Programming, Programming the Robot Controller
Resources
- Robot Controller Documentation from InnovationFIRST (IFI)
- 2004 Programming Reference Guide (pdf) from IFI
- 2004 Default Software Reference Guide (pdf) from IFI
- 2007 FRC Default/User and Master code (zip) from IFI (the old 2004 code no longer available)

