Home | Blog | What's New? | Software | Raspberry Pi | Contact

Driving the motors - Basic (Shell scripts)

The simpliest way of getting the motors to work (And to check that they are wired up correctly) is using a shell script to drive the GPIO pins directly. To keep things easy we will use one shell script to setup the GPIO pins and then additional scripts to actually turn the motors on and off.

In case we need to alter the GPIO pin numbers at any time (i.e. the motors get wired up backwards) we can define the values in a single file and have the other shell scripts include it. So when a change happens only one file needs to be updated

#!/bin/sh
# gpio.sh

# Setup all the appropiate pins to output, and configure them low (This ensures the
# motors won't be running

# 4 and 25 control motor A
export A0=25
export A1=4

# 21 and 18 control motor B
export B0=18
export B1=21

# 17 controls the 'standby pin' which enables/disables the motors
export STANDBY=17

Now that we know what GPIO pins we are using we need to setup the direction of the pins and make sure they are in a 'stopped' state.

#!/bin/sh
# setup.sh

# Setup all the appropiate pins to output, and configure them low (This ensures the
# motors won't be running

# Pull in details about the gpio pins to use
. ./gpio.sh

# 21 and 18 control motor B
echo "$B0" > /sys/class/gpio/export
echo "out" > /sys/class/gpio/gpio$B0/direction

echo "$B1" > /sys/class/gpio/export
echo "out" > /sys/class/gpio/gpio$B1/direction

# 17 controls the 'standby pin' which enables/disables the motors
echo "$STANDBY" > /sys/class/gpio/export
echo "out" > /sys/class/gpio/gpio$STANDBY/direction

# 4 and 25 control motor A
echo "$A0" > /sys/class/gpio/export
echo "out" > /sys/class/gpio/gpio$A0/direction

echo "$A1" > /sys/class/gpio/export
echo "out" > /sys/class/gpio/gpio$A1/direction

# Set all to 0
echo "0" > /sys/class/gpio/gpio$A0/value
echo "0" > /sys/class/gpio/gpio$A1/value
echo "0" > /sys/class/gpio/gpio$STANDBY/value
echo "0" > /sys/class/gpio/gpio$B0/value
echo "0" > /sys/class/gpio/gpio$B1/value

With everything setup we can now go forwards. The following script supplies power to the motors, wait for a specified number of seconds and then stops the motors again. This reduces the change of the BigTrak driving madly out of control as you try to re-run the 'setup' script to make the motors stop.

#!/bin/sh
# forward.sh

# Read in the GPIO pins to use
. ./gpio.sh

# Setup all the appropiate pins to output, and configure them low (This ensures the
# motors won't be running

# Set all motor to go forwards
echo "1" > /sys/class/gpio/gpio$A0/value
echo "0" > /sys/class/gpio/gpio$A1/value

echo "1" > /sys/class/gpio/gpio$B0/value
echo "0" > /sys/class/gpio/gpio$B1/value

echo "1" >  /sys/class/gpio/gpio$STANDBY/value

# Sleep for the time passed in
sleep  $1

# And stop
echo "0" > /sys/class/gpio/gpio$A0/value
echo "0" > /sys/class/gpio/gpio$A1/value

echo "0" > /sys/class/gpio/gpio$B0/value
echo "0" > /sys/class/gpio/gpio$B1/value
# and enable it
echo "0" >  /sys/class/gpio/gpio$STANDBY/value

So using the above scripts you can make the BigTrak drive forwards for half a second by doing
sh setup.sh
sh forward.sh 0.5

In most cases you will need to be running as the 'root' user to be allowed to access the GPIO pins in this manner, in which case you can prefix the commands with 'sudo' to request permission. For example

sudo sh setup.sh
sudo sh forward.sh 0.5

The scripts for turning left, right and move backwards are similar and an archive containing all these files can be downloaded here.