async def zap(): await asyncio.sleep_ms(1000) if mode == "s": print("soft_reset()") machine.soft_reset() else: machine.reset()
async def usb_keypad_emu(): # Take keypresses on USB virtual serial port (when not in REPL mode) # and converts them into keypad events. Super handy for UX testing/dev. # # IMPORTANT: # - code is **not** used in real product, but left here for devs to use # - this code isn't even called; unless you add code to do so, see ../stm32/my_lib_boot2.py # await sleep_ms(1000) # avoid slowing the startup from ux import the_ux from menu import MenuSystem from seed import WordNestMenu import gc u = pyb.USB_VCP() remap = { '\r': 'y', '\x1b': 'x', '\x1b[A': '5', '\x1b[B': '8', '\x1b[C': '9', '\x1b[D': '7' } while 1: await sleep_ms(100) while u.isconnected() and u.any(): from glob import numpad k = u.read(3).decode() if k in '\x04': # ^D # warm reset from machine import soft_reset soft_reset() if k == 'T': ckcc.vcp_enabled(True) print("Repl") continue if k == 'm': print("free = %d" % gc.mem_free()) continue if k in remap: k = remap[k] if k in '0123456789xy': numpad.inject(k) continue
def test(has_usb=True, delay=60, retry=60): global debug debug = has_usb wt = asyncio.run(main(client)) # True == success # wt allows different wait time in case of failure. client.close() tim = delay if wt else retry if debug: # In testing with USB don't deepsleep sleep(tim) soft_reset( ) # Assumes main.py imports this module and calls this function deepsleep(tim * 1000)
def ha_sta(): """ Check and repair STA network mode """ from ConfigHandler import cfgget from network import STA_IF, WLAN # Set STA and Connect if cfgget('nwmd') == 'AP' or not WLAN(STA_IF).isconnected(): # Soft reset micropython VM - fast recovery from machine import soft_reset soft_reset() return '{} mode, OK'.format(cfgget('nwmd'))
def demo_soft_reboot(): ''' Demonstrates how to initiate a soft reboot from MicroPython. With a soft reboot, it just restarts the MicroPython interpreter (and restarts main.py/main.mpy (if configured to do so with the ATPS1 setting). It does not restart the entire device. https://www.digi.com/resources/documentation/digidocs/90002219/#tasks/t_reset_repl.htm Note that there does not appear to be a way to initiate a soft reset using AT commands, perhaps because a soft reset is specific to the MicroPython code. ''' import machine machine.soft_reset()
def ota(self): url = 'https://raw.githubusercontent.com/jsonnet/CANLogger/master/version' response = self.modem.http_request(url, 'GET') # If a newer version is available if float(response.text) > self.VERSION: url = 'https://raw.githubusercontent.com/jsonnet/CANLogger/master/code/main.py' response = self.modem.http_request(url, 'GET') # Override existing main file and reboot with open(self.PATH + 'main.py', 'w') as f: print(response.text, file=f) # Force buffer write and restart os.sync() machine.soft_reset()
async def usb_keypad_emu(): # Take keypresses on USB virtual serial port (when not in REPL mode) # and converts them into keypad events. Super handy for UX testing/dev. # # IMPORTANT: # - code is **not** used in real product, but left here for devs to use # - this code isn't even called; unless you add code to do so. # u = pyb.USB_VCP() while 1: await sleep_ms(100) while u.isconnected() and u.any(): from main import numpad k = u.read(3).decode() remap = { '\r': 'y', '\x1b': 'x', 'q': 'x', '\x1b[A': '5', '\x1b[B': '8', '\x1b[C': '9', '\x1b[D': '7' } if k in remap: k = remap[k] if k in '\x04': # ^D # warm reset from machine import soft_reset soft_reset() if k == 'U': # enter DFU #from machine import bootloader #bootloader() import callgate callgate.enter_dfu() if k in '0123456789xy': numpad.inject(k) else: print('? %r' % k)
async def usb_keypad_emu(): # Take keypresses on USB virtual serial port (when not in REPL mode) # and converts them into keypad events. Super handy for UX testing/dev. # # IMPORTANT: # - code is **not** used in real product, but left here for devs to use # - this code isn't even called; unless you add code to do so, see ../stm32/my_lib_boot2.py # u = pyb.USB_VCP() while 1: await sleep_ms(100) while u.isconnected() and u.any(): from main import numpad k = u.read(3).decode() remap = { '\r': 'y', '\x1b': 'x', 'q': 'x', '\x1b[A': '5', '\x1b[B': '8', '\x1b[C': '9', '\x1b[D': '7' } if k in remap: k = remap[k] if k in '\x04': # ^D # warm reset from machine import soft_reset soft_reset() if k == 'd': numpad.debug = (numpad.debug + 1) % 3 continue if k == 'n': if numpad.disabled: numpad.start() else: numpad.stop() print("npdis = %d" % numpad.disabled) continue if k == 'r': numpad.trigger_baseline = True continue if k == 's': numpad.sensitivity = (numpad.sensitivity + 1) % 5 print("sensi = %d" % numpad.sensitivity) continue if k == 'U': # enter DFU import callgate callgate.enter_dfu() # not reached if k == 't': ckcc.vcp_enabled(True) print("Repl enabled") continue if k == 'm': import gc print("Memory: %d" % gc.mem_free()) continue if k in '0123456789xy': numpad.inject(k) else: print('? %r' % k)
publish_Obs() else: pass else: pass else: pass else: pass tCheck = time.ticks_ms() if tCheck < timeStart: ## cover wrap around timing timeStart = tCheck else: timeDiff = tCheck - timeStart if timeDiff > 600000: machine.soft_reset() wdt.feed() if publish_Success == True and file_lineRef > 0: readFiles() if ((utime.localtime()[4]%15) == 0) and timeUpdated == False: printLocalTime() timeUpdated = True wdt.feed() if (timeUpdated == True) and ((utime.localtime()[4]%15) != 0): timeUpdated = False if ((utime.localtime()[4]%30) == 0) and rssiUpdated == False: topic_ToSend.append(msg_Topic) msg_ToSend.append("RSSI: " + str(xbeeRSSI)) obs_publishReady = obs_publishReady + 1 rssiUpdated = True wdt.feed()
async def reset_self(*a): import machine machine.soft_reset()
async def usb_keypad_emu(): # Take keypresses on USB virtual serial port (when not in REPL mode) # and converts them into keypad events. Super handy for UX testing/dev. # # IMPORTANT: # - code is **not** used in real product, but left here for devs to use # - this code isn't even called; unless you add code to do so, see ../stm32/my_lib_boot2.py # from ux import the_ux from menu import MenuSystem from seed import WordNestMenu u = pyb.USB_VCP() remap = { '\r': 'y', '\x1b': 'x', '\x1b[A': '5', '\x1b[B': '8', '\x1b[C': '9', '\x1b[D': '7' } while 1: await sleep_ms(100) while u.isconnected() and u.any(): from main import numpad k = u.read(3).decode() if k in '\x04': # ^D # warm reset from machine import soft_reset soft_reset() if 0: if k == 'd': numpad.debug = (numpad.debug + 1) % 3 continue if k == 'n': if numpad.disabled: numpad.start() else: numpad.stop() print("npdis = %d" % numpad.disabled) continue if k == 'U': # enter DFU import callgate callgate.enter_dfu() # not reached if k == 'T': ckcc.vcp_enabled(True) print("Repl enabled") continue if k == 'M': import gc print("Memory: %d" % gc.mem_free()) continue # word menus: shortcut for first letter top = the_ux.top_of_stack() if top and isinstance(top, WordNestMenu) and len(top.items) > 6: pos = min(len(i.label) for i in top.items) if pos >= 2: for n, it in enumerate(top.items): if it.label[pos-2] == k: top.goto_idx(n) top.show() k = None break if k is None: continue if k in remap: k = remap[k] if k in '0123456789xy': numpad.inject(k) continue print('? %r' % k)
def command(): # Flag for incorrect command incorrect_cmd = False messages = read_uart() # Splits up multiple commands in the buffer messages = messages.split('\n') # Delete empty commands if len(messages[-1]) == 0: del messages[-1] for message in messages: message.strip() if message == "stop": stop() message = message.split('/') # If there is no separator (invalid input), length will be 1 if len(message) > 1: cmd_type = message[0] command = message[1] # Absolute motor command # m_a/l,10,r,-43 # m_a_j is only used by the joystick and is used # to suppress displaying motor command messages on front end if (cmd_type == 'm_a') or (cmd_type == 'm_a_j'): command = command.split(',') # Single side motor command needs 2 arguments. If less/more are given, it raises the flag if len(command) == 2: flag = command_verify((command[0], command[1])) # Correct command if flag[0] is False: if debug_flag is True: debug((str(command[0]), int(command[1])), debug_flag) motor.motor_abs((str(command[0]), int(command[1]))) if cmd_type == 'm_a': send_uart(cmd_type + "/ valid input\n", log_flag) else: incorrect_cmd = True error_msg = flag[1] # Double side motor command needs 4 arguments. If less/more are given, it raises the flag elif len(command) == 4: flag = command_verify((command[0], command[1]), (command[2], command[3])) # Correct command if flag[0] is False: if debug_flag is True: debug((str(command[0]), int( command[1]), str(command[2]), int(command[3])), debug_flag) motor.motor_abs((str(command[0]), int(command[1])), (str(command[2]), int(command[3]))) if cmd_type == 'm_a': send_uart(cmd_type + "/ valid input\n", log_flag) else: incorrect_cmd = True error_msg = flag[1] else: incorrect_cmd = True error_msg = 'Incorrect number of motor arguments\n' # Relative motor command # m_r/l,-4,r,32 elif cmd_type == 'm_r': command = command.split(',') if len(command) == 2: flag = command_verify((command[0], command[1])) # Correct command if flag[0] is False: if debug_flag is True: debug((str(command[0]), int(command[1])), debug_flag) rel_flag = motor.motor_rel( (str(command[0]), int(command[1]))) if rel_flag[0] is False: send_uart("valid input\n", log_flag) else: send_uart(rel_flag[1], log_flag) else: incorrect_cmd = True error_msg = flag[1] elif len(command) == 4: flag = command_verify((command[0], command[1]), (command[2], command[3])) # Correct command if flag[0] is False: if debug_flag is True: debug((str(command[0]), int( command[1]), str(command[2]), int(command[3])), debug_flag) rel_flag = motor.motor_rel( (str(command[0]), int(command[1])), (str(command[2]), int(command[3]))) if rel_flag[0] is False: send_uart("valid input\n", log_flag) else: send_uart(rel_flag[1], log_flag) else: incorrect_cmd = True error_msg = flag[1] # PID # pid/start, pid/stop, pid/set_pid(P,I,D), pid/set_pwr(MAXPOWER), pid/get_pid, pid/get_pwr elif cmd_type == 'pid': command = command.split('(') if command[0] == 'start': _thread.start_new_thread(global_variables.pid_thread, ()) elif command[0] == 'stop': global_variables.pid_test.stop_update() elif command[0] == 'set_pid': end = command[1].find(')') if end is not -1: command = command[1][0:end] command = command.split(',') try: p = float(command[0]) i = float(command[1]) d = float(command[2]) global_variables.pid_test.set_pid(p, i, d) send_uart("PID coefficients set", log_flag) except ValueError: error_msg = 'invalid PID' incorrect_cmd = True elif command[0] == 'get_pid': (Kp, Ki, Kd) = global_variables.pid_test.get_pid() send_uart( "Kp: {0:.2f} Ki: {1:.2f} Kd: {2:.2f}".format( Kp, Ki, Kd), log_flag) elif command[0] == 'set_pwr': end = command[1].find(')') if end is not -1: try: pwr = float(command[1][0:end]) flag = global_variables.pid_test.set_pwr(pwr) if flag is True: send_uart( "PID power limit is: {0:.2f}".format(pwr), log_flag) else: send_uart("PID power limit out of range", log_flag) except ValueError: error_msg = 'invalid PID power' incorrect_cmd = True elif command[0] == 'get_pwr': send_uart( "PID power limit is: {0:.2f}".format( global_variables.pid_test.get_pwr()), log_flag) else: incorrect_cmd = True error_msg = 'Invalid PID command' # Reset # reset/soft, reset/hard elif cmd_type == 'reset': if command == 'soft': send_uart("soft reset\n", log_flag) utime.sleep_ms(300) machine.soft_reset() elif command == 'hard': send_uart("hard reset\n", log_flag) utime.sleep_ms(300) machine.reset() else: incorrect_cmd = True error_msg = 'Invalid reset' # GPS # gps/status, gps/read, gps/update elif cmd_type == 'gps': if command == 'status': global_variables.gps_device.gps_data() elif command == 'read': global_variables.gps_device.print_msg() elif command == 'update': global_variables.gps_device.update() else: error_msg = "Invalid gps command" incorrect_cmd = True # Planner # planner/init, planner/start, planner/stop, planner/status, planner/pause, planner/add_start, planner/coord:[[-43.23, 233],[23.43, 234.4]] elif cmd_type == 'planner': command = command.split(':') if command[0] == 'init': global_variables.gps_planner.waypoint_init() elif command[0] == 'add_start': global_variables.gps_planner.waypoint_add(True) elif command[0] == 'add_end': global_variables.gps_planner.waypoint_add(False) elif command[0] == 'coord': coord = command[1].split(',') global_variables.gps_planner.waypoint_coord(coord) elif command[0] == 'start': global_variables.gps_planner.start_planner() elif command[0] == 'pause': global_variables.gps_planner.pause_planner() elif command[0] == 'resume': global_variables.gps_planner.resume_planner() elif command[0] == 'stop': global_variables.gps_planner.stop_planner() elif command[0] == 'home': global_variables.gps_planner.go_home() elif command[0] == 'status': global_variables.gps_planner.planner_data() else: error_msg = "Invalid planner command" incorrect_cmd = True # UART_comms_master # uart/on, uart/off elif cmd_type == 'uart': if command == 'on': global_variables.UART_comms_master = True send_uart("UART on", log_flag) elif command == 'off': send_uart("UART off", log_flag) global_variables.UART_comms_master = False else: error_msg = "Invalid UART command" incorrect_cmd = True # Ping # ping/ elif cmd_type == 'ping': backup = global_variables.UART_comms_master global_variables.UART_comms_master = True send_uart("ping", log_flag) global_variables.UART_comms_master = backup # Calibrate # calibrate/ elif cmd_type == 'calibrate': sensors.fusion_calibrate() # Sensors # sensors/battery elif cmd_type == 'sensors': if command == 'battery': sensors.send_voltage() elif command == 'motor_current': sensors.motor_current() else: error_msg = "Invalid sensor command" incorrect_cmd = True # Servo # servo/setup-1 elif cmd_type == 'servo': command = command.split('-') if command[0] == 'init': sensors.servo_init(command[1]) elif command[0] == 'move': args = command[1].split(',') sensors.servo_move(int(args[0]), str(args[1])) elif command[0] == 'deinit': sensors.servo_deinit(command[1]) else: error_msg = "Invalid servo command" incorrect_cmd = True # -------------------------- # Must be below all commands else: incorrect_cmd = True error_msg = 'Incorrect first letter in command\n' if incorrect_cmd is True: send_uart(error_msg, log_flag) # Resetting the flag incorrect_cmd = False else: if message[0] == 'stop': stop() elif message[0] != 'no msg': error_msg = 'Invalid input: {}, no separator'.format( str(message)) send_uart(error_msg, log_flag)
def reset(): pyb.sync() machine.soft_reset()