Controlling Your Robot

With all the motor parts in place, it’s now time to hook everything up and start testing out your robot. Once the motor control unit is attached to the Raspberry Pi and it is all powered up, you’re ready to start testing its movement in code.

READY TO ROLL

Our robot still has a way to go to be complete but with the servos and wheels attached to the motor control unit and the power in place, it’s ready to start rolling around.

STEP 1

You should have all six wires attached to the EduKit Controller Board unit. Double check that the black wire from the battery is going to GND and the red wire to VIN and that the black and red wires to Motor A and Motor B are the mirror opposite way around (as shown in this photo). Gently push the motor control unit into the GPIO pins on the left side.

STEP 2

Now it’s time to power up your robot and see if it moves. Attach the battery pack to the Raspberry Pi and switch on the battery box. Give the Raspberry Pi 30 seconds to go through the boot process and connect to it from a computer using VNC and make sure it is responding.

STEP 3

Open the CamJam website (camjam.me/?page_id=1035#worksheets) and download Worksheet 2 - Running the motors. Open a text editor on your computer and type out the code for the 2-motors.py program. You can cut and paste it to your text editor but we think it makes more sense to type it line by line so you understand what it’s doing.

STEP 4

Open a FTP program such as FileZilla and choose File > Site Manager. Enter the IP address in the Host field, “pi” in the User field and your password (“raspberry” by default). Right-click on pi under Remote Site and choose Create Directory, name it “EduKitRobotics”. Now transfer the 2-motors.py file to the EduKit directory on your robot.

STEP 5

Open the VNC program and connect to your Raspberry Pi. You should see the Xfce desktop even if you set up the robot to book into the command line. Enter cd EduKitRobotics. Make sure your robot is on the floor and has about a meter of space in front of it. Now enter python3 2-motors.py and press Return.

STEP 6

All being well your robot should move forwards. If it moves backwards you need to swap the position of the red and black wires on both Motor A and Motor B. If it fails to move then check the wires to the motor control unit, check that verything is switched on and try replacing the batteries (you need good quality batteries with enough power to move the servos).

DRIVING CODE

Now that our robot is up and moving, it’s time to give it better control functionality. This code enables you to move the robot forward, backwards and rotate left and right. It also makes it easy to give the robot multiple directions in order.

STEP 1

Head back to the CamJam website (camjam.me/?page_ id=1035#worksheets) and download Worksheet 3 - Driving and Turning. Type in the first block of code and save it as 3-driving.py in the EduKitRobotics folder. Make sure you enter the code from the section Moving the Robot at the end of your main block of code.

STEP 2

The worksheet advises using nano to create code directly on your Raspberry Pi robot but we think it makes more sense to type it out on your computer and transfer it using FTP. Use VNC to connect to your Raspberry Pi and run the program using python3 3-driving.py. The robot should move forwards then backwards.

STEP 3

Take a good look at the Python code to see how it creates function definitions for Forwards(), Backwards() and StopMotors(). These definitions enable you to move the robot using functions, rather than typing out the code each time. Now add the Left() and Right() functions from the worksheet to the code.

STEP 4

Add the movement instructions to the very end of the code and save the 3-driving.py program to your EduKitRobotics folder (using FTP to transfer it). Use VNC to run python3 3-driving.py on the Raspberry Pi. The robot should move forwards, rotate left, move forwards again, rotate right, move backwards and then stop.

More information