Simple target tracking code not working

userHead williampretty 2026-04-22 23:16:42 18 Views0 Replies

So now that I have the simple target tracking code working it is time to write some code that causes the robot to track the object outlined in the blue box.

The following code generates the following error: 

 

error: expected primary-expression before '.' token
    if (Result.xCenter < (160 - centerThreshold)) {

 

----------- Code ------------

 

#include "DFRobot_HuskylensV2.h"

#include "DFMobile.h"

#include "Wire.h"


 

// Create object

HuskylensV2 huskylens;


 

DFMobile Robot(4, 5, 7, 6); // Motor control pins for Romeo

int centerThreshold = 50; // Tolerance for steering


 

void setup() {

  Wire.begin();

  Serial.begin(9600);

  while (!huskylens.begin(Wire)) {

    Serial.println("Begin HUSKYLENS failed!");

    delay(100);

  }

 

 Serial.print("Switching to tracking mode");


 

  // Switch to Object Tracking mode


 

  huskylens.switchAlgorithm(ALGORITHM_OBJECT_TRACKING);


 

}


 

void loop() {

   // Request results from the sensor


 

      if (huskylens.available(ALGORITHM_OBJECT_TRACKING)) {

    huskylens.getResult(ALGORITHM_OBJECT_TRACKING);

   

    // Check if object is left, right, or center

    if (Result.xCenter < (160 - centerThreshold)) {

      // Object on left: Turn Left

      Robot.Speed(-150, 150);

    } else if (Result.xCenter > (160 + centerThreshold)) {

      // Object on right: Turn Right

      Robot.Speed(150, -150);

    } else {

      // Object centered: Move Forward

      Robot.Speed(200, 200);

    }

    }  

   else {

    // No object found: Stop

    Robot.Speed(0, 0);

  }

 

}