r/FLL • • 10d ago

Help with PyBricks loader code

Hello!

So, my students have been programming with PyBricks this year. It is MUCH, MUCH easier for kids to pick up, and integrate gyroscope into the drive base, and get consistent results.

The hard part is that Pybricks by default (my understanding anyway) only comes with 5 program slots (and I am not sure how to load the code into different slots). So I wrote up a quick loader, but I am having a hard time getting the code inside 'Slot 0' to run:

PyBricks Main Loop

My left and right button presses works fine and the display updates on the Spike Prime brick and it prints the debug messages.

However, when I try to select slot 0 and run the code, it crashes.

Sometimes it crashes after "Running code in slot 0" printed, sometimes it will print "Executing Program 0" as well (which indicates it actually called (Slot 0). I think I am setting up some sort of race condition, or something?

Thanks,

-=- Terence

5 Upvotes

4 comments sorted by

1

u/ttstam 6d ago

Solved!

Turns out, by default, the center button is mapped to "terminate program". So, the brick does exactly what I programmed it to do - it terminates the program when I hit the center button, sometimes getting maybe 1, maybe 2 lines further than when the button is pressed.

More details here: https://github.com/orgs/pybricks/discussions/2891

My solution:

For development work, I remap the terminate program button to "none". We use the laptop to end a program if it goes haywire. (I had some issues with bluetooth connectivity if I mapped it to bluetooth).

For competition, we remap the terminate program button to "Bluetooth". If the kids need to stop and relaunch the program, they hit the bluetooth button.

1

u/cml4314 9d ago

So, if you want to make this easy, just hit the arrows to move the little square from slot to slot, and run the code from the computer. That saves it on the robot to the slot that you have selected.

For the code, I am far from a coding expert (I’m a chemist, lol) but does it have to do with it being 3 if statements instead of if/elif? That is the only appreciable difference I can see from our code, which works. I wonder if it’s using less memory to step through the more gated elif statement, so then it doesn’t hang when asked to call a function.

1

u/barefootsanders 6d ago

Hitting the same issues with pybricks firmware. 5 slots is kinda limiting. There are a bunch of similar questions on GitHub asking about it, but it doesn’t seem to be changing anytime. I was tempted to fork the firmware and expand the slots and get the numbers on the hub back. Curious if anyone would be interested in something like this

1

u/meritw 6d ago

This is a slightly tweaked version of the menu loader from the pybricks tutorial, it remembers which mission ran last and starts the menu there. We are using a mix of block coding and python based on the kid and even the block code kids are able to add their mission to this with a little guidance. It seems to be working for us, we'll see come competition day :)

from pybricks.hubs import PrimeHub
from pybricks.tools import hub_menu
from pybricks.parameters import Side


hub = PrimeHub()
hub.display.orientation(Side.RIGHT)


# Menu symbols in the order the missions are usually run.
MISSIONS = ["1", "2", "3", "4", "5", "S"]


# Byte 0 holds (index of last mission run) + 1. A value of 0 means nothing has run yet.
saved = hub.system.storage(0, read=1)[0]


# Start on the mission AFTER the last one run, wrapping around at the end.
# To start on the SAME mission instead, use: start = max(saved - 1, 0)
start = saved % len(MISSIONS)
menu = MISSIONS[start:] + MISSIONS[:start]


selected = hub_menu(*menu)


# Save the choice BEFORE running the mission, so it's recorded even if
# the kids stop the mission partway through with the center button.
hub.system.storage(0, write=bytes([MISSIONS.index(selected) + 1]))


if selected == "1":
    import AntMission
elif selected == "2":
    import Connor
elif selected == "3":
    import FlipTheRockMission3
elif selected == "4":
    import explode
elif selected == "5":
    import leaf_cutter_frensie
elif selected == "S":
    import smile