Netduino home hardware projects downloads community

Jump to content


The Netduino forums have been replaced by new forums at community.wildernesslabs.co. This site has been preserved for archival purposes only and the ability to make new accounts or posts has been turned off.

Jason Smith

Member Since 08 Aug 2010
Offline Last Active Dec 23 2011 11:14 PM
-----

Topics I've Started

Memsic 2125 Accelerometer / Servo

14 August 2010 - 07:12 AM

All,

I successfully got the Memsic 2125 Accelerometer to work on my Netduino, so I decided to take a crack at controlling a servo by measuring Tilt. I'm sure I could do a lot to make this more efficient but I just wanted to get this working first:

Below is the code I used (I plan on doing a lot more work to get this going right. Once I do, I will properly comment my code and clean things up to make it more user friendly.):

using System;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using SecretLabs.NETMF.Hardware;
using SecretLabs.NETMF.Hardware.Netduino;

namespace Memsic
{
    public class Program
    {
        static int count = 0;
        static uint _lastposition = 0;
        static InputPort Xin = new InputPort(Pins.GPIO_PIN_D2, false, Port.ResistorMode.Disabled);
        static InputPort Yin = new InputPort(Pins.GPIO_PIN_D3, false, Port.ResistorMode.Disabled);
        static PWM ServoY = new PWM(Pins.GPIO_PIN_D9);
        public static void Main()
        {
            while (true)
            {
                int X = TiltToServoPulse(GhettoTilt(Xin));
                int Y = TiltToServoPulse(GhettoTilt(Yin));
                MoveServo(ServoY, (uint)Y);
                //Debug.Print("X:" + X + "," + "Y: " +  Y);
             }
        }

       //Read the specified port and grab some values to calculate Ghetto Tilt.
        static int GhettoTilt(InputPort axe)
        {
            count = 0;
            // Loop until pin reads a low 
            while (axe.Read() == true) { }
            // Loop until pin reads a high 
            while (axe.Read() == false) { }
            // Loop until pin reads a low and count
            while (axe.Read() == true)
            {
                if (axe.Read() == false) { break; }
                count += 1;
            }
            count = count - 21;
            return count;
        }

        static void MoveServo(PWM whatservo, uint whereto, int sleep = 0)
        {
            if (_lastposition == whereto) { return; }
            uint HowLong = 0;
            if (whereto > 2000 || whereto < 1000) { return; }
            whatservo.SetPulse(20000, whereto);
            if (_lastposition == 0) { HowLong = 300; }
            else { HowLong = _lastposition > whereto ? (_lastposition - whereto) / 3 : (whereto - _lastposition) / 3; }
            Debug.Print("" + HowLong);
            Thread.Sleep((int)HowLong);
            whatservo.SetPulse(0, whereto);
            _lastposition = whereto;
            if (sleep != 0) { Thread.Sleep(sleep); }
        }

        static int TiltToServoPulse(double Tilt)
        {
            double pulse = 0;
            Tilt = Tilt - 1;
            double calc1 = Tilt / 18;
            pulse = (calc1 * 1000) + 1000;
            return (int)pulse;
        }
    }
}


..and a video of the results:
http://www.youtube.com/watch?v=xTijhSgqMfM

Jason

Steps to restore an Erased Netduino

13 August 2010 - 06:08 AM

All, Can someone provide the steps needed to restore an Netduino that has been erased? Believe it or not.. I managed to accidentally erase my Netduino during testing... Jason.

HXT 900 Servo

12 August 2010 - 04:59 AM

All,

I was able to successfully control an HXT 900 servo using the Netduino board this evening and wanted everyone's thoughts on the code.

Basically, to figure out how to use the servo I read through a beginners guide to C# and the micro framework created by GHI located Here.

..after attempting to apply this code to the Netduino I realized that the logic for using the PWM.SetPulse method must be different on the Netduino, or I am just completely using it wrong...although was still able to get this to work. I know there are also different methods I could have used to get this to work, but this one really does seem to work well. I ran this servo for over an hour tonight without any issues.


using System;
using System.Threading;
using Microsoft.SPOT;
using SecretLabs.NETMF.Hardware;
using SecretLabs.NETMF.Hardware.Netduino;

namespace Servo1
{
    public class Program
    {
        static PWM Servo1 = new PWM(Pins.GPIO_PIN_D9);
        static uint _left = 1000;
        static uint _center = 1500;
        static uint _right = 2000;
        static uint _lastposition = 0;

        public static void Main()
        {
            while (true)
            {
                MoveServo(Servo1,_left, 100);
                MoveServo(Servo1,_center,100);
                MoveServo(Servo1,_right, 100);
            }
        }

        //Move the servo between 1000(left) on 2000(right).
        static void MoveServo(PWM whatservo, uint whereto, int sleep = 0) 
        {
            uint HowLong = 0;
            //exit the void to avoid servo damage if the value's are not within range.
            if (whereto > 2000 || whereto < 1000) {return;}
            //Begin sending the pulse to the servo.
            whatservo.SetPulse(20000, whereto);
            //if the lastposition of the servo is unknown (the default 0 value set above),
            //default the value to 300, otherwise calculate how longe the servo shoud run.
            if (_lastposition == 0) { HowLong = 300; }
            else { HowLong = _lastposition > whereto ? (_lastposition - whereto) / 3 : (whereto - _lastposition) / 3; }
            //continue sending the pulse using the howlong calculation.
            Thread.Sleep((int)HowLong);
            //stop the pulse.
            whatservo.SetPulse(0, whereto);
            //reset the last position after the move.
            _lastposition = whereto;
            //sleep the specified amount of time if applicable.
            if (sleep != 0) { Thread.Sleep(sleep); }
        }
    }
}


ON a side note, the above code works for both 3.3v and 5v, but the servo seems to operate a lot smoother on 3.3v because of the "HowLong" calculation I am using.. a small adjustment to the calculation would fix this.

You see a video of the servo in action below:

http://www.youtube.com/watch?v=yUgzQP85qc4

Jason

TMP36 Temperature Sensor/SB Protoshield

10 August 2010 - 06:05 PM

I seem to be having an issue reading the TMP36 Temperature sensor. I setup the sensor using a SB(Solorbotics) Protoshield following the tutorial at the following link : http://www.ladyada.n...sors/tmp36.html. I have the sensor setup on the Netduino exactly as shown in the below Arduino Picture:

Posted Image

Here is the code I am using to attempt to read the sensor:

using System;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using SecretLabs.NETMF.Hardware;
using SecretLabs.NETMF.Hardware.Netduino;

namespace NetduinoApplication2
{
    public class Program
    {
        static AnalogInput Tmp36 = new AnalogInput(Pins.GPIO_PIN_A0);
        public static void Main()
        {
            // write your code here
            while (true)
            {
                double voltage = Tmp36.Read() * 5.0 / 1024;
                double tempC = (voltage - 0.5) * 100;
                double tempF = (tempC * 9 / 5) + 32;
                Debug.Print("Temp in C is: " + tempC);
                Debug.Print("Temp in F is: " + tempF);
                Thread.Sleep(1000);
            }
        }
    }
}


The above code seems to be fine, but I dont appear to be getting back any proper data from the sensor. I am getting a reading in the mid 800's all the time using this code...even without the sensor plugged in. I was wondering if someone could help me figure out what I might be doing wrong. It is possible (I am new to microcontroller projects, but work as a .net programmer...) that the temperature sensor may be defective, and I have not yet attempted to test it with a multimeter. Its also possible the issue might be with my protoshield. I have not attempted to connect the sensor without it.

Can anyone see any issues with the way I am trying to read this sensor before I check it with a multimeter?

home    hardware    projects    downloads    community    where to buy    contact Copyright © 2016 Wilderness Labs Inc.  |  Legal   |   CC BY-SA
This webpage is licensed under a Creative Commons Attribution-ShareAlike License.