Robot C2 - Using Pseudo Code and Flowcharts For RobotC Programming For LEGO
Robot C2 - Using Pseudo Code and Flowcharts For RobotC Programming For LEGO
Reference
task main()
{
while ( touch sensor is not pressed )
{
Robot runs forward
if (sonar detects object < 20 cm away)
{
Robot stops
Robot turns right
}
}
}
Descriptions
There are no actual motor
commands in this section of
the code, but the pseudocode
suggests where the commands
belong and what they need
to accomplish.
This pseudocode example includes elements of both programming language, and the English
language. Curly braces are used as a visual aid for where portions of code need to be placed
when they are finally written out in full and proper syntax.
Carnegie Mellon Robotics Academy / For use with LEGO MINDSTORMS Education NXT software and base set 9797
ROBOTC
Reference
Start/Stop
Action
Decision
Start
Is the Touch
Sensor
pressed?
No
Run both
motors forward
Yes
Stop
motors
End
Carnegie Mellon Robotics Academy / For use with LEGO MINDSTORMS Education NXT software and base set 9797
A module of a program is a function or separate file that is called from the main program. In a wellwritten modular program, one module can be updated without requiring changes in the other parts of
the program.
Consider the print dialog box that appears from most Windows applications, as shown below:
When a new printer is added to the computer, the programs that use the printer do not have to be rewritten. Instead the features of the new printer are automatically available to all programs that access
the dialog box. This ease of updating the program functionality is due to the modularity of the
program. The print drivers are written separately than the calling program. To achieve this modularity
care must be taken to ensure that there is proper information flow between the calling program and
the module (e.g. printer features and data to be printed).
When should one write a separate module (Matlab function or script file)?
When there are well defined separate functions of the program, one should separate the
modules of the program accordingly. For example, in a dynamic simulation, one module should
could calculate the dynamics of the system, another module could perform the plotting for an
animation, and another module could define the parameters of the model.
Whenever one finds repeated code, it probably makes sense to write this code in a separate
function. Otherwise, when a change is required in the code (and it always is), one needs to
make the exact change in numerous locations that, which is in itself an error prone process.
In general Matlab functions are considered better code than a script file. With a function, the input and
output arguments are clearly defined, so it can easily be discerned what variables are modified within
the function. Moreover, the same function could be reused to operate on variables of different names.
Generalization
A generalizable piece of code is preferred over a code that can only be used for a unique application.
For example, code which fixes the number of links in a robot arm is less useful than code where that
number is variable and can be used for a wide range of robot arms. Below are some examples:
Never hardcode values into program. Instead define variables for such parameters. For
example, by defining the number of links on a robot as a variable, it can easily be modified in
a single location. As a link is added or removed from the robot. It is best to locate the definition
of all parameters in a single location, so one can view all parameters at once (instead of
having surprises hiding deep within the code)
Use arrays instead of sequentially numbered variables. If you find yourself defining variables
such as: link_1 and link_2, replace this with an array link(1) and link(2). There are many
advantages to using arrays:
o The number of elements can be easily adjusted
o One can write loops to perform repeated operations, rather than rewriting code with
different variable names
o Matlab and C-coding (for example) have many matrix operations that take advantage of
arrays.
Good Example
Bad Example
% GOOD CODE
% BAD CODE
disp(link_length_1);
disp(link_length_2);
disp(link_length_3);
disp(link_length_4);
The purpose of good documentation is so that the author of the code as well as others can easily
understand the program and avoid mistakes. If one starts with good habits, it only takes a small
amount of effort to use meaningful variables names and document the code while one is writing it.
However, it can be very difficult to improve documentation and variable notation after the code has
been written. Some pointers are below:
Do not shy away from long or medium length variable names. It is much better to be
unambiguous in a variable name. Some good variable names are:
o spring_length_initial
% initial spring length
o spring_length_cur
% current spring length
o spring_length_ar
% array of spring lengths
Do not use the same variable name for multiple meanings.
Be consistent in how one names variables. For example:
o Add the suffix _ar to variable names that are arrays
o Add the prefix d to designate derivatives
o Add the prefix n to indicate number of
Document the code while you are writing it. The documentation does not have to be length, but
rather timely!
Always specify the units of numbers, where they are being entered.
Good Example
Bad Example
% GOOD DOCUMENTATION
% BAD DOCUMENTATION