Created
April 21, 2022 13:36
-
-
Save MrCheatEugene/f43d5b7a360554fc5d00e1769c070753 to your computer and use it in GitHub Desktop.
Run software on Lego NXT From Python 2.7! USB Only!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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