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
00001 #include "AutoScale.h" 00002 #include <algorithm> 00003 00004 AutoScale::AutoScale(AnalogFilterInterface *chain,long expectedMin, long expectedMax) : 00005 AnalogFilterInterface(chain), _expectedMax(expectedMax), _expectedMin(expectedMin), _currentMax(expectedMin), _currentMin(expectedMax), _current(0), _a(0.), _b(0), _mul(1.0) 00006 { 00007 } 00008 AutoScale::AutoScale(AnalogFilterInterface *chain,long expectedMin, long expectedMax, double multiplier) : 00009 AnalogFilterInterface(chain), _expectedMax(expectedMax), _expectedMin(expectedMin), _currentMax(expectedMin), _currentMin(expectedMax), _current(0), _a(0.), _b(0), _mul(multiplier) 00010 { 00011 } 00012 00013 AutoScale::~AutoScale() 00014 { 00015 } 00016 00017 void AutoScale::setData(long dataPoint) 00018 { 00019 getChain()->setData(dataPoint); 00020 _current = getChain()->getData(); 00021 00022 if (_current < _currentMin) { 00023 _currentMin = _current; 00024 reCalc(); 00025 } 00026 if (_current > _currentMax) { 00027 _currentMax = _current; 00028 reCalc(); 00029 } 00030 _current = (long)((_a * _current + _b)*_mul); 00031 00032 _current = std::min(_current, _expectedMax); 00033 _current = std::max(_current, _expectedMin); 00034 } 00035 00036 long AutoScale::getData() const 00037 { 00038 return _current; 00039 } 00040 00041 void AutoScale::reCalc() 00042 { 00043 _a = (_expectedMax - _expectedMin) / (double)(_currentMax - _currentMin); 00044 _b = -((_a * _currentMax) - _expectedMax); 00045 } 00046
Generated on Wed Jul 13 2022 20:21:10 by
1.7.2
