Middle – VCC – connect to Pi’s pin 1 (3.3V – I didn’t see it working on 5V)
Right – OUT – connect to any Pi’s GPIO pin
Testing
To see it working, I had my IR receiver working, and I ran
mode2 -d /dev/lirc0
to see the IR receiver output.
I installed the WiringPi library, as it has a very simple way to play with GPIO pins.
So, run on your /home/pi:
git clone git://git.drogon.net/wiringPi
cd wiringPi
./build
You know the GPIO pin where you connected the OUT, right? So, run:
gpio readall
and check what is the WiringPi number got that pin. For example, GPIO 5 (pin 24) is WiringPi number 5.
Check if that pin is on IN or OUT, that is, if it is configured to read or to write. Since we are going to write data, we set it to output with:
gpio mode 5 out
Now I created a bash script with several lines as:
I have a TSOP1838 IR Infrared 37.9Khz Receiver (18m-Range / 2.7~5.5V) that I bought on dx.com.
It comes in a board, and it was a nightmare to understand which pin goes to… I actually tried it wrong and the sensor heated a lot, but luckily it wasn’t ruined.
Furiously googling for any answer, I saw this picture that pointed me into the right direction.
So, with the board facing you with the sensor and the pins pointing down, it is:
Left – GND – connect to Pi’s pin 6
Middle – VCC – connect to Pi’s pin 1 or 2 (works on both 3.3V or 5V, but read further down on this)
Right – OUT – connect to any Pi’s GPIO pin (in the picture, it is on the pin 12, the default one for lirc)
Installing
1) sudo apt-get install lirc
2) add on /etc/modules:
lirc_dev
lirc_rpi gpio_in_pin=18 gpio_out_pin=24
You do not need to set gpio_in_pin and/or gpio_out_pin.
Note that those are GPIO numbers, so they actually match physical Pi’s pins nrs 12 and 18, respectively.
For this IR receiver, the one that matters is the gpio_in_pin.
3) reboot helps. Then do sudo /etc/init.d/lircd stop
Make sure that the daemon is stopped, we do not need it.
4) mode2 -d /dev/lirc0
That will debug what the sensor is seeing. You should see a lot of spaces and pulses if you use your TV’s remote controller and press random buttons into the IR receiver.
I observed that the IR receiver captures a lot of noise signal when it is working on 3.3v. Powering it with 5V eliminates most, but not all of the noise.
Yet, I compiled the C code for the MMA7660 given by this guy here, and it works fine. Nonetheless, I want to use only Python on my project, and since the accelerometer is not a very important sensor for now, I will leave it on the side for now.
Connect both brown & orange wires to an external battery (4.5v or 6V are fine).
If you use the Raspberry Pi to power the servo motor with the 5V pin, it will crash the Pi, as it does not have enough power once the servo starts moving too much.
Connect the signal wire to Pi’s pin 12 (GPIO 18, the PWM pin, or the wiringPi pin 1).
Damn, that thing is light!
So, I attached the sonar to the servo, and then connected the servo motor to a 4.5V battery pack I “created” (not perfect, but a squeeze and a LED signal helps…)
Testing
I used a modified version of this demo code to send loop signals to the servo motor, ranging from 1 to 12, which game me a 180º coverage.
This means that I have a 15º resolution for each sonar capture, which I believe is quite enough. The trick here is to trigger the sonar when the servo is stationary, proceed to the next angle, stop, trigger the sonar, all in the shortest time possible.
From what we can see, on the video, this servo takes approximately 1 second to do the 180º (with 4.5V), so I assume 2 seconds with sonar trigger i a good number. Fine for me.
edit file /etc/modprobe.d/raspi-blacklist.conf and comment out the line blacklist i2c-bcm2708
edit /etc/modules, and add the lines: i2c-bcm2708 i2c-dev
Allow i2c access from users other than root, by creating the file /etc/udev/rules.d/99-i2c.rules with this line: SUBSYSTEM=="i2c-dev", MODE="0666"
Reboot the Pi. When it goes up again, type: ls /dev/i2c*
On my Pi (Model B, Revision 2 version, early 2013) it generates: /dev/i2c-0 /dev/i2c-1
Optional: For python, install the smbus python library with: apt-get install python-smbus
Install Python 3, can’t hurt, and i2clibraries needs it. Just type sudo apt-get install python3
Test if the compass is detected, by typing: i2cdetect -y 1 (for Revision 1 Pis, replace 1 with 0).
Replace with 0 for Revision 1 Raspberry Pis and with 1 for Revision 2 boards. This is the output:
0 1 2 3 4 5 6 7 8 9 a b c d e f
00: -- -- -- -- -- -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- 1e --
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
70: -- -- -- -- -- -- -- --
Tip: if you don’t see it, it’s because you haven’t welded the pins to the sensor. Just press with your finger. Or weld it.
Add the quick2wire code. Pull from git: git clone https://github.com/quick2wire/quick2wire-python-api.git. On /etc/profile, add: export QUICK2WIRE_API_HOME=/home/pi/quick2wire-python-api
export PYTHONPATH=$PYTHONPATH:$QUICK2WIRE_API_HOME
Add i2clibraries. Pull from git:git clone https://bitbucket.org/thinkbowl/i2clibraries.git
Testing
I used this demo code to check the sensor. First, I had to visit http://magnetic-declination.com/ and fix the magnetic declination on the script. Mine was:
You are here*OSLO OSLO
Latitude: 59° 54' 45.8" N
Longitude: 10° 44' 45.9" E
Magnetic declination: 2° 20' EAST
Declination is POSITIVE
Inclination: 72° 43'
Magnetic field strength: 51056.8 nT
I know there are smaller battery packs around that would fit nicely on the trunk of the 4×4 crawler, but mine is a big 5000 mAh solar charger that neeeded a bigger trunk.
I removed the fenders and extended the trunk, like this:
I got more spare parts, and I even have some locks to make sure that the battery pack doesn’t dance while on an off-road course. The IR sensor is still unblocked.
This Lego car is amazing. Beautifully crafted, robust, beautiful, and lots of fun! Can’t ask for a more perfect choice for this project.
I’m planning on leaving the Raspberry Pi on the rooftop, the battery on the trunk, and the bread board either on the hood, or on the cockpit. The camera goes into the front bumper, and the servo with the sonar will be attached to the hood with some Lego bars — it is going to oscillate too much, so it needs a sturdy fixation. Other attachments will be made with velcro — the compass and accelerometer can co on the top of the Pi box.
Now, it’s time to wait for the electronics to arrive, maybe next week. First steps: – investigate the servo bought, how to attach it to the car, and how to control it with the Pi. – investigate Lego’s IR protocol, setup the IR transmitter, and try to control the car with some python controls.
So, time to buy the new Rover Car. On the shopping list:
– A Lego Technic 9398 4×4 Crawler – Raspberry Pi bread board, jumpers, cobbler kits, resistors, LEDs, etc. – Sensors: an ultrasonic sensor, an IR transmitter, accelerometer, compass.
Where does the Raspberry Pi fits in it? And what it the autonomous plan?
The Lego car engines are controlled by an IR receiver. This page has a thorough explanation of the Lego IR receiver contacts and the motor contacts, and how they operate. Basically, the IR receiver channels the 9V from the battery pack into the C1 and C2 channels of the engines, both servo and DC engines, thus making them rotate.
The first idea I had was to put the Raspberry Pi between the IR receiver and the engines. The advantage were:
– I could have a direct control of the engines, just like with the Mk. I Rover car.
yet, the disadvantages were higher:
– I had to cut the motor wires so I could add them into the bread board. I could order some extension wires from the Lego shop, nonetheless.
– We are dealing with 9 V here, and any mistake can cause me to ruin my Raspberry Pi, and I don’t want that.
– No idea on how to still make both manual and automatic controls work, that is, making the Raspberry Pi to contol the car, while still having manual control to override it.
There is a better solution: instead of cutting wires and using the bread board for engine control, why can’t I just use an IR transmitter?
The advantages are:
– No wire cutting. Raspberry Pi sends IR signals to control the engines.
– No need to modify the 4×4 crawler car. Everything stays there.
– Both the Lego controller and the Raspberry Pi controller can send IR signals, so they can both control the car at the same time.
– No risk on ruining the Raspberry Pi. Bread board stuff is only for the sensors, who use voltage that the Pi can handle, no need for 9 V.
The only disadvantage, so far, and not a big one, is that I have to teach the Raspberry Pi to speak “Legolese”, but since Lego kindly published the RC protocol, anyone can reproduce the IR codes.
The next thing is the sensors: how to detect the obstacles around the car? Th first idea was to buy 8 ultrasonic sensors, and placing them on the corners of the car, and on the sides. That idea was scrapped immediately, when I though that a cheap servo could do the trick.
This boebot shows what I have in my mind:
If I attach the ultrasonic sensor to a servo motor, and if I can control its angle, then I can do several measures per second, on a 180º range. That is more than enough to detect big obstacles such as walls or boxes, and it is fine for now.
This is good enough to make sure that the car can drive autonomously without hitting walls. Some Python code can ensure that he knows how to turn and reverse, if such obstacles are found. Yet, not good enough if we want the car to go from point A to point B.
Now, that is the core of Artificial Intelligence research, and that is basically given by whatever Python code I can put in the Raspberry Pi. So, let’s leave it there; in terms of hardware, I believe that an accelerometer and a compass are important, in order to provide a certain awareness of the whereabouts of the car (a GPS receiver wouldn’t be much help on a car that has a speed of 50 cm per second, operating inside buildings). If the car knows his speed and bearings, it can calculate its position from a reference point; given a destination point, he should at least know the bearings where he should be heading.
Now, it’s time to wait for the sensors and Pi accessories to arrive. The Lego car is here, and already assembled, waiting for some Pi sweetness.