### Author Topic: Algorithm for robot returning to original position  (Read 861 times)

0 Members and 1 Guest are viewing this topic.

#### SnowingTree

• Beginner
• Posts: 1
##### Algorithm for robot returning to original position
« on: January 11, 2013, 04:13:25 AM »
Basically, I have a robot with 2 wheels. There are encoders on both of the wheels and after the robot go somewhere else, it has to return to the original position.
Below is my sketch of the pathways of the robot.
Idea 2 is currently what I am trying to figure out but finding it difficult.
I know I must store the angular rotation of the wheel in a form of stack and pop it out to backtrack, but I can't make it work.
Additionally, I am curious on the possibility of successful implementation for the Idea 1, provided that the environment is new. Excluding the option of placing a beacon at original position.

« Last Edit: January 11, 2013, 04:21:46 AM by SnowingTree »

#### newInRobotics

• Supreme Robot
• Posts: 1,015
• N.I.R.
##### Re: Algorithm for robot returning to original position
« Reply #1 on: January 11, 2013, 01:48:18 PM »
It it is big track we are talking about, then using GPS can be one of the options. If You are talking about robot operating in the room, then You can use Dead Reckoning.

In my opinion Idea 2 is easier to implement as it does not require frame transformations using encoder data. Here's the way I'd try to achieve it, although I'm not sure if microcontroller You use has enough RAM to achieve it. To move robot You operate 2 servos/motors. To operate motor/servo one has to use PWM. My thought is that there should be two arrays (one for each servo/motor) in which You can record encoder count and PWM values every time PWM value changes. At the end of the track You can "play back" arrays to get to initial position. All that should work if track is in one plane (no hills or pits)  and motors turn at the same speed to both sides (probably it's more of an issue with modified servos that were not properly centred).

Here's some pseudo code:
Code: [Select]
`static void main(){    char leftIndexTracker = 0;    char rightIndexTracker = 0;    char leftCounter = 0;    char rightCounter = 0;    char leftSideArray[array_length];    char rightSideArray[array_length];    registerCounterInterrupts();}void setLeftPWM(byte pwmValue){    leftServoPWMValue = pwmValue;        leftSideArray[leftIndexTracker] = leftCounter;    leftCounter = 0;    leftIndexTracker++;    leftSideArray[leftIndexTracker] = pwmValue;    leftIndexTracker++;}void setRightPWM(byte pwmValue){    rightServoPWMValue = pwmValue;    rightSideArray[rightIndexTracker] = rightCounter;    rightCounter = 0;    rightIndexTracker++;    rightSideArray[rightIndexTracker] = pwmValue;    rightIndexTracker++;}void onLeftCounterInterrupt(){    leftCounter++;}void onRightCounterInterrupt(){    rightCounter++;}`
« Last Edit: January 11, 2013, 04:39:25 PM by newInRobotics »
"Computer games don't affect kids; I mean if Pac-Man affected us as kids, we'd all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music." - Kristian W

#### jwatte

• Supreme Robot
• Posts: 1,343
##### Re: Algorithm for robot returning to original position
« Reply #2 on: January 11, 2013, 02:51:35 PM »
If you have encoders, and are sure that the wheels don't slip, you can get good accuracy simply through counting forward when you get the encoder ticks.
You can keep a "breadcrumb" of locations that the robot has been at -- say, save a coordinate every second (if you have the RAM.)
Then, you navigate back by targeting each coordinate in turn.
If you know that the track won't have barriers between the position you're at and the home position, you can just skip to the first (0,0) position as your target, to take option 1 to get "home."
The more slippage, rubber deformation, slanting surfaces, and rubbish gets in the way, the less accurate this method will be.

#### newInRobotics

• Supreme Robot
• Posts: 1,015
• N.I.R.
##### Re: Algorithm for robot returning to original position
« Reply #3 on: January 11, 2013, 05:03:29 PM »
If you have encoders, and are sure that the wheels don't slip, you can get good accuracy simply through counting forward when you get the encoder ticks.
You can keep a "breadcrumb" of locations that the robot has been at -- say, save a coordinate every second (if you have the RAM.)
How would You calculate location coordinates from encoder ticks?
"Computer games don't affect kids; I mean if Pac-Man affected us as kids, we'd all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music." - Kristian W

#### jwatte

• Supreme Robot
• Posts: 1,343
##### Re: Algorithm for robot returning to original position
« Reply #4 on: January 12, 2013, 03:40:53 PM »
The ticks give you rate of travel for left and right wheels. A hundred times a second, convert this to movement and rotation of your robot. Keep accumulating position each time you do this. There's a bit of an art to getting enough readings from the encoder between each sample interval, so that you don't get lots of noise/jitter, but don't wait long enough between each interval that the uncertainty of when exactly you were turning within the interval becomes a problem.

If your motor gives you 6000 ticks per gearbox output shaft revolution, as an example, and the diameter of your wheels is 100 millimeters, then you know each wheel travels (100*pi)/6000 millimeters per tick, or about .05236 millimeters.
Now, if you get 62 ticks from one wheel, but 59 ticks from the other, you know that the wheels have been traveling 32.4632mm and 30.8924mm respectively. Now, find two circles whose difference in radius is the wheel base of your robot, and whose arc length at the same degree of travel is these values; find the center circle between these circles, and use that same arc length, and that's the path your robot has traveled.

Repeat!