I set the unit to below 100Hz in broadcast mode to stream YAW PITCH ROLL and ACCEL_Z, and made this code for an arduino Mega. It sometimes in a specific interval skip over things and get really bad results... But most of the time I get good ones. I have not yet managed to check the checksum...
/*
Honk Super Test of CHR-6DM
It is set to stream YAW, PITCH, ROLL and ACCEL_Z in 100Hz (Broadcast setting 73)
*/
float CHR_yawClean;
float CHR_pitchClean;
float CHR_rollClean;
float CHR_accel_zClean;
#define CHR_MESSAGE_LENGTH 16 //in bytes
//byte 1 contains 's'
//byte 2 contains 'n'
//byte 3 contains 'p'
//byte 4 contains PT, packet type
//byte 5 contains N (the number of data bytes to expect)
//FIRST DATA BYTE IS BYTE NUMBER 6
//byte 6 and 7 contains info about active channels
//byte 8 and 9 contains YAW
//byte 10 and 11 contains PITCH
//byte 12 and 13 contains ROLL
//byte 14 and 15 contains ACCEL_Z
//byte 16 and 17 contains CHK_SUM (N+6/N+7)
#define BYTE1 0
#define BYTE2 1
#define BYTE3 2
#define PT_BYTE 3
#define N_BYTE 4
#define DATA_BYTE1 5
#define DATA_BYTE2 6
#define YAW_BYTE1 7
#define YAW_BYTE2 8
#define PITCH_BYTE1 9
#define PITCH_BYTE2 10
#define ROLL_BYTE1 11
#define ROLL_BYTE2 12
#define ACCEL_Z_BYTE1 13
#define ACCEL_Z_BYTE2 14
#define CHK_BYTE1 15
#define CHK_BYTE2 16
#define CHR_YAW_FACTOR 0.0109863F //from datasheet
#define CHR_PITCH_FACTOR 0.0109863F
#define CHR_ROLL_FACTOR 0.0109863F
#define CHR_ACCEL_Z_FACTOR 0.106812F
//transmission freq. from CHR6DM: 100Hz (73 in the Broadcast setting)
void setup() {
Serial.begin(57600);
Serial1.begin(115200);
}
void loop() {
readCHR6DM();
Serial.print("Yaw: ");
Serial.print(CHR_yawClean);
Serial.print(" Pitch: ");
Serial.print(CHR_pitchClean);
Serial.print(" Roll: ");
Serial.print(CHR_rollClean);
Serial.print(" Acc_Z: ");
Serial.println(CHR_accel_zClean);
delay(350); //unit set to 100Hz continous sending
}
void readCHR6DM() {
int CHR_yawWord, CHR_pitchWord, CHR_rollWord, CHR_accel_zWord;
unsigned int CHR_checksumWord, calculated_checksumWord;
int i, j;
unsigned char buffer[CHR_MESSAGE_LENGTH+2]; //17 bytes
unsigned long sumOfBytes;
//byte 1 contains 's'
//byte 2 contains 'n'
//byte 3 contains 'p'
//byte 4 contains PT, packet type
//byte 5 contains N (the number of data bytes to expect)
//FIRST DATA BYTE IS BYTE NUMBER 6
//byte 6 and 7 contains info about active channels
//byte 8 and 9 contains YAW
//byte 10 and 11 contains PITCH
//byte 12 and 13 contains ROLL
//byte 14 and 15 contains ACCEL_Z
//byte 16 and 17 contains CHK_SUM (N+6/N+7) //N is expected to be 10
if (Serial1.available() > 0) {
startOfRead:
i = 0;
buffer = Serial1.read();
_ if(buffer == 's') { //byte 1 element 0_
* i++;*
_ buffer = Serial1.read();
* //Serial.println(buffer[BYTE1]); //debug*
* }
else { goto startOfRead; }
if(buffer == 'n') { //byte 2 element 1
i++;
buffer = Serial1.read(); //byte 3 element 2
//Serial.println(buffer[BYTE2]); //debug*
* }
else { goto startOfRead; }
if(buffer == 'p') { //byte 3 element 2, begin getting the whole thing because 3 start bytes checked to be valid
i++;
buffer = Serial1.read(); // byte 4 element 3 (PT_BYTE)
//Serial.println(buffer[BYTE3]); //debug
}
else { goto startOfRead; }
if(buffer == 0xB7) { // byte 4 element 3 (PT_BYTE)
i++;_
buffer = Serial1.read(); // byte 5 element 4 (N_BYTE)
//Serial.println(buffer[PT_BYTE], HEX); //debug*
* //Serial.println(buffer[N_BYTE], DEC); //debug this is 10 dec!
_ }
else { goto startOfRead; }
if(buffer == 0x0A) { // byte 5 element 4 (N_BYTE) expect 10 dec checks out!
i++;_
buffer = Serial1.read(); // byte 6 element 5 (DATA_BYTE1)
//Serial.println(buffer[DATA_BYTE1], BIN); //debug this prints 1110000 checks out*
* }*
* else { goto startOfRead; }*
if(buffer == 0xE0) { // byte 6 element 5 (DATA_BYTE1) expect 70 hex this should check out
* i++;*
buffer = Serial1.read(); // byte 7 element 6 (DATA_BYTE2)
* //Serial.println(buffer[DATA_BYTE2], BIN); //debug*
* }*
* else { goto startOfRead; }*
if(buffer == 0x02) { // byte 7 element 6 (DATA_BYTE2) expect 2 hex
* i++;*
buffer = Serial1.read(); //byte 8 element 7 (YAW_BYTE1)
* i++;*
buffer = Serial1.read(); //byte 9 element 8 (YAW_BYTE2)
* i++;*
buffer = Serial1.read(); //byte 10 element 9 (PITCH_BYTE1)
* i++;*
buffer = Serial1.read(); //byte 11 element 10 (PITCH_BYTE2)
* i++;*
buffer = Serial1.read(); //byte 12 element 11 (ROLL_BYTE1)
* i++;*
buffer = Serial1.read(); //byte 13 element 12 (ROLL_BYTE2)
* i++;*
buffer = Serial1.read(); //byte 14 element 13 (ACCEL_Z_BYTE1)
* i++;*
buffer = Serial1.read(); //byte 15 element 14 (ACCEL_Z_BYTE2)
* i++;*
buffer = Serial1.read(); //byte 16 element 15 (CHK_BYTE1)
_ //Serial.println(buffer*, DEC);
i++;
buffer = Serial1.read(); //byte 17 element 16 (CHK_BYTE2)
//Serial.println(buffer, DEC);
}
//else { goto startOfRead; }_
//for(i = 0; i < CHR_MESSAGE_LENGTH; i++){ //we're still at byte 3 element 2, execution gets us to byte 4 element 3*
_ //buffer = Serial1.read(); //first time here i=3 (byte 4), i=4 (byte 5), i=5 (byte 6), i=6, i=7, i=8, i=9, i=10, i=11, i=12, i=13, i=14, i=15, i=16(byte 17), i=17(byte18 nonexistent)
* //}
}
else{ Serial.println("Sensor not connected"); }*_
// sumOfBytes = 0;
// for(i=-1;i<13;i++) { //ska gå element 0 till 14
// sumOfBytes += buffer*;*
// }
*// *
// Serial.print("sumOfBytes: ");
// Serial.print(sumOfBytes, DEC);
*// *
// CHR_checksumWord = (unsigned)buffer[CHK_BYTE1]<<8;
// CHR_checksumWord |= buffer[CHK_BYTE2];
// //CHR_checksumWord = (unsigned long)(buffer[CHK_BYTE1] + buffer[CHK_BYTE2]);
// Serial.print(" CHR_checksumWord: ");
// Serial.println(CHR_checksumWord, DEC);
*// *
* //if( ) {*
* //plMSB = Serial1.read();*
* //plLSB = Serial1.read();*
* //myInt = (int)plMSB<<8; YÄÄÄÄH!*
* //myInt |= plLSB;*
* CHR_yawWord = (int)buffer[YAW_BYTE1]<<8;
CHR_yawWord |= buffer[YAW_BYTE2];*
* CHR_pitchWord = (int)buffer[PITCH_BYTE1]<<8;
CHR_pitchWord |= buffer[PITCH_BYTE2];*
* CHR_rollWord = (int)buffer[ROLL_BYTE1]<<8;
CHR_rollWord |= buffer[ROLL_BYTE2];*
* CHR_accel_zWord = (int)buffer[ACCEL_Z_BYTE1]<<8;
CHR_accel_zWord |= buffer[ACCEL_Z_BYTE2];*
CHR_yawClean = (CHR_yawWord * CHR_YAW_FACTOR);
CHR_pitchClean = (CHR_pitchWord * CHR_PITCH_FACTOR);
CHR_rollClean = (CHR_rollWord * CHR_ROLL_FACTOR);
CHR_accel_zClean = (CHR_accel_zWord * CHR_ACCEL_Z_FACTOR);
* //}*
}