diff --git a/motorApp/PiSrc/Makefile b/motorApp/PiSrc/Makefile index c3cc1104..b38e4d27 100644 --- a/motorApp/PiSrc/Makefile +++ b/motorApp/PiSrc/Makefile @@ -16,6 +16,7 @@ SRCS += devPIC630.cc drvPIC630.cc PIC630Register.cc SRCS += devPIC848.cc drvPIC848.cc PIC848Register.cc SRCS += devPIC662.cc drvPIC662.cc PIC662Register.cc SRCS += devPIC862.cc drvPIC862.cc PIC862Register.cc +SRCS += devPIC663.cc drvPIC663.cc PIC663Register.cc SRCS += devPIE710.cc drvPIE710.cc PIE710Register.cc SRCS += devPIE516.cc drvPIE516.cc PIE516Register.cc SRCS += devPIE816.cc drvPIE816.cc PIE816Register.cc diff --git a/motorApp/PiSrc/PIC663Register.cc b/motorApp/PiSrc/PIC663Register.cc new file mode 100644 index 00000000..d89e2335 --- /dev/null +++ b/motorApp/PiSrc/PIC663Register.cc @@ -0,0 +1,72 @@ +/* +FILENAME... PIC663Register.cc +USAGE... Register PI motor device driver shell commands. +*/ +/* + * Copied from PIC862Register.cc by Jonathan Thompson, Jan 2011 + * + * Modification Log: + * ----------------- + * Jan 2011 - All references to 862 changed to 663 + * Status register definitions changed to match 663 + */ + +/***************************************************************** + 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 "drvPIC663.h" +#include "epicsExport.h" + +extern "C" +{ + +// Pi Setup arguments +static const iocshArg setupArg0 = {"Max. controller count", iocshArgInt}; +static const iocshArg setupArg1 = {"Polling rate", iocshArgInt}; +// Pi Config arguments +static const iocshArg configArg0 = {"Card being configured", iocshArgInt}; +static const iocshArg configArg1 = {"asyn port name", iocshArgString}; +static const iocshArg configArg2 = {"asyn address (GPIB)", iocshArgInt}; + +static const iocshArg * const PIC663SetupArgs[2] = {&setupArg0, &setupArg1}; +static const iocshArg * const PIC663ConfigArgs[3] = {&configArg0, &configArg1, + &configArg2}; + +static const iocshFuncDef setupPIC663 = {"PIC663Setup", 2, PIC663SetupArgs}; +static const iocshFuncDef configPIC663 = {"PIC663Config", 3, PIC663ConfigArgs}; + +static void setupPIC663CallFunc(const iocshArgBuf *args) +{ + PIC663Setup(args[0].ival, args[1].ival); +} + + +static void configPIC663CallFunc(const iocshArgBuf *args) +{ + PIC663Config(args[0].ival, args[1].sval, args[2].ival); +} + + +static void PIC663motorRegister(void) +{ + iocshRegister(&setupPIC663, setupPIC663CallFunc); + iocshRegister(&configPIC663, configPIC663CallFunc); +} + +epicsExportRegistrar(PIC663motorRegister); + +} // extern "C" diff --git a/motorApp/PiSrc/devPIC663.cc b/motorApp/PiSrc/devPIC663.cc new file mode 100644 index 00000000..f85c918b --- /dev/null +++ b/motorApp/PiSrc/devPIC663.cc @@ -0,0 +1,305 @@ +/* +FILENAME... devPIC663.cc +USAGE... Motor record device level support for Physik Instrumente (PI) + GmbH & Co. C-663 motor controller. +*/ + +/* + * Copied from devPIC862.cc by Jonathan Thompson, Jan 2011 + * + * Modification Log: + * ----------------- + * Jan 2011 - All references to 862 changed to 663 + */ + + +#include +#include +#include "motorRecord.h" +#include "motor.h" +#include "motordevCom.h" +#include "drvPIC663.h" +#include "epicsExport.h" + +extern struct driver_table PIC663_access; + +/* ----------------Create the dsets for devPIC663----------------- */ +static struct driver_table *drvtabptr; +static long PIC663_init(void *); +static long PIC663_init_record(void *); +static long PIC663_start_trans(struct motorRecord *); +static RTN_STATUS PIC663_build_trans(motor_cmnd, double *, struct motorRecord *); +static RTN_STATUS PIC663_end_trans(struct motorRecord *); + +struct motor_dset devPIC663 = +{ + {8, NULL, (DEVSUPFUN) PIC663_init, (DEVSUPFUN) PIC663_init_record, NULL}, + motor_update_values, + PIC663_start_trans, + PIC663_build_trans, + PIC663_end_trans +}; + +extern "C" {epicsExportAddress(dset,devPIC663);} + +/* --------------------------- program data --------------------- */ + +/* This table is used to define the command types */ +/* WARNING! this must match "motor_cmnd" in motor.h */ + +static msg_types PIC663_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 */ + VELOCITY /* JOG_VELOCITY */ +}; + + +static struct board_stat **PIC663_cards; + +/* --------------------------- program data --------------------- */ + + +/* initialize device support for PIC663 servo motor */ +static long PIC663_init(void *arg) +{ + long rtnval; + int after = (arg == 0) ? 0 : 1; + + if (after == 0) + { + drvtabptr = &PIC663_access; + (drvtabptr->init)(); + } + + rtnval = motor_init_com(after, *drvtabptr->cardcnt_ptr, drvtabptr, &PIC663_cards); + return(rtnval); +} + + +/* initialize a record instance */ +static long PIC663_init_record(void *arg) +{ + struct motorRecord *mr = (struct motorRecord *) arg; + return(motor_init_record_com(mr, *drvtabptr->cardcnt_ptr, drvtabptr, PIC663_cards)); +} + + +/* start building a transaction */ +static long PIC663_start_trans(struct motorRecord *mr) +{ + motor_start_trans_com(mr, PIC663_cards); + return(OK); +} + + +/* end building a transaction */ +static RTN_STATUS PIC663_end_trans(struct motorRecord *mr) +{ + struct motor_trans *trans = (struct motor_trans *) mr->dpvt; + struct mess_node *motor_call; + char *msgptr; + int last; + + /* Remove trailing cmnd separator (",") from message. */ + motor_call = &(trans->motor_call); + msgptr = motor_call->message; + last = strlen(msgptr) - 1; + if (msgptr[last] == ',') + msgptr[last] = (char) NULL; + + motor_end_trans_com(mr, drvtabptr); + return(OK); +} + + +/* add a part to the transaction */ +static RTN_STATUS PIC663_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; + char buff[110]; + int card; + unsigned int size; + double dval; + int cntrl_units; + RTN_STATUS rtnval; + bool send; + + send = true; /* Default to send motor command. */ + rtnval = OK; + buff[0] = '\0'; + + /* Protect against NULL pointer with WRTITE_MSG(GO/STOP_AXIS/GET_INFO, NULL). */ + dval = (parms == NULL) ? 0.0 : *parms; + + + motor_call = &(trans->motor_call); + card = motor_call->card; + brdptr = (*trans->tabptr->card_array)[card]; + if (brdptr == NULL) + return(rtnval = ERROR); + + + cntrl_units = (int) dval; + + if (PIC663_table[command] > motor_call->type) + motor_call->type = PIC663_table[command]; + + if (trans->state != BUILD_STATE) + return(rtnval = ERROR); + + if (command == PRIMITIVE && mr->init != NULL && strlen(mr->init) != 0) + strcat(motor_call->message, mr->init); + + switch (command) + { + case MOVE_ABS: + case MOVE_REL: + case HOME_FOR: + case HOME_REV: + case JOG: + if (strlen(mr->prem) != 0) + { + strcat(motor_call->message, mr->prem); + strcat(motor_call->message, ","); + } + if (strlen(mr->post) != 0) + motor_call->postmsgptr = (char *) &mr->post; + break; + + default: + break; + } + + + switch (command) + { + case MOVE_ABS: + sprintf(buff, "MA%d,", cntrl_units); + break; + + case MOVE_REL: + sprintf(buff, "MR%d,", cntrl_units); + break; + + case HOME_FOR: + sprintf(buff, "FE0"); + break; + case HOME_REV: + sprintf(buff, "FE1"); + break; + + case LOAD_POS: + if (cntrl_units == 0.0) + sprintf(buff, "DH"); + else + rtnval = ERROR; + break; + + case SET_VEL_BASE: + send = false; /* DC motor; not base velocity. */ + break; + + case JOG_VELOCITY: + case SET_VELOCITY: + sprintf(buff, "SV%d,", cntrl_units); + break; + + case SET_ACCEL: + sprintf(buff, "SA%d,", cntrl_units); + break; + + case ENABLE_TORQUE: + sprintf(buff, "MN"); + break; + + case DISABL_TORQUE: + sprintf(buff, "MF"); + break; + + case GO: + /* The PIC663 starts moving immediately on move commands, GO command + * does nothing. */ + send = false; + break; + + case PRIMITIVE: + 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: + sprintf(buff, "ST"); + break; + + case JOG: + /* + * C-663 does not have a jog command. Simulate with move absolute + * to the appropriate software limit. + */ + sprintf(buff, "SV%d,", abs(cntrl_units)); + strcat(motor_call->message, buff); + if (dval > 0.) + sprintf(buff, "MA%d,", (int) (mr->dhlm / mr->mres)); + else + sprintf(buff, "MA%d,", (int) (mr->dllm / mr->mres)); + break; + + case SET_PGAIN: + cntrl_units = (int) (32767 * dval); + sprintf(buff, "DP%d", cntrl_units); + break; + case SET_IGAIN: + cntrl_units = (int) (32767 * dval); + sprintf(buff, "DI%d", cntrl_units); + break; + case SET_DGAIN: + cntrl_units = (int) (32767 * dval); + sprintf(buff, "DD%d", cntrl_units); + break; + + case SET_HIGH_LIMIT: + case SET_LOW_LIMIT: + case SET_ENC_RATIO: + trans->state = IDLE_STATE; /* No command sent to the controller. */ + send = false; + break; + + default: + send = false; + rtnval = ERROR; + } + + size = strlen(buff); + if (send == false) + return(rtnval); + else if (size > sizeof(buff) || (strlen(motor_call->message) + size) > MAX_MSG_SIZE) + errlogMessage("PIC663_build_trans(): buffer overflow.\n"); + else + { + strcat(motor_call->message, buff); + } + return(rtnval); +} diff --git a/motorApp/PiSrc/devPIMotor.dbd b/motorApp/PiSrc/devPIMotor.dbd index 67a10166..67584994 100644 --- a/motorApp/PiSrc/devPIMotor.dbd +++ b/motorApp/PiSrc/devPIMotor.dbd @@ -28,6 +28,12 @@ driver(drvPIC862) registrar(PIC862motorRegister) variable(drvPIC862debug) +# Physik Instrumente (PI) GmbH & Co. C-663 driver support. +device(motor,VME_IO,devPIC663,"PIC663") +driver(drvPIC663) +registrar(PIC663motorRegister) +variable(drvPIC663debug) + # Physik Instrumente (PI) GmbH & Co. E-710 driver support. device(motor,VME_IO,devPIE710,"PIE710") driver(drvPIE710) diff --git a/motorApp/PiSrc/drvPIC663.cc b/motorApp/PiSrc/drvPIC663.cc new file mode 100644 index 00000000..9519416a --- /dev/null +++ b/motorApp/PiSrc/drvPIC663.cc @@ -0,0 +1,571 @@ +/* +FILENAME... drvPIC663.cc +USAGE... Motor record driver level support for Physik Instrumente (PI) + GmbH & Co. C-663 motor controller. +*/ +/* + * Copied from devPIC862.cc by Jonathan Thompson, Jan 2011 + * + * Modification Log: + * ----------------- + * Jan 2011 - All references to 862 changed to 663 + * - Handling of status request results changed to cope with 663 output + */ + + +/* +DESIGN LIMITATIONS... + 1 - Like all controllers, the PIC663 must be powered-on when EPICS is first + booted up. +*/ + +#include +#include +#include +#include "motorRecord.h" +#include "motor.h" +#include "drvPIC663.h" +#include "epicsExport.h" + +#define GET_IDENT 0x01 + +#define PIC663_NUM_CARDS 8 +#define MAX_AXES 1 +#define BUFF_SIZE 100 /* Maximum length of string to/from PIC663 */ + +/*----------------debugging-----------------*/ +#ifdef __GNUG__ + #ifdef DEBUG + #define Debug(l, f, args...) { if(l<=drvPIC663debug) printf(f,## args); } + #else + #define Debug(l, f, args...) + #endif +#else + #define Debug() +#endif +volatile int drvPIC663debug = 0; +extern "C" {epicsExportAddress(int, drvPIC663debug);} + +/* --- Local data. --- */ +int PIC663_num_cards = 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, char const *, char *); +static int set_status(int, int); +static long report(int); +static long init(); +static int motor_init(); +static void query_done(int, int, struct mess_node *); + +/*----------------functions-----------------*/ + +struct driver_table PIC663_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, + NULL, + &initialized, + NULL +}; + +struct +{ + long number; + long (*report) (int); + long (*init) (void); +} drvPIC663 = {2, report, init}; + +extern "C" {epicsExportAddress(drvet, drvPIC663);} + +static struct thread_args targs = {SCAN_RATE, &PIC663_access, 0.017}; + +/********************************************************* + * Print out driver status report + *********************************************************/ +static long report(int level) +{ + int card; + + if (PIC663_num_cards <=0) + printf(" No PIC663 controllers configured.\n"); + else + { + for (card = 0; card < PIC663_num_cards; card++) + { + struct controller *brdptr = motor_state[card]; + + if (brdptr == NULL) + printf(" PIC663 controller %d connection failed.\n", card); + else + { + struct PIC663controller *cntrl; + + cntrl = (struct PIC663controller *) brdptr->DevicePrivate; + printf(" PIC663 controller #%d, port=%s, id: %s \n", card, + cntrl->asyn_port, brdptr->ident); + } + } + } + return(OK); +} + + +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 (PIC663_num_cards <= 0) + { + Debug(1, "init(): PIC663 driver disabled. PIC663Setup() missing from startup script.\n"); + } + return((long) 0); +} + + +static void query_done(int card, int axis, struct mess_node *nodeptr) +{ +} + + +/******************************************************************************** +* * +* FUNCTION NAME: set_status * +* * +* LOGIC: * +* Initialize. * +* Send "Moving Status" query. * +* Read response. * +* IF normal response to query. * +* Set communication status to NORMAL. * +* ELSE * +* IF communication status is NORMAL. * +* Set communication status to RETRY. * +* NORMAL EXIT. * +* ELSE * +* Set communication status error. * +* ERROR EXIT. * +* ENDIF * +* ENDIF * +* * +* IF "Moving Status" indicates any motion (i.e. status != 0). * +* Clear "Done Moving" status bit. * +* ELSE * +* Set "Done Moving" status bit. * +* ENDIF * +* * +* * +********************************************************************************/ + +static int set_status(int card, int signal) +{ + struct PIC663controller *cntrl; + struct mess_node *nodeptr; + struct mess_info *motor_info; + struct motorRecord *mr; + /* Message parsing variables */ + char buff[BUFF_SIZE]; + C663_Status_Reg1 mstat1; + C663_Status_Reg2 mstat2; + C663_Status_Reg3 mstat3; + epicsUInt16 dev_sts1, dev_sts2, dev_sts3; + + int rtn_state, convert_cnt, charcnt; + epicsInt32 motorData; + bool plusdir, ls_active = false, plusLS, minusLS, LSactiveH; + msta_field status; + + cntrl = (struct PIC663controller *) motor_state[card]->DevicePrivate; + motor_info = &(motor_state[card]->motor_info[signal]); + nodeptr = motor_info->motor_motion; + if (nodeptr != NULL) + mr = (struct motorRecord *) nodeptr->mrecord; + else + mr = NULL; + status.All = motor_info->status.All; + + if (cntrl->status != NORMAL) + charcnt = recv_mess(card, buff, FLUSH); + + send_mess(card, "TS", (char) NULL); /* Tell Status */ + charcnt = recv_mess(card, buff, 1); + if (charcnt > 9) + convert_cnt = sscanf(buff, "S:%2hx %2hx %2hx\n", + &dev_sts1,&dev_sts2,&dev_sts3); + + if (charcnt > 9 && convert_cnt == 3) + { + cntrl->status = NORMAL; + status.Bits.CNTRL_COMM_ERR = 0; + } + else + { + if (cntrl->status == NORMAL) + { + cntrl->status = RETRY; + rtn_state = OK; + goto exit; + } + else + { + cntrl->status = COMM_ERR; + status.Bits.CNTRL_COMM_ERR = 1; + status.Bits.RA_PROBLEM = 1; + rtn_state = 1; + goto exit; + } + } + + mstat1.All = dev_sts1; + mstat2.All = dev_sts2; + mstat3.All = dev_sts3; + + status.Bits.RA_DONE = (mstat1.Bits.on_target) ? 1 : 0; + status.Bits.EA_POSITION = (mstat1.Bits.drv_cur_act) ? 0 : 1; + status.Bits.RA_DIRECTION = 0; + + plusLS = mstat2.Bits.hi_limit ? 0 : 1; + minusLS = mstat2.Bits.lo_limit ? 0 : 1; + + /* Parse motor position */ + send_mess(card, "TP", (char) NULL); /* Tell Position */ + recv_mess(card, buff, 1); + motorData = NINT(atof(&buff[2])); + + if (motorData == motor_info->position) + { + if (nodeptr != 0) /* Increment counter only if motor is moving. */ + motor_info->no_motion_count++; + } + else + { + motor_info->position = motor_info->encoder_position = motorData; + motor_info->no_motion_count = 0; + } + + plusdir = (status.Bits.RA_DIRECTION) ? true : false; + + /* Set limit switch error indicators. */ + if (plusLS == true) + { + status.Bits.RA_PLUS_LS = 1; + if (plusdir == true) + ls_active = true; + } + else + status.Bits.RA_PLUS_LS = 0; + + if (minusLS == true) + { + status.Bits.RA_MINUS_LS = 1; + if (plusdir == false) + ls_active = true; + } + else + status.Bits.RA_MINUS_LS = 0; + + /* encoder status */ + status.Bits.EA_SLIP = 0; + status.Bits.EA_SLIP_STALL = 0; + status.Bits.EA_HOME = 0; + + status.Bits.RA_PROBLEM = 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); + nodeptr->postmsgptr = NULL; + } + +exit: + motor_info->status.All = status.All; + return(rtn_state); +} + + +/*****************************************************/ +/* send a message to the PIC663 board */ +/* send_mess() */ +/*****************************************************/ +static RTN_STATUS send_mess(int card, char const *com, char *name) +{ + char local_buff[MAX_MSG_SIZE]; + struct PIC663controller *cntrl; + int comsize, namesize; + size_t nwrite; + + comsize = (com == NULL) ? 0 : strlen(com); + namesize = (name == NULL) ? 0 : strlen(name); + + if ((comsize + namesize) > MAX_MSG_SIZE) + { + errlogMessage("drvPIC663.cc:send_mess(); message size violation.\n"); + return(ERROR); + } + else if (comsize == 0) /* Normal exit on empty input message. */ + return(OK); + + if (!motor_state[card]) + { + errlogPrintf("drvPIC663.cc:send_mess() - invalid card #%d\n", card); + return(ERROR); + } + + local_buff[0] = (char) NULL; /* Terminate local buffer. */ + + /* this device deos not have axis info and so name is ignored! */ + + strcat(local_buff, com); /* Make a local copy of the string. */ + Debug(2, "send_mess(): message = %s\n", local_buff); + + cntrl = (struct PIC663controller *) motor_state[card]->DevicePrivate; + pasynOctetSyncIO->write(cntrl->pasynUser, local_buff, strlen(local_buff), + COMM_TIMEOUT, &nwrite); + + return(OK); +} + + +/*****************************************************/ +/* receive a message from the PIC663 board */ +/* recv_mess() */ +/*****************************************************/ +static int recv_mess(int card, char *com, int flag) +{ + struct PIC663controller *cntrl; + size_t nread = 0; + asynStatus status = asynError; + int eomReason; + + /* Check that card exists */ + if (!motor_state[card]) + return(ERROR); + + cntrl = (struct PIC663controller *) motor_state[card]->DevicePrivate; + + if (flag == FLUSH) + pasynOctetSyncIO->flush(cntrl->pasynUser); + else + status = pasynOctetSyncIO->read(cntrl->pasynUser, com, BUFF_SIZE, + COMM_TIMEOUT, &nread, &eomReason); + + if ((status != asynSuccess) || (nread <= 0)) + { + com[0] = '\0'; + nread = 0; + } + else + com[nread - 1] = '\0'; /* Strip traling CR. */ + + Debug(2, "recv_mess(): message = \"%s\"\n", com); + return(nread); +} + + +/*****************************************************/ +/* Setup system configuration */ +/* PIC663Setup() */ +/*****************************************************/ +RTN_STATUS +PIC663Setup(int num_cards, /* maximum number of controllers in system. */ + int scan_rate) /* polling rate - 1/60 sec units. */ +{ + int itera; + + if (num_cards < 1 || num_cards > PIC663_NUM_CARDS) + PIC663_num_cards = PIC663_NUM_CARDS; + else + PIC663_num_cards = num_cards; + + /* 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 structures. Note this must be done + * before PIC663Config 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 + */ + motor_state = (struct controller **) malloc(PIC663_num_cards * + sizeof(struct controller *)); + + for (itera = 0; itera < PIC663_num_cards; itera++) + motor_state[itera] = (struct controller *) NULL; + + return(OK); +} + + +/*****************************************************/ +/* Configure a controller */ +/* PIC663Config() */ +/*****************************************************/ +RTN_STATUS +PIC663Config(int card, /* card being configured */ + const char *name, /* asyn port name */ + int addr) /* asyn address ( device address for daisy chaining */ +{ + struct PIC663controller *cntrl; + + if (card < 0 || card >= PIC663_num_cards) + return(ERROR); + + motor_state[card] = (struct controller *) malloc(sizeof(struct controller)); + motor_state[card]->DevicePrivate = malloc(sizeof(struct PIC663controller)); + cntrl = (struct PIC663controller *) motor_state[card]->DevicePrivate; + + strcpy(cntrl->asyn_port, name); + cntrl->asyn_address = addr; + 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 PIC663controller *cntrl; + int card_index, motor_index; + char buff[BUFF_SIZE]; + int status; + asynStatus success_rtn; + static const char output_terminator[] = "\r"; + static const char input_terminator[] = "\n\03"; + + initialized = true; /* Indicate that driver is initialized. */ + + /* Check for setup */ + if (PIC663_num_cards <= 0) + return(ERROR); + + for (card_index = 0; card_index < PIC663_num_cards; card_index++) + { + if (!motor_state[card_index]) + continue; + + brdptr = motor_state[card_index]; + brdptr->ident[0] = (char) NULL; /* No controller identification message. */ + brdptr->cmnd_response = false; + total_cards = card_index + 1; + cntrl = (struct PIC663controller *) brdptr->DevicePrivate; + + /* Initialize communications channel */ + success_rtn = pasynOctetSyncIO->connect(cntrl->asyn_port, 0, + &cntrl->pasynUser, NULL); + + if (success_rtn == asynSuccess) + { + int retry = 0; + + pasynOctetSyncIO->setOutputEos(cntrl->pasynUser, output_terminator, + strlen(output_terminator)); + pasynOctetSyncIO->setInputEos(cntrl->pasynUser, input_terminator, + strlen(input_terminator)); + + /* Send a message to the board, see if it exists */ + /* flush any junk at input port - should not be any data available */ + pasynOctetSyncIO->flush(cntrl->pasynUser); + + /* To start communicating with the device requires to enable the device + To do this send "0x01" and a singel letter address (0-F) + To make sure we talk ask the status with "TB" command for the address + It replies "B:000x" where x is 0-F + The command "VE" provides a complete Identification string if we need. + */ + + + do + { + sprintf(buff,"\001%1XVE", cntrl->asyn_address); + send_mess(card_index, buff, (char) NULL); + status = recv_mess(card_index, buff, 1); + retry++; + } while (status == 0 && retry < 3); + } + + if (success_rtn == asynSuccess && status > 0) + { + strcpy(brdptr->ident, &buff[0]); + brdptr->localaddr = (char *) NULL; + brdptr->motor_in_motion = 0; + + /* number of axes is always one.*/ + brdptr->total_axis = 1; + motor_index = 0; + + struct mess_info *motor_info = &brdptr->motor_info[motor_index]; + + motor_info->status.All = 0; + motor_info->no_motion_count = 0; + motor_info->encoder_position = 0; + motor_info->position = 0; + brdptr->motor_info[motor_index].motor_motion = NULL; + /* PIC663 has DC motor support only */ + 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; + } + + 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 *) "PIC663_motor", epicsThreadPriorityMedium, + epicsThreadGetStackSize(epicsThreadStackMedium), + (EPICSTHREADFUNC) motor_task, (void *) &targs); + + return(OK); +} + diff --git a/motorApp/PiSrc/drvPIC663.h b/motorApp/PiSrc/drvPIC663.h new file mode 100644 index 00000000..91d8675a --- /dev/null +++ b/motorApp/PiSrc/drvPIC663.h @@ -0,0 +1,122 @@ +/* File: drvPIC663.h */ + + +/* Device Driver Support definitions for motor */ +/* + * Copied from devPIC862.cc by Jonathan Thompson, Jan 2011 + * + * Modification Log: + * ----------------- + * Jan 2011 - All references to 862 changed to 663 + * Status register definitions changed to match 663 + */ + +#ifndef INCdrvPIC663h +#define INCdrvPIC663h 1 + +#include "motordrvCom.h" +#include "asynDriver.h" +#include "asynDriver.h" +#include "asynOctetSyncIO.h" + +#define COMM_TIMEOUT 2.0 /* Timeout in seconds. */ + +struct PIC663controller +{ + asynUser *pasynUser; /* asynUser structure */ + int asyn_address; /* Use for GPIB or other address with asyn */ + CommStatus status; /* Controller communication status. */ + char asyn_port[80]; /* asyn port name */ +}; + + +/* Operation status */ +typedef union +{ + epicsUInt16 All; + struct + { +#ifdef MSB_First + unsigned int na8 :8; /* NA8. */ + unsigned int drv_cur_act :1; /* 7 - Drive current active. */ + unsigned int brake_on :1; /* 6 - Brake on. */ + unsigned int brake_off :1; /* 5 - Brake off. */ + unsigned int macro_act :1; /* 4 - Macro running. */ + unsigned int joy_on :1; /* 3 - Joystick on. */ + unsigned int ref_drv_act :1; /* 2 - Reference drive active. */ + unsigned int on_target :1; /* 1 - On target. */ + unsigned int ready :1; /* 0 - Ready. */ +#else + unsigned int ready :1; /* 0 - Ready. */ + unsigned int on_target :1; /* 1 - On target. */ + unsigned int ref_drv_act :1; /* 2 - Reference drive active. */ + unsigned int joy_on :1; /* 3 - Joystick on. */ + unsigned int macro_act :1; /* 4 - Macro running. */ + unsigned int brake_off :1; /* 5 - Brake off. */ + unsigned int brake_on :1; /* 6 - Brake on. */ + unsigned int drv_cur_act :1; /* 7 - Drive current active. */ + unsigned int na8 :8; /* NA8. */ +#endif + } Bits; +} C663_Status_Reg1; + +/* Hardware flags */ +typedef union +{ + epicsUInt16 All; + struct + { +#ifdef MSB_First + unsigned int na8 :8; /* NA8. */ + unsigned int in_4 :1; /* 7 - Digital input 4. */ + unsigned int in_3 :1; /* 6 - Digital input 3. */ + unsigned int in_2 :1; /* 3 - Digital input 2. */ + unsigned int in_1 :1; /* 4 - Digital input 1. */ + unsigned int unused :1; /* 3 - No function. */ + unsigned int hi_limit :1; /* 2 - Positive limit. */ + unsigned int ref_signal :1; /* 1 - Reference signal. */ + unsigned int lo_limit :1; /* 0 - Negative limit. */ +#else + unsigned int lo_limit :1; /* 0 - Negative limit. */ + unsigned int ref_signal :1; /* 1 - Reference signal. */ + unsigned int hi_limit :1; /* 2 - Positive limit. */ + unsigned int unused :1; /* 3 - No function. */ + unsigned int in_1 :1; /* 4 - Digital input 1. */ + unsigned int in_2 :1; /* 3 - Digital input 2. */ + unsigned int in_3 :1; /* 6 - Digital input 3. */ + unsigned int in_4 :1; /* 7 - Digital input 4. */ + unsigned int na8 :8; /* NA8. */ +#endif + } Bits; +} C663_Status_Reg2; + +/* Error reporting */ +typedef union +{ + epicsUInt16 All; + struct + { +#ifdef MSB_First + unsigned int na8 :8; /* NA8. */ + unsigned int error :8; /* Error code. */ +#else + unsigned int error :8; /* Error code. */ + unsigned int na8 :8; /* NA8. */ +#endif + } Bits; +} C663_Status_Reg3; + +#define C663_Status_Reg3_NO_ERROR 0x00 +#define C663_Status_Reg3_RS232_TIMEOUT 0x01 +#define C663_Status_Reg3_RS232_OVERFLOW 0x02 +#define C663_Status_Reg3_MACRO_STORAGE_FULL 0x03 +#define C663_Status_Reg3_MACRO_OUT_OF_RANGE 0x04 +#define C663_Status_Reg3_WRONG_MACRO_CMD 0x05 +#define C663_Status_Reg3_COMMAND_ERROR 0x06 + + +/* Function prototypes. */ +extern RTN_STATUS PIC663Setup(int, int); +extern RTN_STATUS PIC663Config(int, const char *, int); + +#endif /* INCdrvPIC663h */