EV3-G API infrared sensor#374
EV3-G API infrared sensor#374dwalton76 merged 10 commits intoev3dev:developfrom dwalton76:ev3-api-infrared-sensor
Conversation
|
We have the InfraredSensor functionality split into three classes
but the EV3-G block do all of this via on InfraredSensor block. I like the way we did it much more, it feels much cleaner. Anyway, I moved the functionality from RemoteControl and BeaconSeeker up to InfraredSensor but did so in a way to make RemoteControl and BeaconSeeker backwards compatible. |
WasabiFan
left a comment
There was a problem hiding this comment.
Would it be possible to re-arrange the classes as they were before so the diff isn't so weird?
ev3dev/core.py
Outdated
| return value | ||
|
|
||
| @property | ||
| def auto_mode(self): |
There was a problem hiding this comment.
Why? I can't imagine we have anyone using this property... I think we should just remove it.
There was a problem hiding this comment.
We don't use it in any of the demos...I would be ok with chopping it
|
|
||
| self._mode = self.set_attr_string(self._mode, 'mode', value) | ||
| self._mode_value = value | ||
|
|
There was a problem hiding this comment.
Don't you need to update the getter as well?
There was a problem hiding this comment.
True I could just have it return self._mode_value
|
Looking into the api_test failure... |
ev3dev/core.py
Outdated
| '_decimals', | ||
| '_driver_name', | ||
| '_mode', | ||
| '_mode_value', |
There was a problem hiding this comment.
The PR title says 'Infrared sensor', but this looks like a move to introduce caching for the mode value. Should this be split into a couple of PRs?
There was a problem hiding this comment.
Will pull this part out into a new PR
yep will put them back where they were, we can move them around later via another PR |
|
Should be easier to read now |
ev3dev/core.py
Outdated
|
|
||
| class InfraredSensor(Sensor): | ||
|
|
||
| class InfraredSensor(Sensor): |
There was a problem hiding this comment.
I had to look at this for a minute to figure out what in the world this change was. It is because I deleted the blank line between the class InfraredSensor and the """. It is odd that it displayed it that way though
| 'IR-REMOTE', | ||
| 'IR-REM-A', | ||
| 'IR-CAL', | ||
| MODE_IR_PROX, |
| ) | ||
|
|
||
| _BUTTON_VALUES = { | ||
| 0: [], |
There was a problem hiding this comment.
Could we get a comment explaining what these indicies are?
There was a problem hiding this comment.
How about
The following are all of the various combinations of button presses for the remote
control. The key/index is the number that will be written in the attribute file to
indicate what combination of buttons are currently pressed.
| } | ||
|
|
||
| #: Handles ``Red Up`` events. | ||
| on_red_up = None |
There was a problem hiding this comment.
Are the buttons color-coded? I have never actually used the remote after taking it out of the box 😆
There was a problem hiding this comment.
They are all gray but there is a red mark on the remote between the two buttons on the left and a blue mark between the two on the right.
| if self.auto_mode: | ||
| self.mode = self.MODE_IR_PROX | ||
| # BeaconSeeker | ||
| def heading(self, channel=1): |
There was a problem hiding this comment.
What's the idea behind making these methods instead of property getters?
There was a problem hiding this comment.
It is so you can pass the channel that you want...the EV3-G block has this.
ev3dev/core.py
Outdated
| Returns heading (-25, 25) to the beacon on the given channel. | ||
| """ | ||
| self.mode = self.MODE_IR_SEEK | ||
| channel = max(1, min(4, channel)) - 1 |
There was a problem hiding this comment.
Warn/error if the channel is out-of-bounds?
There was a problem hiding this comment.
yeah I'll put an assert in here...it would be really confusing behavior if someone passed in channel 5
ev3dev/core.py
Outdated
| def distance(self, channel=1): | ||
| """ | ||
| Returns distance (0, 100) to the beacon on the given channel. | ||
| Returns -128 when beacon is not found. |
There was a problem hiding this comment.
Well duh, of course it returns -128 😆 Do we want to catch that and return None instead? I think no one actually reads our docs 😉
There was a problem hiding this comment.
Not sure...what does the EV3-G block do in this scenario?
| return (self.heading(channel), self.distance(channel)) | ||
|
|
||
| # RemoteControl | ||
| def red_up(self, channel=1): |
There was a problem hiding this comment.
is_red_up_pressed? or red_up_pressed? I originally read this thinking that this was asking whether "red" was released.
There was a problem hiding this comment.
yeah the up/down naming convention is a little odd. I don't think we should change this though, we use the RemoteControl a fair amount in the demos, I imagine these are used a lot.
There was a problem hiding this comment.
Now that we are not worried about being backwards compatible do we want to change the names of red_up, red_down, etc? IMO they are pretty confusing. Brainstorming....
- We could label them
top_left,bottom_left,top_right,bottom_right - We could rename
red_up()tois_top_left_pressed(), etc - We could rename
on_red_uptoon_top_left_press, etc
ev3dev/core.py
Outdated
|
|
||
| #: Handles ``Beacon`` events. | ||
| on_beacon = None | ||
| def __init__(self, sensor=None, channel=1): |
There was a problem hiding this comment.
Is this the order we want it in? Or should channel be first? I'd imagine that providing a custom sensor will be quite rare. Not that it really matters as you can reference it by name anyway.
There was a problem hiding this comment.
It was that way before so we should probably leave it as-is
There was a problem hiding this comment.
The PR makes the class a subclass of InfraredSensor. Why do we need the sensor parameter (and _sensor field) here at all?
There was a problem hiding this comment.
for backwards compatibility
There was a problem hiding this comment.
I mean, the BeaconSeeker class here already is InfraredSensor (since it inherits from it). So why store another instance of InfraredSensor in _sensor?
There was a problem hiding this comment.
python zen: "flat is better than nested"
having a single InfraredSensor class sounds like a good call.
There was a problem hiding this comment.
We should start keeping track of all breaking changes; do you guys want to do that in the changelog file as we go or add a "changelog" section to the README? Or maybe create an issue thread to post into? I'm up for whatever's easiest for you guys.
There was a problem hiding this comment.
We can start a draft of new release here. It seems README could grow too long too fast if we start putting changes there. Another idea is we could add another section in the documentation for this.
There was a problem hiding this comment.
Interesting idea! I just created a release where we can try out tracking this; as we merge PRs we should make sure to update it. If this doesn't go well we can try out a more conventional file approach. https://github.com/rhempel/ev3dev-lang-python/releases/tag/untagged-f43144149d22a7d3cc09
Note that the link might break once we add a version tag.
| on_red_up = None | ||
|
|
||
| #: Handles ``Red Down`` events. | ||
| on_red_down = None |
There was a problem hiding this comment.
These are user-defined event handling functions that should fire up during call to process() method (and by the way, I don't see the method here). As in
def beep_when_pressed(state):
if state:
Sound.beep()
rc.on_red_up = beep_when_pressed
while True:
rc.process()I don't see how these would work as is, since we are merging RemoteControl with the InfraredSensor, and there is no way to set the channel here (we used to set channel in the constructor of RemoteControl).
We should probably change these into dictionaries: on_press[1] = beep_when_pressed (not lists because channel numbers start at 1).
There was a problem hiding this comment.
Re process() method: in the RemoteControl class it was inherited from ButtonBase. If we do want to merge RemoteControl with InfraredSensor, we should probably inherit from ButtonBase as well.
There was a problem hiding this comment.
gotcha, will test/work on this part this evening
There was a problem hiding this comment.
This is working now
There was a problem hiding this comment.
How exactly does it work now? Where do we specify channel to use with the handles?
There was a problem hiding this comment.
Prior to this PR you would need to create a RemoteControl object for each channel that you wished to define handlers for.
Yes, that is correct. But the new implementation of infrared sensor allows to (or claims to) be channel-independent: channel is passed to methods like heading() or process() instead of being assigned at construction. But for RC handlers to work with different channels users would have to create an instance of InfraredSensor per channel, which does not seem logical. I think we could drop ButtonBase from parents, and reimplement the functionality so that handlers would take not just state, but also channel, or something to the same effect?
There was a problem hiding this comment.
If we think that supporting per-channel handlers in a single object is something folks will use
I used this ability for the big shooting robot (EV3STORM?). One channel for leg movement, the other for shooting. It does come handy from time to time.
There was a problem hiding this comment.
Good point. Are you ok if we merge this PR and file a new issue to add per-channel capabilities to the handlers? We are at 10 commits and 60+ comments on this PR and short of the per-channel handlers I think it is in pretty good shape.
There was a problem hiding this comment.
Let's do that, it seems in good shape otherwise.
ev3dev/core.py
Outdated
|
|
||
|
|
||
| class RemoteControl(ButtonBase): | ||
| class RemoteControl(ButtonBase, InfraredSensor): |
There was a problem hiding this comment.
I think we should either drop RemoteControl and BeakonSeeker or keep everything as is. The classes do not do anything that InfraredSensor already does not, so why keep them?.
There was a problem hiding this comment.
agreed they aren't doing much but we need them for backwards compatibility
I like |
|
What do you guys say we go ahead and merge this one and we can kick around renaming Thank you for all of the feedback on this PR, I think this is the most comments I've seen on a single PR here :) I like where we ended up, it looks much cleaner now and is 1:1 with the EV3-G block. |
WasabiFan
left a comment
There was a problem hiding this comment.
Remember to add this to the draft release for VNext.

No description provided.