Concept Explanation: Phases of a program

Printer-friendly version

 

With all programming languages, there is an inherent order that certain instructions should be performed in. This behavior dictates were certain types of instructions are placed in the program. By using a template, we can streamline the creation process and build a uniform framework that all of our programs follow. The first step is to identify generic regions based on the actions taken by the NXT. Let's break apart our sample code and see how it is organized:

Phases

In RobotC, the first general region boundaries revolve around "task main {}". The boundaries are placed at the start (line 6) and the end (line 22) of "task main {}". Anything that happens before "task main{}" starts is considered to be in the "Pre-Initialization" Phase. This phase is where we configure everything that will be used in the program. At the other end of "task main{}" is the shutdown phase. This where the attached devices are turned off and the program closes. RobotC handles all of this for us once we reach the closing curly brace (line 22) of "task main{}".

Everything inside of "task main{}" is considered to be a part of the "Main Task Phase". Inside of this phase, there are two sub regions that occur at the start and end of the task. At the start of the task is the Initialization phase, not to be confused with Pre-Initialization phase that occurs outside of "task main {}". This phase is where we setup the initial state of the NXT. Most of our Variables will be created here.

At the end of the Main Task Phase is the Park Phase. This is where we stop any work being done by the NXT and report any relevant data. Not all programs require a park phase. The shutdown phase will turn off motors if they are currently running. We mainly use the park phase to report results of the work done.

When writing programs using this template, you should remember that it is only a guideline. Depending on the desired behavior, you may come up with a different template. For now, use the framework provided, but don't let it constrain your creativity. The great thing about Programming is that every problem has multiple solutions.