-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.c
112 lines (93 loc) · 2.98 KB
/
main.c
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
#include <time.h>
#include <stdlib.h>
#include <stdio.h>
#include "cs5463.h"
#include "ipc.h"
void initialize_cs5463(){
unsigned int cycleCount = 4000;
init();
unsigned int* offsetI;
unsigned int* offsetV;
unsigned int* offsetIac;
unsigned int* offsetVac;
offsetI = malloc(sizeof(*offsetI));
offsetV = malloc(sizeof(*offsetV));
offsetIac = malloc(sizeof(*offsetIac));
offsetVac = malloc(sizeof(*offsetVac));
setCycleCount(cycleCount);
setCurrentGain(1.0);
setVoltageGain(1.0);
readCalibrationParams(
"/home/pi/.config/cs5463/calibration.txt",
offsetI, offsetV, offsetIac, offsetVac
);
// DC offset is not used when HPF (High Pass Filter) is enabled
setCurrentOffset((int)*offsetI);
setVoltageOffset((int)*offsetV);
setCurrentACOffset((int)*offsetIac);
setVoltageACOffset((int)*offsetVac);
enableHighPassFilter();
setIGain50();
performContinuousComputation();
cycleCount = getCycleCount();
printf("Status mask: 0x%06d\n", getStatusMask());
printf("Cycle Count: %d\n", cycleCount);
printf("Current Gain: %f\n", getCurrentGain());
printf("Voltage Gain: %f\n", getVoltageGain());
printf("Current Offset: %f\n", getCurrentOffset());
printf("Voltage Offset: %f\n", getVoltageOffset());
printf("Current AC Offset: %d (%f)\n", getCurrentACOffsetInt(),
getCurrentACOffset() * I_FACTOR_RMS);
printf("Voltage AC Offset: %d (%f)\n", getVoltageACOffsetInt(),
getVoltageACOffset() * V_FACTOR_RMS);
getOperationMode();
}
int main() {
double i, v, p, rmsI, rmsV, preal, temp = 0.0;
double gainI, gainV = 0.0;
double q, avgQ = 0.0;
double peakI, peakV, qReact, pf, s = 0.0;
initialize_cs5463();
double measTime;
int sock_fd;
int status;
char buffer[512];
char *string = NULL;
connect_socket(&sock_fd);
while(1) {
status = waitDataReady2(30000);
if (status < 0) {
printf("Measurement timed out after 30 seconds!\n");
initialize_cs5463();
continue;
}
/* t = clock() - t; */
/* measTime = ((double)t) / CLOCKS_PER_SEC; */
/* t = clock(); */
/* printf("----------------------------------------------\n"); */
/* printf("Measurement ready after %f seconds\n", measTime * 10); */
/* printf("----------------------------------------------\n"); */
i = getInstantaneusCurrent();
v = getInstantaneusVolt();
p = getRealPower();
rmsI = getRMSCurrent();
rmsV = getRMSVolt();
preal = getRealPower();
q = getInstantaneousReactivePower();
avgQ = getAverageReactivePower();
peakI = getPeakCurrent();
peakV = getPeakVoltage();
qReact = getReactivePower();
pf = getPowerFactor();
s = getApparentPower();
preal = preal * P_REAL_FACTOR_RMS;
rmsI = rmsI * I_FACTOR_RMS;
rmsV = rmsV * V_FACTOR_RMS;
printf("Measurement ready --> Irms=%f, Vrms=%f, Preal=%f\n\n", rmsI, rmsV, preal);
string = make_json(preal, avgQ, preal, rmsI, rmsV, 50.0);
status = socket_send_data(&sock_fd, string);
if (status == 0) {
break;
}
}
}