Hello,
Hope everyone has a great New Year
Finally have my webcam hooked up to my bot using RoboRealm tracking 'a red object' setup.
The data 'distanceX' is sent via Serial to my Arduino. DistanceX is basically the center of gravity of the object, X value of a 320 x 240 image on cam.
In my Arduino sketch I have a function 'trackCalc()' to receive the data in a buffer and depending on the value of distanceX, drives my motors, then resets the buffer.
The problem I'm having is that when an object is within the center threshold (forward), every several seconds it stutters just for a second (almost like it wants to turn).
Left and right differential turns are fine though...
Here is the function:
void trackCalc()
{
if (Serial.available())
{
// Get the data coming through the serial port and store it in the buffer
while (z < 4)
{
incomingData[z] = Serial.read(); // Assign the input value to the incomingData buffer
z++; // Increment the counter
}
distanceX = atoi(incomingData); // Convert ASCII to Int
if((distanceX > 100) && (distanceX < 220)) // forward threshold of image (X value or 320)
{
// go forward
myservoSteering.write(neutralSteering);
myservoThrottle.write(medFowThrottle);
}
else
{
if(distanceX <= 99)
{
myservoThrottle.write(neutralThrottle);
myservoSteering.write(slowLeftSteering);
}
else if(distanceX >= 221)
{
myservoThrottle.write(neutralThrottle);
myservoSteering.write(slowRightSteering);
}
}
}
z = 0; // Reset the counter
delay(20);//delay(20);// Delay 20ms to allow the servo to rotate to the position
}
And the code in the loop:
void loop()
{
//...
// PING code here...
//...
if(range [2] >= pingThreshCenter) // if no obstructions detected on center PING sensor run function 'trackCalc()'
{
trackCalc();
}
else if(range [2] < pingThreshCenter) // if there is an obstruction detected, stop motors (neutral).
{
myservoThrottle.write(neutralThrottle);
myservoSteering.write(neutralSteering);
//delay(10);
}
}
Thomas