#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include // #include #include #include #define BASEPORT 0x0378 #define bl 127 #define ubl 63 int xi,y; int ppc_fix; int i, j, xn, yyn, icolor; int xx1, xx2, yy1, yy2; // Global int running, run, runc, graphdriver, graphmode; int nx,ny; long int count; float con1, con2, sumtime, v1c, v2c, xa, denom, circum; float a, sumxx, b, sumxy, sumx, sumy, del, delx, dely; float maxx, maxy, maxxx, maxxy, scalet, scalev; char cho, filename[20], buffer[80], dum[80]; double on[100], off[100], fac, cpu, fr, frc; float x, time1[100], vel[100], rvel[100]; int ntot, s1, s2; float ss1, inter, slop; unsigned char mem1[100], mem2[100], onmem1[100], onmem2[100]; unsigned long tl[100], th[100], ontl[100], onth[100], ptest; unsigned long dum1, dum2; FILE *inf; void menu(); void calbrat(); void setpoints(); void takedat(); //244 void datview(); //157 void velplot(); //167 void testgate(); //98 int main() { printf("\n\n\n ATWOOD MACHINE PROGRAM \n\n\n"); printf("Software by Rich Aguayo (Cal Poly Pomona Physics graduate 91)\n"); printf(" Ray Chen (Cal Poly Pomona Physics graduate 04)\n"); printf(" and Andrew Avila (Biotech Major 07)\n\n"); printf("Hardware by Hector Maciel (Cal Poly Pomona Physics Technician and\n"); printf(" Physics graduate 06)\n\n"); // Get the cpu frequency char filename[]="/proc/cpuinfo"; inf=fopen(filename,"r"); while(fr<100.0) { fscanf(inf,"%s", dum); fr=atof(dum); if(fr>10) frc=fr; // printf("%s %f\n",dum, fr); } cpu=frc*1000000.0; printf("The cpu clock frequency is %f Hz\n\n",cpu); printf("menu:\n\n"); ntot=12; for (i=0;i<100;i++) { time1[i]=(i-1)*1.0; vel[i]=(i-1)*1.0; } // if (ioperm(BASEPORT, 5, 1)) // {perror("ioperm"); exit(1);} iopl(3); circum=30.37; fac=4294967296.0; // cpu=700000000.0; menu(); running=1; do { { printf("input an option (then hit return): \n"); scanf("%c", &cho); switch(cho) { case 'n' : setpoints(); //setpoints menu(); break; case 'd' : takedat(); //takedata break; case 'g' : velplot(); //velplot menu(); break; case 'v' : datview(); //datview menu; break; case 't' : testgate(); //testgate menu; break; case 'm' : menu(); break; case 'q' : running = 2; default : menu(); break; } } }while (running==1); return(0); } void menu() { printf(" ntot = %d \n", ntot); printf(" press n to set the number of data points\n"); printf(" press d to take data\n"); printf(" press g to plot v vs. t\n"); printf(" press v to view data\n"); printf(" press t to test the photogate\n"); printf(" press m to see the menu\n"); printf(" press Q to exit the program\n\n\n"); } void testgate() { for (i=1; i<10;i++) { ptest=inb(BASEPORT+1); printf("%u ", ptest); if (inb(BASEPORT+1)==ubl) printf(" Un-blocked\n"); if (inb(BASEPORT+1)==bl) printf(" Blocked\n"); usleep(1000000); } printf(" \n The test is over\n"); printf("Press t to test again or m to menu\n\n"); } void setpoints() { printf("Input the # of times the photogate will be blocked\n"); printf("when the holder falls to the ground\n"); scanf("%d",&s1); ntot=s1; if (s1<7) ntot=7; if (s1>18) ntot=18; return; } void datview() { printf("Data Points Time(sec) Velocity(cm/sec) \n"); for (i=1; i<=ntot; i++) { printf(" %d %f %f \n",i, time1[i], vel[i]); } } void velplot() { #define NIL (0) char str1[40]; Display *dpy = XOpenDisplay(NIL); //Define Display assert(dpy); Colormap cmap; //To set colors XColor color,ignore; long fgred, fggreen, fgblue; long eventmask = ButtonPressMask|ExposureMask|KeyPressMask; //events for the window XEvent event; int blackColor = BlackPixel(dpy,DefaultScreen(dpy)); //Define colors int whiteColor = WhitePixel(dpy,DefaultScreen(dpy)); cmap = DefaultColormap(dpy,DefaultScreen(dpy)); XAllocNamedColor(dpy,cmap,"red",&color,&ignore); fgred = color.pixel; XAllocNamedColor(dpy,cmap,"green",&color,&ignore); fggreen = color.pixel; XAllocNamedColor(dpy,cmap,"blue",&color,&ignore); fgblue = color.pixel; Window w = XCreateSimpleWindow(dpy, DefaultRootWindow(dpy), 0,0,900,700,0,whiteColor,whiteColor); //Define window XSelectInput(dpy,w,StructureNotifyMask); XMapWindow(dpy,w); //Map the window GC gc = XCreateGC(dpy,w,0,NIL); //Graphics Console XSetForeground(dpy,gc,blackColor); for(;;) { XEvent e; //Notify that the window is set up XNextEvent(dpy,&e); if (e.type==MapNotify) break; } scalet = 1; scalev = 1; maxxx = 0; maxxy = 0; for (i=1; i<=ntot;i++) { if (time1[i] > maxxx) maxxx = time1[i]; if (vel[i] > maxxy) maxxy = vel[i]; } maxx=900; maxy=700; XDrawLine(dpy,w,gc,300,50,300,500); XDrawLine(dpy,w,gc,300,500,850,500); char str2[]="Time"; XDrawString(dpy,w,gc,850,520,str2,strlen(str2)); char str3[]="Velocity"; XDrawString(dpy,w,gc,300,40,str3,strlen(str3)); XFlush(dpy); scalet=(maxx-350.0)/maxxx; delx=maxxx/(maxx-300.0); scalev=(maxy-250.0)/maxxy; dely=maxxy/(maxy-200.0); XSetForeground(dpy,gc,fgblue); for (i=1; i<=ntot; i++) { nx=floor(300.0+time1[i]*scalet); ny=floor(maxy-200.0-scalev*vel[i]); XDrawArc(dpy,w,gc,nx-5,ny-5,10,10,0,21600); } sumx = 0.0; sumxx = 0.0; sumy = 0.0; sumxy = 0.0; for (i=1; i<=ntot;i++) { sumx = sumx + time1[i]; sumxx = sumxx + time1[i]*time1[i]; sumy = sumy + vel[i]; sumxy = sumxy + time1[i]*vel[i]; } del = ntot*sumxx - sumx*sumx; a = (sumxx*sumy-sumx*sumxy)/del; b = (ntot*sumxy-sumx*sumy)/del; XSetForeground(dpy,gc,fggreen); for (i=1; i<(maxx-80);i++) { nx=floor(300+i*delx*scalet); ny=floor(maxy-200-scalev*(b*i*delx+a)); XDrawArc(dpy,w,gc,nx-1,ny-1,2,2,0,21600); } v1c=a+b*time1[1]; v2c=a+b*time1[7]; denom=v2c*v2c-v1c*v1c; if (denom==0) denom=1.0; xa=2.0*circum*b/denom; XSetForeground(dpy,gc,fgred); sprintf(str1," Time Velocity"); XDrawString(dpy,w,gc,5,15,str1,strlen(str1)); for (i=1;i<=ntot;i++) { sprintf(str1,"%10.2f %10.2f",time1[i],vel[i]); XDrawString(dpy,w,gc,5,20+10*i,str1,strlen(str1)); } sprintf(str1,"The slope is %10.2f cm/sec^2",xa*b); XDrawString(dpy,w,gc,400,550,str1,strlen(str1)); sprintf(str1,"The intercept is %10.2f cm/sec^2",xa*a); XDrawString(dpy,w,gc,400,580,str1,strlen(str1)); char str[] = "Press any key to clear graphing window"; XDrawString(dpy,w,gc,40,(maxy-20),str,strlen(str)); XSelectInput(dpy,w,eventmask); XMapWindow(dpy,w); for(;;){ XWindowEvent(dpy,w,eventmask,&event); if (event.type == KeyPress) break; } //Wait till keypressed XFreeGC(dpy,gc); XCloseDisplay(dpy); } void takedat() { asm volatile("rdtsc":"=a"(dum1),"=d"(dum2)); ontl[0]=dum1; onth[0]=dum2; for (i=1; i<=ntot; i++) { count=0; run=1; runc=1; while (run==1 && runc==1) { count=count+1; if (inb(BASEPORT+1)==bl) run=2; if (count>900000000) runc=2; } asm volatile("rdtsc":"=a"(dum1),"=d"(dum2)); tl[i]=dum1; th[i]=dum2; // printf("%d %u %u \n", i, tl[i], th[i]); usleep(200); count=0; run=1; runc=1; while (run==1 && runc==1) { count=count+1; if (inb(BASEPORT+1)==ubl) run=2; if (count>900000000) runc=2; } asm volatile("rdtsc":"=a"(dum1),"=d"(dum2)); ontl[i]=dum1; onth[i]=dum2; // printf("%d %u %u \n", i, ontl[i], onth[i]); usleep(200); } printf("\n %d data points have been recorded. \n\n", (i-1)); off[0]=1; on[0]=1; for (i=1; i<=ntot; i++) { off[i]=(1.0*tl[i]-1.0*ontl[i-1])+fac*(th[i]-onth[i-1]); off[i]=off[i]/cpu; on[i]=(1.0*ontl[i]-1.0*tl[i])+fac*(onth[i]-th[i]); on[i]=on[i]/cpu; if (on[i]==0) on[i]=1; vel[i]=1.0/on[i]; // printf("%f %f \n",on[i],off[i]); } for (i=1; i<=ntot; i++) { sumtime=0.0; for (j=1; j<=i; j++) { sumtime=sumtime+off[j]+on[j]; } time1[i]=sumtime-on[i]/2.0; } // for (i=1; i<=ntot; i++) // { // time1[i]=i; // vel[i]=10+20.0*time1[i]; // } sumx=0.0; sumxx=0.0; sumy=0.0; sumxy=0.0; for (i=1; i<=ntot; i++) { sumx=sumx+time1[i]; sumxx=sumxx+time1[i]*time1[i]; sumy=sumy+vel[i]; sumxy=sumxy+time1[i]*vel[i]; } del=ntot*sumxx-sumx*sumx; a=(sumxx*sumy-sumx*sumxy)/del; b=(ntot*sumxy-sumx*sumy)/del; v1c=a+b*time1[1]; v2c=a+b*time1[7]; denom=v2c*v2c-v1c*v1c; if (denom==0) denom=1; xa=2.0*circum*b/denom; for (i=1; i<=ntot; i++) { rvel[i]=xa*vel[i]; } printf("Data Point Time(sec) Velocity(cm/sec) \n"); for (i=1; i<=ntot; i++) { printf(" %d %f %f \n",i, time1[i], xa*vel[i]); } printf("The acceleration is %f cm/sec2 \n",xa*b); {if (runc==2) printf("Bad Data");} }