Controlling a Scalextric Car Using a Gertboard
- Overview
- System Configuration
- Bill of Materials
- Set of Instructions
- More Details
- Racing the Scalextric Car
- Trouble Shooting
- Further Development Ideas
- Appendix A - The Gertboard
- Appendix B - The Software
- Appendix C - The Scalextric Primer
- Terminology
- About this Document
Overview
This Project is about using the Gertboard to extend the functionality of the Raspberry Pi (R-Pi) to control the speed of a Scalextric car (note this is for the standard analogue cars and NOT the digital equivalents). The normal Scalextric car controller is replaced by an R-Pi/Gertboard combination. The speed of the car is then controlled using several different hardware/software approaches:
- Controlling the speed via a potentiometer;
- Controlling the speed via the keyboard on the R-Pi;
- Controlling the speed via a browser.
The key learning objectives of this project are for an intermediate/novice developer to learn:
- How to use the LED, push-button switch, ADC and external external motor control features on a Gertboard connected to a R-Pi;
- How to write software that can be used by any browser-enable device e.g. smart phone, tablet, desktop, etc. to access the control the speed of a Scalextric car.
The associated Bill of Materials describes the set of components, hardware and software, required to complete this project. When complete, the system will look as shown in the Figure below. There is a significant amount of information about the Gertboard available on the Element14 Community Gertboard web-site.
The completed system. |
---|
<html><head><title>Jive SBS</title></head> <body><font face="arial,helvetica,sans-serif"> <b>Error</b><br><font size="-1"> An general error occurred while processing your request. </font></font></body></html> |
1. System Configuration
The system configuration is shown in Figure 1.1. The basic configuration is:
- R-Pi (Model A/B) - this project can make use of either Model. In both cases it is recommended that a WiPi is used to provide network connectivity (wireless, IEEE 802.11, via the USB2 port on the R-Pi). Network connectivity is required to provide access to the R-Pi Web server and to download and install the necessary software updates required by the Gertboard software. A USB2 hub is recommended to enable more than two USB devices to be linked to the R-Pi (the R-Pi has two USB ports). It is assumed that the keyboard and mouse are linked via the USB Hub and, if necessary, the WiPi. The monitor can be linked either via the HDMI interface or the yellow RCA phono connector for composite video output. In this project a PiView connector is used to enable a monitor with a VGA connector to be connected to the R-Pi via the HDMI interface;
- Gertboard - a single Gertboard is connected to the GPIO interface on the R-Pi. The Gertboard configurations vary depending on the input/output operations required. For this set of instructions the Gertboard will be configured, separately, to drive the internal/external Light Emitting Diodes (LEDs), the external switch/button, the external potentiometer and the motor controller interface. The Gertboard User Manual (Revision 2.0 published in 2012 by Fen Logic Ltd) is available at: Gertboard User Manual. This is required reading;
- LEDs, Switch and Potentiometer (Pot) and Motor Controller circuitry - the 6 LEDS are wired so that they can be driven as outputs via the Gertboard buffers. The LEDs used in this project have a forward voltage of between 2.0V-2.2V. The external switch is a 'Push-to-Make' button and also connected to the Gertboard buffer. The 'Pot' is wired to the Analogue-to-Digital Converter (ADC) interface on the Gertboard. The Scalextric track and the Variable DC Power Supply are connected to the Motor Controller interface on the Gertboard.
The basic approach is to use the Motor Controller interface on the Gertboard to vary the voltage supplied to the Scalextric track i.e. to replace the usual Scalextric car controller. The voltage is varied via the R-Pi and the use of Pulse Width Modulation (PWM) output from the R-Pi and supplied to the Gertboard through the GPIO interface. The PWM signal is applied to the voltage supplied by the Variable DC Power Supply (the variability of this power supply is NOT used to vary the voltage to the Scalextric cars) and the resulting output voltage from the Motor Controller interface on the Gertboard applied directly to the Scalextric track.
Figure 1.1 System configuration. |
---|
When the complete system is built it will look as shown in Figure 1.2. The views shown in Figure 1.2 are full view (a) and close-up (b).
Figure 1.2 The completed system. | |
---|---|
(a) Full view. |
(b) Close-up view. |
2. Bill of Materials
2.1 System Configuration
The architecture is based upon the use of an R-Pi to drive a Gertboard with the software controlled by access through a web browser. The configuration is shown in Figure 2.1 and the key to the alphanumeric labels is provided in Tables 2.1 and 2.2.
Figure 2.1 System configuration for the bill of materials. |
---|
<html><head><title>Jive SBS</title></head> <body><font face="arial,helvetica,sans-serif"> <b>Error</b><br><font size="-1"> An general error occurred while processing your request. </font></font></body></html> |
The associated bill of materials for the system shown in Figure 2.1 is listed in Table 2.1.
Table 2.1 - the Bill of Materials.
ID | Part | Comment | Qty | Order Code |
---|---|---|---|---|
1(a) | R-Pi Model A | Either Model can be used (includes 4GB SD Card). Use a Multicomp enclosure to protect the R-Pi base. | 1 | Raspberry Pi Model ARaspberry Pi Model A |
1(b) | R-Pi Model B | Use a Multicomp enclosure to protect the R-Pi base. Can include a Cat 5 Ethernet cable. | 1 | Raspberry Pi Model BRaspberry Pi Model B |
1(c) | R-Pi Enclosure | Multicomp enclosure to protect the R-Pi base. | 1 | PR-Pi EnclosurePR-Pi Enclosure |
1(d) | SD Card | Storage medium (8GB) for the R-Pi (with NOOBS pre-installed). | 1 | SD CardSD Card |
2 | Gertboard | The external board, connected to the R-Pi using the GPIO interface, responsible for controlling the speed of the Scalextric car. | 1 | GertboardGertboard |
3 | Keyboard | Include a USB cable. Get as part of an Accessory Bundle (see 4 below). | 1 |
Accessory BundleAccessory Bundle |
4 | Mouse | Include a USB cable. Get as part of an Accessory Bundle (see 3 above). | 1 | |
5 | WiPi | Wireless access via USB. Required to provide network access if a R-Pi Model A is used. | 1 | WiPiWiPi |
6 | PiView | HDMI/VGA adapter to provide connection to a non-HDMI enabled monitor. | 1 | PiViewPiView |
7 | Monitor | A suitable colour monitor (VGA or HDMI connectivity recommended). | 1 | N/A |
8 | USB2 Hub | Includes the power supply and USB2 cables. | 1 | USB2 HubUSB2 Hub |
9 | Power Supply | R-Pi power supply with 5V/1A output. Micro USB connector. | 1 | R-Pi Power SupplyR-Pi Power Supply |
10 | Power Supply | USB2 power supply (5V/1A with 2.1mm x 5.5mm Barrel Plug connector). Included with the USB2 Hub (see 8 above). | 1 | N/A |
11 | Browser-based Devices | Tablet Computers, Smart Phones, Laptops and Desktop Computers. | 1 | N/A |
12 | Scalextric Set | The Scalextric Set consisting of the Track, Cars, Powerbase and Car Controllers. | 1 | N/A |
13 | Variable DC Power Supply | The power supply that will be modulated by the Pulse Width Modulation signal from the R-Pi/Gertboard to provide the voltage to drive the Scalextric car. | 1 | Variable DC Power SupplyVariable DC Power Supply |
14 | LEDs | 2 Red, 2 Green and 2 Yellow 5MM/2V LEDs. | 2 2 2 | Red LEDsRed LEDs Green LEDsGreen LEDs Yellow LEDsYellow LEDs |
15 | Resistors | 60-100 Ohm resistors to drop the 3.3V voltage on the Gertboard to the 2.2 V for the LEDs. | 3 | N/A |
16 | Switch | Multicomp 'Push-to-Make' switch. 28mm long and 7mm diameter. | 1 | SwitchSwitch |
17 | Potentiometer | A linear/radial analogue potentiometer. | 1 | Rotary PotentiometerRotary Potentiometer |
18 | 2-Pin Headers | These pins (2.54mm) are used to provide connection to the external LEDs and switches. | 4 | 2-Pin Header2-Pin Header |
19 | Wires | A 2m mixture a red/black single core wires of wiring of the LEDs, Switch, Potentiometer and the Variable DC Power Supply. A set of longer wiring straps (8cm) for connecting on the Gertboard. | 1 12 | N/A N/A |
20 | Copper Strip Board | Used to hold the LEDs, switches and resistors to create the display and the switch mount units. | 1 | N/A |
2.2 Hardware Configuration
The hardware connectivity is detailed in Table 2.2.
Table 2.2 - Hardware connectivity.
ID | Connector | Connection | Comment |
---|---|---|---|
A | VGA (PiView) | Monitor/R-Pi | Part of the Monitor cable to the R-Pi. |
B | HDMI (PiView) | R-Pi/Monitor | Part of the Monitor cable to the R-Pi. |
C | Power Cable | R-Pi Micro USB/Mains | Power supply for the R-Pi. |
D | GPIO | R-Pi/Gertboard | The Gertboard can be plugged directly into the R-Pi. Alternatively a separate GPIO flat-flex cable (male-to-female). |
E | Power Cable | USB2 Hub | Power for the USB2 hub - the hub supports seven ports. |
F | USB Cable | USB2 Hub/Mouse + Keyboard | USB cable connection for the Mouse and Keyboard. |
G | USB Insert | R-Pi/WiPi | The WiPi will be plugged directly into the R-Pi. |
H | USB Cable | USB2 Hub/R-Pi | USB cable to connect the USB hub to the R-Pi. The R-Pi can also draw power using this cable. |
I | Wires | LEDs wiring Switch wiring Potentiometer wiring Variable DC Power Supply. | External wiring for the LEDs, potentiometer, switch and between the variable DC power supply/Gertboard motor controller. |
J | Wiring Straps | Gertboard/Gertboard | A set of coloured straps ar supplied as part of the Gertboard. More coloured wiring straps are required to support the full range of links. |
K | Jumpers | Gertboard | These are used to configure the various I/O capabilities for the Gertboard. |
L | Wires | Variable DC Power Supply/Gertboard Motor Controller | The red/black wires that are used to connect the positive/negative poles of the peer supply to the MOT+ and GND ports of the Gertboard Motor Control interface. |
M | Modified Scalextric Controller Cable | Gertboard Motor Controller/Scalextric Powerbase | The cable that is used to connect the Gertboard Motor Control interface (MOTA and MOTB ports) to the car controller socket on the Scalextric Powerbase device that supplies the voltage to the track. |
2.3 Software Configuration
The software required to support this project that must be installed on the R-Pi is:
- R-Pi Operating System (Raspbian) - this is installed using the NOOBS that comes pre-installed on the R-Pi SD cards. If NOOBS is not preinstalled it is available at Downloads;
- Integrated Development Environment - IDLE with Python 3.x support with the 'tkinter' modules installed (normally installed as part of Raspbian);
- The Apache2 Web Server - this may be installed as part of the Raspbian operating system. If not, then use the instructions provided in Appendix B1.5.
3. Set of Instructions
The set of instructions for using the Gertboard to control a Scalextric car is:
- Configure the R-Pi system in preparation for the connection of the Gertboard and usage of the software i.e. the monitor, USB2 Hub, keyboard and mouse and the network connection (using either a WiPi for wireless access or using the wired Ethernet port). If the WiPi is to be used then insert the WiPi into one of the USB ports on the R-Pi - see More Detail;
- The next step is to connect the Gertboard to the R-Pi. The Gertboard/R-Pi connection is via the GPIO interface on the top of the R-Pi and the underside of the Gertboard (the layout of the Gertboard is shown in Appendix A). The Gertboard is supplied with a set of 6 plastic rods that can be inserted into the Gertboard so that it can be stood without the underside of the board touching the surface and so that the GPIO interface does not take all of the weight of the Gertboard on the R-Pi. If the Gertboard is to be inserted directly into the R-Pi then do not insert the plastic rod on the bottom left corner of the Gertboard when looking at the underside with the GPIO connector at the bottom (this corner rests over the video jack port). It is possible to use a extra GPIO male/female cable (not supplied as part of the Gertboard) to connect the Gertboard to the R-Pi but in these instructions direct connection is assumed - see More Detail;
- The Scalextric track can now be built. Check that the track and cars are working correctly. The mains transformer must be connected to the Scalextric Powerbase as should the two controllers (see Video 1 for wiring the Scalextric track and car);
- Once it has been confirmed that the track and car are working correctly the next step is to replace one of the standard controllers with the R-Pi/Gertboard approach. The best way to create the new cable between the Gertboard and the Scalextric Powerbase is to cut off the cable from a standard controller (this means that the jack connector for the Powerbase can be used). At the cut end of the cable, tape the Green wire so that it cannot short the other two wires. The Black and Red wires are now ready to be inserted into the Motor Controller interface on the Gertboard. Video 2 demonstrates how the Gertboard/Scalextric Powerbase are wired together - see More Detail;
- Now that the basic system has been wired the next step is to control the speed of the Scalextric car using three different methods: i) a potentiometer, ii) the R-Pi keyboard, and iii) a Web browser;
- To control the Scalextric car using the R-Pi keyboard download and install the software (this is the file 'fsmot-wpv1p0p0.py' on the Element14 Github - see Appendix B3). Once the Python software is started the Scalextric car can be driven around the track by using the 'f' (to go faster) and 's' (to go slower) keys on the R-Pi keyboard (see Video 5) - see More Detail;
- To control the Scalextric car using a potentiometer the ADC pins on the Gertboard are used. The Gertboard has two ADC channels but only one is used. Three wires are used to connect the potentiometer to the ADC channel 0 on the Gertboard (see Video 3 for how to wire the potentiometer configuration for the Gertboard). The Python software required to drive the Scalextric car is now downloaded and installed (this is the file 'potmot-1way-wpv1p0p0.py' on the Element14 Github - see Appendix B2). Once the Python software is started the Scalextric car can be driven around the track by turning the knob on the potentiometer to make the car go faster and slower (see Video 4) - see More Detail;
- When using a Web browser to control the Scalextric car the Gertboard wiring just requires the basic motor controller wiring. The Web browser software must be downloaded and installed (downloaded from the Element14 Github - see Appendix B4). A Web browser is now used to open the Web page 'ScalextricControllerv1p0p0.html' on the R-Pi Web server (this requires the use of the IP address for the R-Pi). The car can now be driven around the track using the Web browser. The next step is to wire the Gertboard to enable the countdown board to be used. The countdown board is required so that the external LEDs and button can be controlled using the Gertboard (see Video 6) to provide a start of race LEDs countdown. The countdown software (either of the files 'countdown-rgv1p0p0.py' and 'countdown-wpv1p0p0.py' is now used to start the race) and the Web browser used to control one of the Scalextric cars (see Video 7) - see More Detail.
4. More Details
4.1 Configure the R-Pi System
The layout of the R-Pi (Model B) is shown in Figure 4.1. Once the various cables have been connected to the R-Pi, the configuration is as shown in Figure 4.2 (the alphanumeric annotations refer to the identifier entries in Tables 2.1 and 2.2). The latest versions of the Raspbian operating system can be installed using the instructions in Appendix B1.1. Make sure that the Git client is installed (see Appendix B1.2).
Figure 4.1 - R-Pi Model board B layout. | Figure 4.2 - The R-Pi with the set of connected cables. |
---|---|
If the WiPi has been inserted, the use of the appropriate wireless network must be established (this requires a wireless network to be activated, via an appropriate wireless network access device e.g. a wireless router, and for that device to be assigning IP addresses using DHCP). This is achieved by working on the desktop:
- Click on the 'wifi config' icon;
- Now click on the 'scan' button;
- From the resulting scanned list pick your wireless network;
- Enter the password for the network in the 'psk' box and click 'Add';
- Click 'Connect'. The wireless network access point will now allocate an IP address to the R-Pi and will display the IP address (for example this will take the form of 192.168.1.99). Make a note of this address as this value will be required in some of the later instructions;
- Test that there is network access by running the Midori web browser to access a web-site e.g. the BBC web-site at: http://www.bbc.co.uk.
Once the above WiPi configuration has been completed, the R-Pi can be rebooted without repeating the above actions. However, each time the R-Pi is rebooted, the DHCP may not allocate the same IP address. In this case the IP address can be obtained by:
- Click on the 'LXTerminal' icon;
- Type the instruction: Hostname -I
The same approach for obtaining the IP address is used if the Ethernet port on the R-Pi Model B is being used.
4.2 Connecting the Gertboard
The Gertboard and the R-Pi are connected via the GPIO connectors. The GPIO interface on the underside of the Gertboard (female) is inserted into the GPIO interface (male) on the R-Pi. Figure 4.3 shows the resulting layout. If the 'Missing' Stand Pin was not missing then this form of connection would NOT be possible and so a separate cable would be required.
Figure 4.3 - Connecting the Gertboard to the R-Pi. |
---|
Once the Gertboard and the R-Pi have been connected then various software should be installed (the installation of the software does not require the Gertboard to be connected to the R-Pi). The software to be installed is:
- The WiringPi Python module - see Appendix B1.3;
- The SPI Dev module - see Appendix B1.4;
- The Apache2 Web Server - see Appendix B1.5;
- The new Gertboard Scalextric control software (from the Element14 Github) - see Appendices B2, B3 and B4.
A jumper should be placed across the top two pins of the three 3V3 pins to ensure that a logic '1' level on the board equates to a voltage of 3.3V (this jumper is not shown in Figure 4.3 but can be seen in Figure 4.4). Figure 4.4 shows the wiring of the DC power supply and the Powerbase cables to the Gertboard's motor controller interface (more detail on the detailed wiring of the Gertboard for this project is given in Appendix A2).
Figure 4.4 - The wiring for the motor control interface on the Gertboard. |
---|
The two wiring straps required to configure the motor controller interface are labelled in Figure 4.4. If a variable DC power supply is used then set it at a supply voltage of 12V as this will not cause damage to the track. A 12V battery pack can be used instead of the variable supply voltage. The power supply needs to be able to provide at least a 0.25A current and a voltage of at least 2V is required before the car will start. It should be noted that if a 10V supply is used that, in general, this does not stop the car from reaching a maximum speed. In many cases, for a 12V supply, the PWM setting for the car will not exceed 300 (note that this has an absolute maximum value of 1023) before the car starts to veer off curves in the track. If a lower voltage is used then a higher PWM is required to achieve the same speed but this will still be well below the 1023 maximum. See Video 2 for a demonstration the wiring of the motor controller interface.
4.3 Using the Potentiometer to Control the Scalextric Car
To control the Scalextric car using a potentiometer the ADC pins on the Gertboard are used. The Gertboard has two ADC channels but only one is used. Three wires are used to connect the potentiometer to the ADC channel 0 on the Gertboard (see Video 3 for how to wire the potentiometer configuration for the Gertboard). The wiring on the Gertboard is shown in Figure 4.5.
Figure 4.5 - Wiring of the Gertboard for potentiometer control of the Scalextric car. |
---|
The Python software required to drive the Scalextric car is now downloaded and installed (this is the file 'potmot-1way-rgv1p0p0.py' on the Element14 Github - see Appendix B2). The program is started using the LXTerminal command:
sudo python potmot-1way-wpv1p0p0.py
Once the Python software is started the Scalextric car can be driven around the track by turning the knob on the potentiometer to make the car go faster and slower as shown in Video 4.
4.4 Using the R-Pi Keyboard to Control the Scalextric Car
The wiring of the Gertboard for control by the R-Pi keyboard requires only the use of the motor controller interface (remove the ADC wires and jumpers in Figure 4.5). However, the software will still work if the wiring for the other modes remains in place (the software ensures that the GPIO pins are used in the correct mode). To control the Scalextric car using the R-Pi keyboard, download and install the software (this is the file 'fsmot-wpv1p0p0.py' on the Element14 Github - see Appendix B3). The program is started using the LXTerminal command:
sudo python fsmot-wpv1p0p0.py
Once the Python software is started the Scalextric car can be driven around the track by using the 'f' or 'F' (to go faster) and 's' or 'S' (to go slower) keys on the R-Pi keyboard (see Video 5). It is a simple edit of the Python program to allow the use of other keys.
4.5 Using a Browser to Control the Scalextric Car
Figure 4.6 shows the full countdown-based Gertboard configuration.
Figure 4.6 - The component wiring for the Web browser Scalextric car control. |
---|
The next step is to build the countdown board to provide a start of race LEDs countdown. The countdown board enables the external LEDs and button to be controlled using the Gertboard. Figure 4.7 shows the countdown board top view (a) and underside view (b) and a demonstration of wiring the board is shown in Video 6. The Gertboard provides a 3.3V source voltage and so a 60-100Ohm drop-down resistor is required for each pair of LEDs (the LEDs have a forward voltage of 2.0-2.2V). Each set of coloured LEDs is wired in parallel (as shown in Figure 4.7b).
Figure 4.7 - The wiring of the countdown board. | |
---|---|
(a) Top view of the countdown board. | (b) Underside view of countdown board. |
The detailed R-Pi/Gertboard wiring when using the countdown board and the Web browser to control the Scalextric car is shown in Figure 4.8.
Figure 4.8 - The R-Pi/Gertboard configuration for the Web browser Scalextric car control. | |
---|---|
(a) | (b) |
(c) | (d) |
The countdown software (either of the files 'countdown-rgv1p0p0.py' and 'countdown-wpv1p0p0.py' is now used to start the race). The LXTerminal application is used to start these programs using the following instructions:
sudo python countdown-wpv1p0p0.py
or
sudo python countdown-rgv1p0p0.py
A Web browser is now used to open the Web page 'ScalextricControllerv1p0p0.html' on the R-Pi Web server (this requires the use of the IP address for the R-Pi - see Appendix B4). The web-page is opened by using the following URL:
http://<Your IP Address>/ScalextricControllerv1p0p0.html
The car can now be driven around the track using the Web browser. A demonstration of using the Web browser to control one of the Scalextric cars is shown in Video 7.
5. Racing the Scalextric Car
All of the videos below are available in medium resolution (a) and high resolution (b) - the high resolution will require, typically, a 3.5Mbps broadband data rate to view in streamed mode.
5.1 Powering the Scalextric Cars Via the Track
A video, 6 minutes long, explaining how the Gertboard will be used to control the speed of the Scalextric by varying the voltage applied to the Scalextric track is shown in Video 1.
Video 1 - Providing the voltage to the Scalextric track. | |
---|---|
(a) High resolution. |
(b) Medium resolution. |
5.2 Wiring the Gertboard Motor Controller
A video, 4 minutes long, explaining how the Gertboard motor controller is wired to support the various control software modes is shown in Video 2.
Video 2 - Wiring the motor controller on the Gertboard | |
---|---|
(a) High resolution. |
(b) Medium resolution. |
5.3 Wiring the Gertboard Potentiometer
A video, 4 minutes long, explaining how the Gertboard is wired for the use of the potentiometer to control the Scalextric car is shown in Video 3.
Video 3 - Wiring of the Gertboard for the use of the potentiometer to control the Scalextric car. | |
---|---|
(a) High resolution. |
(b) Medium resolution. |
5.4 Using the Potentiometer to Control the Scalextric Car
A video, 2 minutes long, explaining how the potentiometer can be used to control the speed of the Scalextric car is shown in Video 4.
Video 4 - Using the potentiometer to control the speed of the Scalextric car. | |
---|---|
(a) High resolution. |
(b) Medium resolution. |
5.5 Using the R-Pi Keyboard to Control the Scalextric Car
A video, 2 minutes long, explaining how the R-Pi keyboard can be used to control the speed of the Scalextric car is shown in Video 5.
Video 5 - Using the R-Pi keyboard to control the speed of the Scalextric car. | |
---|---|
(a) High resolution. |
(b) Medium resolution. |
5.6 Wiring the Countdown Kit
A video, 2 minutes long, explaining how the countdown block is created is shown in Video 6.
Video 6 - Wiring the countdown block. | |
---|---|
(a) High resolution. |
(b) Medium resolution. |
5.7 Using the Browser to Control the Scalextric Car
A video, 2 minutes long, explaining how a browser can be used to control the speed of the Scalextric car is shown in Video 7.
Video 7 - Using a browser to control the speed of the Scalextric car. | |
---|---|
(a) High resolution. |
(b) Medium resolution. |
6. Trouble Shooting
Problem 1 | At runtime, a WiringPi module error is displayed when attempting to run one of the Python software files: 'countdown-wpv1p0p0.py.', 'fsmot-wpv1p0p0.py' or 'pot mot-1way-wpv1p0p0.py'. |
Solution | This error occurs when the WiringPi module has not been installed on the R-Pi. To install the module correctly follow the instructions in Appendix B1.3. Once installed rerun the Python software again. |
Problem 2 | At runtime, a py-spidev module error is displayed when attempting to run the Python 'potmot-1way-wpv1p0p0.py' file. |
Solution | This error occurs when either the 'py-spidev' Python module is not installed or the SPI has not be configured correctly on the R-Pi. To install the 'py-spidev' Python module and to configure the SPI correctly see the instructions in Appendix B1.4. Once installed rerun the Python software again. |
Problem 3 | When attempting to access the demonstration software web-page, 'ScalextricControllerv1p0p0.html', using a web browser, the message 'Server not found' is displayed on the browser. |
Solution | This type of error message occurs when the Web Server that contains the required web-page(s) cannot be found. The possible causes are: i) the R-Pi is not on the network; ii) the device hosting the browser cannot connect with the network to which the R-Pi is connected; iii) the Web Server on the R-Pi is not installed, active or correctly configured. To confirm that the R-Pi is connected to the Internet use the Midora browser on the R-PI to visit a well known web-site. If this access is not possible then confirm that the R-Pi has an IP address (see Section 4.1) and that either the WiPi is correctly installed/working (see Section 4.1) or the Ethernet cable is connected to the R-Pi. You may need to confirm that connection to the Internet is possible through your router (for some networks the relevant MAC address, in this case the MAC address for the R-PI, must be registered with the router - see the corresponding instructions for your router). If there are no network access issues then make sure that the Apache2 Web Server is installed and active on the R-Pi (see Appendix B1.5). |
Problem 4 | When attempting to access the demonstration software web-page, 'ScalextricControllerv1p0p0.html', using a web browser, the message '404 Page not found' is displayed or there is a placeholder were the expected image is missing is displayed on the browser. |
Solution | These errors occur when the Web Server cannot get the web-pages requested by the browser. In this case make sure that the HTML web-page and the three image files are correctly installed/located in the Web Server. See Appendix B4 for more details on installing the web-pages. Also, make sure that you are using the correct file name for the test web page i.e. 'ScalextricControllerv1p0p0.html'. |
Problem 5 | When attempting to access the demonstration software web-page, 'ScalextricControllerv1p0p0.html', using a web browser, the message 'You do not have the access rights' is displayed on the web browser. |
Solution | This error occurs when the web-page being requested is attempting local processing on the Web Server but the Web Server does not have the correct access rights for the files to be processed. The access rights to the files must be changed (see Appendix B4 for the details on how to change the access rights and for which files these rights must be changed). |
Problem 6 | When varying the value of the external potentiometer i.e. twisting the knob, there is NO change in the speed of the car. |
Solution | Make sure that the wire to the wiper pin on the potentiometer (the central pin) is correctly connected. Check that the other two wires are correctly used. |
Problem 7 | When using the countdown board, the LEDs are not lit in the correct sequence (red, red/amber, red/amber flashing, green). |
Solution | This can be resolved by checking that the wiring from the Buffered Output pins on the Gertboard are connected to the correct LEDs 2-Pin headers on the countdown board. If this is correct, then check that the wiring straps between the GPIO pins and the 'B1-B12' In/Out port pins are correct (note that if you have changed the GPIO ports used in the corresponding Python software then the wiring must be altered to be consistent with the software). If only some of the LEDs are working then check that the LEDs are not 'blown' and that the wiring on the Countdown board is sound. Finally, make sure the there jumpers on the 'B2-B4' Output Jumper pins are present. |
Problem 8 | The Scalextric car goes the wrong way around the track to that expected. The car should go in the same direction whether a standard controller or the Gertboard controller approaches are used. |
Solution | The wiring for the Motor Controller interface on the Gertboard is incorrect. The DC power supply should be connected to the GND (-ve) and the MOT+ (+ve) ports. The connection to the Scalextric Powerbase from the Gertboard should have the Black wire connected to the MOTA port (to which the PWM is being applied) and the Red wire to the MOTB port. If the order of those wires has been reversed then the car will travel the wrong way/backwards around the track. |
Problem 9 | When using the potentiometer-based or keyboard-based controller approaches, the car does not move unless the speed value exceeds 140 (this value has a range of 0 ≤ value ≤1023 ). |
Solution | This is to be expected. The drive from the car has to overcome the basic inertia of the motor and the friction of the track. A value of 140, when the DC power source is set at 12V, is sufficient to overcome this inertia. It should also be noted that the gradual increase of the voltage may be insufficient to get the car moving until some tipping point at which the car accelerates rapidly. In this case use a rapid change in the voltage (rapid rotation of the potentiometer or many key/button presses) should be sufficient to make the car move. |
Problem 10 | When using the bowser-based controller approach and when the 'Start' button is pressed, the Scalextric car either does not move or sets off too quickly and 'flies' off the track at the first curve. |
Solution | When the 'Start' button is pressed, the associated software sets the speed value at 250. If the car moves too rapidly then lower this value to approximately 200 and try again (this change requires the Python software in the 'startv1p0p0.py' file in the 'cgi' directory to be edited). Conversely, if the car is moving too slowly then increase the value to 275 and try again. |
Problem 11 | When using the browser-based controller approach, the Scalextric car is very slow to respond to the various button presses. |
Solution | This problem is caused by the response time of the Web server and the network delay encountered between the browser and the R-Pi. In general, a faster local network will have minimal improvements. Avoid having to use any network other than the one in your home i.e. make sure both the device hosting the browser and the R-Pi are on the same wireless network. When using the R-Pi avoid having other software running at the same time as your are using the Web server. The R-Pi is a relatively slow device. One cause of delay is the use of a local file on the Web sever to store the speed setting for the car. An alternative Web service design can be used to avoid having to use local storage (see Further Development Ideas). |
Problem 12 | When using any of the Gertboard-based controller approaches, it is impossible to get the Scalextric car to move. |
Solution | There is no power being supplied from the Motor Controller interface to the Scalextric Powerbase. Check that the wiring on the Motor Controller interface is correct (see Solution 8 above). If a Variable DC Power Supply is being used then make sure that it is switched on. Check that the basic track/car is OK by using a standard controller in the same Powerbase socket (when using a standard controller make sure the mains transformer is connected to the Powerbase). If the car does not move using the standard controller then inspect the car, Powerbase and track to find the problem. |
Problem 13 | When using any of the Gertboard-based controller approaches the Scalextric car moves off and remains at a fixed speed irrespective of the attempts to vary the speed. |
Solution | If the PWM is not working as expected the logic '1' signal on a GPIO port is interpreted as an equivalent PWM value of 1023. Only GPIO18 provides the hardware PWM signal from the R-Pi. A wiring strap is required between the GPIO18 pin and the MOTA Motor Controller pin on the Gertboard. If any other GPIO pin is used then PWM software must be used otherwise the MOTA port on the Motor Controller interface will be logic '1' i.e. interpreted as PWM value 1023. Therefore, check that the two wiring straps for the GPIO17 and GPIO18 pins are connected to the Motor Controller pins MOTB and MOTA respectively. |
Problem 14 | When the Gertboard is connected to the Scalextric Powerbase, the red light on the Powerbase flashes. |
Solution | In this case the cable connections between the Powerbase and the Gertboard's Motor Controller interface are incorrect. It is ONLY the Black and Red wires in the controller cable that are used i.e. isolate/tape the Green wire so that it cannot short the other two wires. The Black wire must be connected to the MOTA port and the Red wire to the MOTB port on the Gertboard. |
7. Further Development Ideas
This project can be developed in a number of ways. Some ideas for you to think about, and work on, are:
- The current Web server approach makes use of a local file to store the speed of the car. Think about an alternative approach where no local storage is required e.g. pass the actual speed in the HTTP call being made from the browser. The current speed must now be stored in the browser and the new speed passed as an input parameter on the HTTP call;
- In the current project the speed of the car is controlled by a human using the R-Pi/Gertboard as a delivery mechanism. Try using just the R-Pi to control the speed of the car. This should be possible using various approaches:
- Require the layout of the track to be entered into the R-Pi. Without the use of sensors to provide information about the position of the car the software will have to per-determine the speed of the car at different times. The R-Pi must then vary the speed according to the elapsed time
- Take the previous approach and add support for external sensors i.e. light and/or pressure sensors, at various points around the track. This information should the be used to confirm actual against expected position on the track. The variation of the speed can the be modified to improve the accuracy of the prediction and hence control of the car
- Instead of using predefined knowledge of the track layout, use a PiCamera to enable the layout to be acquired automatically using image capture and analysis. The speed of the car can then be either predefined or sensor input can be used to refine the speed variation
- Alternatively a PiCamera can be used to provide real-time visual information about the progress of the car around the track (it will not be possible to place the R-Pi/PiCamera on the car itself). This will require real-time vision processing to identify when the car is approaching/leaving a curve;
- At present each R-Pi can only control a single Scalextric Car. To enable a race between two cars would require a second independent R-Pi/Gertboard configuration, for three cars it would need three R-Pi/Gertboard systems, etc. Is it possible to have a single R-Pi control more than one Scalextric Car? A Gertboard has only one Motor Control interface and so a separate Gertboards will be required for each car. If several Gertboard can be linked to a R-Pi (a PiRack will provide the physical connection but control lines will need to be added to enable the R-Pi to control the flow of separate information to each Gertboard). If the control is via a browser then separate users must be supported with concurrent access to the Web Server.
Appendix A - The Gertboard
A1 The Gertboard Layout
The layout of the Gertboard is shown in Figures A1.1a (top view), A1b (top view with detailed jumper pins layout) and A1.1c (underside view). The top view shows the location of the GPIO pins, the buffered input/output pins, the onboard LEDs, the ADC pins, the 3.3V power pins, the motor controller pins and the three onboard switches. The Gertboard provides 12 buffer ports that can be configured for either Input or Output (see Figure A1.1a) and these have a set of LEDs that also show the status of the signal level on the port. The buffer ports are configured as either Input or Output ports using the three sets of input jumper pins (the 'B1..B4', 'B5..B8' and 'B9..B12' In-Jumper pins) and the three sets of output jumper pins (the 'B1..B4', 'B5..B8' and 'B9..B12' Out-Jumper pins) - see Figure A1.1b. Jumpers are placed cross the relevant pins to set the configuration. The buffered ports are linked to the corresponding GPIO ports, and hence the R-PI, using wiring straps between the twelve GPIO Pins (Figure A1.1a) and the twelve 'B1..B12' In/Out Port Pins (see Figure A1.1b). Details for the configuration of the Gertboard for external LED control, external button control and the ADC pins is explained in the Introduction to Using the Gertboard with the R-Pi instructions.
Figure A1.1 - The Gertboard layout. |
---|
(a) Top view. |
(b) Top view for detailed jumper pins layout. |
(c) Underside view. |
The Gertboard is supplied with a set of 6 plastic rods that can be inserted into the Gertboard so that it can stand without the underside of the board touching the surface and so that the GPIO interface does not take all of the weight of the Gertboard on the R-Pi. If the Gertboard is to be inserted directly into the R-Pi then do not insert the plastic rod on the bottom left corner of the Gertboard when looking at the underside with the GPIO connector at the bottom (this corner rests over the video jack port).
A2 The Gertboard Motor Controller
The wiring of the Gertboard for use with the motor controller, LEDs, button and ADC is shown in Figures A2.1a (top view) and A2.1b (schematic top view). This wiring is for the use of the ADC0 channel only. The wiring for this configuration is:
- One short wiring strap is used to link the GPIO25 pins to the 'B1' In/Out port pins (to set as input port). Two long, wiring straps are used to connect the external button to the buffered I/O ports. This defines the external button usage configuration;
- Three, short, wiring straps are used to link the GPIO24-GPIO22 pins to the 'B2-B4' In/Out port pins (to set as output ports). Finally, 6, long, wiring straps are used to connect the LEDs to the buffered I/O ports;
- Three wiring straps are required when the ADC is to be used i.e. Black to the 3.3V pin, Red to the GND pin (line 1 in Figure A3.1b) and Yellow to the ADC0 pin. It is the Yellow wire that must be connected to the wiper on the potentiometer. The connection of the Red and Black wires to the potentiometer determines the direction in which movement changes, increasing/decreasing, the resistance;
- Two, short, wiring straps are used to link the GPIO18-GPIO17 pins to the MOTA and MOTB pins respectively (note that the hardware PWM signal from the R-Pi is only available on the GPIO18 port);
- The Variable DC Power Supply is connected to the 'GND' and 'MOT+' ports on the motor controller. The car controller wires are connected to the 'MOTA' (black wire in the controller cable) and 'MOTB' (red wire in the controller wire) ports on the motor controller. This configuration provides the PWM signal from the GPIO port to the 'MOTA' port on the motor controller.
The associated set of jumpers are (note that no jumpers are required for configuration of the motor controller port):
- Three jumpers on the B2-B4 Out jumper pins sets used to define the output ports to drive the three sets of LEDs;
- One jumper on the B1 In jumper pins set used to define the input ports for the button monitoring;
- Four Jumpers between the pins GP11-SCLK, GP10-MOSI, GP9-MISO and GP8-CSnA are required when the ADC input is used for the external potentiometer;
- A Jumper on the 3V3 pins is used to set the high logic level as 3.3V.
Figure A2.1 - Gertboard wiring for the use of the motor controller, LEDs, button and ADC. | |
---|---|
(a) Top view wiring for use of the motor control, LEDs and button (ADC wiring not present). | (b) Schematic view for use of the motor control, LEDs, button and ADC. |
Further side views of the layout are shown in Figure A2.2.
Figure A2.2 - Side views for the Gertboard wiring for the use of the motor controller, LEDs, button and ADC. | |
---|---|
(a) Side view. |
(b) Side view. |
(c) Side view. |
(d) Side view. |
Appendix B - The Software
B1 System Software
B1.1 Update the Operating System
To ensure that you have the latest version of the operating system and the Python development environment then working in the LXTerminal application type the following instructions:
sudo apt-get update
sudo apt-get install python-get python-pip
This will download and install the latest versions of the software.
B1.2 Install GIT
A lot of the required software is stored in GitHub. Access to this software repository requires the use of Git. To install Git, use the LXTerminal and type the following instructions:
sudo apt-get install git-core
The installation process will inform you if you already have the latest version of Git installed and will not overwrite that latest version.
B1.3 Install the Wiring.Pi Module
Using the LXTerminal application type the following instruction:
sudo clone git://git.drogon.net/wiringPi
To build and install the WiringPi module type:
cd wiringPi
sudo ./build
Once this installation has been completed it can be confirmed by typing the instruction:
gpio -v
This should print to the terminal a list of the states of all the GPIO devices. This list provides a map between the WiringPi Pin Number, the BCM (Broadcom chip) GPIO Pin Number, Physical Pin Number, Mode and Value. Take a copy of this table.
NOTE: If you use the GPIO Utility then be careful because some functions use the 'BCM Pin Number' whereas others use the 'WiringPi Pin Number' by default. Read the GPIO Utility notes carefully.
B1.4 Install and Enable the SPI Dev Module
Using the LXTerminal application, the 'py-spidev' module is installed by typing the following instructions (make sure that Git has been installed):
cd ~
git clone git://github.com/doceme/py-spidev
Once completed, type:
cd py-spidev
sudo python setup.py install
Once the module has been installed, the SPI capability for the R-Pi operating system must be enabled. To enable the SPI driver the file raspi-blacklist.conf file has to be edited and the line 'blacklist spi-bcm2708' must be 'commented out'. Once edited the line should read as (the hash makes the line a comment):
#blacklist spi-bcm2708
The file raspi-blacklist.conf can be edited by typing into the terminal interface:
sudo nano /etc/modprobe.d/raspi-blacklist.conf
If you use the alternative technique of typing into the terminal the instruction:
sudo modprobe spi-bcm2708
then you will have to do this every time the R-Pi is restarted. Now restart the R-Pi.
B1.5 Installation and Configuration of the Apache2 Web Server
The new software written for this project makes use of a Web Server. To install the Apache2 Web Server, using the LXTerminal application type the following instruction:
sudo apt-get install apache2
Now that once the Web Server has been installed it has to be configured to support the running of Python programs. All of the web pages must be placed in the folder /var/www whereas the Python programs must be placed in the folder /usr/lib/cgi-bin. Whereas Apache is already configured to handle '.html' files it must be configured to execute the '.cgi' and '.py' files. Again using the LXTerminal application type the instructions:
cd //
cd /etc/apache2/sites-available
Now make a copy of the current default file so that the original can be reused at a later date.
sudo cp default old_default
Now edit the current 'default' file by starting the editor:
sudo nano edit default
In the section starting ScriptAlias /cgi-bin /usr/lib/cgi-bin/ the following line must be added:
AddHandler cg-script .cgi .py
Save the change and exit the editor. Now restart Apache:
sudo restart apache2
This new line has configured Apache2 so that any script files in the folder /usr/lib/cgi-bin which have a file extension of '.cgi' or '.py' are scripts that can be called and executed from '.html' files.
B2 Potentiometer-based Scalextric Speed Control Software
B2.1 Installing the Software
The software written for this project is available in the Element14 GitHub at: https://github.com/element14/pi_project/tree/master/Gertboard_Scalextric. A clone of this software should be established using an appropriate software versioning application. The clone can be established at: https://github.com/element14/pi_project.git. The Python software used to control the Scalextric car using the potentiometer is contained in the 'potmot-1way-wpv1p0p0.py' file in the directory named 'python'.
B2.2 Using the Software
To run the potentiometer control software use the LXTerminal or double click on the Python program (double clicking will start the IDLE environment). If the LXTermminal is used then make sure that you are in the same directory as the python source file and type:
sudo python potmot-1way-wpv1p0p0.py
This software uses the WiringPi and py-spidev modules and so it requires root access hence the use of 'sudo'. The program will now start and display the configuration requirements for using the ADC input to drive the motor controller. When ready to start the 'enter' key should be pressed. As the potentiometer is turned a line of '#' characters are printed on the screen indicating the voltage level followed by an integer value (in the range 0 ≤ value ≤ 1023) that indicates the input value from the ADC lines. As the rotation of the potentiometer is altered, the voltage level can be increased/decreased appropriately, and so the corresponding speed of the Scalextric car is increased/decreased. The software is designed to run for about 30s or less if the 'CTRL+C' keys are pressed. It is a simple change to allow the software to run for longer periods.
B2.3 Design of the Software
This is a very simple piece of software. It has no classes and consists of three functions and the main code. The three functions are:
- get_adc(channel) - to read the SPI input i.e. the ADC value on the identified channel;
- reset_ports() - to rest the ports for a safe exit from the program;
- display(char, reps, add_value, spaces) - to provide the '#' bar display.
Once the program has started it prints the Gertboard configuration expectations i.e. using GPIO 17 and 18 (with 18 set in PWM mode). Next the user presses 'enter' to start the program. Once started the program samples the ADC channel every 0.05 seconds and writes this value to the GPIO18 port (this is the PWM value). The program will run for 30s or until the 'CTRL+C' keys are pressed. Remember, this software requires the installation of both the WiringPi and py-spidev Python modules.
B3 R-Pi Keyboard-based Scalextric Control Software
B3.1 Installing the Software
The software written for this project is available in the Element14 GitHub at: https://github.com/element14/pi_project/tree/master/Gertboard_Scalextric. A clone of this software should be established using an appropriate software versioning application. The clone can be established at: https://github.com/element14/pi_project.git. The Python software used to control the Scalextric car using the potentiometer is contained in the 'fsmot-wpv1p0p0.py' file in the directory named 'python'.
B3.2 Using the Software
To run the keyboard control software use the LXTerminal or double click on the Python program (double clicking will start the IDLE environment). If the LXTermminal is used then make sure that you are in the same directory as the python source file and type:
sudo python fsmot-wpv1p0p0.py
This software uses the WiringPi module and so it requires root access hence the use of 'sudo'. The program will now start and display the configuration requirements drive the motor controller. The program the requests the user to enter the step value to be use to increment/decrement the speed of the car as the faster/slower keys are pressed. When ready to start the 'enter' key should be pressed. The 'f' key is pressed to make the car go faster and the 's' key to make it go slower. As the 'f' and 's' keys are pressed a line of '#' characters are printed on the screen indicating the voltage level followed by an integer value (in the range 0 ≤ value ≤ 1023) that indicates the input value from the PWM. As the 'f' and 's' keys are pressed the voltage level can be increased/decreased appropriately, and so the corresponding speed of the Scalextric car is increased/decreased. The software will run until either the 'ESC' or the 'CTRL+C' keys are pressed.
B3.3 Design of the Software
This is a relatively simple piece of software. It has no classes and consists of four functions and the main code. The four functions are:
- inkey() - to read a key press;
- setup_ports() - to configure the GPIO system to drive motor using PWM;
- reset_ports() - to rest the ports for a safe exit from the program;
- display(char, pwm_value) - to provide the '#' bar display.
Once the program has started it prints the Gertboard configuration expectations i.e. using GPIO 17 and 18 (with 18 set in PWM mode). Next the program asks the user to supply the increment value (1 ≤ increment ≤ 200) - the software requires a number in the predefined otherwise the user will be prompted again. Next the user presses 'enter' to start the program. The program senses the keyboard every 0.05 seconds. If the 'f' or 'F' keys are pressed the new value for the PWM is used (old value plus increment) and if the 's' or 'S' keys are pressed the new PWM values is calculated (old value minus the increment). The software ensure the PWM value is within the range 0 ≤ PWM < 1023. The software will run unit either the 'ESC' or the 'CTRL+C' keys are pressed.
B4 Web/Browser-based Scalextric Speed Control Software
B4.1 Installing the Software
The software written for this project is available in the Element14 GitHub at: https://github.com/element14/pi_project/tree/master/Gertboard_Scalextric. A clone of this software should be established using an appropriate software versioning application. The clone can be established at: https://github.com/element14/pi_project.git. The software is collected in three folders: 'cgi-bin', 'www' and 'python'. The software files are:
'cgi-bin' folder
- fasterv1p0p0.py - the Python script to increase the speed of the car;
- slowerv1p0p0.py - the Python script to decrease the speed of the car;
- startv1p0p0.py - the Python script to start the car;
- stopv1p0p0.py - the Python script to stop the car;
- pwmvaluev1p0p0.txt - the container for the current speed of the car.
'www' folder
- ScalextricControllerv1p0p0.html - the webpage you see on the remote browser;
- FasterButtonv1p0p0.png - the image for the “FASTER” button;
- SlowerButtonv1p0p0.png - the image for the “SLOWER” button;
- StartButtonv1p0p0.png - the image for the “START” button;
- StopButtonv1p0p0.png - the image for the “STOP” button.
'python'
- countdown-rgv1p0p0.py - the race countdown Python program (using the RP.GPIO module);
- countdown-wpv1p0p0.py - the race countdown Python program (using the WiringPi module);
- fsmot-wpv1p0p0.py - keyboard-based speed controller Python program;
- potmot-1way-wpv1p0p0.py - potentiometer-based speed controller Python program.
The files in the 'www' folder should be copied into the R-Pi directory '/var/www/' and the files in the 'cgi-bin' folder should be copied into the R-Pi directory '/usr/ib/cgi-bin/'. Place the files in the 'python' directory wherever is most convenient to you. The next step is to make sure that these files have the correct access rights i.e. readable and exectable by Apache2. Using the LXTerminal application type the following instructions:
cd //
cd /var/www
sudo chmod 755 ScalextricControllerv1p0p0.html
sudo chmod 755 FasterButtonv1p0p0.png
sudo chmod 755 SlowerButtonv1p0p0.png
sudo chmod 755 StartButtonv1p0p0.png
sudo chmod 755 StopButtonv1p0p0.png
cd //
cd /usr/lib/cgi-bin
sudo chmod 755 fasterv1p0p0.py
sudo chmod 755 pwmvaluev1p0p0.txt
sudo chmod 755 slowerv1p0p0.py
sudo chmod 755 startv1p0p0.py
sudo chmod 755 stopv1p0p0.py
Ensure that the Apache2 Web Server is active.
B4.2 Using the Software
B4.2.1 The Browser-Related Software
To use and test that the software has been correctly installed start a browser on a device that is on the same local network as the R-Pi. In your browser type in the location (the URL for the test page):
http://<Your IP Address>/ScalextricControllerv1p0p0.html
The 'Your IP Address' can be obtained using the LXTerminal application and typing the command:
Hostname -I
If the browser software has been installed correctly the web page show in Figure B4.1 should be displayed on your browser. If not, then read the Troubleshooting instructions.
Figure B4.1 - The start web page displayed for the Scalextric Controller. |
---|
The web-page provides four buttons to control the speed of the car. The speed of the car is shown in the centre cell and this has a value of 0 ≤ speed ≤ 1023 (this is the value that is being sent via the PWM pin). When the 'START' button is pressed the car speed will become 250 and when the 'STOP' button is reseed it will become 0. The 'SLOWER' and 'FASTER' buttons decrease/increase the speed by a value of 10.
B4.2.2 The Countdown Software
The countdown software is used to provide a race countdown. It is available is two form: 'countdown-rgv1p0p0.py' which uses the RP.GPIO Python module; and 'countdown-wpv1p0p0.py' which uses the WiringPi Python module. To start these programs using the LXTerminal application, type:
sudo python countdown-rgv1p0p0.py
or
sudo python countdown-wpv1p0p0.py
Once the software starts the line "Press button to start countdown" is printed to the terminal screen and all the LEDs on the countdown board will be off. The software waits until the button on the countdown board is pressed. Once pressed the statement "Starting countdown" is displayed. The LEDs on the countdown board are now lit in the sequence:
- Only the Red LEDs are on for 2s;
- Both the Red and Yellow LEDs are on for 2s;
- Both the Red and Yellow LEDs are flashing for 2s;
- Only the Green LEDs are on for 6s.
Once complete the statement "Countdown complete" is printed on the screen. The race should start once the Green LEDs are lit.
B4.3 Design of the Software
B4.3.1 The Overall Design
The software for the browser-based control has four components:
- The four Python scripts that control the speed of the car i.e. start, stop, faster and slower. These files are placed in the 'cgi-bin' directory;
- The text file 'pmwvalue.txt' that is used to contain the current PWM value of the car i.e. its speed. The Web Service is a stateless data exchange and so this file is placed on the Web Server as a non-volatile store for that speed thereby removing the need for the speed value to be sent from the browser to the server. This file is also placed in the 'cgi-bin' directory;
- The entry HTML web-page and the associated images that used called by the browser. These files are placed in the 'www' directory;
- The countdown control Python code that is used to control the countdown board.
The countdown control software is invoked using the LXTerminal application whereas the Scalextric control HTML page is invoked using a browser. All of this software is under the R-Pi operating system,
B4.3.2 The HTML Code
When using a Browser the associated Python programs used by the Web Server make use of 'bash' shell commands to achieve the same access to the R-PI's GPIO interface. The core HTML code for the web-page is shown below:
01 <html xmlns="http://www.w3.org/1999/xhtml">
02 <head>
03 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
04 <title>Gertboard Scalextric Car Controller Program</title>
05 </head>
06 <body>
07 <h1 align="center">Scalextric Controller Program</h1>
08 <hr/>
09 <table width="450" border="0" align="center" bgcolor="#CCC">
10 <tr>
11 <td></td>
12 <td><div align="center"><p><a href="/cgi-bin/startv1p0p0.py"><img src="StartButtonv1p0p0.png" width="120" height="75" alt="Start Button"/></a></p></div></td>
13 <td></td>
14 </tr>
15 <tr>
16 <td><div align="center"><a href="/cgi-bin/slowerv1p0p0.py"><img src="SlowerButtonv1p0p0.png" width="120" height="75" alt="Slower Button"/></a></div></td>
17 <td bgcolor="#FFF"><div align="center"><p><b>Speed</b><br/>0</p></div></td>
18 <td><div align="center"><a href="/cgi-bin/fasterv1p0p0.py"><img src="FasterButtonv1p0p0.png" width="120" height="75" alt="Faster Button"/></a></div></td>
19 </tr>
20 <tr>
21 <td></td>
22 <td><div align="center"><p><a href="/cgi-bin/stopv1p0p0.py"><img src="StopButtonv1p0p0.png" width="120" height="75" alt="Stop Button"/></a></p></div></td>
23 <td></td>
24 </tr>
25 </table>
26 <div align="center"><p>To control your Scalextric Car use the buttons above.</p></div
27 <hr/>
28 </body>
29 </html>
The key features are lines 12, 16, 18 and 22 which define the code that is to be run on the Web Server when the associated 'button' on the web-page is pressed. For example, in line 12, the image StartButtonv1p0p0.png' is used to invoke the Python code in the file 'startv1p0p0.py' in the 'cgi-bin' directory. When a user presses the image, an HTTP call is sent to the Web Server which upon receipt runs the corresponding Python program. In this case it causes the car to start. Once competed the Python code redraws the web-page on the browser in preparation for another user action. The speed of the car is written by line 17.
B4.3.3 The CGI Code
The 'cgi-bin' folder contains four Python scripts:
- fasterv1p0p0.py - the Python script to increase the speed of the car. Writes the new incremented value for the PWM into the pwm text file and applies the value to the GPIO18 pin. The Scalextric controller web page is now displayed with the new speed and the car should accelerate to that new speed;
- slowerv1p0p0.py - the Python script to decrease the speed of the car. Writes the new decremented value for the PWM into the pwm text file and applies the value to the GPIO18 pin. The Scalextric controller web page is now displayed with the new speed and the car should slow to that new speed;
- startv1p0p0.py - the Python script to start the car. Writes the initial value for the PWM into the pwm text file and applies the value to the GPIO18 pin. The Scalextric controller web page is now displayed with the new speed and the car should start to move;
- stopv1p0p0.py - the Python script to stop the car. Writes the value '0' for the PMW into the pmw text fie and applied the value to the GPIO 18 pin. The Scalextric controller web page is now displayed with the 'speed=0' and the car should come to a halt.
The web page display control is contained in each of the four scripts so that the format can be varied. If no variation is required then this code could be removed and placed into a single common Python file that is linked to the others. All of the Python scripts control the settings of the GPIO pins using 'bash' instructions. The 'pmwvalue.txt' file is the non-volatile store on the Web Server for the speed of the car.
B4.3.4 The Countdown Code
The code for the countdown control is very simple. The only complexity is to ensure that the GPIO pins used to drive the LEDs and monitor the button are not 17 and 18 (these are used for the motor controller). GPIO 25 is assigned to the button and GPIO 24, 23 and 22 to the Red, Yellow and Green LEDs.
Appendix C - Scalextric Primer
In this project we are using the original analogue Scalextric cars. The newer digital cars have different electrical characteristics and so the approach used in this project will not work for digital cars.
C1 The Cars
The Scalextric cars consist of a plastic shell for the car frame that contains the electric motor to drive the rear wheels. This is a standard 12-20V DC brush motor depending on the type of car. Between the front wheels is the slot rider surrounded by the metal track brushes that supply the drive voltage to the motor. A traction magnet is placed towards the rear of the car to reduce the likelihood of the car skidding out of control when curves are encountered at high sped.
C2 The Car Controllers
The wiring for a Scalextric Car Controller that is supplied with a Scalextric Kit is shown in Figure C1.1.
Figure C1.1 - Wiring for a Scalextric Controller. |
---|
<html><head><title>Jive SBS</title></head> <body><font face="arial,helvetica,sans-serif"> <b>Error</b><br><font size="-1"> An general error occurred while processing your request. </font></font></body></html> |
The controller handle has a lever that is used to vary the value of the potentiometer enclosed in the controller. The controller is connected to the Scalextric track via the Scalextric Powerbase box. The Powerbase has two jack connectors, one for each of the car controllers, and the socket for the mains transformer (the transformer provides the 12V-20V DC supply for each of the controllers). The controller cable consists of there coloured wires: red, black and green. The wires are used to:
- Red - provide the ground voltage from the Powerbase (-ve) and this is not connected to the potentiometer. When the wiper is connected to this wire it acts as a break to the car;
- Green - provide the positive voltage from the Powerbase (+ve) to the potentiometer. Note that this wire is not connected to the actual Scalextric track;
- Black - is connected to the wiper on the potentiometer (+ve).
The power to the Scalextric car, via the track, is supplied via the Red (-ve) and Black (+ve) wires.
C3 The Track
The standard Scalextric track parts have two slots and so two cars can be race against each other. If more cars are to be raced at the same time then multiple tracks are used in parallel. The power to the track is supplied via the Powerbase. The mains transformer is connected to the Powerbase (the transformer produces the 12V-20V DC supply). Each Powerbase has two jack connectors - one for each of the two controllers (this configuration can be used for one or two car racing). When the Powerbase is correctly powered from the transformer the red light is continuously on. If there is a problem with the power to the Powerbase, the red light will flash. The total current used by the track depends on the size of the track: the bigger the track the larger the current required. The typical current drawn is 0.5A-1A. The glider on the underside of the Scalextric car is placed in one of the slots: the wire brushes on either side of the glider now rest on the two metallic strips adjacent to the slot. This now completes the circuit to the DC motor in the Scalextric car. When the controller lever is now squeezed, the corresponding DC voltage is applied to the two metallic strips, and hence the DC motor, and so the Scalextric car moves forward. When the lever is released the DC voltage flow ends and so the car comes to a stop. The speed of the car is varied by increasing/decreasing the squeeze on the lever of the controller.
Terminology
Analogue to Digital Converter | This is the conversion of an arbitrary analogue signal (in the range 0V-3.3V) into its digital equivalent. The ADC capability on the Gertboard is based upon two channels with a sampling rate of 72 samples per second with a maximum digital value of 1023 for an input single of 3.3V. |
DHCP | The Dynamic Host Configuration Protocol (DHP) is responsible for allocating an IP Address to a device from a DHCP Server (typically supported in the local wireless/broadband router that is also connected to the Internet). DHCP avoids having to allocate a device with a specific, unique, IP address. |
Gertboard | The Gertboard is an input/output extension board for the R-Pi. It fits onto the GPIO pins of the R-Pi via a socket on the back of the Gertboard. The Gertboard has a collection of functional blocks which can be connected together, using cable straps and jumpers, in a wide range of ways using header pins. |
Gertboard Motor Controller | The Gertboard has a ROHM BD6222HFP motor controller. This is for brushed DC motors and can handle a maximum 18V and a maximum current of 2A. The speed of the motor is controlled using Pulse Width Modulation (PWM). More information is available in the Gertboard User Manual (page 27). |
GPIO | The General Purpose Input/Output interface that is used to connect the Gertboard to the R-Pi. The GPIO can be driven in several ways depending on what is required and the nature of the software that is driving the interface. In the case of Python-based software the GPIO interface can be driven using the RPi.GPIO and WiringPi packages. |
HDMI | High Definition Multimedia Interface. This is one of the interfaces to the monitor. The PiView is connected to this interface. |
IDLE | Integrated Development Environment for Python. It is completely written in Python and the Tkinter Graphical User Interface (GUI) toolkit (wrapper functions for Tcl/Tk). This is the development environment used for producing the software in this project. |
IP Address | This is the address for a device that enables it to access the Internet. Every device that uses the standard Internet (assuming that it is based upon the Internet Protocol, IP) must have an IP Address that is in the form of '***.***.***.***' e.g. '192.68.2.1'. This is typically allocated through the use of DHCP. |
LED | Light Emitting Diode. A set of 6 LEDs is used in this project. |
NOOBS | New Out Of Box Software. NOOBS is used to install the appropriate version of Operating System (O/S) onto the R-Pi SD card. The Raspbian O/S is used for this project. In many cases, NOOBS is now pre-installed on SD cards that are sold for use with the R-Pi. The latest version of NOOBS is available from the Raspberry Pi Foundation downloads. |
Pulse Width Modulation (PWM) | This is the process by which the width of a square wave, or pulse, is used to modulate a voltage source. Although this modulation technique can be used to encode information for transmission, its main use is to allow the control of the power supplied to electrical devices, especially to inertial loads such as motors. PWM is used in this project to enable the R-Pi/Gertboard to replace the manual Scalextric Car Controller. More information is available on Wikipedia. |
Python | The software in this project is written using Python 3. IDLE is used to support the development of the Python code. |
Raspberry Pi | The R-Pi is the single board computer at the heart of this project. This project will work on both Model A and B versions. |
Scalextric Powerbase | This is the unit that provides the voltage to the Scalextric track. It takes a 12-20V DC input (this is usually supplied via the mains transformer that is supplied with a Scalextric Kit) and provides the connectors for the Car Controllers. These controllers are used to vary the voltage actually applied to the track. The controller contains a potentiometer that is varied by moving the lever in the handle. |
Scalextric Racing (Slot Car Racing) | This is a low voltage model car racing game that uses a slotted track (hence the term slot car racing) to guide the path of the cars. Races are typically between two cars but tracks for greater numbers are regularly used. Further information is available at Scalextric and Wikipedia websites. |
SD | Secure Digital (SD) is a non-volatile memory card format for use in portable devices. This is the storage medium for the R-Pi. |
USB | Universal Serial Bus. |
Variable DC Power Supply | This is a DC voltage source in which the output voltage can be varied. This source is plugged into the mains and so is a convenient way to avoid using batteries. In this project this is used to supply the source voltage for the Scalextric track. |
VGA | A Video Graphics Array (VGA) connector is a three-row 15-pin DE-15 connector. The 15-pin VGA connector is found on many video cards, computer monitors, and high definition television sets. In this project a PiView was used to connect the VGA monitor to the HDMI on the R-Pi. |
Web Server | A Web Server is the software that is responsible for providing access to web pages i.e. pages written in HTML and accessed using HTTP. The R-Pi contains a Web Server. The corresponding web services are accessed by pointing a browser at the web-site using the appropriate combination of the IP Address and the specific directory/file names (this avoids having to register a domain name e.g. 'www.element14.com' but still allows the R-Pi Web Server features to be used). |
WiPi | The device that can connect an R-Pi to a wireless network (using the IEEE 802.11 media access control protocol). The WiPi is inserted into the USB port of the R-Pi. A WiPi must be used for all R-Pi Model A devices otherwise access to the Internet will not be available. |
Wireless Router | This is the router that provides the interconnection between the local wireless network and the broadband Internet access. It is the router that creates the wireless network to which all compatible user devices are connected. |
About this Document
Authors | Colin Smythe (Dunelm Services Limited) | |
Date | 28th April, 2014 | |
Version | 1.0 | |
Copyright | Premier Farnell | |
License | GPLv3+ |