Sensor library
Revision 0:eb491ad3a263, committed 2021-06-29
- Comitter:
- noamnahum
- Date:
- Tue Jun 29 17:28:29 2021 +0000
- Commit message:
- Formula Sensor read library;
Changed in this revision
Sensor.cpp | Show annotated file Show diff for this revision Revisions of this file |
Sensor.h | Show annotated file Show diff for this revision Revisions of this file |
diff -r 000000000000 -r eb491ad3a263 Sensor.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Sensor.cpp Tue Jun 29 17:28:29 2021 +0000 @@ -0,0 +1,98 @@ +#include "mbed.h" +#include "Sensor.h" + +Sensor::Sensor(float* Sensor_1, float* Sensor_2, float* lastscope, float* bias, float weight){ + _Sensor_1 = Sensor_1; + _Sensor_2 = Sensor_2; + _bias = bias; + _Sensor_1_max = 0.337; + _Sensor_2_max = 0.72; + _Sensor_1_min = 0.245; + _Sensor_2_min = 0.619; + _Error = 0; + Sensor_sub = -1; + Sensor_sum = -1; + + _lastscope = lastscope; + _weight = weight; + +} + + +int Sensor::ErrorCheck() { // if there is error for more than 100ms Sensor_Value_1 is -1 + Sensor_sum = *_Sensor_1 + *_Sensor_2; + Sensor_sub = abs(Sensor_sum-1); + if (Sensor_sub<0.1){ + _Error = 0; + timer.stop(); + timer.reset(); + } + if (Sensor_sub>0.1){ + if (_Error == 0){ + //_Error = 1; + timer.start(); + } + if (timer.read() > 0.1){ + timer.stop(); + _Error = 1; + } + } + return _Error; + } + +void Sensor::ApplyFilter(float newscope) +{ + newscope = (_weight*newscope) + ((1-_weight)*(*_lastscope)); + *_lastscope = *_bias + newscope; +} + +void Sensor::ChangeWeight(float new_weight) +{ + _weight= new_weight; +} +float Sensor::Sensor_Value_1() { + /*if (*_Sensor_1 > _Sensor_1_max){ + _Sensor_1_max = *_Sensor_1; + } + if (*_Sensor_1 < _Sensor_1_min){ + _Sensor_1_min = *_Sensor_1; + }*/ + _Sensor_Value_1 = ((*_Sensor_1 - _Sensor_1_min)*(255 - 0))/((_Sensor_1_max - _Sensor_1_min) + 0 ); + if (_Sensor_Value_1<0){ + return 0; + } + if (_Sensor_Value_1>255){ + return 255; + } + else{ + return _Sensor_Value_1; + } + } +float Sensor::Sensor_Value_2() { + /*if (*_Sensor_2 > _Sensor_2_max){ + _Sensor_2_max = *_Sensor_2; + } + if (*_Sensor_2 < _Sensor_2_min){ + _Sensor_2_min = *_Sensor_2; + }*/ + _Sensor_Value_2 = ((*_Sensor_2 - _Sensor_2_min)*(255 - 0))/((_Sensor_2_max - _Sensor_2_min) + 0 ); + if (_Sensor_Value_2<0){ + return 0; + } + if (_Sensor_Value_2>255){ + return 255; + } + else{ + return _Sensor_Value_2; + } + } +float Sensor::Sensor_Sum(){ + return Sensor_sum; + } +float Sensor::Sensor_Sub(){ + return Sensor_sub; + } +float Sensor::Sensor_timer(){ + return timer.read(); + } +
diff -r 000000000000 -r eb491ad3a263 Sensor.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Sensor.h Tue Jun 29 17:28:29 2021 +0000 @@ -0,0 +1,51 @@ +#ifndef SENSOR_H +#define SENSOR_H +#include "mbed.h" +/* + Noam Nahum and Dror Balbul 05/10/2020 + Sensor reading and filtering for formula student vehicle + The library takes 2 diffrential sensor and calculate to the diffrence between them. + this library is for the FSAE regulation For reading Pedal Sensors and Thorttle Sensors. + + + + Note: Sensor objects use given pointers, ie Sensor_1 + Sensor_2, lastscope. When reading/ modifying + these vars make sure we don't have possible read/write + conflicts if the interrupt fires. */ + + +class Sensor { + public: + Sensor(float* Sensor_1, float* Sensor_2, float* lastscope, float* bias, float weight); // Sensor_1, (Analogin value of sensor 1) + float Sensor_Value_1(); // Sensor_1 Value between 0-255 + float Sensor_Value_2(); // Sensor_1 Value between 0-255 + //int Get_Error(); + void ApplyFilter(float newscope); + void ChangeWeight(float new_weight); + int ErrorCheck(); + float Sensor_Sum(); + float Sensor_Sub(); + float Sensor_timer(); + + private: + Timer timer; + int _Error; + float* _Sensor_1; + float* _Sensor_2; + float _Sensor_Value_1; + float _Sensor_Value_2; + float Sensor_sum, Sensor_sub; + float _weight; + float* _lastscope; + float* _bias; + float _Sensor_1_max; + float _Sensor_1_min; + float _Sensor_2_max; + float _Sensor_2_min; + +}; + + +#endif + \ No newline at end of file