Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug] Sensor's only works once. Hub needs restart to make them work again #456

Closed
Vinz1911 opened this issue Aug 28, 2021 · 21 comments
Closed
Labels
bug Something isn't working platform: Powered Up Issues related to LEGO Powered Up software: pybricks-micropython Issues with Pybricks MicroPython firmware (or EV3 runtime) topic: sensors Issues involving sensors
Milestone

Comments

@Vinz1911
Copy link

Vinz1911 commented Aug 28, 2021

Describe the bug
I tried the following on the Inventor Hub with the ColorSensor and the UltraSonic Sensor:
Version: ('primehub', '3.1.0a3', 'v3.1.0a3-62-g51bdd097 on 2021-08-26')

Problem: Sensor's only works on first run of the script after stopping the script and run it again, the sensor's are not working.
After restart of the hub, the sensor's work again for the first run and then the problem appears again.

To reproduce
Steps to reproduce the behavior:
Just run this script, stop it and run it again:

from pybricks.pupdevices import ColorSensor, UltrasonicSensor
from pybricks.parameters import Port
from pybricks.tools import wait
from pybricks import version

# Initialize the sensor.
sensor = UltrasonicSensor(Port.C)
sensor.lights.on(100)

while True:
    distance = sensor.distance()
    print(distance)
    wait(100)

Expected behavior
Sensor's work every time the script is running

@Vinz1911 Vinz1911 added the triage Issues that have not been triaged yet label Aug 28, 2021
@dlech dlech added bug Something isn't working platform: Powered Up Issues related to LEGO Powered Up software: pybricks-micropython Issues with Pybricks MicroPython firmware (or EV3 runtime) topic: sensors Issues involving sensors hub: primehub/inventorhub Issues related to the LEGO SPIKE Prime hub and LEGO MINDSTORMS Robot Invetor hub and removed triage Issues that have not been triaged yet labels Aug 28, 2021
@BertLindeman
Copy link

BertLindeman commented Aug 28, 2021

@Vinz1911 I tried this on my cityhub (as it was next to me) with an UltraSonic sensor:
What I noticed:
First run after hub boot and BT connect running as expected.
Stop run and start the program again:

  • Sensor light does not go ON
  • measured distance is constant 2000

After disconnect and re-boot the hub:
the first run is OK.

{edit typo]

@dlech dlech removed the hub: primehub/inventorhub Issues related to the LEGO SPIKE Prime hub and LEGO MINDSTORMS Robot Invetor hub label Aug 28, 2021
@dlech dlech added this to the v3.1 milestone Aug 28, 2021
@Vinz1911
Copy link
Author

Hi @BertLindeman

Exactly, thats also the behaviour i discovered. With the Color Sensor it shows constantly Color.NONE

@BertLindeman
Copy link

BertLindeman commented Aug 28, 2021

@Vinz1911 I used ambient on the color sensor and THAT keep working the second run.
My current program:

print(version)
from pybricks.pupdevices import ColorSensor, UltrasonicSensor
from pybricks.parameters import Port
from pybricks.tools import wait
from pybricks import version

# Initialize the sensor.
sensorA = UltrasonicSensor(Port.A)
sensorA.lights.on(60)
sensorB = ColorSensor(Port.B)
sensorB.lights.on(60)
i = 0
while True:
    # distance = sensor.distance()
    # print(distance)
    i += 1
    sensorA.lights.on(i)
    sensorB.lights.on(60)
    print(i, sensorA.distance(), sensorB.ambient())
    wait(500)

Part of the second run:

('cityhub', '3.1.0a3', 'v3.1.0a3-60-ga2b2e44e on 2021-08-18')
1 2000 0.2
2 2000 0.2
3 2000 0.2
4 2000 0.1
5 2000 0.0

[edit: forgot to paste the program]

@Vinz1911
Copy link
Author

Vinz1911 commented Aug 31, 2021

Just as Info:

I discovered that you don't need to use the sensor's in Code. You can also run the default main script delivered with the Firmware. Press Start, then Stop will disable the Sensor (Light's Go off) and then they stay off.

@laurensvalk
Copy link
Member

Thanks for opening this issue. This used to be fixed, but it sounds like it has come back.

If you want to help, it would be great to know when this started happening by going through older builds.

Which was the last build that worked? It's usually best to make big jumps first (like going back a couple of weeks), and then make smaller steps as you get closer.

I discovered that you don't need to use the sensor's in Code. You can also run the default main script delivered with the Firmware. Press Start, theb Stop will disable the Sensor (Light's Go off) and then they stay off.

This is good, because it's relatively quick to see whether it was still working for a particular version.

The probably happens because we turn off power to all ports at the end of a program. There is a check to skip this for LEGO sensors with particular properties, but apparently this is no longer being checked.

@Vinz1911
Copy link
Author

Hi Laurens,

Yes sure, i will test it later today and give you feedback here 😊

@BertLindeman
Copy link

BertLindeman commented Aug 31, 2021

Intermediate result, so we do not do all the same ;-)

firmware from Build #1221 has the error.
Version ('technichub', '3.1.0a3', 'v3.1.0a3-60-ga2b2e44e on 2021-08-18')

firmware from Pre release v3.1.0a1 is OK.
Version ('technichub', '3.1.0a1', '3ab93ae on 2021-06-23')

firmware from Pre release v3.1.0a3 has the error.
Version ('technichub', '3.1.0a3', 'v3.1.0a3 on 2021-07-19')

[edit:
will add to this comment as I go along..
will strike through irrelevant versions]

@Vinz1911
Copy link
Author

Vinz1911 commented Aug 31, 2021

My Results:

works fine with (also tested all functions of the sensors):
('primehub', '3.1.0a1', '3ab93aef on 2021-06-23')

same with the alpha2
('primehub', '3.1.0a2', 'v3.1.0a2 on 2021-07-06')

and broke with the alpha3
('primehub', '3.1.0a3', 'v3.1.0a3 on 2021-07-19')

so anything broke with the alpha3 version, all version before seems to work very well

@laurensvalk
Copy link
Member

laurensvalk commented Aug 31, 2021

Thank you both!

Bonus points and eternal gratitude shall be awarded for finding the specific commit in between those releases 😄

@Vinz1911
Copy link
Author

we will do our best 😜

@Vinz1911
Copy link
Author

Here we go:

latest working one:
('primehub', '3.1.0a2', 'v3.1.0a2-4-gde278587 on 2021-07-07')

and then broke with:
('primehub', '3.1.0a2', 'v3.1.0a2-5-g4d6d1724 on 2021-07-13')

pbio/servo: Make angle reset optional.
Build #1161: Commit 4d6d172 pushed by laurensvalk

@laurensvalk
Copy link
Member

Thanks guys! I'll look into this and make sure to fix it.

@laurensvalk
Copy link
Member

This is also a good reminder to run our physical tests from time to time. We probably need to document this somewhere.

@laurensvalk
Copy link
Member

This used to be fixed, but it sounds like it has come back.

pybricks/pybricks-micropython@baada48

This caused powered sensors to be reset and not turned back on after
a program completed. It would be better to review this logic for the
next release instead of breaking the release candidate.

We'll probably have to have a closer look at this so we don't break it once again.

@BertLindeman
Copy link

This is also a good reminder to run our physical tests from time to time. We probably need to document this somewhere.

And this issue shows that running a test only once is not enough.

@laurensvalk
Copy link
Member

The run_all runs all scripts, so it would have caught this if we actually ran it.

@laurensvalk
Copy link
Member

laurensvalk commented Aug 31, 2021

I've found the underlying issue and a workaround. Don't worry too much about understanding the jargon below.

Deep down, the problem is that pbdrv_counter_get_dev succeeds even when there isn't a motor attached. This causes the motor process logic to think there's a motor, so the power is turned off.

This used to be fine, because before pybricks/pybricks-micropython@4d6d172, we always made at least one call to pbio_tacho_get_count during reset, which does fail correctly.

The workaround is to always make that call anyway. We were considering revising device management and pbio more generally, but probably not for the upcoming release. The workaround may have to do, unless we want to update pbdrv_counter_get_dev to return the expected error.

laurensvalk added a commit to pybricks/pybricks-micropython that referenced this issue Aug 31, 2021
pbdrv_counter_get_dev succeeds even when there isn't a counter, so we have to do at least one read to ensure there is a tacho motor.

Fixes pybricks/support#456
laurensvalk added a commit to pybricks/pybricks-micropython that referenced this issue Aug 31, 2021
pbdrv_counter_get_dev succeeds even when there isn't a counter, so we have to do at least one read to ensure there is a tacho motor.

Fixes pybricks/support#456
@laurensvalk
Copy link
Member

When pybricks/pybricks-micropython@7a24497 finishes building, you can verify if this fixes it.

@BertLindeman
Copy link

Can confirm: OK running ('technichub', '3.1.0a4', 'v3.1.0a4-3-g7a24497d on 2021-08-31')

Have problems downloading this firmware (using code.pybricks.com and beta.pybricks.com) to both my cityhub and movehub. So it took more time than I wanted.
While downloading the default firmware (just press the install firmware button) goes OK.

Anyway fix works.

@Vinz1911
Copy link
Author

Thank you Laurens,

everything works now (again) es expected 👍

@laurensvalk
Copy link
Member

laurensvalk commented Aug 31, 2021

Thanks both of you for reporting and verifying! This is a big help, I appreciate it.

I'll keep the issue open until it's merged to master.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working platform: Powered Up Issues related to LEGO Powered Up software: pybricks-micropython Issues with Pybricks MicroPython firmware (or EV3 runtime) topic: sensors Issues involving sensors
Projects
None yet
Development

No branches or pull requests

4 participants