Code for autonomous rover for Sparkfun AVC. DataBus won 3rd in 2012 and the same code was used on Troubled Child, a 1986 Jeep Grand Wagoneer to win 1st in 2014.
Dependencies: mbed Watchdog SDFileSystem DigoleSerialDisp
Sensors/IncrementalEncoder/IncrementalEncoder.cpp@25:bb5356402687, 2018-11-30 (annotated)
- Committer:
- shimniok
- Date:
- Fri Nov 30 16:11:53 2018 +0000
- Revision:
- 25:bb5356402687
- Parent:
- 23:a34af501ea89
Initial publish of revised version.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
shimniok | 0:a6a169de725f | 1 | #include "IncrementalEncoder.h" |
shimniok | 0:a6a169de725f | 2 | |
shimniok | 0:a6a169de725f | 3 | IncrementalEncoder::IncrementalEncoder(PinName pin): _lastTicks(0), _ticks(0), _new(false), _interrupt(pin) { |
shimniok | 0:a6a169de725f | 4 | _interrupt.mode(PullNone); // default is pulldown but my encoder board uses a pull-up and that just don't work |
shimniok | 23:a34af501ea89 | 5 | _interrupt.rise(callback(this, &IncrementalEncoder::_incRise)); |
shimniok | 23:a34af501ea89 | 6 | _interrupt.fall(callback(this, &IncrementalEncoder::_incFall)); |
shimniok | 0:a6a169de725f | 7 | _t.start(); |
shimniok | 0:a6a169de725f | 8 | _t.reset(); |
shimniok | 0:a6a169de725f | 9 | _lastTime = _t.read_us(); |
shimniok | 0:a6a169de725f | 10 | } |
shimniok | 0:a6a169de725f | 11 | |
shimniok | 0:a6a169de725f | 12 | unsigned int IncrementalEncoder::read() { |
shimniok | 0:a6a169de725f | 13 | // disable interrupts? |
shimniok | 0:a6a169de725f | 14 | unsigned int ticks = _ticks - _lastTicks; |
shimniok | 0:a6a169de725f | 15 | _lastTicks = _ticks; |
shimniok | 0:a6a169de725f | 16 | _new=false; |
shimniok | 0:a6a169de725f | 17 | return ticks; |
shimniok | 0:a6a169de725f | 18 | } |
shimniok | 0:a6a169de725f | 19 | |
shimniok | 0:a6a169de725f | 20 | unsigned int IncrementalEncoder::readTotal() { |
shimniok | 0:a6a169de725f | 21 | _new=false; |
shimniok | 0:a6a169de725f | 22 | return _ticks; |
shimniok | 0:a6a169de725f | 23 | } |
shimniok | 0:a6a169de725f | 24 | |
shimniok | 0:a6a169de725f | 25 | unsigned int IncrementalEncoder::readRise() { |
shimniok | 0:a6a169de725f | 26 | _new=false; |
shimniok | 0:a6a169de725f | 27 | return _rise; |
shimniok | 0:a6a169de725f | 28 | } |
shimniok | 0:a6a169de725f | 29 | |
shimniok | 0:a6a169de725f | 30 | unsigned int IncrementalEncoder::readFall() { |
shimniok | 0:a6a169de725f | 31 | _new=false; |
shimniok | 0:a6a169de725f | 32 | return _fall; |
shimniok | 0:a6a169de725f | 33 | } |
shimniok | 0:a6a169de725f | 34 | |
shimniok | 0:a6a169de725f | 35 | unsigned int IncrementalEncoder::readTime() { |
shimniok | 0:a6a169de725f | 36 | return _time; |
shimniok | 0:a6a169de725f | 37 | } |
shimniok | 0:a6a169de725f | 38 | |
shimniok | 0:a6a169de725f | 39 | void IncrementalEncoder::reset() { |
shimniok | 0:a6a169de725f | 40 | _ticks = _lastTicks = 0; |
shimniok | 0:a6a169de725f | 41 | } |
shimniok | 0:a6a169de725f | 42 | |
shimniok | 0:a6a169de725f | 43 | void IncrementalEncoder::_increment() { |
shimniok | 0:a6a169de725f | 44 | _ticks++; |
shimniok | 0:a6a169de725f | 45 | } |
shimniok | 0:a6a169de725f | 46 | |
shimniok | 0:a6a169de725f | 47 | #define A 0.3 // mix in how much from previous readings? |
shimniok | 0:a6a169de725f | 48 | void IncrementalEncoder::_incRise() { |
shimniok | 0:a6a169de725f | 49 | _rise++; |
shimniok | 0:a6a169de725f | 50 | _ticks++; |
shimniok | 0:a6a169de725f | 51 | _new=true; |
shimniok | 0:a6a169de725f | 52 | // compute time between ticks; only do this for rise |
shimniok | 0:a6a169de725f | 53 | // to eliminate jitter |
shimniok | 0:a6a169de725f | 54 | int now = _t.read_us(); |
shimniok | 0:a6a169de725f | 55 | _time = A*_time + (1-A)*(now - _lastTime); |
shimniok | 0:a6a169de725f | 56 | // Dead band: if _time > xxxx then turn off and wait until next tick |
shimniok | 0:a6a169de725f | 57 | // to start up again |
shimniok | 0:a6a169de725f | 58 | _lastTime = now; |
shimniok | 0:a6a169de725f | 59 | } |
shimniok | 0:a6a169de725f | 60 | |
shimniok | 0:a6a169de725f | 61 | void IncrementalEncoder::_incFall() { |
shimniok | 0:a6a169de725f | 62 | _fall++; |
shimniok | 0:a6a169de725f | 63 | _ticks++; |
shimniok | 0:a6a169de725f | 64 | _new=true; |
shimniok | 0:a6a169de725f | 65 | } |