Repository navigation
Closed
Description
- ev3dev version: 4.4.19-15-ev3dev-ev3
- ev3dev-lang-python version: 0.7.0
I'm getting odd errors while attempting to set the delay_on/delay_off attributes for use with the timer trigger. For example:
>>> Leds.set(Leds.LEFT, brightness_pct=1, trigger="timer", delay_on=2000)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python3/dist-packages/ev3dev/ev3.py", line 90, in set
setattr(led, k, kwargs[k])
File "/usr/lib/python3/dist-packages/ev3dev/core.py", line 2051, in delay_on
self._delay_on = self.set_attr_int(self._delay_on, 'delay_on', value)
File "/usr/lib/python3/dist-packages/ev3dev/core.py", line 213, in set_attr_int
return self._set_attribute(attribute, name, str(int(value)))
File "/usr/lib/python3/dist-packages/ev3dev/core.py", line 202, in _set_attribute
attribute.write(value.encode())
io.UnsupportedOperation: File not open for writing
I have also seen a different error, which I can no longer reproduce:
>>> Leds.set(Leds.LEFT, brightness_pct=1, trigger="timer", delay_off=100)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python3/dist-packages/ev3dev/ev3.py", line 90, in set
setattr(led, k, kwargs[k])
File "/usr/lib/python3/dist-packages/ev3dev/core.py", line 2065, in delay_off
self._delay_off = self.set_attr_int(self._delay_off, 'delay_off', value)
File "/usr/lib/python3/dist-packages/ev3dev/core.py", line 213, in set_attr_int
return self._set_attribute(attribute, name, str(int(value)))
File "/usr/lib/python3/dist-packages/ev3dev/core.py", line 202, in _set_attribute
attribute.write(value.encode())
OSError: [Errno 19] No such device
The first one (and likely the second) seem to be related to the file handle cache, but I can't quite tell. @ddemidov IIRC you have had a fair amount of experience with the caching system; thoughts?
I can write to these attributes without issue from Bash.
Activity
ddemidov commentedon Oct 21, 2016
Would that work with
? The
delay_{on,off}attributes are only created whentriggeris set totimer, and there is some delay in between.WasabiFan commentedon Oct 21, 2016
Unfortunately, that doesn't fix it; even giving it a few whole seconds manually doesn't resolve the problem.
ddemidov commentedon Oct 21, 2016
Ok, will look into it this weekend.
ensonic commentedon Oct 21, 2016
I am applying the same workaround:
https://github.com/OpenRoberta/robertalab-ev3dev/blob/develop/roberta/ev3.py#L191-L200
Let me re-test here too.
ddemidov commentedon Oct 21, 2016
works for me. But, when I do
I get the same error as you (File not open for writing). And after that, any attempt to change the
delay_onattribute results in the same error. So it does look like a bug in cache implementation.ddemidov commentedon Oct 21, 2016
I think I know what happens. Device._attribute_file_open() checks permissions on the requested file and opens it in appropriate mode ('r' for readonly, 'w' for write-only, 'r+' for read-write). After
triggeris set to 'timer', new attributes, 'delay_on'/'delay_off' are created, but initially those have read-only access, and after some delay the access is changed to read-write. So when you set bothtriggeranddelay_onin the same call, the file is opened in incorrect access mode.I guess we could change the implementation of Device._set_attribute() to attempt a file reopen on failure, but since this should only be needed for the
Ledclass, and should result in an error at first attempt anyway, I am inclined to mark this as 'wontfix'.WasabiFan commentedon Oct 21, 2016
Nice sleuthing! That cause makes sense.
Honestly, I don't think we can call this wontfix and move on. We expect most beginners to be using Python and this library, and that demographic can't really handle having to work around special cases like this, Whereas more experienced programmers would probably be fine with just a note in the docs, I don't think that beginners would be able to use the functionality at all.
I'd say we should aim for some sort of a one-statement solution. I would be fine with something like a
set_timer(brightness_pct, delay_on, delay_off)method which handles this manually or some other special case for thesetmethod. We'd need to make sure that it works on individual LEDs, LED groups and any other LED-related interfaces though.ddemidov commentedon Oct 22, 2016
ev3dev-lang-cpp has
led::flash(on_ms, off_ms)that does this. We could port the function to python.WasabiFan commentedon Oct 22, 2016
That looks like a nice solution; I think that would work well.
ensonic commentedon Oct 30, 2016
The change of the permissions is strange though and we should have to work around it. We know that is takes a little while before the new attributes appear, but they should be created with the right permissions right away. @ddemidov did you wrote a little test to repro this? @dlech can you confirm this from the kernel side?
Also adding special api is kind of nice, but the low-level access would still fail.
ddemidov commentedon Oct 30, 2016
This was discussed in ev3dev/ev3dev#225
ensonic commentedon Oct 30, 2016
Ah, right. What if we do a led-blink thread? I could give this a try. This way we don't need to switch attributes at all and can also do various other blink patterns and pulses.
dlech commentedon Oct 30, 2016
This comment is still accurate and sums up my views on the matter.
ddemidov commentedon Oct 30, 2016
I am thinking of providing a custom version of Led.trigger property, so that the property writer only returns when the
delay_onanddelay_offare actually created and have the appropriate permissions.This should be more reliable than doing things in a separate thread.
ensonic commentedon Oct 30, 2016
@ddemidov the downside of the custom code is that it will be less deterministic. It can wait for quite some time and applications might prefer a non-blocking behavior. At least that is one thing that I don't like about the code that I have in the openroberta codebase.
ddemidov commentedon Oct 31, 2016
We can easily provide both ways.
trigger/delay_on/delay_offare standard linux attributes, so we can support those with the workaround I mentioned. And if you provide a reliable thread-based interface for pattern blinking, we could have that as well.If an application needs to be deterministic (although I am not sure that low EV3 resources combined with python may be deterministic), one could set the
triggertotimeranddelay_onto 0 during program initialization, and only setdelay_on/delay_offas needed later.Replace autogenerated properties for delay_on/off with custom ones
Replace autogenerated properties for trigger, delay_on/off with custo…