Tag Archives: rasppi

Mobile computing

Ages ago I had my raspberry Pi set up to run headless and wireless, and had built a motor driver based on an H-bridge, all ready for it to become mobile. In fact it did become mobile by canabilising a remote control dalek toy of my son’s, but that didn’t work too well (it didn’t all fit in). A short while ago I saw a video from @museumsinspire of some Pi-based moving robots which inspired me to try again.

@winkleink pointed me to the parts required–just £8 for the bits I didn’t already have, and after a few weeks wait while they ship from China:2015-09-05 12.23.16
No instructions, which I like: it saves all those “why don’t you read the instructions” arguments. After some help from my daughter2015-09-05 12.52.31

2015-09-05 18.50.40 Soldering connectors to the motors was a real pain. Also, the chassis came with a 4xAA battery, but it was a bit bulky and I fancied 9v for two motors. However that PP3 doesn’t last long.

The pi and its power pack fit no the top in various configurations, I’m still not sure what works best. In the long run I guess I’ll drill some holes so they can be held more securely (sadly but not surprisingly, none of the existing holes are in the right place).
2015-09-06 12.14.39 (The motor controller is in a wee box hidden on the other side of the pi.

I use the GPIO utility that comes with WiringPi for quick tests of the motor driver, to work out which GPIO pin send which wheel forwards or backwards, and also to write short shell scripts such as

#!/bin/bash
#go forwards
gpio mode 0 out
gpio mode 1 out
gpio mode 2 out
gpio mode 3 out
gpio write 1 0 && gpio write 2 0
gpio write 0 1 && gpio write 3 1

to put this little pi bot through its paces

Not a bad start. The battery was running low, it was nippier than that at first. Good enough to build on, I think. Once I’ve worked out where best to put all the parts, and got them fixed a little more securely, there are some other enhancements I would like to make. First, the wheels came with optical encoder discs but no sensors for them, so there is scope for some more precise control of positioning. Secondly there’s a pi camera module on that pi, so some scope for a seeing robot, and it would be nice to add other senses as well.

Compiling OpenCV on Raspberry Pi

swap file for raspberry pi

I want to compile OpenCV on my raspberry pi. I know form previous experience that there isn’t enough memory on board to do that, so I need to make some swap space. Following this post by Jermal Smith, create a 2GB file, make it readable by root only, and make it swap

$ sudo dd if=/dev/zero of=/swap bs=1M count=2048
$ sudo chmod 600 /swap
$ sudo mkswap /swap
$ sudo swapon /swap

free -m shows 2147M of swap free

adding this to /etc/fstab to keep swap after reboot:

/swap   swap   swap   defaults   0   0

Update:
While this seems to work (free and top show the expected memory and swap), either it wasn’t enough, or something wasn’t stable enough to build OpenCV. In the end for the actual compilation I partitioned RaspPi’s SD card to give a more conventional swap partition (and yes, I do know that running a swap on an SD card shortens its life because of the high number of reads & writes, I don’t intend to use it routinely)

Motor driver

Having made the pi wireless and headless, that is not tethered to power socket or keyboard and monitor, what it needs to be mobile is the ability to drive a motor. I had an L293N which allows control of 4 drives at 1Amp at 9v quite easily, that’s enough to power four motors in one direction, or two motors in forwards and back, or one bipolar stepper motor.

The circuit is straightforward, I use a 9v battery to power the motors and (via a voltage regulator and a couple of capacitors to deal with spikes) the L293 — I prefer this to taking power from the pi’s 5v pin, which seemed to be too much for the battery pack the pi was running off. The resistors act to pull the GPIO outputs to ground when they are not set high.

Circuit schematic for two-way control of four motors
Circuit schematic for two-way control of four motors
Physical layout of two-way control of two motors

[Aside: these images were created with Fritzing, which I just discovered looking for a something to draw breadboard prototypes. Getting the schematic was a bit of a faff, but once I realised it was more that a schematic drawing tool I began to think it could be really useful]

I’ve installed WiringPi which gives a nice little command-line utility to set the mode and state of the pi’s GPIO pins, and so is useful for testing.so I knew what pin does what to the motors.

In python I used the RPi.GPIO module to write scripts to run motors as shown above, to test out pwm, and to drive a stepper motor salvaged from an old epson printer.

Example: pulse width modulation for single motor, one-way.

#!/usr/bin/python
import RPi.GPIO as GPIO
from time import sleep
GPIO.setmode(GPIO.BOARD)
gpio1 = 12
GPIO.setup(gpio1, GPIO.OUT)

p = GPIO.PWM(12,50)
p.start(0)
number = 0
old_number = 999

print "Enter number 0..100 to control speed"
print "Enter anything else to stop"

while True:    
    try:
        userInput = int(input())
    except:
        p.stop()
        GPIO.cleanup()
        break
    else:
        number = userInput
        if (number < 101):
            p.ChangeDutyCycle(number)
        else:
            p.stop()
            GPIO.cleanup()
            break

Example 2: stepper motor controller

#!/usr/bin/python
import RPi.GPIO as GPIO
from time import sleep
from sys import argv
from sys import exit

try:
    script, n, p = argv
    nsteps = int(n)
    pausetime = float(p)
except:
    print("usage: stepper2 nsteps pausetimentnsteps (integer) number of motor step to make;ntpausetime (float) time to hold current on each step.nte.g stepper2 10 0.1")
    exit()
    

GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)
gpio0 = 11
gpio1 = 12
gpio2 = 13
gpio3 = 15
gpio4 = 16
GPIO.setup(gpio0, GPIO.OUT)
GPIO.setup(gpio1, GPIO.IN)
GPIO.setup(gpio2, GPIO.OUT)
GPIO.setup(gpio3, GPIO.OUT)
GPIO.setup(gpio4, GPIO.OUT)

a1 = gpio0
a2 = gpio2
b1 = gpio3
b2 = gpio4


def forwardstep(t):
    GPIO.output(a1, 1)
    GPIO.output(b1, 1)
    GPIO.output(a2, 0)
    GPIO.output(b2, 0)
    sleep(t)
    GPIO.output(a1, 0)
    GPIO.output(b1, 1)
    GPIO.output(a2, 0)
    GPIO.output(b2, 0)
    sleep(t)
    GPIO.output(a1, 0)
    GPIO.output(b1, 1)
    GPIO.output(a2, 1)
    GPIO.output(b2, 0)
    sleep(t)
    GPIO.output(a1, 0)
    GPIO.output(b1, 0)
    GPIO.output(a2, 1)
    GPIO.output(b2, 0)
    sleep(t)
    GPIO.output(a1, 0)
    GPIO.output(b1, 0)
    GPIO.output(a2, 1)
    GPIO.output(b2, 1)
    sleep(t)
    GPIO.output(a1, 0)
    GPIO.output(b1, 0)
    GPIO.output(a2, 0)
    GPIO.output(b2, 1)
    sleep(t)
    GPIO.output(a1, 1)
    GPIO.output(b1, 0)
    GPIO.output(a2, 0)
    GPIO.output(b2, 1)
    sleep(t)
    GPIO.output(a1, 1)
    GPIO.output(b1, 0)
    GPIO.output(a2, 0)
    GPIO.output(b2, 0)
    sleep(t)

def stop():
    GPIO.output(a1, 0)
    GPIO.output(b1, 0)
    GPIO.output(a2, 0)
    GPIO.output(b2, 0)

def init():
    GPIO.output(a1, 1)
    GPIO.output(b1, 0)
    GPIO.output(a2, 0)
    GPIO.output(b2, 0)
    sleep(1)
    stop()

for i in range(1,nsteps):
    forwardstep(pausetime)
stop()

Wireless

Headless Pi

Connecting the Pi to keyboard, mouse and screen was a right pain: there’s limited room on my desk that is already occupied by the keyboard mouse and screen of another computer. Also, it seems that one of better things to do with a Pi is to stick it on wheels so it can drive itself around, in which case physical connections have to go.

So, I installed tightvnc, following the guide at Penguin tutor. I use xtightvncviewer on my main computer. I’ve written a two-line bash script so that it connects from the command line just by typing pi. On my tablet I’ve got android vnc viewer (but haven’t yet used it much).