summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--globals.h5
-rw-r--r--monitor.c38
2 files changed, 32 insertions, 11 deletions
diff --git a/globals.h b/globals.h
index 534a9bc..85d81d4 100644
--- a/globals.h
+++ b/globals.h
@@ -350,6 +350,9 @@ typedef struct {
} CalStruct;
+#define CURR_MON_MAX_OLD_COUNTS 50 // monitor readings are around 50/sec, average over one second
+#define MAX_STEP_DEV_FROM_AVG 5 // reset monitor average if more than N steps difference
+
typedef struct {
float frequency; /* the global frequency variable */
float delay; /* the global delay variable */
@@ -361,6 +364,8 @@ typedef struct {
/* not part of sav/rcl, generated by amplitude routines */
float Curr_Mon_value; /* current monitor reading */
+ float old_mon_vals[CURR_MON_MAX_OLD_COUNTS]; /* for monitor averaging */
+ int num_mon_vals; /* how many monitor readings are stored */
float displayed_mon_val; /* used to determine when LCD update is required */
int Curr_Mon_offset; /* current monitor ADC offset */
diff --git a/monitor.c b/monitor.c
index fff7711..4c14bf8 100644
--- a/monitor.c
+++ b/monitor.c
@@ -155,7 +155,7 @@ int I2C_Get_Monitor_Word(int channel)
int I2C_Check_Monitors(void)
{
int monitor_word;
- float step_size;
+ float new_val, avg_val, step_size;
int channel;
int ampl_range,point_found,UseNegData,entry,word_out,atten_range;
@@ -198,16 +198,34 @@ int I2C_Check_Monitors(void)
&ampl_range,&atten_range,&UseNegData,&entry,&word_out,&actual_pol,NULL);
/* 5.0V is the full-scale voltage of the 12bit ADC */
- globals.ChannelState[channel].Curr_Mon_value=((((float) monitor_word)/4095.0) * 5.0)
+ new_val = ((((float) monitor_word)/4095.0) * 5.0)
/ globals.Flash.mon_vi_ratio[channel][ampl_range][use_neg_data];
- /* convert to even step size */
+ /* averaging */
+ for (i=CURR_MON_MAX_OLD_COUNTS-1;i>0;i--) {
+ globals.ChannelState[channel].old_mon_vals[i]=globals.ChannelState[channel].old_mon_vals[i-1];
+ }
+ globals.ChannelState[channel].old_mon_vals[0] = new_val;
+
+ if (globals.ChannelState[channel].num_mon_vals<CURR_MON_MAX_OLD_COUNTS) {
+ ++globals.ChannelState[channel].num_mon_vals;
+ }
+
+ avg_val = 0.0;
+ for (i=0;i<globals.ChannelState[channel].num_mon_vals;i++) {
+ avg_val += globals.ChannelState[channel].old_mon_vals[i] / globals.ChannelState[channel].num_mon_vals;
+ }
+
step_size=globals.Flash.monitor_step[channel];
- globals.ChannelState[channel].Curr_Mon_value=
- ((float)
- ((long) (globals.ChannelState[channel].Curr_Mon_value/step_size))
- )
- * step_size;
+
+ /* reset average if change is bigger than MAX_STEP_DEV_FROM_AVG steps */
+ if (fabs((new_val-avg_val)/step_size) > MAX_STEP_DEV_FROM_AVG) {
+ globals.ChannelState[channel].num_mon_vals = 1;
+ avg_val = new_val;
+ }
+
+ /* convert to even step size */
+ globals.ChannelState[channel].Curr_Mon_value= ((float) ((long) (avg_val/step_size)) ) * step_size;
/* AVO-8D2: check for duty cycle problems, as a function of measured amplitude */
if ( globals.Flash.hard_current_limit_enabled[channel] &&
@@ -230,9 +248,7 @@ int I2C_Check_Monitors(void)
} else if (globals.MenuStatus.Type_Of_Menu==Main_Menu_On &&
!globals.MenuStatus.Error_Screen &&
!globals.MenuStatus.Nonstd_Display &&
- globals.ChannelState[channel].Curr_Mon_value!=globals.ChannelState[channel].displayed_mon_val &&
- (seconds_since_last > 0))
- /* only update display once a second */
+ globals.ChannelState[channel].Curr_Mon_value!=globals.ChannelState[channel].displayed_mon_val)
{
++update_display;
}