Line Tracking Example

Line tracking reads route angle, length and X/Y data, like a digital ruler following a road map. Using HUSKYLENS 2 with UNIHIKER K10, the code initializes I2C, switches to ALGORITHM_LINE_TRACKING, fetches the current branch, then prints angle, length, vector components and upcoming branch count to the K10 canvas display in real time.

1.Line Tracking Code Example

Before You Start:

1.1 Line Tracking and Output Relevant Data

Under the Line Tracking function, HUSKYLENS 2 can mark the trajectory of the route in the image and obtain the current route's length, angle, and X/Y components. When the route has branches, it can obtain the number of branches at the intersection and the corresponding data of each branch counterclockwise.

The example program is as follows:

#include "unihiker_k10.h"
#include "DFRobot_HuskylensV2.h"
// Create objects (Huskylens: line tracking; UNIHIKER K10: screen)
HuskylensV2  huskylens;
UNIHIKER_K10 k10;
uint8_t      screen_dir = 2;  // Screen display direction


// Main program initialization
void setup() {
  k10.begin();          // Initialize UNIHIKER K10
  Wire.begin();         // Initialize I2C communication

  // Wait for Huskylens connection (retry every 100ms)
  while (!huskylens.begin(Wire)) {
    delay(100);
  }

  k10.initScreen(screen_dir);       // Init screen with set direction
  k10.creatCanvas();                // Create drawing canvas
  huskylens.switchAlgorithm(ALGORITHM_LINE_TRACKING);  // Set to line tracking mode
}

void loop() {
  huskylens.getResult(ALGORITHM_LINE_TRACKING);  // Get latest line tracking data

  if (huskylens.available(ALGORITHM_LINE_TRACKING)) {
    // Store current branch data to simplify repeated calls
    auto currentBranch = huskylens.getCurrentBranch(ALGORITHM_LINE_TRACKING);
    
    // Display line tracking info (blue: 0x0000FF; font: eCNAndENFont24)
    k10.canvas->canvasText(String("Route angle: ") + String(RET_ITEM_NUM(currentBranch, Result, angle)), 0, 0, 0x0000FF, k10.canvas->eCNAndENFont24, 50, true);
    k10.canvas->canvasText(String("Route length: ") + String(RET_ITEM_NUM(currentBranch, Result, length)), 0, 30, 0x0000FF, k10.canvas->eCNAndENFont24, 50, true);
    k10.canvas->canvasText("X & Y components:", 0, 60, 0x0000FF, k10.canvas->eCNAndENFont24, 50, true);
    k10.canvas->canvasText(String(RET_ITEM_NUM(currentBranch, Result, xTarget)) + String(",") + String(RET_ITEM_NUM(currentBranch, Result, yTarget)), 0, 90, 0x0000FF, k10.canvas->eCNAndENFont24, 50, true);
    k10.canvas->canvasText(String("Junction branch ") , 0, 120, 0x0000FF, k10.canvas->eCNAndENFont24, 50, true);
    k10.canvas->canvasText( (String("count: ") +String(huskylens.getUpcomingBranchCount(ALGORITHM_LINE_TRACKING))), 0, 150, 0x0000FF, k10.canvas->eCNAndENFont24, 50, true);
    
  }

  k10.canvas->updateCanvas();  // Refresh screen
  delay(1000);                 // 1s delay for stable data reading
}

Running Result:As shown in the figure, align the HUSKYLENS with the route map, and observe that the K10 screen displays output data such as the current route length and angle. When the route consists of multiple branches,

Interface Diagram

Was this article helpful?

TOP