I am taking a little break from the Bicycle Computer – I have three articles on that in draft but none finished. While that series is intended to show that you can do serious embedded applications with .NET, this one is intended to show that you can have fun with .NET as well. In fact, the three bike computer articles are unfinished because I have been playing with the Netduino and also a secret project from MSR. We’ll be rolling out the secret project next month at Maker Faire so stay tuned for that. But now, let’s look at the Netduino board that just came out.
The Netduino (netduino.com) board is an Arduino pin compatible board it runs on an Atmel SAM7 processor and partly because of that, it has a price point of less than $35. (That is the real price but since they have been sold out at Amazon, someone is selling their own for $89.99 + $19.99!) At $35, the Netduino is about the same price as the Arduino. If you are not a hobbyist, the Arduino board has become amazingly popular in the last few years and is used in classes as well as by a large number of hobbyists. The ‘challenges’ of the Arduino are the very simple programming model and the tools -particularly the lack of real debugging support.
Netduino is a new product from Secret Labs LLC – located in New York. The board seems to be very well engineered and these was a good deal of attention paid to the out of box experience which was seamless.
For a project, I decided to bring an old robotic arm back to life. We used this arm in a demo when we first started NETMF and the hardware that it ran on is no longer available. The arm has 6 servos or servo pairs and the Netduino only supports 4 PWM channels so I am splitting the arm in two (logically) and controlling the top three with the Netduino and the bottom three with the secret ‘gadget’. Let’s get started on the Netduino project.
As I mentioned, the out of box experience was very easy from opening the new blue box to installing the software to finding the template. There is only one and it is in with the NETMF templates. The first thing that I did was to create a servo class because, with some experimentation, I found that not all the servos have the same range of motion. I need to set the range bounds for each so that I don't damage the servos but I also didn’t want to artificially limit the servos. Also, some move too slowly unless I move by increments > 1 and others move very unevenly unless I move by increments of 1 The Servo class encapsulates the PWM port that drives the servo and tracks the range and current position of the servo and the constructor gives me the option of taking the defaul configuration or tuning it as I create it. The class looks like this (the collapsed properties implement simple Get and Set functionality:
The first thing that I did was to wire up the PWM port through a breadboard and have the arm wave at me – but that was not very interesting. I needed a way to control the arm and a joystick seemed like a natural so I ordered an Arduino Joystick shield from Sparkfun (DEV-09760).
Looking out at the Netduino Forum, I found that there are two challenges to integrating this shield. The first is that it generates two analog signals (for horizontal and vertical positioning) and there was a problem in the original firmware when there was more than one AnalogInput being used at a time. Fortunately, the Secret Labs team had identified and issued an update within a week.
The second problem was on me – I had ordered a shield that generated a 0-5V input on the AnalogInput port. This works for the Arduino but the Atmel SAM7 will only accept 0-3.3V. So I had to break the traces from the joystick and wire them into a simple voltage divider circuit. Fortunately, they folks designed this joystick board with me in mind an added a prototyping area that I could use. Three resistors and some header pins are all it takes. For that circuit, I used very large (470K Ohm) resistors to limit the current draw. This may have had the side effect of making the AnalogInput readings a little off – potentially because of the low current – but only marginally and not enough to effect my application.
Now back to the software. If you press the joystick down, there is a button that I can hang an interrupt on. Since I have only one joystick and three servos, I will use that button to select the servo that the joystick controls.
The rest of the code is in the RobotArmController cclasse and is shown below. The Main() just instantiates the RobotArmController and calls its Start() method. Below ou can see the core logic is a loop that polls the AnalogInput and if it has changed, moves the target servo.
These projects are a bit of an anti-climax to write up because they are so easy in the end that there is no revelation – no deep insight required. Good, well engineered development boards are available, there are excellent hardware components to integrate, and the libraries are so complete that doing something fun only takes a few lines of code. The high level programming model that supports event driven programming makes this all much easier. See if you can find a friend to write the same logic on the Arduino in C and compare. Next steps are to add LEDs to show which servo is currently targeted and a button to break out of the endless loop but those will be very easy – I guess that’s the point.
So, if you have not played around with the kinds of projects that you did in college for a while, now you have no excuse. You can see how easy it is to program the Netduino to do some pretty fun stuff in no time at all. There are several boards available that can support these projects with that run the same tools that you use in your day job and there is a full ecosystem of components on other support to make it all very easy. Check it out – at these prices, you can’t go wrong. Who out there has played with any of these small platforms?
I put a quick video (< 2 min) on YouTube to show that this really does work :-)