Real2Virtual202111

main_controller_01.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
 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
-
!
 
 
 
 
 
 
 
 
 
 
 
-
!
 
 
 
 
 
 
 
 
 
 
 
 
-
!
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
-
!
 
 
 
 
 
 
 
 
 
-
!
 
 
 
 
 
 
 
 
 
 
 
-
!
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
-
|
|
|
|
|
!
 
 
 
 
-
|
!
-
!
-
!
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
-
!
from m5stack import *
from m5ui import *
from uiflow import *
import module
import time
 
setScreenColor(0x565600)
 
a_true = None
b_true = None
c_true = None
r_dir = None
in_chars = None
line = None
 
stepmotor = module.get(module.STEPMOTORDRIVER)
 
label0 = M5TextBox(169, 47, "LIMIT SW1:", lcd.FONT_Default, 0xFFFFFF, rotate=0)
label1 = M5TextBox(15, 68, "LIMIT SW2:", lcd.FONT_Default, 0xFFFFFF, rotate=0)
label2 = M5TextBox(169, 68, "LIMIT SW3:", lcd.FONT_Default, 0xFFFFFF, rotate=0)
label3 = M5TextBox(15, 89, "LIMIT SW4:", lcd.FONT_Default, 0xFFFFFF, rotate=0)
label4 = M5TextBox(169, 89, "ALL SW:", lcd.FONT_Default, 0xFFFFFF, rotate=0)
label5 = M5TextBox(15, 24, "FAULT X:", lcd.FONT_Default, 0xFFFFFF, rotate=0)
label8 = M5TextBox(15, 119, "FW VER:", lcd.FONT_Default, 0xFFFFFF, rotate=0)
label9 = M5TextBox(169, 119, "I2C ADD:", lcd.FONT_Default, 0xFFFFFF, rotate=0)
title0 = M5Title(title="STEP MOTOR CONTROL", x=75, fgcolor=0xFFFFFF, bgcolor=0xa80000)
label10 = M5TextBox(260, 47, "label10", lcd.FONT_Default, 0xFFFFFF, rotate=0)
label11 = M5TextBox(110, 68, "label11", lcd.FONT_Default, 0xFFFFFF, rotate=0)
label12 = M5TextBox(260, 68, "label12", lcd.FONT_Default, 0xFFFFFF, rotate=0)
label13 = M5TextBox(110, 89, "label13", lcd.FONT_Default, 0xFFFFFF, rotate=0)
label14 = M5TextBox(260, 89, "label14", lcd.FONT_Default, 0xFFFFFF, rotate=0)
label15 = M5TextBox(110, 23, "label15", lcd.FONT_Default, 0xFFFFFF, rotate=0)
label18 = M5TextBox(98, 119, "label18", lcd.FONT_Default, 0xFFFFFF, rotate=0)
label19 = M5TextBox(253, 119, "label19", lcd.FONT_Default, 0xFFFFFF, rotate=0)
label20 = M5TextBox(51, 217, "stop", lcd.FONT_Default, 0x00ff00, rotate=0)
label21 = M5TextBox(132, 217, "reverse", lcd.FONT_Default, 0x00ff00, rotate=0)
label22 = M5TextBox(228, 217, "rotate", lcd.FONT_Default, 0x00ff00, rotate=0)
label6 = M5TextBox(15, 150, "label6", lcd.FONT_Default, 0xFFFFFF, rotate=0)
 
from numbers import Number
 
def xprintln(x):
   uart1.write(x+"\r\n")
 
# Describe this function...
def switch_loop():
  global a_true, b_true, c_true, r_dir, in_chars, line, uart1
  if a_true:
    stepmotor.resetMotor(0, 1)
    a_true = 0
    return True
  if b_true:
    r_dir=1-r_dir
    stepmotor.setStepDir(0, r_dir)
    b_true = 0
    return True
  if c_true:
    #xprintln("c_true=true")
    stepmotor.resetMotor(0, 0)
    c_true = 0
    return True
  if uart1.any():
    in_chars = (uart1.read()).decode()
    if '\r' != in_chars:
      line = (str(line) + str(in_chars))
    else:
      label6.setText(str(line))
      parse_command(line)
      line = ''
      xprintln("")
    uart1.write(bytes(in_chars.encode()))
    #label6.setText(str(line))
    return True
  return False
 
def buttonA_wasPressed():
  global a_true, b_true, c_true, r_dir, in_chars, line, uart1
  a_true = (a_true if isinstance(a_true, Number) else 0) + 1
  pass
btnA.wasPressed(buttonA_wasPressed)
 
def buttonB_wasPressed():
  global a_true, b_true, c_true, r_dir, in_chars, line, uart1
  b_true = (b_true if isinstance(b_true, Number) else 0) + 1
  pass
btnB.wasPressed(buttonB_wasPressed)
 
def buttonC_wasPressed():
  global a_true, b_true, c_true, r_dir, in_chars, line, uart1
  c_true = (c_true if isinstance(c_true, Number) else 0) + 1
  pass
btnC.wasPressed(buttonC_wasPressed)
 
def move_left_for_t(t):
   global a_true, b_true, c_true, r_dir
   time_now=time.time()*1000
   last_time=time_now
   stepmotor.setStepDir(0, 0)
   stepmotor.resetMotor(0,0)
   move_for_t(t,time_now,last_time)
 
def move_right_for_t(t):
   global a_true, b_true, c_true, r_dir
   time_now=time.time()*1000
   last_time=time_now
   stepmotor.setStepDir(0, 1)
   stepmotor.resetMotor(0,0)
   move_for_t(t,time_now,last_time)
 
def move_for_t(t,time_now,last_time):
    while time_now - last_time <t:
      if switch_loop():
         return
      time.sleep(0.001)
      time_now=time.time()*1000
    stepmotor.resetMotor(0,1)
 
#parse command
def parse_key(key,inx,rest):
    keylen=len(key)
    inlen=len(inx)
    if inx.startswith(key):
        rest[0]=inx[keylen:inlen]
        return True
    rest[0]=inx
    return False
 
def parse_p_int(command,ix,rest):
    #xprintln("parse_p_int("+command+",ix,rest)")
    i=0
    c=command[0]
    if c in ['0','1','2','3','4','5','6','7','8','9']:
        clen=len(command)
        while True:
            i=i*10+int(c)
            command=command[1:clen]
            clen=len(command)
            if clen==0:
                ix[0]=i
                rest[0]=""
                return True
            #xprintln("parse_p_int command="+command)
            c=command[0]
            if not (c in ['0','1','2','3','4','5','6','7','8','9']):
                ix[0]=i
                rest[0]=command
                return True
    rest[0]=command
    ix[0]=0
    return False
 
def r_b(inx):
    rx=[""]
    while parse_key(" ",inx,rx):
        inx=rx[0]
    return rx[0]
 
def parse_String_Constant(inx,strc,rest):
    rx=[""]
    xstr=""
    if parse_key('"',inx,rx):
        inx=rx[0]
        fx=inx[0]
        xlen=len(inx)
        while fx!='"':
            if xlen==0:
                return False
            if fx=='\\':
                inx=inx[1:xlen]
            else:
                xstr=xstr+fx
            xlen=len(inx)
            inx=inx[1:xlen]
            fx=inx[0]
        if parse_key('"',inx,rx):
            strc[0]=xstr
            rest[0]=rx[0]
            return True
    return False
 
#
# command
#    rotate
#    reverse
#    stop
#
def parse_command(command):
    global a_true, b_true, c_true, r_dir, in_chars, line, uart1
    rest=[""]
    strc=[""]
    ix=[0]
    #command_field.delete(0,END)
    #command_field.insert(END,command)
    command=r_b(command)
    #xprintln("parse_command("+command+")")
    if parse_key("rotate",command,rest):
        #xprintln("parse_key(rotate "+command+","+rest[0]+")")
        c_true=1
        return True
    elif parse_key("reverse",command,rest):
        command=r_b(rest[0])
        b_true=1
        return True
    elif parse_key("left ",command,rest):
       command=r_b(rest[0])
       if parse_p_int(command,ix,rest):
          move_left_for_t(ix[0])
          return True
       return False
    elif parse_key("right ",command,rest):
       command=r_b(rest[0])
       if parse_p_int(command,ix,rest):
          move_right_for_t(ix[0])
          return True
       return False
    elif parse_key("stop",command,rest):
        a_true=1
        return True
 
a_true = 0
b_true = 0
c_true = 0
r_dir = 0
stepmotor.initDevice(0x27)
uart1 = machine.UART(1, tx=1, rx=3)
uart1.init(115200, bits=8, parity=None, stop=1)
xprintln("start m5stepmotor_06.py")
line = ''
stepmotor.setStepPulse(500, 0)
stepmotor.setStepDir(0, 1)
stepmotor.enableMotor(1)
label18.setText(str(stepmotor.readStatus(0XFE)))
label19.setText(str(stepmotor.readStatus(0XFF)))
stepmotor.resetMotor(0, 1)
while True:
  label10.setText(str(stepmotor.readPinStatus(0)))
  label11.setText(str(stepmotor.readPinStatus(1)))
  label12.setText(str(stepmotor.readPinStatus(2)))
  label13.setText(str(stepmotor.readPinStatus(3)))
  label14.setText(str(stepmotor.readIOstatus()))
  label15.setText(str(stepmotor.readFaultPinStatus(0)))
  switch_loop()
  #wait_ms(1)
  time.sleep(0.001)

Counter: 169, today: 1, yesterday: 0

トップ   新規 一覧 単語検索 最終更新   ヘルプ   最終更新のRSS