Compass information with arduino UART

Frequent questions asked online, offline, in forums are answered here
Post Reply
hunteril
Posts: 5
Joined: Thu Jun 25, 2020 9:11 pm

Compass information with arduino UART

Post by hunteril »

Hello

I am using arduino UART connection with marvelmind modem. I can get the x,y,z coordinates on the serial port. However, I need the compass raw data. is there any way to get them on arduino too?

PS: I am using the following attached code:

int hedgehog_x, hedgehog_y;// coordinates of hedgehog (X,Y), cm
int hedgehog_h;// height of hedgehog, cm
int hedgehog_pos_updated;// flag of new data from hedgehog received

///

#define HEDGEHOG_BUF_SIZE 30
#define HEDGEHOG_PACKET_SIZE 23
byte hedgehog_serial_buf[HEDGEHOG_BUF_SIZE];
byte hedgehog_serial_buf_ofs;

const int hedgehog_packet_header[5]= {0xff, 0x47, 0x01, 0x00, 0x10};

typedef union {byte b[2]; unsigned int w;} uni_8x2_16;

// Marvelmind hedgehog support initialize
void setup_hedgehog()
{
Serial.begin(9600); // hedgehog transmits data on 500 kbps

hedgehog_serial_buf_ofs= 0;
hedgehog_pos_updated= 0;
}

// Marvelmind hedgehog service loop
void loop_hedgehog()
{int incoming_byte;
int total_received_in_loop;
int packet_received;

total_received_in_loop= 0;
packet_received= 0;
while(Serial.available() > 0)
{
if (hedgehog_serial_buf_ofs>=HEDGEHOG_BUF_SIZE) break;// buffer overflow
total_received_in_loop++;
if (total_received_in_loop>100) break;// too much data without required header

incoming_byte= Serial.read();
// check first 5 bytes for constant value
if (hedgehog_serial_buf_ofs<5)
{
if (incoming_byte != hedgehog_packet_header[hedgehog_serial_buf_ofs])
{
hedgehog_serial_buf_ofs= 0;// restart bufer fill
continue;
}
}
hedgehog_serial_buf[hedgehog_serial_buf_ofs++]= incoming_byte;
if (hedgehog_serial_buf_ofs == HEDGEHOG_PACKET_SIZE)
{// received packet with required header
packet_received= 1;
hedgehog_serial_buf_ofs= 0;// restart bufer fill
break;
}
}

if (packet_received)
{
hedgehog_set_crc16(&hedgehog_serial_buf[0], HEDGEHOG_PACKET_SIZE);// calculate CRC checksum of packet
if ((hedgehog_serial_buf[HEDGEHOG_PACKET_SIZE] == 0)&&(hedgehog_serial_buf[HEDGEHOG_PACKET_SIZE+1] == 0))
{// checksum success
hedgehog_x= int(hedgehog_serial_buf[9]) + (int(hedgehog_serial_buf[10])<<8);
hedgehog_y= int(hedgehog_serial_buf[11]) + (int(hedgehog_serial_buf[12])<<8);// coordinates of hedgehog (X,Y), cm
hedgehog_h= int(hedgehog_serial_buf[13]) + (int(hedgehog_serial_buf[14])<<8);// height of hedgehog, cm (FW V3.97+)
hedgehog_pos_updated= 1;// flag of new data from hedgehog received
}
}
}

// Calculate CRC-16 of hedgehog packet
void hedgehog_set_crc16(byte *buf, byte size)
{uni_8x2_16 sum;
byte shift_cnt;
byte byte_cnt;

sum.w=0xffffU;

for(byte_cnt=size; byte_cnt>0; byte_cnt--)
{
sum.w=(unsigned int) ((sum.w/256U)*256U + ((sum.w%256U)^(buf[size-byte_cnt])));

for(shift_cnt=0; shift_cnt<8; shift_cnt++)
{
if((sum.w&0x1)==1) sum.w=(unsigned int)((sum.w>>1)^0xa001U);
else sum.w>>=1;
}
}

buf[size]=sum.b[0];
buf[size+1]=sum.b[1];// little endian
}// hedgehog_set_crc16

// END OF MARVELMIND HEDGEHOG RELATED PART
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
//Echo_pin


void setup()
{
Serial.begin(9600);
setup_hedgehog();// Marvelmind hedgehog support initialize
}

void loop()
{
loop_hedgehog();// Marvelmind hedgehog service loop

if (hedgehog_pos_updated)
{// new data from hedgehog available
hedgehog_pos_updated= 0;// clear new data flag
// output hedgehog position to LCD
Serial.print("X=");
Serial.print(hedgehog_x);
Serial.print(" ");

Serial.print("Y=");
Serial.print(hedgehog_y);
Serial.print(" ");

Serial.print("H=");
Serial.print(hedgehog_h);
Serial.print(" ");
}
}

smoker77
Posts: 350
Joined: Sat Feb 06, 2016 4:03 pm

Re: Compass information with arduino UART

Post by smoker77 »

Hello,

Yes, you can get raw IMU data from any mobile beacon via modem.
You should enable 'IMU via modem' option on the mobile beacon (in 'Interfaces' section of dashboard settings).
Note that only HW v4.9 beacons with IMU have compass onboard, other types of beacons have only accelerometer and gyro.

Arduino code for reading raw IMU data is in attachment.
Attachments
hedgehog_sample_uart_imu_raw_v1.zip
(2.59 KiB) Downloaded 293 times

hunteril
Posts: 5
Joined: Thu Jun 25, 2020 9:11 pm

Re: Compass information with arduino UART

Post by hunteril »

Hello smoker77

many thanks for reply. I tried the exact code you sent and did the same configuration on the dashboard, but nothing appears on serial output (I made sure the serial is running by typing "Hello world" then typing :
Serial.print(imu_compass_x);
Serial.print('\n');

Serial.print(imu_compass_y);
Serial.print('\n');

Serial.print(imu_compass_z);
Serial.print('\n');

but I only got:
Hello world!


please note that I am using HW 4.9 beacons with the following configuration to arduino (as in the photo attached)
Note also I am able to retrieve the x,y,z coordinated on the same configuration.

thanks in advance
Attachments
1.png

smoker77
Posts: 350
Joined: Sat Feb 06, 2016 4:03 pm

Re: Compass information with arduino UART

Post by smoker77 »

Hello,

Please ensure your beacon is with IMU. HW v4.9 beacon can be with or without IMU onboard.
Word 'IMU' should present on the sticker on the bottom of the beacon if your beacon is with IMU.
Also try to connect the beacon to PC via USB and check following:
- 'IMU mode' setting in 'IMU' settings section in dashboard should be in state 'Normal' or 'High performance'
- select menus 'View/Accelerometer data', 'View/Gyroscope data', 'View/Compass data' and check graphs of data from these sensors

sherpaman
Posts: 2
Joined: Wed Dec 16, 2020 7:05 pm

Re: Compass information with arduino UART

Post by sherpaman »

Hello,

I am also working on the hedgehog to Arduino connection. I had been using the UART code provided by marvelmind before and there were no x,y,z coordinates updates. Then I used your code (hunteril) and had the same result.

I am using Dashboard V6.240, HW v4.9, an Arduino MEGA 2560. I have connected the RX, TX, and GND of the hedgehog to ports 18TX1, 19RX1, and GND of the Arduino MEGA, respectively. I've attached the Arduino output below. The values showing up for the coordinates are not correct. On the interfaces section of the dashboard, I have it set the hedgehog to 9600 bps, USB+UART, Marvelmind, Worker ( eventually will be a 'General Robot Control), USART RX, disabled, disabled, (raw distance data) enabled, (quality data stream) enabled, (location of other hedgehogs) enabled. For the modem, I have the same settings, with the (high resolution mode) disabled.

I've tried many iterations of the code with zero success. I'd appreciate any help. Please let me know if there is any other information you'd like to know.

Thanks,
Attachments
Hedgehog to Arduino MEGA output
Hedgehog to Arduino MEGA output

sherpaman
Posts: 2
Joined: Wed Dec 16, 2020 7:05 pm

Re: Compass information with arduino UART

Post by sherpaman »

Hello,

I figured out the solution to my issue.

Thanks,

yeni riska
Posts: 3
Joined: Thu Feb 18, 2021 4:29 pm

Re: Compass information with arduino UART

Post by yeni riska »

hello, how to get output from arduino like that ? i have been try using beacon mini rx as a hedgehog and didn't show correct coordinate. then i have been try using modem hw 4.9 and didn't show correct coordinate too. can you show me how dashboard setting interface and arduino code program ? thank you

Post Reply