-
Notifications
You must be signed in to change notification settings - Fork 86
/
telem.c
152 lines (133 loc) · 4.38 KB
/
telem.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
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
/*
* Displays voltage and current sensors for CubeSatSim
*
* uses python3 code ina219.py
*
*/
#include "main.h"
int main(int argc, char *argv[]) {
int debug = OFF;
if (argc > 1) {
if ( * argv[1] == 'd') {
debug = ON;
}
}
printf("CubeSatSim v2.0 INA219 Voltage and Current Telemetry\n");
map[MINUS_X] = MINUS_Y;
map[PLUS_Z] = MINUS_X;
map[MINUS_Y] = PLUS_Z;
snprintf(busStr, 10, "%d %d", test_i2c_bus(1), test_i2c_bus(3));
// Reading I2C voltage and current sensors
// printf("Starting\n");
strcpy(pythonStr, pythonCmd);
strcat(pythonStr, busStr);
strcat(pythonConfigStr, pythonStr);
strcat(pythonConfigStr, " s");
char cmdbuffer[1000];
FILE *file1 = sopen(pythonConfigStr); // try new function
fgets(cmdbuffer, 1000, file1);
if (debug == ON)
{
printf("New Bus String: %s \n", busStr);
fprintf(stderr, "pythonConfigStr: %s \n", pythonConfigStr);
fprintf(stderr, "pythonStr result: %s\n", cmdbuffer);
}
int count1;
char *token;
const char space[2] = " ";
token = strtok(cmdbuffer, space);
float voltage[9], current[9];
memset(voltage, 0, sizeof(voltage));
memset(current, 0, sizeof(current));
for (count1 = 0; count1 < 8; count1++)
{
if (token != NULL)
{
voltage[count1] = atof(token);
// #ifdef DEBUG_LOGGING
// printf("voltage: %f ", voltage[count1]);
// #endif
token = strtok(NULL, space);
if (token != NULL)
{
current[count1] = atof(token);
if ((current[count1] < 0) && (current[count1] > -1))
current[count1] *= (-1.0);
// #ifdef DEBUG_LOGGING
// printf("current: %f\n", current[count1]);
// #endif
token = strtok(NULL, space);
}
}
}
printf("\n");
printf("+X | % 4.2f V % 5.0f mA \n", voltage[map[PLUS_X]], current[map[PLUS_X]]);
printf("+Y | % 4.2f V % 5.0f mA \n", voltage[map[PLUS_Y]], current[map[PLUS_Y]]);
printf("+Z | % 4.2f V % 5.0f mA \n", voltage[map[PLUS_Z]], current[map[PLUS_Z]]);
printf("-X | % 4.2f V % 5.0f mA \n", voltage[map[MINUS_X]], current[map[MINUS_X]]);
printf("-Y | % 4.2f V % 5.0f mA \n", voltage[map[MINUS_Y]], current[map[MINUS_Y]]);
printf("-Z | % 4.2f V % 5.0f mA \n", voltage[map[MINUS_Z]], current[map[MINUS_Z]]);
printf("Bat | % 4.2f V % 5.0f mA \n", voltage[map[BAT]], current[map[BAT]]);
printf("Bat2 | % 4.2f V % 5.0f mA \n\n", voltage[map[BAT2]], current[map[BAT2]]);
fclose(file1);
return 0;
}
int test_i2c_bus(int bus)
{
int output = bus; // return bus number if OK, otherwise return -1
char busDev[20] = "/dev/i2c-";
char busS[5];
snprintf(busS, 5, "%d", bus);
strcat (busDev, busS);
// printf("Bus Dev String: %s \n", busDev);
if (access(busDev, W_OK | R_OK) >= 0) { // Test if I2C Bus is present
// printf("bus is present\n\n");
char result[128];
const char command_start[] = "timeout 10 i2cdetect -y ";
char command[50];
strcpy (command, command_start);
strcat (command, busS);
// printf("Command: %s \n", command);
FILE *i2cdetect = popen(command, "r");
while (fgets(result, 128, i2cdetect) != NULL) {
;
// printf("result: %s", result);
}
int error = pclose(i2cdetect)/256;
// printf("%s error: %d \n", &command, error);
if (error != 0)
{
printf("ERROR: %d bus has a problem \n Check I2C wiring and pullup resistors \n", bus);
output = -1;
}
} else
{
printf("ERROR: %d bus has a problem \n Check software to see if enabled \n", bus);
output = -1;
}
return(output); // return bus number or -1 if there is a problem with the bus
}
// code by https://stackoverflow.com/questions/25161377/open-a-cmd-program-with-full-functionality-i-o/25177958#25177958
FILE *sopen(const char *program)
{
int fds[2];
pid_t pid;
if (socketpair(AF_UNIX, SOCK_STREAM, 0, fds) < 0)
return NULL;
switch(pid=vfork()) {
case -1: /* Error */
close(fds[0]);
close(fds[1]);
return NULL;
case 0: /* child */
close(fds[0]);
dup2(fds[1], 0);
dup2(fds[1], 1);
close(fds[1]);
execl("/bin/sh", "sh", "-c", program, NULL);
_exit(127);
}
/* parent */
close(fds[1]);
return fdopen(fds[0], "r+");
}