@@ -5,13 +5,21 @@ class Motor:
55 A wrapper class handling direction and power sets for DC motors on the XRP robots
66 """
77
8- def __init__ (self , direction_pin : int , speed_pin : int , flip_dir :bool = False ):
9- self ._dirPin = Pin (direction_pin , Pin .OUT )
10- self ._speedPin = PWM (Pin (speed_pin , Pin .OUT ))
11- self ._speedPin .freq (50 )
8+ def __init__ (self , in1_direction_pin : int , in2_speed_pin : int , flip_dir :bool = False , pwmMode :bool = False ):
9+ self ._pwmMode = pwmMode
1210 self .flip_dir = flip_dir
1311 self ._MAX_PWM = 65534 # Motor holds when actually at full power
1412
13+ if self ._pwmMode :
14+ self ._in1DirPin = PWM (Pin (in1_direction_pin , Pin .OUT ))
15+ self ._in2SpeedPin = PWM (Pin (in2_speed_pin , Pin .OUT ))
16+ self ._in1DirPin .freq (50 )
17+ self ._in2SpeedPin .freq (50 )
18+ else :
19+ self ._in1DirPin = Pin (in1_direction_pin , Pin .OUT )
20+ self ._in2SpeedPin = PWM (Pin (in2_speed_pin , Pin .OUT ))
21+ self ._in2SpeedPin .freq (50 )
22+
1523 def set_effort (self , effort : float ):
1624 """
1725 Sets the effort value of the motor (corresponds to power)
@@ -20,18 +28,28 @@ def set_effort(self, effort: float):
2028 :type effort: float
2129 """
2230
23- if effort < 0 :
24- # Change direction if negative power
25- effort *= - 1
26- self ._set_direction (1 )
31+ if self ._pwmMode :
32+ in1Pwm = (effort < 0 ) ^ (self .flip_dir )
33+ if in1Pwm :
34+ self ._in1DirPin .duty_u16 (int (abs (effort )* self ._MAX_PWM ))
35+ self ._in2SpeedPin .duty_u16 (int (0 ))
36+ else :
37+ self ._in1DirPin .duty_u16 (int (0 ))
38+ self ._in2SpeedPin .duty_u16 (int (abs (effort )* self ._MAX_PWM ))
2739 else :
28- self ._set_direction (0 )
29- # Cap power to [0,1]
30- effort = max (0 ,min (effort ,1 ))
31- self ._speedPin .duty_u16 (int (effort * self ._MAX_PWM ))
40+ if effort < 0 :
41+ # Change direction if negative power
42+ effort *= - 1
43+ self ._set_direction (1 )
44+ else :
45+ self ._set_direction (0 )
46+ # Cap power to [0,1]
47+ effort = max (0 ,min (effort ,1 ))
48+ self ._in2SpeedPin .duty_u16 (int (effort * self ._MAX_PWM ))
49+
3250
3351 def _set_direction (self , direction : int ):
3452 if self .flip_dir :
35- self ._dirPin .value (not direction )
53+ self ._in1DirPin .value (not direction )
3654 else :
37- self ._dirPin .value (direction )
55+ self ._in1DirPin .value (direction )
0 commit comments