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.
 
 
 
 

138 lines
3.1 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_FT232R_USB_UART_A6007wc5-if00-port0");
  27. char *gpibdev=strdup("/dev/serial/by-id/usb-Prologix_Prologix_GPIB-USB_Controller_PXQQY20G-if00-port0");
  28. int getopt_index;
  29. int zoom, gpib;
  30. unsigned long seed = 1337;
  31. static struct option long_opts[] = {
  32. { "zoom-device", required_argument, NULL, 'Z' },
  33. { "gpib-device", required_argument, NULL, 'G' },
  34. { "seed", required_argument, NULL, 's' },
  35. { "help", no_argument, NULL, 'h' },
  36. { 0, 0, 0, 0}
  37. };
  38. int help=0;
  39. char c;
  40. while ((c = getopt_long(argc, argv, "Z:G:s:h?",
  41. long_opts, &getopt_index)) != -1) {
  42. switch(c)
  43. {
  44. case 'Z':
  45. free(zoomdev);
  46. zoomdev = strdup(optarg);
  47. break;
  48. case 'G':
  49. free(gpibdev);
  50. gpibdev = strdup(optarg);
  51. break;
  52. case 's':
  53. seed = atol(optarg);
  54. if(seed == 0)
  55. errx(1, "invalid seed: %s", optarg);
  56. break;
  57. case 'h':
  58. case '?':
  59. default:
  60. help = 1;
  61. break;
  62. }
  63. }
  64. if (help) {
  65. fprintf(stderr, "Zoom Nilm DC Test\n");
  66. fprintf(stderr, "usage: %s [options]\n\n", *argv);
  67. fprintf(stderr, " -Z, --zoom-device %-14s zoom NILM serial port\n", "/dev/xxx" /*zoomdev*/);
  68. fprintf(stderr, " -G, --gpib-device %-14s GPIB serial port\n", "/dev/xxx" /*gpibdev*/);
  69. fprintf(stderr, " -s, --seed %-16ld random seed\n", seed);
  70. fprintf(stderr, " -h, --help this help\n");
  71. return 1;
  72. }
  73. signal(SIGINT, handle_sig);
  74. info("Initializing twister with seed %ld\n", seed);
  75. init_genrand(seed);
  76. info("Opening Zoom NILM device %s\n", zoomdev);
  77. if ((zoom = serial_open(zoomdev, 115200)) == -1)
  78. err(1, "failed to open zoom device %s", zoomdev);
  79. info("Opening GPIB device %s\n", gpibdev);
  80. if ((gpib = serial_open(gpibdev, 9600)) == -1)
  81. err(1, "failed to open gpib device %s", gpibdev);
  82. /* do it */
  83. write_random(zoom, gpib);
  84. close(zoom);
  85. close(gpib);
  86. return 0;
  87. }
  88. void write_random(int zoom, int gpib)
  89. {
  90. int dac;
  91. double meas;
  92. struct timeval now;
  93. info("Initializing Zoom NILM\n");
  94. if (zoom_init_nozero(zoom) < 0) goto fail;
  95. info("Initializing GPIB\n");
  96. if (gpib_init(gpib) < 0) goto fail;
  97. info("Initializing Keithley\n");
  98. if (gpib_addr(gpib, 23) < 0) goto fail;
  99. if (keithley2002_init_volts(gpib) < 0) goto fail;
  100. if (isnan(keithley2002_read(gpib))) goto fail;
  101. info("Running\n");
  102. while (!g_quit) {
  103. dac = genrand_int32() & 0x3ff;
  104. zoom_write_dac(zoom, dac);
  105. gettimeofday(&now, NULL);
  106. meas = keithley2002_read(gpib);
  107. printf("%ld.%06ld %d %.12f\n", now.tv_sec, now.tv_usec, dac, meas);
  108. fflush(stdout);
  109. }
  110. safecleanup:
  111. return;
  112. fail:
  113. info("Failed\n");
  114. goto safecleanup;
  115. }