diff --git a/afsk/telem.c b/afsk/telem.c index 92882ad7..ec123dd6 100644 --- a/afsk/telem.c +++ b/afsk/telem.c @@ -52,208 +52,9 @@ #define OFF -1 #define ON 1 -int twosToInt(int val, int len); - const char pythonCmd[] = "python3 /home/pi/CubeSatSim/python/voltcurrent.py "; char pythonStr[100], pythonConfigStr[100], busStr[10]; -int map[8] = { 0, 1, 2, 3, 4, 5, 6, 7}; - -/* -struct SensorConfig { - int fd; - uint16_t config; - int calValue; - int powerMultiplier; - int currentDivider; - char commandv[100]; - char commandi[100]; -}; - -struct SensorData { - double current; - double voltage; - double power; -}; - -/** - * @brief Read the data from one of the i2c current sensors. - * - * Reads the current data from the requested i2c current sensor configuration and - * stores it into a SensorData struct. An invalid file descriptor (i.e. less than zero) - * results in a SensorData struct being returned that has both its #current and #power members - * set to NAN. - * - * @param sensor A structure containing sensor configuration including the file descriptor. - * @return struct SensorData A struct that contains the current, voltage, and power readings - * from the requested sensor. - * -struct SensorData read_sensor_data(struct SensorConfig sensor) { - struct SensorData data = { - .current = 0, - .voltage = 0, - .power = 0 - }; - - if (sensor.fd < 0) { - return data; - } - -// FILE* file = popen("python3 /home/pi/CubeSatSim/python/voltage.py 1 0x44", "r"); - FILE* file = popen(sensor.commandv, "r"); - char cmdbuffer[1000]; - fgets(cmdbuffer, 1000, file); - pclose(file); - data.voltage = atof(cmdbuffer); - -// printf("voltage: %s \n", cmdbuffer); - - file = popen(sensor.commandi, "r"); - fgets(cmdbuffer, 1000, file); - pclose(file); - -// printf("current: %s \n", cmdbuffer); - - data.current = atof(cmdbuffer); - -/* - // doesn't read negative currents accurately, shows -0.1mA - wiringPiI2CWriteReg16(sensor.fd, INA219_REG_CALIBRATION, sensor.calValue); - wiringPiI2CWriteReg16(sensor.fd, INA219_REG_CONFIG, sensor.config); - wiringPiI2CWriteReg16(sensor.fd, INA219_REG_CALIBRATION, sensor.calValue); - delay(1); - int value = wiringPiI2CReadReg16(sensor.fd, INA219_REG_CURRENT); - if (value == -1) - { - sensor.fd = -1; - return data; - } - data.current = (float) twosToInt(value, 16) / (float) sensor.currentDivider; - - wiringPiI2CWrite(sensor.fd, INA219_REG_BUSVOLTAGE); - delay(1); // Max 12-bit conversion time is 586us per sample - value = (wiringPiI2CRead(sensor.fd) << 8 ) | wiringPiI2CRead (sensor.fd); - data.voltage = ((float)(value >> 3) * 4) / 1000; - // power has very low resolution, seems to step in 512mW values - delay(1); - data.power = (float) wiringPiI2CReadReg16(sensor.fd, INA219_REG_POWER) * (float) sensor.powerMultiplier; - * - return data; -} - -/** - * @brief Configures an i2c current sensor. - * - * Calculates the configuration values of the i2c sensor so that - * current, voltage, and power can be read using read_sensor_data. - * Supports 16V 400mA and 16V 2.0A settings. - * - * @param sensor A file descriptor that can be used to read from the sensor. - * @param milliAmps The mA configuration, either 400mA or 2A are supported. - * @return struct SensorConfig A struct that contains the configuraton of the sensor. - * -//struct SensorConfig config_sensor(int sensor, int milliAmps) { -struct SensorConfig config_sensor(char *bus, int address, int milliAmps) { - struct SensorConfig data; - - if (access(bus, W_OK | R_OK) < 0) { // Test if I2C Bus is missing - printf("ERROR: %s bus not present \n Check raspi-config Interfacing Options/I2C and /boot/config.txt \n", bus); - data.fd = OFF; - return (data); - } - - char str[100]; - strcpy (str, bus); - char *buss; - const char dash[2] = "-"; - buss = strtok(str, dash); -// printf("buss: %s\n", buss); - buss = strtok(NULL, dash); -// printf("bus: %s\n", buss); - - char result[128]; - int pos = strlen(bus) / sizeof(bus[0]) - 1; -// printf("Bus size %d \n", pos); -// printf("Bus value %d \n", atoi(&bus[pos])); - char command[50] = "timeout 10 i2cdetect -y "; -// strcat (command, &bus[pos]); - 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: %s bus has a problem \n Check I2C wiring and pullup resistors \n", bus); - data.fd = OFF; - return (data); - } - data.fd = ON; - - char spacev[] = " 0x"; - char pythonv[100] = "python3 /home/pi/CubeSatSim/python/voltage.py "; - char pythoni[100] = "python3 /home/pi/CubeSatSim/python/current.py "; - - strcat (pythonv, buss); - strcat (pythonv, spacev); - char addr[10]; - snprintf( addr, 10, "%x", address ); - strcat (pythonv, addr); - strcpy (data.commandv, pythonv); - -// printf("V Command: %s \n", data.commandv); - - char spacei[] = " 0x"; - strcat (pythoni, buss); - strcat (pythoni, spacei); - strcat (pythoni, addr); - strcpy (data.commandi, pythoni); - -// printf("V Command: %s \n", data.commandi); - - /* - data.fd = wiringPiI2CSetupInterface(bus, address); - - data.config = INA219_CONFIG_BVOLTAGERANGE_32V | - INA219_CONFIG_GAIN_1_40MV | - INA219_CONFIG_BADCRES_12BIT | - INA219_CONFIG_SADCRES_12BIT_1S_532US | - INA219_CONFIG_MODE_SANDBVOLT_CONTINUOUS; - - if (milliAmps == 400) { // INA219 16V 400mA configuration - data.calValue = 8192; - data.powerMultiplier = 1; - data.currentDivider = 20; // 40; in Adafruit config - } - else { // INA219 16V 2A configuration - data.calValue = 40960; - data.powerMultiplier = 2; - data.currentDivider = 10; // 20; in Adafruit config - } - - //#ifdef DEBUG_LOGGING -// printf("Sensor %s %x configuration: %d %d %d %d %d\n", bus, address, data.fd, -// data.config, data.calValue, data.currentDivider, data.powerMultiplier); -// printf("Sensor %s %x | ", bus, address); - //#endif -* - return data; -} - -* - -struct SensorConfig sensorV; -struct SensorData readingV; -struct SensorConfig tempSensor; -struct SensorConfig sensor[8]; // 8 current sensors in Solar Power PCB vB4/5 -struct SensorData reading[8]; // 8 current sensors in Solar Power PCB vB4/5 - -*/ +int map[8] = { 0, 1, 2, 3, 4, 5, 6, 7 }; int main(int argc, char *argv[]) { @@ -271,14 +72,11 @@ int main(int argc, char *argv[]) { if (digitalRead(2) != HIGH) { printf("vB3 with TFB Present\n"); -/* sensor[PLUS_X] = config_sensor("/dev/i2c-1", 0x40, 400); - sensor[PLUS_Y] = config_sensor("/dev/i2c-1", 0x41, 400); - sensor[PLUS_Z] = config_sensor("/dev/i2c-1", 0x44, 400); - sensor[BAT] = config_sensor("/dev/i2c-1", 0x45, 400); - sensor[BUS] = config_sensor("/dev/i2c-1", 0x4a, 2000); - sensor[MINUS_X] = config_sensor("/dev/i2c-0", 0x40, 400); - sensor[MINUS_Y] = config_sensor("/dev/i2c-0", 0x41, 400); - sensor[MINUS_Z] = config_sensor("/dev/i2c-0", 0x44, 400); */ + map[BUS] = MINUS_Z; + map[BAT] = BUS; + map[PLUS_Z] = BAT; + map[MINUS_Z] = PLUS_Z; + strcpy(busStr,"1 0"); } else { @@ -288,14 +86,6 @@ int main(int argc, char *argv[]) { if (digitalRead(3) != HIGH) { printf("vB4 Present\n"); -/* sensor[PLUS_X] = config_sensor("/dev/i2c-1", 0x40, 400); - sensor[PLUS_Y] = config_sensor("/dev/i2c-1", 0x41, 400); - sensor[BUS] = config_sensor("/dev/i2c-1", 0x44, 400); - sensor[BAT] = config_sensor("/dev/i2c-1", 0x45, 400); - sensor[PLUS_Z] = config_sensor("/dev/i2c-0", 0x40, 400); - sensor[MINUS_X] = config_sensor("/dev/i2c-0", 0x41, 400); - sensor[MINUS_Y] = config_sensor("/dev/i2c-0", 0x44, 400); - sensor[MINUS_Z] = config_sensor("/dev/i2c-0", 0x45, 400); */ map[BAT] = BUS; map[BUS] = BAT; strcpy(busStr,"1 0"); @@ -307,56 +97,35 @@ int main(int argc, char *argv[]) { if (digitalRead(26) != HIGH) { - printf("vB5 Present\n"); // Don't print normal board detection -/* sensor[PLUS_X] = config_sensor("/dev/i2c-1", 0x40, 400); - sensor[PLUS_Y] = config_sensor("/dev/i2c-1", 0x41, 400); - sensor[BUS] = config_sensor("/dev/i2c-1", 0x45, 400); - sensor[BAT] = config_sensor("/dev/i2c-1", 0x44, 400); */ + printf("vB5 Present\n"); // Don't print normal board detection - if (access("/dev/i2c-11", W_OK | R_OK) >= 0) { // Test if I2C Bus 11 is present - printf("/dev/i2c-11 is present\n\n"); - /* sensor[PLUS_Z] = config_sensor("/dev/i2c-11", 0x40, 400); - sensor[MINUS_X] = config_sensor("/dev/i2c-11", 0x41, 400); - sensor[MINUS_Y] = config_sensor("/dev/i2c-11", 0x44, 400); - sensor[MINUS_Z] = config_sensor("/dev/i2c-11", 0x45, 400); */ - strcpy(busStr,"1 11"); - } else { - /* sensor[PLUS_Z] = config_sensor("/dev/i2c-3", 0x40, 400); - sensor[MINUS_X] = config_sensor("/dev/i2c-3", 0x41, 400); - sensor[MINUS_Y] = config_sensor("/dev/i2c-3", 0x44, 400); - sensor[MINUS_Z] = config_sensor("/dev/i2c-3", 0x45, 400); */ - strcpy(busStr,"1 3"); - } + if (access("/dev/i2c-11", W_OK | R_OK) >= 0) { // Test if I2C Bus 11 is present + printf("/dev/i2c-11 is present\n\n"); + strcpy(busStr,"1 11"); + } else { + strcpy(busStr,"1 3"); + } } else { printf("VB3 Present\n"); -/* sensor[PLUS_X] = config_sensor("/dev/i2c-1", 0x40, 400); - sensor[PLUS_Y] = config_sensor("/dev/i2c-1", 0x41, 400); - sensor[PLUS_Z] = config_sensor("/dev/i2c-1", 0x44, 400); - sensor[BAT] = config_sensor("/dev/i2c-1", 0x45, 400); - sensor[BUS] = config_sensor("/dev/i2c-1", 0x4a, 2000); - sensor[MINUS_X] = config_sensor("/dev/i2c-0", 0x40, 400); - sensor[MINUS_Y] = config_sensor("/dev/i2c-0", 0x41, 400); - sensor[MINUS_Z] = config_sensor("/dev/i2c-0", 0x44, 400); */ map[BUS] = MINUS_Z; map[BAT] = BUS; map[PLUS_Z] = BAT; map[MINUS_Z] = PLUS_Z; strcpy(busStr,"1 0"); } - } + } } // Reading I2C voltage and current sensors - printf("Starting\n"); +// printf("Starting\n"); strcpy(pythonStr, pythonCmd); strcat(pythonStr, busStr); strcat(pythonConfigStr, pythonStr); strcat(pythonConfigStr, " c"); -// FILE* file1 = popen("python3 /home/pi/CubeSatSim/python/voltcurrent.py 1 11 c", "r"); FILE* file1 = popen(pythonConfigStr, "r"); char cmdbuffer[1000]; fgets(cmdbuffer, 1000, file1); @@ -365,13 +134,11 @@ int main(int argc, char *argv[]) { int count1; char *token; -// char cmdbuffer[1000]; -// FILE *file = popen("python3 /home/pi/CubeSatSim/python/voltcurrent.py 1 11", "r"); - FILE* file = popen(pythonStr, "r"); - fgets(cmdbuffer, 1000, file); -// printf("result: %s\n", cmdbuffer); - pclose(file); + FILE* file = popen(pythonStr, "r"); + fgets(cmdbuffer, 1000, file); +// printf("result: %s\n", cmdbuffer); + pclose(file); const char space[2] = " "; token = strtok(cmdbuffer, space); @@ -385,125 +152,51 @@ int main(int argc, char *argv[]) { if (token != NULL) { voltage[count1] = atof(token); - #ifdef DEBUG_LOGGING - printf("voltage: %f ", voltage[count1]); - #endif +// #ifdef DEBUG_LOGGING +// printf("voltage: %f ", voltage[count1]); +// #endif token = strtok(NULL, space); if (token != NULL) { current[count1] = atof(token); - #ifdef DEBUG_LOGGING - printf("current: %f\n", current[count1]); - #endif +// #ifdef DEBUG_LOGGING +// printf("current: %f\n", current[count1]); +// #endif token = strtok(NULL, space); } - } - } - -/* - FILE* file = popen("python3 /home/pi/CubeSatSim/python/voltcurrent.py 1 3 c", "r"); - char cmdbuffer[1000]; - fgets(cmdbuffer, 1000, file); - pclose(file); - - int count; - char *token; -// char cmdbuffer[1000]; - - while (1) { - file = popen("python3 /home/pi/CubeSatSim/python/voltcurrent.py 1 3", "r"); -// char cmdbuffer[1000]; -// char cmdbuffer[1000]; - fgets(cmdbuffer, 1000, file); -// printf("result: %s\n", cmdbuffer); - pclose(file); - - const char space[2] = " "; - token = strtok(cmdbuffer, space); - for (count = 0; count < 8; count++) - { - printf("voltage: %s ", token); - token = strtok(NULL, space); - printf("current: %s\n", token); - token = strtok(NULL, space); - } - - printf("\n"); - } -*/ - // data.voltage = atof(cmdbuffer); - -// } - -// return; - -// { - -// reading[count] = read_sensor_data(sensor[count]); -// printf("Read sensor[%d] % 4.2fV % 6.1fmA % 6.1fmW \n", -// count, reading[count].voltage, reading[count].current, reading[count].power); -// } + } + } printf("\n"); - -// sensorV = config_sensor("/dev/i2c-1", 0x40, 400); -// readingV = read_sensor_data(sensorV); - printf("+X | sensor[%d] % 4.2fV % 6.1fmA \n", -// PLUS_X, readingV.voltage, readingV.current, readingV.power); - PLUS_X, voltage[map[PLUS_X]], current[map[PLUS_X]]); -/* -// sensorV = config_sensor("/dev/i2c-1", 0x41, 400); -// readingV = read_sensor_data(sensorV); - printf("+Y | sensor[%d] % 4.2fV % 6.1fmA % 6.1fmW \n", -// PLUS_Y, readingV.voltage, readingV.current, readingV.power); - PLUS_Y, reading[PLUS_Y].voltage, reading[PLUS_Y].current, reading[PLUS_Y].power); + printf("+X | % 4.2fV % 6.1fmA \n", voltage[map[PLUS_X]], current[map[PLUS_X]]); + + printf("+Y | % 4.2fV % 6.1fmA \n", voltage[map[PLUS_Y]], current[map[PLUS_Y]]); //sensorV = config_sensor("/dev/i2c-0", 0x40, 400); //readingV = read_sensor_data(sensorV); - printf("+Z | sensor[%d] % 4.2fV % 6.1fmA % 6.1fmW \n", -// PLUS_Z, readingV.voltage, readingV.current, readingV.power); - PLUS_Z, reading[PLUS_Z].voltage, reading[PLUS_Z].current, reading[PLUS_Z].power); + printf("+Z | % 4.2fV % 6.1fmA \n", voltage[map[PLUS_Z]], current[map[PLUS_Z]]); // sensorV = config_sensor("/dev/i2c-0", 0x41, 400); // readingV = read_sensor_data(sensorV); - printf("-X | sensor[%d] % 4.2fV % 6.1fmA % 6.1fmW \n", -// MINUS_X, readingV.voltage, readingV.current, readingV.power); - MINUS_X, reading[MINUS_X].voltage, reading[MINUS_X].current, reading[MINUS_X].power); + printf("-X | % 4.2fV % 6.1fmA \n", voltage[map[MINUS_X]], current[map[MINUS_X]]); // sensorV = config_sensor("/dev/i2c-0", 0x44, 400); // readingV = read_sensor_data(sensorV); - printf("-Y | sensor[%d] % 4.2fV % 6.1fmA % 6.1fmW \n", -// MINUS_Y, readingV.voltage, readingV.current, readingV.power); - MINUS_Y, reading[MINUS_Y].voltage, reading[MINUS_Y].current, reading[MINUS_Y].power); + printf("-Y | % 4.2fV % 6.1fmA \n", voltage[map[[MINUS_Y]], current[map[MINUS_Y]]); //sensorV = config_sensor("/dev/i2c-0", 0x45, 400); // readingV = read_sensor_data(sensorV); - printf("-Z | sensor[%d] % 4.2fV % 6.1fmA % 6.1fmW \n", -// MINUS_Z, readingV.voltage, readingV.current, readingV.power); - MINUS_Z, reading[MINUS_Z].voltage, reading[MINUS_Z].current, reading[MINUS_Z].power); + printf("-Z | % 4.2fV % 6.1fmA \n", voltage[map[MINUS_Z]], current[map[MINUS_Z]]); // sensorV = config_sensor("/dev/i2c-1", 0x45, 400); // readingV = read_sensor_data(sensorV); - printf("Bat | sensor[%d] % 4.2fV % 6.1fmA % 6.1fmW \n", -// BAT, readingV.voltage, readingV.current, readingV.power); - BAT, reading[BAT].voltage, reading[BAT].current, reading[BAT].power); + printf("Bat | % 4.2fV % 6.1fmA \n", voltage[map[BAT]], current[map[BAT]]); // sensorV = config_sensor("/dev/i2c-1", 0x44, 400); // readingV = read_sensor_data(sensorV); - printf("Bus | sensor[%d] % 4.2fV % 6.1fmA % 6.1fmW \n", -// BUS, readingV.voltage, readingV.current, readingV.power); - BUS, reading[BUS].voltage, reading[BUS].current, reading[BUS].power); + printf("Bus | % 4.2fV % 6.1fmA \n", voltage[map[BUS]], current[map[BUS]]); */ printf("\n\n"); return 0; } - -int twosToInt(int val,int len) { // Convert twos compliment to integer -// from https://www.raspberrypi.org/forums/viewtopic.php?t=55815 - - if(val & (1 << (len - 1))) - val = val - (1 << len); - - return(val); -}