1
1
from machine import Pin , PWM
2
- class Motor :
2
+
3
+ class SinglePWMMotor :
3
4
4
5
"""
5
6
A wrapper class handling direction and power sets for DC motors on the XRP robots
7
+
8
+ This version is used for the beta version of the XRP, which uses the rp2040 processor
6
9
"""
7
10
8
- def __init__ (self , in1_direction_pin : int , in2_speed_pin : int , flip_dir :bool = False , pwmMode :bool = False ):
9
- self ._pwmMode = pwmMode
11
+ def __init__ (self , in1_direction_pin : int , in2_speed_pin : int , flip_dir :bool = False ):
10
12
self .flip_dir = flip_dir
11
13
self ._MAX_PWM = 65534 # Motor holds when actually at full power
12
14
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 )
15
+ self ._in1DirPin = Pin (in1_direction_pin , Pin .OUT )
16
+ self ._in2SpeedPin = PWM (Pin (in2_speed_pin , Pin .OUT ))
17
+ self ._in2SpeedPin .freq (50 )
22
18
23
19
def set_effort (self , effort : float ):
24
20
"""
@@ -28,28 +24,73 @@ def set_effort(self, effort: float):
28
24
:type effort: float
29
25
"""
30
26
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 ))
27
+ if effort < 0 :
28
+ # Change direction if negative power
29
+ effort *= - 1
30
+ self ._set_direction (1 )
39
31
else :
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
-
32
+ self ._set_direction (0 )
33
+ # Cap power to [0,1]
34
+ effort = max (0 ,min (effort ,1 ))
35
+ self ._in2SpeedPin .duty_u16 (int (effort * self ._MAX_PWM ))
50
36
51
37
def _set_direction (self , direction : int ):
52
38
if self .flip_dir :
53
39
self ._in1DirPin .value (not direction )
54
40
else :
55
41
self ._in1DirPin .value (direction )
42
+
43
+ def brake (self ):
44
+ # Motor holds with the real max duty cycle (65535)
45
+ self ._in2SpeedPin .duty_u16 (self ._MAX_PWM + 1 )
46
+
47
+ def coast (self ):
48
+ raise NotImplementedError ("Motor.coast is not implemented for not implemented for the XRP RP2040" )
49
+
50
+ class DualPWMMotor :
51
+ """
52
+ A wrapper class handling direction and power sets for DC motors on the XRP robots
53
+
54
+ This version of the Motor class is used
55
+ """
56
+
57
+ def __init__ (self , in1_pwm_forward : int , in2_pwm_backward : int , flip_dir :bool = False ):
58
+ self .flip_dir = flip_dir
59
+ self ._MAX_PWM = 65535 # Motor holds when actually at full power
60
+
61
+ self ._in1ForwardPin = PWM (Pin (in1_pwm_forward , Pin .OUT ))
62
+ self ._in2BackwardPin = PWM (Pin (in2_pwm_backward , Pin .OUT ))
63
+ self ._in1ForwardPin .freq (50 )
64
+ self ._in2BackwardPin .freq (50 )
65
+
66
+ def set_effort (self , effort : float ):
67
+ """
68
+ Sets the effort value of the motor (corresponds to power)
69
+
70
+ :param effort: The effort to set the motor to, between -1 and 1
71
+ :type effort: float
72
+ """
73
+
74
+ in1Pwm = (effort < 0 ) ^ (self .flip_dir )
75
+ if in1Pwm :
76
+ self ._in1ForwardPin .duty_u16 (int (abs (effort )* self ._MAX_PWM ))
77
+ self ._in2BackwardPin .duty_u16 (int (0 ))
78
+ else :
79
+ self ._in1ForwardPin .duty_u16 (int (0 ))
80
+ self ._in2BackwardPin .duty_u16 (int (abs (effort )* self ._MAX_PWM ))
81
+
82
+ def brake (self ):
83
+ """
84
+ Powers the motor in both directions at the same time, enabling it to hold position
85
+ """
86
+ self ._in1ForwardPin .duty_u16 (int (self ._MAX_PWM ))
87
+ self ._in2BackwardPin .duty_u16 (int (self ._MAX_PWM ))
88
+
89
+ def coast (self ):
90
+ """
91
+ Disables the motor in both directions at the same time, enabling it to spin freely
92
+ """
93
+ self ._in1ForwardPin .duty_u16 (int (0 ))
94
+ self ._in2BackwardPin .duty_u16 (int (0 ))
95
+
96
+
0 commit comments