Sensor library

Committer:
noamnahum
Date:
Tue Jun 29 17:28:29 2021 +0000
Revision:
0:eb491ad3a263
Formula Sensor read library;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
noamnahum 0:eb491ad3a263 1 #include "mbed.h"
noamnahum 0:eb491ad3a263 2 #include "Sensor.h"
noamnahum 0:eb491ad3a263 3
noamnahum 0:eb491ad3a263 4 Sensor::Sensor(float* Sensor_1, float* Sensor_2, float* lastscope, float* bias, float weight){
noamnahum 0:eb491ad3a263 5 _Sensor_1 = Sensor_1;
noamnahum 0:eb491ad3a263 6 _Sensor_2 = Sensor_2;
noamnahum 0:eb491ad3a263 7 _bias = bias;
noamnahum 0:eb491ad3a263 8 _Sensor_1_max = 0.337;
noamnahum 0:eb491ad3a263 9 _Sensor_2_max = 0.72;
noamnahum 0:eb491ad3a263 10 _Sensor_1_min = 0.245;
noamnahum 0:eb491ad3a263 11 _Sensor_2_min = 0.619;
noamnahum 0:eb491ad3a263 12 _Error = 0;
noamnahum 0:eb491ad3a263 13 Sensor_sub = -1;
noamnahum 0:eb491ad3a263 14 Sensor_sum = -1;
noamnahum 0:eb491ad3a263 15
noamnahum 0:eb491ad3a263 16 _lastscope = lastscope;
noamnahum 0:eb491ad3a263 17 _weight = weight;
noamnahum 0:eb491ad3a263 18
noamnahum 0:eb491ad3a263 19 }
noamnahum 0:eb491ad3a263 20
noamnahum 0:eb491ad3a263 21
noamnahum 0:eb491ad3a263 22 int Sensor::ErrorCheck() { // if there is error for more than 100ms Sensor_Value_1 is -1
noamnahum 0:eb491ad3a263 23 Sensor_sum = *_Sensor_1 + *_Sensor_2;
noamnahum 0:eb491ad3a263 24 Sensor_sub = abs(Sensor_sum-1);
noamnahum 0:eb491ad3a263 25 if (Sensor_sub<0.1){
noamnahum 0:eb491ad3a263 26 _Error = 0;
noamnahum 0:eb491ad3a263 27 timer.stop();
noamnahum 0:eb491ad3a263 28 timer.reset();
noamnahum 0:eb491ad3a263 29 }
noamnahum 0:eb491ad3a263 30 if (Sensor_sub>0.1){
noamnahum 0:eb491ad3a263 31 if (_Error == 0){
noamnahum 0:eb491ad3a263 32 //_Error = 1;
noamnahum 0:eb491ad3a263 33 timer.start();
noamnahum 0:eb491ad3a263 34 }
noamnahum 0:eb491ad3a263 35 if (timer.read() > 0.1){
noamnahum 0:eb491ad3a263 36 timer.stop();
noamnahum 0:eb491ad3a263 37 _Error = 1;
noamnahum 0:eb491ad3a263 38 }
noamnahum 0:eb491ad3a263 39 }
noamnahum 0:eb491ad3a263 40 return _Error;
noamnahum 0:eb491ad3a263 41 }
noamnahum 0:eb491ad3a263 42
noamnahum 0:eb491ad3a263 43 void Sensor::ApplyFilter(float newscope)
noamnahum 0:eb491ad3a263 44 {
noamnahum 0:eb491ad3a263 45 newscope = (_weight*newscope) + ((1-_weight)*(*_lastscope));
noamnahum 0:eb491ad3a263 46 *_lastscope = *_bias + newscope;
noamnahum 0:eb491ad3a263 47 }
noamnahum 0:eb491ad3a263 48
noamnahum 0:eb491ad3a263 49 void Sensor::ChangeWeight(float new_weight)
noamnahum 0:eb491ad3a263 50 {
noamnahum 0:eb491ad3a263 51 _weight= new_weight;
noamnahum 0:eb491ad3a263 52 }
noamnahum 0:eb491ad3a263 53 float Sensor::Sensor_Value_1() {
noamnahum 0:eb491ad3a263 54 /*if (*_Sensor_1 > _Sensor_1_max){
noamnahum 0:eb491ad3a263 55 _Sensor_1_max = *_Sensor_1;
noamnahum 0:eb491ad3a263 56 }
noamnahum 0:eb491ad3a263 57 if (*_Sensor_1 < _Sensor_1_min){
noamnahum 0:eb491ad3a263 58 _Sensor_1_min = *_Sensor_1;
noamnahum 0:eb491ad3a263 59 }*/
noamnahum 0:eb491ad3a263 60 _Sensor_Value_1 = ((*_Sensor_1 - _Sensor_1_min)*(255 - 0))/((_Sensor_1_max - _Sensor_1_min) + 0 );
noamnahum 0:eb491ad3a263 61 if (_Sensor_Value_1<0){
noamnahum 0:eb491ad3a263 62 return 0;
noamnahum 0:eb491ad3a263 63 }
noamnahum 0:eb491ad3a263 64 if (_Sensor_Value_1>255){
noamnahum 0:eb491ad3a263 65 return 255;
noamnahum 0:eb491ad3a263 66 }
noamnahum 0:eb491ad3a263 67 else{
noamnahum 0:eb491ad3a263 68 return _Sensor_Value_1;
noamnahum 0:eb491ad3a263 69 }
noamnahum 0:eb491ad3a263 70 }
noamnahum 0:eb491ad3a263 71 float Sensor::Sensor_Value_2() {
noamnahum 0:eb491ad3a263 72 /*if (*_Sensor_2 > _Sensor_2_max){
noamnahum 0:eb491ad3a263 73 _Sensor_2_max = *_Sensor_2;
noamnahum 0:eb491ad3a263 74 }
noamnahum 0:eb491ad3a263 75 if (*_Sensor_2 < _Sensor_2_min){
noamnahum 0:eb491ad3a263 76 _Sensor_2_min = *_Sensor_2;
noamnahum 0:eb491ad3a263 77 }*/
noamnahum 0:eb491ad3a263 78 _Sensor_Value_2 = ((*_Sensor_2 - _Sensor_2_min)*(255 - 0))/((_Sensor_2_max - _Sensor_2_min) + 0 );
noamnahum 0:eb491ad3a263 79 if (_Sensor_Value_2<0){
noamnahum 0:eb491ad3a263 80 return 0;
noamnahum 0:eb491ad3a263 81 }
noamnahum 0:eb491ad3a263 82 if (_Sensor_Value_2>255){
noamnahum 0:eb491ad3a263 83 return 255;
noamnahum 0:eb491ad3a263 84 }
noamnahum 0:eb491ad3a263 85 else{
noamnahum 0:eb491ad3a263 86 return _Sensor_Value_2;
noamnahum 0:eb491ad3a263 87 }
noamnahum 0:eb491ad3a263 88 }
noamnahum 0:eb491ad3a263 89 float Sensor::Sensor_Sum(){
noamnahum 0:eb491ad3a263 90 return Sensor_sum;
noamnahum 0:eb491ad3a263 91 }
noamnahum 0:eb491ad3a263 92 float Sensor::Sensor_Sub(){
noamnahum 0:eb491ad3a263 93 return Sensor_sub;
noamnahum 0:eb491ad3a263 94 }
noamnahum 0:eb491ad3a263 95 float Sensor::Sensor_timer(){
noamnahum 0:eb491ad3a263 96 return timer.read();
noamnahum 0:eb491ad3a263 97 }
noamnahum 0:eb491ad3a263 98