Arduino UNO and chr-6d

Hello,

for my application I use Arduino UNO

and CHR-6d IMU (datasheet : pololu.com/file/0J342/chr6d_datasheet.pdf)

I have problems with communication between board and sensor. I have almost no experience in programming so I would need some help how to send proper packets to sensor and see response.

I think I wired it correctly. 3.3V to 3.3V, GND to GND, Rx to Tx, and Tx to Rx.

Below is my code, which is not working.

#include <SoftwareSerial.h>
#define rxPin 0
#define txPin 1

SoftwareSerial mySerial = SoftwareSerial(rxPin, txPin);

uint8_t SET_SILENT_MODE[] = {
  0x73, 0x6E, 0x70, 0x83,0x00, 0x01, 0x2B};
uint8_t GET_DATA[] = {
  0x73, 0x6E, 0x70, 0x01, 0x00, 0x01, 0x52};
uint8_t SET_BROADCAST_MODE[] = {
  0x73,0x6E,0x70,0x84,0x01,0x05,0x01,0x24};
void setup() {                
  mySerial.begin(115200);
  Serial.begin(115200);
  pinMode(rxPin, INPUT);
  pinMode(txPin, OUTPUT);
  delay(500);
  mySerial.write(SET_SILENT_MODE,sizeof(SET_SILENT_MODE));
}
void loop() {
  if (Serial.available()>0){
  char c = Serial.read();
  Serial.print(c, HEX);
 }
}

Problem is i don’t get any response in Serial Monitor. Can you help me with the code?

Thanks in advance,

best regards,

Hello.

It looks like you implement SoftwareSerial on pins 0 and 1. Those pins have built-in hardware for serial communication that is used to communicate with the computer, and implementing software serial on those pins might be causing problems due to the two interfering with each other. I suggest you try implementing software serial on different pins.

- Jeremy

Hello, thanks for reply. I changed my code and now problem is I get to few data. In one minute I only get like 30 pakets but I should get them 1200(transmit frequency 20Hz).

Probaby problem is with synchronisation. Can anyone help me with that?

Best regards,

my code :

#include <SD.h>
int chipSelect = 4;
File dataFile;
byte buffer[24];
uint8_t SET_BROADCAST_MODE[] = {
  's','n','p',0x84,0x01,0x01,0x01,0xD7};
uint8_t SET_ACTIVE_CHANNELS[] = {
  's','n','p',0x82,0x01,0x07,0x01,0xDB};
void setup() {
  Serial.begin(115200);
  pinMode(10, OUTPUT);
  SD.begin(chipSelect);
  delay(100);
  if(SD.exists("meritve.txt"))
  {
    SD.remove("meritve.txt");
  }
  dataFile = SD.open("meritve.txt", FILE_WRITE);
  Serial.write(SET_BROADCAST_MODE,sizeof(SET_BROADCAST_MODE));
}
void loop() {
  if(Serial.available()>0){
startofRead:
    int i=0;
    buffer[i]=Serial.read();
    if(buffer[i] == 0x73){
      i++;
      buffer[i]=Serial.read();
    }
    else {
      goto startofRead;
    }
    if(buffer[i] == 0x6E){
      i++;
      buffer[i]=Serial.read();
    }
    else {
      goto startofRead;
    }
    if(buffer[i] == 0x70){
      i++;
      buffer[i]=Serial.read();
    }
    else {
      goto startofRead;
    }
    if(buffer[i] ==0xB7){
      i++;
      buffer[i]=Serial.read();
      i++;
      buffer[i]=Serial.read();
      i++;
      buffer[i]=Serial.read();
      i++;
      buffer[i]=Serial.read();
      i++;
      buffer[i]=Serial.read();
      i++;
      buffer[i]=Serial.read();
      i++;
      buffer[i]=Serial.read();
      i++;
      buffer[i]=Serial.read();
      i++;
      buffer[i]=Serial.read();
      i++;
      buffer[i]=Serial.read();
      i++;
      buffer[i]=Serial.read();
      i++;
      buffer[i]=Serial.read();
      i++;
      buffer[i]=Serial.read();
      i++;
      buffer[i]=Serial.read();
      i++;
      buffer[i]=Serial.read();
      i++;
      buffer[i]=Serial.read();
      i++;
      buffer[i]=Serial.read();
      i++;
      buffer[i]=Serial.read();
      i++;
      buffer[i]=Serial.read();
      i++;
      buffer[i]=Serial.read();
    }
      for(int k=0; k<24;k++)
  {
    dataFile.print(buffer[k], HEX);
    dataFile.print(" "); 
  }
  dataFile.println();
  dataFile.flush();
  Serial.flush();
  }
  delay(15);
}

Hello, nejcmdev.

I took a careful look at your code and noticed you’re only enabling 3 channels (0x07 in SET_ACTIVE_CHANNELS to enable all 3 accelerometer channels), but you’re reading a number of bytes that corresponds with all 8 channels being enabled. According to section 7.2.9 in the datasheet, the number of data bytes for the SENSOR_DATA packet is N = 1 + 2*(# of active channels), which would be 7 data bytes with 3 channels active (14 bytes total) or 17 data bytes with 8 channels active (24 bytes total).

To make the number of active channels consistent with the number of bytes you read, you should either get rid of 10 calls to Serial.read() and make your buffer 10 bytes shorter, or you should enable all 8 channels with 0xFF in SET_ACTIVE_CHANNELS (don’t forget to update the checksum if you do this).

Please let us know if this helps.

- Kevin

Hello,

SET_ACTIVE_CHANNELS is only written in code but never sent through Serial.write()

Or just having it in code might be a problem?

Best regards,

Sorry, you’re right; I missed that you never actually send the SET_ACTIVE_CHANNELS command. However, I checked the datasheet again to see which channels are active by default. Section 6 says:

So it looks like you still have a similar problem: you are expecting data from 8 channels when you are only actually getting data from 6 (packet length = 20). My suggestion should be changed to: either get rid of 4 read() calls and make your buffer 4 bytes shorter, or enable all 8 channels. Does that help?

- Kevin

hi, I corrected the code. You can see i added SoftwareSerial so i can monitor data in Serial monitor, no SD needed until i fix mi problems with packets.

my code :

#include <SoftwareSerial.h>
SoftwareSerial CHR(6, 7); // RX, TX
uint8_t SET_BROADCAST_MODE[] = {'s','n','p',0x84,0x01,0x00,0x01,0xD6};
uint8_t SET_ACTIVE_CHANNELS[] = {'s','n','p',0x82,0x01,0x3F,0x02,0x13};
void setup() {
  Serial.begin(115200);
  CHR.begin(115200);
  CHR.write(SET_BROADCAST_MODE,sizeof(SET_BROADCAST_MODE));
  CHR.write(SET_ACTIVE_CHANNELS,sizeof(SET_ACTIVE_CHANNELS));
}
void loop() {
  byte buffer[20];
  if(CHR.available()>0){
startofRead:
    int i=0;
    buffer[i]=CHR.read(); //1
    if(buffer[i] == 0x73){
      i++;
      buffer[i]=CHR.read(); //2
    }
    else {
      goto startofRead;
    }
    if(buffer[i] == 0x6E){
      i++;
      buffer[i]=CHR.read(); //3
    }
    else {
      goto startofRead;
    }
    if(buffer[i] == 0x70){
      i++;
      buffer[i]=CHR.read(); //4
    }
    else {
      goto startofRead;
    }
    if(buffer[i] ==0xB7){
      i++;
      buffer[i]=CHR.read();
      i++;
      buffer[i]=CHR.read();
      i++;
      buffer[i]=CHR.read();
      i++;
      buffer[i]=CHR.read();
      i++;
      buffer[i]=CHR.read();
      i++;
      buffer[i]=CHR.read();
      i++;
      buffer[i]=CHR.read();
      i++;
      buffer[i]=CHR.read();
      i++;
      buffer[i]=CHR.read();
      i++;
      buffer[i]=CHR.read();
      i++;
      buffer[i]=CHR.read();
      i++;
      buffer[i]=CHR.read();
      i++;
      buffer[i]=CHR.read();
      i++;
      buffer[i]=CHR.read();
      i++;
      buffer[i]=CHR.read();
      i++;
      buffer[i]=CHR.read();
    }
      for(int k=0; k<20;k++)
  {
    Serial.print(buffer[k], HEX);
    Serial.print(" "); 
  }
  Serial.println();
  delay(50);
  }
}

Result:

73 6E 70 B7 D 3F E2 51 E2 F2 7C 60 FE E1 C0 D6 31 D D8 BC 
73 6E 70 B0 D 3F E2 51 E2 F2 7C 60 FE E1 C0 D6 31 D D8 BC 
73 6E 70 B7 D 3F E2 2D E2 9B F1 F4 F8 8B 1 57 17 6C 9 8A 
73 6E 70 B7 D 3F E2 52 7C 60 7C 18 81 1 57 17 68 7 D2 73 
73 6E 70 B7 D 3F F1 4E E2 E8 E3 4 F8 6E 1 5C 17 DB 84 BE 
73 6E 70 B7 D 3F F1 42 E2 C9 E2 F9 F8 7D 1 59 17 B0 89 44 
73 6E 70 B7 D 3F F1 3C E2 CF E3 4 F8 83 1 B5 B1 6D 61 D7 
73 6E 70 B7 C3 26 43 E2 B7 E3 2 F8 71 1 5B 17 B5 88 3D FF 
73 6E 70 B7 C3 26 4E E2 F4 E3 8 F8 84 1 AF 8F 69 88 A0 FF 
73 6E 70 B7 D 3F E2 3D E2 C9 E3 1 FC 7F 1 5A 17 68 8 53 
73 6E 70 B7 D 3F E2 47 E2 DD 7C 18 82 1 5B 17 6D 8 7D FF 
73 6E 70 B7 D 3F E2 3A E2 BD E2 FA FC 88 1 55 17 69 9 41 
73 6E 70 B7 D 3F E2 48 F1 E8 E2 FA F8 86 1 58 17 64 9 AB 
73 6E 70 B7 D 3F E2 3B F1 CE E2 F7 F8 66 1 63 17 6D 9 40 
73 6E 70 B7 D 3F E2 3A F1 BE E2 FF F8 79 1 4C 17 63 9 CA 
73 6E 70 B7 D 3F F1 48 E2 E7 E3 4 F8 91 1 56 17 6D 61 E4 
73 6E 70 B7 D 3F F1 3B E2 B6 E2 F2 F8 7E 1 5D 17 5B D8 F3 
73 6E 70 B7 D 3F F1 46 E2 D0 E2 FE F8 95 1 AA 17 69 9 70 
73 6E 70 B7 C3 26 46 E2 D9 E2 FC F8 85 1 B2 97 68 9 76 FF 
73 6E 70 B7 C3 26 3A E2 B8 E3 6 F8 7E 1 D6 31 36 61 E8 FF

I get instead of regular packet some messy packets. I don’t know why. Changing delay doesn’t help.
Should I add some more if’s until I reach data packets(if for 0x0D and 0x3F) so we’ll be reading packets only if we get first 6 bytes OK?

I would be grateful again for any help,

Best regards

I’m not sure why you’re getting these “messy” packets, as they don’t seem to contain entirely valid data. Are you at least getting packets at a rate closer to what you expect? You could try checking the 0x0D and 0x3F packets like you said to see if it helps. Other ideas that might work are to try verifying the checksums or lowering the baud rate.

- Kevin