Lab 6


Controlling servo motors

Do recipe 11.1, our first lab exercise using motors. In this recipe you will control a servo motor. A servo motor can rotate an arm between 0 and 180 degrees (i.e., half a circle).

PWM (Pulse Width Modulation), which was used in lecture 2 to control the brightness of LEDs, is used to control the position of the servo motor. In this recipe, a pulse is sent every 10 milliseconds (resulting in 100 pulses per second or 100Hz) and the position, between 0 and 180 degrees, of the servo motor is determined by the length of the pulse. If the length of the pulse is 1 milliseconds or less (i.e., 10% or less of 10 milliseconds) the motor will be at 0 degrees. If the length of the pulse is 2 milliseconds or more (i.e., 20% or more of 10 milliseconds) the motor will be at 180 degrees. Pulse lengths between 1 millisecond and 2 milliseconds map uniformly to motor positions between 0 and 180 degrees.

For more info about the difference between servo motors, DC motors, and stepper motors see
https://www.modmypi.com/blog/whats-the-difference-between-dc-servo-stepper-motors ***
For basic info on how motors work see
http://hyperphysics.phy-astr.gsu.edu/hbase/magnetic/mothow.html ***


Controlling the Speed of a DC Motor

Raspberry Pi Cookbook Do recipe 11.4, using Figure 11-7 rather than Figure 11-6 to tie your circuit.

A DC motor has two terminals (wires). One is tied to a source of 5V and the other is tied to ground; it doesn't matter which terminal is tied to 5V. Depending on how you tie up your circuit, the motor will rotate clockwise of counterclockwise.


Controlling the Direction of a DC Motor

Do recipe 11.5, using option 1.

In this recipe the goal is to tie a DC motor in a H-bridge circuit that will allow us to change is rotation direction from within a Python program. Here is an illustration of an H-bridge:

Raspberry Pi Cookbook

(from the textbook)

When switches S1 and S4 are closed and S2 and S3 are open, terminal A is positive and B is negative and the motor will rotate. If S1 and S4 are open but S2 and S3 are closed, then terminal A is negative and B is positive and the motor will rotate in the opposite direction.

An H-bridge can be built using several transistors. Instead, we use the Texas Instruments L293D H-Bridge chip which actually contains 2 H-bridges on each side of the chip (allowing it to control 2 DC motors which we will make use of with the raspirobot). This illustration explains the wiring  in Figure 11-8 used in recipe 11.5, option 1:

(from instructables.com)
A source of 5V is connected to pin 16 and pin 5 is connected to ground. Pin 8 is connected to the power supply for the motor. Pins 1, 2, 3, 6, and 7 are used to control one motor and pins 9, 10, 11, 14, and 15 are used to control another. (In this recipe we only use one motor so we do not make use of these last 5 pins.)

Pin 1 is connected to a Pi output pin that uses PWM to control the speed of the motor. Pins 3 and 6 are connected to the DC motor's two terminals. Pins 2 and 7 are used to control the direction of rotation of the motor. Setting 2 to high and 7 to low sets the direction one way; setting 2 to low and 7 to high reverses the direction.

For more info, see
Raspberry Pi Cookbook
L293D chip datasheet


Using a Unipolar Stepper Motor

Raspberry Pi Cookbook Do recipe 11.6.


Building a simple robot rover

Follow the instructions in
https://github.com/simonmonk/raspirobotboard2 ***
if you picked up a RaspiRobot V2 expansion board or in
https://github.com/simonmonk/raspirobotboard3 ***
if you picked up a RaspiRobot V3 expansion board.

WARNING: Never power the Raspberry Pi from both batteries and the USB power connector. One or other, but NOT both.

See module rrb2.py (or rrb3.py) to see the available commands to control the robot and how they work.

Exercise: Using a rangefinder, write a program that moves the robot forward until it is within a foot of an obstacle. When successful, extend your program so that, when it is within a foot of the obstacle, the robot turns around 180 degrees and returns back to its starting position.


Homework

Re-read the recipes in the textbook we have covered in Lab 6. Also go through the starred (***) online tutorials and online documentation covered in Lab 6.