-
Algorithms, programs, and programming languages
-
Motors
-
Sensors
Algorithms, programs, and programming languages
- What is an algorithm?
- What is a program?
- What is the difference between instructions we give to people and
the instructions we write for computers?
Over the course of the semester, you will have the opportunity to program
vehicular robots with the programming language Interactive C (IC).
While programming languages are not the same as human languages, they
do share certain properties. Programming languages have
- Vocabulary for referring to entities;
- Vocabulary to refer to primitive operations/functions;
- Rules for forming sentences (or more complex instructions) -- i.e.,
syntax.
We will begin by learning some simple vocabulary that will allow us
to refer to the motors and sensors on the robot as well as to primitive
operations on those.
Motors
The Handy Board has 4 motor ports (0, 1, 2, 3)
- Motors may be set
- In a "forward" direction
- In a "backward" direction
- Power level of motors may be controlled
- Range from 100 = full power in the "forward" direction
- to -100 = full power in the "backward" direction
- Really only seven degrees of power, but still a fair bit of control.
"Forward" movement
fd(0);turns motor 0 on in the forward direction at full power
fd(1);turns motor 1 on in the forward direction at full power
fd(2);turns motor 2 on in the forward direction at full power
fd(3);turns motor 3 on in the forward direction at full power
"Backward" movement
bk(0);turns motor 0 on in the backward direction at full power
bk(1);turns motor 1 on in the backward direction at full power
bk(2);turns motor 2 on in the backward direction at full power
bk(3);turns motor 3 on in the backward direction at full power
To control motor speed
motor(0, 50);turns motor 0 on in the forward direction with a power
level of 50%
motor(3,-10);turns motor 3 on in the backward direction with a power
level of 10%
in general
motor(m, s);
where m is the name of the motor port and s is the power level
Note that:
- "Power" is not the same as "Speed", especially in a well-geared motor.
- motor(m, s) can be used either to turn the motor on at a specific level
of power or can be used to change the level of power once the motor is
running.
To shut a motor off
off(0);shuts off motor 0
off(1);shuts off motor 1
off(2);shuts off motor 2
off(3);shuts off motor 3
To shut off all motors
alloff();
ao();short form for "alloff"
Note the use of semicolons above! Each statement (i.e., command)
in C must be terminated by a semicolon.
Sensors
The Handy Board has 16 sensor ports (0-15)
- Ports 0-6 for analog sensors
- Analog = continuously varying.
- e.g., our light sensors.
- Ports 7-15 for digital sensors
- Digital = like a switch (on or off).
- e.g., our touch sensors.
digital(11) gives the value of the digital sensor connected to port 11.
So if a touch sensor is connected to that port:
- 1 = true = touch sensor pressed
- 0 = false = touch sensor not pressed
- digital(15) gives the value of the digital sensor connected to port 15
- digital(14) gives the value of the digital sensor connected to port 14
... and so on.
analog(6) gives the value of the sensor in port 6. It returns
a value between 0 and 255. For example, if a photocell is attached to
port 6, might return
- 0 = most bright
- 255 = totally dark
Note that the actual values will likely be different, but lower numbers
will always mean "lighter" and higher numbers will always mean "darker".
- analog(6) gives the value of sensor in port 6
- analog(5)
- analog(4)
- analog(3)
- etc.
Note that I havent used any semicolons here!
If these are used in isolated cases (i.e., not
as part of a larger program), then we will use semicolons. When used in
a program, however, these will typically not appear alone. They will appear
in the context of other code. In those cases, they will not be immediately
followed by semicolons.
A note about Cadmium Sulfide photocells: Photocells are resistors.
The resistance varies in proportion to the amount of light striking
the surface of the device. The greater the intensity of the light, the lower
the resistance.
What happens if you ask for a digital reading of an analog sensor?
In theory... this doesn't necessarily happen in practice...
- If the analog measurement is less than
127, the digital reading will be true (i.e., 1)
- If the analog measurement is greater than or equal to
127, the digital reading will be false (i.e., 0)
What if you ask for an analog reading of a digital sensor?
In theory... this doesn't necessarily happen in practice...
- If the digital reading is 0, then 0 is returned.
- If the digital reading is 1, then 255 is returned.
We can also sense the state of the start and stop buttons:
start_button() gives the state of the start button on the Handy Board.
- 1 = true = the start button is pressed
- 0 = false = the start button is not pressed
stop_button() gives the state of the stop button on the Handy Board.
- 1 = true = the stop button is pressed
- 0 = false = the stop button is not pressed