Arduino UAV Serial Comms

Hey all,

I have a piece of code here to read in data from an AHRS (sparkfun razor).

char dataset[27]="";
char incoming=-1;
int counta=0;
int count=0;
String StringData="";
int comma1;
int comma2;
int datastart;
int dataend;
String rollstring;
String pitchstring;
String yawstring;
float rollval;
float pitchval;
float yawval;
char dan1='D';
char checker=-1;
int searchcount=0;
float rollval2;
int memavail=0;


void setup()

{
  Serial.begin(57600);
  Serial1.begin(57600);
}



void loop()
{
 // memavail=0;
  for (int mastercounter=0;mastercounter<=15;mastercounter++)
  {
   for(int i=0;i<=30;i++)
   {
     dataset[i]=0;
   }
   count=0;
   
 char rollstringchar[8]="";
char pitchstringchar[8]="";
char yawstringchar[8]="";
 
  count=0;
 
     StringData="";
    while (incoming!='X')
    {
      incoming=-1;
      if (Serial1.available())
      {
        incoming=Serial1.read();     // Read a byte of the serial port
        dataset[count]=incoming;
        count++;
      }
      //delay(100);
    }
    StringData=dataset;
   
    //  Serial.println(StringData);
 
  
   
 
datastart=StringData.indexOf(':');
dataend=StringData.indexOf('X');
comma1=StringData.indexOf(',',datastart+1);
comma2=StringData.indexOf(',',comma1+1);
rollstring=StringData.substring(datastart+1,comma1);
pitchstring=StringData.substring(comma1+1,comma2);
yawstring=StringData.substring(comma2+1,dataend);


rollstring.toCharArray(rollstringchar,7);
rollval=atof(rollstringchar);
//rollval2=atof(rollstringchar);

yawstring.toCharArray(yawstringchar,7);
yawval=atof(yawstringchar);

pitchstring.toCharArray(pitchstringchar,7);
pitchval=atof(pitchstringchar);

//memavail=availableMemory();

//Serial.print("Roll Check: ");
//Serial.println(rollstringchar);
Serial.print("Roll: ");
Serial.println(rollval);
Serial.print("Yaw: ");
Serial.println(yawval);
Serial.print("Pitch: ");
Serial.println(pitchval);
Serial.println(mastercounter);
Serial.println(memavail);
Serial.println("---------------------------------");
  }

delay(100);
 /* for(int i=0;i<=7;i++)
   {
     rollstringchar[i]=0;
       yawstringchar[i]=0;
         pitchstringchar[i]=0;
   }
   count=0;
  */
}


int availableMemory() {
  int size = 8000; // Use 2048 with ATmega328
  byte *buf;

  while ((buf = (byte *) malloc(--size)) == NULL)
    ;

  free(buf);

  return size;
}

This reads in the data just fine (I have added some commented out code to try and find out how much ram this was using) the problem is that if it does anything else, such as even a delay(100); after the code has executed things start getting weird. What really happens is that the code executes with non uniform speed and instead of executing the loop 15 times as it is supposed to it will execute 3 or 4. I originally thought that it might be a memory issue, but the code works, just unexpectedly. I might be really off base here but I was beginning to think that once the code finishes and the arduino refreshes it loses synch and has to reestablish it... Is this at all accurate, this is really concerning me because I have to run another similar code to access data from a gps which means that while this isnt running the machine will have no guidance so I would like this time to be as short as possible. Any tips?

Thanks
Dan

Combatraffi:

char dataset[27]="";

for(int i=0;i<=30;i++)
  {
    dataset[i]=0;
  }

In this section you are writing past the end of dataset[].

    while (incoming!='X')

{
     incoming=-1;
     if (Serial1.available())
     {
       incoming=Serial1.read();     // Read a byte of the serial port
       dataset[count]=incoming;
       count++;
     }

Here, you should also have a test that count is not stepping off the end of dataset[]. Random line noise on your serial line would cause the "X" you are looking for to not appear for many more bytes that you think, butchering your memory, and this may be what you are seeing.

What is the syntax of the string you are trying to parse on the input? There is likely a much more robust and debuggable approach to parsing it.

that seems like a reasonable explanation the data being sent by the AHRS board is formatted as follows
!ANG:-000.00,-000.00,-000.00X

the reason the decryption is so convoluted is because if there is say a bank angle of 45 degrees the output will be

!ANG:45,-000.00,-000.00X
this made it more difficult and I used the String object to quickly search for the commas to find the data sep points.

Thanks for your help i would love to see anything you come up with that could help with this!

I used the String object to quickly search for the commas to find the data sep points.

How do you suppose the String class finds the commas? You could ditch the whole String class, use char arrays, and the strtok() function to do the parsing.

What reason do you have for having any delay() calls? Reading serial data as soon as it arrives seems important to me. If you spend all of your time waiting for serial data, that's fine. Spending it twiddling your thumbs in a delay() call is not.