@@ -340,29 +340,28 @@ this for applications requiring rapid response.
340
340
341
341
# 6. Quadrature encoders
342
342
343
+ This is a work in progress. Changes may occur.
344
+
343
345
The ` Encoder ` class is an asynchronous driver for control knobs based on
344
346
quadrature encoder switches such as
345
- [ this Adafruit product] ( https://www.adafruit.com/product/377 ) . This is not
346
- intended for high throughput encoders such as those used in CNC machines where
347
- [ an interrupt based solution] ( https://github.com/peterhinch/micropython-samples#47-rotary-incremental-encoder )
348
- is required. This is because the driver works by polling the switches. The
349
- latency between successive readings of the switch state will depend on the
350
- behaviour of other tasks in the application, but if changes occur rapidly it is
351
- likely that transitions will be missed.
347
+ [ this Adafruit product] ( https://www.adafruit.com/product/377 ) . The driver is
348
+ not intended for applications such as CNC machines where
349
+ [ a solution such as this one] ( https://github.com/peterhinch/micropython-samples#47-rotary-incremental-encoder )
350
+ is required. Drivers for NC machines must never miss an edge. Contact bounce or
351
+ vibration induced jitter can cause transitions to occur at a high rate; these
352
+ must be tracked.
352
353
353
- In the context of a rotary dial this is usually not a problem, firstly because
354
- changes occur at a relatively low rate and secondly because there is usually
355
- some form of feedback to the user. A single missed increment on a CNC machine
356
- is a fail. In a user interface it usually is not.
354
+ This driver runs the user supplied callback in an ` asyncio ` context, so it runs
355
+ only when other tasks have yielded to the scheduler. This ensures that the
356
+ callback can run safely. The driver allows limits to be assigned to the control
357
+ so that a dial running from (say) 0 to 100 may be implemented. If limits are
358
+ used, encoder values no longer represent absolute angles.
357
359
358
- The API uses a callback which occurs whenever the value changes. Alternatively
359
- the ` Encoder ` may be queried to retrieve the current position.
360
+ The callback only runs if a change in position has occurred.
360
361
361
- A high throughput solution can be used with rotary dials but there is a
362
- difference in the way contact bounce (or vibration induced jitter) are handled.
363
- The high throughput solution results in +-1 count jitter with the callback
364
- repeatedly occurring. This driver uses hysteresis to ensure that transitions
365
- due to contact bounce are ignored.
362
+ A consequence of the callback running in an ` asyncio ` context is that, by the
363
+ time it runs, the encoder's position may have changed by more than one
364
+ increment.
366
365
367
366
## 6.1 Encoder class
368
367
@@ -375,14 +374,17 @@ Constructor arguments:
375
374
Optionally maximum and/or minimum limits can be set.
376
375
5 . ` vmax=None `
377
376
6 . ` callback=lambda *_ : None ` Optional callback function. The callback
378
- receives two args, ` v ` being the encoder's current value and ` fwd ` being
379
- ` True ` if the value has incremented of ` False ` if it decremented . Further args
380
- may be appended by the following.
377
+ receives two args, ` v ` being the encoder's current value and ` delta ` being
378
+ the signed difference between the current value and the previous one . Further
379
+ args may be appended by the following.
381
380
7 . ` args=() ` An optional tuple of args for the callback.
382
381
383
382
Synchronous method:
384
383
* ` value ` No args. Returns an integer being the ` Encoder ` current value.
385
384
385
+ Class variable:
386
+ * ` LATENCY=50 ` This sets a minumum period (in ms) between callback runs.
387
+
386
388
###### [ Contents] ( ./DRIVERS.md#1-contents )
387
389
388
390
# 7. Additional functions
0 commit comments