-
Notifications
You must be signed in to change notification settings - Fork 40
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Negative TachoMotor position sensor bugfix #7
Comments
Hi, I've noticed that same bug as well, my fix is to do a two-s-complement conversion from 32bit unsigned to 32bit signed int with something like:
I've put it into _change handler, but it should better be put in decoding code. |
Why not just use the import struct
struct.unpack_from('<i', buffer, offset) # little-endian signed 32-bit integer
struct.unpack_from('<I', buffer, offset) # little-endian unsigned 32-bit integer |
I've got this problem also with motor speed reading when reversing. Indeed we need to know whether the data is signed on unsigned int and interpret it accordingly. When in |
As far as I know, sensor values should always be treated as signed. https://lego.github.io/lego-ble-wireless-protocol-docs/index.html#value-format This doesn't say it explicitly, but I know from extensive experience with the EV3 source code (EV3 uses the same UART protocol for I/O devices) that these are all little-endian signed values. |
This does the trick for me:
|
Cool. You should send a pull request. There should also be a data type received for each mode that could/should be used instead of looking at the byte count. In practice, I don't think any sensors use floating point data values, but in theory, 4 bytes could mean 32-bit integer or 32-bit floating point. |
See PR #10 with the above fix. I did not include support for floating point values - to me it looks the information about the type is nowhere to access when updating the value. Perhaps we need to extend |
Hi!
I think this is a super cool project and I am really happy to see you are working on the new Control Plus hub :-). I noticed a small bug in the sensor readings. If I attach a CPlusMotor using
@attach(CPlusLargeMotor, name='stearing', port=2, capabilities=['sense_pos'])
and then steer it towards negative positions,
sense_pos
will return very large numbers. It seems negative numbers are encoded in the format max_value-abs(value), and the following (really dumb) fix toperipheral.py
seems to solve the issue:although the proper way is obviously to change
_convert_bytes
.On a related note, I have model 42099 (which I guess from the examples you also got?) and I wonder if you have some input on the following: I have written a small control program for the 42099 model which takes keyboard inputs and try to accelerate and turn the front wheels using .set_pos(...). I change the position in small increments (+/- 15 degrees) based on keyboard inputs. Sometimes, it seems the motor will not fully perform the command, and instead freeze and make a high-pitched whining sound for about 0.5-1 seconds (and in this mode subsequent commands are paused). I have tried to change the
max_power
to 100 but this does not seem to fully solve the issue. I have also tried mucking around with the other input parameters to set_pos, including the speed, but to no avail (I confess I do not know what all of them really do).This behavior is not one I have observed with the lego smartphone app, which seems to respond much more "crisply" to inputs. So my question is fairly high-level: If I wanted to write a control app to control the wheels, and avoid the above behavior (or more specifically, imitate the lego smartphone app), do you know if I should still use .set_pos and is there something obvious I am missing? I am attaching the code just in case it has any interest to anyone.
lego_device.txt
The text was updated successfully, but these errors were encountered: