You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

141 lines
3.1 KiB

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <sys/types.h>
  4. #include <errno.h>
  5. #include <unistd.h>
  6. #include <getopt.h>
  7. #include <stdint.h>
  8. #include <string.h>
  9. #include <syslog.h>
  10. #include <err.h>
  11. #include <linux/serial.h>
  12. #include <sys/signal.h>
  13. #include "serial-util.h"
  14. #include "gpib.h"
  15. #include "zoom.h"
  16. #include "math.h"
  17. void calibrate(int zoom, int gpib);
  18. #define info(x...) fprintf(stderr,x)
  19. int g_quit = 0;
  20. void handle_sig(int sig) { g_quit = 1; }
  21. int main(int argc, char *argv[])
  22. {
  23. char *zoomdev=strdup("/dev/ttyUSB1");
  24. char *gpibdev=strdup("/dev/ttyUSB0");
  25. int getopt_index;
  26. int zoom, gpib;
  27. static struct option long_opts[] = {
  28. { "zoom-device", required_argument, NULL, 'z' },
  29. { "gpib-device", required_argument, NULL, 'g' },
  30. { "help", no_argument, NULL, 'h' },
  31. { 0, 0, 0, 0 }
  32. };
  33. int help=0;
  34. char c;
  35. while ((c = getopt_long(argc, argv, "z:g:h?",
  36. long_opts, &getopt_index)) != -1) {
  37. switch(c)
  38. {
  39. case 'z':
  40. free(zoomdev);
  41. zoomdev = strdup(optarg);
  42. break;
  43. case 'g':
  44. free(gpibdev);
  45. gpibdev = strdup(optarg);
  46. break;
  47. case 'h':
  48. case '?':
  49. default:
  50. help = 1;
  51. break;
  52. }
  53. }
  54. if (help) {
  55. fprintf(stderr, "Zoom Nilm Calibration Tool\n");
  56. fprintf(stderr, "usage: %s [options]\n\n", *argv);
  57. fprintf(stderr, " -z, --zoom-device %-14s zoom NILM serial port\n", zoomdev);
  58. fprintf(stderr, " -g, --gpib-device %-14s GPIB serial port\n", gpibdev);
  59. fprintf(stderr, " -h, --help this help\n");
  60. return 1;
  61. }
  62. if ((zoom = serial_open(zoomdev, 115200)) == -1)
  63. err(1, "failed to open zoom device %s", zoomdev);
  64. if ((gpib = serial_open(gpibdev, 9600)) == -1)
  65. err(1, "failed to open gpib device %s", gpibdev);
  66. signal(SIGINT, handle_sig);
  67. calibrate(zoom, gpib);
  68. close(zoom);
  69. close(gpib);
  70. return 0;
  71. }
  72. void calibrate(int zoom, int gpib)
  73. {
  74. float idesired, iactual;
  75. int i;
  76. int zero;
  77. int dac[ZOOM_SWEEP_COUNT];
  78. int adc[ZOOM_SWEEP_COUNT];
  79. info("Initializing Zoom NILM\n");
  80. if (zoom_init(zoom) < 0) goto fail;
  81. info("Zeroing\n");
  82. if (zoom_zero_start(zoom) < 0) goto fail;
  83. info("Initializing GPIB\n");
  84. if (gpib_init(gpib) < 0) goto fail;
  85. info("Initializing Keithley\n");
  86. if (gpib_addr(gpib, 24) < 0) goto fail;
  87. if (keithley_init(gpib) < 0) goto fail;
  88. if (keithley_current(gpib, 0) < 0) goto fail;
  89. if (isnan(keithley_read(gpib))) goto fail;
  90. info("Stop zeroing\n");
  91. if (zoom_zero_stop(zoom) < 0) goto fail;
  92. info("Sweeping\n");
  93. for (idesired = -1.0; idesired <= 1.0 && !g_quit; idesired += 0.01) {
  94. info("Zeroing\n");
  95. if (zoom_zero_start(zoom) < 0) goto fail;
  96. info("Setting current: %f\n", idesired);
  97. keithley_current(gpib, idesired);
  98. iactual = keithley_read(gpib);
  99. info("Actual current: %f\n", iactual);
  100. info("Stop zeroing\n");
  101. if ((zero = zoom_zero_stop(zoom)) < 0) goto fail;
  102. info("DAC zero point = %d\n", zero);
  103. info("Sweeping\n");
  104. if (zoom_sweep(zoom, dac, adc) < 0) goto fail;
  105. info("Results:\n");
  106. for (i = 0; i < ZOOM_SWEEP_COUNT; i++) {
  107. printf("%8f %d %d\n", iactual, dac[i], adc[i]);
  108. }
  109. }
  110. zoom_zero_start(zoom);
  111. keithley_off(gpib);
  112. usleep(100000);
  113. zoom_zero_stop(zoom);
  114. return;
  115. fail:
  116. info("Failed\n");
  117. return;
  118. }