forked from 0matjaz0/tankec
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmotorcki.py
64 lines (50 loc) · 1.78 KB
/
motorcki.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
64
import RPi.GPIO as GPIO
import time
class DCMotor:
def __init__(self, pwm_pin, hbridge_sw1_pin, hbridge_sw2_pin):
self.pwm_pin = pwm_pin
self.s1_pin = hbridge_sw1_pin
self.s2_pin = hbridge_sw2_pin
self.PWM_FREQ = 100
self.pwm_duty = 0
GPIO.setmode(GPIO.BCM) # choose SOC numbering scheme
GPIO.setup(self.s1_pin, GPIO.OUT)
GPIO.setup(self.s2_pin, GPIO.OUT)
GPIO.setup(self.pwm_pin, GPIO.OUT)
self.pwm = GPIO.PWM(self.pwm_pin, self.PWM_FREQ)
def start(self):
self.pwm.start(self.pwm_duty)
return self
def stop(self):
self.pwm.stop()
return self
def set_speed(self, speed):
""" set speed to integer in [-100:100], negative means backward"""
self.pwm_duty = speed
if speed > 0: # go forward
self.s1_state = True
self.s2_state = False
else # go backward
self.s1_state = False
self.s2_state = True
self.pwm.ChangeDutyCycle(self.pwm_duty)
return self
def main():
motor_left = DCMotor(pwm_pin=22, hbridge_sw1_pin=25, hbridge_sw2_pin=8)
motor_right = DCMotor(pwm_pin=10, hbridge_sw1_pin=23, hbridge_sw2_pin=24)
motor_left.start()
motor_left.set_speed(20)
time.sleep(2) # let it run 2 seconds
motor_left.stop()
time.sleep(2) # quiet 2 seconds
motor_left.start() # see if it remembered pwm duty cycle from last run
time.sleep(2) # let it run 2 seconds
motor_left.stop()
GPIO.cleanup()
# todo: implement direction setting
# GPIO.output(25, True)
# GPIO.output(8, False)
# GPIO.output(23, True)
# GPIO.output(24, False)
if __name__ == "__main__":
main()