问题
I have been using the new LEGO® MINDSTORMS® Robot Inventor.
When creating a new "Project", the said project seems to contain a single file. However, at the start of every sample code file, one can see that the mindstorm modules are imported, like so:
from mindstorms import MSHub, Motor, MotorPair, ColorSensor, DistanceSensor, App
from mindstorms.control import wait_for_seconds, wait_until, Timer
from mindstorms.operator import greater_than, greater_than_or_equal_to, less_than, less_than_or_equal_to, equal_to, not_equal_to
import math
Does anybody know how can I create my own modules, and import them into a given project? Is it possible?
回答1:
EDIT: spent some more time on this, and figured it out! I'm keeping the original answer below. Here's a short reproducible solution. Tested working with (taken from os.uname
)
sysname='LEGO Technic Large Hub'
nodename='LEGO Learning System Hub'
release='1.11.0'
version='v1.11-1139-gf7407e5a0 on 2020-06-19'
machine='LEGO Technic Large Hub with STM32F413xx'
Create a new python project with this content:
import os import sys def example() -> None: print("imported") # EOF # upload the module to the Hub print("beginning upload from {}...".format(__name__)) os.chdir("projects") open("__init__.py", "w").close() filename = "{}.py".format(__name__.split("/")[-1]) new_filename = "mystorms.py" try: os.remove(new_filename) except: print("{} does not exists".format(new_filename)) os.rename(filename, new_filename) # remove everything after EOF with open(new_filename, "r") as f: content = f.read() content = content.split("# EOF")[0] with open(new_filename, "w") as f: f.write(content) print(os.listdir()) sys.exit()
The stuff after
# EOF
does the actual "uploading" and ensures that when you try to import this elsewhere, you don't re-"upload" it. Note that, in regular python, you'd wrap this inif __name__ == "__main__"
, but Mindstorms doesn't use that convention when it's running.Send that project to your Hub and run it. Note that you'll have those files that persist on your Hub, and I have no idea if there are memory constraints that you'll need to worry about.
Create a new python project with the following:
import os import projects.mystorms as ms print(os.uname()) ms.example()
The above should work. Note that if you need to update the file, you'll have to adjust the module code to delete the original if it exists, but that's a minor change. I have not done anything beyond the above, but this could lead to a way to get missing stdlib stuff into Mindstorms as well.
Haven't found a solution, but I did go through most of the standard library to see what else you can import which is kind of related... The below did not throw an error, but I have not yet attempted anything with the packages themselves.
import array
import builtins
import cmath
import ctypes
import errno
import gc
import hashlib
import heapq
import io
import json
import math
import os
import random
import re
import select
import struct
import sys
import time
The underlying theme seems to be limiting access to the filesystem and forcing any async operations to be done with the actual Mindstorms methods (e.g. hub.speaker.start_beep()
and the like). There is some directory structure (the 0-19 files), but I haven't figured out if that can be used to achieve what you need.
Also interesting to note is that you can't import typing
, but you can use type hints in your functions. I also strongly dislike that you can't copy-paste from the console.
来源:https://stackoverflow.com/questions/64449448/how-to-import-from-custom-python-modules-on-new-lego-mindstorms-robot-inventor