I’ve been a bit out of sorts recently. Restless. Fidgety. Bad-tempered. At times, it’s got so bad that even tea hasn’t restored my karma, and I’ve taken to firing up the coffee machine. Yeah, it’s been bad. I put it down, mainly, to the weather really. The false start to Spring that we had a few weeks ago really got my hopes up that the seasons were changing, and then we had a couple of cold snaps which sent us right back into the depths of winter, and me back into my gloomy place.
I needed a project. So, I was sitting in my shed the other day, just looking around. On my workbench were the remnants of the fork valve conversion that I recently performed on the TRX. Sitting next to that graveyard of unwanted bits was a small pile of Raspberry Pi computers, with bits of wire hanging out of the box, leftover from when I was having a bit of a clearout a few weeks back. The first book on the end of my bookshelf, Data Science at the Command Line, grabbed my attention.
Motorbikes. Computers. Data… s’obvious, innit? Home-made datalogging. So, I did what any normal person would do. Put the kettle on, and had a cuppa. By the time I’d finished the cuppa, I realised that a Raspberry Pi was pretty unsuited to the job at hand, so I’d ordered an Arduino Uno, and a few bits of wire.
Spurred on by this, I decided to write down what I’d actually like to log, and came up with:
- Throttle Position
And that was about it really. I was starting to wonder if this was going to be a bit of a rubbish project, but, I was intrigued enough to want to put some thought into how I’d collect this data, and how to present it to the Arduino, and then, importantly, seeing that the Arduino has no real storage to speak of, how to actually log anything. This was rapidly turning into a multiple tea project.
So, first up, speed. Just take a feed from the speedo. Oh – of course, I’ve got a mechanical speedo drive. Hmmm, I could buy a GPS speedo, and see if I could hack into it to get a serial feed, or… hold on a moment… idea time! Get a GPS card for the Arduino, and use that to extrapolate speed. As a bonus, I could also map my journeys, and I love maps. Really. I spend hours looking at them. So, back to the internet, and a quick bit of googlage found a few Arduino GPS shields that also contained an SD card socket. So, two birds, one stone. Nice.
Next: Throttle position. I have a TPS on the TRX, so it should just be a case of feeding it into one of the analogue pins on the Arduino. I put this to one side for the time being, as it seemed pretty straightforward.
Revs: I thought about this for a while, and decided to put this one on hold – I need an oscilloscope to look at the feed to the rev counter to see what it’s doing, and how I’d feed that to the Arduino. I don’t have a scope, so I’ve put this idea on hold for now.
Brakes: This one took me by surprise really. I thought that a pressure transducer would be cheap and cheerful, the kind of thing that you pick up on eBay for 99p. So, I’d fit one, and again, take a feed to one of the analogue pins. This idea lasted for as long as it took me to find the price and availability. So, plan B was hatched. I’d just take a feed from the brake light circuit into one of the digital pins instead. So, I’d get an on/off indicator for the brakes – this is still useful, as at the track, I can tell how long I’m holding the brakes on for, and how soon I get on the throttle once I come off the brakes. But, a complication came up – the brake light circuit is obviously 12V, and the Arduino input is 5V. Also, vehicle electrical systems are notoriously noisy. I scratched my head, and put the kettle on, and again, fired up Google. And of course, someone has already beaten me to the whole bloody idea: http://www.instructables.com/id/Racing-Datalogger-With-an-Arduino/ – where I found this brilliantly simple circuit:
At about this time, I phoned Sol of 150bhp Katana fame, and we got chatting, and the idea of measuring lean angle was thrown into the mix. I was expecting an inclinometer to be expensive. But, I was surprised to find that an ADXL335 accelerometer can be had for under a fiver, and with a bit of trigonometry, lean angle in three axes can be calculated.
By this time, I had quite a pile of bits on my desk, and the Arduino IDE fired up on my PC. The GPS unit still hadn’t turned up, so I set about measuring lean angle, and throwing together the brake like opto-isolating circuit. I came up with this:
And, with no surprises whatsoever, it didn’t work. The lean angle was all over the place, and the brake sensor steadfastly refused to do anything. Considering its only function is to emit either a 1 or a 0, this is a pretty poor state of affairs.
I put the kettle on. The lean angle (the ADXL335 is the little square thing on the end of the long ribbon cable for those who are interested) was just me screwing up the maths. I really should have listened to my maths teacher in school, as really, it’s just A-level trigonometry – in the end, I just grabbed the code from http://bildr.org/2011/04/sensing-orientation-with-the-adxl335-arduino/ and hacked around with it a bit. Then again, my A levels were 32 years ago, so it’s not surprising I’ve forgotten things. The brake light thingie was incompetence on my part. I shoved 12V straight into the input, without going through the resistor first. Luckily the things cost something like 18p each, and come in packs of 10. Once I’d fixed this, it was all fired up again, the code reflashed to the arduino, and all was well with the world. Next step was just to transfer the prototype brake circuit to stripboard for mounting to the bike:
I still need to cut the board to size (excellent! Angle grinder action ahead…), but that’s ready to be wrapped in heatshrink, and fitted to the bike now. Down in the bottom right hand corner of that photo you can see the GPS shield, which has now turned up. A quick bit of poking with the lovely TinyGPS library has shown that it’s giving me location data, and as a bonus, there’s a function in that library to calculate speed already. So, that saves me a job. Writing data to the SD card has also been tested, and that works OK too.
And that’s really where we are right now. Need to finish the coding to write the GPS data to the SD card, to go with the lean angle and brake status the code currently logs. Then I need to shuffle out to the garage to work out how to grab a feed from the throttle position sensor. I may have to do something with the voltage on this one – if it’s between zero and five volts from closed to fully open, I’m good, as I can just stuff that straight into an analogue pin on the Arduino. May want to see about filtering it somehow to prevent spikes. I have no clue how to do this, so any ideas will be gratefully received.
Total cost, so far, is refreshingly not very much at all. The Arduino was old stock, and I got it for about a tenner. The most expensive thing was the GPS unit, at about £20. The accelerometer was under £5. Resistors, optocouplers, and stripboard also came to under a fiver.
Next instalment should, with any luck, contain some actual ride logging data. Need to crack on with that coding. This may take some time, as I’m not a good programmer at the best of times, but I’m thoroughly enjoying diving into the Arduino IDE and learning about GPS protocols. I really should get out more.
Firstly though, I’d best put the kettle on. I’m done with coffee for a while now.