Project-SBC
Published © CC BY

HuskyLens Data Packet Parser

A quick arduino code to format the data received by a DF Robot HuskyLens..

BeginnerProtip6 minutes2,588
HuskyLens Data Packet Parser

Things used in this project

Hardware components

Arduino Leonardo
Arduino Leonardo
×1

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Code

HuskyLens Data Viewer

Arduino
This code can be uploaded to an Arduino Leonardo or other similar Arduino to parse the incoming data into a viewable format.
void setup() {
  //Open USB Serial
  Serial.begin(57600);
  //Open HuskyLens Serial
  Serial1.begin(57600);
}

void loop() {

  //If connection to HuskyLens is available then run code
  if (Serial1.available() > 0) {
      //Read data to look for first header of data packet 0x55
      if (Serial1.read() == 0x55) {
        //Delay to allow next byte to be received in buffer, repeat for remainder of packet until first data is received
        delay(1);
        // Header 2
        Serial1.read();
        delay(1);
        // Device Address
        Serial1.read();
        delay(1);
        // Data Length
        Serial1.read();
        delay(1);
        // Command ID
        Serial1.read();
        delay(1);

        
        // X origin coordinate
        //Read low byte of X origin coordinate
        long int oX = Serial1.read();
        //Delay to populate buffer for high byte of X origin coordinate
        delay(1);
        //Multiply high byte of buffer by 256 and add to low byte to determine X origin coordinate
        oX = Serial1.read()*256 + oX;
        //Print coordinate
        Serial.print("X Origin ");
        Serial.println(oX, DEC);
        delay(1);

        // Y origin coordinate
        //Read low byte of Y origin coordinate
        long int oY = Serial1.read();
        //Delay to populate buffer for high byte of Y origin coordinate
        delay(1);
        //Multiply high byte of buffer by 256 and add to low byte to determine Y origin coordinate
        oY = Serial1.read()*256 + oY;
        //Print coordinate
        Serial.print("Y Origin ");
        Serial.println(oY, DEC);
        delay(1);

        // Box Width [Line tracking mode: X Termination Coordinate]
        //Read low byte of Box width/X termination coordinate
        long int tX = Serial1.read();
        //Delay to populate buffer for high byte of X value
        delay(1);
        //Multiply high byte of buffer by 256 and add to low byte to determine X value
        tX = Serial1.read()*256 + tX;
        //Print coordinate
        Serial.print("X Value ");
        Serial.println(tX, DEC);
        delay(1);

        // Box Height [Line tracking mode: Y Termination Coordinate]
        //Read low byte of Box height/Y termination coordinate
        long int tY = Serial1.read();
        //Delay to populate buffer for high byte of Y value
        delay(1);
        //Multiply high byte of buffer by 256 and add to low byte to determine Y value
        tY = Serial1.read()*256 + tY;
        //Print coordinate
        Serial.print("Y Value ");
        Serial.println(tY, DEC);
        delay(1);

        // Object Learned ID
        //Read low byte of learned ID
        long int ID = Serial1.read();
        //Delay to populate buffer for high byte of ID
        delay(1);
        //Multiply high byte of buffer by 256 and add to low byte to determine ID
        ID = Serial1.read()*256 + ID;
        //Print coordinate
        Serial.print("Object Learned ID ");
        Serial.println(ID, DEC);
        delay(1);

  
        //checksum
        Serial1.read();
        Serial.println("------");

      

      }
  }

}
        

Credits

Project-SBC
1 project • 3 followers

Comments