Coverage for src/main/python/motor_bidirectional.py : 91%
Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1import machine
2from motor import Motor
5class MotorBidirectional(Motor):
7 """ Motor DC rotates in one or the other direction with current, it can be inverted,
8 it's basically mandatory to use a H-Bridge, a pin (0,04 A) doesn't have enough Ampere to move a motor"""
10 def __init__(self, pin1_num, pin2_num, direction=1, running=False):
11 """ constructor.
12 :param pin1_num: number of pin that will give power or be ground
13 :param pin2_num: number of pin that will give power or be ground
14 :param direction: which pin should give current, can be 1 or 2
15 :param running: the initial condition of the motor
16 """
17 self.pin1_num = pin1_num
18 self.pin2_num = pin2_num
19 self.direction = direction
20 self.running = running
21 if running:
22 self.on()
24 def __str__(self):
25 """prints the object."""
26 return "Motor bidirectional currently is running: {}, direction: {}".format(self.running, self.direction)
28 def on_pin(self, pin_num):
29 """Turns on given pin and off the other."""
30 # for safety off-everything
31 self.off()
32 # turning on only one pin
33 self.running = True
34 if pin_num == self.pin1_num:
35 self.direction = 1
36 # pin 2 input
37 pin2 = machine.Pin(self.pin2_num, machine.Pin.OUT)
38 pin2.off()
39 # on pin1
40 pin1 = machine.Pin(self.pin1_num, machine.Pin.OUT)
41 pin1.on()
42 if pin_num == self.pin2_num:
43 self.direction = 2
44 # pin 1 input
45 pin1 = machine.Pin(self.pin1_num, machine.Pin.OUT)
46 pin1.off()
47 # on pin2
48 pin2 = machine.Pin(self.pin2_num, machine.Pin.OUT)
49 pin2.on()
51 def on_direction(self, direct):
52 """starts motor in direction specified."""
53 if direct == 1:
54 self.on_pin(self.pin1_num)
55 elif direct == 2:
56 self.on_pin(self.pin2_num)
58 def on(self):
59 """starts motor, in direction memorized."""
60 self.on_direction(self.direction)
62 def on_direction_opposite(self):
63 """starts motor in the opposite direction."""
64 if self.direction == 1:
65 self.on_direction(2)
66 elif self.direction == 2:
67 self.on_direction(1)
69 def off(self):
70 """stops motor."""
71 self.running = False
72 pin1 = machine.Pin(self.pin1_num, machine.Pin.OUT)
73 pin1.off()
74 pin2 = machine.Pin(self.pin2_num, machine.Pin.OUT)
75 pin2.off()
77 def get_direction(self):
78 """the direction selected."""
79 return self.direction