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.
 
 
 
 

144 lines
3.3 KiB

  1. /* Test DAC for stability etc */
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <sys/types.h>
  5. #include <errno.h>
  6. #include <unistd.h>
  7. #include <getopt.h>
  8. #include <stdint.h>
  9. #include <string.h>
  10. #include <syslog.h>
  11. #include <err.h>
  12. #include <linux/serial.h>
  13. #include <sys/signal.h>
  14. #include <sys/time.h>
  15. #include "serial-util.h"
  16. #include "gpib.h"
  17. #include "zoom.h"
  18. #include <math.h>
  19. #include "mt19937ar.h"
  20. void write_random(int zoom, int gpib);
  21. #define info(x...) fprintf(stderr,x)
  22. int g_quit = 0;
  23. void handle_sig(int sig) { g_quit = 1; }
  24. int main(int argc, char *argv[])
  25. {
  26. char *zoomdev=strdup("/dev/serial/by-id/usb-FTDI_"
  27. "FT232R_USB_UART_A6007wc5-if00-port0");
  28. char *gpibdev=strdup("/dev/serial/by-id/usb-Prologix_"
  29. "Prologix_GPIB-USB_Controller_PXQQY20G-if00-port0");
  30. int getopt_index;
  31. int zoom, gpib;
  32. unsigned long seed = 1337;
  33. static struct option long_opts[] = {
  34. { "zoom-device", required_argument, NULL, 'Z' },
  35. { "gpib-device", required_argument, NULL, 'G' },
  36. { "seed", required_argument, NULL, 's' },
  37. { "help", no_argument, NULL, 'h' },
  38. { 0, 0, 0, 0}
  39. };
  40. int help=0;
  41. char c;
  42. while ((c = getopt_long(argc, argv, "Z:G:s:h?",
  43. long_opts, &getopt_index)) != -1) {
  44. switch(c)
  45. {
  46. case 'Z':
  47. free(zoomdev);
  48. zoomdev = strdup(optarg);
  49. break;
  50. case 'G':
  51. free(gpibdev);
  52. gpibdev = strdup(optarg);
  53. break;
  54. case 's':
  55. seed = atol(optarg);
  56. if(seed == 0)
  57. errx(1, "invalid seed: %s", optarg);
  58. break;
  59. case 'h':
  60. case '?':
  61. default:
  62. help = 1;
  63. break;
  64. }
  65. }
  66. if (help) {
  67. fprintf(stderr, "Zoom Nilm DC Test\n");
  68. fprintf(stderr, "usage: %s [options]\n\n", *argv);
  69. fprintf(stderr, " -Z, --zoom-device %-14s "
  70. "zoom NILM serial port\n", "/dev/xxx" /*zoomdev*/);
  71. fprintf(stderr, " -G, --gpib-device %-14s "
  72. "GPIB serial port\n", "/dev/xxx" /*gpibdev*/);
  73. fprintf(stderr, " -s, --seed %-16ld random seed\n", seed);
  74. fprintf(stderr, " -h, --help "
  75. "this help\n");
  76. return 1;
  77. }
  78. signal(SIGINT, handle_sig);
  79. info("Initializing twister with seed %ld\n", seed);
  80. init_genrand(seed);
  81. info("Opening Zoom NILM device %s\n", zoomdev);
  82. if ((zoom = serial_open(zoomdev, 115200)) == -1)
  83. err(1, "failed to open zoom device %s", zoomdev);
  84. info("Opening GPIB device %s\n", gpibdev);
  85. if ((gpib = serial_open(gpibdev, 9600)) == -1)
  86. err(1, "failed to open gpib device %s", gpibdev);
  87. /* do it */
  88. write_random(zoom, gpib);
  89. close(zoom);
  90. close(gpib);
  91. return 0;
  92. }
  93. void write_random(int zoom, int gpib)
  94. {
  95. int dac;
  96. double meas;
  97. struct timeval now;
  98. info("Initializing Zoom NILM\n");
  99. if (zoom_init_nozero(zoom) < 0) goto fail;
  100. info("Initializing GPIB\n");
  101. if (gpib_init(gpib) < 0) goto fail;
  102. info("Initializing Keithley\n");
  103. if (gpib_addr(gpib, 23) < 0) goto fail;
  104. if (keithley2002_init_volts(gpib) < 0) goto fail;
  105. if (isnan(keithley2002_read(gpib))) goto fail;
  106. info("Running\n");
  107. while (!g_quit) {
  108. dac = genrand_int32() & 0x3ff;
  109. zoom_write_dac(zoom, dac);
  110. gettimeofday(&now, NULL);
  111. meas = keithley2002_read(gpib);
  112. printf("%ld.%06ld %d %.12f\n", now.tv_sec, now.tv_usec,
  113. dac, meas);
  114. fflush(stdout);
  115. }
  116. safecleanup:
  117. return;
  118. fail:
  119. info("Failed\n");
  120. goto safecleanup;
  121. }