-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathrobot_car.py
75 lines (59 loc) · 2.53 KB
/
robot_car.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
65
66
67
68
69
70
71
72
73
74
75
from machine import Pin, PWM
"""
Class to represent our robot car
"""
class RobotCar():
def __init__(self, enable_pins, motor_pins, speed):
self.right_motor_enable_pin = PWM(Pin(enable_pins[0]), freq=2000)
self.left_motor_enable_pin = PWM(Pin(enable_pins[1]), freq=2000)
self.right_motor_control_1 = Pin(motor_pins[0], Pin.OUT)
self.right_motor_control_2 = Pin(motor_pins[1], Pin.OUT)
self.left_motor_control_1 = Pin(motor_pins[2], Pin.OUT)
self.left_motor_control_2 = Pin(motor_pins[3], Pin.OUT)
self.speed = speed
def stop(self):
print('Car stopping')
self.right_motor_control_1.value(0)
self.right_motor_control_2.value(0)
self.left_motor_control_1.value(0)
self.left_motor_control_2.value(0)
self.right_motor_enable_pin.duty_u16(0)
self.left_motor_enable_pin.duty_u16(0)
def forward(self):
print('Move forward')
self.right_motor_enable_pin.duty_u16(self.speed)
self.left_motor_enable_pin.duty_u16(self.speed)
self.right_motor_control_1.value(1)
self.right_motor_control_2.value(0)
self.left_motor_control_1.value(1)
self.left_motor_control_2.value(0)
def reverse(self):
print('Move reverse')
self.right_motor_enable_pin.duty_u16(self.speed)
self.left_motor_enable_pin.duty_u16(self.speed)
self.right_motor_control_1.value(0)
self.right_motor_control_2.value(1)
self.left_motor_control_1.value(0)
self.left_motor_control_2.value(1)
def turnLeft(self):
print('Turning Left')
self.right_motor_enable_pin.duty_u16(self.speed)
self.left_motor_enable_pin.duty_u16(self.speed)
self.right_motor_control_1.value(1)
self.right_motor_control_2.value(0)
self.left_motor_control_1.value(0)
self.left_motor_control_2.value(0)
def turnRight(self):
print('Turning Right')
self.right_motor_enable_pin.duty_u16(self.speed)
self.left_motor_enable_pin.duty_u16(self.speed)
self.right_motor_control_1.value(0)
self.right_motor_control_2.value(0)
self.left_motor_control_1.value(1)
self.left_motor_control_2.value(0)
def set_speed(self, new_speed):
self.speed = new_speed
def cleanUp(self):
print('Cleaning up pins')
self.right_motor_enable_pin.deinit()
self.left_motor_enable_pin.deinit()