Hi,

I'm trying to adapt my project from mavlink v0.9 to v1.0 but i have a problem computing the CRC, does anyone know how to do, because my crc calculation of the v0.9 doesn't work on the v1.0...

i heard of an extra caracter to add to the crc calculation?

thanks

Views: 1400

Reply to This

Replies to This Discussion

I have a problem too for compute the CRC... have you found somthing?

 

Best

 

Guillaume

Hi!

what you have to do to get the good CRC:

-init the CRCaccum with the funciton

-accumulate all the bytes of the message (excluding the startsign)

-accumulate one "magic byte" as they call

-get the result in the variable crcAccum

to accumulate you have to call for each byte b the function crc_accumulate(unsigned char b)

the magic byte (different for each message...) you can found it in the header of each message. I give you the magic bytes of some messages ;) (you can found it in the .h of the mavlink sources!!

mavlink_msg_attitude.h : line 78 : value 39

mavlink_msg_heartbeat.h: line 72: value 50

mavlink_msg_raw_imu.h: line 93: value 144

hope i helped you!

unsigned int crcAccum; //global variable

/**
* @brief Initiliaze the buffer for the X.25 CRC
*
* @param crcAccum the 16 bit X.25 CRC
*/
void crc_init()
{
crcAccum = 0xFFFF;
return;
}

/**
* @brief Accumulate the X.25 CRC by adding one char at a time.
*
* The checksum function adds the hash of one char at a time to the
* 16 bit checksum (uint16_t).
*
* @param data new char to hash
* @param crcAccum the already accumulated checksum
**/

unsigned int crc_accumulate(unsigned char b)
{
/*Accumulate one byte of data into the CRC*/
unsigned char tmp;

tmp = b ^ ((unsigned char)crcAccum & 0xff);
tmp = (tmp ^ (tmp4)) & 0xFF;
crcAccum = (crcAccum>>8) ^ (tmp8) ^ (tmp3) ^ (tmp>>4);
crcAccum = crcAccum & 0xFFFF;
return crcAccum;
}

Thanks for your response!

In fact my problem was with the extra crc (your magic number)...

I am coding a Java version of MAVLink generator so i can't  use C header files... :-)

In python generator i have found the method to compute it for each message :

def message_checksum(msg):
    '''calculate a 8-bit checksum of the key fields of a message, so we
       can detect incompatible XML changes'''
    crc = mavutil.x25crc(msg.name + ' ')
    for f in msg.ordered_fields:
        crc.accumulate(f.type + ' ')
        crc.accumulate(f.name + ' ')
        if f.array_length:
            crc.accumulate(chr(f.array_length))
    return (crc.crc&0xFF) ^ (crc.crc>>8)

 

So now i can build the crcs array use to accumulate the extra crc...

 

Best regards

 

Guillaume

Thanks for clearing this for me. Here is my implementation in Java:

/**
* X.25 CRC calculation for MAVlink messages.
* The checksum must be initialized,
* updated with witch field of the message,
* and then finished with the message id.
*
*/
private static class CRC {

private static final int[] MAVLINK_MESSAGE_CRCS = {50, 124, 137, 0, 237, 217, 104, 119, 0, 0, 0, 89, 0, 0, 0, 0, 0, 0, 0, 0, 214, 159, 220, 168, 24, 23, 170, 144, 67, 115, 39, 246, 185, 104, 237, 244, 222, 212, 9, 254, 230, 28, 28, 132, 221, 232, 11, 153, 41, 39, 214, 223, 141, 33, 15, 3, 100, 24, 239, 238, 0, 0, 183, 0, 130, 0, 148, 21, 0, 52, 124, 0, 0, 0, 20, 0, 152, 143, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 183, 63, 54, 0, 0, 0, 0, 0, 0, 0, 19, 102, 158, 208, 56, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 134, 219, 208, 188, 84, 22, 19, 21, 134, 0, 78, 68, 189, 127, 42, 21, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 204, 49, 170, 44, 83, 46, 247};
private static final int CRC_INIT_VALUE = 0xffff;
private static int CRCvalue;

/**
* Accumulate the X.25 CRC by adding one char at a time.
*
* The checksum function adds the hash of one char at a time to the 16
* bit checksum (uint16_t).
*
* @param data
* new char to hash
* @param crcAccum
* the already accumulated checksum
**/
private static void mavlink_update_checksum(int data) {
int tmp;
tmp = data ^ (CRCvalue & 0xff);
tmp ^= (tmp 4) & 0xff;
CRCvalue = ((CRCvalue >> 8) & 0xff) ^ (tmp 8) ^ (tmp 3) ^ ((tmp >> 4)&0xf);
}

/**
* Finish the CRC calculation of a message, by running the CRC with the Magic Byte.
* This Magic byte has been defined in MAVlink v1.0.
* @param msgid The message id number
*/
public static void mavlink_finish_checksum(int msgid) {
mavlink_update_checksum(MAVLINK_MESSAGE_CRCS[msgid]);
}

/**
* Initialize the buffer for the X.25 CRC
*
*/
private static void mavlink_start_checksum() {
CRCvalue = CRC_INIT_VALUE;
}

public static int getMSB() {
return ((CRCvalue >> 8) & 0xff);
}

public static int getLSB() {
return (CRCvalue & 0xff);
}

}

Reply to Discussion

RSS

Groups

Season Two of the Trust Time Trial (T3) Contest 
A list of all T3 contests is here. The current round, the Vertical Horizontal one, is here

© 2018   Created by Chris Anderson.   Powered by

Badges  |  Report an Issue  |  Terms of Service