Skip to content

Instantly share code, notes, and snippets.

@MrCheatEugene
Created April 21, 2022 13:36
Show Gist options
  • Save MrCheatEugene/f43d5b7a360554fc5d00e1769c070753 to your computer and use it in GitHub Desktop.
Save MrCheatEugene/f43d5b7a360554fc5d00e1769c070753 to your computer and use it in GitHub Desktop.
Run software on Lego NXT From Python 2.7! USB Only!
# usage: python run_uploaded_software.py ProgramName 1/0
# 0 - close running program and run ProgramName
# 1 - close running program, wait for ProgramName to finish, run previous program.
# ProgramName - program name, uploaded on NXT
# Requires pyusb, setuptools, setuptools-scm, nxt-python. You can install them from attachement below.
import nxt.locator
import sys
import time
b = nxt.locator.find_one_brick()
if (str(sys.argv[2]) == "0"):
try:
b.get_current_program_name()
print("Program is running, stopping it..")
b.stop_program()
pass
except Exception as e:
if(e=="No active program"):
print("No active program")
pass
b.start_program(str(sys.argv[1])+".rxe")
print("Program started: "+str(sys.argv[1])+".rxe")
elif(str(sys.argv[2]) == "1"):
prevName=""
print("Return to previous program is enabled")
try:
prevName=b.get_current_program_name()
print("Program is running, stopping it..")
b.stop_program()
time.sleep(2)
pass
except Exception as e:
if(e=="No active program"):
print("No active program")
pass
b.start_program(str(sys.argv[1])+".rxe")
print("Program started: "+str(sys.argv[1])+".rxe")
while True:
if prevName == "":
print("No previous program was found,exiting.")
break;
pass
try:
b.get_current_program_name()
pass
except Exception as e:
print(e)
time.sleep(1)
print("Program "+str(sys.argv[1])+".rxe is stopped. Returining to previous..")
b.start_program(prevName)
print("Started previous program: "+prevName)
try:
while(b.get_current_program_name() != prevName):
b.start_program(prevName)
time.sleep(1)
pass
except Exception as e:
time.sleep(2)
b.start_program(prevName)
print("Exiting..")
break;
pass
pass
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment