-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstepmotor.py
More file actions
103 lines (84 loc) · 2.69 KB
/
Copy pathstepmotor.py
File metadata and controls
103 lines (84 loc) · 2.69 KB
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
from machine import Pin
import time
import uasyncio as asyncio
class Model_28BYJ_48():
CCW_8_STEP_SEQ = -1
CCW_4_STEP_SEQ = -2
CW_8_STEP_SEQ = 1
CW_4_STEP_SEQ = 2
PIN_MAP = {"D1":5,"D2":4,"D3":0,"D4":2,"D5":14,"D6":12,"D7":13,"D8":15,"SCL":5,"SDA":4,"SCK":14,"MISO":12,"MOSI":13,"SS":15}
def __init__(self,pins=[]):
self.pins = []
for i in range(0,4):
self.pins.append(Pin(self.PIN_MAP[pins[i]],Pin.OUT))
self.seq = [[1,0,0,1],
[1,0,0,0],
[1,1,0,0],
[0,1,0,0],
[0,1,1,0],
[0,0,1,0],
[0,0,1,1],
[0,0,0,1]]
self.step_formular = {-1:(8,4076,5.625),
-2:(4,2038,11.25),
1:(8,4096,5.625),
2:(4,2038,11.25)}
self.step_count = len(self.seq)
self.dir_err = "Invalid direction value"
self.wt_err="waiting time must be an integer between 2 and 20.(Lower is faster)"
async def step(self,rnd=1,direction=2,waiting_time=2):
assert (direction in [-1,-2,1,2]),self.dir_err
assert (2 <= waiting_time <= 20) ,self.wt_err
info_touple = self.step_formular[direction]
max_rev = info_touple[1]*rnd
await self._step(max_rev,direction,waiting_time)
async def angular_step(self,angle=90,direction=2,waiting_time=2,bi_direction=False):
assert (direction in [-1,-2,1,2]),self.dir_err
assert (2 <= waiting_time <= 20) ,self.wt_err
info_touple = self.step_formular[direction]
rnd,angle = self._div(angle,360)
max_rev = info_touple[1]*rnd + angle * info_touple[1] // 360
self._step(max_rev,direction,waiting_time)
if bi_direction :
await self._step(max_rev,-direction,waiting_time)
async def _step(self,max_rev,direction,wait_time):
rev = 0
step_counter = 0
while rev < max_rev:
for pin in range(0,4):
step_val = self.seq[step_counter][pin]
self.pins[pin].value(step_val)
step_counter += direction
# If we reach the end of the sequence
# start again
if (step_counter >= self.step_count):
step_counter = 0
if (step_counter < 0):
step_counter = self.step_count + direction
# Wait before moving on
#time.sleep_ms(wait_time)
await asyncio.sleep_ms(wait_time)
rev += 1
def _div(self,v1,v2):
res=(0,0)
if v2 >= v1 :
return (0,v1)
vt = v1 - v2
m = 1
while vt > v2 :
vt = vt - v2
m +=1
return (m,vt)
async def step_a():
st_mot = Model_28BYJ_48(["D1","D2","D3","D4"])
await st_mot.angular_step(angle=360,direction=2,waiting_time=2)
asyncio.sleep_ms(100)
async def step_b():
st_mot = Model_28BYJ_48(["D5","D6","D7","D8"])
await st_mot.angular_step(angle=360,direction=-2,waiting_time=2)
asyncio.sleep_ms(100)
loop = asyncio.get_event_loop()
#loop.create_task(step_a())
#loop.create_task(step_b())
loop.run_until_complete(step_a())
loop.close()