Society of Robots - Robot Forum

Software => Software => Topic started by: Hydro on May 25, 2007, 04:06:03 AM

Title: Dual sensors differential drive?
Post by: Hydro on May 25, 2007, 04:06:03 AM
Was wondering how I could accomplish a simple thing as that.
I mean, I obviously have to take in both sensors in consideration for each motor, but is there a short way to combine the two?

Last year I did something that involved one sensor, so it was easy, and I even used a derived answer or something (not sure how to say it in English :p - the method that involves checking the last error).

Basically, what I'm looking for (I'm using normal GP12, by the way) is a simple formula and calculation method, to keep my robot right in between 2 walls. Also, since it was a year ago, I don't really remember what, and how I used the "last error" method, but I remember that without it the robot would drive like a wave - coming closer and further from the wall, never slowing down to a full line. So prehaps, including that would be great.

Thanks.
Title: Re: Dual sensors differential drive?
Post by: JonHylands on May 25, 2007, 04:53:58 AM
You're looking for PID control - doing wall following with PID is relatively straight-forward...

- Jon
Title: Re: Dual sensors differential drive?
Post by: Hydro on May 25, 2007, 06:35:08 AM
Yes, I've read about it, but I'm not on how to take 2 sensors into account, for both motors.
Title: Re: Dual sensors differential drive?
Post by: Admin on May 25, 2007, 07:07:43 AM
Quote
Yes, I've read about it, but I'm not on how to take 2 sensors into account, for both motors.
Its exactly like a photovore, but instead of light brightness, you use distance to the wall.
http://www.societyofrobots.com/programming_photoresistor.shtml

Have the robot drive straight when distances are equal. The code and electronics are EXACTLY the same as a photovore, just the sensors are different.
Title: Re: Dual sensors differential drive?
Post by: Hydro on May 26, 2007, 02:33:54 AM
Well, of course that's one way of using it.
I'm just thinking if there's a way to use it, with just one line, no "if"s.
(Considering I have a Drive function, which works like so: Drive(Left Motor Speed%, Right Motor Speed%)).
I was wondering if there's a simple way I could use to calculate the needed %?

Maybe something like (assume SensLeft and SensRight are already new information)
(SensLeft - SensRight) * KC + General_Speed.
would work? (Well, for the Right Motor you use SensRight - SensLeft).
Title: Re: Dual sensors differential drive?
Post by: Admin on May 26, 2007, 07:57:00 AM
you are very close, try this:

Left Motor = SensLeft * KC + General_Speed
Right Motor = SensRight * KC + General_Speed

or this:

Right Motor = SensLeft * KC + General_Speed
Left Motor = SensRight * KC + General_Speed

check this out:
http://www.societyofrobots.com/programming_fuzzy_logic.shtml
Title: Re: Dual sensors differential drive?
Post by: Hydro on May 26, 2007, 10:23:12 AM
The problem with what you gave is that I simply don't think it will try to center itself in between the 2 surrounding walls.
Or am I wrong?


Also, if I want to use the "last error" method again, it should be something like this, right?
global variable -> unsigned int lastLeftError = 0, lastRightError = 0;
...
LeftMotor = LeftSens * KC + General_Speed - lastLeftError * KC2
RightMotor = RightSens * KC + General_Speed - lastRightError * KC2
Title: Re: Dual sensors differential drive?
Post by: Admin on May 26, 2007, 10:48:00 AM
Hmmmm I feel stupid, ignore my previous advice . . . it wont actually work . . .

Im not sure why you dont want IF statements . . . I think what you dont want is case based, right?

So there are two ways to drive between the walls:
1) if the distance between both walls are always equal, then put both IR sensors on only one side of your robot (the right side in this example).

//if too far from wall, turn right
if distance_to_wall_threshold_far > (sense_top+sense_bottom)/2
then left_motor = ((sense_top+sense_bottom)/2 + distance_to_wall_far)*constant + general_speed
and  right_motor = (distance_to_wall_far - (sense_top+sense_bottom)/2)*constant + general_speed

//if too close to wall, turn left
if distance_to_wall_threshold_close < (sense_top+sense_bottom)/2
then left_motor = (distance_to_wall_close - (sense_top+sense_bottom)/2)*constant + general_speed
and  right_motor = ((sense_top+sense_bottom)/2 + distance_to_wall_close)*constant + general_speed

//else go straight
left_motor = general_speed
right_motor = general_speed

there are many other ways to do that above code

2) if the center location between walls is unpredictable, you cannot use just two sharp IR to center your robot. its impossible. unless of course if your robot does mapping, you can predict where the center is. or if you mount your sharp IR on a scanning servo.
Title: Re: Dual sensors differential drive?
Post by: Hydro on May 26, 2007, 10:57:37 AM
I don't see why you can't center you robot.
I mean, you know the robot's width, and you have 2 IR sensors on both sides of the robot.
Your aim is basically to reach a point where both IR read the same value...

However, the distance is known and constant, but I want to follow both walls, hence why I have 2 IRs, one on each side.
That might explain why I thought:
RightMotor = (SensLeft - SensRight) * KC + General_Speed.
LeftMotor = (SensRight - SensLeft) * KC + General_Speed.

Because, if both sensors return about same distance from the wall, the KC will be nullified, and I'll be left with the General_Speed alone, on both engines. If the right sensor returns a larger value then the left one, then the RightMotor will decrease, and LeftMotor increase - the robot continues forward, but drives towards the right wall.
Same for the left wall...


What about the lastError one?
Title: Re: Dual sensors differential drive?
Post by: Admin on May 26, 2007, 11:42:32 AM
Quote
I don't see why you can't center you robot.
I mean, you know the robot's width, and you have 2 IR sensors on both sides of the robot.
Your aim is basically to reach a point where both IR read the same value...
Picture it for a second. If there are IR on both sides of the robot, how would your robot know if it is pointing 30 degrees or -30 degrees? It cant with that setup.
Title: Re: Dual sensors differential drive?
Post by: Hydro on May 26, 2007, 11:46:39 AM
Should've probably mentioned both IR's are at 45? to the wall.

So basically each motor should work on the corrosponding IR relatively to him? (Left works with Right motor, etc?).
Title: Re: Dual sensors differential drive?
Post by: Admin on May 26, 2007, 12:21:55 PM
Quote
So basically each motor should work on the corrosponding IR relatively to him? (Left works with Right motor, etc?).
Again, if you put one on each side of your robot, it wont work. Try if you dont believe me :P
Even at 45 degrees, your robot wont know if its really close the the left wall, or if the robot is just pointing left.

And you must compare values from both sensors, its not a 'split brain' system . . . ignore the photovore comment I made before, I wasnt thinking at the time ;D
Title: Re: Dual sensors differential drive?
Post by: Hydro on May 26, 2007, 01:09:21 PM
Quote
So basically each motor should work on the corrosponding IR relatively to him? (Left works with Right motor, etc?).
Again, if you put one on each side of your robot, it wont work. Try if you dont believe me :P
Even at 45 degrees, your robot wont know if its really close the the left wall, or if the robot is just pointing left.

And you must compare values from both sensors, its not a 'split brain' system . . . ignore the photovore comment I made before, I wasnt thinking at the time ;D
Ok :P
So just a final question - relating to the one I asked about the "lastError" method.
Is it like this:
global variable -> unsigned int lastError = 0;
...
Motor <- LeftSens * KC + General_Speed - lastError * KC2
lastError <- currentError (Current Distance - Wanted Distance)
Title: Re: Dual sensors differential drive?
Post by: Admin on May 26, 2007, 01:24:31 PM
Your robot will probably work fine without any PID on the sensors, so I wouldnt worry about it.

Quote
Is it like this:
global variable -> unsigned int lastError = 0;
...
Motor <- LeftSens * KC + General_Speed - lastError * KC2
lastError <- currentError (Current Distance - Wanted Distance)
You dont want it set as a global variable, just local. And you need to incorporate both sensors in your equation, one alone wont work. The rest looks ok to me.
Title: Re: Dual sensors differential drive?
Post by: Ratfink on June 09, 2007, 06:25:16 PM
I came across the exact same problem and the simple fact is no matter what you do you cannot drive exactly straight.

Next time your in the passenger seat observe a normal person driving you will notice they will also very slightly adjust the direction of the car from right to left to try and stay "straight" which is always relatively center.

The way i did it was to simply have two sensors pointing 40 degrees from center both right and left and set limits on the right and left sensors to adjust for the other direction. Its very simple but very effective, to try and make the car appear to be moving straight set the angle of the wheel based on the difference between both sensors. Once the sensor difference is within a certain limit adjust the angle to move like 1 degree in the other direction once a limit has been reached. This will make the car stay within the center of two walls and be relatively straight.

I got the videos here: http://www.project-scamp.com/testvids.html

I didn't exactly set the wheel angle limits small but i got to this point and was happy with it, as the purpose of the project didn't require the car to appear to be straight as possible.

Quick tip for turns: Your car is in the center after a turn when both the right and left centers are more or less the same i put limits in the region of +-3 difference between right and left sensor and it works fine.