Controlling your mobile platform programmatically using Raspberry Pi

Now that you have your basic motor controller functionality up and running, you need to connect both motor controllers to Raspberry Pi. This section will cover that and also show you how to control your entire platform programmatically.

First, let's configure all of the hardware on top of the mobile platform, as shown in the following image:

Controlling your mobile platform programmatically using Raspberry Pi

I tend to use lots of cable ties, but if you'd like it to look even more polished, feel free to use more metal and nuts and bolts. The following is a diagram of the connections:

Controlling your mobile platform programmatically using Raspberry Pi

I suggest that you use Python in your initial attempts to control the motor. It is very straightforward to code, run, and debug your code in Python. I am going to include the directions for Python in this chapter; you can also go to the Pololu website at www.pololu.com/ and find instructions for how to access the capabilities in C.

The first Python program you are going to create is shown in the following screenshot:

Controlling your mobile platform programmatically using Raspberry Pi
  1. To create the program shown in the previous screenshot, create a directory called track in your home directory by typing mkdir track and then type cd track.
  2. You should now be in the track directory.
  3. Now open the file by typing emacs dcmotor.py (if you are using a different editor, open a new file with the name dcmotor.py).
  4. Now enter the program. Let's go through the program step-by-step:
    • #!/usr/bin/python – This is the first line which allows your program to be run outside of the Python environment. You'll use it later when you want to execute your code using voice commands.
    • import serial – The next line imports the serial library. You need this to talk to your motor controllers. In order to run this program, you'll need the serial library. Install it by typing sudo apt-get install Python-serial in the prompt. You'll then need to add yourself to the dialout group; do this by typing sudo adduser ubuntu dialout. Then type a sudo reboot command to enable all these changes.
    • import time – This line imports the time library; you'll need this to add some delays in your code.
    • def setSpeed(ser, motor, direction, speed) – This function sets the speed and direction of one of your two motors. Since you are going to control the motors throughout your program, it is easier if you put this functionality in a function.
    • if motor == 0 and direction == 0: – This if statement tests to see if Motor 1 is going in the forward direction.
    • sendByte = chr(0xC2) – This line sets the address if you are going to send data to Motor 1 to move it in the forward direction.
    • if motor == 1 and direction == 0: – This if statement tests to see if Motor 2 is going in the forward direction.
    • sendByte = chr(0xCA) – This line sets the address if you are going to send data to Motor 2 in the forward direction.
    • if motor == 0 and direction == 1: – This if statement tests to see if Motor 1 is going in the reverse direction.
    • sendByte = chr(0xC1)This line sets the address if you are going to send data to Motor 1 in the reverse direction.
    • if motor == 0 and direction == 1: – This if statement tests to see if Motor 2 is going in the reverse direction.
    • sendByte = chr(0xC9)This line sets this address if you are going to send data to Motor 2 in the reverse direction.
    • ser.write(sendByte) – This line writes the sendByte command to the motor controller, which sets the motor's movements in different directions.
    • ser.write(chr(speed)) – This line writes the speed to the motor controller, setting the speed.
    • ser = serial.Serial('/dev/ttyUSB0', 19200, timeout = 1) – This line opens the serial port.
    • setSpeed(ser, 0, 0, 100) – This line calls the function and sets the speed of the motor from 1 to 100.
    • setSpeed(ser, 1, 1, 100) – This line calls the function and sets the speed of the motor from 2 to 100. Note that you have to make this motor go in the opposite direction if you want the platform to move forward.
    • time.sleep(1) – This line makes the program wait for one second.
    • setSpeed(ser, 0, 0, 0) – This line calls the function and sets the speed of the motor from 1 to 0.
    • setSpeed(ser, 1, 0, 0) – This line calls the function and sets the speed of the motor from 2 to 0.
    • time.sleep(1) – This line again makes the program wait for one second.
    • ser.close(): This line closes the serial port.
  5. After the installation process, you can run your program. To do this, type python dcmotor.py. Your motor should run for one second and then stop. You can now control the motor through Python. Additionally, you'll want to make this program available to run from the command line. Type chmod +x dcmotor.py. If you now do ls (list programs), you'll see that your program is now green, which means you can execute it directly. Now you can type ./dcmotor.py.

Now that you know the basics of commanding your mobile platform, feel free to add even more setSpeed commands to make your mobile platform move. Running just one motor will make the platform turn, as will running both motors in opposite directions.

The platforms you've looked at up until now had two DC motors. It would be easy to add even more motors. There are several platforms that provide DC motors for all four wheels. In such a case, you could just add more motor controllers and then update the code to access each individual controller.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.139.238.226