#author("2023-10-03T23:20:20+09:00","default:Real2Virtual202111","Real2Virtual202111") #author("2023-10-03T23:20:42+09:00","default:Real2Virtual202111","Real2Virtual202111") [[Real2Virtual202111]] * main_controller_01.py [#kb9d58b1] - GUI -- &ref(main_controller_01.py/main_controller_01_gui.png,50%); #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_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