Applied PIMPL by moving members to internal struct sinqAxisImpl

This change makes sinqMotor ready for 1.x releases where ABI stability
and backwards compatibility is guaranteed.
This commit is contained in:
2025-05-22 13:12:09 +02:00
parent 275672aaef
commit 83aa437b6b
4 changed files with 299 additions and 218 deletions

View File

@@ -5,8 +5,12 @@
#include "asynOctetSyncIO.h"
#include "epicsExport.h"
#include "iocsh.h"
#include "msgPrintControl.h"
#include "sinqAxis.h"
#include <deque>
#include <errlog.h>
#include <initHooks.h>
#include <unordered_map>
#include <vector>
/*
@@ -34,6 +38,65 @@ void sinqController::epicsInithookFunction(initHookState iState) {
}
}
struct sinqControllerImpl {
// Number of fast polls which still need to be performed before adaptive
// polling is active again.
int outstandingForcedFastPolls;
// Number of polls forced by wakeupPoller which are still
// Pointer to the port user which is specified by the char array
// `ipPortConfigName` in the constructor
asynUser *pasynOctetSyncIOipPort;
// Message print control
msgPrintControl msgPrintC;
// Internal variables used in the communication timeout frequency watchdog
time_t comTimeoutWindow; // Size of the time window
size_t maxNumberTimeouts; // Maximum acceptable number of events within the
// time window
// Deque holding the timestamps of the individual events
std::deque<time_t> timeoutEvents;
// Communicate a timeout to the user after it has happened this many times
// in a row
int maxSubsequentTimeouts;
bool maxSubsequentTimeoutsExceeded;
/*
These integers are indices to paramLib entries and are populated when the
parameters are created. See the documentation in db/sinqMotor.db.
*/
int motorMessageText;
int motorReset;
int motorEnable;
int motorEnableRBV;
int motorCanDisable;
int motorEnableMovWatchdog;
int motorCanSetSpeed;
int motorLimitsOffset;
int motorForceStop;
int motorConnected;
/*
These parameters are here to write values from the hardware to the EPICS
motor record. Using motorHighLimit_ / motorLowLimit_ does not work:
https://epics.anl.gov/tech-talk/2023/msg00576.php. Therefore, some
additional records are introduced which read from these parameters and write
into the motor record.
*/
int motorVeloFromDriver;
int motorVbasFromDriver;
int motorVmaxFromDriver;
int motorAcclFromDriver;
int motorHighLimitFromDriver;
int motorLowLimitFromDriver;
int adaptivePolling;
int encoderType;
};
#define NUM_SINQMOTOR_DRIVER_PARAMS 18
sinqController::sinqController(const char *portName,
const char *ipPortConfigName, int numAxes,
double movingPollPeriod, double idlePollPeriod,
@@ -48,34 +111,22 @@ sinqController::sinqController(const char *portName,
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
msgPrintControl_() {
1, // autoconnect
0, 0) // Default priority and stack size
{
asynStatus status = asynSuccess;
// Handle to the asynUser of the IP port asyn driver
pasynOctetSyncIOipPort_ = nullptr;
// Initial values for the average timeout mechanism, can be overwritten
// later by a FFI function
comTimeoutWindow_ = 3600; // seconds
// Number of timeouts which may occur before an error is forwarded to the
// user
maxNumberTimeouts_ = 60;
// Queue holding the timeout event timestamps
timeoutEvents_ = {};
// Inform the user after 10 timeouts in a row (default value)
maxSubsequentTimeouts_ = 10;
maxSubsequentTimeoutsExceeded_ = false;
// Store the poll period information. The poller itself will be started
// later (after the IOC is running in epicsInithookFunction)
movingPollPeriod_ = movingPollPeriod;
idlePollPeriod_ = idlePollPeriod;
// The paramLib indices are populated with the calls to createParam
pSinqC_ = std::make_unique<sinqControllerImpl>(
(sinqControllerImpl){.outstandingForcedFastPolls = 0,
.pasynOctetSyncIOipPort = nullptr,
.msgPrintC = msgPrintControl(),
.comTimeoutWindow = 3600,
.maxNumberTimeouts = 60,
.timeoutEvents = {},
.maxSubsequentTimeouts = 10,
.maxSubsequentTimeoutsExceeded = false});
// =========================================================================;
@@ -83,9 +134,9 @@ sinqController::sinqController(const char *portName,
We try to connect to the port via the port name provided by the constructor.
If this fails, the function is terminated via exit.
*/
pasynOctetSyncIO->connect(ipPortConfigName, 0, &pasynOctetSyncIOipPort_,
NULL);
if (status != asynSuccess || pasynOctetSyncIOipPort_ == nullptr) {
pasynOctetSyncIO->connect(ipPortConfigName, 0,
&pSinqC_->pasynOctetSyncIOipPort, NULL);
if (status != asynSuccess || pSinqC_->pasynOctetSyncIOipPort == nullptr) {
errlogPrintf("Controller \"%s\" => %s, line %d:\nFATAL ERROR (cannot "
"connect to MCU controller).\n"
"Terminating IOC",
@@ -98,8 +149,8 @@ sinqController::sinqController(const char *portName,
// MOTOR_MESSAGE_TEXT corresponds to the PV definition inside sinqMotor.db.
// This text is used to forward status messages to NICOS and in turn to the
// user.
status =
createParam("MOTOR_MESSAGE_TEXT", asynParamOctet, &motorMessageText_);
status = createParam("MOTOR_MESSAGE_TEXT", asynParamOctet,
&pSinqC_->motorMessageText);
if (status != asynSuccess) {
asynPrint(this->pasynUserSelf, ASYN_TRACE_ERROR,
"Controller \"%s\" => %s, line %d:\nFATAL ERROR (creating a "
@@ -109,7 +160,7 @@ sinqController::sinqController(const char *portName,
exit(-1);
}
status = createParam("MOTOR_ENABLE", asynParamInt32, &motorEnable_);
status = createParam("MOTOR_ENABLE", asynParamInt32, &pSinqC_->motorEnable);
if (status != asynSuccess) {
asynPrint(this->pasynUserSelf, ASYN_TRACE_ERROR,
"Controller \"%s\" => %s, line %d:\nFATAL ERROR (creating a "
@@ -119,7 +170,7 @@ sinqController::sinqController(const char *portName,
exit(-1);
}
status = createParam("MOTOR_RESET", asynParamInt32, &motorReset_);
status = createParam("MOTOR_RESET", asynParamInt32, &pSinqC_->motorReset);
if (status != asynSuccess) {
asynPrint(this->pasynUserSelf, ASYN_TRACE_ERROR,
"Controller \"%s\" => %s, line %d:\nFATAL ERROR (creating a "
@@ -129,7 +180,8 @@ sinqController::sinqController(const char *portName,
exit(-1);
}
status = createParam("MOTOR_ENABLE_RBV", asynParamInt32, &motorEnableRBV_);
status = createParam("MOTOR_ENABLE_RBV", asynParamInt32,
&pSinqC_->motorEnableRBV);
if (status != asynSuccess) {
asynPrint(this->pasynUserSelf, ASYN_TRACE_ERROR,
"Controller \"%s\" => %s, line %d:\nFATAL ERROR (creating a "
@@ -139,8 +191,8 @@ sinqController::sinqController(const char *portName,
exit(-1);
}
status =
createParam("MOTOR_CAN_DISABLE", asynParamInt32, &motorCanDisable_);
status = createParam("MOTOR_CAN_DISABLE", asynParamInt32,
&pSinqC_->motorCanDisable);
if (status != asynSuccess) {
asynPrint(this->pasynUserSelf, ASYN_TRACE_ERROR,
"Controller \"%s\" => %s, line %d:\nFATAL ERROR (creating a "
@@ -150,8 +202,8 @@ sinqController::sinqController(const char *portName,
exit(-1);
}
status =
createParam("MOTOR_CAN_SET_SPEED", asynParamInt32, &motorCanSetSpeed_);
status = createParam("MOTOR_CAN_SET_SPEED", asynParamInt32,
&pSinqC_->motorCanSetSpeed);
if (status != asynSuccess) {
asynPrint(this->pasynUserSelf, ASYN_TRACE_ERROR,
"Controller \"%s\" => %s, line %d:\nFATAL ERROR (creating a "
@@ -162,7 +214,7 @@ sinqController::sinqController(const char *portName,
}
status = createParam("MOTOR_LIMITS_OFFSET", asynParamFloat64,
&motorLimitsOffset_);
&pSinqC_->motorLimitsOffset);
if (status != asynSuccess) {
asynPrint(this->pasynUserSelf, ASYN_TRACE_ERROR,
"Controller \"%s\" => %s, line %d:\nFATAL ERROR (creating a "
@@ -172,7 +224,8 @@ sinqController::sinqController(const char *portName,
exit(-1);
}
status = createParam("MOTOR_CONNECTED", asynParamInt32, &motorConnected_);
status = createParam("MOTOR_CONNECTED", asynParamInt32,
&pSinqC_->motorConnected);
if (status != asynSuccess) {
asynPrint(this->pasynUserSelf, ASYN_TRACE_ERROR,
"Controller \"%s\" => %s, line %d:\nFATAL ERROR (creating a "
@@ -188,7 +241,7 @@ sinqController::sinqController(const char *portName,
the declaration of motorHighLimitFromDriver_.
*/
status = createParam("MOTOR_HIGH_LIMIT_FROM_DRIVER", asynParamFloat64,
&motorHighLimitFromDriver_);
&pSinqC_->motorHighLimitFromDriver);
if (status != asynSuccess) {
asynPrint(this->pasynUserSelf, ASYN_TRACE_ERROR,
"Controller \"%s\" => %s, line %d:\nFATAL ERROR (creating a "
@@ -199,7 +252,7 @@ sinqController::sinqController(const char *portName,
}
status = createParam("MOTOR_LOW_LIMIT_FROM_DRIVER", asynParamFloat64,
&motorLowLimitFromDriver_);
&pSinqC_->motorLowLimitFromDriver);
if (status != asynSuccess) {
asynPrint(this->pasynUserSelf, ASYN_TRACE_ERROR,
"Controller \"%s\" => %s, line %d:\nFATAL ERROR (creating a "
@@ -210,7 +263,7 @@ sinqController::sinqController(const char *portName,
}
status = createParam("MOTOR_ENABLE_MOV_WATCHDOG", asynParamInt32,
&motorEnableMovWatchdog_);
&pSinqC_->motorEnableMovWatchdog);
if (status != asynSuccess) {
asynPrint(this->pasynUserSelf, ASYN_TRACE_ERROR,
"Controller \"%s\" => %s, line %d:\nFATAL ERROR (creating a "
@@ -221,7 +274,7 @@ sinqController::sinqController(const char *portName,
}
status = createParam("MOTOR_VELO_FROM_DRIVER", asynParamFloat64,
&motorVeloFromDriver_);
&pSinqC_->motorVeloFromDriver);
if (status != asynSuccess) {
asynPrint(this->pasynUserSelf, ASYN_TRACE_ERROR,
"Controller \"%s\" => %s, line %d:\nFATAL ERROR (creating a "
@@ -232,7 +285,7 @@ sinqController::sinqController(const char *portName,
}
status = createParam("MOTOR_VBAS_FROM_DRIVER", asynParamFloat64,
&motorVbasFromDriver_);
&pSinqC_->motorVbasFromDriver);
if (status != asynSuccess) {
asynPrint(this->pasynUserSelf, ASYN_TRACE_ERROR,
"Controller \"%s\" => %s, line %d:\nFATAL ERROR (creating a "
@@ -243,7 +296,7 @@ sinqController::sinqController(const char *portName,
}
status = createParam("MOTOR_VMAX_FROM_DRIVER", asynParamFloat64,
&motorVmaxFromDriver_);
&pSinqC_->motorVmaxFromDriver);
if (status != asynSuccess) {
asynPrint(this->pasynUserSelf, ASYN_TRACE_ERROR,
"Controller \"%s\" => %s, line %d:\nFATAL ERROR (creating a "
@@ -254,7 +307,7 @@ sinqController::sinqController(const char *portName,
}
status = createParam("MOTOR_ACCL_FROM_DRIVER", asynParamFloat64,
&motorAcclFromDriver_);
&pSinqC_->motorAcclFromDriver);
if (status != asynSuccess) {
asynPrint(this->pasynUserSelf, ASYN_TRACE_ERROR,
"Controller \"%s\" => %s, line %d:\nFATAL ERROR (creating a "
@@ -264,7 +317,8 @@ sinqController::sinqController(const char *portName,
exit(-1);
}
status = createParam("ADAPTIVE_POLLING", asynParamInt32, &adaptivePolling_);
status = createParam("ADAPTIVE_POLLING", asynParamInt32,
&pSinqC_->adaptivePolling);
if (status != asynSuccess) {
asynPrint(this->pasynUserSelf, ASYN_TRACE_ERROR,
"Controller \"%s\" => %s, line %d:\nFATAL ERROR (creating a "
@@ -274,7 +328,7 @@ sinqController::sinqController(const char *portName,
exit(-1);
}
status = createParam("ENCODER_TYPE", asynParamOctet, &encoderType_);
status = createParam("ENCODER_TYPE", asynParamOctet, &pSinqC_->encoderType);
if (status != asynSuccess) {
asynPrint(this->pasynUserSelf, ASYN_TRACE_ERROR,
"Controller \"%s\" => %s, line %d:\nFATAL ERROR (creating a "
@@ -284,7 +338,8 @@ sinqController::sinqController(const char *portName,
exit(-1);
}
status = createParam("MOTOR_FORCE_STOP", asynParamInt32, &motorForceStop_);
status = createParam("MOTOR_FORCE_STOP", asynParamInt32,
&pSinqC_->motorForceStop);
if (status != asynSuccess) {
asynPrint(this->pasynUserSelf, ASYN_TRACE_ERROR,
"Controller \"%s\" => %s, line %d:\nFATAL ERROR (creating a "
@@ -351,11 +406,11 @@ asynStatus sinqController::writeInt32(asynUser *pasynUser, epicsInt32 value) {
}
// Handle custom PVs
if (function == motorEnable_) {
if (function == motorEnable()) {
return axis->enable(value != 0);
} else if (function == motorReset_) {
} else if (function == motorReset()) {
return axis->reset();
} else if (function == motorForceStop_) {
} else if (function == motorForceStop()) {
return axis->stop(0.0);
} else {
return asynMotorController::writeInt32(pasynUser, value);
@@ -373,10 +428,10 @@ asynStatus sinqController::readInt32(asynUser *pasynUser, epicsInt32 *value) {
return asynError;
}
if (pasynUser->reason == motorEnableRBV_) {
if (pasynUser->reason == motorEnableRBV()) {
getAxisParamChecked(axis, motorEnableRBV, value);
return asynSuccess;
} else if (pasynUser->reason == motorCanDisable_) {
} else if (pasynUser->reason == motorCanDisable()) {
getAxisParamChecked(axis, motorCanDisable, value);
return asynSuccess;
} else {
@@ -389,7 +444,7 @@ asynStatus sinqController::couldNotParseResponse(const char *command,
int axisNo,
const char *functionName,
int line) {
asynPrint(pasynOctetSyncIOipPort_, ASYN_TRACE_ERROR,
asynPrint(pasynOctetSyncIOipPort(), ASYN_TRACE_ERROR,
"Controller \"%s\", axis %d => %s, line %d:\nCould not interpret "
"response \"%s\" for command \"%s\".\n",
portName, axisNo, functionName, line, response, command);
@@ -418,7 +473,7 @@ asynStatus sinqController::paramLibAccessFailed(asynStatus status,
int line) {
if (status != asynSuccess) {
asynPrint(pasynOctetSyncIOipPort_, ASYN_TRACE_ERROR,
asynPrint(pasynOctetSyncIOipPort(), ASYN_TRACE_ERROR,
"Controller \"%s\", axis %d => %s, line %d:\n Accessing the "
"parameter library failed for parameter %s with error %s.\n",
portName, axisNo, functionName, line, parameter,
@@ -440,40 +495,42 @@ asynStatus sinqController::checkComTimeoutWatchdog(int axisNo,
asynStatus paramLibStatus = asynSuccess;
// Add a new timeout event to the queue
timeoutEvents_.push_back(time(NULL));
pSinqC_->timeoutEvents.push_back(time(NULL));
// Remove every event which is older than the time window from the deque
while (1) {
if (timeoutEvents_.empty()) {
if (pSinqC_->timeoutEvents.empty()) {
break;
}
if (timeoutEvents_[0] + comTimeoutWindow_ <= time(NULL)) {
timeoutEvents_.pop_front();
if (pSinqC_->timeoutEvents[0] + pSinqC_->comTimeoutWindow <=
time(NULL)) {
pSinqC_->timeoutEvents.pop_front();
} else {
break;
}
}
// Check if the maximum allowed number of events has been exceeded
bool wantToPrint = timeoutEvents_.size() > maxNumberTimeouts_;
bool wantToPrint =
pSinqC_->timeoutEvents.size() > pSinqC_->maxNumberTimeouts;
if (msgPrintControl_.shouldBePrinted(portName, axisNo, __PRETTY_FUNCTION__,
__LINE__, wantToPrint,
pasynUserSelf)) {
if (pSinqC_->msgPrintC.shouldBePrinted(portName, axisNo,
__PRETTY_FUNCTION__, __LINE__,
wantToPrint, pasynUserSelf)) {
asynPrint(this->pasynUserSelf, ASYN_TRACE_ERROR,
"Controller \"%s\", axis %d => %s, line %d:\nMore than %ld "
"communication timeouts in %ld "
"seconds.%s\n",
portName, axisNo, __PRETTY_FUNCTION__, __LINE__,
maxNumberTimeouts_, comTimeoutWindow_,
msgPrintControl_.getSuffix());
pSinqC_->maxNumberTimeouts, pSinqC_->comTimeoutWindow,
pSinqC_->msgPrintC.getSuffix());
}
if (wantToPrint) {
snprintf(motorMessage, motorMessageSize,
"More than %ld communication timeouts in %ld seconds. Please "
"call the support.",
maxNumberTimeouts_, comTimeoutWindow_);
pSinqC_->maxNumberTimeouts, pSinqC_->comTimeoutWindow);
paramLibStatus = setIntegerParam(motorStatusCommsError_, 1);
if (paramLibStatus != asynSuccess) {
@@ -505,8 +562,8 @@ asynStatus sinqController::checkMaxSubsequentTimeouts(int timeoutNo, int axisNo,
size_t motorMessageSize) {
asynStatus paramLibStatus = asynSuccess;
if (timeoutNo >= maxSubsequentTimeouts_) {
if (!maxSubsequentTimeoutsExceeded_) {
if (timeoutNo >= pSinqC_->maxSubsequentTimeouts) {
if (!pSinqC_->maxSubsequentTimeoutsExceeded) {
snprintf(motorMessage, motorMessageSize,
"Communication timeout between IOC and motor controller. "
"Trying to reconnect ...");
@@ -516,7 +573,7 @@ asynStatus sinqController::checkMaxSubsequentTimeouts(int timeoutNo, int axisNo,
"subsequent communication timeouts. Check whether the "
"controller is still running and connected to the network.\n",
this->portName, axisNo, __PRETTY_FUNCTION__, __LINE__,
maxSubsequentTimeouts_);
pSinqC_->maxSubsequentTimeouts);
paramLibStatus = setIntegerParam(motorStatusCommsError_, 1);
if (paramLibStatus != asynSuccess) {
@@ -524,12 +581,12 @@ asynStatus sinqController::checkMaxSubsequentTimeouts(int timeoutNo, int axisNo,
"motorStatusCommsError_", axisNo,
__PRETTY_FUNCTION__, __LINE__);
}
maxSubsequentTimeoutsExceeded_ = true;
pSinqC_->maxSubsequentTimeoutsExceeded = true;
}
return asynError;
} else {
maxSubsequentTimeoutsExceeded_ = false;
pSinqC_->maxSubsequentTimeoutsExceeded = false;
motorMessage[0] = '\0';
return asynSuccess;
}
@@ -551,8 +608,8 @@ asynStatus sinqController::checkMaxSubsequentTimeouts(int timeoutNo,
asynStatus sinqController::poll() {
// Decrement the number of outstanding forced fast polls, if they are not
// zero
if (outstandingForcedFastPolls_ > 0) {
outstandingForcedFastPolls_--;
if (pSinqC_->outstandingForcedFastPolls > 0) {
pSinqC_->outstandingForcedFastPolls--;
}
return asynMotorController::poll();
}
@@ -560,10 +617,82 @@ asynStatus sinqController::poll() {
asynStatus sinqController::wakeupPoller() {
// + 1 since outstandingForcedFastPolls_ is reduced once at the start of
// a poll cycle
outstandingForcedFastPolls_ = forcedFastPolls_ + 1;
pSinqC_->outstandingForcedFastPolls = forcedFastPolls_ + 1;
return asynMotorController::wakeupPoller();
}
asynStatus sinqController::setMaxSubsequentTimeouts(int maxSubsequentTimeouts) {
pSinqC_->maxSubsequentTimeouts = maxSubsequentTimeouts;
return asynSuccess;
}
/**
* @brief Get a reference to the map used to control the maximum number of
* message repetitions. See the documentation of `printRepetitionWatchdog`
* in msgPrintControl.h for details.
*/
msgPrintControl &sinqController::getMsgPrintControl() {
return pSinqC_->msgPrintC;
}
/**
* @brief Read the number of outstanding forced fast polls currently
* specified
*
*/
int sinqController::outstandingForcedFastPolls() {
return pSinqC_->outstandingForcedFastPolls;
}
/**
* @brief Return a pointer to the low-level octet (string) IP Port
*
* @return asynUser*
*/
asynUser *sinqController::pasynOctetSyncIOipPort() {
return pSinqC_->pasynOctetSyncIOipPort;
}
asynStatus sinqController::setThresholdComTimeout(time_t comTimeoutWindow,
size_t maxNumberTimeouts) {
pSinqC_->comTimeoutWindow = comTimeoutWindow;
pSinqC_->maxNumberTimeouts = maxNumberTimeouts;
return asynSuccess;
}
int sinqController::motorMessageText() { return pSinqC_->motorMessageText; }
int sinqController::motorReset() { return pSinqC_->motorReset; }
int sinqController::motorEnable() { return pSinqC_->motorEnable; }
int sinqController::motorEnableRBV() { return pSinqC_->motorEnableRBV; }
int sinqController::motorCanDisable() { return pSinqC_->motorCanDisable; }
int sinqController::motorEnableMovWatchdog() {
return pSinqC_->motorEnableMovWatchdog;
}
int sinqController::motorCanSetSpeed() { return pSinqC_->motorCanSetSpeed; }
int sinqController::motorLimitsOffset() { return pSinqC_->motorLimitsOffset; }
int sinqController::motorForceStop() { return pSinqC_->motorForceStop; }
int sinqController::motorConnected() { return pSinqC_->motorConnected; }
int sinqController::motorVeloFromDriver() {
return pSinqC_->motorVeloFromDriver;
}
int sinqController::motorVbasFromDriver() {
return pSinqC_->motorVbasFromDriver;
}
int sinqController::motorVmaxFromDriver() {
return pSinqC_->motorVmaxFromDriver;
}
int sinqController::motorAcclFromDriver() {
return pSinqC_->motorAcclFromDriver;
}
int sinqController::motorHighLimitFromDriver() {
return pSinqC_->motorHighLimitFromDriver;
}
int sinqController::motorLowLimitFromDriver() {
return pSinqC_->motorLowLimitFromDriver;
}
int sinqController::adaptivePolling() { return pSinqC_->adaptivePolling; }
int sinqController::encoderType() { return pSinqC_->encoderType; }
// Static pointers (valid for the entire lifetime of the IOC). The number behind
// the strings gives the integer number of each variant (see also method
// stringifyAsynStatus)