Sensor library

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Sensor.cpp Source File

Sensor.cpp

00001 #include "mbed.h"
00002 #include "Sensor.h"
00003  
00004 Sensor::Sensor(float* Sensor_1, float* Sensor_2, float* lastscope, float* bias, float weight){
00005     _Sensor_1 = Sensor_1; 
00006     _Sensor_2 = Sensor_2;
00007     _bias = bias;
00008     _Sensor_1_max = 0.337;
00009     _Sensor_2_max = 0.72;
00010     _Sensor_1_min = 0.245;
00011     _Sensor_2_min = 0.619;
00012     _Error = 0;
00013     Sensor_sub = -1;
00014     Sensor_sum = -1;
00015     
00016     _lastscope = lastscope;
00017     _weight = weight;
00018     
00019 }
00020 
00021     
00022 int Sensor::ErrorCheck() { // if there is error for more than 100ms Sensor_Value_1 is -1
00023     Sensor_sum = *_Sensor_1 + *_Sensor_2;
00024     Sensor_sub = abs(Sensor_sum-1);
00025     if (Sensor_sub<0.1){
00026         _Error = 0;
00027         timer.stop();
00028         timer.reset();
00029         }
00030     if (Sensor_sub>0.1){
00031         if (_Error == 0){
00032             //_Error = 1;
00033             timer.start();
00034             }
00035         if (timer.read() > 0.1){
00036             timer.stop();
00037             _Error = 1;
00038             }  
00039         }
00040         return _Error;
00041     }
00042     
00043 void Sensor::ApplyFilter(float newscope)
00044 {
00045     newscope = (_weight*newscope) + ((1-_weight)*(*_lastscope));
00046     *_lastscope = *_bias + newscope; 
00047 }
00048 
00049 void Sensor::ChangeWeight(float new_weight)
00050 {
00051     _weight=  new_weight;
00052 } 
00053 float Sensor::Sensor_Value_1() {
00054     /*if (*_Sensor_1 > _Sensor_1_max){
00055         _Sensor_1_max = *_Sensor_1;
00056         }
00057     if (*_Sensor_1 < _Sensor_1_min){
00058         _Sensor_1_min = *_Sensor_1;
00059         }*/
00060     _Sensor_Value_1 = ((*_Sensor_1 - _Sensor_1_min)*(255 - 0))/((_Sensor_1_max - _Sensor_1_min) + 0 );
00061     if (_Sensor_Value_1<0){
00062         return 0;
00063         }
00064     if (_Sensor_Value_1>255){
00065         return 255;
00066         }
00067     else{
00068         return _Sensor_Value_1;
00069         }
00070     }
00071 float Sensor::Sensor_Value_2() {
00072     /*if (*_Sensor_2 > _Sensor_2_max){
00073         _Sensor_2_max = *_Sensor_2;
00074         }
00075     if (*_Sensor_2 < _Sensor_2_min){
00076         _Sensor_2_min = *_Sensor_2;
00077         }*/
00078     _Sensor_Value_2 = ((*_Sensor_2 - _Sensor_2_min)*(255 - 0))/((_Sensor_2_max - _Sensor_2_min) + 0 );
00079     if (_Sensor_Value_2<0){
00080         return 0;
00081         }
00082     if (_Sensor_Value_2>255){
00083         return 255;
00084         }
00085     else{
00086         return _Sensor_Value_2;
00087         }
00088     } 
00089 float Sensor::Sensor_Sum(){
00090     return Sensor_sum;
00091     }
00092 float Sensor::Sensor_Sub(){
00093     return Sensor_sub;
00094     }
00095 float Sensor::Sensor_timer(){
00096     return timer.read();
00097     }
00098