Wednesday, March 23, 2011

Followup with Netduino

Just thought I would follow up with what I have done with the Netduino Plus that I am using. Currently this morning when I tested the unit with the GPS and IMU packages I am able to bring this data in and output again in a format that I want for logging purposes. The code I used is below. I did find that coding the Netduino a little difficult at first but once I got used to the coding structure that it was much easier to diagnose problems and add features than the Arduino. I will also add a schematic of each setup later.


using System;
using System.Collections;
using System.IO.Ports;
using System.IO;
using System.Threading;
using System.Text;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using SecretLabs.NETMF.Hardware;
using SecretLabs.NETMF.Hardware.NetduinoPlus;
 
 
namespace NetduinoPlusGPS
{
public class Program
{
public static void Main()
{
//create a connection to the led to show we're running
OutputPort led = new OutputPort(Pins.ONBOARD_LED, false);
//instantiate the serial port connection
SerialPort serialPort1 = new SerialPort("COM1", 57600, Parity.None, 8, StopBits.One);
serialPort1.Open();
SerialPort serialPort2 = new SerialPort("COM2", 115200, Parity.None, 8, StopBits.One);
serialPort2.Open();
 
while (true)
{
int GPStoRead = serialPort1.BytesToRead;
int CHRtoRead = serialPort2.BytesToRead;
//*start reading the stream
if (GPStoRead > 0)
{
//blink the LED to show we got data
led.Write(true);
//Thread.Sleep(100);
led.Write(false);
// get the waiting ++data
byte[] GPSbuffer = new byte[GPStoRead];
//serialPort1.Flush();
serialPort1.Read(GPSbuffer, 0, GPSbuffer.Length);
serialPort1.Write(GPSbuffer, 0, GPSbuffer.Length);
String GPSMessage = new String(System.Text.Encoding.UTF8.GetChars(GPSbuffer));
Debug.Print(GPSMessage);
}
if (CHRtoRead > 12)
{
int i = 0;
//blink the LED to show we got data
led.Write(true);
//Thread.Sleep(100);
led.Write(false);
// get the waiting data
byte[] CHRbuffer = new byte[CHRtoRead];
//serialPort2.Flush();
serialPort2.Read(CHRbuffer, 0, CHRbuffer.Length);
if (CHRbuffer[i] == 's')
{
//Print final result
i = i + 7;
int YawVal = (int)((((CHRbuffer[i++] << 8) | CHRbuffer[i++]) << 16) >> 16);
String ShowYaw = YawVal.ToString();
double YawCalc = YawVal * 0.0109863;
String ShowYCalc = YawCalc.ToString("F2");
int PitchVal = (int)((((CHRbuffer[i++] << 8) | CHRbuffer[i++]) << 16) >> 16);
String ShowPitch = PitchVal.ToString();
double PitchCalc = PitchVal * 0.0109863;
String ShowPCalc = PitchCalc.ToString("F2");
int RollVal = (int)((((CHRbuffer[i++] << 8) | CHRbuffer[i++]) << 16) >> 16);
String ShowRoll = RollVal.ToString();
double RollCalc = RollVal * 0.0109863;
String ShowRCalc = RollCalc.ToString("F2");
String CTAP = "$CTAP,R," + ShowRCalc + ",P," + ShowPCalc + ",Y," + ShowYCalc;
Debug.Print(CTAP);
byte[] CTAPArray = Encoding.UTF8.GetBytes(CTAP);
serialPort1.Write(CTAPArray, 0, CTAPArray.Length);
}
else
{
i++;
}
}
Thread.Sleep(500);
//serialPort1.Flush();
}
}
}
}

Tuesday, March 22, 2011

Late update

So yes its been a long time and a lot has transpired for this project. From flipping to a Netduino Plus to flipping back to Arduino to wanting to go back to the Netduino again LOL. Both have their own advantages and disadvantages. The only problem Im having with the Netduino is on the SDA/SCL lines where my barometer is supposed to plug into. When I wire into these pins I get no data or erratic data and when I wire the same sensor to the Arduino I get perfect data every time. I'm waiting on a logic analyzer to arrive to properly test these pins and hopefully diagnose what is going on.

In the interim I have been working on a NMEA parser for the Arduino Uno as well bringing in both GPS and Pitch/Roll data on the Uno. Using the NewSoftSerial driver (http://arduiniana.org/libraries/newsoftserial/) I am able to bring both sensors in and view the data. Next on my task list is to log the data to an OpenLog microSD data logger. (http://www.sparkfun.com/products/9530)

Here is the code I am using to parse out the NMEA strings. It works perfectly and is able to parse multiple NMEA messages on the same port. So now if I need to I can pull out data from a NMEA string like say the Altitude and use that somewhere else in my code.

int incomingByte = 0;      // for incoming serial data
int index = 0;
int count,i = 0;
String GPSStrings[20];
String field;
String message;
void setup() {
  Serial.begin(57600);        // opens serial port, sets data rate to 9600 bps
  message = String("");
  field = String("");
}

void loop() {

  // wait for data to be present on com port.
  if (Serial.available() > 0) {
    // read the incoming byte:
    incomingByte = Serial.read();
    message.concat(char(incomingByte)); //append char to message
   


    if(incomingByte == '\n'){  //if newline is found
      Serial.write("NMEA Message: ");
      Serial.print(message);// print message


      if(message.indexOf(",") != -1){ //if a comma is in message:
       
        do{
          index = message.indexOf(","); //find first comma
          GPSStrings[i] = message.substring(0,index); //grab data to first comma
          message = message.substring(index+1); //toss data to first comma       
                   
          Serial.println(GPSStrings[i]);
         
        }
        while(index != -1); //while there is still a comma
        i++;
      } 
     
      else
        Serial.print(message);//if there was no comma to begin with just print the message.
       
      message = String(""); //wipe out message for next line 
    }
  }
}