.Net Gadgeteer: IR Radar

In this demo I am going to show how to integrate with multiple external (non-Gadgeteer) hardware devices. 

In previous posts I have covered the basics of using a single extender module to connect to devices like a Bluetooth module. Here we show that it is possible to use several extenders. This is what I meant when I said .Net Gadgeteer is “hardware glue” in a previous post.

 

Suppose you wanted to connect a servo and an IR range finder (such as a Sharp GP2Y0A21F9Y)  to make a IR radar gadget. I want the servo to sweep the IR range finder across an arc and measure distance. Then I want the results to be displayed like a radar screen.

The issue you will run into is that there are no sockets that cover both PWM (needed by the servo) and analog input (needed by the IR range finder). This is a case where you must use multiple Extender Modules.

You could create your own extender module too. Just use the Extender Module that comes with the GHI kit have to map the pins with a voltmeter.

In this example, I opted to get another Extender Module. Here is what it looks like. Notice that the new one is using a straight header I soldered on. The other one uses a 90 degree header. This gives me more options later when prototyping. 

The code is very simple (works but could be improved):

   public partial class Program
    {
         void ProgramStarted()
        {
            // Do one-time tasks here
            Debug.Print("Program Started");
            GT.Interfaces.PWMOutput servo = this.extender.SetupPWMOutput(GT.Socket.Pin.Seven);//must be 7,8,9
            GT.Interfaces.AnalogInput IRRange = this.extender1.SetupAnalogInput(GT.Socket.Pin.Four);
            IRRange.Active = true;
            servo.Active = true;
            ushort minDistance = 0;
            ushort minDistanceTemp = 0;
            uint period = 20 * 1000 * 1000;
            uint hightime = 2000; //hightime range 700-2000 *1000
            uint minHightime = 0;
            int t = 0;
            int x0 = (int)(display.Width / 2);
            int y0 = (int)display.Height - 50;
            int x1 = 0;
            int y1 = 0;
            int factor;
            const int ANGLE_STEP_SIZE = 10;
            int diameter = 1500;
            int angle = 270;
            display.SimpleGraphics.SetPixel(GT.Color.White, 1, 1);
            servo.SetPulse(period, hightime * 1000);
            while (1 == 1)
            {
                for (hightime = 700; hightime                 
                {
                    diameter = 1500;
                    servo.SetPulse(period, hightime * 1000);;
                    factor =( diameter - minDistanceTemp) * ANGLE_STEP_SIZE * (int)System.Math.PI / 180 / 2;

                    x1 = ( factor *
                              (int)Microsoft.SPOT.Math.Sin(angle) / 1000) + (int)(display.Width / 2);
                    y1= (factor *
                              (int)Microsoft.SPOT.Math.Cos(angle) / 1000) +190;

                    //  line(x0, y0, x1, y1); //line takes too long to draw.
                    //using circles instead to show range.
                    display.SimpleGraphics.DisplayEllipse(GT.Color.Green, (uint)x1, (uint)y1, (uint)3, (uint)3);
                    angle -= ANGLE_STEP_SIZE;
                    if (angle < 90)
                    {
                        display.SimpleGraphics.Clear();
                        angle = 270;
                    }
                    System.Threading.Thread.Sleep(30);
                    minDistanceTemp = GP2D12ToCentimeters(IRRange.ReadProportion()*1000);
                    if (minDistanceTemp < minDistance || minDistance==0)
                    {                         
                        minDistance = minDistanceTemp;                        
                        minHightime = hightime;                     
                    }                 
                }             
            }         
        }         
        private UInt16 GP2D12ToCentimeters(double input)         
        {            
            UInt16 current=0;             
            if (input > 0)
            {
                //You can use Excel to calculate this function. 
                current = (UInt16)System.Math.Floor(System.Math.Pow((4187.9 / input), 1.1060));
                return current;
            }
            return current;
        }
    }

Note: if you have trouble with the Sharp IR sensor sometimes it helps to put a 30uf cap across vcc and gnd.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s