Introduction: Breathe New Life Into That Old Exercise Bike
The basic idea stems from the fact that the Raspberry Pi computer has
quite a capable graphics processor, ideal for rendering moving scenery and easily accessed digital input and output, ideal for reading pulses from a sensor. So all kinds of things spring to mind but this seemed an obvious one.
Step 1: Outline and Cost
You can buy a Raspberry Pi A+ now for $20 dollars (£16.83 delivered from CPC) and if you don't have a spare computer monitor you can get 2nd hand ones from TV shops for next to nothing (use the "..How much? The computer I'm attaching it to only cost £16 so I can't spend more than £10 on the monitor.." negotiating ploy)
The other bits needed:
soldering iron (+solder and flux)
a hall effect sensor £1.0 to 2.00 I used A3144 such as http://www.ebay.co.uk/itm/190823658126 but other switching types would almost certainly do if they can be driven by 3.3V or 5.0V (such as A3213EUA-T which is available from CPC though I've not tried it)
some cable £2.50 http://www.ebay.co.uk/itm/Multicore-Cable-7-0-22-...
some female connectors £5.50 http://www.ebay.co.uk/itm/271391818023 and http://www.ebay.co.uk/itm/271391818041
some shrink tubing £1.00 http://www.ebay.co.uk/itm/Heatshrink-Thin-Wall-2-...
but of the last few adding up to £9.00 you will have enough for 10 set-ups so the actual price is nearer £0.90 for the connectors and cable!
two buttons £3.00 http://www.ebay.co.uk/itm/Push-Button-Switch-1-5A...
a micro SD card £4.00 you probably have an old 2GB to spare but lots available on line.
Step 2: Software
You need to get the software onto the SD card. You will have to either download the NOOBS raspbian, standard raspbian or minimal raspbian and add the software needed (instructions below) or use the image that I have made. But for all of them you will need to burn an image to your SD card. And the instructions for that are here
the NOOBS system is probably easiest as it doesn't require special software to write to the SD (or use of the scary dd linux command) However you still need to install some software. I will describe the process for raspbian first then what you need to do differently for the other options; read those first before you decide which route is for you!
raspbian (assuming you have just logged in as per instruction as user=pi and at command prompt)
- $ sudo apt-get install python3-dev python3-setuptools libjpeg-dev zlib1g-dev libpng12-dev libfreetype6-dev
- $ sudo apt-get install python3-pip
- $ sudo pip-3.2 install Pillow
- $ sudo pip-3.2 install pi3d
- $ cd ~
- $ git clone https://github.com/paddywwoof/pi3d_scenery.git
- $ sudo raspi-config # change the graphics memory to 128
in which 1. are packages used by pip and Pillow; NB that's a 'one' in zlib1g-dev 2. pip is a handy installer for python packages. On the Raspberry Pi with python3 it can't make up its mind what it should be called. If pip-3.2 and pip3 don't work then search for a file with pip in its name ($ sudo find / -name 'pip*') 3. Pillow is the supported version of PIL the python imaging library. It will probably take a while to compile, go and have a cup of tea. 4. pi3d is the python 3D graphics module 5. just in case your weren't in the home directory 6. copies the scenery stuff and program into a new directory /home/pi/pi3d_scenery/ 7. change the allocation of graphics memory, best done after the compiling stages which run faster with plenty of memory available.
To run the software now you have to log in as user pi then
- $ cd pi3d_scenery
- $ sudo python3 Scenery.py
*NB see the later page on running the software for details of what you need to do
You can make this happen automatically when you power up the Raspberry Pi by editing /etc/inittab i.e. following this suggestion
you need to tell .bashrc to run a bash script that you create in /home/pi/ something like
$ nano launch.sh
#!/bin/bash cd /home/pi/ sudo python3 Scenery.py
$ chmod 755 launch.sh
The advantage of this route is that it produces a much lighter weight SD card so it's quicker to burn. This is what I used to create the download image below. It needs to include the software installed as above but some additional things as well. Essentially the things missing from this SD image are: python, numpy, rpi-gpio and the non-root user pi.
First download the image from here http://www.linuxsystems.it/raspbian-wheezy-armhf-r... and burn it to your SD card using the instructions at the top of this page. You will log in as root, password raspberry so the apt-get install commands don't need to be prefixed by sudo (and there is a # not a $ at the start of each line). Before you do the raspbian installs you need to install python
# apt-get install python3 # apt-get install python3-numpy
then do the raspbian installs up to 4. above but beforeyou install pi3d_scenery and do the autorun changes you need to create a new user called pi following the instructions here http://www.raspberrypi.org/documentation/linux/us...
# adduser pi # visudo
You will also need to install the GPIO module before running the software:
# pip-3.2 install rpi-gpio
ready made image
You should be able to download that from http://www.eldwick.org.uk/files/pi3d_scenery001.z...
and follow the instructions at the top of this page to burn it to SD. You should then have a working copy of the minimal raspbian version above that you can put in your Raspberry Pi and just power up!
Step 3: Wiring It Up and Sticking It Together
Things to watch out for:
The hall effect and magnet polarity only work one way round so do some manual checks before finally sticking them both in position. You could use good quality double sided tape, hot glue gun or good glue for the magnet. Probably not a good idea to use very hot glue on the sensor.
Make sure the sensor and magnet are passing reasonably close but not catching on something. This is especially relevant if you can only access part of a flywheel as on my bike; the edge of the flywheel passed through a constriction where I couldn't see it and I nearly wiped the magnet off!
Remember to thread the shrink tubing onto the wires before soldering (same issue with fixing nuts if you solder the buttons before you attach them (because you can't get at the contact lugs after fitting, say))
Once it's all set up and working you can tidy the wires up with cable ties and so on. If the Raspberry Pi is going to be permanently used for this purpose you can also attach that either in a commercial enclosure, using the holes, gluing it or cable tying it.
Step 4: Running the Software
One critical feature of the software is that it uses python pickling to make the scenery loading faster. However the pickle files are not stored and download from github (as they are specific to the type of python being used). Instead they are generated as a one-off job the first time the program runs. This takes a few minutes on the Raspberry Pi (put the kettle on!) also see the warning below in the change scenes menu options.
The program relies of much of the 'standard' functionality of http://pi3d.github.io so if you're interested in the details that's a good place to start. However the crucial elements are:
- Camera object that moves around and turns depending on inputs from the GPIO pins (if there are any) and key presses (if there is a keyboard) It's position and movement also depends on..
- ElevationMaps. These model the grass, rocks, snow, water etc and each takes at least two images to generate it. A grey-scale elevation image and a coloured texture with option for normal mapping detailed bumps and a reflection.
The version that I have uploaded contains three sets of scenery: karst, fjords and alpine and each has its own set of variables in a file that is imported into Scenery.py (karst.py fjords.py alpine.py) as well as a sub-directory containing the image and 3D model obj files.
There is no reason why you can't make your own version by copying and editing one of the existing ones. Note that the elevation map files need to be 33x33 with the edge pixel values the same as the edge of the adjoining map otherwise there will be cracks and discontinuity between the maps.
When a new set of scenery is loaded (see menu operations below) the program checks to see if there is a file called map00.pkl and if there isn't it goes through the whole list of objects in the scene and generates new pickle files. So if you change something (a texture or elevation image) you need to delete the map00.pkl pickle file to force regeneration.
buttons and keys
If buttons are connected to the GPIO then a press on either button will give an impulse to the rotational speed of the camera. So you can steer! The buttons are set to only detect a change so holding them down doesn't repeat the turning effect. However if you press a button and it detects that the other button already depressed the program will enter menu mode. In menu mode the left button steps through the options and the right button selects:
['cancel', 'zero stats', 'alpine', 'fjords', 'karst', 'quit']
If a keyboard is connected then W can be used to peddle, A will start and step through the menu, B will select menu options, Esc will quit
NB the first time you change scenery the animation will freeze while the new scenery items are pickled, don't panic.