Add SDO and PDO iocsh commands

This commit is contained in:
Anders Sandstrom
2021-03-09 13:47:02 +01:00
parent 02d9ef3244
commit 399023e023
9 changed files with 462 additions and 55 deletions

View File

@@ -1,6 +1,62 @@
{
"files.associations": {
"typeindex": "cpp",
"typeinfo": "cpp"
"typeinfo": "cpp",
"stdexcept": "cpp",
"cctype": "cpp",
"clocale": "cpp",
"cmath": "cpp",
"csetjmp": "cpp",
"csignal": "cpp",
"cstdarg": "cpp",
"cstddef": "cpp",
"cstdio": "cpp",
"cstdlib": "cpp",
"cstring": "cpp",
"ctime": "cpp",
"cwchar": "cpp",
"cwctype": "cpp",
"array": "cpp",
"atomic": "cpp",
"strstream": "cpp",
"*.tcc": "cpp",
"bitset": "cpp",
"cfenv": "cpp",
"chrono": "cpp",
"cinttypes": "cpp",
"complex": "cpp",
"condition_variable": "cpp",
"cstdint": "cpp",
"deque": "cpp",
"forward_list": "cpp",
"list": "cpp",
"unordered_map": "cpp",
"unordered_set": "cpp",
"vector": "cpp",
"exception": "cpp",
"fstream": "cpp",
"functional": "cpp",
"future": "cpp",
"initializer_list": "cpp",
"iomanip": "cpp",
"iosfwd": "cpp",
"iostream": "cpp",
"istream": "cpp",
"limits": "cpp",
"mutex": "cpp",
"new": "cpp",
"ostream": "cpp",
"numeric": "cpp",
"ratio": "cpp",
"scoped_allocator": "cpp",
"sstream": "cpp",
"streambuf": "cpp",
"system_error": "cpp",
"thread": "cpp",
"regex": "cpp",
"tuple": "cpp",
"type_traits": "cpp",
"utility": "cpp",
"valarray": "cpp"
}
}

View File

@@ -162,3 +162,8 @@ int ecmcCANOpenDevice::addSDO(uint32_t cobIdTx, // 0x580 + CobId
sdoCounter_++;
return 0;
}
uint32_t ecmcCANOpenDevice::getNodeId() {
return nodeId_;
}

View File

@@ -58,6 +58,7 @@ class ecmcCANOpenDevice {
uint32_t ODSize,
int readSampleTimeMs,
const char* name);
uint32_t getNodeId();
protected:
int validateFrame(can_frame *frame);
ecmcSocketCANWriteBuffer *writeBuffer_;

View File

@@ -33,7 +33,7 @@ ecmcCANOpenMaster::ecmcCANOpenMaster(ecmcSocketCANWriteBuffer* writeBuffer,
syncPdo_ = NULL;
heartPdo_ = NULL;
isMaster_ = true;
int errorCode = 0;
// lssPdo_ = new ecmcCANOpenPDO( writeBuffer_, 0x7E5,DIR_WRITE,0,0,1000,exeSampleTimeMs_,"lss", cfgDbgMode_);
@@ -51,12 +51,12 @@ ecmcCANOpenMaster::ecmcCANOpenMaster(ecmcSocketCANWriteBuffer* writeBuffer,
// Test sync signal
// can0 0x80 [0]
// syncPdo_ = new ecmcCANOpenPDO( writeBuffer_, 0x80,DIR_WRITE,0,0,1000,exeSampleTimeMs_,"sync", cfgDbgMode_);
errorCode = addPDO(0x80, // uint32_t cobId,
errorCode = addPDO(0x80, // uint32_t cobId,
DIR_WRITE, // ecmc_can_direction rw,
0, // uint32_t ODSize,
0, // int readTimeoutMs,
1000, // int writeCycleMs, if < 0 then write on demand.
"sync"); // const char* name);
"sync"); // const char* name);
if(errorCode) {
throw std::runtime_error( "Sync PDO NULL.");

View File

@@ -56,22 +56,27 @@ ecmcSocketCAN::ecmcSocketCAN(char* configStr,
char* portName,
int exeSampleTimeMs) {
// Init
cfgCanIFStr_ = NULL;
cfgDbgMode_ = 0;
cfgCanIFStr_ = NULL;
cfgDbgMode_ = 0;
cfgAutoConnect_ = 1;
destructs_ = 0;
socketId_ = -1;
connected_ = 0;
writeBuffer_ = NULL;
//testSdo_ = NULL;
//testPdo_ = NULL;
//lssPdo_ = NULL;
//syncPdo_ = NULL;
//heartPdo_ = NULL;
destructs_ = 0;
socketId_ = -1;
connected_ = 0;
writeBuffer_ = NULL;
//testSdo_ = NULL;
//testPdo_ = NULL;
//lssPdo_ = NULL;
//syncPdo_ = NULL;
//heartPdo_ = NULL;
//basicConfSdo_ = NULL;
testDevice_ = NULL;
testMaster_ = NULL;
cycleCounter_ = 0;
testDevice_ = NULL;
testMaster_ = NULL;
cycleCounter_ = 0;
deviceCounter_ = 0;
masterDev_ = NULL;
for(int i = 0; i<ECMC_CAN_MAX_DEVICES;i++) {
devices_[i] = NULL;
}
exeSampleTimeMs_ = exeSampleTimeMs;
@@ -161,6 +166,11 @@ ecmcSocketCAN::~ecmcSocketCAN() {
destructs_ = 1; // maybe need todo in other way..
doWriteEvent_.signal();
doConnectEvent_.signal();
for(int i = 0; i<ECMC_CAN_MAX_DEVICES;i++) {
delete devices_[i];
}
}
void ecmcSocketCAN::parseConfigStr(char *configStr) {
@@ -268,6 +278,11 @@ void ecmcSocketCAN::doReadWorker() {
// heartPdo_->newRxFrame(&rxmsg_);
// }
// forward all data to devices (also master)
for(int i = 0; i < deviceCounter_; i++){
devices_[i]->newRxFrame(&rxmsg_);
}
if(testDevice_) {
testDevice_->newRxFrame(&rxmsg_);
}
@@ -371,6 +386,10 @@ void ecmcSocketCAN::execute() {
testDevice_->execute();
}
for(int i = 0; i < deviceCounter_; i++){
devices_[i]->execute();
}
// cycleCounter_++;
// if(basicConfSdo_) {
// basicConfSdo_->execute();
@@ -614,3 +633,97 @@ std::string ecmcSocketCAN::to_string(int value) {
// return asynSuccess;
//}
//
void ecmcSocketCAN::addMaster(uint32_t nodeId,
const char* name) {
if(masterDev_) {
throw std::runtime_error("Master already added.");
}
if(deviceCounter_ >= ECMC_CAN_MAX_DEVICES) {
throw std::out_of_range("Device array full.");
}
if(nodeId >= 128) {
throw std::out_of_range("Node id out of range.");
}
masterDev_ = new ecmcCANOpenMaster(writeBuffer_,nodeId,exeSampleTimeMs_,name, cfgDbgMode_);
// add as a normal device also for execute and rxframe
devices_[deviceCounter_] = masterDev_;
deviceCounter_++;
}
void ecmcSocketCAN::addDevice(uint32_t nodeId,
const char* name){
if(deviceCounter_ >= ECMC_CAN_MAX_DEVICES) {
throw std::out_of_range("Device array full.");
}
if(nodeId >= 128) {
throw std::out_of_range("Node id out of range.");
}
devices_[deviceCounter_] = new ecmcCANOpenDevice(writeBuffer_,nodeId,exeSampleTimeMs_,name, cfgDbgMode_);
deviceCounter_++;
}
int ecmcSocketCAN::findDeviceWithNodeId(uint32_t nodeId) {
for(int i=0; i < deviceCounter_;i++) {
if(devices_[i]) {
if(devices_[i]->getNodeId() == nodeId) {
return i;
}
}
}
return -1;
}
void ecmcSocketCAN::addPDO(uint32_t nodeId,
uint32_t cobId,
ecmc_can_direction rw,
uint32_t ODSize,
int readTimeoutMs,
int writeCycleMs, //if <0 then write on demand.
const char* name) {
int devId = findDeviceWithNodeId(nodeId);
if(devId < 0) {
throw std::out_of_range("Node id not found in any configured device.");
}
int errorCode = devices_[devId]->addPDO(cobId,
rw,
ODSize,
readTimeoutMs,
writeCycleMs,
name);
if(errorCode > 0) {
throw std::runtime_error("AddPDO() failed.");
}
}
void ecmcSocketCAN::addSDO(uint32_t nodeId,
uint32_t cobIdTx, // 0x580 + CobId
uint32_t cobIdRx, // 0x600 + Cobid
ecmc_can_direction rw,
uint16_t ODIndex, // Object dictionary index
uint8_t ODSubIndex, // Object dictionary subindex
uint32_t ODSize,
int readSampleTimeMs,
const char* name) {
int devId = findDeviceWithNodeId(nodeId);
if(devId < 0) {
throw std::out_of_range("Node id not found in any configured device.");
}
int errorCode = devices_[devId]->addSDO(cobIdTx,
cobIdRx,
rw,
ODIndex,
ODSubIndex,
ODSize,
readSampleTimeMs,
name);
if(errorCode > 0) {
throw std::runtime_error("AddSDO() failed.");
}
}

View File

@@ -36,6 +36,7 @@
#include <linux/can/raw.h>
#define ECMC_CAN_MAX_WRITE_CMDS 128
#define ECMC_CAN_MAX_DEVICES 128
#define ECMC_CAN_ERROR_WRITE_FULL 10
#define ECMC_CAN_ERROR_WRITE_BUSY 11
#define ECMC_CAN_ERROR_WRITE_NO_DATA 12
@@ -80,6 +81,32 @@ class ecmcSocketCAN {
uint8_t data7);
int getlastWritesError();
void execute(); // ecmc rt loop
void addMaster(uint32_t nodeId,
const char* name);
void addDevice(uint32_t nodeId,
const char* name);
void addPDO(uint32_t nodeId,
uint32_t cobId,
ecmc_can_direction rw,
uint32_t ODSize,
int readTimeoutMs,
int writeCycleMs, //if <0 then write on demand.
const char* name);
void addSDO(uint32_t nodeId,
uint32_t cobIdTx, // 0x580 + CobId
uint32_t cobIdRx, // 0x600 + Cobid
ecmc_can_direction rw,
uint16_t ODIndex, // Object dictionary index
uint8_t ODSubIndex, // Object dictionary subindex
uint32_t ODSize,
int readSampleTimeMs,
const char* name);
int findDeviceWithNodeId(uint32_t nodeId);
private:
void parseConfigStr(char *configStr);
@@ -104,7 +131,9 @@ class ecmcSocketCAN {
int lastWriteSumError_;
int exeSampleTimeMs_;
int deviceCounter_;
ecmcSocketCANWriteBuffer *writeBuffer_;
//ecmcCANOpenSDO *testSdo_;
//ecmcCANOpenPDO *testPdo_;
//ecmcCANOpenPDO *lssPdo_;
@@ -113,7 +142,10 @@ class ecmcSocketCAN {
//ecmcCANOpenSDO *basicConfSdo_;
ecmcCANOpenDevice *testDevice_;
ecmcCANOpenDevice *testMaster_;
ecmcCANOpenMaster *testMaster_;
ecmcCANOpenDevice *devices_[ECMC_CAN_MAX_DEVICES];
ecmcCANOpenMaster *masterDev_;
int cycleCounter_;
};

View File

@@ -19,8 +19,8 @@
#define ECMC_PLUGIN_CONNECT_OPTION_CMD "CONNECT="
enum ecmc_can_direction {
DIR_READ,
DIR_WRITE };
DIR_WRITE = 1,
DIR_READ = 2 };
enum ecmc_read_states {
READ_IDLE,

View File

@@ -152,7 +152,7 @@ void deleteSocketCAN() {
void ecmcCANOpenAddMasterPrintHelp() {
printf("\n");
printf(" Use \"ecmcCANOpenAddMaster(<name>, <node id>\n");
printf(" Use ecmcCANOpenAddMaster(<name>, <node id>)\n");
printf(" <name> : Name of master device.\n");
printf(" <node id> : CANOpen node id of master.\n");
printf("\n");
@@ -170,8 +170,19 @@ int ecmcCANOpenAddMaster(const char* name, int nodeId) {
return asynSuccess;
}
/* CODE HERE*/
if(!can) {
printf("Plugin not initialized/loaded.\n");
return asynError;
}
try {
can->addMaster((uint32_t)nodeId,name);
}
catch(std::exception& e) {
printf("Exception: %s. Add master failed.\n",e.what());
return asynError;
}
return asynSuccess;
}
@@ -194,9 +205,9 @@ static void initCallFunc_0(const iocshArgBuf *args) {
void ecmcCANOpenAddDevicePrintHelp() {
printf("\n");
printf(" Use \"ecmcCANOpenAddDevice(<name>, <node id>\n");
printf(" Use ecmcCANOpenAddDevice(<name>, <node id>)\n");
printf(" <name> : Name of device.\n");
printf(" <node id> : CANOpen node id of master.\n");
printf(" <node id> : CANOpen node id of device.\n");
printf("\n");
}
@@ -212,7 +223,18 @@ int ecmcCANOpenAddDevice(const char* name, int nodeId) {
return asynSuccess;
}
/* CODE HERE*/
if(!can) {
printf("Plugin not initialized/loaded.\n");
return asynError;
}
try {
can->addDevice((uint32_t)nodeId,name);
}
catch(std::exception& e) {
printf("Exception: %s. Add device failed.\n",e.what());
return asynError;
}
return asynSuccess;
}
@@ -236,13 +258,28 @@ static void initCallFunc_1(const iocshArgBuf *args) {
void ecmcCANOpenAddSDOPrintHelp() {
printf("\n");
printf(" Use \"ecmcCANOpenAddSDO(<name>, <node id>\n");
printf(" <name> : Name of master device.\n");
printf(" <node id> : CANOpen node id of master.\n");
printf(" Use ecmcCANOpenAddSDO(<name>, <node id>,.....)\n");
printf(" <name> : Name of master device.\n");
printf(" <node id> : CANOpen node id of device/master.\n");
printf(" <cob id tx> : CANOpen cob id of Tx of slave SDO.\n");
printf(" <cob id rx> : CANOpen cob id of Rx of slave SDO.\n");
printf(" <dir> : Direction 1=write and 2=read.\n");
printf(" <ODIndex> : OD index of SDO.\n");
printf(" <ODSubIndex> : OD sub index of SDO.\n");
printf(" <ODSize> : OS Size.\n");
printf(" <readTimeoutMs> : Readtimeout in ms.\n");
printf("\n");
}
int ecmcCANOpenAddSDO(const char* name, int nodeId) {
int ecmcCANOpenAddSDO(const char* name,
int nodeId,
int cobIdTx,
int cobIdRx,
int dir,
int ODIndex,
int ODSubIndex,
int ODSize,
int readTimeoutMs) {
if(!name) {
printf("Error: name.\n");
ecmcCANOpenAddSDOPrintHelp();
@@ -254,8 +291,71 @@ int ecmcCANOpenAddSDO(const char* name, int nodeId) {
return asynSuccess;
}
/* CODE HERE*/
if(cobIdRx < 0) {
printf("Error: invalid cobIdRx.\n");
ecmcCANOpenAddSDOPrintHelp();
return asynError;
}
if(cobIdTx < 0) {
printf("Error: invalid cobIdTx.\n");
ecmcCANOpenAddSDOPrintHelp();
return asynError;
}
if(dir > 2 || dir <= 0) {
printf("Error: invalid dir.\n");
ecmcCANOpenAddSDOPrintHelp();
return asynError;
}
if(ODIndex < 0) {
printf("Error: invalid ODIndex.\n");
ecmcCANOpenAddSDOPrintHelp();
return asynError;
}
if(ODSubIndex < 0) {
printf("Error: invalid ODSubIndex.\n");
ecmcCANOpenAddSDOPrintHelp();
return asynError;
}
if(ODSize < 0) {
printf("Error: invalid ODSize.\n");
ecmcCANOpenAddSDOPrintHelp();
return asynError;
}
if(readTimeoutMs < 0) {
printf("Error: invalid readTimeoutMs.\n");
ecmcCANOpenAddSDOPrintHelp();
return asynError;
}
ecmc_can_direction tempDir = DIR_READ;
if(dir == 1) {
tempDir = DIR_WRITE;
}
try {
can->addSDO((uint32_t)nodeId,
cobIdTx,
cobIdRx,
tempDir,
ODIndex,
ODSubIndex,
ODSize,
readTimeoutMs,
name);
}
catch(std::exception& e) {
printf("Exception: %s. Add PDO failed.\n",e.what());
return asynError;
}
return asynSuccess;
}
@@ -263,28 +363,67 @@ static const iocshArg initArg0_2 =
{ "Name", iocshArgString };
static const iocshArg initArg1_2 =
{ "Node Id", iocshArgInt };
static const iocshArg initArg2_2 =
{ "COB id TX", iocshArgInt };
static const iocshArg initArg3_2 =
{ "COB id RX", iocshArgInt };
static const iocshArg initArg4_2 =
{ "Direction", iocshArgInt };
static const iocshArg initArg5_2 =
{ "OD Index", iocshArgInt };
static const iocshArg initArg6_2 =
{ "OD sub index", iocshArgInt };
static const iocshArg initArg7_2 =
{ "OD size", iocshArgInt };
static const iocshArg initArg8_2 =
{ "Read timeout ms", iocshArgInt };
static const iocshArg *const initArgs_2[] = { &initArg0_2,
&initArg1_2};
&initArg1_2,
&initArg2_2,
&initArg3_2,
&initArg4_2,
&initArg5_2,
&initArg6_2,
&initArg7_2,
&initArg8_2};
static const iocshFuncDef initFuncDef_2 = { "ecmcCANOpenAddSDO", 2, initArgs_2 };
static const iocshFuncDef initFuncDef_2 = { "ecmcCANOpenAddSDO", 9, initArgs_2 };
static void initCallFunc_2(const iocshArgBuf *args) {
ecmcCANOpenAddSDO(args[0].sval, args[1].ival);
ecmcCANOpenAddSDO(args[0].sval,
args[1].ival,
args[2].ival,
args[3].ival,
args[4].ival,
args[5].ival,
args[6].ival,
args[7].ival,
args[8].ival);
}
/**
* EPICS iocsh shell command: ecmcCANOpenAddPDO
*/
void ecmcCANOpenAddPDOPrintHelp() {
printf("\n");
printf(" Use \"ecmcCANOpenAddPDO(<name>, <node id>\n");
printf(" <name> : Name of master device.\n");
printf(" <node id> : CANOpen node id of master.\n");
printf(" Use \"ecmcCANOpenAddSDO(<name>, <node id>\n");
printf(" <name> : Name of master device.\n");
printf(" <node id> : CANOpen node id of device/master.\n");
printf(" <cob id> : CANOpen cob id of PDO.\n");
printf(" <dir> : Direction 1=write and 2=read.\n");
printf(" <ODSize> : Size of PDO (max 8 bytes).\n");
printf(" <readTimeoutMs> : Readtimeout in ms.\n");
printf(" <writeCycleMs> : Cycle time for write (if <= 0 then only write on change).\n");
printf("\n");
}
int ecmcCANOpenAddPDO(const char* name, int nodeId) {
int ecmcCANOpenAddPDO(const char* name,
int nodeId,
int cobId,
int dir,
int ODSize,
int readTimeoutMs,
int writeCycleMs) {
if(!name) {
printf("Error: name.\n");
ecmcCANOpenAddPDOPrintHelp();
@@ -296,8 +435,47 @@ int ecmcCANOpenAddPDO(const char* name, int nodeId) {
return asynSuccess;
}
/* CODE HERE*/
if(dir > 2 || dir <= 0) {
printf("Error: invalid dir.\n");
ecmcCANOpenAddPDOPrintHelp();
return asynError;
}
if(ODSize < 0) {
printf("Error: invalid ODSize.\n");
ecmcCANOpenAddPDOPrintHelp();
return asynError;
}
if(readTimeoutMs < 0) {
printf("Error: invalid readTimeoutMs.\n");
ecmcCANOpenAddPDOPrintHelp();
return asynError;
}
if(writeCycleMs < 0) {
printf("Error: invalid writeCycleMs.\n");
ecmcCANOpenAddPDOPrintHelp();
return asynError;
}
ecmc_can_direction tempDir = DIR_READ;
if(dir == 1) {
tempDir = DIR_WRITE;
}
try {
can->addPDO((uint32_t)nodeId,
cobId,
tempDir,
ODSize,
readTimeoutMs,
writeCycleMs,name);
}
catch(std::exception& e) {
printf("Exception: %s. Add PDO failed.\n",e.what());
return asynError;
}
return asynSuccess;
}
@@ -305,13 +483,35 @@ static const iocshArg initArg0_3 =
{ "Name", iocshArgString };
static const iocshArg initArg1_3 =
{ "Node Id", iocshArgInt };
static const iocshArg initArg2_3 =
{ "COB Id", iocshArgInt };
static const iocshArg initArg3_3 =
{ "Direction", iocshArgInt };
static const iocshArg initArg4_3 =
{ "ODSize", iocshArgInt };
static const iocshArg initArg5_3 =
{ "Read Timeout ms", iocshArgInt };
static const iocshArg initArg6_3 =
{ "Write cycle ms", iocshArgInt };
static const iocshArg *const initArgs_3[] = { &initArg0_3,
&initArg1_3};
&initArg1_3,
&initArg2_3,
&initArg3_3,
&initArg4_3,
&initArg5_3,
&initArg6_3
};
static const iocshFuncDef initFuncDef_3 = { "ecmcCANOpenAddPDO", 2, initArgs_3 };
static const iocshFuncDef initFuncDef_3 = { "ecmcCANOpenAddPDO", 7, initArgs_3 };
static void initCallFunc_3(const iocshArgBuf *args) {
ecmcCANOpenAddPDO(args[0].sval, args[1].ival);
ecmcCANOpenAddPDO(args[0].sval,
args[1].ival,
args[2].ival,
args[3].ival,
args[4].ival,
args[5].ival,
args[6].ival);
}
/**

View File

@@ -1,15 +1,15 @@
IOC_TEST:PLC-0-enable
REQMOD:mcag-trgt-muts--16738:MODULES
REQMOD:mcag-trgt-muts--16738:VERSIONS
REQMOD:mcag-trgt-muts--16738:MOD_VER
REQMOD:mcag-trgt-muts--16738:exit
REQMOD:mcag-trgt-muts--16738:BaseVersion
REQMOD:mcag-trgt-muts--16738:require_VER
REQMOD:mcag-trgt-muts--16738:ecmccfg_VER
REQMOD:mcag-trgt-muts--16738:asyn_VER
REQMOD:mcag-trgt-muts--16738:exprtk_VER
REQMOD:mcag-trgt-muts--16738:motor_VER
REQMOD:mcag-trgt-muts--16738:ecmc_VER
REQMOD:mcag-trgt-muts--16738:ecmc_plugin_socketcan_VER
REQMOD:mcag-trgt-muts--29223:MODULES
REQMOD:mcag-trgt-muts--29223:VERSIONS
REQMOD:mcag-trgt-muts--29223:MOD_VER
REQMOD:mcag-trgt-muts--29223:exit
REQMOD:mcag-trgt-muts--29223:BaseVersion
REQMOD:mcag-trgt-muts--29223:require_VER
REQMOD:mcag-trgt-muts--29223:ecmccfg_VER
REQMOD:mcag-trgt-muts--29223:asyn_VER
REQMOD:mcag-trgt-muts--29223:exprtk_VER
REQMOD:mcag-trgt-muts--29223:motor_VER
REQMOD:mcag-trgt-muts--29223:ecmc_VER
REQMOD:mcag-trgt-muts--29223:ecmc_plugin_socketcan_VER
IOC_TEST:PLC-0-scantime
IOC_TEST:PLC-0-error