Hi, I am trying to read distance data from ESP UWB sensors with Serial. My code works fine when just reading, but when I try to use the distance to control my RoboClaw motor controller, it does not work... any insight on what I can try? Thanks!
Btw... in turn_left, turn_right, etc, even if I do a serial print, it messes up the read and gives me "ERROR: STALE PACKET"
#include "RoboClaw.h"
#include <SoftwareSerial.h>
#include "SerialTransfer.h"
SoftwareSerial serial(10,11);
RoboClaw roboclaw(&serial, 10000);
SerialTransfer anchor1_transfer;
SerialTransfer anchor2_transfer;
#define address 0x80
float output1;
float output2;
unsigned long starttime;
unsigned long endtime;
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
Serial3.begin(115200);
anchor1_transfer.begin(Serial3);
Serial2.begin(115200);
anchor2_transfer.begin(Serial2);
roboclaw.begin(38400);
}
void read_serial() {
if(anchor2_transfer.available()){
uint16_t recSize = 0;
Serial.println("New Data 2");
recSize = anchor2_transfer.rxObj(output2, recSize);
Serial.println(output2);
}
else if(anchor2_transfer.status < 0){
Serial.print("ERROR: ");
Serial.println(anchor2_transfer.status);
}
if(anchor1_transfer.available()){
uint16_t recSize = 0;
Serial.println("New Data 1");
recSize = anchor1_transfer.rxObj(output1, recSize);
Serial.println(output1);
}
else if(anchor1_transfer.status < 0){
Serial.print("ERROR: ");
Serial.println(anchor1_transfer.status);
}
}
void turn_left() {
roboclaw.ForwardM1(address, 20);
}
void turn_right() {
roboclaw.ForwardM2(address, 20);
}
void forward() {
roboclaw.ForwardM1(address, 20);
roboclaw.ForwardM2(address, 20);
}
void stop () {
roboclaw.ForwardM1(address, 0);
roboclaw.ForwardM2(address, 0);
}
void loop () {
read_serial();
// when the following is commented out, the readings are fine.
// when the following is not commented, I get 'ERROR: STALE PACKET'
if (output1 > 0.5){
if (output1 - output2 > 0.4){
turn_right();
}
else if (output2 - output1 > 0.4){
turn_left();
}
else{
forward();
}
}
else{
return stop();
}
}