Browse Source

add warit's changes

git-svn-id: https://bucket.mit.edu/svn/nilm/zoom@7675 ddd99763-3ecb-0310-9145-efcb8ce7c51f
tags/zoom-1.0
nilm 12 years ago
parent
commit
ac0e30aaf3
5 changed files with 247 additions and 31 deletions
  1. +0
    -13
      pc/USB0-115200.ini
  2. +0
    -13
      pc/USB1-115200.ini
  3. +177
    -4
      pc/calibrate.c
  4. +66
    -1
      pc/gpib.c
  5. +4
    -0
      pc/gpib.h

+ 0
- 13
pc/USB0-115200.ini View File

@@ -1,13 +0,0 @@
define \%P /dev/ttyUSB0
define \%B 115200

set line \%P
if failure { echo "Couldn't open \%P" , exit }
set carrier-watch off
set flow-control none
set speed \%B
set serial 8n1
set key \127 \8
log session /tmp/session.log
connect
quit

+ 0
- 13
pc/USB1-115200.ini View File

@@ -1,13 +0,0 @@
define \%P /dev/ttyUSB1
define \%B 115200

set line \%P
if failure { echo "Couldn't open \%P" , exit }
set carrier-watch off
set flow-control none
set speed \%B
set serial 8n1
set key \127 \8
log session /tmp/session.log
connect
quit

+ 177
- 4
pc/calibrate.c View File

@@ -17,6 +17,9 @@

void startup_zero(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);

#define info(x...) fprintf(stderr,x)

int g_quit = 0;
@@ -29,18 +32,23 @@ int main(int argc, char *argv[])
int getopt_index;
int zoom, gpib;
int do_zero = 0;
int ota_opt = 0;
int ota_sweep_count = 64;
int ota_cmd = 32768;

static struct option long_opts[] = {
{ "zoom-device", required_argument, NULL, 'z' },
{ "gpib-device", required_argument, NULL, 'g' },
{ "startup-zero", required_argument, NULL, 's' },
{ "ota-sweep", required_argument, NULL, 'o' },
{ "ota-hold", required_argument, NULL, 'l' },
{ "help", no_argument, NULL, 'h' },
{ 0, 0, 0, 0 }
{ 0, 0, 0, 0}
};
int help=0;
char c;

while ((c = getopt_long(argc, argv, "z:g:sh?",
while ((c = getopt_long(argc, argv, "z:g:solh?",
long_opts, &getopt_index)) != -1) {
switch(c)
{
@@ -55,7 +63,14 @@ int main(int argc, char *argv[])
case 's':
do_zero = 1;
break;
case 'o':
ota_opt = 1;
break;
case 'l':
ota_opt = 2;
break;
case 'h':

case '?':
default:
help = 1;
@@ -69,6 +84,8 @@ int main(int argc, char *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, " -o, --ota-sweep sweep OTA\n");
fprintf(stderr, " -l, --ota-hold hold OTA constantt\n");
fprintf(stderr, " -h, --help this help\n");
return 1;
}
@@ -88,7 +105,22 @@ int main(int argc, char *argv[])
err(1, "failed to open gpib device %s", gpibdev);

signal(SIGINT, handle_sig);
calibrate(zoom, gpib);

/* sweep OTA only */
if(ota_opt == 1){
sweep_ota(zoom, gpib, ota_sweep_count);

}
else if(ota_opt == 2){
if(argc > 2){
sscanf(argv[2],"%d",&ota_cmd);
}

hold_ota(zoom, gpib, ota_cmd);
}
else{
calibrate(zoom, gpib);
}

close(zoom);
close(gpib);
@@ -157,7 +189,7 @@ fail:
info("Failed (code %d)\n", r);
goto safecleanup;
}
void startup_zero(int zoom)
{
char buf[128];
@@ -175,3 +207,144 @@ void startup_zero(int zoom)
printf("%s\n", buf);
}
}


void sweep_ota(int zoom, int gpib, int ota_sweep_count)
{
double iactual;
int i,j;
int r = 0;
char buf[128];
char zcmd[128];
int mycmd = 0;
int cmd_inc = 4096;
info("Initializing Zoom NILM\n");
if (zoom_init(zoom) < 0) goto fail;

info("Initializing GPIB\n");
if (gpib_init(gpib) < 0) goto fail;

info("Initializing Keithley 2002 Multimeter\n");
if (gpib_addr(gpib, 23) < 0) goto fail;
if (keithley2002_init(gpib) < 0) goto fail;
if (isnan(keithley2002_read(gpib))) goto fail;

buf[0] = '0';
if (safewrite(zoom, buf, 1) != 1)
errx(1, "write failed");
if (fdgets(buf, 128, zoom, 1000) == NULL)
errx(1, "read timeout");
drain(zoom);



info("Sweeping OTA\n");
for (i = 0; i <= 16 && !g_quit; i++){

for(j = -2; j <= 2; j++){
if(mycmd+j > 65535 || mycmd+j < 0)
continue;

if(sprintf(zcmd,"v%.4x",(mycmd +j)) < 5)
errx(1, "fail hex conversion");

if (safewrite(zoom, zcmd, 5) != 5)
errx(1, "write failed");
if (fdgets(buf, 128, zoom, 1000) == NULL)
errx(1, "read timeout");
usleep(100);
iactual = keithley2002_read(gpib);
printf("%d %.8f\n",(int)(mycmd + j), iactual);
}
mycmd += cmd_inc;

}

// return to zero
buf[0] = '0';
if (safewrite(zoom, buf, 1) != 1)
errx(1, "write failed");
if (fdgets(buf, 128, zoom, 1000) == NULL)
errx(1, "read timeout");



safecleanup:
return;

fail:
info("Failed (code %d)\n", r);
goto safecleanup;
}


void hold_ota(int zoom, int gpib, int ota_cmd)
{
double iactual;
double tx;
int i;
int r = 0;
char buf[128];
char zcmd[128];
info("Initializing Zoom NILM\n");
if (zoom_init(zoom) < 0) goto fail;

info("Initializing GPIB\n");
if (gpib_init(gpib) < 0) goto fail;

info("Initializing Keithley 2002 Multimeter\n");
if (gpib_addr(gpib, 23) < 0) goto fail;
if (keithley2002_init2(gpib) < 0) goto fail;
if (isnan(keithley2002_read(gpib))) goto fail;

buf[0] = '0';
if (safewrite(zoom, buf, 1) != 1)
errx(1, "write failed");
if (fdgets(buf, 128, zoom, 1000) == NULL)
errx(1, "read timeout");
drain(zoom);

if(ota_cmd > 65535 || ota_cmd < 0)
errx(1, "ota command out-of-range");

if(sprintf(zcmd,"v%.4x",(ota_cmd)) < 5)
errx(1, "fail hex conversion");

if (safewrite(zoom, zcmd, 5) != 5)
errx(1, "write failed");
if (fdgets(buf, 128, zoom, 1000) == NULL)
errx(1, "read timeout");
usleep(100);

info("Holding OTA\n");
for (i = 0; i <= 500 && !g_quit; i++){

iactual = keithley2002_read2(gpib, &tx);
printf("%.8f %.8f\n", tx, iactual);
}

// return to zero
buf[0] = '0';
if (safewrite(zoom, buf, 1) != 1)
errx(1, "write failed");
if (fdgets(buf, 128, zoom, 1000) == NULL)
errx(1, "read timeout");



safecleanup:
return;

fail:
info("Failed (code %d)\n", r);
goto safecleanup;
}

+ 66
- 1
pc/gpib.c View File

@@ -33,7 +33,7 @@ int gpib_addr(int fd, int addr)

int keithley_init(int fd)
{
double i;
double i;
gput(":syst:beep:stat 1");
gput(":sour:func:mode curr");
gput(":sour:del 0");
@@ -74,3 +74,68 @@ int keithley_off(int fd)
return 0;
}


/* this function is for Keithley2002 Digital multimeter */
int keithley2002_init(int fd)
{
double i;
gput(":trac:cle");
gput(":sens:curr:dc:rang:auto on");
gput(":sens:curr:dc:dig 8");
gput(":form:elem read");
gput(":init:cont off");
drain(fd);
i = keithley2002_read(fd);
if (isnan(i))
return -1;
return 0;

return 0;
}

int keithley2002_init2(int fd)
{
double i;
gput(":trac:cle");
gput(":sens:curr:dc:rang:auto on");
gput(":sens:curr:dc:dig 8");
gput(":form:elem read, time");
gput(":syst:tst:type rel");
gput(":syst:tst:rel:res");
gput(":init:cont off");
drain(fd);
i = keithley2002_read(fd);
if (isnan(i))
return -1;
return 0;

return 0;
}

double keithley2002_read(int fd)
{
char s[128];
double i;
gputv("read?", NAN);
gputv("++read", NAN);
if (fdgets(s, 128, fd, 2000) == NULL)
return NAN;
if (sscanf(s, "%lf", &i) != 1)
return NAN;
return i;

}

double keithley2002_read2(int fd, double* tx)
{
char s[128];
double i;
gputv("read?", NAN);
gputv("++read", NAN);
if (fdgets(s, 128, fd, 2000) == NULL)
return NAN;
if (sscanf(s, "%lf,%lf", &i, tx) < 1)
return NAN;
return i;

}

+ 4
- 0
pc/gpib.h View File

@@ -9,5 +9,9 @@ int keithley_current(int fd, double amps);
double keithley_read(int fd);
int keithley_off(int fd);

int keithley2002_init(int fd);
int keithley2002_init2(int fd);
double keithley2002_read(int fd);
double keithley2002_read2(int fd, double* tx);
#endif


Loading…
Cancel
Save