0% found this document useful (0 votes)
1K views17 pages

C-Robot Simulator: Program Structure

The document describes the structure and functions of a C-Robot simulator program. It contains two main functions: void set-up() which runs once, and void loop() which runs infinitely. Functions use opening and closing parentheses and braces to define statements and commands. The document also describes basic robot movements like forward, backward, turning, and using motors and sleep commands. It provides examples of sensor calibration, object detection, line following, and color sorting programs for the simulator.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1K views17 pages

C-Robot Simulator: Program Structure

The document describes the structure and functions of a C-Robot simulator program. It contains two main functions: void set-up() which runs once, and void loop() which runs infinitely. Functions use opening and closing parentheses and braces to define statements and commands. The document also describes basic robot movements like forward, backward, turning, and using motors and sleep commands. It provides examples of sensor calibration, object detection, line following, and color sorting programs for the simulator.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

C-ROBOT SIMULATOR

Learner’s Hand-outs

PROGRAM STRUCTURE
Two Main Functions
1. void set-up (){} – a function that runs the once.

2. void loop (){} – a function that runs the program


infinitely or over and over

Figure 1.1

FOR () / Parenthesis
The open and close parenthesis in a function is used to instruct statements / commands in braces in
order to do or repeat operation following the number of cycles required.

Open and Close Braces


The open and close braces determine the beginning and end of a function.
*In Figure 1.1, the open and close braces in Line 2&4 determine the begiining and end of the
function void setup(), while Line 6&8 are for the void loop().

MOTOR ORIENTATION

Motor 1 (M1) Motor 2 (M2)


Left Wheels Right Wheels

Figure 1.2
C-ROBOT SIMULATOR
Learner’s Hand-outs

BASIC MOVEMENTS

1. FORWARD / BACKWARD

Using motor command:


motor (motor number, speed);

Example: motor(1,100);
motor(2,100);

* 100 is the maximum speed of the motor.


* Negative speed (-100) means moving the opposite
direction.

Example: Backward movement


motor(1,-100);
motor(2,-100);

* A semicolon must be placed after every statement /


command to separate multiple statement.
Figure 1.3

Using shortcut commands:


fd(100); - forward
bk(100); - backward

2. SLEEP COMMAND
use to set duration of a command in millisecond.
Example: How long will your robot move forward?

*The robot will move forward for 1000 millisecond or


1 second then stop.

sleep(duration in millisecond);
sleep(1000);

Figure 1.4
C-ROBOT SIMULATOR
Learner’s Hand-outs

3. TURN MOVEMENTS
3 DIFFERENT TURNS
Curve Turn – 1 motor is moving faster than the other motor.
Example: motor (1,100);
motor (2,30);

Shortcut: fd2(speed of M1,speed of M2);


fd2(100,30);

Pivot Turn – a kind of curve turn wherein 1 motor is moving forward and the other is at rest.
Example: motor (1,100);
motor( 2,0);

Shortcut: tl(100); - turn left


tr(100); - turn right

Spin Turn – both motors have the same speed, but 1 motor is moving the opposite direction
Example: motor (1,100);
motor (2,-100);

Shortcut: sl(100); - spin left


sr(100); - spin right

SENSOR CALIBRATION

1. DISPLAYING TEXT AND NUMBERS IN THE LCD SCREEN

lcd(“text”);

Example: lcd(“REGION 1”);


*The c-robot simulator LCD can only display 36 words or 18 words per row.

Figure 1.5
C-ROBOT SIMULATOR
Learner’s Hand-outs

2. DISPLAYING SENSOR VALUE IN THE LCD SCREEN

A. In your c-robot simulator, click on the ZX SETTING icon.

Figure 1.6

B. Select the sensor you want to calibrate.

Example: PA8

Figure 1.7

C. Create your calibration codes and run the program.


Sample Calibration code:
int sense = analog(8);
lcd (“%d”, sense);
C-ROBOT SIMULATOR
Learner’s Hand-outs

Figure 1.8

int - a data type used to define numeric variables holding whole number
a - variable name/ user define
analog(8) - analog port of the sensor you want to calibrate. (ex., PA8)
%d - variable value/sensor value

OBJECT DETECTION

A. In your c-robot simulator, click on the ZX SETTING icon.


C-ROBOT SIMULATOR
Learner’s Hand-outs

B. Select the sensor port PA8 To enable the distance sensor, click Apply and OK.

C. Calibrate your distance sensor PA8.

D. Define your variable and sensor.

E. Create the robot’s initial movement.


C-ROBOT SIMULATOR
Learner’s Hand-outs

F. Set the condition.


*Using if/else statement.
If/else statement executes a code if a specified condition is true or false.
“If” – condition is TRUE
“else” – condition is FALSE

G. Define the action code of the if/else statement.

F. Compile and run the program.


*The robot must stop from moving once it
detects the ball.
C-ROBOT SIMULATOR
Learner’s Hand-outs

TRY THIS CODE!

*The command keep_up() is used to grab or carry the ball while keep_down() is used to
release the ball.
C-ROBOT SIMULATOR
Learner’s Hand-outs

LINE FOLLOWING

1. Click on the ZX Setting icon and select your preffered sensor.

*Try to select PA0 and PA4

*Check on the VISIBLE POINT ON ROBOT


to make the position of the sensor visible in your
robot. You may choose your desired color of the
visible point.
C-ROBOT SIMULATOR
Learner’s Hand-outs

2. Calibrate your sensors.

*In this calibration code, we use

“left” as variable name for the left sensor in Port


Analog 0, and

“right” as variable name for right sensor in Port


Analog 4.

*Observe and remember the value for each of the sensors. The value of the IR sensor must be 1024
if placed in the White surface, and 0 if in the black line/surface.

3. Load Map 04: Basic Line Following Map


C-ROBOT SIMULATOR
Learner’s Hand-outs

4. Create your Line Following program.

 BOTH LEFT AND RIGHT SENSOR IN WHITE SURFACE

Run Forward

 LEFT SENSOR DETECTS BLACK

SPIN LEFT
C-ROBOT SIMULATOR
Learner’s Hand-outs

 RIGHT SENSOR DETECTS BLACK

SPIN RIGHT

TRY THIS!

Initial movement, both sensors detect WHITE

Left sensor detects black line

Right sensor detects black line


C-ROBOT SIMULATOR
Learner’s Hand-outs

LINE FOLLOWING WITH INTERSECTION

Load Map 05

1. Create the Basic Following Program

2. Determine the intersections.

*Both LEFT and RIGHT sensor detect


BLACK at the same time.
C-ROBOT SIMULATOR
Learner’s Hand-outs

3. From your Basic Line Following Program, add a GLOBAL VARIABLE.

GLOBAL VARIABLE

It is a variable defined outside a


function. It has a global scope means
it holds its value throughout the
program.
C-ROBOT SIMULATOR
Learner’s Hand-outs

4. Create conditional statement for intersection.

CONDITION

INCREMENTS THE COUNT VARIABLE BY 1

INITIAL MOVEMENT

1ST INTERSECTION DETECTED

2ND INTERSECTION DETECTED

NOTE: *The robot will do the initial movement if no “if count statement” is being declared for a
particicular if intersection.

Example:

NO DECLARED “If count statement” for the 2nd intersection.


So the robot will do the INITIAL MOVEMENT.
C-ROBOT SIMULATOR
Learner’s Hand-outs

COLOR SORTING

 LOAD MISSION 06 MAP.


1. Click on the Map icon in your c-robot simulator and select “Load Map as”.
2. Go to Local Disk (C:) and select the Program Files (x86).
3. Open c-robot_sim folder and select mission06

 Determine the number of intersections until the robot reaches the correct area where to release
the ball.

1 5 2

7 8 10

11

FOR THE RED BALL


8th Intersection – grab the ball
11th Intersection – release the ball
C-ROBOT SIMULATOR
Learner’s Hand-outs

 From your Line Tracing with intersection


program, add variable declaration for distance sensor
and the “if conditional statement” to grab or get the
ball.

distance sensor

Condition grab or get the ball

 Add keep_down command to the correct


intersection to where the robot should drop or release
the ball.

Other intersections - initial movement

2nd intersection - spin right

5th intersection - spin left

7th intersection - spin left

10th intersection - spin right

11th intersection,
DROP/RELEASE THE BALL

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy