Programming Concepts: (Part B)
Programming Concepts: (Part B)
(Part B)
ENGR 10
Introduction to Engineering
Hsu/Youssefi 1
Summary
Most commonly used logic statements:
Hsu/Youssefi 4
Sequential execution of main program
which calls the defined function
A, B, C, Z1, and Z2 are defined as global variables
Main Program
Function
Hsu/Youssefi 5
Introduction to Local Variable
• The MAX_Three function finds the greatest of the
three numbers A, B and C.
Hsu/Youssefi 6
Program using Function with
passing variables x, y, and z are defined as
Local variables
Hsu/Youssefi 7
What is a Local variable ?
• The value of a local variable is accessible only to
the function within which it is defined.
• The main function (main program) passes the
values into the function and these values are
copied into the local variables of the function.
• The function doesn’t care what are the names of
the variables defined in the main function.
• The function does what it should do and then
returns the result to the main function.
• The values of these local variables are lost after
the execution of the function is completed.
Hsu/Youssefi 8
Global Variable Concept
A, B, C, and Z are Main Function
defined as global
variables
Defined Function
A Task of
B the Z
function
C
Hsu/Youssefi 9
Local Variable Concept
Main Function
Defined Function
x, y, and z are defined
as Local variables
A x Task of
the max Z
B y
function
C z
Hsu/Youssefi 10
Mechanical Components – Motors
Clutch 1. Motor, continuous rotation of
the shaft (3 wire)
Motor
(Continuous) 2. Servo, shaft rotates through
a specified angle (3 wire)
3 pins
2 pins
Hsu/Youssefi 12
This chart describes
the combination of
the motor movement
required to perform
each maneuver
Hsu/Youssefi 13
How to connect the motor to the
Controller?
• On the VEX controller you have several ports
available so that you can connect not only
motors but also other devices and sensors to it.
PORT:
• A port is a specific place on the board where we
can connect other external devices, so that the
two devices in connection can exchange
information or data.
Hsu/Youssefi 14
Continuous Motor Choose motor
number, as connected
to the Controller Max. speed
counter clockwise
Hsu/Youssefi 15
How to move a motor?
• Use the following statement to move a motor
SetMotor ( 2 , 60 );
8 analog and
12 digital
ports. Digital
ports are
used for Limit,
X
Bumper and
connected to the IRB Ultrasonic
mounted on the robot
Hsu/Youssefi sensors 17
Side view of the VEX Controller
Hsu/Youssefi 18
Cortex
Two continuous motors
to drive the robot (2-pin)
Hsu/Youssefi 19
Program to move the robot forward
• Now let us write a program to make the robot
move forward for 2 seconds and then stop.
Assume:
Left Motor – connected to motor port 3
Right Motor – connected to motor port 2
Hsu/Youssefi 20
Program to make the motor move
forward for 2 seconds and then
stop
Hsu/Youssefi 21
Clicker Question 3
What value of x will cause the motor for Cortex
controller to be stopped: SetMotor(2, x)?
A. 64
B. 127
C. 0
D. 255
E. -127
Hsu/Youssefi 22
Servos
• Servo units are similar to motors except that they
control position rather than speed.
• Servo units have limited range of motion
(about 120o)
• A value between (-127) and (127) sets
the direction of the servo.
0
60o -60o
127 -127
Hsu/Youssefi 24
Servo
• Similar to the motor the following statement is
used to set the servo to turn to a direction.
SetServo ( 5 , 64 );
clo
i se 0o ckw
oc kw ise
r-c l
u nte 0
30o
Co
64
600 -60o
127 -127
Hsu/Youssefi 26
Program showing how to move
the servo continuously
Hsu/Youssefi 27
Output of the above program
se
clo
c kw i
0o
ckw
lo ise
te r-c
n
Cou -30o
0 30o
-64 64
60o - 60o
127 -127
Hsu/Youssefi 28
Mechanical Components - Sensors
Sensor
blocks
Ultrasonic Sensor
(Digital)
Bumper
Hsu/Youssefi Switch Sensor (Digital) 29
Introduction to Logic Signals
• A logic signal can take on either 0 volt or 5 volt
Hsu/Youssefi 30
Bumper/Limit Switch
• A Bumper/Limit Switch is a digital sensor.
Hsu/Youssefi 31
Bumper & Limit Switches
• Use of the following statement in the program
to read the bumper/limit switch.
Bumper = GetDigitalInput ( 2 );
Hsu/Youssefi 32
Program to make the robot stop and turn for 2 seconds
when it hits an obstacle
Hsu/Youssefi 35
Digital Output
• This statement is used in the program to
control the devices i.e., send digital signals.
SetDigitalOutput ( 11 , 1 );
Hsu/Youssefi 37
Ultrasonic Sensor
• The Ultrasonic Sensor is a digital sensor.
• The Ultrasonic Sensor uses high-frequency
sound waves to detect objects.
• It emits a sound wave, measures how long it
takes the sound wave to bounce back, and then
calculates how far away the object is.
• The measurement is translated into a numeric
value from 2 - 100.
• Lower signal value corresponds to a closer
detected object and vice versa.
Hsu/Youssefi 38
Ultrasonic Sensor Connections
Digital
Input
Output port
Digital Input
port Output
(Interrupt port)
Hsu/Youssefi 40
Ultrasonic Sensor
The following statement is used to read a value from the sensor
Range = GetUltrasonic ( 5 , 6 );
Variable into The sensor is connected
which the Input to Ultrasonic to output port # 6 on the
translated value is port #5 controller
stored
Select Get option
Hsu/Youssefi 41
Changing Input port to Output port Edit Config
Change port 6
from input to
output by clicking
on the circle
Digital ports
taken by
the IRB
Hsu/Youssefi 42
Input to Ultrasonic sensor
connected to output port 6
on controller
Output from Ultrasonic
sensor connected to
input port 5 on controller
Hsu/Youssefi 43
Analog Signals
• For the VEX controller, an analog signal can take any
value between 0v and 5v.
5v 1023
→ 0v is represented by a value 0
→ 5v is represented by a value 1023 (210)
Hsu/Youssefi 44
Light Sensor
• A light sensor is an example of an analog sensor.
• A light sensor reads the intensity of light of an
area, translates the reading into a single
numeric value, and sends that value to the
robot.
• The range of values used by this sensor is
0-1023
• The higher the value, the darker will be the area
and vice versa.
Hsu/Youssefi 45
Light sensor
• The following statement is used to read the
analog signal from a light sensor.
light = GetAnalogInput ( 1 ) ;
Hsu/Youssefi 46
Program to make the robot move
towards the moving light source
ELSE IF is used for more than 2 cases
Hsu/Youssefi 47
Optical Shaft Encoder
• The optical shaft encoder is a digital sensor. It is
used to measure rotational movement.
• As the disc rotates, an infrared light sensor is used
to count the number of slots passed.
• A count of 90 makes one revolution.
Hsu/Youssefi 48
Optical Shaft Encoder
• The following statement is used to start the encoder
counting.
StartEncoder ( 2 ) ;
Hsu/Youssefi 49
Optical Shaft Encoder
• The following statement is used to get the
present count value from the encoder.
Count = GetEncoder ( 2 ) ;
Hsu/Youssefi 50
PresetEncoder ( 2, 20) ;
This statement is used to make the encoder start
counting from a given number (here 20) instead
of from zero.
Example
Write a program to make the robot move
for 1 meter and then stop.
Assume 5 turns of the wheel = 1 meter
Hsu/Youssefi 51
Program to make the robot
move forward for 1 meter
Hsu/Youssefi 52