As part of my Big Trak refurbishment (not going as well as I wanted it to), I decided to use the Adafruit Prototyping Pi Plate to mount the additional electronics on. If youāre not familiar with it, then this is it:
So itās a double-sided PCB with a GPIO connector tracked to various pads round the outside of the board with space to solder in headers or screw-terminals (or both!), as well as having space in the middle to solder components to, supporting a standard DIL type layout, SOIC chip mounting pads and an open area.
An interesting feature is that it extends the GPIO pins through the board, giving the possibility for stacking ā almost like an Arduino shieldā¦
So for the Pi Trak, what I needed to do:
- Power supply. Powering the Pi off batteries, I needed a regulator to feed a stable 5V supply into the Pi.
- A motor controller. Essentially a dual H-bridge controller of some sort.
- Interface from a wheel position sensor of some sort.
- Interface to the keypad.
As I was running out of time to produce a prototype, I used a handy 7805 type regulator. This is great, but not too efficient ā ie. it gets a bit hot when fed from 9v.
The keypad was originally going to be the Big Traks own keypad, but I substituted one Iād made earlier (some 25 years ago!) ā itās a simple 4Ć4 matrix and you can see 4 resistors on the protoboard. There is a 5th next to them which acts as a limiter for the LED thatās on one of the switches.
For the motor controller, I used the Pololu TB6612FNG unit which I bought locally via SKPang. (The Adafruit parts were supplied directly from the US, but note than SKPang now stock Adafruit components on the UK!)
The wheel position sensor was an interesting one ā initially I thought I could use a Lego sensor (since the rest of the thing was going to be made out of Lego by that point), however it turns out the Lego rotation sensor is analog ā and outputs 4 different voltages depending on the angleā¦ So a Plan B was hatched and I re-cycled the sensor from the original Big Track ā seen here under test using the Adafruit Cobler board:
The wheel rotation sensor is the board to the left ā itās nothing more than an infra-red LED pointing to an IR photo diode. With a suitable current limiting resistor on the LED, the photo diode can be connected directly to one of the Piās GPIO inputs.
I also used to to test the motor driver too. I was a very easy way to do some ārapid prototypingā before committing to solder!
The end results was this:
This was the first time Iād used a board like this ā more often Iād build projects on a re-usable breadboard, or on stripboard, so it did pose a few issues which Iād never encountered ā The DIL area where I mounted the voltage regulator and the motor controller seemed fairly straightforwards, but with hindsight, Iād have made some of the links to the +3.3v and 0v supplies under the motor controller! In the open area, I cheated and just bent the wires of the components over to touch the pins of the 8-way socket Iād soldered on. Is that cheating, or is it the way everyone else does it? (Sensible answers please!)
However, overall, the board was very easy to work with ā and does look a lot more professional than just hacking together some stripboard, and combine the board with the Adafruit case as Iāve done here, and it does open up many more possibilities for Pi interfacing projects.
Finally:
This is the Mk1 Lego PiTrak. Much room for improvement and I need to mount the keypad on it ā also want to put on an LCD display, so watch this space as they sayā¦
Bristol RaspberryJam
Just a quick post and a few photos from last nights RaspberryJam in Bristol ā held in the most excellent Bristol and Bath Science Park.
Alan (@teknoteacher) the driving force behind the RaspberryJams, was on-form as usual, keeping everything flowing nicely with his usual jokes, and while I didnāt count the people, there seemed to be well over 100 attending! There was a nice time to do some networking before the main event (spread the jam as Alan says!), then the talks then a big break-out session afterwards with people showing off what theyāre doing with their Piās and so on.
I did a one of the talks, and while it wasnāt quite fully what Iād planned, due to some technical hitches before-hand with Keith Dunlops presentation ā all about RiscOS on the Piā¦
(The Pi is a bit of a fussy little beast which wonāt normally output to HDMI when it doesnāt detect a āliveā HDMI display connected to it, so when you connect a Pi into a fancy multi-switching input thingy, unless the switch is set to take the HDMI input, then the Pi wonāt boot up in HDMI mode!)
However after that delay, as Keith often ends his talk with BBC BASIC running under RiscOS on the Pi, I cut my talk a little short and spoke more about my BASIC than what Iād fully planned to speak about ā however the essence of what I was going to saw was that programming (or Software engineering!) isnāt just about glitzy/glamorous web āappsā with shiny graphics and fancy buttonsā¦ Programming also encompasses a lot more ā which some people think is boring, but others (like me) find much more exciting ā and thatās the world of computer control.
I also think of myself as a Programmer or Software Engineer. Iām not a developer ā Iāll leave that term for the airy fairy web people
So, someone, somewhere has to write the code that drives the braking system on your car, the engine management and so on. Someone has to write the low-level software for your hand held GPS, your microwave, your washing machine, etc. Someone (or probably some team!) wrote the software for the Mars Curiosity robot ā now thereās a project you canāt afford to get wrong!
And if youāre wondering who wrote the software for some of these things, and thinking itās some underpaid chap in India or China, then think again ā Heber who hosted the RaspberryJam last night is based in the UK, manufacture their own control boards (in the UK!) and write software for things as diverse as vending machines to the Dyson Washing machine!
Hereās a few photos from the evening:
Links to other peoples photos, etc.:
- This is Bristol (newspaper/site)
- RaspberryPi Spy (g+)
Learning GIT
Iāve decided to start to use the GIT Source Code Management systemā¦ So hoe better to learn about it than to jump in at the deep end and actually use it for a projectā¦ So to that end, Iāve moved wiringPi to it and setup some simple hosting for it ā mostly for my own convenience, but also to allow others whoāre using GIT to quickly obtain updated versions and so on.
Iāll eventually be putting links from the main downloads site Iām using to the GIT repository, but until then, a quick look and it can be found here.
Accurate Delays on the Raspberry Pi
One of the issues on the Raspberry Pi is that Iām now seeing a lot of people coming to it from a traditional microcontroller background and trying to use the same ides on the Pi as they use on the microcontrollers ā and promptly falling into a trap, or getting wildly unexpected results.
One of these areas is timed-delays. On a microcontroller, you can often simply loop for a number of times and get the result you desireā¦ On the Pi, your program can be pre-emtively interrupted by Linux doing something else, it can be stalled by the GPU accessing memory and probably several other things. So timing loops in software is generally not that useful (Not to mention the CPU usage they consume ā again, not an issue on a microcontroller without an operating system, but under Linux, itās wasteful if you have something else to do!)
Soā¦ Linux provides a standard set of system calls ā the issue is that you have the overhead of calling them ā and that overhead can be quite variable, it depends on the system call and what it needs to do. The standard delay call, nanosleep(2) will sleep for at least how long you tell it, plus any operating system overhead and that comes to at least 20 to sometimes over 100 microseconds.
So.. when we need accurate short delays in the order of microseconds, itās not always the best way, so to combat this, after studying the BCM2835 ARM Peripherals manual and chatting to others, Iāve come up with a hybrid solution for wiringPi. What I do now is for delays of under 100Ī¼S I use the hardware timer (which appears to be otherwise unused), and poll it in a busy-loop, but for delays of 100Ī¼S or more, then I resort to the standard nanosleep(2) call.
A quick look at the wiringPi code to see what Iām doing:
void delayMicrosecondsHard (unsigned int howLong) { Ā *(timer + TIMER_LOAD)Ā Ā Ā = howLong ; Ā *(timer + TIMER_IRQ_CLR) = 0 ; Ā while (*timerIrqRaw == 0) Ā Ā Ā ; }Ā Ā void delayMicrosecondsWPi (unsigned int howLong) { Ā struct timespec sleeper, dummy ; Ā Ā /**/ if (howLong ==Ā Ā 0) Ā Ā Ā return ; Ā else if (howLongĀ < 100) Ā Ā Ā delayMicrosecondsHard (howLong) ; Ā else Ā { Ā Ā Ā sleeper.tv_secĀ = 0 ; Ā Ā Ā sleeper.tv_nsec = (long)(howLong * 1000) ; Ā Ā Ā nanosleep (&sleeper, &dummy) ; Ā } }
Iām using the count-down timer and polling for the IRQ event (it doesnāt generate an IRQ, weāre just polling for the completion event here) This avoids any issues with having to take into account counters that wrap over.
This isnāt perfect though, and it can still take longer than we want it to, but for small delays it should be a lot closer to what we want than just calling the standard nanosleep(2) routine.
This will be in the next version of wiringPi which Iāll publish in a day or 2.
Pi, Linux and C Training
I recenty ran a 2-day course for someone who was keen to learn more about Linux, C and the Raspberry Pi.
Teaching isnāt new to me, Iāve run courses on my telephone systems, various aspects of Internet operations for people like web design companies and even used to teach something called Engineering Computing during my years at Napier, so to run a 2-day course in my office wasnāt hard and turned out to be a very rewarding experience for both myself and my student!
So what did we do? Started with getting more to grips with the Linux command-line, the use of pipes and redirection. Demonstrating that programmers are lazy (ie. command and filename completion!) We then went on to look at C programming. My student had been doing some C already, but wanted to learn a bit more. Variable scope ā the difference between local and global variables. Static variables and so on. We moved on to separate compilation and linking files together ā how we can use header (.h) files to give the compiler āhintsā about how to call a function in another file.
We also moved on to some of the wiringPi functions too. Talked about concurrent programming and had a brief introduction to threads (with the simplified thread library inside wiringPi).
So a very worthwhile 2 days ā itās always good to share knowledge and meet more like-minded people too, and Iāve just sent my student some homework
For more detail: Adafruit Protoplate for the Rasbperry Pi