/* ================================================= LIPS slit stage controll program slitcont.c for Linux (Vine 2.1; Kernel 2.2.17) Windows/Dos Ver 1.0 2001. 1.19 H.Akitaya Ver 1.1 2001. 1.23 H.Akitaya Ver 1.2 2001. 1.24 H.Akitaya ================================================= */ /* to complile... Linux : gcc -DLINUX -O2 -lm -o slitcontrol slitcontrol.c Dos/Win : */ #include #include #include #ifdef LINUX #include #define extern #include #endif #ifdef DOS #include #include #include #endif #include "motor.h" #include "lipsinst.h" #define TRUE 0 #define P_BASE 0x3BC /* lp0 */ #define WAIT 0x80 int port_initialize( void ); int stage_initialize( unsigned int *p_status , unsigned int p_width ); int setorg( unsigned int status , unsigned int p_width ); int moveslit_cw( unsigned int pulse_n, long *p_pos, unsigned int status, unsigned int p_width ); int moveslit_ccw( unsigned int pulse_n, long *p_pos, unsigned int status, unsigned int p_width ); void showhelp( void ); void start_msg( void ); #ifdef LINUX #include "lpio_lnx.c" #endif #ifdef DOS #include "lpio_dos.c" #endif main() { unsigned int o_data, status, p_width; long pos, pulse_n; int error, i; char com_line[255], buf[255]; start_msg(); /* initialize */ pos = 0; p_width = PULSEWIDTH; port_initialize(); /* port initialization */ stage_initialize( &status , p_width ); /* stage intialization */ printf("Stage position : %i\n", pos ); printf("Pulse width : %i micro sec.\n", p_width ); /* main routine */ while(1){ putchar('>'); gets( com_line ); if ( strncmp( com_line, "mv", 2) == TRUE ){ i=0; while( ( buf[i] = com_line[i + 3] ) != '\0' ) i++; pulse_n = atol( buf ); if( pulse_n > 0 ) moveslit_cw( pulse_n, &pos, status, p_width ); if( pulse_n < 0 ) moveslit_ccw( labs(pulse_n), &pos, status, p_width ); printf("Stage position : %i\n", pos ); } if ( strncmp( com_line, "pw", 2) == TRUE ){ i=0; while( ( buf[i] = com_line[i + 3] ) != '\0' ) i++; p_width = atol( buf ); printf("Pulse width (micro sec.) : %i\n", p_width ); } if ( strcmp( com_line, "org" ) == TRUE ){ error = setorg( status, p_width ); pos = 0; printf("Stage position : %i\n", pos ); } if ( strcmp( com_line, "full" ) == TRUE ){ status &= ~B_FULLHALF; out_lp0( status ); printf( "motor speed : full step\n" ); } if ( strcmp( com_line, "half" ) == TRUE ){ status |= B_FULLHALF; out_lp0( status ); printf( "motor speed : half step\n" ); } if ( strcmp( com_line, "hoff" ) == TRUE ){ status |= B_HOFF; out_lp0( status ); printf( "motor current : off\n" ); } if ( strcmp( com_line, "hcton" ) == TRUE ){ status |= B_HCTON; out_lp0( status ); printf( "HCT lamp is turned on\n" ); } if ( strcmp( com_line, "hctoff" ) == TRUE ){ status &= ~B_HCTON; out_lp0( status ); printf( "HCT lamp is turned off\n" ); } if ( strcmp( com_line, "hon" ) == TRUE ){ status &= ~B_HOFF; out_lp0( status ); printf( "motor current : on\n" ); } if ( strcmp( com_line, "help" ) == TRUE || strcmp( com_line, "?" ) == TRUE ){ showhelp(); } if ( strcmp( com_line, "quit" ) == TRUE || strcmp( com_line, "q" ) == TRUE ){ printf("Good bye!!\n"); exit(0); } } } int port_initialize( void ) { unsigned int error; printf("Opening ports ... "); #ifdef LINUX error = ioperm( WAIT, 1, 1); if( error == -1 ){ printf("ERROR!\n"); exit(-1); } error = ioperm( P_BASE, 3, 1); if( error == -1 ){ printf("ERROR!\n"); exit(-1); } #endif printf("OK\n"); return( 0 ); } int stage_initialize( unsigned int *p_status , unsigned int p_width ) { int c; unsigned int status, error; printf("Initializing stage ... "); status = 0x0 & ~B_HOFF | B_FULLHALF & ~B_CDINH & ~B_HCTON; out_lp0( status ); printf("OK status = 0x%x\n", status); printf("Set stage position to the origin (y/n)?" ); c = getchar(); if ( c == 'y' ){ error = setorg( status , p_width ); if( error == -1 ){ printf("ERROR!\n"); exit(-1); } printf("OK\n"); }else{ printf("Skiped ....\n"); } *p_status = status; return(-1); } /* set stage to org. position */ int setorg( unsigned int status , unsigned int p_width ) { unsigned int n_limit; /* find CCW-LIMIT (FULL STEP)*/ printf("Finding the CCW Limit ... "); status &= ~B_FULLHALF; /* full step */ n_limit = 30000; /* timeout pulse */ while( ( in_lp0() & B_CCWLS ) == B_CCWLS ){ out_lp0( status | B_CCWP ); p_wait( p_width ); out_lp0( status & ~B_CCWP ); p_wait( p_width ); if ( --n_limit == 0) return(-1); } printf("Found!!\n"); /* find ORG (HALF STEP) */ printf("Finding the origin ... "); status |= B_FULLHALF; /* half step */ n_limit = 10000; /* timeout pulse */ while( ( in_lp0() & B_ORG ) == B_ORG ){ out_lp0( status | B_CWP ); p_wait( p_width ); out_lp0( status & ~B_CWP); p_wait( p_width ); if ( --n_limit == 0) return(-1); } printf("Found!!\n"); return(0); } /* move slit pulse_n steps in CW direction */ int moveslit_cw( unsigned int pulse_n, long *p_pos, unsigned int status, unsigned int p_width ) { unsigned int i, signal, speed; if( pulse_n <= 0){ printf("Can't move !\n"); return(-1); } if( (status & B_FULLHALF) == B_FULLHALF ){ speed = 1; /* half step */ }else{ speed = 2; /* full step */ } printf("Moving ...\n"); i = 0; while( i < pulse_n ){ signal = in_lp0(); if( (signal & B_CWLS ) != B_CWLS ){ printf("CW Limit !! +%i steps moved\n", (i * speed) ); *p_pos += ( i * speed ); return(0); } out_lp0( status | B_CWP ); p_wait( p_width ); out_lp0( status & ~B_CWP); p_wait( p_width ); i++; } printf("Completed. +%i steps moved\n", (i * speed ) ); *p_pos += (i * speed) ; return(0); } /* move slit pulse_n steps in CCW direction */ int moveslit_ccw( unsigned int pulse_n, long *p_pos, unsigned int status, unsigned int p_width ) { unsigned int i, signal, speed; if( pulse_n <= 0){ printf("Can't move !\n"); return(-1); } if( (status & B_FULLHALF) == B_FULLHALF ){ speed = 1; /* half step */ }else{ speed = 2; /* full step */ } printf("Moving ...\n"); i = 0; while( i < pulse_n ){ signal = in_lp0(); if( ( signal & B_CCWLS ) != B_CCWLS ){ printf("CCW Limit !! -%i steps moved\n", ( i * speed ) ); *p_pos -= i * speed; return(0); } out_lp0( status | B_CCWP ); p_wait( p_width ); out_lp0( status & ~B_CCWP); p_wait( p_width ); i++; } printf("Completed. -%i steps moved\n", ( i * speed ) ); *p_pos -= ( i * speed ); return(0); } /* Show command help */ void showhelp( void ){ printf("============= available commands =============\n"); printf("mv (pulse_n) : move stage (pulse_n) steps\n"); printf(" org : set stage position to the origin\n"); printf(" half : half step (1 micron/step; default)\n"); printf(" full : full step (2 microns/step )\n"); printf(" hon : motor current on (default)\n"); printf(" hoff : motor current off\n"); printf(" hcton : turn on HCT lamp\n"); printf(" hctoff : turn off HCT lamp\n"); printf("pw (p_width) : set pulse width (p_width) micro sec.\n"); printf(" ?, help : show command help (this document)\n"); printf(" q, quit : quit program\n"); printf("==============================================\n"); return; } /* startup messages */ void start_msg( void ){ printf("=================================================\n"); printf(" LIPS slit stage controll program \n"); #ifdef LINUX printf(" for Linux \n"); #endif #ifdef DOS printf(" for Windows/Dos \n"); #endif printf(" Ver 1.2 2001. 1.24 H.Akitaya \n"); printf("=================================================\n"); return; }