-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathThreeStateSteering.py
63 lines (46 loc) · 1.89 KB
/
ThreeStateSteering.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import logging
import time
import pigpio
import math
from PWMGenerator import AbstractPWMGenerator
# Cheap RC cars have 3-state steering, which requires 2 GPIO pins to control
#
# | Power Pin | Direction Pin |
# -------|------------|----------------|
# Left: | 1 | 0 |
# Right: | 1 | 1 |
# Center:| 0 | 0 |
#
# The center position is unpowered. Handled by a spring mechanism.
#
class ThreeStateSteering(AbstractPWMGenerator):
def __init__(self, name, gpio_power_pin, gpio_direction_pin, android_socket):
self.logger = logging.getLogger("Steering_%s" % gpio_power_pin)
self.STEP = 20000 # uS
self.SLEEP = 0.02 # seconds
self.gpio_direction_pin = gpio_direction_pin
AbstractPWMGenerator.__init__(self, name, gpio_power_pin, 0, self.STEP, android_socket)
self.center_steering()
def center_steering(self):
self.current_pulse_width = 0
self.set_duty_cycle(0)
self.report_device_state()
def stop_device(self):
self.center_steering()
def set_duty_cycle(self, pulse_width_us):
pigpio.set_PWM_dutycycle(self.pin, math.fabs(pulse_width_us))
self.logger.info("Position = %s" % pulse_width_us)
self.set_direction_pin()
# Selects spin direction for the motor. Negative pulse width represents Reverse.
def set_direction_pin(self):
if ( self.current_pulse_width >= 0):
pigpio.write(self.gpio_direction_pin, 0)
else:
pigpio.write(self.gpio_direction_pin, 1)
self.logger.info("Direction pin = %s" % pigpio.read(self.gpio_direction_pin))
def turn_right(self):
self.increase_duty_cycle()
self.report_device_state()
def turn_left(self):
self.decrease_duty_cycle()
self.report_device_state()