/* ************************************************************** */ /* * Module SXDRIVE * This module contains I/O functions for the Compumotor SX model * indexer/drive controlled using RS232. * * Zachary Wolf * 1/22/02 */ /* ************************************************************** */ /* INCLUDES */ #include #include #include #include #include #include "sxdrive.h" /* ************************************************************** */ /* PRIVATE FUNCTIONS */ int sxdrive_open_dev(int rs232_dev_addr); void sxdrive_close_dev(int dev_ID); int sxdrive_out(int dev_ID, char *buf); int sxdrive_in(int dev_ID, char* buf); int sxdrive_check_dev_open(int dev_ID); int sxdrive_read_error_status(int dev_ID); void sxdrive_hold_until_move_complete(int dev_ID); void sxdrive_message(char* msg); void sxdrive_error(char* msg); /* ************************************************************** */ /* PRIVATE DEVICE TABLE */ /* * This table allows several devices of the same type to be * used in the system. * dev_addr, contains the addresses of opened devices * dev_count, contains the number of devices open of this type */ static int dev_addr[SXDRIVE_MAX_NUM_DEV + 1]; static int dev_count; /* ************************************************************** */ /* PRIVATE GLOBAL VARIABLES */ /* * com_port, port number for I/O * cmd, buffer for RS232 I/O strings * msg, buffer for message strings to Standard I/O */ static int com_port; static char cmd[SXDRIVE_MAX_CMD + 1]; static char msg[SXDRIVE_MAX_CMD + 1]; /* ************************************************************** */ /* PUBLIC FUNCTIONS */ /* ************************************************************** */ /* * sxdrive_init * This function opens the device, queries for ID, and * initializes the device to a known state. * * Input: * rs232_com_port, RS232 com port the device is connected to (1 to 32) * rs232_dev_addr, RS232 address of the device (00 to 99, 00 is reserved) * * Output: * ID, identifier for future references to the device * * Zachary Wolf * 1/22/02 */ void sxdrive_init(int rs232_com_port, int rs232_dev_addr, int* ID) { /* Declare variables */ int dev_ID; int err; /* Message */ sxdrive_message(""); sxdrive_message("Initializing the SXDRIVE..."); /* Check input parameters */ if (rs232_com_port < 1 || rs232_com_port > 32) { Fmt(msg, "%s 99) { Fmt(msg, "%s SXDRIVE_MAX_NUM_DEV) { Fmt(msg, "%s 9999.99) { Fmt(msg, "%s 50.) { Fmt(msg, "%s 2147483647) { Fmt(msg, "%s SXDRIVE_MAX_NUM_DEV) { Fmt(msg, "%s 9999.99) { Fmt(msg, "%s 50.) { Fmt(msg, "%s 2147483647) { Fmt(msg, "%s SXDRIVE_MAX_NUM_DEV) { Fmt(msg, "%s*%i", &pos_steps); if (num_scan != 1) sxdrive_error("sxdrive_get_mot_position: Can't read the motor position."); /* Convert the position in steps to position in revolutions */ *pos = (double) pos_steps / (double) SXDRIVE_NUM_STEPS_PER_REV; /* Done */ return; } /* ************************************************************** */ /* * sxdrive_get_enc_position * This function gets the encoder position from the indexer. * * Input: * dev_ID, device identifier * * Output: * pos, position (encoder lines), can be + or - * * Zachary Wolf * 2/15/02 */ void sxdrive_get_enc_position(int dev_ID, double* pos) { /* Declare variables */ int dev_open; long int pos_steps; int num_scan; /* Check input parameters */ if (dev_ID < 1 || dev_ID > SXDRIVE_MAX_NUM_DEV) { Fmt(msg, "%s*%i", &pos_steps); if (num_scan != 1) sxdrive_error("sxdrive_get_enc_position: Can't read the encoder position."); /* Convert the position in steps to position in encoder lines */ *pos = (double) pos_steps / 4.; /* Done */ return; } /* ************************************************************** */ /* * sxdrive_get_inputs * This function gets the state of the limit switches and * input lines from the indexer. * * Input: * dev_ID, device identifier * * Output: * inputs, status of the inputs from the indexer (see p. 35) * * Zachary Wolf * 2/25/02 */ void sxdrive_get_inputs(int dev_ID, char inputs[]) { /* Declare variables */ int dev_open; int num_scan; /* Check input parameters */ if (dev_ID < 1 || dev_ID > SXDRIVE_MAX_NUM_DEV) { Fmt(msg, "%s%s", inputs); if (num_scan != 1) sxdrive_error("sxdrive_get_inputs: Can't read the inputs."); /* Done */ return; } /* ************************************************************** */ /* * sxdrive_set_software_limits * This function enables and sets software limits in the indexer. * * Input: * dev_ID, device identifier * cw_limit, CW software limit (rev) * ccw_limit, CCW software limit (rev) * * Zachary Wolf * 2/25/02 */ void sxdrive_set_software_limits(int dev_ID, double cw_limit, double ccw_limit) { /* Declare variables */ int dev_open; /* Check input parameters */ if (dev_ID < 1 || dev_ID > SXDRIVE_MAX_NUM_DEV) { Fmt(msg, "%s SXDRIVE_MAX_NUM_DEV) { Fmt(msg, "%s 999.99) { Fmt(msg, "%s 50.) { Fmt(msg, "%s SXDRIVE_MAX_NUM_DEV) { Fmt(msg, "%s 9999.99) { Fmt(msg, "%s 50.) { Fmt(msg, "%s SXDRIVE_MAX_NUM_DEV) { Fmt(msg, "%s SXDRIVE_MAX_NUM_DEV) { Fmt(msg, "%s 99) { Fmt(msg, "%s%s[t-]", buf); /* Already done */ /* Return the string in buf */ strcpy(buf, in_buf); /* Done */ return 0; } /* ************************************************************** */ /* * sxdrive_check_dev_open * This function checks to see if the specified device is open. * If the device has been opened, a 1 is returned, 0 otherwise. * * Input: * dev_ID, device identifier * * Output: * status, 1 if device is open, 0 otherwise * * Zachary Wolf * 2/23/02 */ int sxdrive_check_dev_open(int dev_ID) { /* See if the device address has a positive value */ if (dev_addr[dev_ID] > 0) { return 1; /* Open */ } else { return 0; /* Not open */ } } /* ************************************************************** */ /* * sxdrive_read_error_status * This function obtains device errors. * The operator is alerted if there is an error. * * Input: * dev_ID, device identifier * * Output: * err, 0 if no errors, -1 otherwise * * Zachary Wolf * 2/23/02 */ int sxdrive_read_error_status(int dev_ID) { /* Have the indexer report any errors */ sxdrive_out(dev_ID, "RSE"); sxdrive_in(dev_ID, msg); /* Check for problems */ if (strcmp(msg, "*NO_ERRORS") != 0 && strcmp(msg, "*ERROR___BATTERY_BACKUP_RAM_CORRUPTED") != 0) { sxdrive_error(msg); return -1; } /* Done, exit here if no errors */ return 0; } /* ************************************************************** */ /* * sxdrive_hold_until_move_complete * This function requests the status of the device in a loop until the * move is complete. * * Input: * dev_ID, device identifier * * Zachary Wolf * 1/30/02 */ void sxdrive_hold_until_move_complete(int dev_ID) { /* Declare variables */ char status[SXDRIVE_MAX_CMD]; /* Have the indexer report its status */ check_again: sxdrive_out(dev_ID, "R"); sxdrive_in(dev_ID, status); /* Check the status */ if (strcmp(status, "*R") == 0) /* Ready */ { return; } else if (strcmp(status, "*S") == 0) /* Attention needed */ { sxdrive_message("SX drive needs attention"); return; } else if (strcmp(status, "*B") == 0) /* Busy */ { Delay(1.); goto check_again; } else if (strcmp(status, "*C") == 0) /* Busy, attention needed */ { Delay(1.); goto check_again; } else if (strcmp(status, "*Z") == 0) /* Busy, auto run mode */ { Delay(1.); goto check_again; } else if (strcmp(status, "*K") == 0) /* Kill input active */ { Delay(1.); goto check_again; } else if (strcmp(status, "*W") == 0) /* Waiting for SSC */ { Delay(1.); goto check_again; } else { sxdrive_error("Unknown SX status response."); return; } /* Done */ return; } /* ************************************************************** */ /* * sxdrive_message * This function handles messages about the SXDRIVE. * * Input: * message, string to display in standard I/O * * Zachary Wolf * 2/23/02 */ void sxdrive_message(char* message) { /* Print the message */ printf("%s\n", message); /* Done */ return; } /* ************************************************************** */ /* * sxdrive_error * This function handles error messages for the SXDRIVE. * * Input: * message, string to display in standard I/O * * Zachary Wolf * 2/23/02 */ void sxdrive_error(char* message) { /* Declare variables */ char buf[80]; /* Notify the operator of the error */ printf("\nSXDRIVE ERROR: %s\n", message); Beep(); Delay(.5); Beep(); /* Terminate the program if the operator desires */ printf("Press ENTER to continue.\nPress any key then ENTER to terminate program.\n"); fgets(buf, 80, stdin); if (buf[0] == '\n') return; else exit(0); }