# How to remember initial position of a robot and return to it

I have an EV3 robot with motors, like a car, that can move forward/backward and left/right and have some sensors attached. I want it to remember its initial position by remembering all the movements of motors when robot starts moving, then after some movements, that can be random in time, distance and direction, I want to return it back home.

How can I do this? What is the principle of returning "home" knowing there are no GPS or other helpers?

You have two options.

You can use an accelerometer. Lego sells one that can be found here. There are also aftermarket ones at mindsensors.com. This allows you to independently measure the acceleration of the robot. You have to then integrate this information twice to get positional change. The way to do this is to put it in your control loop and sum up 0.5*dt*dt*a where a is the accelerometer reading (there will be one for each axis of motion) and dt is the time between samples. This nice thing about this is it gives you absolute displacement. That is it accounts for motions that cancel out so you can set a straight line path back.

You can record each displacement into a variable for x and y directions using the servo readings from the motors - remember to reset the encoder value from the servos after each reading. This is harder because you have to account for the geometry of the wheels and keep track of the robot heading - lego also sells a compass sensor if you don't want to deal with finding your heading, but you'll still have to deal with non-spinning turns. It's also prone to accumulation of errors. (the accelerometer is too but not as bad I think.)

Your admonition of 'no other helpers' could be interpreted to mean you don't want the first solution in which case you are left with using the encoders and figuring out the differential motion of your chassis design (which may be non trivial).

## Get the initial position of an EV3 Motor and return to it.

Setup: I installed Debian Linux on the Mindstorms EV3 brick and I'm using Python 2 to use the ev3.lego package to use the method: `run_position_limited(... , ...)` to spin the motor to a certain angle position.

Python 2 code: which gets the initial position of the Motor on program start:

``````from ev3.lego import Motor
c = Motor(port = Motor.PORT.C)

c.run_position_limited(
position_sp = int(initialMotorLocation) + 150,
speed_sp = 150)
``````

What it does

First we import the Motor library from ev3.lego. Then we instantiate a new connection to the Motor on Port C. Then we read the current position of the angle of the motor on start. Store this value permanently so every time we want to move ahead by 150, we add that value to the location it was at start.

Result:

If you run this python program many times, the motor will rotate forward by exactly 150 each time, rather than the default behavior: "rotating to exactly position 150", which only holds true until the EV3 brick is rebooted."

What did we learn:

We need to learn to read and comprehend the source code ev3.lego documention, Defined here: https://github.com/topikachu/python-ev3

If you scroll down that page you see this command:

``````from ev3.lego import MediumMotor
d = MediumMotor()
d.reset()
``````

So the method called `reset` is an additional way to cause the motor to reset it's internal tachometer to 0 at the current location.

Full Example:

``````from ev3.lego import Motor
c = Motor(port = Motor.PORT.C)
c.reset()  #Set the internal tachometer back to 0
c.run_position_limited(position_sp = 150, speed_sp = 150)
``````

You got told that the `reset()` and `read_value` methods will solve your problem. This is not an example of you finding things out for yourself.

Go to the source code: https://github.com/topikachu/python-ev3

Under `python-ev3/ev3/ev3dev.py` you'll see the source code that does the work I've explained above. Notice the properties. You see the integer value called `position` that you were querying above.

Being able to recompile and understand how the library is working from source is the better part of the education the mindstorms EV3 brick aims to teach you. Dig in.

You should look up 'Dead Reckoning'. It is a method where you always know (or guess) your current position by tracking all your previous moves. Knowing your current position, you can then retrace your steps backward or reach any position back calculating a straight line to it (hoping that there's nothing in the way).

Here's the Wikipedia on the subject: Dead reckoning