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.

berr08

Member Since 04 Aug 2014
Offline Last Active Aug 11 2014 08:17 PM
-----

Posts I've Made

In Topic: Trying to recreate Arduino Code for Netduino, running into issues.

11 August 2014 - 01:36 PM

Interesting, I will give this a shot, thanks for the feedback!


In Topic: Slow I2C Sensor Reads, is this normal?

11 August 2014 - 01:35 PM

Thanks guys, it seems that the netduino isn't the way to go for this project atleast since the speed of the readings needs to be so high.  I look forward to using this again when the project requirements will allow.  Thanks again to all for your help with this!

 

Robert.


In Topic: Slow I2C Sensor Reads, is this normal?

08 August 2014 - 03:46 PM

Thanks that did speed it up some more doing the 1 read of 6 bytes.  I implemented the stopwatch function and here are the findings:

 

Setting the Device Config: 0Ms - 40Ms

magDevice.Config = mag0;

Sending the setup routine:  159Ms - 165Ms

magDevice.Execute(refillTrans, 1);
magDevice.Execute(setTrans, 1);
magDevice.Execute(resetTrans, 1);
magDevice.Execute(takeTrans, 1);

Checking reading is ready (Status): 0Ms - 40Ms

while (((statusBit[0] & 0x01) != 1))
{
     magDevice.Execute(statusTrans, 1);
}

Reading the 6byte reading: 40Ms

magDevice.Execute(getReadings, 1);

//#Definition from outside loop:
//byte[] dataXYZ = new byte[6];
//I2CDevice.I2CTransaction[] getReadings = new I2CDevice.I2CTransaction[2];
//getReadings[0] = I2CDevice.CreateWriteTransaction(new byte[] { 0x00 });
//getReadings[1] = I2CDevice.CreateReadTransaction(dataXYZ);

Assigning to the Byte Structure 0Ms

magData.mag0Xl = dataXYZ[0];
magData.mag0Xh = dataXYZ[1];
magData.mag0Yl = dataXYZ[2];
magData.mag0Yh = dataXYZ[3];
magData.mag0Zl = dataXYZ[4];
magData.mag0Zh = dataXYZ[5];

The entire loop of the 3 sensors takes: 600Ms - 650Ms

 

In order to get to the arduino # of readings I need the 3 sensors to take about 30 milliseconds, which I'm thinking might not be near possible?  Unless I'm still doing some really dumb things, which is always possible LOL.


In Topic: Slow I2C Sensor Reads, is this normal?

08 August 2014 - 12:41 PM

Thanks again for the help, I have changed the way I am doing this up to take your suggestions.  It is still only geting 2 or 3 reads (of all the sensors) a second, an improvement over the 1 a second but the arduino is getting 25 to 35 reads, so I'm still not where I need it to be.

 

I've changed the code to use the 1 main i2cdevice and swap the config, I created all the transactions outside of the loop and am reading all the data registers in one transaction.  Any other input would be great **braces for newb jokes**  Code below for the MemSic MMC3416xPJ sensor:

using System;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using System.Threading;

namespace UDPMag_NetduinoPlus2
{
    class MagSensor
    {
        private MagDataStruct magData = new MagDataStruct();
        private I2CDevice magDevice = null;
        private bool collect = false;
        public struct MagDataStruct
        {
            public byte mag0Xh;
            public byte mag0Yh;
            public byte mag0Zh;
            public byte mag0Xl;
            public byte mag0Yl;
            public byte mag0Zl;

            public byte mag1Xh;
            public byte mag1Yh;
            public byte mag1Zh;
            public byte mag1Xl;
            public byte mag1Yl;
            public byte mag1Zl;

            public byte mag2Xh;
            public byte mag2Yh;
            public byte mag2Zh;
            public byte mag2Xl;
            public byte mag2Yl;
            public byte mag2Zl;
        }

        public delegate void DataReceived(MagDataStruct data);

        public event DataReceived OnDataReceived = null;

        public bool SensorRunning
        {
            get { return collect; }
        }

        public MagSensor()
        {
            // Create a delegate of type "ThreadStart", which is a pointer to the 
            // worker thread's main function
            ThreadStart delegateWorkerThread = new ThreadStart(Main);

            // Next create the actual thread, passing it the delegate to the main function
            Thread threadWorker = new Thread(delegateWorkerThread);

            // Now start the thread.
            threadWorker.Start();

        }

        public void StartCollecting()
        {
            collect = true;
        }
        public void StopCollecting()
        {
            collect = false;
        }

        #region The actual worker thread code
        /// <summary>
        /// This is a method which does the work of the worker thread.
        /// </summary>
        private void Main()
        {
            magDevice = new I2CDevice(null);
            I2CDevice.Configuration mag0 = new I2CDevice.Configuration(0x30, 400);
            I2CDevice.Configuration mag1 = new I2CDevice.Configuration(0x31, 400);
            I2CDevice.Configuration mag2 = new I2CDevice.Configuration(0x32, 400);
            
            I2CDevice.I2CTransaction[] refillTrans = new I2CDevice.I2CTransaction[1];
            refillTrans[0] = I2CDevice.CreateWriteTransaction(new byte[] { 0x07, 0x80 });

            I2CDevice.I2CTransaction[] setTrans = new I2CDevice.I2CTransaction[1];
            setTrans[0] = I2CDevice.CreateWriteTransaction(new byte[] { 0x07, 0x20 });

            I2CDevice.I2CTransaction[] resetTrans = new I2CDevice.I2CTransaction[1];
            resetTrans[0] = I2CDevice.CreateWriteTransaction(new byte[] { 0x07, 0x40 });

            I2CDevice.I2CTransaction[] takeTrans = new I2CDevice.I2CTransaction[1];
            takeTrans[0] = I2CDevice.CreateWriteTransaction(new byte[] { 0x07, 0x01 });
            
            byte[] statusBit = new byte[1];
            I2CDevice.I2CTransaction[] statusTrans = new I2CDevice.I2CTransaction[2];
            statusTrans[0] = I2CDevice.CreateWriteTransaction(new byte[] { 0x06 });
            statusTrans[1] = I2CDevice.CreateReadTransaction(statusBit);
            
            byte[] dataXl = new byte[1];
            byte[] dataXh = new byte[1];
            byte[] dataYl = new byte[1];
            byte[] dataYh = new byte[1];
            byte[] dataZl = new byte[1];
            byte[] dataZh = new byte[1];
            I2CDevice.I2CTransaction[] getReadings = new I2CDevice.I2CTransaction[12];
            getReadings[0] = I2CDevice.CreateWriteTransaction(new byte[] { 0x00 });
            getReadings[1] = I2CDevice.CreateReadTransaction(dataXl);
            getReadings[2] = I2CDevice.CreateWriteTransaction(new byte[] { 0x01 });
            getReadings[3] = I2CDevice.CreateReadTransaction(dataXh);
            getReadings[4] = I2CDevice.CreateWriteTransaction(new byte[] { 0x02 });
            getReadings[5] = I2CDevice.CreateReadTransaction(dataYl);
            getReadings[6] = I2CDevice.CreateWriteTransaction(new byte[] { 0x03 });
            getReadings[7] = I2CDevice.CreateReadTransaction(dataYh);
            getReadings[8] = I2CDevice.CreateWriteTransaction(new byte[] { 0x04 });
            getReadings[9] = I2CDevice.CreateReadTransaction(dataZl);
            getReadings[10] = I2CDevice.CreateWriteTransaction(new byte[] { 0x05 });
            getReadings[11] = I2CDevice.CreateReadTransaction(dataZh);

            // Loop forever
            while (true)
            {
                if (collect)
                {
                    //DateTime t1 = DateTime.Now;

                    //Sensor 0:
                    magDevice.Config = mag0;
                    magDevice.Execute(refillTrans, 1);
                    magDevice.Execute(setTrans, 1);
                    magDevice.Execute(resetTrans, 1);
                    magDevice.Execute(takeTrans, 1);
                    while ((statusBit[0] & 0x01) != 1)
                    {
                        magDevice.Execute(statusTrans, 1);
                    }
                    magDevice.Execute(getReadings, 1);

                    magData.mag0Xl = dataXl[0];
                    magData.mag0Xh = dataXh[0];
                    magData.mag0Yl = dataYl[0];
                    magData.mag0Yh = dataYh[0];
                    magData.mag0Zl = dataZl[0];
                    magData.mag0Zh = dataZh[0];

                    //Sensor 1:
                    magDevice.Config = mag1;
                    magDevice.Execute(refillTrans, 1);
                    magDevice.Execute(setTrans, 1);
                    magDevice.Execute(resetTrans, 1);
                    magDevice.Execute(takeTrans, 1);
                    while ((statusBit[0] & 0x01) != 1)
                    {
                        magDevice.Execute(statusTrans, 1);
                    }
                    magDevice.Execute(getReadings, 1);

                    magData.mag1Xl = dataXl[0];
                    magData.mag1Xh = dataXh[0];
                    magData.mag1Yl = dataYl[0];
                    magData.mag1Yh = dataYh[0];
                    magData.mag1Zl = dataZl[0];
                    magData.mag1Zh = dataZh[0];

                    //Sensor 2:
                    magDevice.Config = mag2;
                    magDevice.Execute(refillTrans, 1);
                    magDevice.Execute(setTrans, 1);
                    magDevice.Execute(resetTrans, 1);
                    magDevice.Execute(takeTrans, 1);
                    while ((statusBit[0] & 0x01) != 1)
                    {
                        magDevice.Execute(statusTrans, 1);
                    }
                    magDevice.Execute(getReadings, 1);

                    magData.mag2Xl = dataXl[0];
                    magData.mag2Xh = dataXh[0];
                    magData.mag2Yl = dataYl[0];
                    magData.mag2Yh = dataYh[0];
                    magData.mag2Zl = dataZl[0];
                    magData.mag2Zh = dataZh[0];
                    
                    //DateTime t2 = DateTime.Now;
                    //TimeSpan ts = t2 - t1;
                    //Debug.Print(ts.Minutes.ToString() + ":" + ts.Seconds.ToString() + ":" + ts.Milliseconds.ToString());
                    
                    // Call Event!
                    if (OnDataReceived != null)
                        OnDataReceived(magData);
                }
            }
        }
        #endregion
    }
}


In Topic: Slow I2C Sensor Reads, is this normal?

07 August 2014 - 07:53 PM

Thanks for the reply.  I tried making each device 1x and when making a second i2cdevice I get an 'System.InvalidOperationException'.  So after toying with it for a while the only way I got it to work was with the using, which I know isn't officient which is why I asked what I can do.  I'm on here to hopefully learn more about this device and working with this compact library.  The way I do things, which I assume is differently that you, is to get things to work and then see what can be done to make it better.  Especially in an instance where the community isn't super responsive yet and there are only a handfull of examples I came across which all appearantly did things the wrong way.

 

I assume what I am doing is possible without a library, I see the example you linked to used the M2Mqtt library, was hoping to not use libraries...


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.