Pages: 1 [2] 3   Go Down
Author Topic: Reading Pitch Roll from CHR6DM  (Read 3218 times)
0 Members and 1 Guest are viewing this topic.
Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 239
Posts: 24371
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

0
Offline Offline
Newbie
*
Karma: 0
Posts: 35
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Yes ideally I am waiting for the 's" byte and then 7 bytes after that are the values I am looking for.
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 239
Posts: 24371
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
while (Serial.read () != 's') ;
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 549
Posts: 46113
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Yes ideally I am waiting for the 's" byte and then 7 bytes after that are the values I am looking for.

Quote
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?
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 35
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

0
Offline Offline
Newbie
*
Karma: 0
Posts: 35
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

AWOL

But if I do that while statement wont it just take everything other than the 's' regardless of character location? For example what if it starts reading mid stream, sure it could get data but it could start at the Yaw Accelerometer and thats not the data Im looking for.

N
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 239
Posts: 24371
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
But if I do that while statement wont it just take everything other than the 's' regardless of character location?
Absolutely, so when the "while" exits, you can be sure you've received a start of packet.
The next character should be an 'n'
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

0
Offline Offline
Newbie
*
Karma: 0
Posts: 35
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Here is my updated code. Already know it doesnt work. But it is reading the serial port because when I unplug the pitch roll sensor the 0's stop flowing....

Code:
void loop()                   
{
 
  while(Serial.read() != 'p');
    int PT1 = Serial.read();
    int PT2 = Serial.read();
    int DATAN = Serial.read();
    byte Yaw1 = Serial.read();
    byte Yaw2 = Serial.read();
    byte Pitch1 = Serial.read();
    byte Pitch2 = Serial.read();
    byte Roll1 = Serial.read();
    byte Roll2 = Serial.read();
   
    int YawVal = (int)((((Yaw1 << 8) | Yaw2) << 16) >> 16);
     double YawCalc = YawVal * 0.0109863;
                                                                       
     int PitchVal = (int)((((Pitch1 << 8) | Pitch2) << 16) >> 16);                       
     double PitchCalc = PitchVal * 0.0109863;       
                                               
     int RollVal = (int)((((Roll1 << 8) | Roll2) << 16) >> 16);                           
     double RollCalc = RollVal * 0.0109863;       
       
     Serial.print("$CTAP,R,");
     Serial.print(RollCalc);
     Serial.print(",P,");
     Serial.print(PitchCalc);
     Serial.print(",Y,");
     Serial.println(YawCalc);     

}
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 239
Posts: 24371
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
while(Serial.read() != 'p');
    int PT1 = Serial.read();
You are STILL reading characters that may not be there!
Why don't you use "available()"?

http://arduino.cc/en/Serial/Read
http://arduino.cc/en/Serial/Available
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

0
Offline Offline
Newbie
*
Karma: 0
Posts: 35
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Ive tested this bit of code and I can fill the buffer and spit the buffer out.

Code:
void loop()                   
{
  byte CHRbuffer[17];
  int CHR;
  if (Serial.available())
  {
    while(Serial.available())
    {
      CHR = Serial.read();
      CHRbuffer[j] = CHR;
      j++;
    }
  }
 
  j=0;
 
  while (Serial.available())
  {
    Serial.print(CHRbuffer[j]);
    j++;
  }
 
}
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 239
Posts: 24371
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

So why don't you apply what you've done there on your code above?
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

0
Offline Offline
Newbie
*
Karma: 0
Posts: 35
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Heres what I have done now and the code seems to hold other than the fact sometimes Ill get erroneous data or the data will stop randomly. Is the arduino prone to losing data at high baud rates?

Code:
float CHR_yawClean;
float CHR_pitchClean;
float CHR_rollClean;

byte CHRbuffer[22];
#define YAW_FACT 0.0109863F
#define Pitch_Fact 0.0109863F
#define Roll_Fact 0.0109863F

int j = 0;

void setup() 
{
  Serial.begin(115200);
   
}

void loop()                   
{
  int CHR_yawWord, CHR_pitchWord, CHR_rollWord;
 
  byte CHR;
  if (Serial.available())
  {
    while(Serial.available())
    {     
      CHRbuffer[j] = Serial.read();
      if (CHRbuffer[j] == 's')
      {
        char n = Serial.read();
        char p = Serial.read();
        byte PT1 = Serial.read();
        byte PT2 = Serial.read();
        byte DN = Serial.read();
        byte space = Serial.read();
       
        byte Yaw1 = Serial.read();
        byte Yaw2 = Serial.read();
        byte Pitch1 = Serial.read();
        byte Pitch2 = Serial.read();
        byte Roll1 = Serial.read();
        byte Roll2 = Serial.read();
       
        CHR_yawWord = (int)Yaw1 << 8;
        CHR_yawWord |= Yaw2;
     
        CHR_pitchWord = (int)Pitch1 << 8;
        CHR_pitchWord |= Pitch2;
     
        CHR_rollWord = (int)Roll1 << 8;
        CHR_rollWord |= Roll2;
     
        CHR_yawClean = (CHR_yawWord * YAW_FACT);
        CHR_pitchClean = (CHR_pitchWord * Pitch_Fact);
        CHR_rollClean = (CHR_rollWord * Roll_Fact);
       
        Serial.print("YAW: ");
        Serial.print(CHR_yawClean);
        Serial.print(" Pitch: ");
        Serial.print(CHR_pitchClean);
        Serial.print(" Roll: ");
        Serial.print(CHR_rollClean);
        Serial.println();
      }

       else
      {
      j++;
      }
    }
  }
 
  j=0;
 
 
 delay(350);
}
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 239
Posts: 24371
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
while(Serial.available())
    {      
      CHRbuffer[j] = Serial.read();
      if (CHRbuffer[j] == 's')
      {
        char n = Serial.read();
You've been told only that there is at least one character available, but you read two or more.

Have you read what I've been saying about not doing this?
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

0
Offline Offline
Newbie
*
Karma: 0
Posts: 35
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I put in

Code:
while(Serial.available() > 0)
    {     

and removed the

Code:
if (Serial.available())
  {

I dont think the first if statement is really needed if I have the while right after. So the data doesnt freeze like it used to but I still get erroneous data every 4th reading which almost too reliablely unreliable LOL
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 239
Posts: 24371
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Think about what happens.
At 115200 baud, a character takes 86 us to transmit.

Serial.available returns 1 when the serial receiver has buffered a character.

So, you go and read out that character and compare it to the letter 's'.
Maybe 5 microseconds (being generous here) have passed - not long enough to have received another character.
The character was an 's' (let us say), so you go ahead and read another character.
But no character has arrived yet.

« Last Edit: March 04, 2011, 07:56:00 am by AWOL » Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Pages: 1 [2] 3   Go Up
Jump to: