|
|
@@ -15,7 +15,9 @@ |
|
|
|
#include "zoom.h" |
|
|
|
#include "math.h" |
|
|
|
|
|
|
|
void startup_zero(int zoom); |
|
|
|
void zero(int zoom); |
|
|
|
void write_dac(int zoom, int dac); |
|
|
|
void single_sweep(int zoom); |
|
|
|
void calibrate(int zoom, int gpib); |
|
|
|
void sweep_ota(int zoom, int gpib, int ota_sweep_count); |
|
|
|
void hold_ota(int zoom, int gpib, int ota_cmd); |
|
|
@@ -33,17 +35,21 @@ int main(int argc, char *argv[]) |
|
|
|
char *gpibdev=strdup("/dev/serial/by-id/usb-Prologix_Prologix_GPIB-USB_Controller_PXQQY20G-if00-port0"); |
|
|
|
int getopt_index; |
|
|
|
int zoom, gpib; |
|
|
|
int do_write = 0; |
|
|
|
int do_zero = 0; |
|
|
|
int write_cmd = 32768; |
|
|
|
int do_single_sweep = 0; |
|
|
|
int ota_opt = 0; |
|
|
|
int ota_sweep_count = 64; |
|
|
|
int ota_cmd = 32768; |
|
|
|
int tmp; |
|
|
|
char *end; |
|
|
|
|
|
|
|
static struct option long_opts[] = { |
|
|
|
{ "zoom-device", required_argument, NULL, 'z' }, |
|
|
|
{ "gpib-device", required_argument, NULL, 'g' }, |
|
|
|
{ "startup-zero", no_argument, NULL, 's' }, |
|
|
|
{ "zoom-device", required_argument, NULL, 'Z' }, |
|
|
|
{ "gpib-device", required_argument, NULL, 'G' }, |
|
|
|
{ "write-dac", required_argument, NULL, 'w' }, |
|
|
|
{ "zero", no_argument, NULL, 'z' }, |
|
|
|
{ "single-sweep", no_argument, NULL, 's' }, |
|
|
|
{ "ota-sweep", required_argument, NULL, 'o' }, |
|
|
|
{ "ota-hold", required_argument, NULL, 'l' }, |
|
|
|
{ "help", no_argument, NULL, 'h' }, |
|
|
@@ -52,39 +58,46 @@ int main(int argc, char *argv[]) |
|
|
|
int help=0; |
|
|
|
char c; |
|
|
|
|
|
|
|
while ((c = getopt_long(argc, argv, "z:g:so:l:h?", |
|
|
|
while ((c = getopt_long(argc, argv, "Z:G:szw:o:l:h?", |
|
|
|
long_opts, &getopt_index)) != -1) { |
|
|
|
switch(c) |
|
|
|
{ |
|
|
|
case 'z': |
|
|
|
case 'Z': |
|
|
|
free(zoomdev); |
|
|
|
zoomdev = strdup(optarg); |
|
|
|
break; |
|
|
|
case 'g': |
|
|
|
case 'G': |
|
|
|
free(gpibdev); |
|
|
|
gpibdev = strdup(optarg); |
|
|
|
break; |
|
|
|
case 'w': |
|
|
|
do_write = 1; |
|
|
|
write_cmd = strtoul(optarg, &end, 0); |
|
|
|
if (*end) { |
|
|
|
fprintf(stderr, "bad number %s\n", optarg); |
|
|
|
help = 1; |
|
|
|
} |
|
|
|
break; |
|
|
|
case 's': |
|
|
|
do_single_sweep = 1; |
|
|
|
break; |
|
|
|
case 'z': |
|
|
|
do_zero = 1; |
|
|
|
break; |
|
|
|
case 'o': |
|
|
|
ota_opt = 1; |
|
|
|
tmp = strtoul(optarg, &end, 0); |
|
|
|
ota_sweep_count = strtoul(optarg, &end, 0); |
|
|
|
if (*end) { |
|
|
|
fprintf(stderr, "bad number %s\n", optarg); |
|
|
|
help = 1; |
|
|
|
} else { |
|
|
|
ota_sweep_count = tmp; |
|
|
|
} |
|
|
|
break; |
|
|
|
case 'l': |
|
|
|
ota_opt = 2; |
|
|
|
tmp = strtoul(optarg, &end, 0); |
|
|
|
ota_cmd = strtoul(optarg, &end, 0); |
|
|
|
if (*end) { |
|
|
|
fprintf(stderr, "bad number %s\n", optarg); |
|
|
|
help = 1; |
|
|
|
} else { |
|
|
|
ota_cmd = tmp; |
|
|
|
} |
|
|
|
break; |
|
|
|
case 'h': |
|
|
@@ -99,9 +112,11 @@ int main(int argc, char *argv[]) |
|
|
|
if (help) { |
|
|
|
fprintf(stderr, "Zoom Nilm Calibration Tool\n"); |
|
|
|
fprintf(stderr, "usage: %s [options]\n\n", *argv); |
|
|
|
fprintf(stderr, " -z, --zoom-device %-14s zoom NILM serial port\n", "/dev/xxx" /*zoomdev*/); |
|
|
|
fprintf(stderr, " -g, --gpib-device %-14s GPIB serial port\n", "/dev/xxx" /*gpibdev*/); |
|
|
|
fprintf(stderr, " -s, --startup-zero assist startup by writing '0' to zoom NILM\n"); |
|
|
|
fprintf(stderr, " -Z, --zoom-device %-14s zoom NILM serial port\n", "/dev/xxx" /*zoomdev*/); |
|
|
|
fprintf(stderr, " -G, --gpib-device %-14s GPIB serial port\n", "/dev/xxx" /*gpibdev*/); |
|
|
|
fprintf(stderr, " -w, --write-dac %-14d write one value to the DAC constantly\n", write_cmd); |
|
|
|
fprintf(stderr, " -s, --single-sweep do a single sweep on the PIC\n"); |
|
|
|
fprintf(stderr, " -z, --zero set DAC value such that ADC input is centered\n"); |
|
|
|
fprintf(stderr, " -o, --ota-sweep %-14d sweep OTA\n", ota_sweep_count); |
|
|
|
fprintf(stderr, " -l, --ota-hold %-14d hold OTA constant\n", ota_cmd); |
|
|
|
fprintf(stderr, " -h, --help this help\n"); |
|
|
@@ -113,8 +128,20 @@ int main(int argc, char *argv[]) |
|
|
|
if ((zoom = serial_open(zoomdev, 115200)) == -1) |
|
|
|
err(1, "failed to open zoom device %s", zoomdev); |
|
|
|
|
|
|
|
if (do_write) { |
|
|
|
write_dac(zoom, write_cmd); |
|
|
|
close(zoom); |
|
|
|
return 0; |
|
|
|
} |
|
|
|
|
|
|
|
if (do_zero) { |
|
|
|
startup_zero(zoom); |
|
|
|
zero(zoom); |
|
|
|
close(zoom); |
|
|
|
return 0; |
|
|
|
} |
|
|
|
|
|
|
|
if (do_single_sweep) { |
|
|
|
single_sweep(zoom); |
|
|
|
close(zoom); |
|
|
|
return 0; |
|
|
|
} |
|
|
@@ -205,16 +232,20 @@ fail: |
|
|
|
goto safecleanup; |
|
|
|
} |
|
|
|
|
|
|
|
void startup_zero(int zoom) |
|
|
|
void write_dac(int zoom, int dac) |
|
|
|
{ |
|
|
|
char buf[128]; |
|
|
|
|
|
|
|
if (zoom_init(zoom) < 0) |
|
|
|
errx(1, "init failed"); |
|
|
|
|
|
|
|
if (dac < 0) |
|
|
|
dac = 0; |
|
|
|
if (dac > 65535) |
|
|
|
dac = 65535; |
|
|
|
while (!g_quit) { |
|
|
|
buf[0] = '0'; |
|
|
|
if (safewrite(zoom, buf, 1) != 1) |
|
|
|
sprintf(buf, "v%04x", dac); |
|
|
|
if (safewrite(zoom, buf, 5) != 5) |
|
|
|
errx(1, "write failed"); |
|
|
|
if (fdgets(buf, 128, zoom, 1000) == NULL) |
|
|
|
errx(1, "read timeout"); |
|
|
@@ -223,6 +254,55 @@ void startup_zero(int zoom) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
void single_sweep(int zoom) |
|
|
|
{ |
|
|
|
int i, r; |
|
|
|
int dac[ZOOM_SWEEP_COUNT]; |
|
|
|
int adc[ZOOM_SWEEP_COUNT]; |
|
|
|
|
|
|
|
info("Initializing Zoom NILM\n"); |
|
|
|
if (zoom_init_nozero(zoom) < 0) goto fail; |
|
|
|
|
|
|
|
info("Sweeping\n"); |
|
|
|
if ((r = zoom_sweep(zoom, dac, adc)) < 0) goto fail; |
|
|
|
|
|
|
|
info("Done\n"); |
|
|
|
for (i = 0; i < ZOOM_SWEEP_COUNT; i++) { |
|
|
|
printf("%d %d\n", dac[i], adc[i]); |
|
|
|
} |
|
|
|
|
|
|
|
return; |
|
|
|
|
|
|
|
fail: |
|
|
|
info("Failed (code %d)\n", r); |
|
|
|
return; |
|
|
|
} |
|
|
|
|
|
|
|
void zero(int zoom) |
|
|
|
{ |
|
|
|
int r; |
|
|
|
int dac; |
|
|
|
|
|
|
|
info("Initializing Zoom NILM\n"); |
|
|
|
if (zoom_init(zoom) < 0) goto fail; |
|
|
|
|
|
|
|
info("Starting zeroing, ^C to stop\n"); |
|
|
|
if (zoom_zero_start(zoom) < 0) goto fail; |
|
|
|
|
|
|
|
while (!g_quit) |
|
|
|
usleep(100000); |
|
|
|
|
|
|
|
info("Stop zeroing\n"); |
|
|
|
if ((dac = zoom_zero_stop(zoom)) < 0) goto fail; |
|
|
|
info("DAC zero point = 0x%04x %d\n", dac, dac); |
|
|
|
|
|
|
|
info("Done\n"); |
|
|
|
return; |
|
|
|
|
|
|
|
fail: |
|
|
|
info("Failed (code %d)\n", r); |
|
|
|
return; |
|
|
|
} |
|
|
|
|
|
|
|
void sweep_ota(int zoom, int gpib, int ota_sweep_count) |
|
|
|
{ |
|
|
|