Attempt to read differents device modes#56
Attempt to read differents device modes#56aileo wants to merge 3 commits intonathankellenicki:masterfrom
Conversation
nutki
left a comment
There was a problem hiding this comment.
Thanks for implementing this. I was thinking of doing similar extensions. Good job on investigating output of all those device modes.
| break; | ||
| } | ||
|
|
||
| case Consts.BoostTiltModes.ACCEL: { |
There was a problem hiding this comment.
This change makes the API incompatible since previously this mode, which is the default mode for this sensor was reported as "tilt".
There was a problem hiding this comment.
I don't know what should be done as it breaks but on the other hand, it use information from the lego side which should be "the truth".
There is a lot of changes in this PR and it definitly could not be merge without a new major version.
| const mode = this._getModeForDeviceType(this._portLookup(port).type); | ||
| this._deactivatePortDevice(this._portLookup(port).value, this._portLookup(port).type, mode, 0x00, () => { | ||
| const { type, value, mode } = this._portLookup(port); | ||
| this._deactivatePortDevice(value, type, mode || this._getModeForDeviceType(type), 0x00, () => { |
There was a problem hiding this comment.
This will switch the port mode to the default for type (_getModeForDeviceType) if the mode is 0 since 0 is falsy.
Technically it should be mode === null but this could only happen when you call unsubscribe before subscribe, so not sure if that should be handled.
There was a problem hiding this comment.
I am not sure to understand how subscriptions work: I don't get why mode is needed to unsubscribe...
| this.id = id; | ||
| this.value = value; | ||
| this.type = Consts.DeviceType.UNKNOWN; | ||
| this.mode = null; |
There was a problem hiding this comment.
Is mode also cleared on device reconnect? If not it could lead into one device "inheriting" the mode of the previous device if you don't use autoSubscribe (which will reset it to default for the device on attach).
There was a problem hiding this comment.
I did not because the mode would be overwritten by the next subscribe (either auto or not, you have to recall subscribe when device change).
However, that's a good point regarding consistency so I fixed it in 3bc8404 . It can also be used to know if the devices already have a subscription.
|
Here is the mode info for WeDo 2.0 tilt sensor (it does not seem to be able to return the raw accelerometer reading like the boost internal sensor): And WeDo 2.0 distance sensor |
|
|
||
| case Consts.BoostTiltModes.TILT: { | ||
| values.push(data[4]); | ||
| event = 'tilt'; |
There was a problem hiding this comment.
In this mode the sensor returns a discrete direction of tilt (1, 3, 5, or 7) for each major direction as far as I remember. This is not a very useful mode IMO, but previously the 'tilt' event was reporting acceleration (boost and control+ hubs) or angles (wedo sensor), so this is another change to the API.
There was a problem hiding this comment.
I have to admit that I don't know either why this mode exists as it could be calculated from angle...
Maybe to save some power and require angle only when you have a tilt trigger ?
| return value.toString(2).padStart(length, "0"); | ||
| } | ||
| export function toDistance(value: number, partial: number = 1) { | ||
| return Math.floor((value + 1 / Math.max(partial, 1)) * 25.4) - 20; |
There was a problem hiding this comment.
Note this changes the result when partial is 0. In the old code nothing was added to the inch distance, now you add 1. I am not sure what is the source for the previous formula (I am guessing empirical reverse engineering). At least after the change the result is not negative as often.
There was a problem hiding this comment.
I was just trying to make it a nice oneliner but I missed that is add 25.4 to the final result. It makes the result >5.4 in any case.
I did not understood that partial value so I tried to get more information. According to a comment on eurobricks from @dlech, the format would be:
<COLOR CODE> <DISTANCE> <LED COLOR> <REFLECTED LIGHT>
If this right, the result should differ by ~24.5 for obstacles with significant reflectivity gap at the same distance.
It also explain why that partial is not part of the distance mode data. So SPEC_1 and PROX mode does not get the same accuracy in this implementation. I guess the formula should be removed for consistency ?
|
this is outdated by #60 |
This is not ready to be merged, working on #55 took me further than I expected and I ended trying to read every input mode from every device I have.
Now I need help/advice/review to check what I did wrong and test devices I don't have (WeDo2 hub and accessories, Duplo train base)
To handle different data from different mode I added several events:
Using the debug output I summed up mode numbers and names for each hub.
The bad news is: the same device on different hubs does not expose exactly the same modes.
Color and distance
Motors
BASIC_MOTOR
BOOST_TACHO_MOTOR
BOOST_MOVE_HUB_MOTOR
CONTROL_PLUS_LARGE_MOTOR / CONTROL_PLUS_XLARGE_MOTOR
Tilt/accel sensors
Internal
WeDo tilt sensor
???