I am trying to solve some Create 2 sensor reading problem that I am having when I came across @NBCKLY's posts (Part 1 and Part 2) that I believe are exactly what I am looking for. I copied his code from the original post into my project and updated the code from the second post as best as I could interpret...but something is not going according to plan.
For example, I am printing the angle to my serial monitor (for now) but I am constantly getting a value of 0 (sometimes 1).
Can @NBCKLY or anybody please check out this code and tell me what I'm doing wrong? I would appreciate it. Thank you very much.
int baudPin = 2;
int data;
bool flag;
int i;
int ledPin = 13;
int rxPin = 0;
signed char sensorData[4];
int txPin = 1;
unsigned long baudTimer = 240000;
unsigned long prevTimer = 0;
unsigned long thisTimer = 0;
void drive(signed short left, signed short right) {
Serial.write(145);
Serial.write(right >> 8);
Serial.write(right & 0xFF);
Serial.write(left >> 8);
Serial.write(left & 0xFF);
}
void updateSensors() {
Serial.write(149);
Serial.write(2);
Serial.write(43); // left encoder
Serial.write(44); // right encoder
delay(100);
i = 0;
while (Serial.available()) {
sensorData[i++] = Serial.read();
}
int leftEncoder = int((sensorData[0] << 8)) | (int(sensorData[1]) & 0xFF);
int rightEncoder = (int)(sensorData[2] << 8) | (int)(sensorData[3] & 0xFF);
int angle = ((rightEncoder * 72 * 3.14 / 508.8) - (leftEncoder * 72 * 3.14 / 508.8)) / 235;
Serial.print("\nAngle: ");
Serial.print(angle);
Serial.print("\n");
}
void setup() {
pinMode(baudPin, OUTPUT);
pinMode(ledPin, OUTPUT);
pinMode(rxPin, INPUT);
pinMode(txPin, OUTPUT);
delay(2000);
Serial.begin(115200);
digitalWrite(baudPin, LOW);
delay(500);
digitalWrite(baudPin, HIGH);
delay(100);
Serial.write(128);
Serial.write(131);
updateSensors();
drive(50, -50);
}
void loop() {
thisTimer = millis();
if (thisTimer - prevTimer > baudTimer) {
i = 0;
prevTimer = thisTimer;
digitalWrite(baudPin, LOW);
delay(500);
digitalWrite(baudPin, HIGH);
Serial.print("Pulse sent...\n");
}
updateSensors();
}
#
What I am asking is why do I only get an angle of rotation of 0 or 1 degrees when the robot is moving in a circle. The angle should be incrementing while the robot is moving.
The output I am getting on the serial monitor shows a line of what looks like garble which I assume is supposed to be the bytes sent back from the Create which is followed by "Angle: 0 (or 1)" What I was expecting to see was an increasing angle value. (1,2,3...360, and so on).