Rock Monitoring  V 1.0
Sensors.cpp
Go to the documentation of this file.
1 #include "Sensors.h"
2 
7 {
11 }
12 
16 {
17  pinMode(ANALOG7, INPUT); //Init ADC for potentiometer
18  PWR.setSensorPower(SENS_3V3,SENS_ON); // Sets the 3,3V switch ON
19  delay(2000); //delay to allow the sensors to configure
20  acc1.init(0);
21  acc2.init(1);
22  acc3.init(2);
23 
24  temp1.init(0);
25  temp2.init(1);
26  temp3.init(2);
27  delay(1000); //delay to allow the sensors to configure
28 }
29 
36 {
38 
39  if(acc1.getAccXYZ(&xyz) == 0)
40  {
41  sensors123->sensor1 = xyz;
42  }
43  else
44  {
45  return RETURN_ERROR; //Error
46  }
47 
48  if(acc2.getAccXYZ(&xyz) == 0)
49  {
50  sensors123->sensor2 = xyz;
51  }
52  else
53  {
54  return RETURN_ERROR; //Error
55  }
56 
57 
58  if(acc3.getAccXYZ(&xyz) == 0)
59  {
60  sensors123->sensor3 = xyz;
61  }
62  else
63  {
64  return RETURN_ERROR; //Error
65  }
66 
67  return RETURN_OK; //ok
68 }
69 
78 {
79  uint8_t state = 0;
80 
82  if(acc1.getAccPitchRoll(&pitchRoll) == 0)
83  {
84  allsensors->sensor1 = pitchRoll;
85  }
86  else
87  {
88  state = 1; //Error reading Acceleration
89  }
90  if(acc2.getAccPitchRoll(&pitchRoll) == 0)
91  {
92  allsensors->sensor2 = pitchRoll;
93  }
94  else
95  {
96  state = 1; //Error reading Acceleration
97  }
98  if(acc3.getAccPitchRoll(&pitchRoll) == 0)
99  {
100  allsensors->sensor3 = pitchRoll;
101  }
102  else
103  {
104  state = 1; //Error reading Acceleration
105  }
106 
107  int extension_;
108  getExtension(extension_);
109  allsensors->extension = extension_;
110  allsensors->batteryLevel=PWR.getBatteryLevel();//Set batterylevel
111 
112  int8_t temp;
113  if(temp1.getTemp(temp) == 0)
114  {
115  allsensors->sensTemp1 = temp;
116  }
117  else
118  {
119  state = 2; //Error reading temperature
120  }
121  if(temp2.getTemp(temp) == 0)
122  {
123  allsensors->sensTemp2 = temp;
124  }
125  else
126  {
127  state = 2; //Error reading temperature
128  }
129  if(temp3.getTemp(temp) == 0)
130  {
131  allsensors->sensTemp3 = temp;
132  }
133  else
134  {
135  state = 2; //Error reading temperature
136  }
137 
138  switch(state)
139  {
140  case 1:
141  {
142  return RETURN_ERROR_Acc;
143  }
144  break;
145  case 2:
146  {
147  return RETURN_ERROR_Temp;
148  }
149  break;
150  default:
151  {
152  return RETURN_OK;
153  }
154  break;
155  }
156 }
157 
158 
162 {
163  Sensors::structSensorsXYZ accsensors;
164  int extension_;
165  getExtension(extension_);
166  if(getSensorAcc123(&accsensors) == 0)
167  {
168  USB.println(F(";"));
169  USB.print(F("ACC1X_"));
170  USB.print(accsensors.sensor1.x);
171  USB.println(";");
172  USB.print(F("ACC1Y_"));
173  USB.print(accsensors.sensor1.y);
174  USB.println(";");
175  USB.print(F("ACC1Z_"));
176  USB.print(accsensors.sensor1.z);
177  USB.println(";");
178 
179  USB.print(F("ACC2X_"));
180  USB.print(accsensors.sensor2.x);
181  USB.println(";");
182  USB.print(F("ACC2Y_"));
183  USB.print(accsensors.sensor2.y);
184  USB.println(";");
185  USB.print(F("ACC2Z_"));
186  USB.print(accsensors.sensor2.z);
187  USB.println(";");
188 
189  USB.print(F("ACC3X_"));
190  USB.print(accsensors.sensor3.x);
191  USB.println(";");
192  USB.print(F("ACC3Y_"));
193  USB.print(accsensors.sensor3.y);
194  USB.println(";");
195  USB.print(F("ACC3Z_"));
196  USB.print(accsensors.sensor3.z);
197  USB.println(";");
198 
199  USB.print(F("POT_"));
200  USB.print(extension_);
201  USB.println(";");
202  }
203  else
204  {
205  USB.println(F("Error read sensors acc 1,2 and 3"));
206  }
207 }
208 
209 
213 {
214  Sensors::structSensors sensorsAll;
215  if(getSensorValues(&sensorsAll) == RETURN_OK)
216  {
217  USB.print(F("ACC1pitch"));
218  USB.print(sensorsAll.sensor1.pitch);
219  USB.println(";");
220  USB.print(F("ACC1roll"));
221  USB.print(sensorsAll.sensor1.roll);
222  USB.println(";");
223 
224  USB.print(F("ACC2pitch_"));
225  USB.print(sensorsAll.sensor2.pitch);
226  USB.println(";");
227  USB.print(F("ACC2roll_"));
228  USB.print(sensorsAll.sensor2.roll);
229  USB.println(";");
230 
231  USB.print(F("ACC3pitch_"));
232  USB.print(sensorsAll.sensor3.pitch);
233  USB.println(";");
234  USB.print(F("ACC3roll_"));
235  USB.print(sensorsAll.sensor3.roll);
236  USB.println(";");
237 
238 
239  USB.print(F("POT_ "));
240  USB.print(sensorsAll.extension);
241  USB.println(";");
242 
243  USB.print(F("Temp1_ "));
244  USB.print(sensorsAll.sensTemp1);
245  USB.println(";");
246 
247  USB.print(F("Temp2_"));
248  USB.print(sensorsAll.sensTemp2);
249  USB.println(";");
250 
251  USB.print(F("Temp3_"));
252  USB.print(sensorsAll.sensTemp3);
253  USB.println(";");
254  }
255  else
256  {
257  USB.println(F("Error read sensors"));
258  }
259 }
260 
265 void Sensors::getExtension(int & extension)
266 {
267  int buffer [30];
268 
269  for(int i = 0;i<30;i++)
270  {
271  buffer[i] = analogRead(ANALOG7);
272  }
273 
274  extension = (int)(0.02981 *100* (1023-median(30,buffer))); //calculates the extension in 1/10 mm with median value of 30 measurements
275 }
279 {
280  acc1.goToSleep();
281  acc2.goToSleep();
282  acc3.goToSleep();
283  temp1.goToSleep();
284  temp2.goToSleep();
285  temp3.goToSleep();
286 }
290 {
291  init();
292 }
293 
299 int Sensors::median(int n, int x[]) {
300  int temp;
301  int i, j;
302  // the following two loops sort the array x in ascending order
303  for(i=0; i<n-1; i++)
304  {
305  for(j=i+1; j<n; j++)
306  {
307  if(x[j] < x[i])
308  {
309  // swap elements
310  temp = x[i];
311  x[i] = x[j];
312  x[j] = temp;
313  }
314  }
315  }
316  if(n%2==0)
317  {
318  // if there is an even number of elements, return mean of the two elements in the middle
319  return((x[n/2] + x[n/2 - 1]) / 2.0);
320  } else
321  {
322  // else return the element in the middle
323  return x[n/2];
324  }
325 }
void print()
This method reads the X, Y and Z - Values of all 3 Accelerometers and the extensiomter and prints the...
Definition: Sensors.cpp:161
#define RETURN_ERROR
Definition: Constants.h:4
FXLS8471Q acc2
Definition: Sensors.h:58
void printAll()
This method reads all the sensors and prints them to the serial Port.
Definition: Sensors.cpp:212
#define RETURN_OK
Definition: Constants.h:9
FXLS8471Q::structPitchRoll sensor3
Definition: Sensors.h:34
void wakeUp()
This method is used to wake up the Sensors, it initializes them.
Definition: Sensors.cpp:289
FXLS8471Q::structPitchRoll sensor1
Definition: Sensors.h:32
#define RETURN_ERROR_Temp
Definition: Constants.h:7
#define RETURN_ERROR_Acc
Definition: Constants.h:8
PCT2075 temp2
Definition: Sensors.h:61
void goToSleep()
This method sets the Temperature Sensor in shutdown mode.
Definition: PCT2075.cpp:76
FXLS8471Q acc1
Definition: Sensors.h:57
uint8_t getSensorValues(Sensors::structSensors *allSensors)
This method reads the pitch and Roll Angle- Values of all 3 Accelerometers, all 3 temperatures and th...
Definition: Sensors.cpp:77
void init(uint8_t slaveAddr)
This method initializes the temperature sensor PCT2075.
Definition: PCT2075.cpp:12
void goToSleep()
This method sets all sensors in Standby.
Definition: Sensors.cpp:278
int median(int n, int x[])
This method calculates the median value of the elements in the array.
Definition: Sensors.cpp:299
uint8_t getSensorAcc123(Sensors::structSensorsXYZ *sensors123)
This method gives back the X, Y and Z - Values of all 3 Accelerometers.
Definition: Sensors.cpp:35
void goToSleep()
This method sets the Accelerometer in Standby.
Definition: FXLS8471Q.cpp:166
Class that handles the communication to the Accelerometer FXLS8471Q.
Definition: FXLS8471Q.h:26
PCT2075 temp1
Definition: Sensors.h:60
void getExtension(int &extension)
This method reads the Extension of the potentiometer, it uses the 10Bit- ADC Converter it calculates...
Definition: Sensors.cpp:265
uint8_t getAccXYZ(FXLS8471Q::structXYZ *xyz)
This method returns the values of the X, Y and Z axis of the Accelerometer FXLS8471Q.
Definition: FXLS8471Q.cpp:101
PCT2075 temp3
Definition: Sensors.h:62
FXLS8471Q acc3
Definition: Sensors.h:59
uint8_t getAccPitchRoll(FXLS8471Q::structPitchRoll *PitchRoll)
This method returns the values of the pitch and roll angles of the Accelerometer FXLS8471Q.
Definition: FXLS8471Q.cpp:125
FXLS8471Q::structPitchRoll sensor2
Definition: Sensors.h:33
uint8_t getTemp(int8_t &Temp)
This method gets the temperatur value of the temperature sensor PCT2075.
Definition: PCT2075.cpp:55
Sensors()
Definition: Sensors.cpp:6
void init()
This method initializes the sensors.
Definition: Sensors.cpp:15
FXLS8471Q::structXYZ sensor2
Definition: Sensors.h:19
FXLS8471Q::structXYZ sensor3
Definition: Sensors.h:20
FXLS8471Q::structXYZ sensor1
Definition: Sensors.h:18
uint8_t batteryLevel
Definition: Sensors.h:40
void init(uint8_t slaveAddr)
This method initializes the Accelerometer FXLS8471Q.
Definition: FXLS8471Q.cpp:12