Balancing Robot


184 posts   Page 3 of 8   1, 2, 3, 4, 5, 6 ... 8
by pygmy_giant » Wed Jun 06, 2012 9:21 pm
You could take the crown for the first pi based robot - I'm not aware of any others...
Ostendo ignarus addo scientia.
Posts: 1569
Joined: Sun Mar 04, 2012 12:49 am
by hunternet93 » Wed Jun 06, 2012 9:24 pm
pygmy_giant wrote:You could take the crown for the first pi based robot - I'm not aware of any others...

I'm working on one. At least, I will be if my Pi ever gets here...
Posts: 319
Joined: Mon Dec 12, 2011 4:34 pm
by pygmy_giant » Fri Jun 08, 2012 10:55 pm
Got my Pi a week ago but struggling with installing the I2C drivers atm -

Once the Debian image is written to ones SD card and one has added a suitable config file to get HDMI working I believe one has to update firmware from here:

https://github.com/raspberrypi/firmware/downloads

and then install drivers / kernel patch from here:

http://www.frank-buss.de/raspberrypi/kernel-rpi-w1.tgz
http://www.frank-buss.de/raspberrypi/modules-rpi-w1.tgz

as described here:

viewtopic.php?f=44&t=6649

after setting up a root pasword and logging in as root (read whole thread before attempting)...

...the problem I'm having is that updating the firmware causes me to loose mouse control in X... :¬/

- any help appreciated...
Ostendo ignarus addo scientia.
Posts: 1569
Joined: Sun Mar 04, 2012 12:49 am
by pygmy_giant » Sat Jun 09, 2012 10:27 am
sorted it - needed to update lib/modules aswell as the /boot files ... couldn't use hexxeh's updater as I don't have a linux compatible internet connection as yet - I only have a 3g mobile broadband dongle (need to inxtall some bits and bobs to get it working) - have been thinking that it could possibly also be used as a GPS positioning device...?
Ostendo ignarus addo scientia.
Posts: 1569
Joined: Sun Mar 04, 2012 12:49 am
by rew » Sat Jun 09, 2012 8:47 pm
Yes, I have a GPS connected and gpsd and navit running on my 'pi.
(the composite connected 320x200 TFT is a bit small though :-( ).
Check out our raspberry pi addons: http://www.bitwizard.nl/catalog/
User avatar
Posts: 396
Joined: Fri Aug 26, 2011 3:25 pm
by pygmy_giant » Sun Jun 10, 2012 9:08 pm
perhaps you need something like this: http://www.microvision.com/vehicle_disp ... plays.html or this: http://www.aaxatech.com/products/l1_las ... jector.htm
Ostendo ignarus addo scientia.
Posts: 1569
Joined: Sun Mar 04, 2012 12:49 am
by pygmy_giant » Mon Jun 11, 2012 12:25 am
About to attempt my first GPIO experiment using Vishay Semiconductors' TRCT5000L Reflective Optical Sensor with Transistor Output (http://www.vishay.com/docs/83760/tcrt5000.pdf - £1.99 for 10 from ebay) with the intent of mimicing encoder oporation (with shiney foil).

Having researched the forums/wiki ( viewtopic.php?f=26&t=5806&p=77270&hilit=GPIO+phototransistor#p77270 / http://elinux.org/Rpi_Low-level_peripherals ) I think I will connect it as so:

IR LED
connect to gnd and GPIO via resistor
led forward voltage = 1.25v
led forward current = 50ma (60ma max)
resistor = (Vout – Vled)/Iled = (3.3-1.25)/0.005 = 0.41K

IR Phototransistor
connect between GPIO and gnd
add a pull-up resistor between Vcc and GPIO: 10K
add another resistor before GPIO: 1K

I think I will use the following bash script to test:


#!/bin/bash

# set up SOC GPIO 4 and set to output (= pin 7 on header)
echo "4" > /sys/class/gpio/export
echo "out" > /sys/class/gpio/gpio4/direction

# set up SOC GPIO 17 and set to input (= pin 0 on header)
echo "17" > /sys/class/gpio/export
echo "in" > /sys/class/gpio/gpio7/direction

# write output
echo "1" > /sys/class/gpio/gpio4/value


#main loop

read_input='to start'
compare_input=read_input

echo # newline
echo # newline

while [ "$read_input" == "$compare_input" ]
do
compare_input=read_input
read_input='cat /sys/class/gpio/gpio17/value'
echo " ...state is: "
echo read_input
done
echo " - change detected"


# clean up
echo "4" > /sys/class/gpio/unexport
echo "17" > /sys/class/gpio/unexport


- just got to get the right value resistors and I'm good to go... please, somebody stop me if Im about toast my Pi or have errors in my script...
Ostendo ignarus addo scientia.
Posts: 1569
Joined: Sun Mar 04, 2012 12:49 am
by pygmy_giant » Fri Jun 15, 2012 9:47 pm
erm, built it on my breadboard and come to wire it to the header and noticed that the http://elinux.org/Rpi_Low-level_peripherals uses two different numbering conventions for the header pins - 0-25 in the bash script and 1-26 in the diagram - too scared to plug it in and test atm - need to work out how chip pin numbers relate to header pin numbers.....
Ostendo ignarus addo scientia.
Posts: 1569
Joined: Sun Mar 04, 2012 12:49 am
by hunternet93 » Fri Jun 15, 2012 9:52 pm
pygmy_giant wrote:erm, built it on my breadboard and come to wire it to the header and noticed that the http://elinux.org/Rpi_Low-level_peripherals uses two different numbering conventions for the header pins - 0-25 in the bash script and 1-26 in the diagram - too scared to plug it in and test atm - need to work out how chip pin numbers relate to header pin numbers.....

My guess is simply that humans start counting at 1 and computers (usually) start counting at 0. For example, pin 26 in the diagram would be called pin 25 when accessing it from a program.
Posts: 319
Joined: Mon Dec 12, 2011 4:34 pm
by pygmy_giant » Fri Jun 15, 2012 10:41 pm
not sure - think the code relates to the chip pins, which have to be translated into their connections' physical location on the header when connecting wires - http://lwk.mjhosting.co.uk/?p=343

mike's link supports the 4 - 7 correlation in the remarks at the top of the bash script on the wiki - so I think there is a typo elsewhere in the remarks that should read 'GPIO11' instead of 'GPIO0' as there is not a pin 0 on the header diagram on that same page.

I think I am going to take a risk and amend my script with the code numbers relating to the chip pins and use mike's key to translate that into the physical pins on the header.
Ostendo ignarus addo scientia.
Posts: 1569
Joined: Sun Mar 04, 2012 12:49 am
by pygmy_giant » Fri Jun 15, 2012 10:51 pm
Actually, pin number appear to be a point of view: http://lwk.mjhosting.co.uk/?p=343 - there appear to be 3 conventions!

pin 'number' , pin 'name' and BCM2835 chip no....

so, pin (name) 0 = pin (number) 11 = chip number 17 (!)
Ostendo ignarus addo scientia.
Posts: 1569
Joined: Sun Mar 04, 2012 12:49 am
by pygmy_giant » Wed Jun 20, 2012 10:04 am
amended my code and got it working:

Code: Select all
#! /bin/bash

cd

# set up SOC GPIO 4 to output (= pin 7 on header)
echo "4" > /sys/class/gpio/export
echo "out" > /sys/class/gpio/gpio4/direction

# set up SOC GPIO 4 to output (= pin 0 on header)
echo "17" > /sys/class/gpio/export
echo "in" > /sys/class/gpio/gpio17/direction

# write output (set pin 4 high)
echo "1" > /sys/class/gpio/gpio4/value

echo # newline
echo "TRCT5000L test"
echo # newline
echo "Place reflective material by sensor for 1 second or quickly enter 'x' to exit..."
echo #newline
sleep 1

#initialise before main loop
test_enter="any value but x"
read_input=`cat /sys/class/gpio/gpio17/value`
compare=$read_input

#loop until change detected
until [[ "$compare" != "$read_input" || $test_enter = "x" ]]; do
echo "sensing... "
compare=$read_input
read_input=`cat /sys/class/gpio/gpio17/value`
read -t 1 test_enter
done

echo $test_enter
echo "start value: $read_input"
echo "end value: $compare"
echo # newline

# clean up
echo "4" > /sys/class/gpio/unexport
echo "17" > /sys/class/gpio/unexport


but have been over cautious with the resistor values on the phototransistor side of the circuit....

...I know the script works because the loop exits when I short accross the legs of the phototransistor (as if its switched) and a multimeter shows that the IR LED is shining and that the photo transistor is switching when I put paper infront of it, but the voltage change is not enough tho trip the GPIO.... bit more math to do - will post up the circuit diagram when its 100% working...
Ostendo ignarus addo scientia.
Posts: 1569
Joined: Sun Mar 04, 2012 12:49 am
by pygmy_giant » Thu Jun 21, 2012 8:43 pm
Yay! :D it was 100% working after all - I just needed to use kitchen foil instead of paper! I can't be bothered to draw circuit diagram so I will describe again the simple circuit using my own naming convention as making up new ones seems to be all the rage (see diagram below).

IR LED connected to GND (pin i) directly and then to GPIO 4 (pin d) via 470R resistor.

Phototransistor emmiter connected to GND directly (pin i) and collector to 3v3 (pin a) via 10K resistor and GPIO 17 (pin f) via 1k resistor.

______
a o o g I
b o o h I
c o o i I
d o o j I
e o o k I
f o o l |

NB There is a typo in the second comment in my code (4 should be 17 and its an input).

I am happy doing this 4x for my 2 encoders - I intend to fasten a half-foiled disc to the non wheel end of the DC motor's axle and using 2 such detectors at 90 degrees to sense speed and direction.

OK - put that to one side - stepper motor control next (for sonar) - will use GPIO +bash to step...watch this space...

NB - have my eye on DEXOS / PUPPI / RISCOS for the eventual OS for the robot - the 'sensing...' message should tick by every second but if you watch the program run you can see that it 'sticks' from time to time suggesting that the OS gets distracted and starts naval gazing elsewhere - not helpful...
Ostendo ignarus addo scientia.
Posts: 1569
Joined: Sun Mar 04, 2012 12:49 am
by beautifulsmall » Thu Jun 21, 2012 11:24 pm
Im hoping for a camera response with at least 100hz , for object tracking i dont need it to recognise futurama one eye, just a 25cm bright yellow ball, but fast. I implemented the Kalman filter for accelerometer and gyro fusion on a pic but i bet the pi could do it better. Jimi Hendrix
Posts: 20
Joined: Mon Jun 04, 2012 9:57 pm
by pygmy_giant » Sun Jun 24, 2012 10:42 pm
we'll see - Puppi + http://www.open.com.au/mikem/bcm2835/ could provide one suitable combo
Ostendo ignarus addo scientia.
Posts: 1569
Joined: Sun Mar 04, 2012 12:49 am
by pygmy_giant » Tue Jun 26, 2012 10:52 pm
Here is a bash script to demonstrate 28BYJ-48 servo control. No components are required other than the motor, ULN2003 controller board and female to female dupont jumper wires. Total cost off ebay approx £3.00.

Code: Select all
#! /bin/bash

cd

echo # newline
echo "28BYJ-48 Servo test"

# set up SOC GPIO 25, 24, 23, 22 to output
echo "25" > /sys/class/gpio/export
echo "out" > /sys/class/gpio/gpio25/direction
echo "24" > /sys/class/gpio/export
echo "out" > /sys/class/gpio/gpio24/direction
echo "23" > /sys/class/gpio/export
echo "out" > /sys/class/gpio/gpio23/direction
echo "22" > /sys/class/gpio/export
echo "out" > /sys/class/gpio/gpio22/direction

# set timing and no. cycles
delay=0.001
cycles=512

# ripple across pins in 3s
for (( i=1; i<=$cycles; i++ ))
do
echo "0" > /sys/class/gpio/gpio24/value
echo "1" > /sys/class/gpio/gpio25/value
sleep $delay
echo "0" > /sys/class/gpio/gpio23/value
echo "1" > /sys/class/gpio/gpio24/value
sleep $delay
echo "0" > /sys/class/gpio/gpio22/value
echo "1" > /sys/class/gpio/gpio23/value
sleep $delay
echo "0" > /sys/class/gpio/gpio25/value
echo "1" > /sys/class/gpio/gpio22/value
sleep $delay
done

# clean up
echo "0" > /sys/class/gpio/gpio25/value
echo "0" > /sys/class/gpio/gpio24/value
echo "0" > /sys/class/gpio/gpio23/value
echo "0" > /sys/class/gpio/gpio22/value
echo "25" > /sys/class/gpio/unexport
echo "24" > /sys/class/gpio/unexport
echo "23" > /sys/class/gpio/unexport
echo "22" > /sys/class/gpio/unexport
Ostendo ignarus addo scientia.
Posts: 1569
Joined: Sun Mar 04, 2012 12:49 am
by pygmy_giant » Tue Jun 26, 2012 11:33 pm
sorry - I meant stepper not servo!

Next stop I2c ultrasonic ranger to make sonar then battery pack to cut the umbellical...
Ostendo ignarus addo scientia.
Posts: 1569
Joined: Sun Mar 04, 2012 12:49 am
by pygmy_giant » Thu Jun 28, 2012 10:45 pm
Got my battery pack charging - it's made from the lipo's extracted from 8 (Venom) Wii Fit battery packs soldered together in paralell (picked them up for £1 each). From what I can tell, the lipos work at 3v3 but the board switches down the (5v USB) charging voltage and switches back up the output voltage to 5v. The sticker on the box reads 100mA output, so looking at 800mA total output, which should be ok for a headless project?

I'm scared of lipos so I'm charging them in an airtight metal box - google lipo fire and you'll see what I mean.... :o
Ostendo ignarus addo scientia.
Posts: 1569
Joined: Sun Mar 04, 2012 12:49 am
by pygmy_giant » Tue Jul 03, 2012 7:28 pm
Ostendo ignarus addo scientia.
Posts: 1569
Joined: Sun Mar 04, 2012 12:49 am
by pygmy_giant » Mon Jul 09, 2012 12:14 am
am looking at theramin sensing and how it could be adapted to discern the robot's angle to the ground: viewtopic.php?f=37&t=10248&p=119012&hilit=theramin#p119012 -
It has applications in obsticle avoidance and the ground is definately a n obsticle to avoid.
Ostendo ignarus addo scientia.
Posts: 1569
Joined: Sun Mar 04, 2012 12:49 am
by bobc » Mon Jul 09, 2012 9:28 am
Timing in real time control algorithms is important for two reasons. You need to sample and process the inputs many times faster than the rate being controlled. A higher frequency is more useful than higher numerical accuracy. The accuracy only needs to be sufficient for the job, so often fixed point arithmetic is used.

The processes being controlled are usually a rate, ie. parameter per unit time. If you know that your control loop is run at exactly every 10ms say, then you can hard code the time interval and simplify the calculations a lot. You can avoid an expensive divide by delta t.

However, if the time varies - 9.9, 10.1, 10.2 - it introduces an artificial noise into the calculations. You will already have a problem with real noise to deal with. If you can get the actual interval, then you could divide by t, but that costs time. More time means lower rate = bad. The same applies to control outputs, they need to occur at a known time.

For a real-time control application, a high rate and a precise time interval are pretty essential. OTOH Linux doesn't really care about precise intervals, and needs to disable interrupts to ensure correct multi-tasking. Linux also wants to allocate CPU according to demand, rather than give a guaranteed fixed slice to a particular process.

So the challenge for creating real-time apps under a general purpose OS like Linux is finding a way to allow quite different processing regimes to co-exist without interfering with each other.
Posts: 86
Joined: Fri Apr 06, 2012 8:01 am
by morphy_richards » Mon Jul 09, 2012 12:05 pm
Every time I read this thread I keep wondering if it might be an idea to strap an arduino to the raspberry pi to deal with all this crucial timing business and leave the pi for the higher level "cognitive" functions - it seems that an arduino's hardware would be able to handle the timing and that arduino "C" would be able to cope with the maths ... but I wonder if that is just kind of taking an easy way out and missing the point. I do think Pygmy is relishing the challenge of pushing the pi to it's limits with this project .
User avatar
Posts: 860
Joined: Mon Mar 05, 2012 3:26 pm
Location: London
by mikerr » Mon Jul 09, 2012 1:37 pm
Two wheel segway type balancing can be done without gyro / accelerometers to an extent - you only really need to know the angle & direction of fall - and you can get that by measuring the distance to the floor - I've seen them done with just photo resistors.

I did one with arduino and just a cheap(£3) sonar sensor:

Early video here, I later got it to balance longer than that
http://www.youtube.com/watch?v=GNenQ4OWgSM

Arduino does sensors and servo and motor control very easily,
but at a basic level doesn't have anything in the way of video/audio or USB capability like Pi.
(Yes you can buy arduiino addon shields - but that gets expensive quickly IMO).
Got a Pi Camera? View it in my android app - Raspicam Remote ! No software required on the pi
User avatar
Posts: 1190
Joined: Thu Jan 12, 2012 12:46 pm
Location: NorthWest, UK
by pygmy_giant » Mon Jul 09, 2012 8:51 pm
This following thread has restored my faith in the Linux' ability to respond quickly: viewtopic.php?f=37&t=7696&p=102464&hilit=logic+analyser#p102464

I plan to get some C code (incorporating armhf) working via Puppy and then re-package it as a bare metal control system.

Have faith in the Pi
Ostendo ignarus addo scientia.
Posts: 1569
Joined: Sun Mar 04, 2012 12:49 am
by pygmy_giant » Thu Jul 19, 2012 5:26 pm
My motors arrived in the post this morning:

Image

The body is 25mm x 50mm and they run at 6v. They have a planetary gear box that gears down to 300rpm. Lots of torque - I hope they're fast enough! They will be powered via an I2C controlled pwm hbridge which is also in the post. The bot needs a little bit of oomph up its sleeve so it can push its wheels ahead of the centre of gravity before decellerating - I might over-volt them to 7.4v for that.

The sensor is the TRCT5000L Reflective Optical Sensor mentioned earlier. I intend to attach a half-foiled disk to the back end of the motor shaft and use these with the sensors as encoders read via the GPIO. I will only use one per motor as the bot should know what direction its wheels are travelling.

I think I will just use the encoders to match the speed of the wheels on both sides to avoid veering - I think an ultrasonic ranger facing forward could be the best way to measure the distance travelled. I think I might use a magnetometer to measure the angle turned as this would be more accurate than encoder info also.

Construction pics to follow...
Ostendo ignarus addo scientia.
Posts: 1569
Joined: Sun Mar 04, 2012 12:49 am