Subversion Repositories FlightCtrl

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1993 - 1
/*
2
MS561101BA_movaverage.pde - Reads from the sensors and outputs a moving averaged pressure reading
3
 
4
Copyright (C) 2011 Fabio Varesano <fvaresano@yahoo.it>
5
 
6
Development of this code has been supported by the Department of Computer Science,
7
Universita' degli Studi di Torino, Italy within the Piemonte Project
8
http://www.piemonte.di.unito.it/
9
 
10
 
11
This program is free software: you can redistribute it and/or modify
12
it under the terms of the version 3 GNU General Public License as
13
published by the Free Software Foundation.
14
 
15
This program is distributed in the hope that it will be useful,
16
but WITHOUT ANY WARRANTY; without even the implied warranty of
17
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
GNU General Public License for more details.
19
 
20
You should have received a copy of the GNU General Public License
21
along with this program.  If not, see <http://www.gnu.org/licenses/>.
22
 
23
*/
24
 
25
 
26
//#define DEBUG_V
27
 
28
#include <Wire.h>
29
//#include <DebugUtils.h>
30
#include <MS561101BA.h>
31
 
32
 
33
#define MOVAVG_SIZE 32
34
 
35
MS561101BA baro = MS561101BA();
36
 
37
float movavg_buff[MOVAVG_SIZE];
38
int movavg_i=0;
39
 
40
 
41
void setup() {
42
  Wire.begin();
43
  Serial.begin(9600);
44
  delay(1000);
45
 
46
  // Suppose that the CSB pin is connected to GND.
47
  // You'll have to check this on your breakout schematics
48
  baro.init(MS561101BA_ADDR_CSB_LOW);
49
  delay(100);
50
 
51
  // populate movavg_buff before starting loop
52
  for(int i=0; i<MOVAVG_SIZE; i++) {
53
    movavg_buff[i] = baro.getPressure(MS561101BA_OSR_4096);
54
  }
55
}
56
 
57
void loop() {
58
  Serial.print(" temp: ");
59
  Serial.print(baro.getTemperature(MS561101BA_OSR_4096));
60
  Serial.print(" degC pres: ");
61
  pushAvg(baro.getPressure(MS561101BA_OSR_4096));
62
  Serial.print(getAvg(movavg_buff, MOVAVG_SIZE));
63
  Serial.println(" mbar");
64
}
65
 
66
void pushAvg(float val) {
67
  movavg_buff[movavg_i] = val;
68
  movavg_i = (movavg_i + 1) % MOVAVG_SIZE;
69
}
70
 
71
float getAvg(float * buff, int size) {
72
  float sum = 0.0;
73
  for(int i=0; i<size; i++) {
74
    sum += buff[i];
75
  }
76
  return sum / size;
77
}