Pybricks doco for Lego Hub shows was_pressed is a function, but compiler gives an error #2137
Replies: 16 comments 12 replies
-
|
Hi, On beta.pybricks.com I made this small program: from pybricks.parameters import Button
pressed = [Button.LEFT_MINUS]
was_pressed = [Button.CENTER]
if Button.LEFT_MINUS in pressed and Button.LEFT_MINUS not in was_pressed:
print("OK")And that runs fine. Might there be a syntax error or the line before this line? Bert |
Beta Was this translation helpful? Give feedback.
-
|
Please provide a link to the doc reference. Based on the context provided, was_pressed and pressed are variables that are likely assumed to be defined and set as a result of a prior line of code not provided. |
Beta Was this translation helpful? Give feedback.
-
|
Thanks for taking the time to reply!
This is my first time writing code in Python, so I am still trying to work
out the syntax of lots of functions although in this instance it was a copy
and paste directly from the doco that was mentioned.
The fact that (unseen/undeclared) variables are magically available through
the IMPORTS means it takes a bit of time to work things out, especially as
the PyBricks doco focussed more on the BLOCK programming side of things.
Google/AI has been helpful filling in the gaps!
I did actually work out the issue but in doing so came up with another
question - What does this mean?
pressed = ()
I will assume it yields the same result as pressed = None, or pressed = 0,
but searching google doesn't understand this and provide too many
meaningless results
The link to the doco is Basic Button Techniques - Pybricks
<https://pybricks.com/projects/tutorials/wireless/remote-control/button-basics/>
Regards
Peter
…On Thu, Mar 27, 2025 at 1:18 AM Erik Thompson ***@***.***> wrote:
Please provide a link to the doc reference. Based on the context provided,
was_pressed and pressed are variables that are likely assumed to be defined
and set as a result of a prior line of code not provided.
—
Reply to this email directly, view it on GitHub
<#2137 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/BQPX7BE63L4PDATRXD26IHD2WKZJ7AVCNFSM6AAAAABZ2AX5YKVHI2DSMVQWIX3LMV43URDJONRXK43TNFXW4Q3PNVWWK3TUHMYTENRSHEZDANA>
.
You are receiving this because you authored the thread.Message ID:
***@***.***>
|
Beta Was this translation helpful? Give feedback.
-
|
Hi Peter, David explained about your question. If you want to learn the python basics you might look at W3schools python tutorial There are differences between python (at in this tutorial), pybricks and micropython. Bert |
Beta Was this translation helpful? Give feedback.
-
|
Hi Bert and Eric,
Thanks for your support during the initial days of coding for my project.
Essentially, the project code was written in 8 sections, all of which
individually work correctly, but when merged into a single source code,
produces a Memory Error that prevents the program from running.
When 7 modules are loaded and mem_info(1) printed, it shows the following
stack: 460 out of 5180
GC: total: 8384, used: 5392, free: 2992
No. of 1-blocks: 62, 2-blocks: 5, max blk sz: 84, max free sz: 129
GC memory layout; from 20004700:
00000000: h=ThhDh======LhLhhh=====================================h=====h=
00000400: =======h================h===============================h=======
00000800: ======================================================SSSSSSSSSS
00000c00: h====hhhhhhhhhhhhhhhhhhhhhhBTTT=TTTTLTT=h=Dh====hSLhLhLhh=hhh...
00001000: ................................................................
00001400: ..............................................................h=
00001800: ================================================================
00001c00: ==================..............................................
00002000: ............
When adding the 8th module of around 100 lines, comprising mostly of 'IF'
statements AND deleting the
"from micropython import const, opt_level, mem_info, qstr_info, stack_use"
call, there is not quite enough memory.
Seems I can delete about 15 lines and all is well.
Given the stack is 460 out of 5180, does this mean there is a lot of space
left in memory somewhere?
Can this space be allocated to Code Space?
What does GC mean and 2992 free /5392 sounds like there is room to grow!
Am I really running out of Code Space or is the memory for variables
running short?
I did add UNDERSCORE to variable names and CONST() to all the constants.
Can you point me to some documentation around this and especially what is
meant by the GC and ThDSLB=. etc
Also, can the Web-Based PyBricks be replaced with an App/Program for
developing code?
I feel the web-based coding leaves me a little unsure at times, especially
when testing. It seems every line of code needs to be executed when
testing, as run-time errors occur for the simplest of errors, such as
mispelling a variable name. The compiler does not pick up on the error
until it is actually executed. This was one of the main reasons to also
segment the code, to allow for comprehensive testing before moving to the
next function
Finally, I would like my code to execute as soon as power is applied to the
hub when standalone, making operation similar to a standard Lego hub. Can
you provide information on how that can be achieved?
Apologies for the lengthy email and the direct contact, but trying to find
answers to all these questions on Google / Github has been challenging.
Regards
Peter
…On Thu, Mar 27, 2025 at 1:15 AM Bert ***@***.***> wrote:
Hi,
On beta.pybricks.com I made this small program:
from pybricks.parameters import Button
pressed = [Button.LEFT_MINUS]was_pressed = [Button.CENTER]if Button.LEFT_MINUS in pressed and Button.LEFT_MINUS not in was_pressed:
print("OK")
And that runs fine.
Might there be a syntax error or the line *before* this line?
Sometimes you get a syntax error reported on the line after the real
problem.
Maybe with non matching quotes or parens.
Bert
—
Reply to this email directly, view it on GitHub
<#2137 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/BQPX7BBUCCSWYOHEH6LSTM32WKSADAVCNFSM6AAAAABZ2AX5YKVHI2DSMVQWIX3LMV43URDJONRXK43TNFXW4Q3PNVWWK3TUHMYTENRSHEYTOMA>
.
You are receiving this because you authored the thread.Message ID:
***@***.***>
|
Beta Was this translation helpful? Give feedback.
-
|
Hi Peter, I only comment on the memory problem you have. Pybricks is using micropython because of the memory constraints. A practical list of techniques to try when you get "memory error" during compilation:
Bert |
Beta Was this translation helpful? Give feedback.
-
|
Hi Bert,
Thanks for the 5 tips, all of which were implemented with success!
The one aspect that I struggled with was the function call and the CityHub.
It seemed that for every function, I had to allocate the hub, as in Hub =
CityHub(parameters) . There were separate functions to control the LED, the
Bluetooth, the Button Press and Port Allocation.
The original code listened to 8 channels, so every re-allocation of the hub
consumed a lot of memory.
I even tried to set the hub parameters to null before exiting the function,
but I still kept running out of memory.
Eventually, the code operated on one frequency only and achieved listening
across all 8 channels inside a loop, resulting in 8 separate and
sequential Hub = CityHub (parameters) allocations.
Ultimately, once I worked out the 8 listening frequencies was the real
issue, I was able to simplify the code again, with no function calls
involving the Hub, and everything fell into place!
The program worked in the code and memory space that was available!
Cheers
Peter
…On Mon, Apr 7, 2025 at 5:47 PM Bert ***@***.***> wrote:
Hi Peter,
I only comment on the memory problem you have.
Pybricks is using micropython because of the memory constraints.
How much memory is available depends also on the type of hub.
A practical list of techniques to try when you get "memory error" during
compilation:
1. Remove unnecessary imports - Only import what you actually use
2. Reduce string literals - Especially long strings or many unique
strings
3. Reduce long variable and long function names
4. Consolidate duplicate code into functions
5. Simplify complex if/elif chains - Consider dictionaries or lookup
tables instead
Bert
—
Reply to this email directly, view it on GitHub
<#2137 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/BQPX7BDFJXGI6N4HGC4GWK32YIUS3AVCNFSM6AAAAABZ2AX5YKVHI2DSMVQWIX3LMV43URDJONRXK43TNFXW4Q3PNVWWK3TUHMYTENZUG42DCMI>
.
You are receiving this because you authored the thread.Message ID:
***@***.***>
|
Beta Was this translation helpful? Give feedback.
-
|
Hi Bert, Still having one issue. Perhaps you can help? But I will also post up on Q&A? The issue is an out of range Remote Control. (More correctly the train goes out of range and then comes back) The error message should have been "OSerror - <class 'OSError'> 116" Is this a known issue? Regards |
Beta Was this translation helpful? Give feedback.
-
|
Answers to your questions: I spent more time diagnosing the issue and refining the test scenario. The bench test: Power up the hub, connect to the web portal and run the code. You can confirm the code is operational just by depowering and repowering the remote control at the bench. Then, with remote-control blinking Red/Yellow and in hand, just walk away from the hub. At this point wait 12 seconds for the remote-control to power off completely. If the lights resume flashing Red/Yellow, the recovery has worked, so turn around and walk out-of-range again to repeat. from pybricks import version
from pybricks.iodevices import PUPDevice
from pybricks.parameters import Button, Color, Direction, Port
from uerrno import ENODEV
from pybricks.hubs import CityHub
from pybricks.pupdevices import Remote
from pybricks.tools import wait
def Remote_LED(value):
try:
my_remote.light.on(value)
except OSError:
print("Remote Control -> Out of range")
Recover_Remote_Control()
def Recover_Remote_Control():
global my_remote
global _Remote_Name
while True:
try:
print("Disconnect")
my_remote.disconnect()
except OSError:
print("D-Error")
try:
print("Reconnecting")
my_remote = Remote(name=_Remote_Name, timeout=2000)
print("\n", "Remote Control -> ACTIVE", "\n", "name = ", _Remote_Name)
return
except OSError as error:
print("OSerror - ", OSError, error.errno)
print ("\n", version)
my_remote = Remote()
_Remote_Name = my_remote.name()
print("\n", "Remote Control -> ACTIVE", "\n", "name = ", _Remote_Name, "\n" )
while True:
Remote_LED (Color.RED)
wait (500)
Remote_LED (Color.YELLOW)
wait (500) |
Beta Was this translation helpful? Give feedback.
-
|
I have a separate reconnect script which I used for tests and it seems more robust but it did do what you say once out of 10-15 tests. I guess that if Remote() tries to run when the signal is still borderline out of range it starts but cannot complete the handshake with the remote. And perhaps that is not being caught? This then crashes/blocks the program and no amount of remote restarts will work. I can power up closer to the hub 100% over and over without failing. I just pull a battery out of the remote and wait a few seconds. I tested this 100 times when I first wrote the script. Have you tried crashing the very first connection ? By running the script then switching the remote on for the first time at some distance? Would imagine the same scenario. Here is my script , maybe do your same tests with it - but I suspect it makes no difference if the reason is a failed handshake by Remote() when the connection is very weak: |
Beta Was this translation helpful? Give feedback.
-
|
HiThe app crashes / or hangs - I think that is clear I am wondering if the Lego firmware / pu app has the same issue ? If so then it comes down to hardware limitations no ? PSent from my iPhoneOn 6 May 2025, at 12:22, Bert ***@***.***> wrote:
Hi Paul,
Did the same test with your script.
hub connected via app beta.pybricks.com
start hub program via app
connect RC
walk away until RC complains and switches off
re-connect RC at the same location or one step closer
reconnect succeeds but not always.
if no reconnect the hub seems connected to the app, but does not react upon it
hub does not stop running on short button press
need to long-press button to power-off.
—Reply to this email directly, view it on GitHub, or unsubscribe.You are receiving this because you commented.Message ID: ***@***.***>
|
Beta Was this translation helpful? Give feedback.
-
|
If you reconnect much closer does it still stall occasionally ? PSent from my iPhoneOn 6 May 2025, at 12:22, Bert ***@***.***> wrote:
Hi Paul,
Did the same test with your script.
hub connected via app beta.pybricks.com
start hub program via app
connect RC
walk away until RC complains and switches off
re-connect RC at the same location or one step closer
reconnect succeeds but not always.
if no reconnect the hub seems connected to the app, but does not react upon it
hub does not stop running on short button press
need to long-press button to power-off.
—Reply to this email directly, view it on GitHub, or unsubscribe.You are receiving this because you commented.Message ID: ***@***.***>
|
Beta Was this translation helpful? Give feedback.
-
|
Have you tried “await Exception as err “ to catch everything, in case not an OSError ? PSent from my iPhoneOn 6 May 2025, at 13:48, Bert ***@***.***> wrote:
With the original script I never had a problem reconnecting up close.
What wonders me is that the UI app still thinks the hub is connected.
—Reply to this email directly, view it on GitHub, or unsubscribe.You are receiving this because you commented.Message ID: ***@***.***>
|
Beta Was this translation helpful? Give feedback.
-
|
Sorry I meant "except Exception as err" not await 😵💫PSent from my iPhoneOn 6 May 2025, at 13:48, Bert ***@***.***> wrote:
With the original script I never had a problem reconnecting up close.
What wonders me is that the UI app still thinks the hub is connected.
—Reply to this email directly, view it on GitHub, or unsubscribe.You are receiving this because you commented.Message ID: ***@***.***>
|
Beta Was this translation helpful? Give feedback.
-
|
Yes - is it something that has to be done at a lower level than the running app /upython can deal with maybe ? Anyway of knowing how Lego PU deals with this ?PSent from my iPhoneOn 6 May 2025, at 23:37, David Lechner ***@***.***> wrote:
If it is reproducible, sure. But it sounds like it would be best if we just made the remote automatically reconnect so that users don't have to do that in their program.
—Reply to this email directly, view it on GitHub, or unsubscribe.You are receiving this because you commented.Message ID: ***@***.***>
|
Beta Was this translation helpful? Give feedback.
-
|
Thanks to those who took the time to reply and even confirm my findings! So a reliable re-connect is important, so as to take control of the train again. Any timeframe available for a firmware fix or patch? |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
This is what the doco says...
But that throws an 'invalid syntax' error.
Is that function still available, but named/called something else?
Beta Was this translation helpful? Give feedback.
All reactions