/* This program load tests Canon BP511 battery packs */ #include #include #include #include using namespace std; #include #include #include "dataqsdk.h" #include "../DI194RS/DI194RS.h" #ifndef PARPORT #ifdef __OpenBSD__ #define PARPORT "/dev/lpa0" #endif #endif #define PLOTCMD "set title 'BP511 Discharge Curve for pack #%s into 50 ohm load' font 'Helvetica,20'\n" \ "set timestamp '%s' bottom\n" \ "set border 3\n" \ "set xlabel 'Time (sec)'\n" \ "set xtics nomirror\n" \ "set format y '%%4.1f'\n" \ "set ylabel 'Battery Voltage'\n" \ "set ytics nomirror\n" \ "set terminal postscript color\n" \ "set output '%s.ps'\n" \ "plot '-' with lines notitle\n" #define PLOTEOD "e\n" #define RLOAD 49.5 #define DEAD 6.0 int main(void) { int parport; FILE *plotter; char logfilename[256]; FILE *log; char batterynum[256]; dataqsdk DI194; int ChannelList[MAXCHANNELS] = { 0, 1, 2, 3 }; int MethodList[MAXCHANNELS] = {0,0,0,0}; short int offsets[MAXCHANNELS]; float scalefactors[MAXCHANNELS]; time_t starttime, endtime; char timestamp[256]; long int count; bool status; short int samplebuf[MAXCHANNELS]; int t; int i; float V1 = 9; float V2; float I1; float amphours = 0; /* Display startup banner */ printf("BP511 Battery Pack Tester\n"); /* Open parallel port device */ parport = open(PARPORT, O_WRONLY); if (parport < 0) { fprintf(stderr, "ERROR: open() for %s failed, %s\n", PARPORT, strerror(errno)); exit(1); } /* Turn off load switch, just in case */ if (write(parport, "\000", 1) != 1) { fprintf(stderr, "ERROR: write() for %s failed, %s\n", PARPORT, strerror(errno)); exit(1); } /* Get battery pack number */ printf("Battery pack number? "); fflush(stdout); memset(batterynum, 0, sizeof(batterynum)); fgets(batterynum, sizeof(batterynum), stdin); if (strlen(batterynum)) batterynum[strlen(batterynum)-1] = 0; /* Set up for the data acquisition */ DI194.ProductName(PRODUCTNAME); SDKerror(&DI194, "ProductName", TRUE); DI194.DeviceFile(SERIALPORT); SDKerror(&DI194, "DeviceFile", TRUE); DI194.EventPoint(1); SDKerror(&DI194, "EventPoint", TRUE); DI194.ADChannelCount(2); SDKerror(&DI194, "ADChannelCount", TRUE); DI194.ADChannelList(ChannelList); SDKerror(&DI194, "ADChannelList", TRUE); DI194.ADMethodList(MethodList); SDKerror(&DI194, "ADMethodList", TRUE); DI194.SampleRate(1.0); SDKerror(&DI194, "SampleRate", TRUE); GetCalibration(offsets, scalefactors); /* Capture test run start time */ starttime = time(NULL); strftime(timestamp, sizeof(timestamp), "Started at %d %b %Y %H:%M:%S", localtime(&starttime)); /* Start up gnuplot */ plotter = popen("gnuplot", "w"); if (plotter == NULL) { fprintf(stderr, "ERROR: popen() for gnuplot failed, %s\n", strerror(errno)); exit(1); } /* Open the log file */ memset(logfilename, 0, sizeof(logfilename)); snprintf(logfilename, sizeof(logfilename), "%s.log", batterynum); log = fopen(logfilename, "w"); if (log == NULL) { fprintf(stderr, "ERROR: fopen() for %s failed, %s\n", logfilename, strerror(errno)); exit(1); } /* Set up the data plot */ fprintf(plotter, PLOTCMD, batterynum, timestamp, batterynum); /* Print log file header */ fprintf(log, "BP511 Battery Pack Tester\n\n"); fprintf(log, "%s\n", timestamp); fprintf(log, "Battery Pack: %s\n\n", batterynum); /* Turn on load switch */ if (write(parport, "\001", 1) != 1) { fprintf(stderr, "ERROR: write() for %s failed, %s\n", PARPORT, strerror(errno)); exit(1); } /* Start data acquisition */ DI194.Start(); SDKerror(&DI194, "Start", TRUE); /* Main acquisition loop */ for (t = 0; V1 > DEAD; ) { status = DI194.NewData(count); SDKerror(&DI194, "NewData", TRUE); for (i = 0; i < count; i++) { t++; DI194.GetDataEx(samplebuf, 2); SDKerror(&DI194, "GetDataEx", TRUE); V1 = scalefactors[0]*(samplebuf[0] - offsets[0]); V2 = scalefactors[1]*(samplebuf[1] - offsets[1]); I1 = (V1 - V2)/RLOAD; amphours += I1/3600.0; printf("%d sec %1.1f V %1.1f V %1.0f mA %1.0f mAh\n", t, V1, V2, I1*1000, amphours*1000); fflush(stdout); fprintf(log, "%d sec %1.1f V %1.1f V %1.0f mA %1.0f mAh\n", t, V1, V2, I1*1000, amphours*1000); fflush(stdout); fprintf(plotter, "%d %e\n", t, V1); } } /* Turn off load switch */ if (write(parport, "\000", 1) != 1) { fprintf(stderr, "ERROR: write() for %s failed, %s\n", PARPORT, strerror(errno)); exit(1); } /* Stop data acquisition */ DI194.Stop(); SDKerror(&DI194, "Stop", TRUE); /* Capture test run end time */ endtime = time(NULL); strftime(timestamp, sizeof(timestamp), "Ended at %d %b %Y %H:%M:%S", localtime(&endtime)); fprintf(log, "%s\n", timestamp); /* Finish rendering the data plot to PDF */ fprintf(plotter, PLOTEOD); fprintf(plotter, "!ps2pdf '%s.ps' '%s.pdf'\n", batterynum, batterynum); fprintf(plotter, "!rm '%s.ps'\n", batterynum); pclose(plotter); fclose(log); close(parport); return 0; }