ok
Dependencies: mbed AnalogIn_Diff_ok MovingAverage_ok
Revision 2:f82c6978c589, committed 2016-01-08
- Comitter:
- fblanc
- Date:
- Fri Jan 08 08:09:34 2016 +0000
- Parent:
- 1:868ff3346841
- Child:
- 3:735c712ffaee
- Commit message:
- ok
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/trms/AnalogIn_Diff.lib Fri Jan 08 08:09:34 2016 +0000 @@ -0,0 +1,1 @@ +https://developer.mbed.org/users/fblanc/code/AnalogIn_Diff_ok/#f39be15f056c
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/trms/MovingAverage.lib Fri Jan 08 08:09:34 2016 +0000 @@ -0,0 +1,1 @@ +https://developer.mbed.org/users/fblanc/code/MovingAverage_ok/#3521837ec075
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/trms/trms.cpp Fri Jan 08 08:09:34 2016 +0000 @@ -0,0 +1,186 @@ +#include "trms.h" + +#define DEBUG 1 +//Debug is disabled by default +#if (defined(DEBUG)) + +#define DBG(x, ...) std::printf("[trms : DBG]"x"\r\n", ##__VA_ARGS__); +#define WARN(x, ...) std::printf("[trms : WARN]"x"\r\n", ##__VA_ARGS__); +#define ERR(x, ...) std::printf("[trms : ERR]"x"\r\n", ##__VA_ARGS__); +#define INFO(x, ...) std::printf("[trms : INFO]"x"\r\n", ##__VA_ARGS__); + +#else + +#define DBG(x, ...) +#define WARN(x, ...) +#define ERR(x, ...) +#define INFO(x, ...) +#endif + +trms::trms(int adc_Diff_number_chan) : AnalogIn_Diff(adc_Diff_number_chan), vtrms(NSAMPLE,UAC_NON2) +{ + + timer_min.stop(); + timer_max.stop(); + timer_min.reset(); + timer_max.reset(); + _time_min=0; + F_timer_min=false; + _time_max=0; + F_timer_max=false; + _min=UAC_NON2; + _max=UAC_NON2; + gain=GAIN; + offset=0.0; + _flag_trms=false; + DBG("Unon =%0.0fV",UAC_NON); + DBG("RAW Umax2 =%d",UAC_MAX2); + DBG("RAW Umin2 =%d",UAC_MIN2); + DBG("Gain =%f",gain); + + + DBG("Freq UAC=%dHz",FREQ); + DBG("Sample =%d",NSAMPLE); + DBG("Sample =%dus",TSAMPLE); + + + +} + + + +float trms::read_rms() +{ + return (sqrt((float)vtrms.GetAverage())*gain-offset); + +} + +/** + * @brief Destructor. + */ +trms::~trms() +{ + + flipperadc_Diff.detach(); +} + +void trms::start() +{ + flipperadc_Diff.attach_us<trms>(this,&trms::flipadc_Diff, TSAMPLE); +} + + + +void trms::flipadc_Diff() +{ + int16_t val_i16; + uint32_t val2_ui32; + + val_i16=read_raws16(); + + vtrms.Insert(val_i16*val_i16); + + val2_ui32=vtrms.GetAverage(); + + //MIN + if(val2_ui32<UAC_MIN2 && F_timer_min ==false) { + + timer_min.start(); + F_timer_min = true; + + } + + if(F_timer_min ==true) { + if(val2_ui32>UAC_MIN2STOP ) { + + timer_min.stop(); + F_timer_min = false; + _flag_trms=true; //????????????????? + _flag_min=true; + } + + + else if(timer_min.read_ms()>20) { + + _time_min=timer_min.read_ms(); + _min=MIN(val2_ui32,_min); + + } + //max + if(val2_ui32<UAC_MAX2STOP && F_timer_max ==true) { + timer_max.stop(); + F_timer_max = false; + _flag_trms=true; + _flag_max=true; + } + if(val2_ui32>UAC_MAX2 && F_timer_max ==false) { + + timer_max.start(); + F_timer_max = true; + + } + if(timer_max.read_ms()>20 && F_timer_max ==true) { + + _time_max=timer_max.read_ms(); + _max=MAX(val2_ui32,_max); + + } + } + +} + +bool trms::flag (float *rms, uint32_t *time) +{ + bool old_flag =_flag_trms; + + if(_flag_trms==true) { + _flag_trms=false; + if(_flag_min==true) { + + *time=_time_min; + timer_min.reset(); + *rms=(sqrt((float)_min)*gain-offset); + _min=UAC_NON2; + _flag_min=false; + + } + + else if(_flag_max==true) { + + *time=_time_max; + timer_max.reset(); + *rms=(sqrt((float)_max)*gain-offset); + _max=UAC_NON2; + _flag_max=false; + + } + + } else { + + *rms=read_rms(); + *time=0; + } + + return old_flag; + +} + + +void trms::set_gain(float _gain) +{ + gain=_gain; +} + +void trms::set_offset(float _offset) +{ + offset=_offset; +} +float trms::get_gain() +{ + return gain; +} + +float trms::get_offset() +{ + return offset; +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/trms/trms.h Fri Jan 08 08:09:34 2016 +0000 @@ -0,0 +1,101 @@ +/* + * Copyright (c) 2014 LAAS-CNRS + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef TRMS_H +#define TRMS_H + +#include "mbed.h" +#include "AnalogIn_Diff.h" +#include "math.h" +#include "MovingAverage.h" + + +#define FREQ 50//en HZ +#define TSAMPLE 125 //en µS +#define R1 1.0E6 +#define R2 510.0 +#define ADCVREF 3.3 +#define GAIN_ACPL_C78 0.125 +#define GAIN ((double)((R1+R2)*2.0*GAIN_ACPL_C78*ADCVREF/R2)/65535.0) +#define UAC_NON 230.0 +#define UAC_MON_i32 ((int32_t)((double)UAC_NON/(double)GAIN)) +#define UAC_MAX ((int32_t)((double)UAC_NON*1.1/(double)GAIN)) +#define UAC_MIN ((int32_t)((double)UAC_NON*0.9/(double)GAIN)) +#define UAC_NON2 ((int32_t)((double)UAC_NON/(double)GAIN*(double)UAC_NON/(double)GAIN)) +#define UAC_MAX2 ((int32_t)((double)UAC_MAX*(double)UAC_MAX)) +#define UAC_MIN2 ((int32_t)((double)UAC_MIN*(double)UAC_MIN)) +#define UAC_MAX2STOP ((int32_t)((double)UAC_MAX*(double)UAC_MAX)*0.95) +#define UAC_MIN2STOP ((int32_t)((double)UAC_MIN*(double)UAC_MIN)*1.05) +#define NSAMPLE ((int32_t)(1/(double)FREQ *1.0E6/(double)TSAMPLE)) +//#define max(a,b) ((a)>=(b)?(a):(b)) +//#define min(a,b) ((a)<=(b)?(a):(b)) + +#define MAX(a,b) ({ typeof(a) aa = (a); typeof(b) bb = (b); aa>=bb? aa: bb; }) +#define MIN(a,b) ({ typeof(a) aa = (a); typeof(b) bb = (b); aa<=bb? aa: bb; }) + +#define VERSION_TRMS "2014_12_10" + + +class trms : public AnalogIn_Diff +{ + +public: + /** + * Constructor + * + * @param a2d_number_chan is ADC_DIFF(#adc, #ch) + * @return true if successful + */ + + trms(int adc_Diff_number_chan) ; + + /** + * destructor + */ + ~trms(); + + void start(); + float read_rms(); + float get_gain(); + void set_gain(float gain); + float get_offset(); + void set_offset(float offset); + bool flag(float *rms, uint32_t *time); + +private: + volatile bool _flag_trms; + volatile bool _flag_max; + volatile bool _flag_min; + + volatile int32_t _min; + volatile int32_t _max; + volatile float gain; + volatile float offset; + volatile bool F_timer_min; + volatile bool F_timer_max; + volatile uint32_t _time_min; + volatile uint32_t _time_max; + + Timer timer_min; + Timer timer_max; + void flipadc_Diff(); + MovingAverage <uint32_t>vtrms; + Ticker flipperadc_Diff; + + +}; + +#endif //TRMS_H