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.
Photo

MicroMag 3-axis Magnetometer


  • Please log in to reply
2 replies to this topic

#1 Kamal

Kamal

    New Member

  • Members
  • Pip
  • 7 posts
  • LocationPlanet Earth

Posted 25 August 2010 - 07:53 PM

Hi, this is my first "real" project using the Netduino. I don't have a lot of experience in hardware programming and the code below is a simple port of what I already found online. But the code combined with the datasheet taught me a few things. I just hope what I learned is consistent with other hardware and datasheet.

I tested the results against a friend's phone that has a built-in compass and they agreed. :)

http://www.sparkfun....products_id=244

I don't have a diagram to show you but hopefully the pin assignments will help.

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

namespace Magnetometer
{
    public class Program
    {

        static Cpu.Pin SCLK = Pins.GPIO_PIN_D7;
        static Cpu.Pin MISO = Pins.GPIO_PIN_D6;
        static Cpu.Pin MOSI = Pins.GPIO_PIN_D5;
        static Cpu.Pin SSNOT = Pins.GPIO_PIN_D4;
        static Cpu.Pin DRDY = Pins.GPIO_PIN_D3;
        static Cpu.Pin RESET = Pins.GPIO_PIN_D2;
        static OutputPort sclk = new OutputPort(SCLK, false);
        static OutputPort reset = new OutputPort(RESET, false);
        static OutputPort mosi = new OutputPort(MOSI, false);
        static OutputPort ssnot = new OutputPort(SSNOT, false);
        static InputPort miso = new InputPort(MISO, false, Port.ResistorMode.Disabled);
        static InputPort drdy = new InputPort(DRDY, false, Port.ResistorMode.Disabled);
        static float x = 0, y = 0, z = 0;
        static float heading = 0;

        public static void Main()
        {
            // write your code here
            while (true)
            {
                x = QueryMag(0);
                y = QueryMag(1);
                z = QueryMag(2);
                heading = getHeading(x, y, z);
                Debug.Print("X: " + x.ToString());
                Debug.Print("Y: " + y.ToString());
                Debug.Print("Z: " + z.ToString());
                Debug.Print("Heading: " + heading.ToString());
                Thread.Sleep(5000);

            }
        }

        private static float getHeading(float x, float y, float z)
        {
            heading = 0;
            if ((x == 0) && (y < 0))
                heading = (float)(System.Math.PI / 2.0);
            if ((x == 0) && (y > 0))
                heading = (float)(3.0 * System.Math.PI / 2.0);
            if (x < 0)
                heading = (float)(System.Math.PI - MathLib.Atan(y / x));
            if ((x > 0) && (y < 0))
                heading = (float)MathLib.Atan(y / x) * -1;
            if ((x > 0) && (y > 0))
                heading = (float)(2.0 * System.Math.PI - MathLib.Atan(y / x));
            return (float)(heading * 180 / System.Math.PI);

        }

        private static void ResetMag()
        {
            reset.Write(false);
            Thread.Sleep(2);
            reset.Write(true);
            Thread.Sleep(2);
            reset.Write(false);
            Thread.Sleep(2);
            Debug.Print("resetted");
        }

        private static void sendBit(bool hilo)
        {
            mosi.Write(hilo);
            Thread.Sleep(2);
            sclk.Write(true);
            Thread.Sleep(2);
            sclk.Write(false);
            Thread.Sleep(2);
        }

        private static float QueryMag(int axis)
        {
            ResetMag();

            sendBit(false);
            sendBit(true);
            sendBit(true);
            sendBit(false);
            sendBit(false);
            sendBit(false);

            switch (axis)
            {
                case 0 :
                    sendBit(false);
                    sendBit(true);
                    break;
                case 1 :
                    sendBit(true);
                    sendBit(false);
                    break;
                case 2 :
                    sendBit(true);
                    sendBit(true);
                    break;
                default:
                    break;
            }

            while (drdy.Read() == false)
            {
                Debug.Print("waiting for DRDY");
            }

            long total = 0;

            long sign = recieveBit();

            for (int ii = 14; ii >= 0; ii--)
            {
                long thisbit = recieveBit();
                thisbit = thisbit << ii;
                total = total | thisbit;
                Debug.Print("fired");
            }

            if (sign == 1)
            {
                total = total - 32768;
            }

            return total;
        }

        private static long recieveBit()
        {
            sclk.Write(true);
            Thread.Sleep(2);
            int bit;
            if (miso.Read() == true)
                bit = 1;
            else
                bit = 0;
            Thread.Sleep(2);
            sclk.Write(false);
            Thread.Sleep(2);
            return bit;
        }

    }
}

I needed a math library for the atan, which I got from http://www.microfram...-with-full-net/

I hope others find this useful.

#2 Chris Walker

Chris Walker

    Secret Labs Staff

  • Moderators
  • 7767 posts
  • LocationNew York, NY

Posted 26 August 2010 - 07:20 AM

Nice, Kamal! Now to think what I can build with compass logic... Chris

#3 James

James

    Advanced Member

  • Members
  • PipPipPip
  • 56 posts

Posted 04 December 2010 - 11:11 PM

I tried out the code with the same part from SparkFun and it worked great for me. Thanks Kamal!




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

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.