Skip to content

IR sensor doesn't respond to toggling off the "beacon" button #498

Closed

Description

@Uldiniad
  • ev3dev version: 4.14.58-ev3dev-2.2.1-ev3
  • ev3dev-lang-python version: python3-ev3dev2

Code: https://gist.github.com/Uldiniad/2608aa8e4a152fe443b151f0d6c5e08b
Output:

// Clicked beacon (runs medium_motor forever)
WARNING:root:State: True
WARNING:root:Running hand_motor forever
// Pressed touch sensor (stops medium_motor)
WARNING:root:Touch sensor pressed. Stopping hand_motor
WARNING:root:Touch sensor pressed. Stopping hand_motor
WARNING:root:Touch sensor pressed. Stopping hand_motor
WARNING:root:Touch sensor pressed. Stopping hand_motor
WARNING:root:Touch sensor pressed. Stopping hand_motor
WARNING:root:Touch sensor pressed. Stopping hand_motor
WARNING:root:Touch sensor pressed. Stopping hand_motor
WARNING:root:Touch sensor pressed. Stopping hand_motor
WARNING:root:Touch sensor pressed. Stopping hand_motor
WARNING:root:Touch sensor pressed. Stopping hand_motor
WARNING:root:Touch sensor pressed. Stopping hand_motor
// Press any other button than beacon (runs large motor until released)
WARNING:root:State: False **//anomaly: large motors should not be in auto_home nor its internals**
// Clicked beacon again (runs medium_motor forever)
WARNING:root:State: True
WARNING:root:Running hand_motor forever
// Pressed touch sensor again (stops medium_motor)
WARNING:root:Touch sensor pressed. Stopping hand_motor
WARNING:root:Touch sensor pressed. Stopping hand_motor
WARNING:root:Touch sensor pressed. Stopping hand_motor
WARNING:root:Touch sensor pressed. Stopping hand_motor
// Pressed beacon again, nothing happens **anomaly: every time beacon is pressed it should execute move(state)*. The only way to have beacon run the medium_motor forever again is to execute the sequence to trigger the first anomaly*

Activity

  1. Uldiniad commented on Aug 17, 2018

    @Uldiniad
    Author

    @WasabiFan opened as requested

  2. WasabiFan commented on Aug 17, 2018

    @WasabiFan
    Member

    What's the behavior you are seeing (physically with the motors)? It seems to me from the log that it does what you expect -- the hand motor starts when you press the button on the remote and stops when the touch sensor is pressed. Which part of that isn't working?

  3. Uldiniad commented on Aug 17, 2018

    @Uldiniad
    Author

    The hand motor doesn't always start when beacon is pressed. As described in the second anomaly, if started and stopped, it will not start again until I execute the first anomaly (which is that I have to run a large motor with any other button and enters without permission nor call the move(state) in auto_home which is only assigned to beacon

  4. WasabiFan commented on Aug 17, 2018

    @WasabiFan
    Member

    OK, I see what you're saying (I think); the beacon stops firing events after the first or second usage. I'll take a look later today and see if I can repro.

  5. Uldiniad commented on Aug 17, 2018

    @Uldiniad
    Author

    yes, that's correct, and instead the other buttons on the same channel fire that event which then allows beacon to fire its event again... 🤷‍♂️

  6. WasabiFan commented on Aug 19, 2018

    @WasabiFan
    Member

    I just pulled out my IR sensor and remote to try this out, When I press one of the up/down buttons, the light that indicates it's sending a signal lights up for a bit then turns off. But when I press the button in the top-center (which I believe is the one you are referring to here), it turns on and doesn't turn off. So, it seems that the built-in behavior of the remote is that the top-center button is a toggle rather than a "hold". That button de-activates when another button is pressed or when you press it again.

  7. dwalton76 commented on Aug 19, 2018

    @dwalton76
    Collaborator

    yep that button toggles the IR beacon on/off

  8. Uldiniad commented on Aug 20, 2018

    @Uldiniad
    Author

    @WasabiFan indeed. as dwalton said it is the intended behaviour of the beacon button. problem is that toggling it off and back on again does not run the medium_motor again unless you click the other up/down buttons

  9. WasabiFan commented on Aug 20, 2018

    @WasabiFan
    Member

    Ok, I had assumed you were just mis-understanding the behavior of the beacon 😆

    This turned out to be a bit of a Schrödinger's cat situation, but after a bit of head-scratching at 20-past-midnight I've localized the problem: a peculiar interaction between setting the mode of the sensor rapidly and the driver responding to updates.

    @dlech:

    If I have an IR sensor plugged in and do the following (with appropriate path, of course):

    echo "IR-REMOTE" > /sys/class/lego-sensor/sensor3/mode
    while true; do cat /sys/class/lego-sensor/sensor3/value0; done

    I notice something (which isn't inherently wrong, but might help in debugging the observations that follow): When I toggle the beacon button off, it takes a second or two for the console output to reflect that. Other buttons are generally immediate.

    Now, after toggling the button a few times and observing that it is responsive, I run this in a separate window simultaneously:

    while true; do echo "IR-REMOTE" > /sys/class/lego-sensor/sensor3/mode; done

    With this running, the button data no longer behaves the same. "9" (for the beacon button) never resets to 0 after toggling the button off. The other buttons still work. To put it differently, setting the sensor mode frequently makes that particular button report turning on but never off. In the case of our Python library, setting the mode is always done immediately before reading the sensor value to ensure the correct behavior.

    Another observation: It appears that setting the mode is faster when the beacon is toggled on. Not sure how that's involved here.

    @dlech Do you have any idea what might be going on here? I know it's a UART sensor, so AFAIK setting the mode translates directly to a serial write... is this a behavior of the sensor itself rather than the ev3dev driver? I find it hard to imagine why our driver would treat that button differently otherwise.

  10. dlech commented on Aug 20, 2018

    @dlech
    Member

    When I toggle the beacon button off, it takes a second or two for the console output to reflect that.

    Looks like I observed the same thing way back when: http://docs.ev3dev.org/projects/lego-linux-drivers/en/ev3dev-stretch/sensor_data.html#lego-ev3-ir-mode3-value0

    setting the mode translates directly to a serial write... is this a behavior of the sensor itself rather than the ev3dev driver

    This is intentional on the part of the driver. Some (very few) modes (e.g. the oneshot ultrasonic sensor mode US-SI-CM) won't update the value return unless the mode is set.

    Do you have any idea what might be going on here?

    Are there any error messages in dmesg? I seem to vaguely recall someone else reporting temperamental behavior of the LEGO EV3 IR sensor, but I have no idea where that might be.

  11. WasabiFan commented on Aug 20, 2018

    @WasabiFan
    Member

    This is intentional on the part of the driver. Some (very few) modes (e.g. the oneshot ultrasonic sensor mode US-SI-CM) won't update the value return unless the mode is set.

    I am not sure if you answered the question better than I expected or if I didn't ask what I thought I was asking; it should have instead been:

    I know it's a UART sensor, so AFAIK setting the mode translates directly to a serial write... is the lack of a change after releasing the button a behavior of the sensor itself rather than the ev3dev driver?


    Are there any error messages in dmesg? I seem to vaguely recall someone else reporting temperamental behavior of the LEGO EV3 IR sensor, but I have no idea where that might be.

    There were some buffer overrun errors earlier in the log (I don't think they were related) but none after plugging in the IR sensor. I can do a more thorough check with a reboot later today.

  12. dlech commented on Aug 20, 2018

    @dlech
    Member

    I think the delay after toggling the beacon button off is a feature of the sensor. If the value gets stuck on and won't toggle back off, then that sounds like a hardware bug.

  13. WasabiFan commented on Aug 20, 2018

    @WasabiFan
    Member

    OK... so it sounds like we need to work around this by not setting the mode every time. Perhaps this is a good time to revisit the policy of always setting the mode before reading (which results in what I believe is a significant perf hit).

  14. changed the title [-]rc_tank.py: inheriting/expanding class behaves incorrectly[/-] [+]IR sensor doesn't respond to toggling off the "beacon" button[/+] on Aug 31, 2018
  15. WasabiFan commented on Sep 3, 2018

    @WasabiFan
    Member

    While I'm ruminating on a proper fix, here's a workaround for anyone hitting the same issue:

    import time
    
    # Don't import InfraredSensor here
    from ev3dev2.sensor import Sensor
    import ev3dev2.sensor.lego
    
    # Workaround for https://github.com/ev3dev/ev3dev-lang-python/issues/498
    class InfraredSensor(ev3dev2.sensor.lego.InfraredSensor):
        def __init__(self, address=None, name_pattern=Sensor.SYSTEM_DEVICE_NAME_CONVENTION, name_exact=False, **kwargs):
            super(InfraredSensor, self).__init__(address, name_pattern, name_exact, **kwargs)
    
        @ev3dev2.sensor.lego.InfraredSensor.mode.setter
        def mode(self, value):
            if self.mode != value:
                super(InfraredSensor, self.__class__).mode.fset(self, value)
    
    
    # Demo of working sensor
    ir = InfraredSensor()
    
    def beacon_action(state):
        print("beacon: %s" % state)
    ir.on_channel1_beacon = beacon_action
    
    print("Watching on channel 1 for beacon button...")
    while True:
        ir.process()
        time.sleep(0.01)

    Modify the imports in your script to remove the InfraredSensor import and add the two above, then paste in that new class. It should be a drop-in replacement.

  16. Uldiniad commented on Sep 13, 2018

    @Uldiniad
    Author

    thanks @WasabiFan much appreciated

  17. WasabiFan commented on Sep 13, 2018

    @WasabiFan
    Member

    Yeah, thanks for doing the initial investigation. FWIW until we release the package with this change the workaround above is still the best way to go.

  18. WasabiFan commented on Sep 24, 2018

    @WasabiFan
    Member

    Fix released in 2.0.0~beta2. To upgrade:

    sudo apt-get update
    sudo apt-get install --only-upgrade python3-ev3dev2
    

    The workaround posted above can be removed from user code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions