Im trying to read from a CHRobotics 6dm using my arduino uno and Im unable to read the values for some reason. Ive transferred my code from Visual C# and am running no errors so Im not sure why Im not getting anything from the sensor. Ive also tried code from a separate forum post with no luck as well, http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1286541868 , Ive attached my code and if you see anything out of the ordinary any advice would be great.
This is the device. It uses the UART onboard my Arduino. pin 0 of the digital pins.
I was previously using a Netduino Plus but some of its functionality has stopped. Here is the code for the Netduino. I took most of it over to the Arduino UNO and modified it to the point where theres no errors but I guess there is something I am forgetting.....
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.Read(CHRbuffer, 0, CHRbuffer.Length);
serialPort1.Write(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 ShowCalc = YawCalc.ToString();
//Debug.Print("Yaw");
//Debug.Print(ShowCalc);
int PitchVal = (int)((((CHRbuffer[i++] << 8) | CHRbuffer[i++]) << 16) >> 16);
String ShowPitch = PitchVal.ToString();
double PitchCalc = PitchVal * 0.0109863;
String ShowPCalc = PitchCalc.ToString();
//Debug.Print("Pitch");
//Debug.Print(ShowPCalc);
int RollVal = (int)((((CHRbuffer[i++] << 8) | CHRbuffer[i++]) << 16) >> 16);
String ShowRoll = RollVal.ToString();
double RollCalc = RollVal * 0.0109863;
String ShowRCalc = RollCalc.ToString();
//Debug.Print("Roll");
//Debug.Print(ShowRCalc);
string CTAP = "$CTAP,Y," + YawCalc + ",P," + PitchCalc + ",R," + RollCalc + "\r\n";
Debug.Print(CTAP);
sw.Write(CTAP);
}
else
{
i++;
}
The index into the array that defines where to store the data needs to be either global or static, and needs to be defined/initialized outside of the while loop.
Okay so Ive gone back to the basics and just read from the port and then output that data back so I know the unit is working and just outputting data. Now I need to figure out how to put it in a variable and then parse that data to get the pitch roll values like I want. Do I use the while loop like I had or is there a more efficient way to do this?
Even when I run this code written by Honk as a test I get 0 values on the pitch roll yaw. I have the TX pin of the unit connected to the RX pin of the digital pin of the arduino so its not a connectivity problem.
My test with this code shows the data flowing freely and quickly at 115200
For those of us who don't have that device laying around, what data are you seeing from the device, and what do you need to parse from the data stream?
You really need to consider the structure of the message, and what damage you could do if your receiver starts working part-way through a message.
What are the message delimiters?
Ideally, you should just read and discard characters until you have either seen tha last of the previous message, or seen the start of a new one.
According to the CHR6dm data sheet the data is output in byte chunks starting with an 's' as the beginning of the packet burst and follows with 'n' 'p'. There is two 8 bit byte values following that describes the packet type and another 8 bit byte value to tell how long the packet is. Following these values are the registers in 8 bit format that are the actual pitch roll yaw values each taking up two registers so there would be a Pitch1/Pitch2 Roll1/Roll2 Yaw1/Yaw2 that when put together give the values for Pitch Roll and Yaw. There are accelerometer packets following these but Im not interested in those values. Which is where the calculation "int YawVal = (int)((((CHRbuffer[i++] << 8) | CHRbuffer[i++]) << 16) >> 16);" comes into play to decipher the byte registers.
When I view the data in the serial monitor I see the "snp" start bits and everything after is the register data looks like "snp· ÿþ<O" in the serial window but I know is valid data but the serial window doesnt decipher the registers.
OK, so just loop reading and discarding input until you find an 's'.
Then read your message in one character at a time, or, having seen the 's', wait until available returns 16, and then read them in.
Yes ideally I am waiting for the 's" byte and then 7 bytes after that are the values I am looking for.
According to the CHR6dm data sheet the data is output in byte chunks starting with an 's' as the beginning of the packet burst and follows with 'n' 'p'. There is two 8 bit byte values following that describes the packet type and another 8 bit byte value to tell how long the packet is. Following these values are the registers in 8 bit format that are the actual pitch roll yaw values each taking up two registers so there would be a Pitch1/Pitch2 Roll1/Roll2 Yaw1/Yaw2 that when put together give the values for Pitch Roll and Yaw.
So, the 7 bytes of interest are the 'n', 'p', 2 for packet type, 1 for packet size, and 2 for pitch?
Sorry for the mistype PaulS its the 'snp' PacketType and PacketNumber are discarded. From my previous coding it was the first 7 bytes that are discarded and everything after those 7 bytes are what Im looking for. I just use the 's' as the start of the packet.