ok
Dependencies: mbed AnalogIn_Diff_ok MovingAverage_ok
Revision 0:e08a063f61c3, committed 2014-12-10
- Comitter:
- fblanc
- Date:
- Wed Dec 10 12:53:53 2014 +0000
- Child:
- 1:868ff3346841
- Commit message:
- ok
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp Wed Dec 10 12:53:53 2014 +0000
@@ -0,0 +1,41 @@
+/*
+ * 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.
+ */
+#include "mbed.h"
+#include "trms.h"
+
+#define VERSION "trms_helloworld_2014_12_08"
+#define CIBLE "K64F"
+
+
+
+int main()
+{
+
+trms line1_AC(ADC_DIFF(0,1));
+ line1_AC.test(10);
+
+ printf("%s %s\r\n",VERSION,CIBLE);
+
+ line1_AC.set_gain(2.0*ADCVREF/65535.0);
+ line1_AC.set_offset(0.0);
+ line1_AC.start();
+ while (true) {
+
+ wait(1.0);
+ printf("Vavg= %f Vrms= %f \r\n",line1_AC.read_average(),line1_AC.read_rms());
+ //line1_AC.test(20);
+ }
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.lib Wed Dec 10 12:53:53 2014 +0000 @@ -0,0 +1,1 @@ +http://developer.mbed.org/users/fblanc/code/mbed/#9dd889aeda0e
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/trms/AnalogIn_Diff.lib Wed Dec 10 12:53:53 2014 +0000 @@ -0,0 +1,1 @@ +http://developer.mbed.org/users/fblanc/code/AnalogIn_Diff/#d17541ceae12
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/trms/MovingAverage.lib Wed Dec 10 12:53:53 2014 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/Alegrowin/code/MoyenneMobile/#54b3249b6ad8
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/trms/trms.cpp Wed Dec 10 12:53:53 2014 +0000
@@ -0,0 +1,167 @@
+#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,0.0), vmoy(NSAMPLE,0.0)
+{
+
+
+ test(9);
+ /*
+ _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=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);
+
+}
+float trms::read_average()
+{
+ return ((float)vmoy.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();
+ vmoy.Insert(val_i16);
+ vtrms.Insert(val_i16*val_i16);
+
+ //val2_ui32=vtrms.GetAverage();
+/*
+ //START
+ if(val2_ui32<UAC_MIN2 && F_timer_min ==false) {
+ DBG("Start timer min %d<%d",val2_ui32,UAC_MIN2);
+ timer_min.start();
+ F_timer_min = true;
+
+ }
+ if(val2_ui32>UAC_MAX2 && F_timer_max ==false) {
+ DBG("Start timer max %d>%d",val2_ui32,UAC_MAX2);
+ timer_max.start();
+ F_timer_max = true;
+
+ }
+ //STOP
+ if(val2_ui32>UAC_MIN2STOP && F_timer_min ==true) {
+ DBG("Stop timer min %d>%d",val2_ui32,UAC_MIN2STOP);
+ timer_min.stop();
+ F_timer_min = false;
+ _flag=true;
+ _flag_min=true;
+ }
+
+ if(val2_ui32<UAC_MAX2STOP && F_timer_max ==true) {
+ DBG("Stop timer max %d<%d",val2_ui32,UAC_MAX2STOP);
+ timer_max.stop();
+ F_timer_max = false;
+ _flag=true;
+ _flag_max=true;
+ }
+ if(timer_min.read_ms()>20 && F_timer_min ==true) {
+ _time_min=timer_min.read_ms();
+ _min=MIN(val2_ui32,_min);
+ //DBG("RAW min2=%d",_min);
+ }
+ if(timer_max.read_ms()>20 && F_timer_max ==true) {
+ _time_max=timer_max.read_ms();
+ _max=MAX(val2_ui32,_max);
+ //DBG("RAW max2=%d",_max);
+ }
+*/
+}
+
+bool trms::flag (float *rms, uint32_t *time)
+{
+ bool val=_flag;
+/*
+ if(_flag==true) {
+ _flag=false;
+ if(_flag_min==true) {
+ *time=_time_min;
+ timer_min.reset();
+ *rms=(sqrt((float)_min)*gain-offset);
+ _min=UAC_NON2;
+ _flag_min=false;
+ }
+ if(_flag_max==true) {
+ *time=_time_max;
+ timer_max.reset();
+ *rms=(sqrt((float)_max)*gain-offset);
+ _max=UAC_NON2;
+ _flag_max=false;
+ }
+
+ DBG("Read rms:%f time:%d",*rms,*time);
+ }
+ */
+ return val;
+}
+
+
+void trms::set_gain(float _gain)
+{
+ gain=_gain;
+}
+
+void trms::set_offset(float _offset)
+{
+ offset=_offset;
+}
\ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/trms/trms.h Wed Dec 10 12:53:53 2014 +0000
@@ -0,0 +1,104 @@
+/*
+ * 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 500 //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_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 read_average();
+ float get_gain();
+ void set_gain(float gain);
+ float get_offset();
+ void set_offset(float offset);
+ bool flag(float *rms, uint32_t *time);
+private:
+ void flipadc_Diff();
+ bool _flag;
+
+ MovingAverage <uint32_t>vtrms;
+ MovingAverage <int32_t>vmoy;
+
+ Ticker flipperadc_Diff;
+
+
+ int32_t _min;
+ int32_t _max;
+ float gain;
+ float offset;
+ Timer timer_min;
+ bool F_timer_min;
+ Timer timer_max;
+ bool F_timer_max;
+ uint32_t _time_min;
+ uint32_t _time_max;
+
+ bool _flag_min;
+ bool _flag_max;
+
+
+};
+
+#endif //TRMS_H
\ No newline at end of file
frederic blanc