ENGR 010 - Lab 3

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

ENGR 010 Spring 2022

Dr. Mihail Cutitaru

Lab 3 – Motor Control

Deliverables and due dates:

1. Demo to your TA during lab or at the latest by Tuesday April 12 at 6PM


2. A .tar.gz archive upload of your code to Coursesite by Tuesday April 12 at 11:59PM.
On the Pi: tar -cvf Lab3.tar.gz forward.py turn.py square.py to archive all 3 files on the RPi.
On your laptop: scp pi@<yourPi>:/home/pi/Desktop/ENGR10/Lab3/Lab3.tar.gz ./Downloads
to copy the files to your laptop under the Downloads folder. Replace <yourPi> with your own
hostname, e.g. pi@mic520 for me.
a. For each of your .py files, add the following comments at the top:
i. Course number (ENGR 10)
ii. Lab number (Lab 3)
iii. Your name
iv. Email address

For this lab, you will need the following from the CamJam kit:
1. Motor control board
2. 2 wheels
3. 2 motors
4. Battery pack
5. Caster ball (not included in figure below)

Your TA will supply:


1. 4 rechargeable batteries
2. A robot body
Wiring

Use the following steps to connect the required hardware before connecting everything to your
Raspberry Pi. Make sure your Raspberry Pi is powered off for all of the following steps.

1. Take a wheel and connect it to a motor as shown below. In the lab, you should be given a 3-D
printed robot body where all components will go in specific places. To make things neater,
connect the wheel to the opposite side of where the wires come out of. Do this for both
wheels/motors.

2. Each motor contains 2 wires coming out of it (red and black). We will use the usual convention
for cable coloring: red – VCC and black – GND. These connections must match exactly to the
correct connections, otherwise your code will not run. You will be using the provided motor
control board to control the motion of the motors. This board uses an H-bridge (discussed in
lecture) to control the motors. The figure below shows the motor control board used in this
CamJam kit.

For this board, there are 3 pairs of connections: motor A on the left, battery connections in the
middle, and motor B on the right. For our purposes, we will use motor A on the right side of the
robot and motor B on the left side of the robot. If you find that your robot moves in the
opposite direction of travel once you program it, flip the orientation of the motor that is driving
in the opposite direction. For example, if you program your robot to drive straight and it makes
a turn instead, flip the motor that does not move in the correct direction.

3. Make the connections to the right and left motors correctly (VCC wires go towards the outside
of the motor control board and GND wires go towards the inside). Sometimes the motors will be
backwards, depending on which one you picked up first and the wire placement, so your motors
may run backwards when you try them later. If so, switch your motors or reverse one of the
motors so that the wires are on the wheel side.

4. After connecting the motors, connect the battery pack. Take the empty battery pack and
connect it to the appropriate terminals (+ for red wire and - for black wire), as shown below. On
the back of the battery pack, make sure the switch is on OFF.

5. Once this is completed, install the battery pack and motors in the appropriate spots on the
robot body.
6. Install 4 rechargeable batteries provided by your TA into the battery pack. Make sure the cover
for the battery pack is closed securely.
7. With the Raspberry Pi still OFF, connect the motor control board to the Raspberry Pi. We are
using the upper half of the pins on the Raspberry Pi starting on the first row, make sure you
have the proper connections before power up the Pi. The motor connections should be towards
the power input of the Pi when connected properly. Have your TA check this before powering
up the Pi. Connecting the motor control board in the wrong direction may damage your Pi or
your motor control board. This is the most important step of the wiring part. Your final
connections should look similar to the ones below (ignore power connections, etc in the figure
below).

8. Connect your caster ball to your robot body.


9. Once you have proper connections for the hardware, connect the power to your Pi. You should
see a view similar to the one above in Step 7. You will see a green LED turned ON on the motor
control board.
10. On the underside of the battery pack, turn the switch to the ON position. You are now ready to
write code to control the motors.

Programming

1. Download these instructions and any other relevant files (lecture slides or gpiozero library or
other files you may need) to your laptop in the lab.
2. Connect your laptop to the ENGR 10 network.
3. Open a SSH connection to your Pi.
4. First, you need to enable the SPI connection on the Pi to be able to use the motors. In your
terminal, type the following command and hit Enter.
sudo raspi-config
You should see a screen similar to the one below.

5. Use the up/down arrows to navigate options, left/right arrows to Select or Finish. Advance to
the Interface Options and hit Enter. You should see a screen similar to the one below.

6. Select SPI and hit Enter. You should see a screen similar to the one below.
7. Select <Yes> and hit Enter. You should see a confirmation screen, similar to the one below.

8. Hit Enter and then right arrow on the main screen to select <Finish>.

9. Create a new folder on the Pi under the ENGR10 folder: home/pi/Desktop/ENGR10/Lab3 . The
cd and mkdir commands will be useful in accomplishing this.
10. You will have 3 tiers of code for each letter grade, as in previous labs. This lab will be a bit
challenging as you will need enough lab space for each grade. Additionally, the power in your
batteries will influence the behavior of your code (e.g. fresh batteries will need a shorter time to
drive the distance when compared to stale batteries).
a. C code: make the robot drive straight for roughly 30 cm. This will be a bit challenging as
the desk space in the lab is limited. You may need to carry the power cable of your Pi
manually and try running the robot on the floor. You will need a stopwatch to see how
long it takes to run the distance (this time may change for every run, so use an
approximation). You may also need to change the speed of the robot as required.
b. B code: make the robot drive straight for roughly 30 cm, turn left, drive straight for
another roughly 30 cm.
c. A code: make the robot make a square of size roughly 30 cm on a side. Land within the
same general area of where you started. This will be challenging.

You will benefit from the following commands (comments are in red, not required for your code):

from gpiozero import CamJamKitRobot # imports the CamJam library


from time import sleep # imports the ability to do time delays
myRobot = CamJamKitRobot() # assigns myRobot to an instance of controlling the
CamJam robot
Functions to control motion on myRobot above:

myRobot.forward() # go forward at full speed


myRobot.forward(0.5) # go forward at 50% speed, can be any real value between 0-1 for
faster or slower movement
myRobot.backward() # go reverse at full speed
myRobot.backward(0.5) # go backward at 50% speed, can be any real value between
0-1 for faster or slower movement
myRobot.left() # turn 90 degrees left, pivot action
myRobot.right() # turn 90 degrees right, pivot action
myRobot.stop() # stops the robot
Example code:

You might also like