antonvh / mpy-robot-tools

Install on a SPIKE Legacy or LEGO MINDSTORMS Robot Inventor hub for easy Bluetooth communication, animation, and motor synchronization.
https://antonsmindstorms.com
GNU General Public License v3.0
35 stars 11 forks source link
lego lego-mindstorms micropython robotics

Micropython robot tools

This is a collection of classes and methods that help you to animate robots. They are specifically aimed at LEGO MINDSTORMS and SPIKE Prime robots, although the classes are abstract enough to be useful elsewhere. The functionality is documented inside the code. Here are just the main usages. For in-depth articles see my blog on antonsmindstorms.com.

Installation

  1. Copy the code from the install script (click the Copy raw contents button) to an empty python project in the LEGO MINDSTORMS or LEGO SPIKE program.
  2. Then run it once, it will take additional time to transfer to the Hub please use patience.
  3. UNPLUG The hub before it completes the restart after the script!! Watch the console output for action to unplug.

Caution if the Spike/Mindstorm app asks for a firmware update, and you accept you will need to re-install. You can just disconnect and ignore the update the Hub will be in a "big square" for waiting for update just long press the center button and power cycle the hub to reconect to the lego app, it won't ask a second time to update in the instance.

Uninstall

To uninstall remove the directory out of the /projects folder using a script, or something like ThonnyIDE File manager.

To factory reset your hub to factory settings, using LEGO app, press the Hub Connection Icon on the Programming Canvas, press the More Button (···) in the Dashboard tab and select Reset Settings. Press OK to confirm the reset, or Cancel to keep the current settings. Be careful - resetting your Hub will delete all of your programs, and they can’t be recovered!

Overview of the libraries

motor_sync.py

light.py

bt.py

By default the UART objects are buffered. They will remember everything ever written to them and you should flush their buffer with _ = read() to start clean. For remote control and state communication the buffer gets in the way. You just want to read the most recent state when it arrives. In that case initialize with additive_buffer=False Example:

# Create link to the snake head, and advertise self as tail.
head_link = UARTPeripheral(name="tail", additive_buffer=False)

If you have multiple Bluetooth objects, like a remote control, a serialtalk and a plain UART, you need to pass a single BLEHandler to all of them:

from projects.mpy_robot_tools.bt import BLEHandler, UARTCentral
from projects.mpy_robot_tools.rc import RCReceiver, R_STICK_VER, L_STICK_HOR, SETTING2

ble = BLEHandler()
seg_1_link = UARTCentral(ble_handler=ble)
seg_2_link = UARTCentral(ble_handler=ble)
rcv = RCReceiver(name="snake", ble_handler=ble)

rc.py

rctrl_plus.py

pyhuskylens.py

helpers.py

Remote UART library: uartremote.py

NOTE: This is temporary. In the future I hope to replace with serialtalk. It's the same idea but it also works over bluetooth and websockets. This is a library for robust, near real-time communication between two UART devices. We developed it with LEGO EV3, SPIKE Prime and other MicroPython (ESP) modules. The library has the following properties:

Read more in the README file of that library.

To Do

Stubs

For programming convenience in VS Code I would love to collect stubs of all LEGO hub libraries. I've been looking into micropython-stubber but it didn't work for me.