Repository navigation
Optimistically check the mode of the sensor before setting it on read - #502
Conversation
| 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. |
There was a problem hiding this comment.
There isn't an obvious easy fix here so I'm inclined to just leave this as-is; open to suggestions.
There was a problem hiding this comment.
We could monkey patch the sensor class when on Travis.
| @property | ||
| def distance_centimeters_ping(self): | ||
| self.mode = self.MODE_US_SI_CM | ||
| self._ensure_mode(self.MODE_US_SI_CM) |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Thanks; that's a very good point. I'll revert these two back to just setting the mode directly and copy over the note.
| @property | ||
| def distance_inches_ping(self): | ||
| self.mode = self.MODE_US_SI_IN | ||
| self._ensure_mode(self.MODE_US_SI_IN) |
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):
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):
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