From 90da52144e19ed20fc5bb2ff044a3e14b9a06e96 Mon Sep 17 00:00:00 2001 From: jsullivan-anl Date: Fri, 14 Apr 2006 20:34:42 +0000 Subject: [PATCH] Initial version of the 662 driver --- motorApp/PiSrc/PIC662Register.cc | 66 ++++ motorApp/PiSrc/devPIC662.cc | 304 ++++++++++++++++ motorApp/PiSrc/drvPIC662.cc | 597 +++++++++++++++++++++++++++++++ motorApp/PiSrc/drvPIC662.h | 108 ++++++ 4 files changed, 1075 insertions(+) create mode 100644 motorApp/PiSrc/PIC662Register.cc create mode 100644 motorApp/PiSrc/devPIC662.cc create mode 100644 motorApp/PiSrc/drvPIC662.cc create mode 100644 motorApp/PiSrc/drvPIC662.h diff --git a/motorApp/PiSrc/PIC662Register.cc b/motorApp/PiSrc/PIC662Register.cc new file mode 100644 index 00000000..1622ae15 --- /dev/null +++ b/motorApp/PiSrc/PIC662Register.cc @@ -0,0 +1,66 @@ +/* +FILENAME... PiRegister.cc +USAGE... Register IMS motor device driver shell commands. + +Version: $Revision: 1.1 $ +Modified By: $Author: sullivan $ +Last Modified: $Date: 2006-04-14 20:34:42 $ +*/ + +/***************************************************************** + 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 "drvPIC662.h" +#include "epicsExport.h" + +extern "C" +{ + +// Pi 662 Setup arguments +static const iocshArg setupArg0 = {"Max. controller count", iocshArgInt}; +static const iocshArg setupArg1 = {"Polling rate", iocshArgInt}; +// Pi 662 Config arguments +static const iocshArg configArg0 = {"Card being configured", iocshArgInt}; +static const iocshArg configArg1 = {"asyn port name", iocshArgString}; + +static const iocshArg * const PIC662SetupArgs[2] = {&setupArg0, &setupArg1}; +static const iocshArg * const PIC662ConfigArgs[2] = {&configArg0, &configArg1}; + +static const iocshFuncDef setupPIC662 = {"PIC662Setup", 2, PIC662SetupArgs}; +static const iocshFuncDef configPIC662 = {"PIC662Config", 2, PIC662ConfigArgs}; + +static void setupPIC662CallFunc(const iocshArgBuf *args) +{ + PIC662Setup(args[0].ival, args[1].ival); +} + + +static void configPIC662CallFunc(const iocshArgBuf *args) +{ + PIC662Config(args[0].ival, args[1].sval); +} + + +static void PIC662motorRegister(void) +{ + iocshRegister(&setupPIC662, setupPIC662CallFunc); + iocshRegister(&configPIC662, configPIC662CallFunc); +} + +epicsExportRegistrar(PIC662motorRegister); + +} // extern "C" diff --git a/motorApp/PiSrc/devPIC662.cc b/motorApp/PiSrc/devPIC662.cc new file mode 100644 index 00000000..c1905c1c --- /dev/null +++ b/motorApp/PiSrc/devPIC662.cc @@ -0,0 +1,304 @@ +/* +FILENAME... devPIC662.cc +USAGE... Motor record device level support for Physik Instrumente (PI) + GmbH & Co. C-844 motor controller. + +Version: $Revision: 1.1 $ +Modified By: $Author: sullivan $ +Last Modified: $Date: 2006-04-14 20:34:42 $ +*/ + +/* + * Original Author: Ron Sluiter + * Date: 12/17/03 + * + * Experimental Physics and Industrial Control System (EPICS) + * + * Copyright 1991, the Regents of the University of California, + * and the University of Chicago Board of Governors. + * + * This software was produced under U.S. Government contracts: + * (W-7405-ENG-36) at the Los Alamos National Laboratory, + * and (W-31-109-ENG-38) at Argonne National Laboratory. + * + * Initial development by: + * The Controls and Automation Group (AT-8) + * Ground Test Accelerator + * Accelerator Technology Division + * Los Alamos National Laboratory + * + * Co-developed with + * The Controls and Computing Group + * Accelerator Systems Division + * Advanced Photon Source + * Argonne National Laboratory + * + * Modification Log: + * ----------------- + * .01 03/08/06 jps - copied from devPI.cc + */ + + +#include +#include "motorRecord.h" +#include "motor.h" +#include "motordevCom.h" +#include "epicsExport.h" +#include "drvPIC662.h" + +extern struct driver_table PIC662_access; + +/* ----------------Create the dsets for devPIC662----------------- */ +static struct driver_table *drvtabptr; +static long PIC662_init(void *); +static long PIC662_init_record(void *); +static long PIC662_start_trans(struct motorRecord *); +static RTN_STATUS PIC662_build_trans(motor_cmnd, double *, struct motorRecord *); +static RTN_STATUS PIC662_end_trans(struct motorRecord *); + +struct motor_dset devPIC662 = +{ + {8, NULL, (DEVSUPFUN) PIC662_init, (DEVSUPFUN) PIC662_init_record, NULL}, + motor_update_values, + PIC662_start_trans, + PIC662_build_trans, + PIC662_end_trans +}; + +extern "C" {epicsExportAddress(dset,devPIC662);} + +/* --------------------------- program data --------------------- */ + +/* This table is used to define the command types */ +/* WARNING! this must match "motor_cmnd" in motor.h */ + +static msg_types PIC662_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 **PIC662_cards; + +/* --------------------------- program data --------------------- */ + + +/* initialize device support for PIC662 stepper motor */ +static long PIC662_init(void *arg) +{ + long rtnval; + int after = (int) arg; + + if (after == 0) + { + drvtabptr = &PIC662_access; + (drvtabptr->init)(); + } + + rtnval = motor_init_com(after, *drvtabptr->cardcnt_ptr, drvtabptr, &PIC662_cards); + return(rtnval); +} + + +/* initialize a record instance */ +static long PIC662_init_record(void *arg) +{ + struct motorRecord *mr = (struct motorRecord *) arg; + return(motor_init_record_com(mr, *drvtabptr->cardcnt_ptr, drvtabptr, PIC662_cards)); +} + + +/* start building a transaction */ +static long PIC662_start_trans(struct motorRecord *mr) +{ + motor_start_trans_com(mr, PIC662_cards); + return(OK); +} + + +/* end building a transaction */ +static RTN_STATUS PIC662_end_trans(struct motorRecord *mr) +{ + motor_end_trans_com(mr, drvtabptr); + return(OK); +} + + +/* add a part to the transaction */ +static RTN_STATUS PIC662_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 PIC662controller *cntrl; + char buff[110]; + int axis, card, maxdigits; + unsigned int size; + double dval, 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; + axis = motor_call->signal + 1; + brdptr = (*trans->tabptr->card_array)[card]; + if (brdptr == NULL) + return(rtnval = ERROR); + + cntrl = (struct PIC662controller *) brdptr->DevicePrivate; + + maxdigits = cntrl->res_decpts; + cntrl_units = dval * cntrl->drive_resolution; + + if (PIC662_table[command] > motor_call->type) + motor_call->type = PIC662_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, "DEV:CONT REM\nPOS %.*f", maxdigits, cntrl_units); + break; + + case MOVE_REL: + sprintf(buff, "DEV:CONT REM\nPOS:REL %.*f", maxdigits, cntrl_units); + break; + + case HOME_FOR: + case HOME_REV: + send = false; + break; + + case LOAD_POS: + send = false; /* Can't Load a Position */ + break; + + case SET_VEL_BASE: + + break; + + case SET_VELOCITY: + send = false; /* DC motor; not base velocity. */ + break; + + case SET_ACCEL: + send = false; /* DC motor; not base velocity. */ + break; + + case GO: + /* The PIC662 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: + // Force device polling to stop. + // There is no HALT command to the motor controller + cntrl->stop_status = true; + send = false; + break; + + case JOG_VELOCITY: + case JOG: + send = false; + break; + + case SET_PGAIN: + send = false; + break; + case SET_IGAIN: + send = false; + break; + case SET_DGAIN: + send = false; + break; + + case ENABLE_TORQUE: + send = false; + break; + + case DISABL_TORQUE: + send = false; + 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("PIC662_build_trans(): buffer overflow.\n"); + else + strcat(motor_call->message, buff); + return(rtnval); +} diff --git a/motorApp/PiSrc/drvPIC662.cc b/motorApp/PiSrc/drvPIC662.cc new file mode 100644 index 00000000..80f69322 --- /dev/null +++ b/motorApp/PiSrc/drvPIC662.cc @@ -0,0 +1,597 @@ +/* +FILENAME... drvPIC662.cc +USAGE... Motor record driver level support for Physik Instrumente (PI) + GmbH & Co. C-844 motor controller. + +Version: $Revision: 1.1 $ +Modified By: $Author: sullivan $ +Last Modified: $Date: 2006-04-14 20:34:42 $ +*/ + +/* + * Original Author: Joe Sullivan + * Date: 03/08/06 + * + * Experimental Physics and Industrial Control System (EPICS) + * + * Copyright 1991, the Regents of the University of California, + * and the University of Chicago Board of Governors. + * + * This software was produced under U.S. Government contracts: + * (W-7405-ENG-36) at the Los Alamos National Laboratory, + * and (W-31-109-ENG-38) at Argonne National Laboratory. + * + * Initial development by: + * The Controls and Automation Group (AT-8) + * Ground Test Accelerator + * Accelerator Technology Division + * Los Alamos National Laboratory + * + * Co-developed with + * The Controls and Computing Group + * Accelerator Systems Division + * Advanced Photon Source + * Argonne National Laboratory + * + * Modification Log: + * ----------------- + * .01 03/08/06 jps - copied from drvPIC884.cc + */ + +/* +DESIGN LIMITATIONS... + 1 - Like all controllers, the PIC662 must be powered-on when EPICS is first + booted up. + 2 - The PIC662 cannot be power cycled while EPICS is up and running. The + consequences are permanent communication lose with the PIC662 until + EPICS is rebooted. +*/ + +#include +#include +#include +#include +#include "motor.h" +#include "drvPIC662.h" +#include "epicsExport.h" + +#define GET_IDENT "*IDN?" +#define GET_STATUS "*ESR?" // Status Register +#define GET_POS "POS?" // Status Register + +#define PIC662_NUM_CARDS 8 +#define MAX_AXES 1 +#define BUFF_SIZE 100 /* Maximum length of string to/from PIC662 */ + +/*----------------debugging-----------------*/ +#ifdef __GNUG__ + #ifdef DEBUG + #define Debug(l, f, args...) { if(l<=drvPIC662debug) printf(f,## args); } + #else + #define Debug(l, f, args...) + #endif +#else + #define Debug() +#endif +volatile int drvPIC662debug = 0; +extern "C" {epicsExportAddress(int, drvPIC662debug);} + +/* --- Local data. --- */ +int PIC662_num_cards = 0; +static char *PIC662_axis[4] = {"1", "2", "3", "4"}; + +/* 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 PIC662_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, + PIC662_axis +}; + +struct +{ + long number; + long (*report) (int); + long (*init) (void); +} drvPIC662 = {2, report, init}; + +extern "C" {epicsExportAddress(drvet, drvPIC662);} + +static struct thread_args targs = {SCAN_RATE, &PIC662_access, 0.0}; + +/********************************************************* + * Print out driver status report + *********************************************************/ +static long report(int level) +{ + int card; + + if (PIC662_num_cards <=0) + printf(" No PIC662 controllers configured.\n"); + else + { + for (card = 0; card < PIC662_num_cards; card++) + { + struct controller *brdptr = motor_state[card]; + + if (brdptr == NULL) + printf(" PIC662 controller %d connection failed.\n", card); + else + { + struct PIC662controller *cntrl; + + cntrl = (struct PIC662controller *) brdptr->DevicePrivate; + printf(" PIC662 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 (PIC662_num_cards <= 0) + { + Debug(1, "init(): PIC662 driver disabled. PIC662Setup() 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 PIC662controller *cntrl; + struct mess_node *nodeptr; + register struct mess_info *motor_info; + /* Message parsing variables */ + char buff[BUFF_SIZE]; + E662_ESR_REG mstat; + int rtn_state; + int comm_status; + long motorData; + bool plusdir, ls_active = false, plusLS, minusLS; + bool eventflgs; + msta_field status; + + cntrl = (struct PIC662controller *) motor_state[card]->DevicePrivate; + motor_info = &(motor_state[card]->motor_info[signal]); + nodeptr = motor_info->motor_motion; + status.All = motor_info->status.All; + + send_mess(card, GET_STATUS, (char) NULL); + comm_status = recv_mess(card, buff, 1); + if (comm_status == 0) + { + if (cntrl->status == NORMAL) + { + cntrl->status = RETRY; + rtn_state = 0; + } + else + { + cntrl->status = COMM_ERR; + status.Bits.CNTRL_COMM_ERR = 1; + status.Bits.RA_PROBLEM = 1; + rtn_state = 1; + } + goto exit; + } + + cntrl->status = NORMAL; + status.Bits.CNTRL_COMM_ERR = 0; + status.Bits.EA_POSITION = 0; + + mstat.All = atoi(&buff[0]); + + eventflgs = (mstat.Bits.OpComplete || + mstat.Bits.DevError || + mstat.Bits.ExeError) ? true : false; + + status.Bits.RA_DONE = (eventflgs || cntrl->stop_status)? 1 : 0; + + cntrl->stop_status = false; + + /* + * Parse motor position + * Position string format: 1TP5.012,2TP1.123,3TP-100.567,... + * Skip to substring for this motor, convert to double + */ + + send_mess(card, GET_POS, (char) NULL); + recv_mess(card, buff, 1); + + motorData = NINT (atof(buff) / cntrl->drive_resolution); + + if (motorData == motor_info->position) + { + if (nodeptr != 0) /* Increment counter only if motor is moving. */ + if (motor_info->no_motion_count++ > 1) + status.Bits.RA_DONE = 1; // No done indicator - stop when at position + + } + else + { + status.Bits.RA_DIRECTION = (motorData >= motor_info->position) ? 1 : 0; + motor_info->position = motorData; + motor_info->no_motion_count = 0; + } + + // motor_info->encoder_position = (epicsInt32) motorData; + status.Bits.RA_PROBLEM = 0; + + plusdir = (status.Bits.RA_DIRECTION) ? true : false; + + // No hardware limit switch feedback + plusLS = false; + minusLS = 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; + + + /* 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); + nodeptr->postmsgptr = NULL; + } + +exit: + motor_info->status.All = status.All; + return(rtn_state); +} + + +/*****************************************************/ +/* send a message to the PIC662 board */ +/* send_mess() */ +/*****************************************************/ +static RTN_STATUS send_mess(int card, char const *com, char *name) +{ + char local_buff[MAX_MSG_SIZE]; + struct PIC662controller *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("drvPIC662.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("drvPIC662.cc:send_mess() - invalid card #%d\n", card); + return(ERROR); + } + + local_buff[0] = (char) NULL; /* Terminate local buffer. */ + + /* Make a local copy of the string. */ + strcat(local_buff, com); + + Debug(2, "send_mess(): message = %s\n", local_buff); + + cntrl = (struct PIC662controller *) motor_state[card]->DevicePrivate; + pasynOctetSyncIO->write(cntrl->pasynUser, local_buff, strlen(local_buff), + COMM_TIMEOUT, &nwrite); + + return(OK); +} + + +/*****************************************************/ +/* receive a message from the PIC662 board */ +/* recv_mess() */ +/*****************************************************/ +static int recv_mess(int card, char *com, int flag) +{ + struct PIC662controller *cntrl; + size_t nread = 0; + asynStatus status = asynError; + int eomReason; + + /* Check that card exists */ + if (!motor_state[card]) + return(ERROR); + + cntrl = (struct PIC662controller *) 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; + } + + Debug(2, "recv_mess(): message = \"%s\"\n", com); + return(nread); +} + + +/*****************************************************/ +/* Setup system configuration */ +/* PIC662Setup() */ +/*****************************************************/ +RTN_STATUS +PIC662Setup(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 > PIC662_NUM_CARDS) + PIC662_num_cards = PIC662_NUM_CARDS; + else + PIC662_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 PIC662Config 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(PIC662_num_cards * + sizeof(struct controller *)); + + for (itera = 0; itera < PIC662_num_cards; itera++) + motor_state[itera] = (struct controller *) NULL; + + return(OK); +} + + +/*****************************************************/ +/* Configure a controller */ +/* PIC662Config() */ +/*****************************************************/ +RTN_STATUS +PIC662Config(int card, /* card being configured */ + const char *name) /* asyn port name */ +{ + struct PIC662controller *cntrl; + + if (card < 0 || card >= PIC662_num_cards) + return (ERROR); + + motor_state[card] = (struct controller *) malloc(sizeof(struct controller)); + motor_state[card]->DevicePrivate = malloc(sizeof(struct PIC662controller)); + cntrl = (struct PIC662controller *) motor_state[card]->DevicePrivate; + + strcpy(cntrl->asyn_port, 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 PIC662controller *cntrl; + int card_index; + char buff[BUFF_SIZE]; + int status; + asynStatus success_rtn; + static const char output_terminator[] = "\n"; + static const char input_terminator[] = "\n"; + + initialized = true; /* Indicate that driver is initialized. */ + + /* Check for setup */ + if (PIC662_num_cards <= 0) + return(ERROR); + + for (card_index = 0; card_index < PIC662_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 PIC662controller *) brdptr->DevicePrivate; + + /* Initialize communications channel */ + success_rtn = pasynOctetSyncIO->connect(cntrl->asyn_port, 0, + &cntrl->pasynUser, NULL); + status = 0; + 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); + + do + { + send_mess(card_index, GET_IDENT, (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; + brdptr->total_axis = 1; /* Single axis controller */ + + /* Fixed resolution */ + cntrl->res_decpts = 3; + cntrl->drive_resolution = 1.0/pow(10.0,cntrl->res_decpts); + + + /* Set Controller to REMOTE mode */ + send_mess(card_index, REMOTE_MODE, (char) NULL); + + { + struct mess_info *motor_info = &brdptr->motor_info[0]; + + motor_info->status.All = 0; + motor_info->no_motion_count = 0; + motor_info->encoder_position = 0; + motor_info->position = 0; + brdptr->motor_info[0].motor_motion = NULL; + /* PIC662 has DC motor support only */ + motor_info->encoder_present = NO; + motor_info->status.Bits.EA_PRESENT = 0; + motor_info->pid_present = NO; + motor_info->status.Bits.GAIN_SUPPORT = 0; + + set_status(card_index, 0); /* 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 *) "PIC662_motor", epicsThreadPriorityMedium, + epicsThreadGetStackSize(epicsThreadStackMedium), + (EPICSTHREADFUNC) motor_task, (void *) &targs); + + return(OK); +} + diff --git a/motorApp/PiSrc/drvPIC662.h b/motorApp/PiSrc/drvPIC662.h new file mode 100644 index 00000000..f45d40ce --- /dev/null +++ b/motorApp/PiSrc/drvPIC662.h @@ -0,0 +1,108 @@ +/* +FILENAME... drvPIC662.h +USAGE... This file contains driver "include" information that is specific to +Physik Instrumente (PI) GmbH & Co. motor controller driver support. + +Version: $Revision: 1.1 $ +Modified By: $Author: sullivan $ +Last Modified: $Date: 2006-04-14 20:34:42 $ +*/ + +/* + * Original Author: Joe Sullivan + * Date: 12/17/03 + * + * Experimental Physics and Industrial Control System (EPICS) + * + * Copyright 1991, the Regents of the University of California, + * and the University of Chicago Board of Governors. + * + * This software was produced under U.S. Government contracts: + * (W-7405-ENG-36) at the Los Alamos National Laboratory, + * and (W-31-109-ENG-38) at Argonne National Laboratory. + * + * Initial development by: + * The Controls and Automation Group (AT-8) + * Ground Test Accelerator + * Accelerator Technology Division + * Los Alamos National Laboratory + * + * Co-developed with + * The Controls and Computing Group + * Accelerator Systems Division + * Advanced Photon Source + * Argonne National Laboratory + * + * + * + * Modification Log: + * ----------------- + * .01 03/08/06 jps - copied from drvPI.h + */ + +#ifndef INCdrvPIh +#define INCdrvPIh 1 + +#include "motor.h" +#include "motordrvCom.h" +#include "asynDriver.h" +#include "asynOctetSyncIO.h" + +#define COMM_TIMEOUT 2 /* Timeout in seconds. */ + +// E662 Command: switch to remote control mode +// This is done before each positioning move because +// the controller defaults to 'Local' mode on power up. +// Status can be read while in 'Local' mode. +#define REMOTE_MODE "DEV:CONT REM" + +/* PIC662 specific data is stored in this structure. */ +struct PIC662controller +{ + asynUser *pasynUser; /* For RS-232 */ + int asyn_address; /* Use for GPIB or other address with asyn */ + char asyn_port[80]; /* asyn port name */ + CommStatus status; /* Controller communication status. */ + + bool stop_status; /* Signal to set_status() - stop polling */ + double drive_resolution; + int res_decpts; /* Max decimal points */ +}; + +/* Standard Event Status register */ + + +typedef union +{ + epicsUInt8 All; + struct + { +#ifdef MSB_First + unsigned int PowerOn :1; // (Bit 7) Power On + unsigned int UserReq :1; // User Request + unsigned int CmdError :1; // Command Error + unsigned int ExeError :1; // Execution Error + unsigned int DevError :1; // Device Dependant Error + unsigned int QueryError :1; // Query Error + unsigned int ReqControl :1; // Request Control + unsigned int OpComplete :1; // (Bit 0) Operation Complete +#else + unsigned int OpComplete :1; // (Bit 0) Operation Complete + unsigned int ReqControl :1; // Request Control + unsigned int QueryError :1; // Query Error + unsigned int DevError :1; // Device Dependant Error + unsigned int ExeError :1; // Execution Error + unsigned int CmdError :1; // Command Error + unsigned int UserReq :1; // User Request + unsigned int PowerOn :1; // (Bit 7) Power On +#endif + } Bits; +} E662_ESR_REG; + + +/* Function prototypes. */ +extern RTN_STATUS PIC662Setup(int, int); +extern RTN_STATUS PIC662Config(int, const char *); + +#endif /* INCdrvPIh */ +