Difference between revisions of "RPi Projects/PiFace controlling a slot-car"

From eLinux.org
Jump to: navigation, search
 
(22 intermediate revisions by the same user not shown)
Line 8: Line 8:
  
 
a) Put your slot car on the track as always and use your control unit to check if it works.
 
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).
 
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).
 
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.
 
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): <tt>piface/scripts/piface-emulator</tt>
 
e) Start the PiFace-emulator from the terminal (i.e. from your home directory): <tt>piface/scripts/piface-emulator</tt>
 +
  
 
f) You should see this window (in this case its important that the upper two blue lights on the right side are activated):
 
f) You should see this window (in this case its important that the upper two blue lights on the right side are activated):
  
[[File:PiEm_Foto1.jpg]]
+
[[File:PiEm_Foto1.jpg|500 px]]
  
g) This means that the relais is in the default position (picture extracted from the PiFace-manual):
+
 
 +
g) This means that the relay is in the default position (picture extracted from the PiFace-manual):
  
 
[[File:PiF_Relais2.jpg]]
 
[[File:PiF_Relais2.jpg]]
 +
  
 
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:
 
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:
  
[[File:PiEm_Foto2.jpg]]
+
[[File:PiEm_Foto2.jpg|500 px]]
 +
 
 +
 
 +
i) Click "Output Pin 1" again and ensure that the relay is in the default position.
  
i) Click "Output Pin 1" again and ensure that the relais 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.
 
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.
 
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...
 
Well, this way of controlling the slot car doesn't really make fun on the long run, so lets think about some improvements...
 +
  
 
<h3>Getting familiar with controlling the speed of the slot car (difficulty: beginner)</h3>
 
<h3>Getting familiar with controlling the speed of the slot car (difficulty: beginner)</h3>
Line 42: Line 53:
 
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):
 
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):
  
[[File:Breadb2.jpg]]
+
[[File:Breadb2.jpg|400 px]]
 +
 
 +
 
 +
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.
  
b) Reconnect the relais 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 relais 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.
 
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 <tt>"slotc1.py"</tt> in your home directory with the following content:
 
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 <tt>"slotc1.py"</tt> in your home directory with the following content:
Line 66: Line 80:
 
So far we have only one speed level to control, so lets think about some improvements...
 
So far we have only one speed level to control, so lets think about some improvements...
  
<h3>Programmed control with 2 different speed level (difficulty: beginner)</h3>
+
 
According to the design of the relais its quite easy to use one of them as the 'main switch' and the other as the 'speed switch'.
+
<h3>Programmed control with 2 different speed levels (difficulty: beginner)</h3>
 +
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):
 
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):
[[File:Schal2jpg.jpg]]
+
 
 +
[[File:Schal2jpg.jpg|400 px]]
 +
 
  
 
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.
 
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 <tt>"slotc2.py"</tt> and to use the buttons of the PiFace to start and stop it (take care of the indentations marked by the ' symbol):
+
 
 +
c) A more interesting way is to control our car by a python script <tt>"slotc2.py"</tt> and to use the buttons of the PiFace to start and stop it (take care of the indentations):
  
 
  #!/usr/bin/python
 
  #!/usr/bin/python
Line 81: Line 99:
 
  import piface.pfio as pfio
 
  import piface.pfio as pfio
 
  pfio.init()
 
  pfio.init()
  &nbsp;
+
   
 
  button_1=0
 
  button_1=0
 
  button_2=0
 
  button_2=0
 
  interval_a=0.1
 
  interval_a=0.1
 
  interval_b=0.5
 
  interval_b=0.5
  &nbsp;
+
   
 
  print "tap button 1 to start the car and button 2 to stop it"
 
  print "tap button 1 to start the car and button 2 to stop it"
 
  pfio.digital_write(1,0)
 
  pfio.digital_write(1,0)
Line 93: Line 111:
 
   sleep(interval_a)
 
   sleep(interval_a)
 
   button_1=pfio.digital_read(1)
 
   button_1=pfio.digital_read(1)
  &nbsp;
+
   
 
  pfio.digital_write(1,1)
 
  pfio.digital_write(1,1)
 
  print "car started!"
 
  print "car started!"
  &nbsp;
+
   
 
  while button_2 == 0:
 
  while button_2 == 0:
 
   pfio.digital_write(2,1)
 
   pfio.digital_write(2,1)
Line 103: Line 121:
 
   sleep(interval_b)
 
   sleep(interval_b)
 
   button_2=pfio.digital_read(2)
 
   button_2=pfio.digital_read(2)
  &nbsp;
+
   
 
  pfio.digital_write(1,0)
 
  pfio.digital_write(1,0)
 
  print "car stopped"
 
  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 relais via the PiFace-emulator manually afterwards.
+
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...
 
In the moment there's no way to find out, where our car is, so lets think about some improvements...
 +
  
 
<h3>Using a sensor to control the car more precisely (difficulty: beginner)</h3>
 
<h3>Using a sensor to control the car more precisely (difficulty: beginner)</h3>
a) Since the PiFace has inputs that deliver 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.
+
 
 +
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 and to the input GND.
 +
 
  
 
b) Build a sensor like this (feel free with the colours and the material):
 
b) Build a sensor like this (feel free with the colours and the material):
  
[[File:Schal2jpg.jpg]]
+
[[File:PiF_Senso1.jpg|400 px]]
 +
 
  
 
c) We also have to expand our script and change from the 'sleep' to the 'clock'-function for improved time management. <tt>"slotc3.py"</tt> looks like:
 
c) We also have to expand our script and change from the 'sleep' to the 'clock'-function for improved time management. <tt>"slotc3.py"</tt> looks like:
Line 130: Line 152:
 
  last_lap=0
 
  last_lap=0
 
  index_count=1
 
  index_count=1
  &nbsp;
+
   
  time_array=[0,0.1,0.2,0.1,0.3,0.1,0.4,0.1,5,0.1,6]
+
  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
 
  # this array contains the seconds between switching from
 
  # high to low speed and vice versa, first entry isn't used
 
  # high to low speed and vice versa, first entry isn't used
Line 138: Line 160:
 
  sensor_timeout=1.3
 
  sensor_timeout=1.3
 
  # if your sensor gets contact earlier, you may reduce this time
 
  # if your sensor gets contact earlier, you may reduce this time
  &nbsp;
+
   
 
  pfio.init()
 
  pfio.init()
 
  pfio.digital_write(2,0)
 
  pfio.digital_write(2,0)
Line 145: Line 167:
 
  while sensor_1 == 1 :
 
  while sensor_1 == 1 :
 
   sensor_1=pfio.digital_read(1)
 
   sensor_1=pfio.digital_read(1)
  &nbsp;
+
   
  print "flying start... you can stop the car with button 2"
+
  print "flying start... you may stop the car with button 2"
 
  print "laps, total time, lap-time, average lap-time"
 
  print "laps, total time, lap-time, average lap-time"
 
  start_time=clock()
 
  start_time=clock()
 
  lap_start=start_time
 
  lap_start=start_time
  &nbsp;
+
   
 
  while sensor_2 == 0 :
 
  while sensor_2 == 0 :
 
   sensor_2=pfio.digital_read(2)
 
   sensor_2=pfio.digital_read(2)
Line 156: Line 178:
 
   pfio.digital_write(2,change_v)
 
   pfio.digital_write(2,change_v)
 
   actual_time=clock()
 
   actual_time=clock()
  &nbsp;
+
   
 
   while time_array[index_count]+actual_time &gt; clock() :
 
   while time_array[index_count]+actual_time &gt; clock() :
     if lap_start+sensor_timeout &lt; clock() :
+
     if lap_start+sensor_timeout < clock() :
 
       sensor_1=pfio.digital_read(1)
 
       sensor_1=pfio.digital_read(1)
  &nbsp;
+
   
 
       if sensor_1 == 0 :
 
       if sensor_1 == 0 :
 
         laps=laps+1
 
         laps=laps+1
Line 170: Line 192:
 
         index_count=0
 
         index_count=0
 
         change_v=0
 
         change_v=0
  &nbsp;
+
   
 
   index_count=index_count+1
 
   index_count=index_count+1
   if index_count &gt; max_entry :
+
   if index_count > max_entry :
 
     sensor_2=1
 
     sensor_2=1
 
     print "end of programmed array..."
 
     print "end of programmed array..."
  &nbsp;
+
   
 
  pfio.digital_write(2,0)
 
  pfio.digital_write(2,0)
 
  pfio.digital_write(1,0)
 
  pfio.digital_write(1,0)
 
  print "all pins reset"
 
  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.
+
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 your array 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...
  
Up to now I couldn't find out, how long the relais will live if they're mechanically working all the time to control the car, so lets think about some improvements...
 
  
 
<h3>Using opto-relais and transistors to control the car (difficulty: beginner/intermediate)</h3>
 
<h3>Using opto-relais and transistors to control the car (difficulty: beginner/intermediate)</h3>
 
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.
 
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.
+
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:
 
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:
  
[[File:Schal3jpg.jpg]]
+
[[File:Schal3jpg.jpg|500 px]]
  
 
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.
 
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:
 
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:
  
[[File:Schal4jpg.jpg]]
+
[[File:Schal4jpg.jpg|500 px]]
  
 
Take care also to change the output pin no. from 2 to 7 in the python script.
 
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: [http://www.youtube.com/watch?v=zfUCaP7PjrQ| link to youtube.com]
 
d) And here's a little video of the electronical way to control the car: [http://www.youtube.com/watch?v=zfUCaP7PjrQ| link to youtube.com]
 +
  
 
<h3>Further projects (difficulty: intermediate)</h3>
 
<h3>Further projects (difficulty: intermediate)</h3>
 
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:
 
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:
  
 +
[[File:Schal5jpg.jpg|500 px]]
  
By dividing the resistor asymmetrically (i.e. 15 + 35 ohm) we would 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.)
+
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.
 
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:
 
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!),
 
- 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,
 
- 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,
 
- 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,
 
- Replacing the contact sensor with a light sensor,
 +
 
- Add more sensors for even better position control,
 
- Add more sensors for even better position control,
 +
 
- Using the TKinter-library to get a GUI for the python script,
 
- Using the TKinter-library to get a GUI for the python script,
 +
 
- Controlling also a car on the other lane,
 
- Controlling also a car on the other lane,
- Coding a web-interface to control the via http,
 
...
 
  
c) As I just found out, some guys had another interesting RPi/slot-car project last month:
+
- Coding a web-interface to control the car via http...
[http://hackaday.com/2013/01/23/replacement-controllers-for-slot-car-racing/| link to hackaday.com]
+
 
 +
 
 +
c) Similar projects:
 +
 
 +
- As I just found out, some guys had another interesting RPi/slot-car project last month: [http://hackaday.com/2013/01/23/replacement-controllers-for-slot-car-racing/ link to hackaday.com]
 +
 
 +
- Further search for other projects brought me here, where you eventually have to select the 'racingpi.pdf': [https://docs.google.com/folder/d/0B-UAZ9CyJCLGQjJ3RDlqa2pqaDg/edit?pli=1&docId=0B-UAZ9CyJCLGNmpMeVpyYlZJUWc link to pdf made by the guys from the manchester university]
 +
 
 +
 
 +
 
 +
{{Template:Raspberry Pi}}

Latest revision as of 22:36, 16 March 2013

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!

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):

PiEm Foto1.jpg


g) This means that the relay is in the default position (picture extracted from the PiFace-manual):

PiF Relais2.jpg


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:

PiEm Foto2.jpg


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):

Breadb2.jpg


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):

Schal2jpg.jpg


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 and to the input GND.


b) Build a sensor like this (feel free with the colours and the material):

PiF Senso1.jpg


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 may 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 your array 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:

Schal3jpg.jpg

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:

Schal4jpg.jpg

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:

Schal5jpg.jpg

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 car 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 the manchester university