ev3dev / ev3dev-lang-python

Pure python bindings for ev3dev
MIT License
422 stars 146 forks source link

LEGO Medium Motor/Servo Motor #744

Open monsfortis1940 opened 4 years ago

monsfortis1940 commented 4 years ago

!/usr/bin/env Python3 version:**

hello

For a linefollowing program I like to use a LEGO MediumMotor for streering a robot by (fast !) turning the front wheels to the left or to the right. (parallel steering) Can I "change"/use the MediumMotor in/as a motor with ServoMotor properties (fi zero point correction)

kind regards Mario

WasabiFan commented 4 years ago

If the question is for a way to request that a MediumMotor rotate to a particular angle, you can use on_to_position: https://ev3dev-lang.readthedocs.io/projects/python-ev3dev/en/ev3dev-stretch/motors.html#ev3dev2.motor.Motor.on_to_position

I think that should be sufficiently responsive for this use case. You'd probably want to set it to non-blocking mode so you can continue operating your robot while the motor turns.

monsfortis1940 commented 4 years ago

Hello Kaelin,

Thank you for your promted answer

Attached you will find a Python program with the by you suggested moto_steering It looks like a servo motor but the problem is that (in my case) the motor has no limits (fi for a servo motor +90 degrees and -90 degrees)

Is it possible to implement two limits for the rotation ?

kind regards

Op zo 14 jun. 2020 om 01:16 schreef Kaelin Laundry <notifications@github.com

:

If the question is for a way to request that a MediumMotor rotate to a particular angle, you can use on_to_position: https://ev3dev-lang.readthedocs.io/projects/python-ev3dev/en/ev3dev-stretch/motors.html#ev3dev2.motor.Motor.on_to_position

I think that should be sufficiently responsive for this use case. You'd probably want to set it to non-blocking mode so you can continue operating your robot while the motor turns.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/ev3dev/ev3dev-lang-python/issues/744#issuecomment-643690771, or unsubscribe https://github.com/notifications/unsubscribe-auth/APL5KRDBVWNFMFQUKYNWC4TRWQCFDANCNFSM4N3IYZXQ .

!/usr/bin/env python3

from ev3dev2.motor import MediumMotor, OUTPUT_A, SpeedPercent

from ev3dev2.motor import MoveTank, OUTPUT_B, OUTPUT_C

from ev3dev2.sensor import INPUT_1, INPUT_2, INPUT_4 from ev3dev2.sensor.lego import ColorSensor

from ev3dev2.sensor.lego import TouchSensor

from time import sleep

from sys import stderr

m = MediumMotor(OUTPUT_A)

ts = TouchSensor(INPUT_4)

col_left = ColorSensor(INPUT_1) col_right = ColorSensor(INPUT_2)

tank_pair = MoveTank(OUTPUT_B, OUTPUT_C)

speed = -25

def limit(value): return min(max(value,-50),+50)

while True:

tank_pair.on(left_speed = speed, right_speed = speed)

error = limit((col_left.reflected_light_intensity - col_right.reflected_light_intensity))

m.on_for_rotations(SpeedPercent(100),-error/4000)
print(str(float(error/4000)),file=stderr)    

"""    
m.on_for_rotations(SpeedPercent(30), 0.05)
sleep(1)
m.on_for_rotations(SpeedPercent(30), 0.05)  
sleep(1)
m.on_for_rotations(SpeedPercent(30), 0.15)
sleep(1)
m.on_for_rotations(SpeedPercent(30), 0.15)
sleep(1)

m.on_for_rotations(SpeedPercent(30), -0.1)  
sleep(1)
m.on_for_rotations(SpeedPercent(30), -0.1)
sleep(1)
m.on_for_rotations(SpeedPercent(30), -0.1) 
sleep(1)
m.on_for_rotations(SpeedPercent(30), -0.1)
sleep(1)

tank_pair.on_for_seconds(left_speed = speed, right_speed = speed, seconds = 2)
tank_pair.off()
sleep(2)
tank_pair.on_for_seconds(left_speed = -speed, right_speed = -speed, seconds = 2)
tank_pair.off()
sleep(2)

"""
WasabiFan commented 4 years ago

It looks like you're using on_for_rotations, which moves it the specified amount relative to where it currently is rather than relative to some consistent reference point. You're going to want on_to_position, which moves the motor to a particular position relative to a known starting position when the EV3 turned on.

Note that you might not want to use the position the motor was in at the time the EV3 booted as the "zero point"; I recommend you have some known position the motor can be moved into, and then set m.position = 0 before starting your main code. That will tell the motor to start at the position it is currently at when being commanded with on_to_position. Again, remember that this position is forgotten every time the EV3 turns off.

monsfortis1940 commented 4 years ago

hello

That is exactly what I mean: by using "on_to _position' there is no fixed reference point . Perhaps there is no solution with the LEGO motors

kind regards

Op ma 15 jun. 2020 om 02:50 schreef Kaelin Laundry <notifications@github.com

:

It looks like you're using on_for_rotations, which moves it the specified amount relative to where it currently is rather than relative to some consistent reference point. You're going to want on_to_position, which moves the motor to a particular position relative to a known starting position when the EV3 turned on.

Note that you might not want to use the position the motor was in at the time the EV3 booted as the "zero point"; I recommend you have some known position the motor can be moved into, and then set m.position = 0 before starting your main code. That will tell the motor to start at the position it is currently at when being commanded with on_to_position. Again, remember that this position is forgotten every time the EV3 turns off.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/ev3dev/ev3dev-lang-python/issues/744#issuecomment-643849263, or unsubscribe https://github.com/notifications/unsubscribe-auth/APL5KRE6RRW4P2OLXHLDHDTRWVV47ANCNFSM4N3IYZXQ .

WasabiFan commented 4 years ago

Yeah, unfortunately the medium/large motors don't have a way to "remember" a position across reboots; they only measure deltas. That being said, the typical solution is to have a hard-stop at the extreme of your motor's travel (e.g. at 45° to the side) and then have your code start by turning all the way until the motor stops moving and save that position as 45°. So every time your code starts, it would calibrate itself. If that isn't an option, you'll probably need a different motor.

monsfortis1940 commented 4 years ago

Thank you very much to pay attention to my problem . Unfortunately there is no solution

Op di 16 jun. 2020 om 12:25 schreef Kaelin Laundry <notifications@github.com

:

Yeah, unfortunately the medium/large motors don't have a way to "remember" a position across reboots; they only measure deltas. That being said, the typical solution is to have a hard-stop at the extreme of your motor's travel (e.g. at 45° to the side) and then have your code start by turning all the way until the motor stops moving and save that position as 45°. So every time your code starts, it would calibrate itself. If that isn't an option, you'll probably need a different motor.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/ev3dev/ev3dev-lang-python/issues/744#issuecomment-644677413, or unsubscribe https://github.com/notifications/unsubscribe-auth/APL5KREPKVTLK3T76WWXMBTRW5CB3ANCNFSM4N3IYZXQ .