You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+23-13Lines changed: 23 additions & 13 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -201,23 +201,33 @@ with pyspacemouse.open(device_spec=custom, nonblocking=False) as device:
201
201
202
202
See [Custom Device Configuration](./docs/mouseApi/index.md#custom-device-configuration) for full API.
203
203
204
-
## Blocking, Non-Blocking, sleeps, and callbacks
205
-
206
-
The default `open()` function uses `nonblocking=True`, which means `read()` can return without any data if none is available.
207
-
However, the spacemouse device sends data very quickly and the operating system may buffer this data if it is not being read quickly enough.
208
-
This means that you are very unlikely to ever _not_ get data from `read()`, even in non-blocking mode.
209
-
This also means that if you have a loop that calls `read()` and it is slow, for example if you add `sleep(0.01)`, you might start to see the data start lagging behind a bit.
210
-
For instance, if you poke the spacemouse it will take a few dozen/hundred milliseconds for non-zero data to be return from `read()`.
211
-
This is why you see many of our examples us `nonblocking=False` and no sleep at all.
212
-
This is an attempt to read as quickly as possible to avoid buffering data.
213
-
If you want to minimize the effects of buffering while still operating at a lower rate, consider something like this:
204
+
## Laggy data and sleeps
205
+
206
+
If you put `read()` in a loop with some slow code, or `sleep()` more than ~0.01, you might see the data returned by `read()` start to "lag".
207
+
Consider the follow extreme example of what NOT to do:
214
208
```python
215
209
with pyspacemouse.open() as device:
216
-
whileTrue:
217
-
state = device.read()
218
-
if
210
+
state = device.read()
211
+
if state.nonzero():
212
+
print(state)
213
+
# VERY BAD!
214
+
time.sleep(0.1)
219
215
```
216
+
If you run this, press the space mouse down, then release, you will see it takes a long time for the state to go back to zero!
217
+
This is because `read()` only reads exactly one message from the device, but the OS (at least on linux) will buffer the data for you so no data is dropped.
218
+
During the 100ms you are sleeping, many messages can be received and buffered, and this buffer can be surprisingly large.
219
+
Then when you release the spacemouse, you have to call `read()` many many times to drain the buffer before you get the final message saying the spacemouse state is all 0s again.
220
+
If you would like to run a low-frequency (<100Hz) loop without this buffering behavior, use `read_latest()` instead.
220
221
222
+
### Solution -- `read_latest()`
223
+
```
224
+
with pyspacemouse.open() as device:
225
+
state = device.read_latest() # <--
226
+
if state.nonzero():
227
+
print(state)
228
+
time.sleep(0.1)
229
+
```
230
+
Now you have no problems anymore, because `read_latest()` will keep reading until the buffer is empty, and only then return you the latest state.
0 commit comments