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.
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.
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!
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)
Use the MINDSTORMS Ble RC Anroid app or another Smart Hub to remote control your robot or Hot Rod
Example
# RCReceiver() - Receives RC commands from the android app or RCTransmitter class.
rcv = RCReceiver(name="snake")
while rcv.is_connected():
speed, turn, delay_setting = rcv.controller_state(R_STICK_VER, L_STICK_HOR, SETTING2)
# RCTransmitter() - Connects to an RCReceiver and transmits the state of 9 gamepad-like controls.
remote_control = RCTransmitter()
remote_control.connect(name="snake")
remote_control.set_stick(L_STICK_HOR, steer_angle )
remote_control.set_stick(R_STICK_VER, forward)
remote_control.transmit()
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.
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.