RPi Projects/PiFace controlling a slot-car
As soon as you have connected your RPi with the PiFace and installed the recommended software (python-libs and piface-emulator) you may gain control over a slot car step by step. All you need to begin besides a simple slot car track, the RPi and the PiFace are two isolated wires of approx. 1 m length in different colours and perhaps an extra control unit, since you have to disassemble one.
WARNING: Always keep in mind that your slot car usually works with a AC/DC-transformator that is also connected to a HIGH-voltage socket-outlet with 110V or 220V! If you don't exactly know what you do, you risk your life!
- 1 Simple on/out control over your slot car (difficulty: beginner)
- 2 Getting familiar with controlling the speed of the slot car (difficulty: beginner)
- 3 Programmed control with 2 different speed levels (difficulty: beginner)
- 4 Using a sensor to control the car more precisely (difficulty: beginner)
- 5 Using opto-relais and transistors to control the car (difficulty: beginner/intermediate)
- 6 Further projects (difficulty: intermediate)
Simple on/out control over your slot car (difficulty: beginner)
One useful aspect of the PiFace are the two galvanic isolated relais which we will use for the first steps so the RPi won't get affected by possible mistakes:
a) Put your slot car on the track as always and use your control unit to check if it works.
b) Disconnect your control unit at the plug near the track, disassemble it until you have access to the ends of both wires (or cut the wires with a pincer and strip the last 0.5 cm of the wires).
c) Ensure that the open ends of the two wires are seperated and reconnect the plug (of the former control unit) with the track. Check if the wires are long enough to reach the PiFace, otherwise you need the extra wire mentioned above. If you have a voltmeter you can take the opportunity to measure the voltage (in my case there were 14.8V).
d) Connect the new free ends of the wires just for a moment. If the car moves (and its really fast now, since it gets current for the maximum speed) everything is ok so far. Disconnect the plug from the track again.
e) Start the PiFace-emulator from the terminal (i.e. from your home directory): piface/scripts/piface-emulator
f) You should see this window (in this case its important that the upper two blue lights on the right side are activated):
g) This means that the relay is in the default position (picture extracted from the PiFace-manual):
h) Now click "Override Enable" and then "Output Pin 1" (which controls also relais 1). Assuming that all the jumpers are in the default position you should here a 'tick' from the relais and see this window which indicates that the switch has connected the lower two ports:
i) Click "Output Pin 1" again and ensure that the relay is in the default position.
j) Use a little screwdriver to connect the ends of the two wires to the lower two ports (of relais 1) on the right side (at this time we don't have to think about the polarity) and reconnect the plug to the track again. If nothing happens - you have done well.
k) But as soon as you click "Output Pin 1" once more, the car should move quite fast again, so be prepared to click this pin again or your car will leave the track in the next curve! Don't finally forget to unplug the wires from the track.
Well, this way of controlling the slot car doesn't really make fun on the long run, so lets think about some improvements...
Getting familiar with controlling the speed of the slot car (difficulty: beginner)
For the next step we additionally need some more wire, a breadboard and some low-ohmic resistors (2x10 ohm; 2x30 ohm; 2x50 ohm; 2x80 ohm and 2x100 ohm) since we have to find out, how much power the car needs to drive slowly.
a) Check if the circuit in Part 1.k) still works, then unplug all wires and reconnect them via the breadboard in this way (ignore all the wires without a label, we need them later):
b) Reconnect the relay as you have done in 1.j) and the other wire to the track. Activate "Output Pin 1" shortly to check if the car speeds up again. Then disconnect one of the wires to the relay again.
c) Now we have to find out which resistor is the best to keep the car on the whole track at a reduced speed. Put one end of the resistor in the same row from where you just disconnected the last wire und connect this disconnected wire to the other end of the resistor via a new and unused row. Start i.e. with the 50 ohm and check what happens if you click Pin 1 again (for me 60 ohm were perfect). A potentiometer would also work but for the further parts a resistor suits better.
d) As soon as you have found a 'good' resistor its time to check if we can control the car via python also. Open a text editor and save a file named "slotc1.py" in your home directory with the following content:
#!/usr/bin/python # -*- coding: utf-8 -*- from time import sleep import piface.pfio as pfio pfio.init() pfio.digital_write(1,1) print "car started" sleep(3) pfio.digital_write(1,0) print "car stopped"
e) Check one more time if your relais is in the default state and if your terminal is in the directory of your script. Then type: python slotc1.py If the car is moving for 3 seconds you have 'programmed control' over your slot-car.
So far we have only one speed level to control, so lets think about some improvements...
Programmed control with 2 different speed levels (difficulty: beginner)
According to the design of the relasy its quite easy to use one of them as the 'main switch' and the other as the 'speed switch'.
a) Check, whether you have still all wires connected corresponding to 2.e). Then connect the wires in this way (if you haven't done already, it's time to check out the polarity now):
b) You can test this with the PiFace-emulator by first activating pin 1 and then for a short time pin 2, which makes the car much faster.
c) A more interesting way is to control our car by a python script "slotc2.py" and to use the buttons of the PiFace to start and stop it (take care of the indentations):
#!/usr/bin/python # -*- coding: utf-8 -*- from time import sleep import piface.pfio as pfio pfio.init() button_1=0 button_2=0 interval_a=0.1 interval_b=0.5 print "tap button 1 to start the car and button 2 to stop it" pfio.digital_write(1,0) pfio.digital_write(2,0) while button_1 == 0: sleep(interval_a) button_1=pfio.digital_read(1) pfio.digital_write(1,1) print "car started!" while button_2 == 0: pfio.digital_write(2,1) sleep(interval_a) pfio.digital_write(2,0) sleep(interval_b) button_2=pfio.digital_read(2) pfio.digital_write(1,0) print "car stopped"
If you want to play around with this code please keep in mind that you can also stop the script with the keys (Control-C). Eventually you have to reset the relays via the PiFace-emulator manually afterwards.
In the moment there's no way to find out, where our car is, so lets think about some improvements...
Using a sensor to control the car more precisely (difficulty: beginner)
a) Since the PiFace has inputs that provide a '1' as long this contact is closed, we need two unisolated wires and two isolated ones to connect them to the PiFace input pin 1.
b) Build a sensor like this (feel free with the colours and the material):
c) We also have to expand our script and change from the 'sleep' to the 'clock'-function for improved time management. "slotc3.py" looks like:
#!/usr/bin/python # -*- coding: utf-8 -*- from time import clock import piface.pfio as pfio sensor_1=1 sensor_2=0 laps=0 change_v=0 last_lap=0 index_count=1 time_array=[0,0.2,0.4,0.2,0.4,0.2,0.4,0.2,5,0.2,6] # this array contains the seconds between switching from # high to low speed and vice versa, first entry isn't used max_entry=9 # if you want to use more entries from the array, change this sensor_timeout=1.3 # if your sensor gets contact earlier, you may reduce this time pfio.init() pfio.digital_write(2,0) pfio.digital_write(1,1) # drive the car slowly to the sensor while sensor_1 == 1 : sensor_1=pfio.digital_read(1) print "flying start... you can stop the car with button 2" print "laps, total time, lap-time, average lap-time" start_time=clock() lap_start=start_time while sensor_2 == 0 : sensor_2=pfio.digital_read(2) change_v=1-change_v pfio.digital_write(2,change_v) actual_time=clock() while time_array[index_count]+actual_time > clock() : if lap_start+sensor_timeout < clock() : sensor_1=pfio.digital_read(1) if sensor_1 == 0 : laps=laps+1 lap_start=clock() print laps,(lap_start-start_time), print lap_start-start_time-last_lap,(lap_start-start_time)/laps last_lap=lap_start-start_time actual_time=0 index_count=0 change_v=0 index_count=index_count+1 if index_count > max_entry : sensor_2=1 print "end of programmed array..." pfio.digital_write(2,0) pfio.digital_write(1,0) print "all pins reset"
Remember to adapt the array to your own track and that another car will need another array because of little mechanical differences. Each time the car passes the sensor the array will be read from the first entry. Just try out how long the car will stay on the track with full speed and fill the of your array also by try and error. If you need more than 9 entries you have to adjust the max_entry variable.
Up to now I couldn't find out, how long the relays will live if they're mechanically working all the time to control the car, since there's a warning in the manual: 'Do not write the relay on and off continuously without a substantial delay.' A good reason to think about some improvements...
Using opto-relais and transistors to control the car (difficulty: beginner/intermediate)
The PiFace has also 8 digital outputs, therefore it's possible to control the car with two of these if we can push up the digital signal.
a) An ideal way to learn something about electronics is to use 2 opto-relais (that also isolate the 2 circuits and normally prevent the PiFace from damage) and 2 transistors for the further steps. And naturally we need some more resistors and maybe a LED to show the status of our new circuit.
b) As a first step let's try to control the low speed circuit with 1 opto-relais (where you can ignore pins 3 and 6) and 1 transistor:
You can test this with the PiFace emulator best. It's possible that you have to vary some of the resistors to get optimzed speed, in this case change the resistors in approx. 10% steps to find out, which combination is the best for your hardware. Remember that now we use output pin 8 (not no.1=the relais) if you play around with the python script.
c) The second step is also to bridge the 50 ohm resistor with a transistor. You can add a LED there that lights up when the car goes full speed:
Take care also to change the output pin no. from 2 to 7 in the python script.
d) And here's a little video of the electronical way to control the car: link to youtube.com
Further projects (difficulty: intermediate)
a) Obviously it's possible to divide the 50 ohm resistor (that makes the difference between basic and full speed) to gain more flexibilty with the speed steps. We just have to add one more output pin, opto-relais, transistor etc. as shown here:
By dividing the resistor asymmetrically (i.e. 15 + 35 ohm) we could gain directly two more speed levels (out 6 on + out 7 on = 0 ohm = full speed; out 6 on + out 7 off = 15 ohm =... etc.)
But since all the electronic elements need some power we loose that energy for our car. Therefore you may learn something about digital-to-analog-converters but it's better to optimize the 'time_array' within the python script if you're interested in the max. car speed.
b) A lot of further improvements can be made:
- Modify the script and the circuit a little bit in order to make the PiFace obsolete and to connect the sensor / opto-relais directly to the GPIO pins (but take special care of the RPi's specifications!),
- Expanding the logic of the 'time_array' within the python script to stop the car shortly for some reason,
- Using der first entry of the array to identify the car you're just driving if you have two or more of them,
- Replacing the contact sensor with a light sensor,
- Add more sensors for even better position control,
- Using the TKinter-library to get a GUI for the python script,
- Controlling also a car on the other lane,
- Coding a web-interface to control the via http...
c) Similar projects:
- As I just found out, some guys had another interesting RPi/slot-car project last month: link to hackaday.com
- Further search for other projects brought me here, where you eventually have to select the 'racingpi.pdf': link to pdf made by the guys from man u