-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolarCharacteristic.ino
417 lines (356 loc) · 9.76 KB
/
SolarCharacteristic.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
// #define AIRSPEED_SENSOR
// #define LCD
#define SOLAR
// #define LOADCELL
#define RTC
// #define BAT
#define CURRENT_SENSOR
// #define SWITCH
#define PWM
#define DATALOGGER
// #define DEBUG_MODE
/************ AIRSPEED *************/
#ifdef AIRSPEED_SENSOR
#include "ms4525do.h"
bfs::Ms4525do ms4525do;
float defaultPressureValue = 0.0;
#define Nb_SAMPLE 10
void calibAirspeedSensor() {
for (int i=0; i<Nb_SAMPLE; i++) {
while (!ms4525do.Read()) {}
defaultPressureValue += ms4525do.pres_pa();
delay(100);
}
defaultPressureValue = defaultPressureValue/ (float)Nb_SAMPLE;
#ifdef DEBUG_MODE
Serial.println("Default difference pressure: " + String(defaultPressureValue));
#endif
}
float getAirspeed(float pressureDiff) {
float deltaP = pressureDiff - defaultPressureValue;
return sqrt(2*deltaP/1.225);
}
#endif
/************ BATTERY **************/
#ifdef BAT
#define BAT1
// #define BAT2
#endif
#ifdef BAT1
// SimpleKalmanFilter bat1VoltageFilter(1, 1, 0.3);
#define PIN_BAT1 A6
const float bat_1_voltage_divider_coefficient = 6.0;
float getVoltageBat1() {
int rawReading = analogRead(PIN_BAT1);
float voltage = 5.0 * rawReading / 1023 * bat_1_voltage_divider_coefficient;
return voltage;
}
// float getBat1FilteredVoltage() {
// return bat1VoltageFilter.updateEstimate(getVoltageBat1());
// }
#endif
#ifdef BAT2
// SimpleKalmanFilter bat2VoltageFilter(1, 1, 0.3);
#define PIN_BAT2 A3
const float bat_2_voltage_divider_coefficient = 6.0;
float getVoltageBat2() {
int rawReading = analogRead(PIN_BAT2);
float voltage = 5.0 * rawReading / 1023 * bat_2_voltage_divider_coefficient;
return voltage;
}
// float getBat2FilteredVoltage() {
// return bat2VoltageFilter.updateEstimate(getVoltageBat2());
// }
#endif
/************ SOLAR **************/
#ifdef SOLAR
#define PIN_SOLAR A2
const float solar_voltage_divider_coefficient = 6.0;
float getVoltageSolar() {
int rawReading = analogRead(PIN_SOLAR);
float voltage = 5.0 * rawReading / 1023 * solar_voltage_divider_coefficient;
return voltage;
}
#endif
/************ LOADCELL **************/
#ifdef LOADCELL
#include "HX711.h"
#define PIN_LOADCELL_DT A0
#define PIN_LOADCELL_SCK A1
HX711 loadcell;
float calibrationFactor = 480.73;
// const float calibObjectWeight = 390.0; //g
bool calibLoadcell() {
if (loadcell.is_ready()) {
// loadcell.set_scale();
// delay(500);
// loadcell.tare();
// loadcell.set_scale(calibrationFactor);
return true;
}
else {
return false;
}
}
float getWeight() {
if (loadcell.wait_ready_timeout(200)) {
return loadcell.get_units(5);
}
return 0.0;
}
#endif
/************ RTC **************/
#ifdef RTC
#define DS1307
// #define DS1302
#endif
#ifdef DS1307
#include <Wire.h> // must be included here so that Arduino library object file references work
#include <RtcDS1307.h>
RtcDS1307<TwoWire> Rtc(Wire);
bool wasError(const char* errorTopic = "")
{
uint8_t error = Rtc.LastError();
if (error != 0)
{
// we have a communications error
// see https://www.arduino.cc/reference/en/language/functions/communication/wire/endtransmission/
// for what the number means
#ifdef DEBUG_MODE
Serial.print("[");
Serial.print(errorTopic);
Serial.print("] WIRE communications error (");
Serial.print(error);
Serial.print(") : ");
#endif
switch (error)
{
case Rtc_Wire_Error_None:
#ifdef DEBUG_MODE
Serial.println("(none?!)");
#endif
break;
case Rtc_Wire_Error_TxBufferOverflow:
#ifdef DEBUG_MODE
Serial.println("transmit buffer overflow");
#endif
break;
case Rtc_Wire_Error_NoAddressableDevice:
#ifdef DEBUG_MODE
Serial.println("no device responded");
#endif
break;
case Rtc_Wire_Error_UnsupportedRequest:
#ifdef DEBUG_MODE
Serial.println("device doesn't support request");
#endif
break;
case Rtc_Wire_Error_Unspecific:
#ifdef DEBUG_MODE
Serial.println("unspecified error");
#endif
break;
case Rtc_Wire_Error_CommunicationTimeout:
#ifdef DEBUG_MODE
Serial.println("communications timed out");
#endif
break;
}
return true;
}
return false;
}
void syncTime() {
RtcDateTime compiled = RtcDateTime(__DATE__, __TIME__);
if (!Rtc.IsDateTimeValid())
{
if (!wasError("setup IsDateTimeValid"))
{
#ifdef DEBUG_MODE
Serial.println("RTC lost confidence in the DateTime!");
#endif
Rtc.SetDateTime(compiled);
}
}
if (!Rtc.GetIsRunning())
{
if (!wasError("setup GetIsRunning"))
{
#ifdef DEBUG_MODE
Serial.println("RTC was not actively running, starting now");
#endif
Rtc.SetIsRunning(true);
}
}
RtcDateTime now = Rtc.GetDateTime();
if (!wasError("setup GetDateTime"))
{
if (now < compiled)
{
#ifdef DEBUG_MODE
Serial.println("RTC is older than compile time, updating DateTime");
#endif
Rtc.SetDateTime(compiled);
}
else if (now > compiled)
{
#ifdef DEBUG_MODE
Serial.println("RTC is newer than compile time, this is expected");
#endif
}
else if (now == compiled)
{
#ifdef DEBUG_MODE
Serial.println("RTC is the same as compile time, while not expected all is still fine");
#endif
}
}
// never assume the Rtc was last configured by you, so
// just clear them to your needed state
Rtc.SetSquareWavePin(DS1307SquareWaveOut_Low);
wasError("setup SetSquareWavePin");
}
#endif
/************ CURRENT SENSOR **************/
#ifdef CURRENT_SENSOR
// #define ACS_712_30A
// #define ACS_712_20A
#define ACS_712_5A
#endif
#define INTERNAL_RESISTOR 0.05767 // Ohm
double computeAdjustedVoltage(double voltage, double filteredCurrent) {
return voltage + filteredCurrent * INTERNAL_RESISTOR;
}
#ifdef CURRENT_SENSOR
#include "ACS712.h"
#include <SimpleKalmanFilter.h>
#define PIN_ACS712 A7
const float VCC = 5.0;
#ifdef ACS_712_30A
float sensitivity = 66; // 100mV/A --> 20A, 185mV/A ---> 5A, 66mv/A -->30A
#elif ACS_712_20A
float sensitivity = 100;
#else
float sensitivity = 185;
#endif
SimpleKalmanFilter currentFilter(1, 1, 0.3);
ACS712 ACS(PIN_ACS712, VCC, 1023, sensitivity);
float getCurrent() {
return ACS.mA_DC()/1000.0;
}
float getFilteredCurrent(float current) {
return currentFilter.updateEstimate(current);
}
#endif
/************ SWITCH **************/
#ifdef SWITCH
#define PIN_SW1 7
#define PIN_SW2 8
#endif
/************ PWM *************/
#ifdef PWM
#define PIN_PWM 9
#endif
/************ DATALOGGER *************/
#ifdef DATALOGGER
#define DATALOGGER_CS 10
#include <SPI.h>
#include <SD.h>
#define MESSAGE_SIZE sizeof(Message)
const int chipSelect = 10; // CS
File dataFile;
String fileName = "data.txt";
bool bIsRecording = false;
void logData(){
if (!bIsRecording) {
dataFile = SD.open(fileName, FILE_WRITE);
String data = "hour:minute:second current voltage";
dataFile.println(data);
dataFile.close();
bIsRecording = true;
}
else {
dataFile = SD.open(fileName, FILE_WRITE);
#ifdef RTC
RtcDateTime currentTime = Rtc.GetDateTime();
uint8_t hour = (uint8_t)(currentTime.Hour());
uint8_t minute = (uint8_t)(currentTime.Minute());
uint8_t second = (uint8_t)(currentTime.Second());
#endif
#ifdef CURRENT_SENSOR
double rawCurrent = getCurrent();
double filteredCurrent = getFilteredCurrent(rawCurrent);
#endif
#ifdef BAT1
double voltage = getVoltageBat1();
#endif
#ifdef BAT2
double voltage = getVoltageBat2();
#endif
#ifdef SOLAR
double voltage = getVoltageSolar();
#endif
String data = "";
#ifdef RTC
data += String(hour) + ":" + String(minute) + ":" + String(second);
#endif
#ifdef CURRENT_SENSOR
data += " " + String(filteredCurrent);
#ifdef DEBUG_MODE
Serial.println("Current: " + String(filteredCurrent) + " A");
Serial.println("Power: " + String(filteredCurrent*voltage) + " W");
#endif
#endif
#ifdef SOLAR
data += " " + String(voltage);
#ifdef DEBUG_MODE
Serial.println("Voltage: " + String(voltage) + " V");
#endif
#endif
dataFile.println(data);
dataFile.close();
}
}
#endif
/**********************************/
void setup() {
bool isAllInitiated = true;
#ifdef DEBUG_MODE
Serial.begin(57600);
#endif
#ifdef PWM
pinMode(PIN_PWM, OUTPUT);
#endif
#ifdef CURRENT_SENSOR
pinMode(PIN_ACS712, INPUT);
ACS.autoMidPoint();
#endif
#ifdef SOLAR
pinMode(PIN_SOLAR, INPUT);
#endif
#ifdef RTC
Rtc.Begin();
// Wire.setWireTimeout(3000 /* us */, true /* reset_on_timeout */);
// syncTime();
#endif
#ifdef DATALOGGER
isAllInitiated = isAllInitiated && SD.begin(DATALOGGER_CS);
#endif
#ifdef DEBUG_MODE
if (isAllInitiated) {
Serial.println("All devices are well initiated");
}
else {
Serial.println("Some devices are not well initiated");
}
#endif
#ifdef DATALOGGER
for (int i=254; i>=0; i--) {
analogWrite(PIN_PWM, i);
logData();
delay(150);
}
#endif
}
void loop() {
}