Calculates checksums

Hello, what does you believe about the problem of checksum? For we see here that ck_a will be greater than 255 probably. it can't work, do I wrong?

 

      for (int i=0;i<ck+2;i++) Serial.print (IMU_buffer[i]); 
      for (int i=0;i<ck+2;i++) {
          IMU_ck_a+=IMU_buffer[i];  //Calculates checksums
          IMU_ck_b+=IMU_ck_a;      
      }
      Serial.print(IMU_ck_a);
      Serial.print(IMU_ck_b);

 

Look my solution with this little test code

 

int main(int argc, const char** argv) {

byte IMU_ck_a = 0;

byte IMU_ck_b = 0;

unsigned int Corrected_IMU_ck_a = 0;

unsigned int Corrected_IMU_ck_b = 0;

byte IMU_buffer[] = {

4, 5, 10, 56, 74, 43, 87, 100, 34, 68, 29

};

for (int i=0;i<10;i++) {

IMU_ck_a+=IMU_buffer[i]; //Calculates checksums

IMU_ck_b+=IMU_ck_a;

}

for (int i=0;i<10;i++) {

Corrected_IMU_ck_a+=IMU_buffer[i]; //Calculates checksums

Corrected_IMU_ck_b+=IMU_ck_a;

}

std::cout << (unsigned int)IMU_ck_a << std::endl;

std::cout << (unsigned int)IMU_ck_b << std::endl;

std::cout << Corrected_IMU_ck_a << std::endl;

std::cout << Corrected_IMU_ck_b << std::endl;

}

You need to be a member of diydrones to add comments!

Join diydrones

Email me when people reply –

Replies

  • Developer
    The checksum as originally implemented is a Fletcher Checksum. It is robust and efficient and will work better than what you propose. Google "Fletcher Checksum" for background.

    What we use is also the checksum used by u-blox. We chose this checksum format for consistency.
  • The byte type is 8 bits so it's maximum value is 255. If you overflow this, it will wrap around, essentially just discarding the higher bits of the result that are beyond what the data type can represent.

    (byte)255 + (byte)1 = (byte)0
  • Well, my solution for Arduimu.

    void printdata(void)
    {
    /*
    IMU Message format
    Byte(s) Value
    0-3 Header "DIYd"
    4 Payload length = 6
    5 Message ID = 2
    6,7 roll Integer (degrees*100)
    8,9 pitch Integer (degrees*100)
    10,11 yaw Integer (degrees*100)
    12,13 checksum
    */

    // This section outputs a binary data message
    // Conforms to new binary message standard (12/31/09)
    byte IMU_buffer[20];
    int tempint;
    int ck;
    uint8_t tempuint;
    long templong;
    unsigned int Corrected_IMU_ck_a = 0;
    unsigned int Corrected_IMU_ck_b = 0;
    Serial.print("DIYd"); // This is the message preamble
    IMU_buffer[0]=0x06;
    ck=6;
    IMU_buffer[1] = 0x02;

    tempint=ToDeg(roll)*100; //Roll (degrees) * 100 in 2 bytes
    IMU_buffer[2]=tempint&0xff;
    IMU_buffer[3]=(tempint>>8)&0xff;

    tempint=ToDeg(pitch)*100; //Pitch (degrees) * 100 in 2 bytes
    IMU_buffer[4]=tempint&0xff;
    IMU_buffer[5]=(tempint>>8)&0xff;

    tempint=ToDeg(yaw)*100; //Yaw (degrees) * 100 in 2 bytes
    IMU_buffer[6]=tempint&0xff;
    IMU_buffer[7]=(tempint>>8)&0xff;

    for (int i=0;i
    for (int i=0;i
    Corrected_IMU_ck_a+=IMU_buffer[i]; //Calculates checksums
    Corrected_IMU_ck_b+=Corrected_IMU_ck_a;
    }

    byte Sended_IMU_ck_a[4] = {
    ((Corrected_IMU_ck_a >> 32) & 0xFF),
    ((Corrected_IMU_ck_a >> 16) & 0xFF),
    ((Corrected_IMU_ck_a >> 8) & 0xFF),
    ((Corrected_IMU_ck_a >> 0) & 0xFF)
    };

    byte Sended_IMU_ck_b[4] = {
    ((Corrected_IMU_ck_b >> 32) & 0xFF),
    ((Corrected_IMU_ck_b >> 16) & 0xFF),
    ((Corrected_IMU_ck_b >> 8) & 0xFF),
    ((Corrected_IMU_ck_b >> 0) & 0xFF)
    };

    for (int i = 0; i < 4; i++) Serial.print(Sended_IMU_ck_a[i]);
    for (int i = 0; i < 4; i++) Serial.print(Sended_IMU_ck_b[i]);

    // This section outputs the gps binary message when new gps data is available

    /*GPS Message format
    Byte(s) Value
    0-3 Header "DIYd"
    4 Payload length = 18
    5 Message ID = 3
    6-9 longitude Integer (value*10**7)
    10-13 latitude Integer (value*10**7)
    14,15 altitude Integer (meters*10)
    16,17 gps speed Integer (M/S*100)
    18,19 gps course not used
    20-23 TOW GPS Millisecond Time of Week
    24,25 checksum
    */
    if(gpsFixnew==1)
    {
    gpsFixnew=0;
    Corrected_IMU_ck_a = 0;
    Corrected_IMU_ck_a = 0;
    Serial.print("DIYd"); // This is the message preamble
    IMU_buffer[0]=0x12;
    ck=18;
    IMU_buffer[1] = 0x03;


    templong=convert_to_dec(lon); //Longitude *10**7 in 4 bytes
    IMU_buffer[2]=templong&0xff;
    IMU_buffer[3]=(templong>>8)&0xff;
    IMU_buffer[4]=(templong>>16)&0xff;
    IMU_buffer[5]=(templong>>24)&0xff;

    templong=convert_to_dec(lat); //Latitude *10**7 in 4 bytes
    IMU_buffer[6]=templong&0xff;
    IMU_buffer[7]=(templong>>8)&0xff;
    IMU_buffer[8]=(templong>>16)&0xff;
    IMU_buffer[9]=(templong>>24)&0xff;

    tempint=alt_MSL*10; // Altitude MSL in meters * 10 in 2 bytes
    IMU_buffer[10]=tempint&0xff;
    IMU_buffer[11]=(tempint>>8)&0xff;

    tempint=speed_3d*100; // Speed in M/S * 100 in 2 bytes
    IMU_buffer[12]=tempint&0xff;
    IMU_buffer[13]=(tempint>>8)&0xff;

    tempint=ground_course*100; // course in degreees * 100 in 2 bytes
    IMU_buffer[14]=tempint&0xff;
    IMU_buffer[15]=(tempint>>8)&0xff;

    IMU_buffer[16]=iTOW&0xff; //TOW GPS Millisecond Time of Week
    IMU_buffer[17]=(iTOW>>8)&0xff;
    IMU_buffer[18]=(iTOW>>16)&0xff;
    IMU_buffer[19]=(iTOW>>24)&0xff;

    for (int i=0;i
    for (int i=0;i
    Corrected_IMU_ck_a+=IMU_buffer[i]; //Calculates checksums
    Corrected_IMU_ck_b+=Corrected_IMU_ck_a;
    }

    byte Sended_IMU_ck_a[4] = {
    ((Corrected_IMU_ck_a >> 24) & 0xFF),
    ((Corrected_IMU_ck_a >> 16) & 0xFF),
    ((Corrected_IMU_ck_a >> 8) & 0xFF),
    ((Corrected_IMU_ck_a >> 0) & 0xFF)
    };

    byte Sended_IMU_ck_b[4] = {
    ((Corrected_IMU_ck_b >> 24) & 0xFF),
    ((Corrected_IMU_ck_b >> 16) & 0xFF),
    ((Corrected_IMU_ck_b >> 8) & 0xFF),
    ((Corrected_IMU_ck_b >> 0) & 0xFF)
    };

    for (int i = 0; i < 4; i++) Serial.print(Sended_IMU_ck_a[i]);
    for (int i = 0; i < 4; i++) Serial.print(Sended_IMU_ck_b[i]);

    }
    }


    long convert_to_dec(float x)
    {
    return x*10000000;
    }
This reply was deleted.

Activity

Neville Rodrigues liked Neville Rodrigues's profile
Jun 30
Santiago Perez liked Santiago Perez's profile
Jun 21
More…