m5stack_uiflow_stepmotor_01.py
をテンプレートにして作成
[
トップ
] [
新規
|
一覧
|
単語検索
|
最終更新
|
ヘルプ
|
ログイン
]
開始行:
[[Real2Virtual202111]]
* main_controller_01.py [#kb9d58b1]
#code(Python){{
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_Defaul...
label1 = M5TextBox(15, 68, "LIMIT SW2:", lcd.FONT_Default...
label2 = M5TextBox(169, 68, "LIMIT SW3:", lcd.FONT_Defaul...
label3 = M5TextBox(15, 89, "LIMIT SW4:", lcd.FONT_Default...
label4 = M5TextBox(169, 89, "ALL SW:", lcd.FONT_Default, ...
label5 = M5TextBox(15, 24, "FAULT X:", lcd.FONT_Default, ...
label8 = M5TextBox(15, 119, "FW VER:", lcd.FONT_Default, ...
label9 = M5TextBox(169, 119, "I2C ADD:", lcd.FONT_Default...
title0 = M5Title(title="STEP MOTOR CONTROL", x=75, fgcolo...
label10 = M5TextBox(260, 47, "label10", lcd.FONT_Default,...
label11 = M5TextBox(110, 68, "label11", lcd.FONT_Default,...
label12 = M5TextBox(260, 68, "label12", lcd.FONT_Default,...
label13 = M5TextBox(110, 89, "label13", lcd.FONT_Default,...
label14 = M5TextBox(260, 89, "label14", lcd.FONT_Default,...
label15 = M5TextBox(110, 23, "label15", lcd.FONT_Default,...
label18 = M5TextBox(98, 119, "label18", lcd.FONT_Default,...
label19 = M5TextBox(253, 119, "label19", lcd.FONT_Default...
label20 = M5TextBox(51, 217, "stop", lcd.FONT_Default, 0x...
label21 = M5TextBox(132, 217, "reverse", lcd.FONT_Default...
label22 = M5TextBox(228, 217, "rotate", lcd.FONT_Default,...
label6 = M5TextBox(15, 150, "label6", lcd.FONT_Default, 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, u...
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, u...
a_true = (a_true if isinstance(a_true, Number) else 0) ...
pass
btnA.wasPressed(buttonA_wasPressed)
def buttonB_wasPressed():
global a_true, b_true, c_true, r_dir, in_chars, line, u...
b_true = (b_true if isinstance(b_true, Number) else 0) ...
pass
btnB.wasPressed(buttonB_wasPressed)
def buttonC_wasPressed():
global a_true, b_true, c_true, r_dir, in_chars, line, u...
c_true = (c_true if isinstance(c_true, Number) else 0) ...
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'...
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,...
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
終了行:
[[Real2Virtual202111]]
* main_controller_01.py [#kb9d58b1]
#code(Python){{
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_Defaul...
label1 = M5TextBox(15, 68, "LIMIT SW2:", lcd.FONT_Default...
label2 = M5TextBox(169, 68, "LIMIT SW3:", lcd.FONT_Defaul...
label3 = M5TextBox(15, 89, "LIMIT SW4:", lcd.FONT_Default...
label4 = M5TextBox(169, 89, "ALL SW:", lcd.FONT_Default, ...
label5 = M5TextBox(15, 24, "FAULT X:", lcd.FONT_Default, ...
label8 = M5TextBox(15, 119, "FW VER:", lcd.FONT_Default, ...
label9 = M5TextBox(169, 119, "I2C ADD:", lcd.FONT_Default...
title0 = M5Title(title="STEP MOTOR CONTROL", x=75, fgcolo...
label10 = M5TextBox(260, 47, "label10", lcd.FONT_Default,...
label11 = M5TextBox(110, 68, "label11", lcd.FONT_Default,...
label12 = M5TextBox(260, 68, "label12", lcd.FONT_Default,...
label13 = M5TextBox(110, 89, "label13", lcd.FONT_Default,...
label14 = M5TextBox(260, 89, "label14", lcd.FONT_Default,...
label15 = M5TextBox(110, 23, "label15", lcd.FONT_Default,...
label18 = M5TextBox(98, 119, "label18", lcd.FONT_Default,...
label19 = M5TextBox(253, 119, "label19", lcd.FONT_Default...
label20 = M5TextBox(51, 217, "stop", lcd.FONT_Default, 0x...
label21 = M5TextBox(132, 217, "reverse", lcd.FONT_Default...
label22 = M5TextBox(228, 217, "rotate", lcd.FONT_Default,...
label6 = M5TextBox(15, 150, "label6", lcd.FONT_Default, 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, u...
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, u...
a_true = (a_true if isinstance(a_true, Number) else 0) ...
pass
btnA.wasPressed(buttonA_wasPressed)
def buttonB_wasPressed():
global a_true, b_true, c_true, r_dir, in_chars, line, u...
b_true = (b_true if isinstance(b_true, Number) else 0) ...
pass
btnB.wasPressed(buttonB_wasPressed)
def buttonC_wasPressed():
global a_true, b_true, c_true, r_dir, in_chars, line, u...
c_true = (c_true if isinstance(c_true, Number) else 0) ...
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'...
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,...
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
ページ名: