1- """An interface to the cyctron motor board. A gpio pin is used for each motor
1+ """
2+ An interface to the cyctron motor board. A GPIO pin is used for each motor
23to give direction and has a PWM signal at 100Hz giving infomation about voltage
34to apply
45"""
5- import wiringpi as wp
6+
7+ import RPi .GPIO as GPIO
68from .greengiant import clamp
79
810_MAX_OUTPUT_VOLTAGE = 12
911
10- _PWM_PIN_1 = 26
11- _PWM_PIN_2 = 23
12- _DIR_PIN_1 = 25
13- _DIR_PIN_2 = 5
14-
15- _WP_OUT = 1
16- _WP_PWM = 2
17-
18- # Wiring pi's PWM has range 0-1024 but we want to present a range of 0-100
19- _WP_PWM_MAX = 1024
20- _RC_PWM_MAX = 100
21-
22-
23- def wp_to_rc_pwm (wp_pwm ):
24- """Convert from wiring pi's numbering to percentages"""
25- return (wp_pwm * _RC_PWM_MAX )/ _WP_PWM_MAX
26-
27-
28- def rc_to_wp_pwm (rc_pwm ):
29- """Convert from percenatges to wiring pi's numbering"""
30- return int ((rc_pwm * _WP_PWM_MAX )/ _RC_PWM_MAX )
12+ _PWM_PIN_1 = 12
13+ _PWM_PIN_2 = 13
14+ _DIR_PIN_1 = 26
15+ _DIR_PIN_2 = 24
3116
32-
33- class CytronBoard ():
17+ class CytronBoard :
3418 def __init__ (self , max_motor_voltage ):
35- """The interface to the CytronBoard
19+ """
20+ The interface to the CytronBoard
3621 max_motor_voltage - The motors will be scaled so that this is the maxium
37- average voltage the Cyctron will output
22+ average voltage the Cytron will output
3823 """
39- # Set up Wiring Pi following the wiring pi numbering scheme
40- if wp .wiringPiSetup () != 0 :
41- raise RuntimeError ("Failed to init Wiring Pi" )
42-
4324 if not (0 <= max_motor_voltage <= 12 ):
4425 raise ValueError ("max_motor_voltage must satisfy 0 <= "
4526 "max_motor_voltage <= 12 but instead is "
@@ -50,49 +31,50 @@ def __init__(self, max_motor_voltage):
5031 self .power_scaling_factor = (
5132 max_motor_voltage / _MAX_OUTPUT_VOLTAGE ) ** 2
5233
53- self ._percentages = [0 , 0 ]
54- self ._dir = [_DIR_PIN_1 , _DIR_PIN_2 ]
55- self ._pwm_pins = [_PWM_PIN_1 , _PWM_PIN_2 ]
56-
57- wp .pinMode (_DIR_PIN_1 , _WP_OUT )
58- wp .pinMode (_DIR_PIN_2 , _WP_OUT )
59- wp .pinMode (_PWM_PIN_1 , _WP_PWM )
60- wp .pinMode (_PWM_PIN_2 , _WP_PWM )
61-
62- wp .pwmSetClock (3000 )
63- for pin in self ._pwm_pins :
64- wp .pwmWrite (pin , 0 )
34+ self ._dir = [
35+ (GPIO .LOW , _DIR_PIN_1 ),
36+ (GPIO .LOW , _DIR_PIN_2 ),
37+ ]
38+ self ._pwm = [
39+ (0 , GPIO .PWM (_PWM_PIN_1 , 100 )),
40+ (0 , GPIO .PWM (_PWM_PIN_2 , 100 )),
41+ ]
42+
43+ GPIO .setmode (GPIO .BCM )
44+ GPIO .setup (_DIR_PIN_1 , GPIO .OUT )
45+ GPIO .setup (_DIR_PIN_2 , GPIO .OUT )
46+ GPIO .setup (_PWM_PIN_1 , GPIO .OUT )
47+ GPIO .setup (_PWM_PIN_2 , GPIO .OUT )
6548
6649 def __getitem__ (self , index ):
67- """Returns the current PWM value in RC units. Adds a sign to represent """
68- if index not in (1 , 2 ):
50+ """Returns current motor PWM value as a percentage """
51+ if index not in (0 , 1 ):
6952 raise IndexError (
70- f"motor index must be in (1,2 ) but instead got { index } " )
53+ f"Motor index must be in (0,1 ) but instead got { index } " )
7154
72- index -= 1
73- return self ._percentages [index ]
55+ return self ._pwm [index ][0 ]
7456
7557 def __setitem__ (self , index , percent ):
76- """Clamps input value, converts from percentage to wiring pi format and
77- sets a PWM format"""
78- if index not in (1 , 2 ):
58+ """Set current motor PWM percentage value"""
59+ if index not in (0 , 1 ):
7960 raise IndexError (
80- f"motor index must be in (1,2 ) but instead got { index } " )
61+ f"Motor index must be in (0,1 ) but instead got { index } " )
8162
82- index -= 1
83- percent = clamp (percent , - 100 , 100 )
84- self ._percentages [index ] = percent
63+ if percent < 0 :
64+ self ._dir [index ][0 ] = GPIO .LOW
65+ else :
66+ self ._dir [index ][0 ] = GPIO .HIGH
8567
86- direction = (percent < 0 )
87- wp .digitalWrite (self ._dir [index ], direction )
68+ GPIO .output (self ._dir [index ][1 ], self ._dir [index ][0 ])
69+
70+ percent = clamp (percent , - 100 , 100 )
71+ self ._pwm [index ][0 ] = percent
8872
89- # Scale such that 50% with a motor limit of 6V is really 3V
90- scaled_value = abs (percent ) * self .power_scaling_factor
91- wp_value = rc_to_wp_pwm (scaled_value )
92- wp .pwmWrite (self ._pwm_pins [index ], wp_value )
73+ percent = abs (percent ) * self .power_scaling_factor
74+ self ._pwm [index ][1 ].start (percent )
9375
9476 def stop (self ):
9577 """Turns motors off"""
96- for pwm_pin , percentage in zip ( self ._pwm_pins , self . _percentages ):
97- wp . pwmWrite ( pwm_pin , 0 )
98- percentage = 0
78+ for i in range ( len ( self ._pwm ) ):
79+ self . _pwm [ i ][ 0 ] = 0
80+ self . _pwm [ i ][ 1 ]. start ( 0 )
0 commit comments