Tuesday, June 12, 2012

My Obstacle Avoiding Car Project, Part 1

Last night was fun.  I got my lego Technic kits and made three things in a few hours.

 I think all three will work very well when I custom make a motor and servo to fit.  The goal, to make it become an obstacle avoiding car. So that left me this morning to work on figuring out some code.
I did a bit with it.  Most of it is detailed in other blogs today to help keep organized.
But first, I needed to figure out how to get a servo working on the Arduino, so I modified a piece of code and turned it into a test code for servos.  It taught me a bit about how to write positions for servos.  That code is on the next blog.   After that, I worked on trying to discover why our ping sensor was not very reliable.  I wrote a test code for that also.  But am still wrestling with some issues.  I talk about that in the next blog also.

The last thing that I did was to try to sketch out the basic idea for how the code will actually run when I have the completed project.  Here are those thoughts.  I wrote this sketch down while waiting for the wind to blow so I could go kite boarding.

The first idea
define - stop_distance, slow_distance, med_distance, fast_distance
  (This was to help me later on tweak my ideas and important distances that would help govern the speed of the car)
Next was to break down three loops.
1. If both sensors are less then stop_distance
    Then Back UP  ( I am not sure if I should have a turn when I back up or not.  TBD)
2. If both sensors are between the slow_distance and fast_distance
    Then set the motor speed as a linear function of the distance.
3. Else
    Turn the motor on full.

So then I realized that this would not work at all because I was not addressing the need to turn and how that would be affected by the distances to sensors.  So came idea 2.
Still use three loops, (Or maybe if statements.  Not sure.  Will need to ask.)
1. "Back Up Range (0..10 inches approx.)
     If Both Sensors are in 0..10 range, then do...
     Set fwd_pin to LOW and delay for 4 seconds.
    Set rev_pin to HIGH but at 1/4 speed.  (The idea here is that I will need two 5v. loops so that I can run the electricity fwd or backwards through the motor to get it to move fwd or backwards)
2. "In the Turning Range" (0..30 inches approx.)
     If either sensor is in 0..30 range, then do set fwd_speed to function of distance...
    if Rsensor is less then Lsensor, then set servo to be an inverse function of difference.
   Visia versa  (But the issue was weather how to write this funtion so I could get a variable turning ability)
3. else motor fwd full speed.

And that got me to the third idea which is getting closer to something that actually looks like code.
1. "Back Up Range" (Both Sensors are 0..10 inches approx.)
   fwd_pin = 0
   Delay(4000)
   Servo (Center_position - 5)    (This is to turn while backing up.  I also am going to use a Center Position defination so I can make some calibration part in the beginning so that the car will drive straight)
  Delay(100)  (These delays are so that I don't bust my motors or gears and so the servo can get into position before the car starts going backwards)
   Rev_Pin = 50  (I assume this can be a value between 0 and 254. I want it to back up slowly)
  Check the back up range again.  If it is still in back up range, keep doing this.  If it is not in back up range any more, then move to next piece of code.
2. "Turn Range" (Either Sensor is 0..20 inches approx.)
    fwd_pin = (function of Avg. Distance s.t. when avg = 20, speed should be very slow.)
   If the Rsensor is less then Lsensor, then Servo(Center+(-mx+b))  S.T x is the difference between sensors
   If the Lsensor is less then Rsensor, then Servo(Center-(-mx+b))
   Check this loop again to see if it still applies.  Otherwise, move on.
3.  Full Speed ahead.  No Obstacles.


No comments:

Post a Comment