Skip to content

Commit c401c58

Browse files
author
sam
committed
Working run_direct of the motor class
1 parent 8f647a6 commit c401c58

3 files changed

Lines changed: 44 additions & 19 deletions

File tree

source/Main.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#!/usr/bin/env python3
22

33
from ev3dev2._platform.ev3 import INPUT_1, INPUT_4, INPUT_3, INPUT_2
4-
from ev3dev2.led import Led
54
from ev3dev2.motor import MoveTank, OUTPUT_A, OUTPUT_D, SpeedPercent
65
from ev3dev2.sensor.lego import ColorSensor
76
from ev3dev2.sensor.lego import TouchSensor
@@ -72,7 +71,5 @@ def check():
7271

7372
tank_drive = MoveTank(OUTPUT_A, OUTPUT_D)
7473

75-
led = Led()
76-
7774
drive()
7875
check()

source/ev3dev2/motor.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -515,6 +515,7 @@ def duty_cycle_sp(self):
515515
@duty_cycle_sp.setter
516516
def duty_cycle_sp(self, value):
517517
self._duty_cycle_sp = value
518+
self.connector.set_duty_cycle(value)
518519

519520

520521
@property
@@ -618,7 +619,6 @@ def position_sp(self):
618619

619620
@position_sp.setter
620621
def position_sp(self, value):
621-
622622
self._position_sp = value
623623
self.connector.set_distance(value)
624624

@@ -864,6 +864,12 @@ def run_direct(self):
864864

865865
self.command = self.COMMAND_RUN_DIRECT
866866

867+
self.connector.set_stop_action(self.STOP_ACTION_BRAKE)
868+
self.connector.stop()
869+
870+
run_time = self.connector.run_direct()
871+
self.running_until = time.time() + run_time
872+
867873

868874
def stop(self):
869875
"""

source/ev3dev2/simulator/connector/MotorConnector.py

Lines changed: 37 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ class MotorConnector:
1515
def __init__(self, address: str):
1616
self.address = address
1717

18+
self.duty_cycle = None
1819
self.speed = None
1920
self.distance = None
2021
self.time = None
@@ -23,13 +24,14 @@ def __init__(self, address: str):
2324
self.command_creator = get_motor_command_creator()
2425

2526

26-
def set_time(self, time: int):
27+
def set_duty_cycle(self, duty_cycle: int):
2728
"""
28-
Set the time to run of the motor belonging to the given address.
29-
:param time: in milliseconds.
29+
Set the percentage of power of the motor belonging to the given address.
30+
-100 being fully backwards and 100 fully forwards.
31+
:param duty_cycle: in percentage.
3032
"""
3133

32-
self.time = time
34+
self.duty_cycle = duty_cycle
3335

3436

3537
def set_speed(self, speed: float):
@@ -50,6 +52,15 @@ def set_distance(self, distance: float):
5052
self.distance = distance
5153

5254

55+
def set_time(self, time: int):
56+
"""
57+
Set the time to run of the motor belonging to the given address.
58+
:param time: in milliseconds.
59+
"""
60+
61+
self.time = time
62+
63+
5364
def set_stop_action(self, action: str):
5465
"""
5566
Set the speed to run at of the motor belonging to the given address.
@@ -67,8 +78,8 @@ def run_forever(self) -> float:
6778
In the real world this would be infinity.
6879
"""
6980

70-
distance = self.speed * FOREVER_MOCK_SECONDS
71-
return self._run(self.speed, distance)
81+
self.distance = self.speed * FOREVER_MOCK_SECONDS
82+
return self._run()
7283

7384

7485
def run_to_rel_pos(self) -> float:
@@ -78,7 +89,7 @@ def run_to_rel_pos(self) -> float:
7889
the given run operation will take.
7990
"""
8091

81-
return self._run(self.speed, self.distance)
92+
return self._run()
8293

8394

8495
def run_timed(self) -> float:
@@ -88,24 +99,35 @@ def run_timed(self) -> float:
8899
the given run operation will take.
89100
"""
90101

91-
distance = self.speed * (self.time / 1000)
92-
return self._run(self.speed, distance)
102+
self.distance = self.speed * (self.time / 1000)
103+
return self._run()
93104

94105

95-
def _run(self, speed: float, distance: float) -> float:
106+
def run_direct(self) -> float:
107+
"""
108+
Run the motor for at the given speed provided by the duty_cycle.
109+
:return a floating point value representing the number of seconds
110+
the given run operation will take.
111+
"""
112+
113+
self.speed = self.duty_cycle / 100 * 1050
114+
self.distance = self.speed * FOREVER_MOCK_SECONDS
115+
116+
return self._run()
117+
118+
119+
def _run(self) -> float:
96120
"""
97121
Run the motor at a speed for a distance.
98-
:param speed: in degrees per second.
99-
:param distance: in degrees.
100122
:return a floating point value representing the number of seconds
101123
the given run operation will take.
102124
"""
103125

104-
if speed == 0 or distance == 0:
126+
if self.speed == 0 or self.distance == 0:
105127
return 0
106128

107-
return self.command_creator.create_drive_command(speed,
108-
distance,
129+
return self.command_creator.create_drive_command(self.speed,
130+
self.distance,
109131
self.stop_action,
110132
self.address)
111133

0 commit comments

Comments
 (0)