@@ -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