Files
motorDSM/dsmApp/src/MD90Driver.cpp

409 lines
14 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);
}
/** Acceleration currently unsupported with MD-90 controller
* \param[in] acceleration The accelerations to ramp up to max velocity
* \param[in] velocity Motor velocity in steps / sec
*/
asynStatus MD90Axis::sendAccelAndVelocity(double acceleration, double velocity)
2012-03-02 00:12:33 +00:00
{
asynStatus status;
int freq;
// static const char *functionName = "MD90::sendAccelAndVelocity";
2012-03-02 00:12:33 +00:00
// Send the velocity
// Velocity provided in steps/sec
// Our unit step size of the encoder is 10 nm, but the motor moves in steps approx. 5 micrometers.
// Motor controller accepts step frequency in Hz.
freq = NINT(fabs(velocity / 500.));
sprintf(pC_->outString_, "SSF %d", freq);
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);
// Position specified in encoder steps (10 nm), but motor move commands are in nanometers
position = position * 10;
2012-03-02 00:12:33 +00:00
if (relative) {
2024-05-24 09:42:53 -05:00
sprintf(pC_->outString_, "CRM %d", NINT(position));
2012-03-02 00:12:33 +00:00
} else {
2024-05-24 09:42:53 -05:00
sprintf(pC_->outString_, "CLM %d", NINT(position));
2012-03-02 00:12:33 +00:00
}
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);
2024-05-24 09:46:58 -05:00
sprintf(pC_->outString_, "HOM");
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);
2024-06-05 14:34:59 -05:00
/* MD-90 does not have jog command. Move max 6000 steps */
sprintf(pC_->outString_, "SNS 6000");
status = pC_->writeReadController();
2012-03-02 00:12:33 +00:00
if (maxVelocity > 0.) {
/* This is a positive move in MD90 coordinates */
2024-06-05 14:34:59 -05:00
sprintf(pC_->outString_, "ESF");
2012-03-02 00:12:33 +00:00
} else {
2024-06-05 14:34:59 -05:00
/* This is a negative move in MD90 coordinates */
sprintf(pC_->outString_, "ESB");
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;
}
/** The ACS driver used this to turn on/off the motor winding current, so
* we'll use this for enabling/disabling the persistent move state.
*/
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
if (closedLoop == 1) {
sprintf(pC_->outString_, "EPM");
} else {
sprintf(pC_->outString_, "DPM");
}
status = pC_->writeReadController();
2012-03-02 00:12:33 +00:00
return status;
}
2024-06-05 16:04:30 -05:00
/** Set the I Gain of the motor control loop. The motor is an I- controller
* and has no P or D terms.
* \param[in] iGain The current I gain in the control loop
*/
2024-06-05 16:04:30 -05:00
asynStatus MD90Axis::setIGain(double iGain)
{
asynStatus status;
//static const char *functionName = "MD90Axis::setIGain";
iGain = iGain * 1000;
2024-06-05 16:04:30 -05:00
if (iGain < 1) iGain = 1.0;
if (iGain > 1000) iGain = 1000.0;
2024-06-05 16:04:30 -05:00
sprintf(pC_->outString_, "SGN %d", NINT(iGain));
status = pC_->writeReadController();
return status;
}
2024-06-05 16:14:40 -05:00
asynStatus MD90Axis::doMoveToHome()
{
asynStatus status;
//static const char *functionName = "MD90Axis::doMoveToHome";
sprintf(pC_->outString_, "CLM 0");
status = pC_->writeReadController();
return status;
}
2012-03-02 00:12:33 +00:00
/** 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];
int replyValue;
2012-03-02 00:12:33 +00:00
int done;
int driveOn;
int homed;
2012-03-02 00:12:33 +00:00
double position;
asynStatus comStatus;
2024-05-23 14:12:04 -05:00
// TODO: Will need to add some more error handling for the motor return codes.
2024-06-21 14:53:50 -05:00
// Read the drive power on status
sprintf(pC_->outString_, "GPS");
2012-03-02 00:12:33 +00:00
comStatus = pC_->writeReadController();
if (comStatus) goto skip;
2024-06-21 14:53:50 -05:00
// The response string is of the form "0: Power supply enabled state: 1"
sscanf (pC_->inString_, "%d: %[^:]: %d", &replyStatus, replyString, &replyValue);
driveOn = (replyValue == '1') ? 1:0;
setIntegerParam(pC_->motorStatusPowerOn_, driveOn);
setIntegerParam(pC_->motorStatusProblem_, 0);
// Read the home status
sprintf(pC_->outString_, "GHS");
comStatus = pC_->writeReadController();
if (comStatus) goto skip;
// The response string is of the form "0: Home status: 1"
sscanf (pC_->inString_, "%d: %[^:]: %d", &replyStatus, replyString, &replyValue);
homed = (replyValue == '1') ? 1:0;
setIntegerParam(pC_->motorStatusHomed_, homed);
2012-03-02 00:12:33 +00:00
// Read the moving status of this motor
2024-05-23 14:12:04 -05:00
sprintf(pC_->outString_, "STA");
2012-03-02 00:12:33 +00:00
comStatus = pC_->writeReadController();
if (comStatus) goto skip;
2024-05-23 14:12:04 -05:00
// The response string is of the form "0: Current status value: 0"
sscanf (pC_->inString_, "%d: %[^:]: %d", &replyStatus, replyString, &replyValue);
2024-05-23 14:12:04 -05:00
done = (replyValue == '2') ? 0:1;
2012-03-02 00:12:33 +00:00
setIntegerParam(pC_->motorStatusDone_, done);
*moving = done ? false:true;
switch(replyValue) {
case 0: // Idle
break;
case 1: // Open loop move complete
break;
case 2: // Move in progress
break;
case 3: // Move stopped
break;
case 4: // Homing error
setIntegerParam(pC_->motorStatusProblem_, 1);
break;
case 5: // Stance error
setIntegerParam(pC_->motorStatusProblem_, 1);
break;
case 6: // Stance complete
break;
case 7: // Open loop move error
setIntegerParam(pC_->motorStatusProblem_, 1);
break;
case 8: // Closed loop move error
setIntegerParam(pC_->motorStatusProblem_, 1);
break;
case 9: // Closed loop move complete
break;
case 10: // End of travel error
setIntegerParam(pC_->motorStatusProblem_, 1);
if (position > 0) {
setIntegerParam(pC_->motorStatusHighLimit_, 1);
} else {
setIntegerParam(pC_->motorStatusLowLimit_, 1);
}
break;
case 11: // Ramp move error
setIntegerParam(pC_->motorStatusProblem_, 1);
break;
default:
break;
}
2012-03-02 00:12:33 +00:00
2024-06-21 14:53:50 -05:00
// Read the current motor position in encoder steps (10 nm)
sprintf(pC_->outString_, "GEC");
2012-03-02 00:12:33 +00:00
comStatus = pC_->writeReadController();
if (comStatus) goto skip;
2024-06-21 14:53:50 -05:00
// The response string is of the form "0: Current position in encoder counts: 1000"
sscanf (pC_->inString_, "%d: %[^:]: %lf", &replyStatus, replyString, &position);
setDoubleParam(pC_->motorPosition_, position);
2012-03-02 00:12:33 +00:00
// Read the current motor integral gain (range 1-1000)
sprintf(pC_->outString_, "GGN");
comStatus = pC_->writeReadController();
if (comStatus) goto skip;
// The response string is of the form "0: Gain: 1000"
sscanf (pC_->inString_, "%d: %[^:]: %d", &replyStatus, replyString, &replyValue);
setDoubleParam(pC_->motorIGain_, replyValue);
2012-03-02 00:12:33 +00:00
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
}