HUSKYLENSGeneral

Problem with tracking tilt

userHead hajnyondra 2020-09-19 20:48:26 2793 Views2 Replies
I'm trying to move x y tilt to the center of the object. I'm using 180 degree servos and huskylens is on top of them.
Problem is that for instance, I get coordinates x=100 and it moves in the opposite direction of the object.
This program is on esp32 :


#include <HardwareSerial.h>
#include <HuskyLensProtocolCore.h>
#include <HUSKYLENS.h>
#include <HUSKYLENSMindPlus.h>
#include <DFRobot_HuskyLens.h>
#include <M5Stack.h>
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
#include <ESP32Servo.h>

HUSKYLENS huskylens;
#define RXD2 17 //Oops Switched my wires!!
#define TXD2 16 //Oops Switched my wires!!

int ID1 = 1;
int ax;
int ay;


const int freq = 50;
const int ledChannel = 4;
const int ledChannel2 = 5;
const int resolution = 10;



Servo myservo;
Servo myservo2;// create servo object to control a servo

void setup() {
M5.begin();

pinMode(4, OUTPUT);
pinMode(5, OUTPUT);

ESP32PWM::allocateTimer(0);
ESP32PWM::allocateTimer(1);
ESP32PWM::allocateTimer(2);
ESP32PWM::allocateTimer(3);
myservo.setPeriodHertz(50);
myservo2.setPeriodHertz(50);// standard 50 hz servo
myservo.attach(4, 771, 2740);
myservo2.attach(5, 500, 2400);
Serial.begin(115200);
Serial2.begin(9600, SERIAL_8N1, RXD2, TXD2); //Communication with Huskylens

while (!huskylens.begin(Serial2))
{
Serial.println(F("Failbus!"));
delay(100);
}
//huskylens.writeAlgorithm(ALGORITHM_LINE_TRACKING);
}

void loop() {
M5.update();

int32_t error;
if (!huskylens.request(ID1)) {
Serial.println(F("Fail to request data from HUSKYLENS, recheck the connection!"));
}
else if (!huskylens.isLearned()) {
Serial.println(F("Nothing learned, press learn button on HUSKYLENS to learn one!"));
}
else if (!huskylens.available()) {
Serial.println(F("No block or arrow appears on the screen!"));
}
else
{
HUSKYLENSResult result = huskylens.read();
printResult(result);

// Calculate the error: where it needs to move for center
error = (int32_t)result.xTarget - (int32_t)160;
}
delay(100);

}

void printResult(HUSKYLENSResult result) {





if (result.xCenter != 160) {


int xpr = result.xCenter;
xpr = map(xpr, 0, 320, 180, 0);
myservo.write(xpr);
delay(1000);
Serial.println("x=");
Serial.println(xpr);



}

// if (result.yCenter != 120) {
// delay(100);

// int ypr = result.yCenter;
// ypr = map(ypr, 0, 240, 180, 0);
// myservo2.write(ypr);
// Serial.println("y=");
// Serial.println(ypr);}
}

It's probably some error in my calculation but I'm simply not able to find it
2020-09-20 05:06:09 I'm not clear on how your servo connections work, but you might just try reversing one of the pairs of arguments in your map() call. Either use

xpr = map(xpr,320, 0, 180, 0);
or
xpr = map(xpr, 0, 320, 0, 180);

I haven't used map() for this. so I'm not sure about it. I have used a servo to position a Huskylens, but have used more direct calculations to change the servo position.

Hope this helps.
userHeadPic james.stanley