Let's Make Robots!

Robot sweep taking too long

A few friends and I have been attempting to build an autonomous robot for our project using:

Arduino - Duemilenove 

Adafruit - Motorshield

Sparkfun - Bluesmirf

Parallax - Ping sensor

We were able to configure the robot such that it is able to run autonomously. We also tried implementing some code in processing to show where an object is detected and how far it is from the ping sensor; it looks similar to a radar.

The problem we are experiencing is when adding the code that interfaces with the processing sketch. Inside the loop, I have the following code:

 


 

 

void loop()

{

 

    LookAhead();    

    //Serial.print(inches);

    //Serial.println(" inches"); // Prints a line in the serial monitor

 

    if(inches >= minSafeDist) /* If the inches in front of an object is greater than or equal to the minimum safe distance (11 inches), react*/

      {

            AllForward(); // All wheels forward

            delay(110); // Wait 0.11 seconds

 

       }else // If not:

 

{

            AllStop(); // Stop all motors

            for (int i=0; i<180; i=i+1) {

             PingServo.write(i);

            delay(50);

            ping();

            delay(50);

           Serial.print("X");

          Serial.print(i);

         Serial.print("V");

        Serial.println(inches*2); 

            }

            LookAround(); // Check your surroundings for best route

 

      if(rightDist > leftDist) // If the right distance is greater than the left distance , turn right

        {

              turnRight();

 

      }else if (leftDist > rightDist) // If the left distance is greater than the right distance , turn left

{

              turnLeft();

 

      }else if (leftDist&&rightDist<minSafeDist) // If the left and right distance is smaller than the min safe distance (11 inch) go back

{

 

               GoBack();

         }

    }

}

 

 


 

The problem area is in bold. When the line of code is removed, the servo pans and receives information just fine. When observing the serial monitor, I can see that the distances from the ping sensor and the object show the correct distances. When we add that line of code to link to processing, it will start to pan really slow. It takes approximately 18 seconds for it to pan, but processing is able to receive the information and translate it. Adjusting the delay doesn't work, and the servo exhibits some kind of jitter. Any help would be appreciated. Thanks

 

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

I would suggest to create an array of measured distances from 0 to 180 degrees and replace the serial stuff in your for loop with writing the measurements to the array. As soon as your scanning is done, send all array data through serial at once to Processing to view it on the screen. This way you can see how long it takes for the scan to happen (use no delays in the scanning loop) and how long it takes for the serial stuff to happen. I would suggest to use the fastest baud rate possible with your setup so the serial takes little time.

2 solutions:

  1. As mentioned before - reduce your delays. Try using delayMicroseconds() for the ping sensor.
  2. taking a scan at 1 degree intervals is a waste as the ping sensor scans ±20°. scan every 15° if you want some overlap.

As said below, it's due to the delays, they're slowing down the servo, taking 100ms for every degree of turning of the servo, that ads up to a looooong time for a 180 sweep. 

change it to this and it'll be fine I reckon  (although I'm not sure if the ping needs delays to stay happy?): 

             AllStop(); // Stop all motors

            for (int i=0; i<180; i=i+1) {

             PingServo.write(i);

            delay(15);

            ping();

          

I'm not familiar with the Arduinos, but a couple of thoughts :-

As Chris says, the timings add up to what you're seeing, but you appear to imply that the problem is only there when you're outputting to the serial port.  Are those 'delay' statements milliseconds, or should it be running faster than that?

Is the serial.print command a background or foreground task?  If it's foreground (i.e. processor completes the serial send before continuing with the rest of the code), that would be the cause of slower operation.

Those delay times appear way to long for me.  Is the first delay a settling time? A typical servo will sweep 60 degrees in 150ms, so a single step will only take a very few.  If you move the servo after you take the reading, it will already be in place the next time the program goes around the loop and hits the Ping routine.  Why do you need the 'delay' command after the ping read at all?

If you're still having problems, try stepping the servo 2 instead of 1 to speed up the sweep.  That's what I'm doing on my latest robot with my 'double speed head sweep'.

For 180 steps:
{
Do some ping stuff
Do some serial stuff
Wait around and do nothing for a total of 100ms
}

Answer? 100ms * 180 = 18000ms = 18 seconds

Your code is working exactly as it should.

 

That could indeed be the location of his long run times. :)

that section of code calls other functions. Could you include the code for the other functions? Most specifically ping() as it is the only function inside the FOR loop. Also, if your ping() function returns a value, shouldn't you have something like distance = ping();? Otherwise, your later statement, Serial.println(inches*2); , won't do anything but multiply whatever value is in the inches variable by 2. Remember the inches variable in the ping(), if there is one, will cease to exist outside of that function due to scope rules. The only way that inches will carry over outside of the ping() function is if you declared it outside of the main loop.