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)
initialMotorLocation= c.read_value("position")
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)
How to get to these answers on your own:
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.