Lag(?) issues

I want the code to:

  • start with some kind of "heading"(intiatially this will be straight forward, or 90 for the servo with the sensor on it, but in subsequent loops this can change depending on where the person the robot is following is detected)
  • turn the servo "heading" + scan degrees
  • engage the sensor
  • return a boolean reading (detectL) on whether the target is within a minimum and maximum ranges
  • turn the servo "heading" - 2 * scan degrees
  • engage the sensor
  • return a second boolean reading (detectR) on whether the target is within the minimum and maximum ranges
  • based on "heading" produce some kind of steering value
    based on these two readings:
    a) both are true (person is in front of the robot)
  • set the rMot and lMot to the base speed value
  • increment the base speed value (yes after setting rMot and lMot so the next loop will be faster)
    b) detectR: true, detectL: false (person is to the right of the robot/ right front)
  • decrease the "heading" by scan (set the heading more rightward)
  • decrease base speed value (before writing motor pwm values)
  • set lMot to base value, set rMot to base value -steering value (to turn the robot right)
    c) detectR: false detectL: true (person is to the left/ left front of the robot)
  • increase "heading" by scan (set heading more left ward)
  • decrease base speed value (before writing motor pwm values)
  • set rMot to base value, set lMot to base value minus steering value
    d) both false (person not detected)
  • set rMot and lMot to 0 (stop the robot)
  • set base back to min value (robot is expect to stop, so it will need to be brought back up to speed when the person is detected again)
  • analoge write motor values
    loop

caveats:

  • the sensor doesn't always detect on the first try, so the sensing procedure is put in a loop to give it a few chances to detect
  • operating the sensor by itself, the sensor performs fastest with a delay of 100ms on the end of the sensing procedure as show in http://arduino.cc/en/Tutorial/Ping?from=Tutorial.UltrasoundSensor. I have tried shortening and removing entirely the large delay, but the device performs fast with that delay.
  • the steering logic/ method is very subject to change, I just want some kind of steering related to the heading of the robot
  • the base speed value logic/ method is very subject to change, I just don't want to robot to jump or lyrch when starting from a stop, slow down in turns, etc.