Graphical Robot C
Graphical Robot C
3 | Page
Graphical RobotC Tutorial Packet Storming Robots
Computational Thinking and Engineering for Gr. 4 to 12
This tutorial is written mainly to be used in our Roboclub. If you are a completely beginner, discussion that you
receive in class will be very helpful.
This tutorial is laid out in the following approach to fit our roboclub model:
Ch. 1 Get to know the working environment. Just in high level tour.
Ch. 2 Learn best practice divide and conquer
Learn to be resourceful
- use the Help feature
- use the debugger
This should help you to become more self-sufficient.
Ch. 3 Learn simple flowchart design. Should start this before programming, even with young
folks like Gr. 4 to 6.
You may skip this chapter if you are already familiar with simple flowchart design.
Ch. 4-9 More indepth explanation in programming concepts by following the program menu side
bar.
Organized by motors, basic sensors and motor encoder with basic gear math. Since there
is no support to incorporate variables, you will have to work out the math work on a piece
of paper first. This is actually a good thing to reinforce mechanical gear math for the
younger folks.
Ch. 10 Others and More in Debugging.
Ch. 11 Advanced challenge.
Chapter 1 should read the entirety. This is easy to read on your own.
Chapter 2 & 3 meant to help you become a resourceful self-learner. Assistance from discussion in class
will definitely be important if you start out as a beginner. Depending on your level,
instructor may skip these 2 chapters, and go right to basic motors operation. If you are
experienced and a mature learner, you should cover these two chapters first.
Chapter 4 to 7 cover basics as well as more complex work. All more complex work is marked with (*).
Instructor may choose to skip (*) depending on your level.
Chapter 8 to 9 jump right into more complex structure, as it assumes you have got much practice from
chapter 4 and 7. At Roboclub, instructor will extract partial chapter based on your level.
4 | Page
Graphical RobotC Tutorial Packet Storming Robots
Computational Thinking and Engineering for Gr. 4 to 12
Chapter 10 This is an invaluable skill. Debugging skill will be continuously exercised in every single
project. You will exercise debugging throughout the entire project process. This is
documented here as your reference.
Chapter 11 Advanced Challenge to evaluate your level of understanding by solving various problems.
Note:
RobotC/CMU Robotics Academy has release a curriculum along using their Virtual World Software
http://robotvirtualworlds.com/. This is an awesome tool for kids to learn about robotics programming without
the cost of a physical kit!
5 | Page
Graphical RobotC Tutorial Packet Storming Robots
Computational Thinking and Engineering for Gr. 4 to 12
Select proper variable names for the motors and sensors ports. Instructors will discuss what rules you should
go by.
6 | Page
Graphical RobotC Tutorial Packet Storming Robots
Computational Thinking and Engineering for Gr. 4 to 12
b) Firmware Download
First time user, you should always download the firmware first. This must be done at least once before you the
RobotC compiler works. You should not need to do this again after that. After you download a new version,
you should redo this firmware download as well.
c) Save File
Save your file often! Even when you are still editing your file, you should have the habit to save your file.
d Compile Program
Compiling your program is a must before you can download it to your robot. It is primarily used for programs that
translate your code. Strictly speaking, a compiler will translate your code to a lower level language that the robot will
eventually understands.
e) Download to Robot
7 | Page
Graphical RobotC Tutorial Packet Storming Robots
Computational Thinking and Engineering for Gr. 4 to 12
Motor Functions
Timer functions.
Display Functions
Each item will be broken in further details in the following sections. The Program Flow is the most extensive
one. This tutorial will skip:
the Line Tracking as it encapsulates line tracking which will be shown as part of the programming
sample instead.
The Remote controller applies only the joystick controller.
8 | Page
Graphical RobotC Tutorial Packet Storming Robots
Computational Thinking and Engineering for Gr. 4 to 12
Most parameters required for other icons are rather self-explanatory. The one that you will find this Help Viewer
particularly useful is the Motor Commands. Go ahead to explore to see the available icons and their
parameters.
Besides the samples provided along with this tutorial, RobotC also provides a rich
array of samples.
9 | Page
Graphical RobotC Tutorial Packet Storming Robots
Computational Thinking and Engineering for Gr. 4 to 12
Define the objective What is the main goal you are trying to achieve?
Analyze the logic What is/are required? List steps to follow.
Design Put logic in symbols (via flowchart). Hand check the logic
Programming Follow the flowchart.
Execute Observe
Troubleshooting Test to maximize stability. Debugging for errors
a meaningless comment: turn motor A for 50% power level for 2 seconds
a meaningful comment : turn around for about 360 degrees
To trouble-shoot, you need to pretend you were the robot. Then, you STRICTLY follow each of the commands
in your program, but NOT WHAT YOU THINK it should be doing.
Remember: the robot cannot read your mind. It only does EXACTLY what the program tells it to do, not what
the programmer wants it to do. So, if your program tells it to jump off a cliff to its demise, it will do just that.
10 | Page
Graphical RobotC Tutorial Packet Storming Robots
Computational Thinking and Engineering for Gr. 4 to 12
- Is a programming tool to help you convert algorithms into computer programs which robot will
understand.
- Consists of symbols which graphically depict the logical steps to carry out a task.
- Shows steps and their interconnections.
- Consists of symbols which represent of a set of behaviors such as a sequence of movements or actions.
- Shows building blocks in relation to its functions.
Icons that you should know for the samples used in this chapter, well use sound/music and timer to show how
to set up basic control structure samples.
Now, just for your future reference, you may hear about Pseudocode. This is English-like phrases with Visual
basic terms to outline the logic flow as well. For more advanced samples in the later chapters, high level design
will use flowcharts, but low level design may use Psuedocode.
Main task : Get ready to get on the school bus to go to school in the morning.
Note: Main task (the complex behavior) is divided into sub-tasks (simple behaviors). Each task can potentially
include many more steps (basic behavior) in order to complete the main task.
11 | Page
Graphical RobotC Tutorial Packet Storming Robots
Computational Thinking and Engineering for Gr. 4 to 12
12 | Page
Graphical RobotC Tutorial Packet Storming Robots
Computational Thinking and Engineering for Gr. 4 to 12
An Expression :
- an expression is like a building block. A set of them will compose a complete building.
- In your case, combination of expressions will compllete your program. This program can be downloaded
into the robot and run/execute.
- It may be a combination of symbols and programming terms that represents functions or value
Boolean Expression :
- an expression which provides a true or false value
- e.g. if ( traffic light turns red ) this is the boolean expression
- I stop the car
- else
- I continue driving
Boolean Operators :
- <, >, <=, >=, ==, !=
- == : is it equal to. Beware:
- E.g. x==y means is x the same as y
- x = y means put the value of y into x
In this chapter, we will use the 4 timers T1, T2, T3, and T4. We wont use sensors or motors yet because we
want to draw your attention to focus on the control structures.
Play a sound.
PlayTone at a frequency
with duration In 10 play a tone at a frequency of 800 for 150
MsecTicks milliseconds
13 | Page
Graphical RobotC Tutorial Packet Storming Robots
Computational Thinking and Engineering for Gr. 4 to 12
Step 3) Hit F7 to compile to see there is any syntax error. Then hit F5 or click on
14 | Page
Graphical RobotC Tutorial Packet Storming Robots
Computational Thinking and Engineering for Gr. 4 to 12
If T1 <= 5 seconds
Play Upward Tones
Wait for 1 second
else
Play Downward Tones
Wait for 2 seconds
Step 2) Write your flowchart for this program Step 3) Program the task.
15 | Page
Graphical RobotC Tutorial Packet Storming Robots
Computational Thinking and Engineering for Gr. 4 to 12
Step 2) Write your flowchart for this program. Step 3) Program the task.
16 | Page
Graphical RobotC Tutorial Packet Storming Robots
Computational Thinking and Engineering for Gr. 4 to 12
17 | Page
Graphical RobotC Tutorial Packet Storming Robots
Computational Thinking and Engineering for Gr. 4 to 12
Loop Examples 1
Step 1) Define your goal : Repeat playing sound Beep Beep 5 times.
Step 2) Write your flowchart for this program. Step 3) Write your program code.
Unfortunately, it does not seem to play the
sound. Fix it.
18 | Page
Graphical RobotC Tutorial Packet Storming Robots
Computational Thinking and Engineering for Gr. 4 to 12
Loop Example 2
Step 1) Define the Task: Play Upward Tone, wait for 1 second. Then, play Downward tone, wait for 1 second.
Repeat this until T1 timer is greater than 5 seconds.
19 | Page
Graphical RobotC Tutorial Packet Storming Robots
Computational Thinking and Engineering for Gr. 4 to 12
20 | Page
Graphical RobotC Tutorial Packet Storming Robots
Computational Thinking and Engineering for Gr. 4 to 12
Step 2) Flowchart
Since the logic may seem to be a bit complex, you need to Divide and Conquer.
Step 3) Program
3.a) Follow the divide and conquer method to create two separate programs:
Sub-program 1
1. Break down the tasks into 2 small task as
shown on the right.
2. Write your flowchart for each of the sub-
programs.
3. For each sub-program, compile and Sub-program 2
download to your robot, and execute.
4. Ensure it produces result as expected.
21 | Page
Graphical RobotC Tutorial Packet Storming Robots
Computational Thinking and Engineering for Gr. 4 to 12
22 | Page
Graphical RobotC Tutorial Packet Storming Robots
Computational Thinking and Engineering for Gr. 4 to 12
Step 2) Flowchart
Again, lets use divide and conquer method again. However, this is a more appropriate way for designing
your modules. First, it starts with a high level flowchart including names of the sub-task(s) only. After that, each
sub-task should have its own detailed design flowchart.
Sub-program 1 flowchart
Step 3) Program
3a) Follow the divide and conquer method to create two separate programs:
23 | Page
Graphical RobotC Tutorial Packet Storming Robots
Computational Thinking and Engineering for Gr. 4 to 12
24 | Page
Graphical RobotC Tutorial Packet Storming Robots
Computational Thinking and Engineering for Gr. 4 to 12
Valid Values
Power level = -100 to 100, where 0 == stop
Programming Expression
NXT EV3
May set any single one of the 4
motor ports to a certain power
level.
May set multiple
25 | Page
Graphical RobotC Tutorial Packet Storming Robots
Computational Thinking and Engineering for Gr. 4 to 12
Ex1) Robot goes forward for 2 seconds and backward for 3 seconds.
b) Use While
26 | Page
Graphical RobotC Tutorial Packet Storming Robots
Computational Thinking and Engineering for Gr. 4 to 12
Ex4) Robots runs for 5 seconds and terminate. During half way into the 5 seconds, it goes forward. Durng
the 2nd half of the 5 seconds, it goes backward.
Challenge 1)
Do a pivot turn for N milliseconds so that it makes almost a perfect 90 degree left turn.
Challenge 2)
Do a pivot turn for N milliseconds so that it makes almost a perfect 90 degree right turn.
Challenge 3)
Robot goes forward for 1 second, then makes a 90 degree left turn.
Challenge 4)
Repeat (3) for 4 times.
a) Use Repeat Until
b) Use While
Challenge 5)
Repeat (3) and stop in 10 seconds .
a) Use Repeat Until
b) Use While
27 | Page
Graphical RobotC Tutorial Packet Storming Robots
Computational Thinking and Engineering for Gr. 4 to 12
Programming Expression
Setup the sensor from the top menu. You can use it in any of the control structures. Example:
28 | Page
Graphical RobotC Tutorial Packet Storming Robots
Computational Thinking and Engineering for Gr. 4 to 12
b) Use While
Flowchart Program
Ex 2) Do (1), but robot turns around for about 180 degree if your robot hits an object.
a) Use Repeat/ Until
Flowchart:
Program
29 | Page
Graphical RobotC Tutorial Packet Storming Robots
Computational Thinking and Engineering for Gr. 4 to 12
Ex 3) Do (2), but robot should do some wobbling dance after it turns 180 degrees around.
a) Use Repeat/ Until
Flowchart:
Program
b) Use While
Now, you should try this yourself.
30 | Page
Graphical RobotC Tutorial Packet Storming Robots
Computational Thinking and Engineering for Gr. 4 to 12
Program:
31 | Page
Graphical RobotC Tutorial Packet Storming Robots
Computational Thinking and Engineering for Gr. 4 to 12
Challenge 1)
Program your robot to do the following forever:
- Go Forward
- If it hits an obstacle, it goes backward for 1 sec. Turn around for approximately 180 degrees.
Steps:
A) Draw a flowchart. Remember to hand test it first before you program.
B) Program it, execute, test and debug.
Challenge 2)
Do Challenge 1, but the robot will terminate after 5 seconds.
Hint: timer in a while loop. Inside the loop, there is an if-structure. Do not do reset timer first.
Challenge 3)
Do Challenge 2, Repeat Until structure:
Hint: timer in a Repeat Until loop instead of while. MUST change the Boolean expression
Challenge 4)
Robot is happily testing its wheel muscles by going forward for 10 seconds. However, if it detects an object, it
will stop and make a sound to signal you to remove the obstacle. Then, it will continue to run forward and hope
that the obstacle will be removed. It will continue to do that within the first 7 seconds. After 7 seconds elapse,
if it detects an object again, it gets very angry, and decides to turn around 180 degrees and terminate.
A) Draw a flow chart first. Remember to hand test it first before you program.
B) Program it, execute, test and debug.
Hint: use while timer loop. Within that, use if-structure
32 | Page
Graphical RobotC Tutorial Packet Storming Robots
Computational Thinking and Engineering for Gr. 4 to 12
LED: emits light which is reflected off of a surface and picked up by the photo-transistor.
Before you proceed, you must attach a light sensor onto your robot. Your light sensor must face down in order
to see the floor.
It reports the amount of light to the main controller (the brain brick) as a number between 0 (total darkness)
and 100 (very bright). The light sensor uses its own light source, the light emitting diode (LED), to illuminate a
small area for its receiver.
For EV3: It comes with a color sensor which provides you the same functionalities as the NXT light sensor.
33 | Page
Graphical RobotC Tutorial Packet Storming Robots
Computational Thinking and Engineering for Gr. 4 to 12
3) This is the threshold value you will use in order for your robot to distinguish between black and white.
Programming Expression
OR
For EV3:
Example to choose a color sensor: (Again, you must first do the Sensor Setup from the Setup Menau.)
Hint: Use
to
display what the feedback value.
This is very useful for debugging
purpose.
34 | Page
Graphical RobotC Tutorial Packet Storming Robots
Computational Thinking and Engineering for Gr. 4 to 12
Program:
Ex2) Robot will trace a circle. The circle is formed by a 1 black tape.
a) Do this indefinitely.
b) Repeat this for only 20 seconds - Repeat Until. (work on this on your own.)
c) Do (b), but with while loop. (work on this on your own.)
Ex3) Do (2), except robot stops when it sees silver.
a) Do this with while loop.
35 | Page
Graphical RobotC Tutorial Packet Storming Robots
Computational Thinking and Engineering for Gr. 4 to 12
36 | Page
Graphical RobotC Tutorial Packet Storming Robots
Computational Thinking and Engineering for Gr. 4 to 12
Challenge 1)
Robot needs to detect two cracks (marked by black tape). After it completely passes the 2nd crack, it stops.
Hint: use counter loop for # of cracks. Watch for what is meant by one a complete crack. Write flowchart
to determine the details.
Challenge 2)
Program your robot to be a right biased line tracing. That means, when it comes to an intersection, it will follow
the rightmost path.
Hint: still use one light sensor. Think Simplcity.
Challenge 3)
Do the Intense line-marathon - Go through a line maze with a lot of 90 degree turns.
Hint: two light sensors. Important that you will write a flowchart first.
Challenge 4)
Robot traces a path using one light sensor. It must stop when it reaches the silver tape.
37 | Page
Graphical RobotC Tutorial Packet Storming Robots
Computational Thinking and Engineering for Gr. 4 to 12
Accuracy will also depend on surface curvature the sound wave hits, and sufficient power source.
Programming Expression
By now, you should be familiar with the basic control structure of if-, if-else, and loop structure using while,
repeat until, and repeat. The structure itself does not change. The changes that will need to take place will
only be the Boolean expressions, where you put sensor type, and its corresponding value.
38 | Page
Graphical RobotC Tutorial Packet Storming Robots
Computational Thinking and Engineering for Gr. 4 to 12
Ex1) Simple Obstacle Maze. Whenever robot detects an obstacle closer than 7 cm, it turns right 180
degrees. Continue this forever.
Flowchart
Program
39 | Page
Graphical RobotC Tutorial Packet Storming Robots
Computational Thinking and Engineering for Gr. 4 to 12
Program:
40 | Page
Graphical RobotC Tutorial Packet Storming Robots
Computational Thinking and Engineering for Gr. 4 to 12
41 | Page
Graphical RobotC Tutorial Packet Storming Robots
Computational Thinking and Engineering for Gr. 4 to 12
Integrate
Challenge 1)
Modify Mini-Challenges done in the Touch Sensor Chapter to use ultrasonic sensor instead of touch sensor.
Challenge 2)
Robot moves in a straight line towards and obstacle. When it gets within 7 cm of the obstacle, it must slowly
back up until it is 20 cm away, then turn and drive quickly in the opposite direction.
Challenge 3)
Robot traces a black line. The end of the line is signified by an obstacle or wall 7 cm away. Robot must stop as
close as possible without touching the object.
42 | Page
Graphical RobotC Tutorial Packet Storming Robots
Computational Thinking and Engineering for Gr. 4 to 12
2) Headings:
This command is useful for making movements relative to the robot's latest reset point, instead of its
current position.
Range from -180 to + 180(in degrees).
Programming Expression
43 | Page
Graphical RobotC Tutorial Packet Storming Robots
Computational Thinking and Engineering for Gr. 4 to 12
44 | Page
Graphical RobotC Tutorial Packet Storming Robots
Computational Thinking and Engineering for Gr. 4 to 12
45 | Page
Graphical RobotC Tutorial Packet Storming Robots
Computational Thinking and Engineering for Gr. 4 to 12
One full Motor rotation = 360 encoder value. MUST note: This denotes a count value, not type geometric
rotational degrees.
Programming Expression
NXT EV3
After Reset the encoder count to 0.
v4.27
Set the target encoder for a single
motor. This does not start the
motor. You need to use setMotor( )
to start it. The internal motor control
library will stop your motor for you
once it reaches the specified target
value.
For NXT:
will also reset the
motorEncoder to 0.
Display the motor encoder value.
Great way for debugging purpose
46 | Page
Graphical RobotC Tutorial Packet Storming Robots
Computational Thinking and Engineering for Gr. 4 to 12
only
Flowchart for
Erin/Anne
Erin / Anne
47 | Page
Graphical RobotC Tutorial Packet Storming Robots
Computational Thinking and Engineering for Gr. 4 to 12
- this will pre-set the target encoder value, and start the motor to the desired power level.
- To wait for the motor to complete its rotation before you proceed to perform other tasks, you need to
do:
Things that you should be aware. For example, you wish to run your robot with 100% power level for 1800
encoder value and stop. You chose to use RobotCs instrinsic motor feedback control setMotorTarget.
Do not do this:
Do this instead:
9.2) E XERCISES
Ex1) Robot runs forword until it reaches 1800 degree and stops.
Ex2) Program your robot to run in a square of 100cm on each side. Hint: use encoder to restrict it to make 90
degrees turn.
a) Use Repeat Until
b) Use While
Note: make sure you will do your Flowchart before Program
48 | Page
Graphical RobotC Tutorial Packet Storming Robots
Computational Thinking and Engineering for Gr. 4 to 12
- Do a) Program your robots to run 100% power level while encoder value < 3000.
- Do b) Mark the start point and end point where robot stops at.
- Do c) Redo the program with setMotorTarget method
49 | Page
Graphical RobotC Tutorial Packet Storming Robots
Computational Thinking and Engineering for Gr. 4 to 12
e.g.
Warning: Do not use long variable names, you may have only be able to see partial number as it exceed the
width size of the screen.
Advice:
Test the value of your calculation before you actually put that as part of actual program. You may utilize the
PC Emulator Mode as well if you are not counting on any physical robot and its sensors value. (From
Robot Compile Target)
50 | Page
Graphical RobotC Tutorial Packet Storming Robots
Computational Thinking and Engineering for Gr. 4 to 12
Advice:
Depending on the students algebra level, it is actually better to mandate the students to do the calculation on
their own in order to reinforce their math skill.
See! While it is more efficient to use variables, if you do not understand it, you wont be able to utilize it in
another applications properly anyway! So, the Key is to UNDERSTAND it, NOT TO MEMORIZE it.
51 | Page
Graphical RobotC Tutorial Packet Storming Robots
Computational Thinking and Engineering for Gr. 4 to 12
Button control allows robot to provide information interactively with human, such as display values, or change
actions, etc. this is a great tool for debugging purpose.
52 | Page
Graphical RobotC Tutorial Packet Storming Robots
Computational Thinking and Engineering for Gr. 4 to 12
Issue: You want the robot to pause for 5 second so that you can check on something. However,
you neglect that robot is still moving while it is in the 5 seconds wait time.
53 | Page
Graphical RobotC Tutorial Packet Storming Robots
Computational Thinking and Engineering for Gr. 4 to 12
NOTE:
Before you tackle the following challenge, you should review the best practice guideline in Chapter 2.
If you follow the best practice discipline, your development time will be much shorter, and obtain higher
change in success.
Reminder:
1) Divide and Conquer
2) Design one high level flowchart. This flow chart should reflects most sub-tasks.
3) Design each sub-task with its own flow chart.
4) Make sure all sub-tasks are well commented with information pertaining to the context of the task or
behavior.
Your robot should be able to navigate the follow path with one or two light sensor .
15cm 15cm
54 | Page
Graphical RobotC Tutorial Packet Storming Robots
Computational Thinking and Engineering for Gr. 4 to 12
There will be a barcode laid out so that each bar denotes a separate note, depending on its width in centimeters.
Your job is to program your robot to figure out what the secret song is.
A B C D E F G
2 cm 3 cm 4 cm 5 cm 6 cm 7 cm 8 cm
To play a note, use the command where the first number (440)
is the frequency of the note, and the second number (50) is the duration in 10 millisecond ticks.
55 | Page
Graphical RobotC Tutorial Packet Storming Robots
Computational Thinking and Engineering for Gr. 4 to 12
A B C D E F G
440 Hz 493.88 Hz 261.63 Hz 293.66 Hz 329.63 Hz 349.23 Hz 392 Hz
**Please note that the frequency is an integer type in the playTone command, so you must round.
Goal : Robot should perform line tracing. When robot detects on obstacle, it should get
around it and stops back on the line behind the obstacle. Obstacle is a cyclinder with a known
size diameter.
Note: you may figure out the power levels for the left and right motor side based on various methods:
a) Ad-hoc : use trial and error , guess and check
b) Computational: calculate the ratio of the two sides power levels based on the diameter of the obstacle.
56 | Page
Graphical RobotC Tutorial Packet Storming Robots
Computational Thinking and Engineering for Gr. 4 to 12
Level 1: Goal Run in the field within 2 minutes, and ensure you wont hit Zombies. Your robot must be able
to reach at least once at all sides of the field, but without going out of bound.
Level 2: Goal Your robot will need to report distance to each zombie at each safety station. Beware, your
robot must stay on the safety zone (the black line) along the whole way. Otherwise, your robot will be eaten up
by the Zombie.
Each safe station, ther is a silver mark. Your robot must report the distance of each zombie on the screen. .
57 | Page