Files
motorDSM/dsmApp/src/devMD90.cc

332 lines
9.1 KiB
C++
Raw Normal View History

/* File: devMD90.cc */
2003-05-27 13:39:41 +00:00
/* Device Support Routines for motor */
/*
* Original Author: Mark Rivers
* Date: 02-24-2002
*
* Modification Log:
* -----------------
* .00 02-24-2002 mlr initialized from devPM304.c
* .01 05-23-2003 rls Converted to R3.14.x.
* .02 02-19-2004 mlr Bug fix when not sending anything
2003-05-27 13:39:41 +00:00
*/
#define VERSION 1.00
#include <string.h>
#include <math.h>
#include "motorRecord.h"
#include "motor.h"
#include "motordevCom.h"
#include "drvMD90.h"
2003-05-27 13:39:41 +00:00
#include "epicsExport.h"
#define STATIC static
extern struct driver_table MD90_access;
2003-05-27 13:39:41 +00:00
#define NINT(f) (long)((f)>0 ? (f)+0.5 : (f)-0.5)
volatile int devMD90Debug = 0;
extern "C" {epicsExportAddress(int, devMD90Debug);}
static inline void Debug(int level, const char *format, ...) {
#ifdef DEBUG
if (level < devMD90Debug)
{
va_list pVar;
va_start(pVar, format);
vprintf(format, pVar);
va_end(pVar);
}
#endif
}
2003-05-27 13:39:41 +00:00
/* Debugging levels:
* devMD90Debug >= 3 Print new part of command and command string so far
* at the end of MD90_build_trans
2003-05-27 13:39:41 +00:00
*/
/* ----------------Create the dsets for devMD90----------------- */
2003-05-27 13:39:41 +00:00
STATIC struct driver_table *drvtabptr;
STATIC long MD90_init(int);
STATIC long MD90_init_record(void *);
STATIC long MD90_start_trans(struct motorRecord *);
STATIC RTN_STATUS MD90_build_trans(motor_cmnd, double *, struct motorRecord *);
STATIC RTN_STATUS MD90_end_trans(struct motorRecord *);
2003-05-27 13:39:41 +00:00
struct motor_dset devMD90 =
2003-05-27 13:39:41 +00:00
{
{8, NULL, (DEVSUPFUN) MD90_init, (DEVSUPFUN) MD90_init_record, NULL},
2003-05-27 13:39:41 +00:00
motor_update_values,
MD90_start_trans,
MD90_build_trans,
MD90_end_trans
2003-05-27 13:39:41 +00:00
};
extern "C" {epicsExportAddress(dset,devMD90);}
2003-05-27 13:39:41 +00:00
/* --------------------------- program data --------------------- */
/* This table is used to define the command types */
static msg_types MD90_table[] = {
2003-05-27 13:39:41 +00:00
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 /* JOB_VELOCITY */
2003-05-27 13:39:41 +00:00
};
static struct board_stat **MD90_cards;
2003-05-27 13:39:41 +00:00
/* --------------------------- program data --------------------- */
/* initialize device support for MD90 stepper motor */
STATIC long MD90_init(int after)
2003-05-27 13:39:41 +00:00
{
long rtnval;
Debug(5, "MD90_init: entry\n");
if (!after)
2003-05-27 13:39:41 +00:00
{
drvtabptr = &MD90_access;
2003-05-27 13:39:41 +00:00
(drvtabptr->init)();
}
rtnval = motor_init_com(after, *drvtabptr->cardcnt_ptr, drvtabptr, &MD90_cards);
Debug(5, "MD90_init: exit\n");
2003-05-27 13:39:41 +00:00
return(rtnval);
}
/* initialize a record instance */
STATIC long MD90_init_record(void *arg)
2003-05-27 13:39:41 +00:00
{
struct motorRecord *mr = (struct motorRecord *) arg;
long rtnval;
Debug(5, "MD90_init_record: entry\n");
2003-05-27 13:39:41 +00:00
rtnval = motor_init_record_com(mr, *drvtabptr->cardcnt_ptr,
drvtabptr, MD90_cards);
2003-05-27 13:39:41 +00:00
return(rtnval);
Debug(5, "MD90_init_record: exit\n");
2003-05-27 13:39:41 +00:00
}
/* start building a transaction */
STATIC long MD90_start_trans(struct motorRecord *mr)
2003-05-27 13:39:41 +00:00
{
return(OK);
}
/* end building a transaction */
STATIC RTN_STATUS MD90_end_trans(struct motorRecord *mr)
2003-05-27 13:39:41 +00:00
{
return(OK);
}
/* add a part to the transaction */
STATIC RTN_STATUS MD90_build_trans(motor_cmnd command, double *parms, struct motorRecord *mr)
2003-05-27 13:39:41 +00:00
{
struct motor_trans *trans = (struct motor_trans *) mr->dpvt;
struct mess_node *motor_call;
struct controller *brdptr;
struct MD90controller *cntrl;
2003-05-27 13:39:41 +00:00
char buff[30];
int axis, card;
RTN_STATUS rtnval;
double dval;
long ival;
bool send;
2003-05-27 13:39:41 +00:00
send = true;
2003-05-27 13:39:41 +00:00
rtnval = OK;
buff[0] = '\0';
/* Protect against NULL pointer with WRTITE_MSG(GO/STOP_AXIS/GET_INFO, NULL). */
dval = (parms == NULL) ? 0.0 : *parms;
ival = NINT(dval);
2003-05-27 13:39:41 +00:00
rtnval = (RTN_STATUS) motor_start_trans_com(mr, MD90_cards);
Debug(5, "MD90_build_trans: entry, motor_start_trans_com=%d\n", rtnval);
2003-05-27 13:39:41 +00:00
motor_call = &(trans->motor_call);
motor_call->type = MD90_table[command];
2003-05-27 13:39:41 +00:00
card = motor_call->card;
axis = motor_call->signal;
brdptr = (*trans->tabptr->card_array)[card];
Debug(5, "MD90_build_trans: axis=%d, command=%d\n", axis, command);
2003-05-27 13:39:41 +00:00
if (brdptr == NULL)
return(rtnval = ERROR);
cntrl = (struct MD90controller *) brdptr->DevicePrivate;
2003-05-27 13:39:41 +00:00
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, MD90_cards);
motor_call->type = MD90_table[command];
2003-05-27 13:39:41 +00:00
}
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 = motor_end_trans_com(mr, drvtabptr);
rtnval = (RTN_STATUS) motor_start_trans_com(mr, MD90_cards);
motor_call->type = MD90_table[command];
2003-05-27 13:39:41 +00:00
}
if (strlen(mr->post) != 0)
motor_call->postmsgptr = (char *) &mr->post;
break;
default:
break;
}
switch (command)
{
case MOVE_ABS:
sprintf(motor_call->message, "#%02dG%+ld", axis, ival);
break;
case MOVE_REL:
sprintf(motor_call->message, "#%02dI%+ld", axis, ival);
break;
case HOME_FOR:
sprintf(motor_call->message, "#%02dH+", axis);
break;
case HOME_REV:
sprintf(motor_call->message, "#%02dH-", axis);
break;
case LOAD_POS:
sprintf(motor_call->message, "#%02dP=%+ld", axis, ival);
break;
case SET_VEL_BASE:
send=false;
2003-05-27 13:39:41 +00:00
trans->state = IDLE_STATE;
break; /* MD90 does not use base velocity */
2003-05-27 13:39:41 +00:00
case SET_VELOCITY:
ival = (int) (fabs(115200./dval) + 0.5);
if (ival < 2) ival=2;
if (ival > 255) ival = 255;
sprintf(motor_call->message, "#%02dV=%ld", axis, ival);
break;
case SET_ACCEL:
/* dval is acceleration in steps/sec/sec */
/* MD-90 is programmed with Ramp Index (R) where: */
2003-05-27 13:39:41 +00:00
/* dval (steps/sec/sec) = 720,000/(256-R) */
/* or R=256-(720,000/dval) */
ival = (int) (256-(720000./dval)+0.5);
if (ival < 1) ival=1;
if (ival > 255) ival=255;
sprintf(motor_call->message, "#%02dR=%ld", axis, ival);
break;
case GO:
/*
* The MD90 starts moving immediately on move commands, GO command
2003-05-27 13:39:41 +00:00
* does nothing
*/
send=false;
2003-05-27 13:39:41 +00:00
trans->state = IDLE_STATE;
break;
case SET_ENC_RATIO:
/*
* The MD90 does not have the concept of encoder ratio, ignore this
2003-05-27 13:39:41 +00:00
* command
*/
send=false;
2003-05-27 13:39:41 +00:00
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:
sprintf(motor_call->message, "#%02dQ", axis);
break;
case JOG:
/* MD-90 does not have jog command. Move 1 million steps */
2003-05-27 13:39:41 +00:00
ival = (int) (fabs(115200./dval) + 0.5);
if (ival < 2) ival=2;
if (ival > 65535) ival = 65535;
sprintf(motor_call->message, "#%02dC=%ld", axis, ival);
rtnval = motor_end_trans_com(mr, drvtabptr);
rtnval = (RTN_STATUS) motor_start_trans_com(mr, MD90_cards);
motor_call->type = MD90_table[command];
2003-05-27 13:39:41 +00:00
if (dval > 0.) {
/* This is a positive move in MD90 coordinates */
2003-05-27 13:39:41 +00:00
sprintf(motor_call->message, "#%02dM+1000000", axis);
} else {
/* This is a negative move in MD90 coordinates */
2003-05-27 13:39:41 +00:00
sprintf(motor_call->message, "#%02dM-1000000", axis);
}
break;
case SET_PGAIN:
case SET_IGAIN:
case SET_DGAIN:
send=false;
2003-05-27 13:39:41 +00:00
trans->state = IDLE_STATE;
break;
case ENABLE_TORQUE:
sprintf(motor_call->message, "#%02dW=1", axis);
break;
case DISABL_TORQUE:
sprintf(motor_call->message, "#%02dW=0", axis);
break;
case SET_HIGH_LIMIT:
case SET_LOW_LIMIT:
send=false;
2003-05-27 13:39:41 +00:00
trans->state = IDLE_STATE;
break;
default:
rtnval = ERROR;
}
if(send == false)
return(rtnval);
else {
rtnval = motor_end_trans_com(mr, drvtabptr);
Debug(5, "MD90_send_msg: motor_end_trans_com status=%d, exit\n", rtnval);
return (rtnval);
}
2003-05-27 13:39:41 +00:00
}