mikerr
Posts: 2768
Joined: Thu Jan 12, 2012 12:46 pm
Location: UK
Contact: Website

2 Pi Robots - Sonar, continuous servos and motors

Sat Apr 13, 2013 10:39 pm

Let me introduce my team of 2 robots:

Image

Both using Raspberry Pi / wifi / and cheap Slice of Pi boards for minimal wiring / cost

Red:
- Magician chassis & motors (£15 )
-raspberry pi
-slice of pi (£4)
-HC-SR04 ranger £2
-H bridge chip ( £2 !)

https://www.youtube.com/watch?v=GmRoibgh3L0

Blue:
- Boe Bot chassis - 2 continuous servos on an aluminum chassis
-raspberry pi
-slice of pi (£4)
-HC-SR04 ranger £2
- no driver components needed, just servoblaster sw

https://www.youtube.com/watch?v=HdCb-Lcx9Qw


It's interesting how much easier the Blue is to interface, needing just 2 GPIOs and no external chips,
vs the Red (magician) needing 4 GPIOs and a H bridge.
Also note how very much faster the Red's motors are than the Blue's servos.

Eyes (ultrasonic rangers) are just for show at the moment, until I wire them up tomorrow!
Last edited by mikerr on Sun Apr 21, 2013 9:34 pm, edited 3 times in total.
Android app - Raspi Card Imager - download and image SD cards - No PC required !

mikerr
Posts: 2768
Joined: Thu Jan 12, 2012 12:46 pm
Location: UK
Contact: Website

Re: 2 Pi Robots - continuous servos and motors

Sat Apr 20, 2013 10:33 pm

Thanks to code from joan and Rasathus on this thread:

http://www.raspberrypi.org/phpBB3/viewt ... 37&t=18291

Sonar ultrasonic ranging is now working

https://www.youtube.com/watch?v=GxmJQ1Ki-Fs

My current code is C, but here's an earlier easier to read version in pure python:

Code: Select all

# sonar obstacle avoiding bot in python

# Import required Python libraries
import time
import RPi.GPIO as GPIO
from subprocess import call

GPIO.setmode(GPIO.BCM)

GPIO_TRIGGER = 24
GPIO_ECHO = 25


# Set pins as output and input
GPIO.setup(GPIO_TRIGGER,GPIO.OUT)  # Trigger
GPIO.setup(GPIO_ECHO,GPIO.IN)      # Echo

# Set trigger to False (Low)
GPIO.output(GPIO_TRIGGER, False)

# Allow module to settle
time.sleep(0.5)

def sonar(n):
        # Send 10us pulse to trigger
        GPIO.output(GPIO_TRIGGER, True)
        time.sleep(0.00001)
        GPIO.output(GPIO_TRIGGER, False)

        start = time.time()

       # this doesn't allow for timeouts !

        while GPIO.input(GPIO_ECHO)==0:
                start = time.time()

        while GPIO.input(GPIO_ECHO)==1:
                stop = time.time()

        # Calculate pulse length
        elapsed = stop-start

        # Distance pulse travelled in that time is time
        # multiplied by the speed of sound (cm/s)
        distance = elapsed * 34000

        # That was the distance there and back so halve the value
        distance = distance / 2

        return distance

# all stop
call (["echo 1=0 > /dev/servoblaster"], shell=True)
call (["echo 2=0 > /dev/servoblaster"], shell=True)
time.sleep(2)

while True:

        time.sleep(0.3)

        distance = sonar (0)
        print distance

        if (distance < 10):
                # spin right
                call (["echo 1=200 > /dev/servoblaster"], shell=True)
                call (["echo 2=200 > /dev/servoblaster"], shell=True)
                time.sleep(0.7)
        # run forwards
        call (["echo 1=100 > /dev/servoblaster"], shell=True)
        call (["echo 2=200 > /dev/servoblaster"], shell=True)

Trigger &/ Echo on GPIO 24/25
Continuous servos on GPIO 17/18 (servoblaster 1/2)

Code for the motor version is similar:

Code: Select all

# sonar obstacle avoiding bot in python

# Import required Python libraries
import time
import RPi.GPIO as GPIO
from subprocess import call

GPIO.setmode(GPIO.BCM)

GPIO_TRIGGER = 24
GPIO_ECHO = 25

MLEFT = 17
MRIGHT =18

# Set pins as output and input
GPIO.setup(GPIO_TRIGGER,GPIO.OUT)  # Trigger
GPIO.setup(GPIO_ECHO,GPIO.IN)      # Echo

# Set trigger to False (Low)
GPIO.output(GPIO_TRIGGER, False)

# Allow module to settle
time.sleep(0.5)

def sonar(n):
        # Send 10us pulse to trigger
        GPIO.output(GPIO_TRIGGER, True)
        time.sleep(0.00001)
        GPIO.output(GPIO_TRIGGER, False)

        start = time.time()

       # this doesn't allow for timeouts !

        while GPIO.input(GPIO_ECHO)==0:
                start = time.time()

        while GPIO.input(GPIO_ECHO)==1:
                stop = time.time()

        # Calculate pulse length
        elapsed = stop-start

        # Distance pulse travelled in that time is time
        # multiplied by the speed of sound (cm/s)
        distance = elapsed * 34000

        # That was the distance there and back so halve the value
        distance = distance / 2

        return distance

GPIO.setup(MLEFT, GPIO.OUT)
GPIO.setup(MRIGHT, GPIO.OUT)

sleep (1)

# both motors forward
GPIO.output(MLEFT, 1)
GPIO.output(MRIGHT, 1)

while True:

        time.sleep(0.3)

        distance = sonar (0)
        print distance

        if (distance < 10):
                # spin right
                GPIO.output(MRIGHT, 0)
                time.sleep(0.7)
                # forwards again
                GPIO.output(MRIGHT, 1)

Android app - Raspi Card Imager - download and image SD cards - No PC required !

camillo777
Posts: 26
Joined: Tue Feb 04, 2014 1:21 pm

Re: 2 Pi Robots - Sonar, continuous servos and motors

Wed Feb 26, 2014 2:48 pm

Nice!
Any update on this project?
I would like to do the same as a surveillance robot!

Does the Magician can run on wooden parquet?
Or it will create lines in the wood?

Thank you!

Camillo

mikerr
Posts: 2768
Joined: Thu Jan 12, 2012 12:46 pm
Location: UK
Contact: Website

Re: 2 Pi Robots - Sonar, continuous servos and motors

Wed Feb 26, 2014 5:12 pm

The magician has rubber wheels and a roller bearing on the back - no problem for wood floors
actually runs slightly better on wood than carpet (the bearing drags more on carpet)
Android app - Raspi Card Imager - download and image SD cards - No PC required !

Dibe
Posts: 1
Joined: Sat May 31, 2014 3:03 pm

Re: 2 Pi Robots - Sonar, continuous servos and motors

Sat May 31, 2014 3:10 pm

Great work!

i 've tried to do the blue one with the 2 servos but it seems that i ve got a problem with the sonar.
Sometimes it doesnt start at all,sometimes it just gives 2-3 measurements and then stops again.Any idea what is causing that?
Thank you in advance!

mikerr
Posts: 2768
Joined: Thu Jan 12, 2012 12:46 pm
Location: UK
Contact: Website

Re: 2 Pi Robots - Sonar, continuous servos and motors

Mon Jun 02, 2014 12:29 pm

That's because python or the module sometimes misses the echo
The simplistic code I posted above should have a timeout of some sort for that case.
In my case it would run for a few minutes before missing an echo though.

these lines:

Code: Select all

while GPIO.input(GPIO_ECHO)==0: 
while GPIO.input(GPIO_ECHO)==1:
should be something like:

Code: Select all

timeout = time.time() + 0.05
while GPIO.input(GPIO_ECHO)==0 and time.time() < timeout: 

timeout = time.time() + 0.05
while GPIO.input(GPIO_ECHO)==1 and time.time() < timeout: 
Android app - Raspi Card Imager - download and image SD cards - No PC required !

tjreed
Posts: 2
Joined: Thu Oct 02, 2014 12:40 am

Re: 2 Pi Robots - Sonar, continuous servos and motors

Thu Oct 02, 2014 12:45 am

Hey Mike! Those bots are pretty cool! I am trying to build one of my own. I have all the parts for the red one and I have built the chassis, but I am having trouble putting the rest of it together. Do you think you could send me some info about that? Thank you!

BMS Doug
Posts: 3824
Joined: Thu Mar 27, 2014 2:42 pm
Location: London, UK

Re: 2 Pi Robots - Sonar, continuous servos and motors

Thu Oct 02, 2014 8:12 am

tjreed wrote:Hey Mike! Those bots are pretty cool! I am trying to build one of my own. I have all the parts for the red one and I have built the chassis, but I am having trouble putting the rest of it together. Do you think you could send me some info about that? Thank you!
there shouldn't be too much difficulty with assembling Red, which H bridge chip did you get? (connection details will vary by H bridge).

connect the slice of pi to the raspberry pi (once you have soldered on the header it just sits on top).
Connect the H bridge to 4 of the slice of pi GPIOs
connect the motors to the H bridge
connect the power supply to the H bridge (as you are driving motors it is better not to use the same power supply as the pi is using).
Doug.
Building Management Systems Engineer.

tjreed
Posts: 2
Joined: Thu Oct 02, 2014 12:40 am

Re: 2 Pi Robots - Sonar, continuous servos and motors

Fri Oct 03, 2014 7:42 am

BMS Doug wrote: there shouldn't be too much difficulty with assembling Red, which H bridge chip did you get? (connection details will vary by H bridge).

connect the slice of pi to the raspberry pi (once you have soldered on the header it just sits on top).
Connect the H bridge to 4 of the slice of pi GPIOs
connect the motors to the H bridge
connect the power supply to the H bridge (as you are driving motors it is better not to use the same power supply as the pi is using).
Hi Doug! Thanks for such a quick reply!

I have gotten as far as soldering and connecting the Slice of Pi, but I don't know how to setup the H bridge. The specs for the H bridge can be found here: http://www.hobbytronics.co.uk/motor-con ... r-sn754410

But I don't understand how to connect it properly. If the motors should connect to pins 3 and 7 and to pins 12 and 15, then how do I connect it to the Slice of Pi?

I have a similar problem connecting the Ultrasonic sensor properly. I want to have it on the front of the bot, but the Pi is on the back. Is there a special wiring to get that would allow this connection?

BMS Doug
Posts: 3824
Joined: Thu Mar 27, 2014 2:42 pm
Location: London, UK

Re: 2 Pi Robots - Sonar, continuous servos and motors

Fri Oct 03, 2014 4:04 pm

tjreed wrote:
BMS Doug wrote: there shouldn't be too much difficulty with assembling Red, which H bridge chip did you get? (connection details will vary by H bridge).

connect the slice of pi to the raspberry pi (once you have soldered on the header it just sits on top).
Connect the H bridge to 4 of the slice of pi GPIOs
connect the motors to the H bridge
connect the power supply to the H bridge (as you are driving motors it is better not to use the same power supply as the pi is using).
Hi Doug! Thanks for such a quick reply!

I have gotten as far as soldering and connecting the Slice of Pi, but I don't know how to setup the H bridge. The specs for the H bridge can be found here: http://www.hobbytronics.co.uk/motor-con ... r-sn754410

But I don't understand how to connect it properly. If the motors should connect to pins 3 and 7 and to pins 12 and 15, then how do I connect it to the Slice of Pi?

I have a similar problem connecting the Ultrasonic sensor properly. I want to have it on the front of the bot, but the Pi is on the back. Is there a special wiring to get that would allow this connection?
Hi TJ,

Image

Pins count anti-clockwise from 1 around the chip.

Pins 1, 9 & 16 should be linked to +5 (can be from raspberry pi +5v pin)
Pins 2 & 7 control the direction of motor 1 and should come from two GPIO's (on slice of Pi) programmed as outputs
Pins 3 & 6 go to Motor 1
Pins 4 & 5 should be connected to ground (all grounds from all components should be linked, the motors have no ground connection)
Pin 8 should come from the power supply for your motors
Pins 10 and 15 control the direction of motor 2 and should come from two GPIO's (on slice of Pi) programmed as outputs
Pins 11 and 14 go to Motor 2
Pins 12 & 13 should be connected to ground (all grounds from all components should be linked, the motors have no ground connection)

Ultrasonic: just connect a cable from the ultrasonic connections to your pi, so that you can extend the connections to reach.

I hope this helps.
Doug.
Building Management Systems Engineer.

thamaeng
Posts: 24
Joined: Fri Oct 10, 2014 12:48 pm

Re: 2 Pi Robots - Sonar, continuous servos and motors

Mon Nov 03, 2014 5:11 pm

hey,

i am getting a syntax error: 'return' outside function. what seems to be the problem.

thank you.

Return to “Automation, sensing and robotics”