Skip to content

Optimistically check the mode of the sensor before setting it on read - #502

Merged
WasabiFan merged 5 commits into
ev3dev-stretchfrom
set-mode-when-needed
Sep 12, 2018
Merged

WasabiFan merged 5 commits into
ev3dev-stretchfrom
set-mode-when-needed

Conversation

@WasabiFan

Copy link
Copy Markdown
Member

As we've recently discovered, some sensors don't respond well to having their mode set repeatedly1. Here's my proposed fix, which I think simultaneously improves sensor operation all-around.

1: My guess is that this was intentional on LEGO's part; the beacon button in particular has a delay on it, presumably added on the sensor side to be resilient to momentary losses of line-of-sight. That timer is probably reset every time we write the mode.


I did some benchmarking with an IR sensor (UART):

robot@ev3dev:~/ev3dev-lang-python$ python3 -m timeit -s "import ev3dev2.sensor; s = ev3dev2.sensor.Sensor(ev3dev2.sensor.INPUT_3)" "s.mode"
1000 loops, best of 3: 975 usec per loop

robot@ev3dev:~/ev3dev-lang-python$ python3 -m timeit -s "import ev3dev2.sensor; s = ev3dev2.sensor.Sensor(ev3dev2.sensor.INPUT_3)" "s.mode = 'IR-REMOTE'"
10 loops, best of 3: 97.8 msec per loop

In other words, reading the mode takes around 1ms, and writing takes 100x that. This is generally what I would expect, given that setting the mode on a UART sensor always requires writing to the serial line and might even wait for a response... I haven't looked into it past a cursory glance at the top-level "set" method. I believe reading is just a memory read (+ sysfs overhead).

For comparison, here's an analog sensor (the only one IIRC), which only has one mode and presumably no-ops when setting the mode anyway (I didn't bother looking up the driver code for this one):

robot@ev3dev:~/ev3dev-lang-python$ python3 -m timeit -s "import ev3dev2.sensor; s = ev3dev2.sensor.Sensor(ev3dev2.sensor.INPUT_3)" "s.mode"
1000 loops, best of 3: 834 usec per loop

robot@ev3dev:~/ev3dev-lang-python$ python3 -m timeit -s "import ev3dev2.sensor; s = ev3dev2.sensor.Sensor(ev3dev2.sensor.INPUT_3)" "s.mode = 'TOUCH'"
1000 loops, best of 3: 827 usec per loop

So, this one is around 1ms for both operations. I'm going to guess that this is just sysfs file I/O overhead.

I didn't see much use in finding an I2C sensor, given that pretty much all of the EV3 ones are UART. It's probably somewhere in between.


What this tells me is that we wait 100ms every time you read a sensor, for no good reason. Given that reading the mode is only 1ms, it seems reasonable to me that we should do the read and only write if we need it to change. In most cases, that's probably the right assumption. Technically, this does introduce a potential race condition, but I think the risk is minor.

I've included that in this PR, and added some related tests.

Fixes #498

@WasabiFan
WasabiFan requested a review from ddemidov September 6, 2018 02:29
Comment thread tests/api_tests.py
self.assertEqual(s.mode, "IR-REMOTE")

val = s.proximity
# Our test environment writes to actual files on disk, so while "seek(0) write(...)" works on the real device, it leaves trailing characters from previous writes in tests. "s.mode" returns "IR-PROXTE" here.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There isn't an obvious easy fix here so I'm inclined to just leave this as-is; open to suggestions.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could monkey patch the sensor class when on Travis.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 Done

@ddemidov ddemidov left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This makes a lot of sense.

Comment thread ev3dev2/sensor/lego.py Outdated
@property
def distance_centimeters_ping(self):
self.mode = self.MODE_US_SI_CM
self._ensure_mode(self.MODE_US_SI_CM)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This mode is special. The value does not update unless you set the mode. So this change will cause the sensor the become unresponsive after the first call.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

interesting, it also appears that this mode has a problem with locking up the sensor if you poll it too often. http://docs.ev3dev.org/projects/lego-linux-drivers/en/ev3dev-stretch/sensor_data.html#lego-ev3-us-mode3-value0

It would be worth copying that note to the python docs if it isn't there already

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks; that's a very good point. I'll revert these two back to just setting the mode directly and copy over the note.

Comment thread ev3dev2/sensor/lego.py Outdated
@property
def distance_inches_ping(self):
self.mode = self.MODE_US_SI_IN
self._ensure_mode(self.MODE_US_SI_IN)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto on this mode.

@WasabiFan
WasabiFan merged commit be77ea3 into ev3dev-stretch Sep 12, 2018
@WasabiFan
WasabiFan deleted the set-mode-when-needed branch September 15, 2018 08:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

3 participants