Begin Programming: Set up Functions
Set up several functions to make programming your bot easier. Save you file as ‘drive.py‘ in ‘home/pi/motor-shield-master/‘.
This is an example of the code you will be writing. You need to add to it functions for backward, left, and right. Study forward to see how it works.
#Your name here! import PiMotor from time import sleep import RPi.GPIO as GPIO #Name of Individual MOTORS m1 = PiMotor.Motor("MOTOR1",1) m2 = PiMotor.Motor("MOTOR2",1) m3 = PiMotor.Motor("MOTOR3",1) m4 = PiMotor.Motor("MOTOR4",1) #To drive all motors together motorAll = PiMotor.LinkedMotors(m1,m2,m3,m4) #Names for Individual Arrows ab = PiMotor.Arrow(1) al = PiMotor.Arrow(2) af = PiMotor.Arrow(3) ar = PiMotor.Arrow(4) def forward(time): motorAll.forward(100) af.on() #This may need to change depending on orientation of pi sleep(time) motorAll.stop()
Add 3 more functions: backwards, left, and right.
Your right and left functions may need to be different depending on the maze you’re maneuvering through.
How fast is your bot going?
The video shows how fast our motors spin. Since all of our robots are direct drive, the angular velocity, spinning speed, of the motor is going to be the same as the angular velocity of the wheel. BUT since there are different wheel sizes, the robots will still move forward at different speeds. We’re always going to run the motors at 100.
- Use a stop watch (you can use a phone) to see how fast the arrow is spinning in revolutions per second. Your answer can be a decimal. You can watch the video on 0.5 speed to take your measurements, but make sure to divide your answer by 2. This is the angular velocity of your wheels.
- Convert your answer (revolutions per second), which is an angular speed, to a linear velocity.
Program your bot to move through the maze
Measure the distances in the maze and program the robot to best maneuver through it.
Depending on the course you’re driving through your code is going to look different. Regardless you will use your functions to make the robot move through the course.
forward(3) left(5) forward(9) right(2) forward(17) ...