Hedgehog to Arduino communication {SOLVED}

Hello,

I am working on the hedgehog to Arduino connection. I had been using the UART code provided by marvelmind before but haven't' had successful x,y,z coordinate updates.

I am using Dashboard - V6.240, Beacon - HW v4.9, and an Arduino MEGA 2560. I have connected the RX, TX, and GND of the hedgehog to ports TX0, RX0, and GND of the Arduino MEGA, respectively. I've attached the connection schematic, Arduino code, and 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,

hedgehog_uart_v3.ino (4.38 KB)

How about editing your post and adding code tags around the posted code, please?

Perhaps a thing do about why and how you are hooking up a Mega to one of these and how it's able to send serial data.

?

Hello Idahowalker,

I have updated the post.

Thanks,

If you could post your code that would be grand, in code tags.

BTW, notice how your using pins 0 and 1 for TX/RX? Those pins are the same as with using an Uno and its serial ports Pin0 and Pin1. And come with the same issues. May I suggest that you initialize Serial1, TX1/RX1, 18/19 as your serial ports of choice for inter device communications. By using Serial1 to do the hedgehog thing you'll avoid the issues that come with using Pins 0 and 1 for serial coms.

Oh, post your code after making the serial changes, so we get the latest.

Some serial port info link to thingy How to Use Arduino Serial Ports | Starting Electronics Blog

Here is the most recent code, connections (Arduino Uno), and output.

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
//#include <SoftwareSerial.h>
#include <AltSoftSerial.h>
AltSoftSerial altSerial(9, 8, 1);//rx tx
#define HEDGEHOG_BUF_SIZE 40 
#define HEDGEHOG_PACKET_SIZE 20
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(115200); // hedgehog transmits data on 500 kbps
    altSerial.begin(115200);
    //Serial1.begin(9600);
    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; 
        }
    }//end while

  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==true;// flag of new data from hedgehog received
        }
    }
}//void loop_hedgehog

// 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
//SoftwareSerial mySerial(10, 11); //RX and TX
void setup()
{
  //pinMode(10, INPUT);
  //#define SP2_RX 10
  //#define SP2_TX 9
  Serial.begin(115200);
  altSerial.begin(115200);
  //altSerial(10, 9);
  //Serial1.begin(9600);
  //Serial1(19, 18, 1);//RX and TX and inverting logic
  //Serial2.begin(9600);
  setup_hedgehog();//Marvelmind hedgehog support initialize
}
void loop()
{ 
   loop_hedgehog();// Marvelmind hedgehog service loop

   if (altSerial.available()> 0)//hedgehog_pos_updated=true)//removed =true
     {
          hedgehog_pos_updated==1;// clear new data flag, changed true to 0

          hedgehog_x= altSerial.read();
          hedgehog_y= altSerial.read();
          hedgehog_h= altSerial.read();
          
          Serial.print("X=");
          Serial.print(hedgehog_x, DEC );
          Serial.print(" ");  
          Serial.print("Y=");
          Serial.print(hedgehog_y, DEC );
          Serial.print(" ");  
          Serial.print("H=");
          Serial.print(hedgehog_h, DEC );
          Serial.print(" \n");  
      }
  delay(250);
}

I'm confused. Mega in one pic and Uno in another pic. Pick Uno, Mega or both. Just so people who are helping you know what's the deal.

Why is there a delay of 250mS? Can you be sure that for 1/4 of the time that the Uno is to be running you want the Uno to do nothing but wait for 250mS?

During that time of waiting there is a guarantee that the hedgehog will not be sending data. Also, if the Uno wakes up when 12/64th of the data was sent by the hedgehog but the Uno or Mega missed that 12/64, so the rest of the data is received and is corrupted and does not print correctly on the screen is a desired condition?

Check this out

void loop_hedgehog()
{

in that function, is this code

 while(Serial.available() > 0)

but the serial port that the hedgehog is connected to is altSerial

And this may get you introuble

 loop_hedgehog();// Marvelmind hedgehog service loop

   if (altSerial.available()> 0)//hedgehog_pos_updated=true)//removed =true
     {

The code in loop_hedgehog() should, when pointed to the correct serial port to be used, have emptied the serial buffer. So, you are asking to see if there is stuff in the serial buffer before you print. Can you see how that will cause issues?

I think you should only print when hedgehog has been updated.

So fix that posted code and post the new code.

I thought hedgehog was set at 9600 >>>> altSerial.begin(115200);

I made those changes among others. I am currently using Arduino UNO but may switch to MEGA later. I will eventually be using this to control a robot. I've set the baud rate to '115200' bps. It seems to 'work' now, however it takes between 10-30 seconds to update the new location of the 'hedgehog'. Also, the serial output values do not have the correct decimal placement. On the attachment provided, the values should have been (-0.80, 0.67,-0.42).
This is updated 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
//#include <SoftwareSerial.h>
#include <AltSoftSerial.h>

AltSoftSerial altSerial(9, 8, 1);//rx tx
#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(115200); // hedgehog transmits data on 500 kbps
    altSerial.begin(115200);
    //Serial1.begin(9600);
    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(altSerial.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= altSerial.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; 
        }
    }//end while

  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
        }
    }
}//void loop_hedgehog

// 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///
//SoftwareSerial mySerial(10, 11); //RX and TX
void setup()
{
  Serial.begin(115200);
  altSerial.begin(115200);
  setup_hedgehog();//Marvelmind hedgehog support initialize
  //altSerial(10, 9);
  //Serial1.begin(9600);
  //Serial1(19, 18, 1);//RX and TX and inverting logic
  //Serial2.begin(9600);
}

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

   if (hedgehog_pos_updated);
     {
          hedgehog_pos_updated=0;

          //hedgehog_x= altSerial.read();
          //hedgehog_y= altSerial.read();
          //hedgehog_h= altSerial.read();
          
          Serial.print("X=");
          Serial.print(hedgehog_x, DEC );
          Serial.print(" ");  
                 
          Serial.print("Y=");
          Serial.print(hedgehog_y, DEC );
          Serial.print(" ");  
          
          Serial.print("H=");
          Serial.print(hedgehog_h, DEC );
          Serial.print(" \n");  
      }
}

Have you looked at the receive hedgehog info thingy with an eye towards why ints and not floats?

The reason why floats are not being received is the below code.

  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

So you can leave the current code alone and miltiply the ints by .1 or .01 or .001 or .XXXXXXXXXXXxxxxxxxWhaterever to get the float values you want as a final output.

If you are using a Uno or Mega, I say keep them ints for as long as possible. On a Due, STM32 or ESP32 use floats.

When you get that fixed post the new code, please. +1

Instead of that I just multiplied at the end.
changed: from 115200 to 9600

Serial.begin(9600);
altSerial.begin(9600);

and added (*0.01)

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

   if (hedgehog_pos_updated);
     {
          hedgehog_pos_updated=0;

          //hedgehog_x= altSerial.read();
          //hedgehog_y= altSerial.read();
          //hedgehog_h= altSerial.read();
          
          Serial.print("X=");
          Serial.print(hedgehog_x*0.01, DEC );
          Serial.print(" ");  
                 
          Serial.print("Y=");
          Serial.print(hedgehog_y*0.01, DEC );
          Serial.print(" ");  
          
          Serial.print("H=");
          Serial.print(hedgehog_h*0.01, DEC );
          Serial.print(" \n");  
      }
}

Thanks for the help!

Solved?

If so add "[solved} to the title.