diff --git a/motorApp/MicosSrc/Makefile b/motorApp/MicosSrc/Makefile new file mode 100644 index 00000000..beff6dc4 --- /dev/null +++ b/motorApp/MicosSrc/Makefile @@ -0,0 +1,20 @@ +# Makefile +TOP = ../.. +include $(TOP)/configure/CONFIG + +# The following are used for debugging messages. +#USR_CXXFLAGS += -DDEBUG + +DBD += devMicos.dbd + +LIBRARY_IOC = Micos + +# Intelligent Motion Systems driver support. +SRCS += MicosRegister.cc +SRCS += devMicos.cc drvMicos.cc + +Micos_LIBS += motor motorCOM_mpf +Micos_LIBS += $(EPICS_BASE_IOC_LIBS) + +include $(TOP)/configure/RULES + diff --git a/motorApp/MicosSrc/MicosRegister.cc b/motorApp/MicosSrc/MicosRegister.cc new file mode 100644 index 00000000..a83b1b2f --- /dev/null +++ b/motorApp/MicosSrc/MicosRegister.cc @@ -0,0 +1,70 @@ +/* +FILENAME... MicosRegister.cc +USAGE... Register Micos MoCo dc motor controller device driver shell commands. + +Version: $Revision: 1.1 $ +Modified By: $Author: sluiter $ +Last Modified: $Date: 2004-02-13 18:21:19 $ +*/ + +/***************************************************************** + COPYRIGHT NOTIFICATION +***************************************************************** + +(C) COPYRIGHT 1993 UNIVERSITY OF CHICAGO + +This software was developed under a United States Government license +described on the COPYRIGHT_UniversityOfChicago file included as part +of this distribution. +**********************************************************************/ + +#include +#include +#include +#include +#include +#include "motor.h" +#include "drvMicos.h" +#include "epicsExport.h" + +extern "C" +{ + +// Micos Setup arguments +static const iocshArg setupArg0 = {"Max. controller count", iocshArgInt}; +static const iocshArg setupArg1 = {"Max. motor count", iocshArgInt}; +static const iocshArg setupArg2 = {"Polling rate", iocshArgInt}; +// Micos Config arguments +static const iocshArg configArg0 = {"Card being configured", iocshArgInt}; +static const iocshArg configArg1 = {"MPF server location", iocshArgInt}; +static const iocshArg configArg2 = {"MPF server task name", iocshArgString}; + + +static const iocshArg * const MicosSetupArgs[3] = {&setupArg0, &setupArg1, + &setupArg2}; +static const iocshArg * const MicosConfigArgs[3] = {&configArg0, &configArg1, + &configArg2}; + +static const iocshFuncDef setupMicos = {"MicosSetup", 3, MicosSetupArgs}; +static const iocshFuncDef configMicos = {"MicosConfig", 3, MicosConfigArgs}; + +static void setupMicosCallFunc(const iocshArgBuf *args) +{ + MicosSetup(args[0].ival, args[1].ival, args[2].ival); +} + + +static void configMicosCallFunc(const iocshArgBuf *args) +{ + MicosConfig(args[0].ival, args[1].ival, args[2].sval); +} + + +static void MicosmotorRegister(void) +{ + iocshRegister(&setupMicos, setupMicosCallFunc); +} + +epicsExportRegistrar(MicosmotorRegister); + +} // extern "C" diff --git a/motorApp/MicosSrc/devMicos.cc b/motorApp/MicosSrc/devMicos.cc new file mode 100644 index 00000000..0698cb82 --- /dev/null +++ b/motorApp/MicosSrc/devMicos.cc @@ -0,0 +1,314 @@ +/* File: devMicos.cc */ + +/* Device Support Routines for Micos MoCo dc motor controller. */ +/* + * Original Author: Kurt Goetze + * Date: 11-24-2003 + * + * Modification Log: + * ----------------- + * .00 11-24-2003 kag initialized from drvMCB4B.c + * .01 02-06-2004 rls Eliminate erroneous "Motor motion timeout ERROR". + * .02 02-12-2004 rls copied from devMicos.c; ported to R3.14.x + */ + + +#define VERSION 2.00 + +#include +#include "motorRecord.h" +#include "motor.h" +#include "motordevCom.h" +#include "drvMicos.h" +#include "epicsExport.h" + +/*----------------debugging-----------------*/ +#ifdef __GNUG__ + #ifdef DEBUG + volatile int devMicosDebug = 0; + #define Debug(l, f, args...) {if (l <= devMicosDebug) printf(f, ## args);} + #else + #define Debug(l, f, args...) + #endif +#else + #define Debug() +#endif + +/* Debugging levels: + * devMicosDebug >= 3 Print new part of command and command string so far + * at the end of Micos_build_trans + */ + +extern struct driver_table Micos_access; + +/* ----------------Create the dsets for devMicos----------------- */ +static struct driver_table *drvtabptr; +static long Micos_init(void *); +static long Micos_init_record(void *); +static long Micos_start_trans(struct motorRecord *); +static RTN_STATUS Micos_build_trans(motor_cmnd, double *, struct motorRecord *); +static RTN_STATUS Micos_end_trans(struct motorRecord *); + +struct motor_dset devMicos = +{ + {8, NULL, (DEVSUPFUN) Micos_init, (DEVSUPFUN) Micos_init_record, NULL}, + motor_update_values, + Micos_start_trans, + Micos_build_trans, + Micos_end_trans +}; + +epicsExportAddress(dset,devMicos); + +/* --------------------------- program data --------------------- */ +/* This table is used to define the command types */ + +static msg_types Micos_table[] = { + MOTION, /* MOVE_ABS */ + MOTION, /* MOVE_REL */ + MOTION, /* HOME_FOR */ + MOTION, /* HOME_REV */ + IMMEDIATE, /* LOAD_POS */ + IMMEDIATE, /* SET_VEL_BASE */ + IMMEDIATE, /* SET_VELOCITY */ + IMMEDIATE, /* SET_ACCEL */ + IMMEDIATE, /* GO */ + IMMEDIATE, /* SET_ENC_RATIO */ + INFO, /* GET_INFO */ + MOVE_TERM, /* STOP_AXIS */ + VELOCITY, /* JOG */ + IMMEDIATE, /* SET_PGAIN */ + IMMEDIATE, /* SET_IGAIN */ + IMMEDIATE, /* SET_DGAIN */ + IMMEDIATE, /* ENABLE_TORQUE */ + IMMEDIATE, /* DISABL_TORQUE */ + IMMEDIATE, /* PRIMITIVE */ + IMMEDIATE, /* SET_HIGH_LIMIT */ + IMMEDIATE /* SET_LOW_LIMIT */ +}; + + +static struct board_stat **Micos_cards; + +/* --------------------------- program data --------------------- */ + + +/* initialize device support for Micos DC motor */ +static long Micos_init(void *arg) +{ + long rtnval; + int after = (int) arg; + + Debug(5, "Micos_init: entry\n"); + if (after == 0) + { + drvtabptr = &Micos_access; + (drvtabptr->init)(); + } + + rtnval = motor_init_com(after, *drvtabptr->cardcnt_ptr, drvtabptr, &Micos_cards); + Debug(5, "Micos_init: exit\n"); + return(rtnval); +} + + +/* initialize a record instance */ +static long Micos_init_record(void *arg) +{ + struct motorRecord *mr = (struct motorRecord *) arg; + long rtnval; + + Debug(5, "Micos_init_record: entry\n"); + rtnval = motor_init_record_com(mr, *drvtabptr->cardcnt_ptr, + drvtabptr, Micos_cards); + Debug(5, "Micos_init_record: exit\n"); + return(rtnval); +} + + +/* start building a transaction */ +static long Micos_start_trans(struct motorRecord *mr) +{ + return(OK); +} + + +/* end building a transaction */ +static RTN_STATUS Micos_end_trans(struct motorRecord *mr) +{ + return(OK); +} + +/* add a part to the transaction */ +static RTN_STATUS Micos_build_trans(motor_cmnd command, double *parms, struct motorRecord *mr) +{ + struct motor_trans *trans = (struct motor_trans *) mr->dpvt; + struct mess_node *motor_call; + struct controller *brdptr; + struct MicosController *cntrl; + char buff[30]; + int axis, card; + RTN_STATUS rtnval; + double dval; /* placeholder for double values passed from motor record */ + long ival; /* placeholder for ints passed from motor record */ + + rtnval = OK; + buff[0] = '\0'; + dval = parms[0]; + ival = NINT(parms[0]); + + rtnval = (RTN_STATUS) motor_start_trans_com(mr, Micos_cards); + Debug(5, "Micos_build_trans: entry, motor_start_trans_com=%ld\n", rtnval); + + motor_call = &(trans->motor_call); + motor_call->type = Micos_table[command]; + card = motor_call->card; /* card is the group of drivers per unique serial port */ + axis = motor_call->signal; /* axis is Micos address, up to 16 (0-15) per serial port */ + /* Note: Each Micos driver drives _1_ motor */ + brdptr = (*trans->tabptr->card_array)[card]; + Debug(5, "Micos_build_trans: axis=%d, command=%d\n", axis, command); + if (brdptr == NULL) + return(rtnval = ERROR); + + cntrl = (struct MicosController *) brdptr->DevicePrivate; + + + if (trans->state != BUILD_STATE) + return(rtnval = ERROR); + + if (command == PRIMITIVE && mr->init != NULL && strlen(mr->init) != 0) + { + strcpy(motor_call->message, mr->init); + rtnval = motor_end_trans_com(mr, drvtabptr); + rtnval = (RTN_STATUS) motor_start_trans_com(mr, Micos_cards); + motor_call->type = Micos_table[command]; + } + + switch (command) + { + case MOVE_ABS: + case MOVE_REL: + case HOME_FOR: + case HOME_REV: + case JOG: + if (strlen(mr->prem) != 0) + { + strcpy(motor_call->message, mr->prem); + rtnval = (RTN_STATUS) motor_start_trans_com(mr, Micos_cards); + motor_call->type = Micos_table[command]; + } + if (strlen(mr->post) != 0) + motor_call->postmsgptr = (char *) &mr->post; + break; + + default: + break; + } + + switch (command) + { + case MOVE_ABS: + sprintf(motor_call->message, "%c%dma%ld", CTLA, axis, ival); + break; + case MOVE_REL: + sprintf(motor_call->message, "%c%dmr%ld", CTLA, axis, ival); + break; + case HOME_FOR: + sprintf(motor_call->message, "%c%dgh", CTLA, axis); + break; + case HOME_REV: + sprintf(motor_call->message, "%c%dgh", CTLA, axis); + break; + case LOAD_POS: /* Micos allows you to define the zero position only */ + if (dval == 0.0) + sprintf(motor_call->message, "%c%ddh,ud", CTLA, axis); + else + rtnval = ERROR; + break; + case SET_VEL_BASE: + trans->state = IDLE_STATE; + break; /* Micos does not use base velocity */ + case SET_VELOCITY: + if (ival < 0) ival = 0; + if (ival > 1000000) ival = 1000000; + sprintf(motor_call->message, "%c%ddv%ld,ud", CTLA, axis, ival); + break; + case SET_ACCEL: + /* dval is acceleration in steps/sec/sec */ + if (ival < 0) ival = 0; + if (ival > 1000000) ival = 1000000; + sprintf(motor_call->message, "%c%dda%ld,ud", CTLA, axis, ival); + break; + case GO: + /* + * The Micos starts moving immediately on move commands, GO command + * does nothing + */ + trans->state = IDLE_STATE; + break; + case SET_ENC_RATIO: + /* + * The Micos does not have the concept of encoder ratio, ignore this + * command + */ + trans->state = IDLE_STATE; + break; + case GET_INFO: + /* These commands are not actually done by sending a message, but + rather they will indirectly cause the driver to read the status + of all motors */ + break; + case STOP_AXIS: /* (decelerate to a) stop */ + sprintf(motor_call->message, "%c%dab1", CTLA, axis); + break; + case JOG: + /* + * Micos does not have a jog command. Simulate with move absolute + * to the appropriate software limit. The record will prevent JOG motion + * beyond its soft limits + */ + if (dval > 0.) + sprintf(motor_call->message, "%c%dma%ld", CTLA, axis, (long)(mr->dhlm / mr->mres)); + else + sprintf(motor_call->message, "%c%dma%ld", CTLA, axis, (long)(mr->dllm / mr->mres)); + break; + + case SET_PGAIN: + if (ival < 0) ival = 0; + if (ival > 32767) ival = 32767; + sprintf(motor_call->message, "%c%ddp%ld,ud", CTLA, axis, ival); + break; + + case SET_IGAIN: + if (ival < 0) ival = 0; + if (ival > 32767) ival = 32767; + sprintf(motor_call->message, "%c%ddi%ld,ud", CTLA, axis, ival); + break; + + case SET_DGAIN: + if (ival < 0) ival = 0; + if (ival > 32767) ival = 32767; + sprintf(motor_call->message, "%c%ddd%ld,ud", CTLA, axis, ival); + break; + + case ENABLE_TORQUE: + sprintf(motor_call->message, "%c%dmn", CTLA, axis); + break; + + case DISABL_TORQUE: + sprintf(motor_call->message, "%c%dmf", CTLA, axis); + break; + + case SET_HIGH_LIMIT: + case SET_LOW_LIMIT: + trans->state = IDLE_STATE; + break; + + default: + rtnval = ERROR; + } + + rtnval = motor_end_trans_com(mr, drvtabptr); + Debug(5, "Micos_send_msg: motor_end_trans_com status=%ld, exit\n", rtnval); + return (rtnval); +} diff --git a/motorApp/MicosSrc/devMicos.dbd b/motorApp/MicosSrc/devMicos.dbd new file mode 100644 index 00000000..037510f5 --- /dev/null +++ b/motorApp/MicosSrc/devMicos.dbd @@ -0,0 +1,5 @@ +# Micos MoCo dc series support. +device(motor,VME_IO,devMicos,"Micos MoCo") +driver(drvMicos) + + diff --git a/motorApp/MicosSrc/drvMicos.cc b/motorApp/MicosSrc/drvMicos.cc new file mode 100644 index 00000000..d18b5a3f --- /dev/null +++ b/motorApp/MicosSrc/drvMicos.cc @@ -0,0 +1,559 @@ +/* File: drvMicos.cc */ + +/* Device Driver Support routines for Micos MoCo dc motor controller. */ +/* + * Original Author: Kurt Goetze + * Date: 11/24/2003 + * + * Modification Log: + * ----------------- + * .00 11-24-2003 kag initialized from drvMCB4B.c + * .01 02-06-2004 rls Eliminate erroneous "Motor motion timeout ERROR". + * .02 02-12-2004 rls Copied from drvMicos.c. Upgraded from R3.14.x + */ + + +#include +#include +#include +#include "motor.h" +#include "drvMicos.h" +#include "serialIO.h" +#include "epicsExport.h" + +#define WAIT 1 + +#define SERIAL_TIMEOUT 2000 /* Command timeout in msec */ + +#define BUFF_SIZE 100 /* Maximum length of string to/from Micos */ + +struct mess_queue +{ + struct mess_node *head; + struct mess_node *tail; +}; + + +/*----------------debugging-----------------*/ +#ifdef __GNUG__ + #ifdef DEBUG + volatile int drvMicosDebug = 0; + #define Debug(l, f, args...) {if (l <= drvMicosDebug) printf(f, ## args);} + #else + #define Debug(l, f, args...) + #endif +#else + #define Debug() +#endif + +/* Debugging notes: + * drvMicosDebug == 0 No debugging information is printed + * drvMicosDebug >= 1 Warning information is printed + * drvMicosDebug >= 2 Time-stamped messages are printed for each string + * sent to and received from the controller + * drvMicosDebug >= 3 Additional debugging messages + */ + +volatile int Micos_num_cards = 0; +volatile int Micos_num_axis = 0; + +/* Local data required for every driver; see "motordrvComCode.h" */ +#include "motordrvComCode.h" + + +/*----------------functions-----------------*/ +static int recv_mess(int, char *, int); +static RTN_STATUS send_mess(int card, const char *com, char c); +static void start_status(int card); +static int set_status(int card, int signal); +static long report(int level); +static long init(); +static int motor_init(); +static void query_done(int, int, struct mess_node *); + +/*----------------functions-----------------*/ + +struct driver_table Micos_access = +{ + motor_init, + motor_send, + motor_free, + motor_card_info, + motor_axis_info, + &mess_queue, + &queue_lock, + &free_list, + &freelist_lock, + &motor_sem, + &motor_state, + &total_cards, + &any_motor_in_motion, + send_mess, + recv_mess, + set_status, + query_done, + start_status, + &initialized +}; + +struct +{ + long number; + long (*report) (int); + long (*init) (void); +} drvMicos = {2, report, init}; + +epicsExportAddress(drvet, drvMicos); + +static struct thread_args targs = {SCAN_RATE, &Micos_access}; + +/********************************************************* + * Print out driver status report + *********************************************************/ +static long report(int level) +{ + int card; + + if (Micos_num_cards <=0) + printf(" NO Micos controllers found\n"); + else + { + for (card = 0; card < Micos_num_cards; card++) + if (motor_state[card]) + printf(" Micos controller group %d, id: %s \n", + card, + motor_state[card]->ident); + } + return (0); +} + + +static long init() +{ + /* + * We cannot call motor_init() here, because that function can do GPIB I/O, + * and hence requires that the drvGPIB have already been initialized. + * That cannot be guaranteed, so we need to call motor_init from device + * support + */ + /* Check for setup */ + if (Micos_num_cards <= 0) + { + Debug(1, "init: *Micos driver disabled*\n"); + Debug(1, "MicosSetup() is missing from startup script.\n"); + return (ERROR); + } + + return ((long) 0); +} + +static void query_done(int card, int axis, struct mess_node *nodeptr) +{ +} + + +/********************************************************* + * Read the status and position of all motors on a card + * start_status(int card) + * if card == -1 then start all cards + *********************************************************/ +static void start_status(int card) +{ + /* The Micos cannot query status or positions of all axes with a + * single command. This needs to be done on an axis-by-axis basis, + * so this function does nothing + */ +} + + +/************************************************************** + * Query position and status for an axis + * set_status() + ************************************************************/ + +static int set_status(int card, int signal) +{ + register struct mess_info *motor_info; + char command[BUFF_SIZE]; + char response[BUFF_SIZE]; + struct mess_node *nodeptr; + int rtn_state, i, j; + long motorData; + long bytes[7]; + char temp[5]; + char buff[BUFF_SIZE]; + bool ls_active = false; + msta_field status; + + motor_info = &(motor_state[card]->motor_info[signal]); + nodeptr = motor_info->motor_motion; + status.All = motor_info->status.All; + + /* Request the moving status of this motor */ + + /* MM4000 has a delay in case the motor is not done due to servo PID-related settling.. do we need this delay? */ + /* ...after some testing, it doesn't look like we need the delay */ + + /* Get the motor status (ts) */ + sprintf(command, "%c%dts", CTLA, signal); + send_mess(card, command, 0); + recv_mess(card, response, WAIT); + /* The response string is of the form "byte0 byte1 ... byte6" */ + + /* Convert ASCII characters to hex */ + temp[0]='0'; temp[1]='x'; temp[2]='0', temp[3]='0', temp[4]='\0'; + if (signal > 9) j = 5; + else j = 4; + for (i = 0; i < 7; i++) { + temp[2] = response[j]; + temp[3] = response[j+1]; + bytes[i] = strtol(temp, (char **)NULL, 0); + j += 3; + } + /* check to see if motor is moving */ + status.Bits.RA_DONE = (bytes[0] & 0x04) ? 1 : 0; + + /* check limits */ + status.Bits.RA_PLUS_LS = status.Bits.RA_MINUS_LS = 0; + if ((bytes[5] & 0x04) & (bytes[3] & 0x04)) { /* if +lim AND pos move */ + status.Bits.RA_PLUS_LS = 1; + ls_active = true; + } + if ((bytes[5] & 0x01) & !(bytes[3] & 0x04)) { /* if -lim AND neg move */ + status.Bits.RA_MINUS_LS = 1; + ls_active = true; + } + + /* encoder status */ + status.Bits.EA_SLIP = 0; + status.Bits.EA_POSITION = 0; + status.Bits.EA_SLIP_STALL = 0; + status.Bits.EA_HOME = 0; + if ((bytes[3] & 0x08) | (bytes[3] & 0x40)) { + printf("drvMicos: set_status: EA_SLIP_STALL = 1, %ld\n", bytes[3]); + status.Bits.EA_SLIP_STALL = 1; + } + + /* Request the position of this motor */ + sprintf(command, "%c%dtp", CTLA, signal); + send_mess(card, command, 0); + recv_mess(card, response, WAIT); + /* The response string is of the form "0P0:+00001000" */ + if (signal > 9) + motorData = atoi(&response[5]); + else + motorData = atoi(&response[4]); + + /* derive direction information */ + if (motorData == motor_info->position) + { + if (nodeptr != 0) /* Increment counter only if motor is moving. */ + motor_info->no_motion_count++; + } + else + { + status.Bits.RA_DIRECTION = (bytes[3] & 0x04) ? 1 : 0; + motor_info->position = motorData; + motor_info->encoder_position = motorData; + motor_info->no_motion_count = 0; + } + + /* Parse motor velocity? */ + /* NEEDS WORK */ + + motor_info->velocity = 0; + + if (!status.Bits.RA_DIRECTION) + motor_info->velocity *= -1; + + rtn_state = (!motor_info->no_motion_count || ls_active == true || + status.Bits.RA_DONE | status.Bits.RA_PROBLEM) ? 1 : 0; + + /* Test for post-move string. */ + if ((status.Bits.RA_DONE || ls_active == true) && nodeptr != 0 && + nodeptr->postmsgptr != 0) + { + strcpy(buff, nodeptr->postmsgptr); + send_mess(card, buff, (char) NULL); + /* The Micos will not send back a response for a 'set' command, don't need next line */ + /* recv_mess(card, buff, WAIT); */ + nodeptr->postmsgptr = NULL; + } + + motor_info->status.All = status.All; + return (rtn_state); +} + + +/*****************************************************/ +/* send a message to the Micos board */ +/* send_mess() */ +/*****************************************************/ +static RTN_STATUS send_mess(int card, const char *com, char c) +{ + char buff[BUFF_SIZE]; + struct MicosController *cntrl; + + /* Check that card exists */ + if (!motor_state[card]) + { + errlogPrintf("send_mess - invalid card #%d\n", card); + return(ERROR); + } + + /* If the string is NULL just return */ + if (strlen(com) == 0) return(OK); + cntrl = (struct MicosController *) motor_state[card]->DevicePrivate; + + strcpy(buff, com); + strcat(buff, OUTPUT_TERMINATOR); + Debug(2, "%.2f : send_mess: sending message to card %d, message=%s\n", + tickGet()/60., card, buff); + cntrl = (struct MicosController *) motor_state[card]->DevicePrivate; + cntrl->serialInfo->serialIOSend(buff, strlen(buff), SERIAL_TIMEOUT); + + return (OK); +} + + +/*****************************************************/ +/* Read a response string from the Micos board */ +/* recv_mess() */ +/*****************************************************/ +static int recv_mess(int card, char *com, int flag) +{ + int timeout; + int len=0; + struct MicosController *cntrl; + + /* Check that card exists */ + if (!motor_state[card]) + { + errlogPrintf("recv_mess - invalid card #%d\n", card); + return (-1); + } + + cntrl = (struct MicosController *) motor_state[card]->DevicePrivate; + + Debug(3, "%.2f : recv_mess entry: card %d, flag=%d\n", + tickGet()/60., card, flag); + if (flag == FLUSH) + timeout = 0; + else + timeout = SERIAL_TIMEOUT; + len = cntrl->serialInfo->serialIORecv(com, BUFF_SIZE, (char *) "\3", timeout); + + /* The response from the Micos is terminated with . Remove */ + if (len < 3) com[0] = '\0'; + else com[len-3] = '\0'; + if (len > 0) { + Debug(2, "%.2f : recv_mess: card %d, message = \"%s\"\n", + tickGet()/60., card, com); + } + if (len == 0) { + if (flag != FLUSH) { + Debug(1, "%.2f: recv_mess: card %d ERROR: no response\n", + tickGet()/60., card); + } else { + Debug(3, "%.2f: recv_mess: card %d flush returned no characters\n", + tickGet()/60., card); + } + } + return (len); +} + + + +/*****************************************************/ +/* Setup system configuration */ +/* MicosSetup() */ +/*****************************************************/ +RTN_STATUS +MicosSetup(int num_cards, /* maximum number of "controllers" in system */ + int num_channels, /* max number of drivers */ + int scan_rate) /* polling rate - 1/60 sec units */ +{ + int itera; + + if (num_cards < 1 || num_cards > MICOS_NUM_CARDS) + Micos_num_cards = MICOS_NUM_CARDS; + else + Micos_num_cards = num_cards; + + if (num_channels < 1 || num_channels > MICOS_NUM_AXIS) + Micos_num_axis = MICOS_NUM_AXIS; + else + Micos_num_axis = num_channels; + + /* Set motor polling task rate */ + if (scan_rate >= 1 && scan_rate <= 60) + targs.motor_scan_rate = scan_rate; + else + targs.motor_scan_rate = SCAN_RATE; + + /* + * Allocate space for motor_state structure pointers. Note this must be done + * before MicosConfig is called, so it cannot be done in motor_init() + * This means that we must allocate space for a card without knowing + * if it really exists, which is not a serious problem since this is just + * an array of pointers. + */ + motor_state = (struct controller **) malloc(Micos_num_cards * + sizeof(struct controller *)); + + for (itera = 0; itera < Micos_num_cards; itera++) + motor_state[itera] = (struct controller *) NULL; + return(OK); +} + + +/*****************************************************/ +/* Configure a controller */ +/* MicosConfig() */ +/*****************************************************/ +RTN_STATUS +MicosConfig(int card, /* "controller" being configured */ + int location, /* MPF server location */ + const char *name) /* MPF server task name */ +{ + struct MicosController *cntrl; + + if (card < 0 || card >= Micos_num_cards) + return (ERROR); + + motor_state[card] = (struct controller *) malloc(sizeof(struct controller)); + motor_state[card]->DevicePrivate = malloc(sizeof(struct MicosController)); + cntrl = (struct MicosController *) motor_state[card]->DevicePrivate; + cntrl->serial_card = location; + strcpy(cntrl->serial_task, name); + return(OK); +} + + +/*****************************************************/ +/* initialize all software and hardware */ +/* This is called from the initialization routine in */ +/* device support. */ +/* motor_init() */ +/*****************************************************/ +static int motor_init() +{ + struct controller *brdptr; + struct MicosController *cntrl; + int card_index, motor_index; + char cmd[BUFF_SIZE]; + char buff[BUFF_SIZE]; + int total_axis = 0; + int status = 0; + bool errind, success_rtn; + + initialized = true; /* Indicate that driver is initialized. */ + + /* Check for setup */ + if (Micos_num_cards <= 0) + { + Debug(1, "motor_init: *Micos driver disabled*\n"); + Debug(1, "MicosSetup() is missing from startup script.\n"); + return (ERROR); + } + + for (card_index = 0; card_index < Micos_num_cards; card_index++) + { + if (!motor_state[card_index]) + continue; + + brdptr = motor_state[card_index]; + total_cards = card_index + 1; + cntrl = (struct MicosController *) brdptr->DevicePrivate; + + /* Initialize communications channel */ + errind = false; + + success_rtn = false; + cntrl->serialInfo = new serialIO(cntrl->serial_card, + cntrl->serial_task, &success_rtn); + + if (success_rtn == true) + { + int retry = 0; + + /* Each "controller" can have max 16 axes. */ + total_axis = Micos_num_axis; + brdptr->total_axis = total_axis; + + /* flush any junk at input port - should not be any data available */ + do { + recv_mess(card_index, buff, FLUSH); + } while (strlen(buff) != 0); + + /* Send a message to each Micos driver. See if it responds */ + for (motor_index = 0; motor_index < total_axis; motor_index++) + { + do + { + sprintf(cmd, "%c%dts", CTLA, motor_index); + send_mess(card_index, cmd, 0); + status = recv_mess(card_index, buff, WAIT); + retry++; + /* Return value is length of response string */ + } while(status == 0 && retry < 3); + if (status == 0) break; + } + } + + + if (success_rtn == true && status > 0) + { + brdptr->localaddr = (char *) NULL; + brdptr->motor_in_motion = 0; + brdptr->cmnd_response = false; + + start_status(card_index); + for (motor_index = 0; motor_index < total_axis; motor_index++) + { + struct mess_info *motor_info = &brdptr->motor_info[motor_index]; + brdptr->motor_info[motor_index].motor_motion = NULL; + /* turn off echo */ + sprintf(buff, "%c%def", CTLA, motor_index); + send_mess(card_index, buff, 0); + /* Don't turn on motor power, too dangerous */ + /*sprintf(buff,"#%02dW=1", motor_index); */ + /* send_mess(card_index, buff, 0); */ + /* Stop motor */ + sprintf(buff,"%c%dab1", CTLA, motor_index); + send_mess(card_index, buff, 0); + /* recv_mess(card_index, buff, WAIT); Throw away response */ + strcpy(brdptr->ident, "MICOS"); + + motor_info->status.All = 0; + motor_info->no_motion_count = 0; + motor_info->encoder_position = 0; + motor_info->position = 0; + + motor_info->encoder_present = YES; + motor_info->status.Bits.EA_PRESENT = 1; + motor_info->pid_present = YES; + motor_info->status.Bits.GAIN_SUPPORT = 1; + + set_status(card_index, motor_index); /* Read status of each motor */ + } + + } + else + motor_state[card_index] = (struct controller *) NULL; + } + + Debug(3, "motor_init: spawning motor task\n"); + + any_motor_in_motion = 0; + + mess_queue.head = (struct mess_node *) NULL; + mess_queue.tail = (struct mess_node *) NULL; + + free_list.head = (struct mess_node *) NULL; + free_list.tail = (struct mess_node *) NULL; + + epicsThreadCreate((char *) "tMicos", 64, 5000, (EPICSTHREADFUNC) motor_task, (void *) &targs); + + return (0); +} diff --git a/motorApp/MicosSrc/drvMicos.h b/motorApp/MicosSrc/drvMicos.h new file mode 100644 index 00000000..9966511d --- /dev/null +++ b/motorApp/MicosSrc/drvMicos.h @@ -0,0 +1,40 @@ +/* File: drvMicos.h */ + + +/* Device Driver Support definitions for Micos MoCo dc motor controller. */ +/* + * Original Author: Kurt Goetze + * Current Author: Kurt Goetze + * Date: 11/24/2003 + * + * Modification Log: + * ----------------- + * .00 11/24/2003 kag initialized from drvMCB4B.h + */ + +#ifndef INCdrvMicosh +#define INCdrvMicosh 1 + +#include "motor.h" +#include "motordrvCom.h" +#include "serialIO.h" + +/* Micos default profile. */ + +#define MICOS_NUM_CARDS 16 +#define MICOS_NUM_AXIS 16 +#define CTLA 1 +#define OUTPUT_TERMINATOR "\r" + +struct MicosController +{ + serialIO *serialInfo; /* For RS-232 */ + int serial_card; /* Card on which Hideos/MPF is running */ + char serial_task[20]; /* Hideos/MPF task/server name for serial port */ +}; + +/* Function prototypes. */ +extern RTN_STATUS MicosSetup(int, int, int); +extern RTN_STATUS MicosConfig(int, int, const char *); + +#endif /* INCdrvMicosh */