Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: USBDevice mbed-rtos mbed
Fork of JoyStick by
AutoScale.cpp@5:a0bb17c379ce, 2016-06-22 (annotated)
- Committer:
- rvt
- Date:
- Wed Jun 22 12:50:16 2016 +0000
- Revision:
- 5:a0bb17c379ce
Latest
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| rvt | 5:a0bb17c379ce | 1 | #include "AutoScale.h" |
| rvt | 5:a0bb17c379ce | 2 | #include <algorithm> |
| rvt | 5:a0bb17c379ce | 3 | |
| rvt | 5:a0bb17c379ce | 4 | AutoScale::AutoScale(AnalogFilterInterface *chain,long expectedMin, long expectedMax) : |
| rvt | 5:a0bb17c379ce | 5 | AnalogFilterInterface(chain), _expectedMax(expectedMax), _expectedMin(expectedMin), _currentMax(expectedMin), _currentMin(expectedMax), _current(0), _a(0.), _b(0), _mul(1.0) |
| rvt | 5:a0bb17c379ce | 6 | { |
| rvt | 5:a0bb17c379ce | 7 | } |
| rvt | 5:a0bb17c379ce | 8 | AutoScale::AutoScale(AnalogFilterInterface *chain,long expectedMin, long expectedMax, double multiplier) : |
| rvt | 5:a0bb17c379ce | 9 | AnalogFilterInterface(chain), _expectedMax(expectedMax), _expectedMin(expectedMin), _currentMax(expectedMin), _currentMin(expectedMax), _current(0), _a(0.), _b(0), _mul(multiplier) |
| rvt | 5:a0bb17c379ce | 10 | { |
| rvt | 5:a0bb17c379ce | 11 | } |
| rvt | 5:a0bb17c379ce | 12 | |
| rvt | 5:a0bb17c379ce | 13 | AutoScale::~AutoScale() |
| rvt | 5:a0bb17c379ce | 14 | { |
| rvt | 5:a0bb17c379ce | 15 | } |
| rvt | 5:a0bb17c379ce | 16 | |
| rvt | 5:a0bb17c379ce | 17 | void AutoScale::setData(long dataPoint) |
| rvt | 5:a0bb17c379ce | 18 | { |
| rvt | 5:a0bb17c379ce | 19 | getChain()->setData(dataPoint); |
| rvt | 5:a0bb17c379ce | 20 | _current = getChain()->getData(); |
| rvt | 5:a0bb17c379ce | 21 | |
| rvt | 5:a0bb17c379ce | 22 | if (_current < _currentMin) { |
| rvt | 5:a0bb17c379ce | 23 | _currentMin = _current; |
| rvt | 5:a0bb17c379ce | 24 | reCalc(); |
| rvt | 5:a0bb17c379ce | 25 | } |
| rvt | 5:a0bb17c379ce | 26 | if (_current > _currentMax) { |
| rvt | 5:a0bb17c379ce | 27 | _currentMax = _current; |
| rvt | 5:a0bb17c379ce | 28 | reCalc(); |
| rvt | 5:a0bb17c379ce | 29 | } |
| rvt | 5:a0bb17c379ce | 30 | _current = (long)((_a * _current + _b)*_mul); |
| rvt | 5:a0bb17c379ce | 31 | |
| rvt | 5:a0bb17c379ce | 32 | _current = std::min(_current, _expectedMax); |
| rvt | 5:a0bb17c379ce | 33 | _current = std::max(_current, _expectedMin); |
| rvt | 5:a0bb17c379ce | 34 | } |
| rvt | 5:a0bb17c379ce | 35 | |
| rvt | 5:a0bb17c379ce | 36 | long AutoScale::getData() const |
| rvt | 5:a0bb17c379ce | 37 | { |
| rvt | 5:a0bb17c379ce | 38 | return _current; |
| rvt | 5:a0bb17c379ce | 39 | } |
| rvt | 5:a0bb17c379ce | 40 | |
| rvt | 5:a0bb17c379ce | 41 | void AutoScale::reCalc() |
| rvt | 5:a0bb17c379ce | 42 | { |
| rvt | 5:a0bb17c379ce | 43 | _a = (_expectedMax - _expectedMin) / (double)(_currentMax - _currentMin); |
| rvt | 5:a0bb17c379ce | 44 | _b = -((_a * _currentMax) - _expectedMax); |
| rvt | 5:a0bb17c379ce | 45 | } |
| rvt | 5:a0bb17c379ce | 46 |
