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.

Kirk K

Member Since 18 Apr 2012
Offline Last Active Jun 14 2013 02:03 AM
-----

Topics I've Started

NetduinoGo Shieldbase SPI hookup

24 May 2013 - 10:59 PM

My fellow Netgoers,

 

I need your help in hooking up a Parallax Gyroscope Module 3-Axis L3G4200D to the shieldbase. I am using the latest firmware for both my GO and Shieldbase, VS2012, .NET Micro 4.2, etc. Based on the pinout diagram provided by Gutworks, I hooked up the devices as followed:

 

Shieldbase   ->   Gyroscope Module 3-Axis L3G4200D Pin D04 NSS  -> CS Pin D11 MOSI -> SDA Pin D12 MISO -> SDO Pin D13 SPCK -> SCL

using System;using System.Threading;using Microsoft.SPOT;using Microsoft.SPOT.Hardware;using SecretLabs.NETMF.Hardware;using SecretLabs.NETMF.Hardware.NetduinoGo;using NetduinoGo;namespace Netduino_Go{    public class Program    {        public static void Main()        {            // write your code here            using (ShieldBase sb = new ShieldBase(GoSockets.Socket5))            {                //Data to write to gyroscope                byte x_MSB = 0x29;                byte x_LSB = 0x28;                byte y_MSB = 0x2B;                byte y_LSB = 0x2A;                byte z_MSB = 0x2D;                byte z_LSB = 0x2C;                byte CTRL_Register1 = 0x20;                byte CTRL_Register2 = 0x21;                byte CTRL_Register3 = 0x23;                //Setup device                SPI device = new SPI(new SPI.Configuration(Pins.GPIO_PIN_D4,   //Chip Select pin                                                false,              //Chip Select Active State                                                0,                  //Chip Select Setup Time                                                0,                  //Chip Select Hold Time                                                true,               //Clock Idle State                                                true,               //Clock Edge                                                10000,               //Clock Rate (kHz)                                                sb.SPI_Devices.SPI1));                device.Write(new byte[] { CTRL_Register1, 0x1F });                device.Write(new byte[] { CTRL_Register2, 0x08 });                device.Write(new byte[] { CTRL_Register3, 0x80 });                Thread.Sleep(1);                while (true)                {                    byte[] data = new byte[6];                    device.WriteRead(new byte[] { x_MSB, x_LSB, y_MSB, y_LSB, z_MSB, z_LSB }, data);                    var x = ((read[0] << 8) | read[1]);                    Debug.Print(x.ToString());                    Thread.Sleep(5);                }            }        }    }}

I am not sure what I am doing here, but I am reading values of 255 on all  from the gyro. Any help would be helpful?

 

ps. I have a competition between a fellow co-worker who is using an Arduino. May have to get +2 to compete against him.

 

 

Thanks,

Kirk


ShieldBase Threading

27 June 2012 - 04:48 PM

Hi,

I have been testing my Netduino Go with a couple Hitec HS311 servos, a Parallex IR sensor and a Parallax Ultrasonic PING))) distance sensor. I am creating this application to read the pins of the ShieldBase on their own thread then update some values back on the initial thread. This works fine when I create the application and a thread that reads the IR input pin state then fires an event based on the state change of the pin. However, when I create a second thread for the PING)) sensor and run the application I get an exception thrown from the ShieldBase. With this I created a locking object that locks each read or write to the ShieldBase pins. This could have an adversed effect based on how long it takes to release the lock on the object. My question is, how does Netduino Go and the ShieldBase handles multi-threading when it comes to the read/write operation? It seems to me that only one pin can be read or written to at any given operation.

Again, I must admit this is my second programming code and so I ask for your patience and understanding. What I am trying to accomplished is to elimate the need to create a while loop that have all my reads and write operation and use event driven style to does different actions that can happen simultaneously.


Here is my code with thread locking:
namespace Multithread_Robot_Application
{


	//Event signature
	public delegate void ObjectStateChangedHandler(object sender, object state);

	public class Program
	{
		object _Lock = new object();
		public event ObjectStateChangedHandler IRValueChanged;
		//Private member variables
		static InputPort _IRSensor;
		static TristatePort _PingSensor;
		static PWM _Servo1;
		static PWM _Servo2;
		static ShieldBase _ShieldBase;

		/// <summary>
		/// Sets the degree to which servo 1 moves to. value 0.1 - 180
		/// </summary>
		public double Servo1Degree
		{
			set
			{
				_Servo1.Duration = CalculatePulseFromDegrees(value);
			}

		}
		/// <summary>
		/// Sets the degree to which servo 2 moves to. value 0.1 - 180
		/// </summary>
		public double Servo2Degree
		{
			set
			{
				_Servo2.Duration = CalculatePulseFromDegrees(value);
			}
		}


		public static void Main()
		{
			// write your code here
			Program _Application = new Program(); //Create the application instance
			_ShieldBase = new ShieldBase(GoSockets.Socket4); //Initialize shieldbase on Go Socket

			//Register for the Infared value changed
			_Application.IRValueChanged += (obj, state) =>
			{
				Debug.Print("The interrupt value is: " + (bool)state);

				//Simulate a double swing door opening and closing
				if((bool)state)
				{
					//Doors open
					_Application.Servo1Degree = 180.0;
					_Application.Servo2Degree = 0.9;
				}
				else
				{
					//Doors closed 
					_Application.Servo1Degree = 90.0;
					_Application.Servo2Degree = 90.0;
				}
			};

			//Initial components connected to the shieldbase
			_Application.InitializeComponents();

			//Starts a thread to listen to the ping sensor
			Thread t1 = new Thread(_Application.TriggerPingSensor);
			t1.Start();

			//Starts a thread to listen to the IR sensor
			Thread t2 = new Thread(_Application.TriggerIRSensor);
			t2.Start();

			Thread.Sleep(Timeout.Infinite);

		}

		/// <summary>
		/// Initializes all the components connected to the 
		/// </summary>
		private void InitializeComponents()
		{
			_Servo1 = new PWM(_ShieldBase.PWMChannels.PWM_0, 20000, 750, PWM.ScaleFactor.Microseconds, false);
			_Servo2 = new PWM(_ShieldBase.PWMChannels.PWM_1, 20000, 1500, PWM.ScaleFactor.Microseconds, false);
			_Servo1.Start();
			_Servo2.Start();

			//Not sure why I could not initialize these on a digital pin 
			_PingSensor = new TristatePort(_ShieldBase.Pins.GPIO_PIN_A1, false, true, Port.ResistorMode.Disabled);
			_IRSensor = new InputPort(_ShieldBase.Pins.GPIO_PIN_A0, false, Port.ResistorMode.Disabled);
		}




		/// <summary>
		/// Calculates the duration for the degree
		/// </summary>
		/// <param name="degree">double: 0.1 - 180.</param>
		/// <returns>uint  representation</returns>
		private uint CalculatePulseFromDegrees(double degree)
		{

			uint neutralPulse = 1500; //This represent the neutral pulse time (in microsecond) of the servo typically when the servo is at 90 degrees. ; 
			float factor = neutralPulse / 90; //This value represent pulse time when the servo is at 1 degree; 
			uint outPulse = (uint)(factor * degree);
			return outPulse;
		}
		#region Public Methods

		/// <summary>
		/// This reads the state of the port on a Parallax IR Sensor.
		/// </summary>
		public void TriggerIRSensor()
		{
			bool oldValue = false;
			while(true)
			{
				bool ret;
				lock(_Lock)
				{
					ret = _IRSensor.Read();
				}
				if(ret != oldValue)
				{
					oldValue = ret;
					Debug.Print(ret.ToString());

					//Raise event if the Infared value changes
					if(IRValueChanged != null)
						IRValueChanged(this, ret);
				}


				Thread.Sleep(50);
			}
		}

		/// <summary>
		/// Uses a tristate port to send a pulse to the 
		/// </summary>
		public void TriggerPingSensor()
		{
			while(true)
			{
				var val = false;
				lock(_Lock)
				{
					_PingSensor.Active = true;
					_PingSensor.Write(true);
					_PingSensor.Active = false;
				}
				int timeElapsed = 0;
				while(val != true && timeElapsed != 18)
				{
					timeElapsed++;
					lock(_Lock)
					{
						val = _PingSensor.Read();
					}
					if(val)
						Debug.Print(val.ToString() + " @ " + timeElapsed);
					Thread.Sleep(1);
				}
				Thread.Sleep(2);
				Debug.Print(val.ToString());
			}
		}
		#endregion
	}
}

Thanks

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.