Files
motorDSM/dsmApp/src/MD90Driver.cpp

341 lines
12 KiB
C++
Raw Normal View History

2012-03-02 00:12:33 +00:00
/*
FILENAME... MD90Driver.cpp
USAGE... Motor driver support for the DSM MD-90 controller.
2012-03-02 00:12:33 +00:00
Mark Rivers
March 1, 2012
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <iocsh.h>
#include <epicsThread.h>
#include <asynOctetSyncIO.h>
#include <epicsExport.h>
#include "MD90Driver.h"
2012-03-02 00:12:33 +00:00
#define NINT(f) (int)((f)>0 ? (f)+0.5 : (f)-0.5)
/** Creates a new MD90Controller object.
2012-03-02 00:12:33 +00:00
* \param[in] portName The name of the asyn port that will be created for this driver
* \param[in] MD90PortName The name of the drvAsynSerialPort that was created previously to connect to the MD90 controller
2012-03-02 00:12:33 +00:00
* \param[in] numAxes The number of axes that this controller supports
* \param[in] movingPollPeriod The time between polls when any axis is moving
* \param[in] idlePollPeriod The time between polls when no axis is moving
*/
MD90Controller::MD90Controller(const char *portName, const char *MD90PortName, int numAxes,
2012-03-02 00:12:33 +00:00
double movingPollPeriod, double idlePollPeriod)
: asynMotorController(portName, numAxes, NUM_MD90_PARAMS,
2012-03-02 00:12:33 +00:00
0, // No additional interfaces beyond those in base class
0, // No additional callback interfaces beyond those in base class
ASYN_CANBLOCK | ASYN_MULTIDEVICE,
1, // autoconnect
0, 0) // Default priority and stack size
{
int axis;
asynStatus status;
MD90Axis *pAxis;
static const char *functionName = "MD90Controller::MD90Controller";
2012-03-02 00:12:33 +00:00
/* Connect to MD90 controller */
status = pasynOctetSyncIO->connect(MD90PortName, 0, &pasynUserController_, NULL);
2012-03-02 00:12:33 +00:00
if (status) {
asynPrint(this->pasynUserSelf, ASYN_TRACE_ERROR,
"%s: cannot connect to MD-90 controller\n",
2012-03-02 00:12:33 +00:00
functionName);
}
for (axis=0; axis<numAxes; axis++) {
pAxis = new MD90Axis(this, axis);
2012-03-02 00:12:33 +00:00
}
startPoller(movingPollPeriod, idlePollPeriod, 2);
}
/** Creates a new MD90Controller object.
2012-03-02 00:12:33 +00:00
* Configuration command, called directly or from iocsh
* \param[in] portName The name of the asyn port that will be created for this driver
* \param[in] MD90PortName The name of the drvAsynIPPPort that was created previously to connect to the MD90 controller
2012-03-02 00:12:33 +00:00
* \param[in] numAxes The number of axes that this controller supports
* \param[in] movingPollPeriod The time in ms between polls when any axis is moving
* \param[in] idlePollPeriod The time in ms between polls when no axis is moving
*/
extern "C" int MD90CreateController(const char *portName, const char *MD90PortName, int numAxes,
2012-03-02 00:12:33 +00:00
int movingPollPeriod, int idlePollPeriod)
{
MD90Controller *pMD90Controller
= new MD90Controller(portName, MD90PortName, numAxes, movingPollPeriod/1000., idlePollPeriod/1000.);
pMD90Controller = NULL;
2012-03-02 00:12:33 +00:00
return(asynSuccess);
}
/** Reports on status of the driver
* \param[in] fp The file pointer on which report information will be written
* \param[in] level The level of report detail desired
*
* If details > 0 then information is printed about each axis.
* After printing controller-specific information it calls asynMotorController::report()
*/
void MD90Controller::report(FILE *fp, int level)
2012-03-02 00:12:33 +00:00
{
fprintf(fp, "MD-90 motor driver %s, numAxes=%d, moving poll period=%f, idle poll period=%f\n",
2012-03-02 00:12:33 +00:00
this->portName, numAxes_, movingPollPeriod_, idlePollPeriod_);
// Call the base class method
asynMotorController::report(fp, level);
}
/** Returns a pointer to an MD90Axis object.
2012-03-02 00:12:33 +00:00
* Returns NULL if the axis number encoded in pasynUser is invalid.
* \param[in] pasynUser asynUser structure that encodes the axis index number. */
MD90Axis* MD90Controller::getAxis(asynUser *pasynUser)
2012-03-02 00:12:33 +00:00
{
return static_cast<MD90Axis*>(asynMotorController::getAxis(pasynUser));
2012-03-02 00:12:33 +00:00
}
/** Returns a pointer to an MD90Axis object.
2012-03-02 00:12:33 +00:00
* Returns NULL if the axis number encoded in pasynUser is invalid.
* \param[in] axisNo Axis index number. */
MD90Axis* MD90Controller::getAxis(int axisNo)
2012-03-02 00:12:33 +00:00
{
return static_cast<MD90Axis*>(asynMotorController::getAxis(axisNo));
2012-03-02 00:12:33 +00:00
}
// These are the MD90Axis methods
2012-03-02 00:12:33 +00:00
/** Creates a new MD90Axis object.
* \param[in] pC Pointer to the MD90Controller to which this axis belongs.
2012-03-02 00:12:33 +00:00
* \param[in] axisNo Index number of this axis, range 0 to pC->numAxes_-1.
*
* Initializes register numbers, etc.
*/
MD90Axis::MD90Axis(MD90Controller *pC, int axisNo)
2012-03-02 00:12:33 +00:00
: asynMotorAxis(pC, axisNo),
pC_(pC)
{
}
/** Reports on status of the axis
2012-03-02 00:12:33 +00:00
* \param[in] fp The file pointer on which report information will be written
* \param[in] level The level of report detail desired
*
* After printing device-specific information calls asynMotorAxis::report()
2012-03-02 00:12:33 +00:00
*/
void MD90Axis::report(FILE *fp, int level)
2012-03-02 00:12:33 +00:00
{
if (level > 0) {
fprintf(fp, " axis %d\n",
axisNo_);
}
// Call the base class method
asynMotorAxis::report(fp, level);
}
asynStatus MD90Axis::sendAccelAndVelocity(double acceleration, double velocity)
2012-03-02 00:12:33 +00:00
{
asynStatus status;
int ival;
// static const char *functionName = "MD90::sendAccelAndVelocity";
2012-03-02 00:12:33 +00:00
// Send the velocity
ival = NINT(fabs(115200./velocity));
if (ival < 2) ival=2;
if (ival > 255) ival = 255;
sprintf(pC_->outString_, "#%02dV=%d", axisNo_, ival);
status = pC_->writeReadController();
2012-03-02 00:12:33 +00:00
// Send the acceleration
// acceleration is in steps/sec/sec
// MD-90 is programmed with Ramp Index (R) where:
2012-03-02 00:12:33 +00:00
// dval (steps/sec/sec) = 720,000/(256-R) */
// or R=256-(720,000/dval) */
ival = NINT(256-(720000./acceleration));
if (ival < 1) ival=1;
if (ival > 255) ival=255;
sprintf(pC_->outString_, "#%02dR=%d", axisNo_, ival);
status = pC_->writeReadController();
2012-03-02 00:12:33 +00:00
return status;
}
asynStatus MD90Axis::move(double position, int relative, double minVelocity, double maxVelocity, double acceleration)
2012-03-02 00:12:33 +00:00
{
asynStatus status;
// static const char *functionName = "MD90Axis::move";
2012-03-02 00:12:33 +00:00
status = sendAccelAndVelocity(acceleration, maxVelocity);
if (relative) {
sprintf(pC_->outString_, "#%02dI%+d", axisNo_, NINT(position));
} else {
sprintf(pC_->outString_, "#%02dG%+d", axisNo_, NINT(position));
}
status = pC_->writeReadController();
2012-03-02 00:12:33 +00:00
return status;
}
asynStatus MD90Axis::home(double minVelocity, double maxVelocity, double acceleration, int forwards)
2012-03-02 00:12:33 +00:00
{
asynStatus status;
// static const char *functionName = "MD90Axis::home";
2012-03-02 00:12:33 +00:00
status = sendAccelAndVelocity(acceleration, maxVelocity);
if (forwards) {
sprintf(pC_->outString_, "#%02dH+", axisNo_);
} else {
sprintf(pC_->outString_, "#%02dH-", axisNo_);
}
status = pC_->writeReadController();
2012-03-02 00:12:33 +00:00
return status;
}
asynStatus MD90Axis::moveVelocity(double minVelocity, double maxVelocity, double acceleration)
2012-03-02 00:12:33 +00:00
{
asynStatus status;
static const char *functionName = "MD90Axis::moveVelocity";
2012-03-02 00:12:33 +00:00
asynPrint(pasynUser_, ASYN_TRACE_FLOW,
"%s: minVelocity=%f, maxVelocity=%f, acceleration=%f\n",
functionName, minVelocity, maxVelocity, acceleration);
2012-03-02 00:12:33 +00:00
status = sendAccelAndVelocity(acceleration, maxVelocity);
/* MD-90 does not have jog command. Move 1 million steps */
2012-03-02 00:12:33 +00:00
if (maxVelocity > 0.) {
/* This is a positive move in MD90 coordinates */
sprintf(pC_->outString_, "#%02dI+1000000", axisNo_);
2012-03-02 00:12:33 +00:00
} else {
/* This is a negative move in MD90 coordinates */
sprintf(pC_->outString_, "#%02dI-1000000", axisNo_);
2012-03-02 00:12:33 +00:00
}
status = pC_->writeReadController();
2012-03-02 00:12:33 +00:00
return status;
}
asynStatus MD90Axis::stop(double acceleration )
2012-03-02 00:12:33 +00:00
{
asynStatus status;
//static const char *functionName = "MD90Axis::stop";
2012-03-02 00:12:33 +00:00
2024-05-22 16:55:58 -05:00
sprintf(pC_->outString_, "STP");
status = pC_->writeReadController();
2012-03-02 00:12:33 +00:00
return status;
}
asynStatus MD90Axis::setPosition(double position)
2012-03-02 00:12:33 +00:00
{
asynStatus status;
//static const char *functionName = "MD90Axis::setPosition";
2012-03-02 00:12:33 +00:00
sprintf(pC_->outString_, "#%02dP=%+d", axisNo_, NINT(position));
status = pC_->writeReadController();
2012-03-02 00:12:33 +00:00
return status;
}
asynStatus MD90Axis::setClosedLoop(bool closedLoop)
2012-03-02 00:12:33 +00:00
{
asynStatus status;
//static const char *functionName = "MD90Axis::setClosedLoop";
2012-03-02 00:12:33 +00:00
sprintf(pC_->outString_, "#%02dW=%d", axisNo_, closedLoop ? 1:0);
status = pC_->writeReadController();
2012-03-02 00:12:33 +00:00
return status;
}
/** Polls the axis.
* This function reads the motor position, the limit status, the home status, the moving status,
* and the drive power-on status.
2012-03-02 00:12:33 +00:00
* It calls setIntegerParam() and setDoubleParam() for each item that it polls,
* and then calls callParamCallbacks() at the end.
* \param[out] moving A flag that is set indicating that the axis is moving (true) or done (false). */
asynStatus MD90Axis::poll(bool *moving)
2012-03-02 00:12:33 +00:00
{
2024-05-23 14:05:25 -05:00
int replyStatus;
char replyString[256];
double replyValue;
2012-03-02 00:12:33 +00:00
int done;
int driveOn;
int limit;
double position;
asynStatus comStatus;
// Read the current motor position
2024-05-23 14:05:25 -05:00
sprintf(pC_->outString_, "GEE");
2012-03-02 00:12:33 +00:00
comStatus = pC_->writeReadController();
if (comStatus) goto skip;
2024-05-23 14:05:25 -05:00
// The response string is of the form "0: Current position in nanometers: 1000"
sscanf (pC_->inString_, "%d: %[^:]: %lf", &replyStatus, replyString, &replyValue);
position = replyValue;
2012-03-02 00:12:33 +00:00
setDoubleParam(pC_->motorPosition_, position);
// Read the moving status of this motor
sprintf(pC_->outString_, "#%02dX", axisNo_);
comStatus = pC_->writeReadController();
if (comStatus) goto skip;
// The response string is of the form "#01X=1"
done = (pC_->inString_[5] == '0') ? 1:0;
setIntegerParam(pC_->motorStatusDone_, done);
*moving = done ? false:true;
// Read the limit status
sprintf(pC_->outString_, "#%02dE", axisNo_);
comStatus = pC_->writeReadController();
if (comStatus) goto skip;
// The response string is of the form "#01E=1"
limit = (pC_->inString_[5] == '1') ? 1:0;
setIntegerParam(pC_->motorStatusHighLimit_, limit);
limit = (pC_->inString_[6] == '1') ? 1:0;
setIntegerParam(pC_->motorStatusLowLimit_, limit);
limit = (pC_->inString_[7] == '1') ? 1:0;
setIntegerParam(pC_->motorStatusAtHome_, limit);
// Read the drive power on status
sprintf(pC_->outString_, "#%02dW", axisNo_);
2012-03-02 00:12:33 +00:00
comStatus = pC_->writeReadController();
if (comStatus) goto skip;
driveOn = (pC_->inString_[5] == '1') ? 1:0;
setIntegerParam(pC_->motorStatusPowerOn_, driveOn);
setIntegerParam(pC_->motorStatusProblem_, 0);
skip:
setIntegerParam(pC_->motorStatusProblem_, comStatus ? 1:0);
callParamCallbacks();
return comStatus ? asynError : asynSuccess;
}
/** Code for iocsh registration */
static const iocshArg MD90CreateControllerArg0 = {"Port name", iocshArgString};
static const iocshArg MD90CreateControllerArg1 = {"MD-90 port name", iocshArgString};
static const iocshArg MD90CreateControllerArg2 = {"Number of axes", iocshArgInt};
static const iocshArg MD90CreateControllerArg3 = {"Moving poll period (ms)", iocshArgInt};
static const iocshArg MD90CreateControllerArg4 = {"Idle poll period (ms)", iocshArgInt};
static const iocshArg * const MD90CreateControllerArgs[] = {&MD90CreateControllerArg0,
&MD90CreateControllerArg1,
&MD90CreateControllerArg2,
&MD90CreateControllerArg3,
&MD90CreateControllerArg4};
static const iocshFuncDef MD90CreateControllerDef = {"MD90CreateController", 5, MD90CreateControllerArgs};
static void MD90CreateContollerCallFunc(const iocshArgBuf *args)
2012-03-02 00:12:33 +00:00
{
MD90CreateController(args[0].sval, args[1].sval, args[2].ival, args[3].ival, args[4].ival);
2012-03-02 00:12:33 +00:00
}
static void MD90Register(void)
2012-03-02 00:12:33 +00:00
{
iocshRegister(&MD90CreateControllerDef, MD90CreateContollerCallFunc);
2012-03-02 00:12:33 +00:00
}
extern "C" {
epicsExportRegistrar(MD90Register);
2012-03-02 00:12:33 +00:00
}