|
- #include <stdio.h>
- #include <stdlib.h>
- #include <sys/types.h>
- #include <errno.h>
- #include <unistd.h>
- #include <getopt.h>
- #include <stdint.h>
- #include <string.h>
- #include <syslog.h>
- #include <err.h>
- #include <linux/serial.h>
- #include <sys/signal.h>
- #include "serial-util.h"
- #include "gpib.h"
- #include "zoom.h"
- #include "math.h"
-
- void calibrate(int zoom, int gpib);
- #define info(x...) fprintf(stderr,x)
-
- int g_quit = 0;
- void handle_sig(int sig) { g_quit = 1; }
-
- int main(int argc, char *argv[])
- {
- char *zoomdev=strdup("/dev/ttyUSB1");
- char *gpibdev=strdup("/dev/ttyUSB0");
- int getopt_index;
- int zoom, gpib;
-
- static struct option long_opts[] = {
- { "zoom-device", required_argument, NULL, 'z' },
- { "gpib-device", required_argument, NULL, 'g' },
- { "help", no_argument, NULL, 'h' },
- { 0, 0, 0, 0 }
- };
- int help=0;
- char c;
-
- while ((c = getopt_long(argc, argv, "z:g:h?",
- long_opts, &getopt_index)) != -1) {
- switch(c)
- {
- case 'z':
- free(zoomdev);
- zoomdev = strdup(optarg);
- break;
- case 'g':
- free(gpibdev);
- gpibdev = strdup(optarg);
- break;
- case 'h':
- case '?':
- default:
- help = 1;
- break;
- }
- }
-
- 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", zoomdev);
- fprintf(stderr, " -g, --gpib-device %-14s GPIB serial port\n", gpibdev);
- fprintf(stderr, " -h, --help this help\n");
- return 1;
- }
-
- if ((zoom = serial_open(zoomdev, 115200)) == -1)
- err(1, "failed to open zoom device %s", zoomdev);
- if ((gpib = serial_open(gpibdev, 9600)) == -1)
- err(1, "failed to open gpib device %s", gpibdev);
-
- signal(SIGINT, handle_sig);
- calibrate(zoom, gpib);
-
- close(zoom);
- close(gpib);
- return 0;
- }
-
- void calibrate(int zoom, int gpib)
- {
- float idesired, iactual;
- int i;
- int zero;
- int dac[ZOOM_SWEEP_COUNT];
- int adc[ZOOM_SWEEP_COUNT];
-
- info("Initializing Zoom NILM\n");
- if (zoom_init(zoom) < 0) goto fail;
-
- info("Zeroing\n");
- if (zoom_zero_start(zoom) < 0) goto fail;
-
- info("Initializing GPIB\n");
- if (gpib_init(gpib) < 0) goto fail;
-
- info("Initializing Keithley\n");
- if (gpib_addr(gpib, 24) < 0) goto fail;
- if (keithley_init(gpib) < 0) goto fail;
- if (keithley_current(gpib, 0) < 0) goto fail;
- if (isnan(keithley_read(gpib))) goto fail;
-
- info("Stop zeroing\n");
- if (zoom_zero_stop(zoom) < 0) goto fail;
-
- info("Sweeping\n");
- for (idesired = -1.0; idesired <= 1.0 && !g_quit; idesired += 0.01) {
- info("Zeroing\n");
- if (zoom_zero_start(zoom) < 0) goto fail;
-
- info("Setting current: %f\n", idesired);
- keithley_current(gpib, idesired);
- iactual = keithley_read(gpib);
- info("Actual current: %f\n", iactual);
-
- info("Stop zeroing\n");
- if ((zero = zoom_zero_stop(zoom)) < 0) goto fail;
- info("DAC zero point = %d\n", zero);
-
- info("Sweeping\n");
- if (zoom_sweep(zoom, dac, adc) < 0) goto fail;
-
- info("Results:\n");
- for (i = 0; i < ZOOM_SWEEP_COUNT; i++) {
- printf("%8f %d %d\n", iactual, dac[i], adc[i]);
- }
- }
-
- zoom_zero_start(zoom);
- keithley_off(gpib);
- usleep(100000);
- zoom_zero_stop(zoom);
- return;
-
- fail:
- info("Failed\n");
- return;
- }
|