From a59e0c30882bde8855351f0eacd5d7925b966dab Mon Sep 17 00:00:00 2001 From: jmclemo6 Date: Fri, 12 Jul 2019 02:11:42 -0400 Subject: [PATCH 1/3] Add function and struct to read sensor data. --- afsk/main.c | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/afsk/main.c b/afsk/main.c index c8ea7caf..d9c3ad4f 100644 --- a/afsk/main.c +++ b/afsk/main.c @@ -35,6 +35,7 @@ #include #include #include "ina219.h" +#include #define CALLSIGN "" // Put your callsign here! @@ -88,6 +89,42 @@ int x_calValue; int y_fd; // I2C bus 0 address 0x41 int z_fd; // I2C bos 0 address 0x44 +struct SensorData { + double power; + double current; +}; + +/** + * @brief Read the data from one of the i2c current sensors. + * + * Reads the current data from the requested i2c current sensor 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. + * + * WARNING: This function currently relies on the global variables x_calValue, config, x_currentDivider, and x_powerMultiplier. + * + * @param sensor A file descriptor that can be used to read from the sensor. + * @return struct SensorData A struct that contains the power and current reading from the requested sensor. + */ +struct SensorData read_sensor_data(int sensor) { + struct SensorData data = { + .power = NAN, + .current = NAN + }; + + if (sensor < 0) { + return data; + } + + wiringPiI2CWriteReg16(sensor, INA219_REG_CALIBRATION, x_calValue); + wiringPiI2CWriteReg16(sensor, INA219_REG_CONFIG, config); + wiringPiI2CWriteReg16(sensor, INA219_REG_CALIBRATION, x_calValue); + data.current = wiringPiI2CReadReg16(sensor, INA219_REG_CURRENT) / x_currentDivider; + data.power = wiringPiI2CReadReg16(sensor, INA219_REG_POWER) * x_powerMultiplier; + + return data; +} int main(void) { From 73675b2f9f0322dec2aca9410d6b818b69a7cafb Mon Sep 17 00:00:00 2001 From: jmclemo6 Date: Fri, 12 Jul 2019 02:15:59 -0400 Subject: [PATCH 2/3] Change i2c sensor reading code to use the read_sensor_data function --- afsk/main.c | 29 +++++++---------------------- 1 file changed, 7 insertions(+), 22 deletions(-) diff --git a/afsk/main.c b/afsk/main.c index d9c3ad4f..79323b2c 100644 --- a/afsk/main.c +++ b/afsk/main.c @@ -391,37 +391,22 @@ int get_tlm(int tlm[][5]) { } */ // read i2c current sensors // - double current = 0, power = 0, y_current = 0, y_power = 0, z_current = 0, z_power = 0; - if (x_fd != -1) { - wiringPiI2CWriteReg16(x_fd, INA219_REG_CALIBRATION, x_calValue); - wiringPiI2CWriteReg16(x_fd, INA219_REG_CONFIG, config); - wiringPiI2CWriteReg16(x_fd, INA219_REG_CALIBRATION, x_calValue); - current = wiringPiI2CReadReg16(x_fd, INA219_REG_CURRENT) / x_currentDivider; - power = wiringPiI2CReadReg16(x_fd, INA219_REG_POWER) * x_powerMultiplier; - wiringPiI2CWriteReg16(y_fd, INA219_REG_CALIBRATION, x_calValue); - wiringPiI2CWriteReg16(y_fd, INA219_REG_CONFIG, config); - wiringPiI2CWriteReg16(y_fd, INA219_REG_CALIBRATION, x_calValue); - y_current = wiringPiI2CReadReg16(y_fd, INA219_REG_CURRENT) / x_currentDivider; - y_power = wiringPiI2CReadReg16(y_fd, INA219_REG_POWER) * x_powerMultiplier; - wiringPiI2CWriteReg16(z_fd, INA219_REG_CALIBRATION, x_calValue); - wiringPiI2CWriteReg16(z_fd, INA219_REG_CONFIG, config); - wiringPiI2CWriteReg16(z_fd, INA219_REG_CALIBRATION, x_calValue); - z_current = wiringPiI2CReadReg16(z_fd, INA219_REG_CURRENT) / x_currentDivider; - z_power = wiringPiI2CReadReg16(z_fd, INA219_REG_POWER) * x_powerMultiplier; - } + struct SensorData x_data = read_sensor_data(x_fd); + struct SensorData y_data = read_sensor_data(y_fd); + struct SensorData z_data = read_sensor_data(z_fd); printf("-X 0x40 current %4.2f power %4.2f -Y 0x41 current %4.2f power %4.2f -Z 0x44 current %4.2f power %4.2f \n", - current, power, y_current, y_power, z_current, z_power); + x_data.current, x_data.power, y_data.current, y_data.power, z_data.current, z_data.power); // printf("1B: ina219[%d]: %s val: %f \n", SENSOR_40 + CURRENT, ina219[SENSOR_40 + CURRENT], strtof(ina219[SENSOR_40 + CURRENT], NULL)); tlm[1][A] = (int)(strtof(ina219[SENSOR_4A + CURRENT], NULL) / 15 + 0.5) % 100; // Current of 5V supply to Pi tlm[1][B] = (int) (99.5 - strtof(ina219[SENSOR_40 + CURRENT], NULL)/10) % 100; // +X current [4] - tlm[1][C] = (int) (99.5 - current/10) % 100; // X- current [10] + tlm[1][C] = (int) (99.5 - x_data.current/10) % 100; // X- current [10] tlm[1][D] = (int) (99.5 - strtof(ina219[SENSOR_41 + CURRENT], NULL)/10) % 100; // +Y current [7] - tlm[2][A] = (int) (99.5 - y_current/10) % 100; // -Y current [10] + tlm[2][A] = (int) (99.5 - y_data.current/10) % 100; // -Y current [10] tlm[2][B] = (int) (99.5 - strtof(ina219[SENSOR_44 + CURRENT], NULL)/10) % 100; // +Z current [10] // was 70/2m transponder power, AO-7 didn't have a Z panel - tlm[2][C] = (int) (99.5 - z_current/10) % 100; // -Z current (was timestamp) + tlm[2][C] = (int) (99.5 - z_data.current/10) % 100; // -Z current (was timestamp) // tlm[2][C] = (int)((time(NULL) - timestamp) / 15) % 100; tlm[2][D] = (int)(50.5 + strtof(ina219[SENSOR_45 + CURRENT], NULL)/10.0) % 100; // NiMH Battery current From 2bfa17abfd5031d66c4dd28ed7ed8e4b0e9f9b36 Mon Sep 17 00:00:00 2001 From: Jacob McLemore Date: Tue, 16 Jul 2019 13:40:07 +0100 Subject: [PATCH 3/3] Fix indentation in read_sensor_data --- afsk/main.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/afsk/main.c b/afsk/main.c index 79323b2c..58f90813 100644 --- a/afsk/main.c +++ b/afsk/main.c @@ -117,11 +117,11 @@ struct SensorData read_sensor_data(int sensor) { return data; } - wiringPiI2CWriteReg16(sensor, INA219_REG_CALIBRATION, x_calValue); - wiringPiI2CWriteReg16(sensor, INA219_REG_CONFIG, config); - wiringPiI2CWriteReg16(sensor, INA219_REG_CALIBRATION, x_calValue); - data.current = wiringPiI2CReadReg16(sensor, INA219_REG_CURRENT) / x_currentDivider; - data.power = wiringPiI2CReadReg16(sensor, INA219_REG_POWER) * x_powerMultiplier; + wiringPiI2CWriteReg16(sensor, INA219_REG_CALIBRATION, x_calValue); + wiringPiI2CWriteReg16(sensor, INA219_REG_CONFIG, config); + wiringPiI2CWriteReg16(sensor, INA219_REG_CALIBRATION, x_calValue); + data.current = wiringPiI2CReadReg16(sensor, INA219_REG_CURRENT) / x_currentDivider; + data.power = wiringPiI2CReadReg16(sensor, INA219_REG_POWER) * x_powerMultiplier; return data; }