Tell me more ×
Electrical Engineering Stack Exchange is a question and answer site for electronics and electrical engineering professionals, students, and enthusiasts. It's 100% free, no registration required.

I have a ArduIMU+ V3 and it is running a configured version of the ArduIMU firmware. I get some valid data very rarely, but I mostly get binary messages that don't pass checksum or just complete garbage.

Here is the code I am using to parse the messages:

// pin for serial input in arduino
#define RX 0
// serial rate for arduIMU
#define IMU_SERIAL_RATE 38400
// message tag
#define IMU_TAG "DIYd"
// number of messages send
#define IMU_VALUES 3

// union structure for serial received binary data
typedef union{
    int16_t word;
    byte    valByte[2];
} IMUpacket;

// intialization of the structures for each passed value
IMUpacket serialPackets[IMU_VALUES];
// first tag in message
char imuTag[] = IMU_TAG;
// first tag in message buffer
char IMUtagBuffer[sizeof(imuTag)];
// check for new data
boolean newData = false;
// vars for checksum
byte msg_checksum_a;
byte msg_checksum_b;

// waits for n bytes to receive
void wait_for_bytes(byte number){
    while(Serial.available() < number);
}
// sums all the values and confront the result with the checksum passed
boolean checksum_is_true(){
    byte current_msg_checksum_a = 0;
    byte current_msg_checksum_b = 0;
    for(uint8_t i=0; i<IMU_VALUES; i++){
        current_msg_checksum_a += serialPackets[i].valByte[0];
        current_msg_checksum_b += current_msg_checksum_a;
                current_msg_checksum_a += serialPackets[i].valByte[1];
        current_msg_checksum_b += current_msg_checksum_a;
    }

    return (current_msg_checksum_a == msg_checksum_a && current_msg_checksum_b == msg_checksum_b);
}

void setup(){
    Serial.begin(IMU_SERIAL_RATE);
    pinMode(RX, INPUT);

        pinMode(13, OUTPUT);
}

void loop(){
    if(Serial.available()>0){
        // check if the byte is the start of new messange
        wait_for_bytes(1);
        IMUtagBuffer[0] = Serial.read(); //  D
        if(IMUtagBuffer[0]==imuTag[0]){


            wait_for_bytes(3);
            IMUtagBuffer[1] = Serial.read(); // I
            IMUtagBuffer[2] = Serial.read(); // Y
            IMUtagBuffer[3] = Serial.read(); // d

            if(IMUtagBuffer[1]==imuTag[1] && IMUtagBuffer[2]==imuTag[2] && IMUtagBuffer[3]==imuTag[3]){
                // new messange
                newData = true;

                                Serial.println("Message received.");

                // receive all values and put it in the packet structure
                wait_for_bytes((IMU_VALUES*2)+2);

                serialPackets[0].valByte[0] = Serial.read();
                serialPackets[0].valByte[1] = Serial.read(); // roll

                serialPackets[1].valByte[0] = Serial.read();
                serialPackets[1].valByte[1] = Serial.read(); // pitch

                serialPackets[2].valByte[0] = Serial.read();
                serialPackets[2].valByte[1] = Serial.read(); // yaw

                msg_checksum_a = Serial.read();
                msg_checksum_b = Serial.read(); // checksum
            } else newData = false;
        }

        // checksum for message
        if(newData && checksum_is_true()){
            // do stuff with received data values
                        Serial.print("Roll: ");
                        Serial.println(serialPackets[0].word/100);
                        Serial.print("Pitch: ");
            Serial.println(serialPackets[1].word/100);
                        Serial.print("Yaw: ");
            Serial.println(serialPackets[2].word/100);

                        digitalWrite(13, LOW);
        } else{
            msg_checksum_a = 0;
            msg_checksum_b = 0;

                        newData = false;

                        if(newData){
                          Serial.println("Checksum failed.");
                          digitalWrite(13, HIGH);
                        }


        }
    }
}

For some reason, I can't post the output on any websites I have tried.

It should be something like this: DIYd(6 bytes of data)(2 byre checksum)

share|improve this question
1  
Maybe take a screenshot of the output and post the screenshot instead? Windows: Alt+PrtSc key on keyboard, then paste into PaintBrush or similar; Linux/Ubuntu: PrtSc key then save from the window that pops up; Mac: Shift+Cmd+3 or Shift+Cmd+4, the screenshot will be saved to Desktop. – Erion Sep 10 '12 at 10:36

Know someone who can answer? Share a link to this question via email, Google+, Twitter, or Facebook.

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.