I get the above error when I try to compile. The error points to lines 34 and 37.
#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);
}
}
PaulRB
April 23, 2026, 4:16pm
2
Use the Tools->Auto Format option in the IDE.
After that, post your code again. Also post the complete set of error messages (in code tags)
Looks like the correct expression is result.xCenter instead of Result.xCenter
PaulRB
April 23, 2026, 4:19pm
4
I can't see either result or Result defined.
It´s part of the HuskyLens library. See GitHub - HuskyLens/HUSKYLENSArduino · GitHub for reference.
I didn't find any result in the librsry that would be used that way.
I haven't time to go look again, but came back with this form one of the examlkes
void loop() {
while (!huskylens.getResult(ALGORITHM_OBJECT_CLASSIFICATION)) {
delay(100);
}
while (huskylens.available(ALGORITHM_OBJECT_CLASSIFICATION)) {
Result *result = huskylens.popCachedResult(ALGORITHM_OBJECT_CLASSIFICATION);
Serial.print("result->name=");
Serial.println(result->name);
Serial.print("result->classID=");
Serial.println(result->classID);
Serial.println();
}
delay(1000);
}
in which we see a result being initialised
Result *result = huskylens.popCachedResult(ALGORITHM_OBJECT_CLASSIFICATION);
The code goes on using the '-> ' notation.
a7
Which lines are 34 and 37?
#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);
}
}
Errors:
C:\Projects\Book - 4 - Robots\Devastator Robot\sketch_apr10a\sketch_apr10a.ino: In function 'void loop()':
C:\Projects\Book - 4 - Robots\Devastator Robot\sketch_apr10a\sketch_apr10a.ino:33:15: error: expected primary-expression before '.' token
if (Result.xCenter < (160 - centerThreshold)) {
^
C:\Projects\Book - 4 - Robots\Devastator Robot\sketch_apr10a\sketch_apr10a.ino:36:22: error: expected primary-expression before '.' token
} else if (Result.xCenter > (160 + centerThreshold)) {
^
exit status 1
Compilation error: expected primary-expression before '.' token
Nowhere in your code do you declare any object called Result.
docdoc
April 24, 2026, 7:54am
10
I don't have that hardware and I never used that library, but based on the examples, I think you just need to change your loop as follows:
// Request results from the sensor
if (huskylens.available(ALGORITHM_OBJECT_TRACKING)) {
huskylens.getResult(ALGORITHM_OBJECT_TRACKING);
Result *result = huskylens.popCachedResult(ALGORITHM_OBJECT_CLASSIFICATION);
// 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);
}
Give it a try.
@docdoc might have just added that the only substantial change to the loop is to create and initialize the previously missing variable result , viz:
Result *result = huskylens.popCachedResult(ALGORITHM_OBJECT_CLASSIFICATION);
and go on to use it, rather than Result subsequently.
Since result is a pointer, the '-> ' is appropriate.
@williampretty where did you find the code that informed your version? It looks like more than a closing brace was missing.
a7
I added the closing brace and now I am having troubles with dfmobile.h
I found the code via google. It was supposed to be example code.
Here is the Latest error after @docdoc 's code.
C:\Users\Bill\AppData\Local\Temp\cc1hdOUN.ltrans0.ltrans.o: In function global constructors keyed to 65535_0_sketch_apr10a.ino.cpp.o.2812': <artificial>:(.text.startup+0xbe): undefined reference to DFMobile::DFMobile(unsigned char, unsigned char, unsigned char, unsigned char)'
C:\Users\Bill\AppData\Local\Temp\cc1hdOUN.ltrans0.ltrans.o: In function loop': C:\Projects\Book - 4 - Robots\Devastator Robot\sketch_apr10a/sketch_apr10a.ino:43: undefined reference to DFMobile::Speed(int, int)'
collect2.exe: error: ld returned 1 exit status
exit status 1
Compilation error: exit status 1
Thanks everyone for your help.
I got the software working
I have more testing to do. I will share the code once I am done testing.
docdoc
April 27, 2026, 7:59am
15
Just out of curiosity, what was causing the error you mentioned in the previous post #12 ? A library version mismatch?
I think so. I found some code that I had written earlier that doesn't use dfmobile.h and it works well so far.
Here is what I have so far:
void camera() {
while (!huskylens.begin(Wire)) {
Serial.println("Waiting ... ");
Serial.println("Huskylens not found! Check wiring.");
delay(100);
}
// Switch to Object Tracking mode
huskylens.switchAlgorithm(ALGORITHM_OBJECT_TRACKING);
Serial.println("Switching to tracking mode");
while (true) {
Serial.println("*");
// Request results from the sensor
huskylens.getResult(ALGORITHM_OBJECT_TRACKING);
if (huskylens.available(ALGORITHM_OBJECT_TRACKING)) {
Serial.println("Tracking ...");
// Get the object closest to the center crosshair
auto target = huskylens.getCachedCenterResult(ALGORITHM_OBJECT_TRACKING);
// Check if object is left, right, or center
Serial.print(RET_ITEM_NUM(target, Result, xCenter));
if ( (RET_ITEM_NUM(target, Result, xCenter)) < (160 - centerThreshold)) {
//Object on left: Turn Left
turnLeft();
} else if ((RET_ITEM_NUM(target, Result, xCenter)) > (160 + centerThreshold)) {
//Object on right: Turn Right
turnRight();
}
}
}
}
It prints the xCenter coordinate but the vehicle doesn't move.
Switching to tracking mode
*
Tracking ...
152*
Tracking ...
152*
That's because a value of 152 fails the condition in each of the following if() statements.
Why aren't you checking the return value from that function call? See the library's README:
/**
* @fn getResult
* @brief Get recognition results from HuskyLens V2
* @param algo The algorithm to get results for
* @return COMMAND_RETURN_ARGS retValue==0 if successful, other values indicate errors
*/
int8_t getResult(eAlgorithm_t algo);
@williampretty - please post complete sketch.
The variabke Result is not defined in your snippet.
a7