1. Algorithms, programs, and programming languages
  2. Motors
  3. Sensors

Algorithms, programs, and programming languages

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 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)

"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:

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)

digital(11) gives the value of the digital sensor connected to port 11. So if a touch sensor is connected to that port:

... 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

Note that the actual values will likely be different, but lower numbers will always mean "lighter" and higher numbers will always mean "darker".

Note that I haven’t 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...

What if you ask for an analog reading of a digital sensor?

In theory... this doesn't necessarily happen in practice...

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.

stop_button() gives the state of the stop button on the Handy Board.