From 68af1fcfb2e6dfb950f08a667008a3670bbfaa10 Mon Sep 17 00:00:00 2001 From: Daniel Lezcano Date: Tue, 15 Sep 2015 00:46:22 +0200 Subject: Fix rshunt values overwrite and optimize correction loop by computing the value at the moment it is read. Signed-off-by: Daniel Lezcano --- aep.c | 97 ++++++++++++++++++++++++++++++++----------------------------------- 1 file changed, 47 insertions(+), 50 deletions(-) diff --git a/aep.c b/aep.c index c8e19fb..7344436 100644 --- a/aep.c +++ b/aep.c @@ -80,16 +80,18 @@ static struct option long_options[] = { }; struct aep_options { - bool average; - bool raw; - bool energy; + bool average; + bool raw; + bool energy; unsigned long timestamp; unsigned long samples; unsigned long time; unsigned long limit; - unsigned int rshunt[AEP_NR_CHANNELS]; - const char *probe[AEP_MAX_PROBES]; - const char *name[AEP_MAX_PROBES]; + unsigned int rshunt0[AEP_MAX_PROBES]; + unsigned int rshunt1[AEP_MAX_PROBES]; + unsigned int rshunt2[AEP_MAX_PROBES]; + const char *probe[AEP_MAX_PROBES]; + const char *name[AEP_MAX_PROBES]; int nrprobe; }; @@ -146,14 +148,14 @@ static int aep_getopt(int argc, char *argv[], struct aep_options *options) if (!token) return -1; *token = '\0'; - options->rshunt[0] = atoi(aux); + options->rshunt0[options->nrprobe] = atoi(aux); aux = token + 1; token = strstr(aux, ","); if (!token) return -1; *token = '\0'; - options->rshunt[1] = atoi(aux); + options->rshunt1[options->nrprobe] = atoi(aux); aux = token + 1; token = strstr(aux, ","); @@ -164,15 +166,16 @@ static int aep_getopt(int argc, char *argv[], struct aep_options *options) options->name[options->nrprobe] = basename(strdup(options->probe[options->nrprobe])); } - options->rshunt[2] = atoi(aux); - - if (!options->rshunt[0] || !options->rshunt[1] || - !options->rshunt[2]) { - fprintf(stderr, "Invalid rshunt specified\n"); - return -1; - } + options->rshunt2[options->nrprobe] = atoi(aux); } + if (!options->rshunt0[options->nrprobe]) + options->rshunt0[options->nrprobe] = 100; + if (!options->rshunt1[options->nrprobe]) + options->rshunt1[options->nrprobe] = 100; + if (!options->rshunt2[options->nrprobe]) + options->rshunt2[options->nrprobe] = 100; + options->nrprobe++; break; @@ -202,10 +205,6 @@ static int aep_getopt(int argc, char *argv[], struct aep_options *options) return -1; } - for (i = 0; i < AEP_NR_CHANNELS; i++) - if (!options->rshunt[i]) - options->rshunt[i] = 100; - for (i = 0; i < options->nrprobe; i++) { fprintf(stderr, "Using probe '%s'\n", options->probe[i]); } @@ -475,10 +474,19 @@ static inline float aep_char2float(unsigned char *value) return ((float)aep_char2short(value) / 1000.0); } +/* + * ValueCorrected = ValueMeasured * 100 / Rsh (mOhms) + */ +static inline float aep_correction(float rshunt, float value) +{ + return value * 100.0 / rshunt; +} + /* * We can't read more the 10000 samples at once */ -static int aep_read_frame(struct aep_device *dev, struct aep_frame *frame, int nr_frame, unsigned char *buffer) +static int aep_read_frame(struct aep_device *dev, struct aep_frame *frame, + int nr_frame, unsigned char *buffer) { ssize_t len, frame_size; ssize_t nr_bytes, bytes_read; @@ -580,17 +588,17 @@ static int aep_read_frame(struct aep_device *dev, struct aep_frame *frame, int n dev->next_frame++; - frame[i].channel[0][AEP_POWER] = aep_char2float(&buffer[(i * 20) + 2]); - frame[i].channel[0][AEP_VOLTAGE] = aep_char2float(&buffer[(i * 20) + 4]); - frame[i].channel[0][AEP_CURRENT] = aep_char2float(&buffer[(i * 20) + 6]); + frame[i].channel[0][AEP_POWER] = aep_correction(dev->channel[0].rshunt, aep_char2float(&buffer[(i * 20) + 2])); + frame[i].channel[0][AEP_VOLTAGE] = aep_correction(dev->channel[0].rshunt, aep_char2float(&buffer[(i * 20) + 4])); + frame[i].channel[0][AEP_CURRENT] = aep_correction(dev->channel[0].rshunt, aep_char2float(&buffer[(i * 20) + 6])); - frame[i].channel[1][AEP_POWER] = aep_char2float(&buffer[(i * 20) + 8]); - frame[i].channel[1][AEP_VOLTAGE] = aep_char2float(&buffer[(i * 20) + 10]); - frame[i].channel[1][AEP_CURRENT] = aep_char2float(&buffer[(i * 20) + 12]); + frame[i].channel[1][AEP_POWER] = aep_correction(dev->channel[1].rshunt, aep_char2float(&buffer[(i * 20) + 8])); + frame[i].channel[1][AEP_VOLTAGE] = aep_correction(dev->channel[1].rshunt, aep_char2float(&buffer[(i * 20) + 10])); + frame[i].channel[1][AEP_CURRENT] = aep_correction(dev->channel[1].rshunt, aep_char2float(&buffer[(i * 20) + 12])); - frame[i].channel[2][AEP_POWER] = aep_char2float(&buffer[(i * 20) + 14]); - frame[i].channel[2][AEP_VOLTAGE] = aep_char2float(&buffer[(i * 20) + 16]); - frame[i].channel[2][AEP_CURRENT] = aep_char2float(&buffer[(i * 20) + 18]); + frame[i].channel[2][AEP_POWER] = aep_correction(dev->channel[2].rshunt, aep_char2float(&buffer[(i * 20) + 14])); + frame[i].channel[2][AEP_VOLTAGE] = aep_correction(dev->channel[2].rshunt, aep_char2float(&buffer[(i * 20) + 16])); + frame[i].channel[2][AEP_CURRENT] = aep_correction(dev->channel[2].rshunt, aep_char2float(&buffer[(i * 20) + 18])); } return nr_frame; @@ -626,14 +634,6 @@ static bool inline aep_throttle(struct aep_device *dev, struct aep_options *opt) return !!(dev->total_frame % opt->limit); } -/* - * ValueCorrected = ValueMeasured * 100 / Rsh (mOhms) - */ -static inline float aep_correction(float value, unsigned int rshunt) -{ - return value * 100 / rshunt; -} - static inline unsigned long aep_timestamp(unsigned long tbegin, unsigned long tend) { return tend - tbegin; @@ -650,21 +650,11 @@ static void aep_print(struct aep_options *opt, struct aep_device *dev, struct aep_frame *frame, char *buffer) { int i, j; - float values[AEP_MAX_PROBES][AEP_NR_CHANNELS][AEP_CHANNEL_SIZE]; size_t len = 0; if (opt->average && !aep_throttle(dev, opt)) aep_print_average(dev); - for (i = 0; i < AEP_NR_CHANNELS; i++) { - - for (j = 0; j < AEP_CHANNEL_SIZE; j++) - values[dev->index][i][j] = - aep_correction(frame->channel[i][j], opt->rshunt[i]);; - - aep_channel_stat(dev, &dev->channel[i], frame->channel[i]); - } - if (opt->raw && !aep_throttle(dev, opt)) { len = sprintf(buffer, "%s ", dev->name); @@ -673,9 +663,13 @@ static void aep_print(struct aep_options *opt, struct aep_device *dev, len += sprintf(buffer + len, "%lu ", aep_timestamp(opt->timestamp, frame->timestamp)); - for (i = 0; i < AEP_NR_CHANNELS; i++) + for (i = 0; i < AEP_NR_CHANNELS; i++) { + for (j = 0; j < AEP_CHANNEL_SIZE; j++) - len += sprintf(buffer + len, "%f ", values[dev->index][i][j]); + len += sprintf(buffer + len, "%f ", frame->channel[i][j]); + + aep_channel_stat(dev, &dev->channel[i], frame->channel[i]); + } len = sprintf(buffer + len, "\n"); @@ -724,7 +718,10 @@ static void *aep_capture_thread(void *data) aep_dev.index = arg->index; aep_dev.name = aep_opt->name[arg->index]; - + aep_dev.channel[0].rshunt = aep_opt->rshunt0[arg->index]; + aep_dev.channel[1].rshunt = aep_opt->rshunt1[arg->index]; + aep_dev.channel[2].rshunt = aep_opt->rshunt2[arg->index]; + eevt.events = EPOLLIN | EPOLLET; eevt.data.ptr = &aep_dev; -- cgit v1.2.3