Saturday, September 17, 2011

Using a Joystick With NETMF and the NETMFx Joystick Library

JoystickOnPandaII
As I’m working on the Omnimote, I decided the next article in the NETMF for Electronics Noobs series should be about how to interface a joystick with a NETMF device.
For this example, I’m using a Parallax joystick from Adafruit.comSparkfun has a similar joystick which should work using the same directions although the labels for the pinouts are slightly different.  I prefer the one from Adafruit because it has a nice rubber feel to it and it’s pin headers fit in a breadboard.  The one from SparkFun is a slippery hard plastic and requires you to buy a separate PCB in order to make it breadboard friendly.  It does have the advantage of an additional push button selector built-in that the one from Adafruit does not.  They both have convex tops.  I would have much rather had concave tops such as you get with an XBox 360 controller but these were not carried.

A joystick controller is simply a stick connected to two small potentiometers – one for the left-right (X) motion and one for the up-down (Y) motion.  To connect the joystick to the board, we will need two analog pins plus +5V and ground connections.  The following table will show the labels and pins for these two joysticks.
Adafruit Joystick Sparkfun Joystick Arduino Compatible Pin
L/R+ VCC +5V
L/R HOR Analog*
U/D+ VCC +5V
U/D VER Analog*
GND GND GND
Any two analog pins can be used for L/R and U/D.  For this tutorial, I am using Analog0 for L/R and Analog1 for U/D.


image
This post also serves as an announcement of a new open source NETMF library I’ve created called NETMFx.  This will serve as a place for me (and anyone else who wants to contribute) to put custom drivers and anything NETMF related.  My goal is to make anything in it compatible with both GHI and netduino so that the greater NETMF community can make use of it.  I’ve done this by using conditional compile symbols when necessary.  Be sure and check out the progress made so far in NETMFx.Joystick and NETMFx.Math projects in the NETMFx source code download.

There is also a “Samples” project in the NETMFx solution where I’ve posted a sample project called “Joystick Tutorial” that is specific to this post.  I’ve done a fairly complete job of documenting the project from the code so I will not bother going into detail in this post.  Feel free to contact me if you need further clarification of how to run the code once you have everything wired up.

Thursday, September 8, 2011

Sawdust - Entertainment Center

I just realized that Software & Sawdust is really all software and no sawdust at the moment...  So, to break things up a little I'm digging up a project I finished last year - a custom built entertainment center.  I keep my woodworking project details on the LumberJocks.com site.  Read more about this project and a few of my other projects.  As the weather is cooling down, I'll be getting out in the new workshop more in the next few months.  We built a new house this year and are in need of a lot of bookshelves and my home office has a lot of needs.  Where to begin...?

Tuesday, August 30, 2011

Controlling a Brushless Motor with NETMF

2011-08-30 22.10.59
Continuing with the NETMF for Electronics Noobs Tutorials.  Today, I’ll demonstrate wiring up a brushless motor to your NETMF device.
 
If you’re building any type of R/C planes, copters, cars, boats or robots with your NETMF device then you’ll likely encounter the need to control a brushless motor.  Controlling a brushless motor is very different than controlling a regular brushed DC motor.  What makes it different is the addition of the Electronic Speed Controller (ESC).

The main reason this article exists is because the hobby parts distributors have done a terrible job of documenting these most basic of parts.  You should be able to go to the manufacturer website for the ESC you purchase and get most of this info.  However, my experience is that you can’t and they assume you know what you’re doing if you buy these parts.  Obviously, not the case or you wouldn’t be here.

If you click on the main photo, and look closely at the ESC (the blue part in the lower left corner) you will see that on one side it has three black wires.  These colors may vary from model to model.  On the other side it will have two thick red & black wires.  It will also have a bundle of two or three thinner wires depending on your model.

2011-08-30 22.12.09First connect the three black wires from the ESC to the three wires coming from your motor.  Right now you’re thinking “which wires on the motor go to which wires on the ESC?”.  Trust me, it doesn’t matter.  To understand why, study how brushless motors work and you’ll understand.  For now, just take a leap of faith.  If you find later that your motor is turning in the wrong direction, then swap any two of these wires and the direction will change.  It’s magic…
2011-08-30 22.24.17
The small bundle of three wires on my ESC are colored brown, red, and orange.  After quite a bit of digging, I finally found that the two outer wires are the important ones.  The brown wire is ground and the orange wire is the control.  The red wire in the center provides 5V power and can be used to power the microcontroller.  Most people do not recommend this due to the fact that when the motor is accelerated quickly it is possible that power could be momentarily lost.  This is an especially bad situation if you’re controlling a flying vehicle.  It’s not so bad if you’re controlling a car or plane.  If your bundle only has two wires, then you do not have the power wire.

You should wire the brown wire of the ESC bundle to any ground on your NETMF device.  For this example, I’m using a GHI Panda-II.  The orange wire should be connected to any PWM pin.  I’m using PWM1 (I/O 10).

2011-08-30 22.12.31Next, all you need to do is connect the red & black wires from the ESC to the respective wires on your battery.  If all is well, you will probably hear a series of tones from your ESC indicating that it is now armed.

NEVER TEST YOUR MOTOR WITH A PROPELLOR ATTACHED!!!

Until you have fully tested out your controls and have everything mounted securely and have a safe place to work, it is a really bad idea to attach the propellers.  If you have never worked with a brushless motor with a propeller on it, you will probably be amazed at how much power they have the first time your power it up.  Propellers are very sharp and will cut you.

Now that everything is wired up, it’s a simple thing to get the software going.  Start by downloading Chris’ Brushless Motor ESC driver class and add it to a new NETMF project.  Assuming you also used pin 10 to control your ESC, the following code should test your motor by powering it up to 20% power for about half a second and then powering it off.  Push the reset button to make it go again.

public static void Main()
{
    var m1 = new BrushlessMotor((PWM.Pin) FEZ_Pin.PWM.Di10);
    m1.Scale = 100;
    m1.SetPower(20);
    Thread.Sleep(500);
    m1.Stop();
}
If that works then you’re off and running.  Just figure out what power your project needs and when then apply where necessary.  If you need additional motors, then rinse & repeat but use a different PWM pin.

If you’re using a netduino or other NETMF device, do not be dissuaded.  It’s very easy to port Chris’ driver class to your device and to make the appropriate change to the test app.  Feel free to drop me a line if you need help.

Choulant asked for it and here they are - the parts used in this tutorial:



Have fun!

Congrats to my new Eagle Scout!

This post isn’t about software or sawdust but about something more important.  My son was awarded the highest rank in Boy Scouts this weekend – Eagle Scout.  Only about 2 out of 100 boys that start the Scouting program achieve this and I’m very proud of him.  He’s worked very hard (with a lot of encouragement ;).  I’m sure this is going to be the first of many great accomplishments we see from him.
imageThe Boy Scouts is unquestionably the best youth program that exists.  If you would like to learn more or find a local troop to join, go to the BSA website.  I’ve been a Scout leader for many years and will be glad to also assist with any questions you may have.

Wednesday, August 24, 2011

Omnicopter–Power Wiring Harness

Lately, I’ve been working on the power distribution (wiring) harness for the Omnicopter.  This turned out to be a much bigger challenge than I had anticipated.  My design objectives for the Omnicopter dictate that I must eventually be able to support.
  1. 8 x 30amp brushless motors
  2. 2 x 3-cell Li-Po batteries
Power - Wiring Harness - Soldered RingAlthough I am currently only using four motors and one battery, I knew this was one part of the project that would be a lot easier to only do once.  Also, because of my electrical current requirements I decided to use 12 gauge wire which is thicker and less flexible than the smaller gauge wires.  Getting all the wire in my 1/2” thick frame proved to be a bit of a challenge.
My first attempt was to try splicing all the different wires together by soldering two or three wires together at a time then covering with shrink tube.  This proved to be an impossible task.  Soldering this thick of wire together like this is very difficult and because of all the joints and shrink tube it simply wasn’t going to fit into the frame if I did get it all soldered together.
Power - Wiring Harness - PCB RingSo, after researching and doing a lot of thinking I decided on what I believe is going to be an excellent design.  I found an example on the net that served as inspiration where he twisted the ends of the wires together into a loop.  That seemed it would be a bit difficult to solder and then you would also have to add an insulator between the positive and negative rings which would add bulk.  I finally got the idea that a ring of double-sided PCB would serve the purpose marvelously.
Power - Wiring Harness - PCB Ring - TinnedUsing a whole saw, I cut out a 1” circle of the PCB.  Because I wanted to be able to run a wire up the center to feed the microprocessor, I then drilled a 1/2” hole in the center.  After washing and roughing up with some fine sandpaper on both sides of the board, I then put a heavy tin of solder on both sides to help thicken the metal and allow it to handle the higher current and to ease in soldering the wires.  Next, I spent most of the afternoon soldering…  After this was all completed, I ran across an article that pointed out that making a closed loop like I did risks the possibility of creating inductance which may later cause me problems.  If I had it to do all over again, I would have removed a thin slice from each side of the PCB to break the loop.  But, once the copper is tinned and the liquid tape is on it's a bit more hassle to do.  So, I'll keep this in mind if any problems should arrise that I think may be due to this.  If I were starting over, I would have definitely put this into the original design.
Power - Wiring Harness - Center Disk PaintedOnce all the wires were connected to the PCB loop, I soldered on all of the XT60 connectors.  I used female yellow plugs for the motor connections and male plugs for the battery connections.  Using a red Sharpie, I colored the battery connections also for easier identification. 
Power - Internal Wiring HarnessThe battery wires were run in parallel and leads were run to the outside of the frame to allow for a switch to be connected between the batteries and the rest of the harness.
Power - External Wiring HarnessOnce all the soldering was complete, I painted on plenty of liquid electrical tape on all the exposed metal surfaces including the entire PCB ring.
Then I used my meter to test for any shorts (probably should have done this before the liquid tape).  Thankfully, everything tested perfect.  So, I put the top side of the frame on and tightened her back down.
Now I have to solder up the ESCs to the motors and the matching XT60 connectors then it’ll be time to start the fun work of figuring out how to make it fly.

Monday, August 15, 2011

Omnicopter – The Frame is Built


2011-08-14 19.00.28
2011-08-14 18.58.52The princess is not happy…  The frame for the Omnicopter is built and the motors are installed but it doesn’t fly!!!  Women.  They want everything “right now!”. ;)  However, I later wired up one of the motors and put a prop on it and showed her what’s coming.  She’s quite pleased at that.  I’ll admit that I was also.  I built this frame as more of a lab/test frame than a final version.  It has a lot of flexibility built into it as far as arm placement goes and very little attention was taken to try and reduce weight.  With 20% power given to one motor with a prop it produced enough lift to lift half of the frame and motors off the ground.  Once all four motors are up and running it will have more than enough power to lift itself and probably 2-4 additional pounds.  Can’t wait to have that test!
2011-08-14 18.59.11
I’ll post some additional detail about the frame later.  Now to build the wiring harness…

Saturday, August 13, 2011

Using an External Button & LED with NETMF

In continuing with the BlinkyLEDNETMF for Electronics Noobs series of tutorials, today I’m going to demonstrate how to connect a button/switch to a Panda-II NETMF microprocessor and how to respond when the button is pushed.  Since I need to do something when the button is pushed, I thought this would be an opportune time to also show how to connect an LED to the board and light it up also.

Nearly every NETMF basic tutorial out there starts with a very simple program that uses the on-board button to light up the on-board LED.  That’s just great and is absolutely fantastic the first time you write a NETMF program.  However, as soon as you start working on a real project you soon discover that the on-board button & LED have very limited uses since they can’t be exposed outside of your project enclosure. Then the question comes up – how do I add more buttons?  How do I light up an LED on my project box?  How do I use more than one button or LED?

I’m going to demonstrate how to do this in the most basic situation – the one that will work for 95% of all hobbyist’s needs.

Panda-II Connections

For this project, you’ll need a momentary button, an LED, a breadboard, one 10K ohm resistor, one 100 ohm resistor, and some wires.

Like most things, there are many ways you could wire this up.  This is just one.  What I did was to first plug my button into the breadboard and wire one side of the button to the ground line (very top row on the breadboard).  Then, I connected a 10K ohm resistor (brown, black, orange) to the power (5V) line (2nd row on the breadboard) and used it to connect power to the other side of the button.  The 10K resistor prevents us from creating a “short” which could fry the electronics by allowing too much current to be pulled through the electronics.  Next, we put a 100 ohm resistor (brown, black, brown) on the power side of the button then connect the other side of the resistor to the analog 0 pin on the Panda-II.  This resistor prevents too much current from getting into the board and damaging it.

Panda-II Connections.Fritzing

Now that we have the button wired up, we’ll wire up the LED.  Since only a very small amount of current flows through pins of the Panda-II board we can connect the anode/positive side (longer wire) of the LED to the D5/PWM5 pin and wire the cathode/negative (shorter) side of the LED to our ground line.  That’s it for the wiring.

I do want to note here that if we were wiring the LED to an external power source that we would need to add a current limiting resistor to one side.  To determine the correct size resistor, you can use an LED calculator.

Now that we have the hardware all wired up, it’s time to write some software that responds to our button being pressed by lighting the LED.

In Visual Studio, we create a new console project and add the following references:

FEZPanda_II_GHIElectronics.NETMF.FEZ
Microsoft.SPOT.Hardware
Microsoft.SPOT.Native
mscorlib

In our Program.cs file that is automatically created, replace what is there by default with the following.

using System;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using GHIElectronics.NETMF.FEZ;
 
namespace ExternalButtonAndLedTutorial
{
    public class Program
    {
        // Tell the CPU we're going to use digital pin #5 to send a signal to the LED.
        static readonly OutputPort _led = new OutputPort((Cpu.Pin)FEZ_Pin.Digital.Di5, false);
 
        public static void Main()
        {
            // Tell the CPU we're going to receive an analog signal from analog pin #0.
            var blueButton = new InterruptPort((Cpu.Pin)FEZ_Pin.Interrupt.An0, true, Port.ResistorMode.PullUp, Port.InterruptMode.InterruptEdgeLow);
            
            // The second parameter in the line above ("true") tells the CPU that we want to use the "glitch" filter.
            // If you disable this line then run the program, you will notice in the debug windows that if you are not very fast
            // at pushing the button that the event will actually get fired multiple times.  We only want the event to fire once.  GHI gives
            // us this property that we can set to put a boundary around our first event and prevent redundant events from being fired.
            // Adjust the number of tick
            Cpu.GlitchFilterTime = new TimeSpan(TimeSpan.TicksPerSecond/2);     // 1 Tick = 100 nanoseconds
 
            // When the button is pushed it will signal an interrupt in the CPU which will in turn cause an OnInterrupt event to be thrown.
            // Our BlueButton_OnInterrup function will be called when the interrupt/event is fired.
            blueButton.OnInterrupt += BlueButton_OnInterrupt;
 
            while(true) Thread.Sleep(10);
        }
 
        private static void BlueButton_OnInterrupt(uint port, uint state, DateTime time)
        {
            _led.Write(true);       // Turn on the LED
            Thread.Sleep(1000);     //  wait 1 second...
            _led.Write(false);      // Turn off the LED
 
            Debug.Print("Blue Button Pushed! " + DateTime.Now.ToString());
        }
    }
}



I’ve chosen to document the code as comments in-line, but if anything isn’t clear please leave a comment and I’ll clear it up.


Make sure and set your project properties to deploy to the USBizi device then run it.  If you’ve done everything correct then when you push the button the LED should come on for half a second then turn back off. 


Now that you have one button and one LED working,  get creative!  You can reproduce this many times and as many different ways as your imagination will allow.  To use more buttons or more LEDs, just use any of the other pins and be sure to specify in your code when you define your buttons or LEDs which pin they are connected to and you’ll be fine.  Put these new skilz to work and be sure to post your results in the comments!