33 : isRecording(false), stopRecordingFlag(false)
40 std::string calibrationFilePath = getProperty<std::string>(
"CalibrationFilePath").getValue();
43 throw LocalException(
"Could not find calibration file '") << calibrationFilePath <<
"'";
45 ARMARX_INFO <<
"reading config " << calibrationFilePath;
49 auto findDaqNode = [&](std::string serialNumber)
53 if (daqNode.attribute_value(
"serialNumber") == serialNumber)
61 offeringTopic(getProperty<std::string>(
"OptoForceTopicName").getValue());
63 OPort* portlist = ports.listPorts(
true);
64 ARMARX_INFO <<
"Found " << ports.getLastSize() <<
" Optoforce device(s).";
67 portlist = ports.listPorts(
true);
68 ARMARX_INFO <<
"Found " << ports.getLastSize() <<
" Optoforce device(s):";
70 for (
int i = 0; i < ports.getLastSize(); i++)
72 std::string deviceName(portlist[i].name);
73 std::string serialNumber(portlist[i].serialNumber);
74 ARMARX_INFO <<
"Found DAQ: deviceName='" << deviceName <<
"', serialNumber='" << serialNumber <<
"'";
78 throw LocalException(
"Could not find config node for device deviceName='") << deviceName <<
"', serialNumber='" << serialNumber <<
"'";
80 DaqWrapperPtr daqPtr(
new DaqWrapper(deviceName, serialNumber, daqNode));
81 daqList.push_back(daqPtr);
84 ARMARX_INFO <<
"number of ports: " << ports.getLastSize();
86 for (
int i = 0; i < ports.getLastSize(); ++i)
88 const DaqWrapperPtr& daq = daqList.at(i);
90 daq->daq.open(portlist[i]);
92 daq->checkSensorCount();
102 topicPrx = getTopic<OptoForceUnitListenerPrx>(getProperty<std::string>(
"OptoForceTopicName").getValue());
106 void OptoForceUnit::run()
111 OptoForceUnitListenerPrx batchPrx = topicPrx->ice_batchOneway();
114 while (readTask->isRunning())
116 bool flushNeeded =
false;
118 for (
const DaqWrapperPtr& daqPtr : daqList)
121 int size = daqPtr->daq.readAll(pa,
false);
132 if (daqPtr->daq.getSize() > 0)
137 int sensorCount = daqPtr->daq.getSensorSize();
139 std::vector<std::array<float, 3>>
data;
140 data.resize(sensorCount);
142 for (
int i = 0; i < sensorCount; i++)
144 if (!daqPtr->enableFlags.at(i))
148 for (
int n = 0; n < size; n++)
150 float countsPerN = daqPtr->countsPerN.at(i);
151 float x = pa[i * size + n].x / countsPerN - daqPtr->offsets.at(i).at(0);
152 float y = pa[i * size + n].y / countsPerN - daqPtr->offsets.at(i).at(1);
153 float z = pa[i * size + n].z / countsPerN - daqPtr->offsets.at(i).at(2);
155 batchPrx->reportSensorValues(daqPtr->deviceName +
":" +
to_string(i), daqPtr->sensorNames.at(i), x, y, z, nowTimestamp);
157 data.at(i) = {x, y, z};
164 recordingFile <<
"{\"timestamp\":\"" << now.toDateTime() <<
"\",";
165 recordingFile <<
"\"daq\":\"" << daqPtr->serialNumber <<
"\",\"data\":[";
167 for (
int i = 0; i < sensorCount; i++)
169 if (!daqPtr->enableFlags.at(i))
175 recordingFile <<
",";
178 recordingFile <<
"[" <<
data.at(i).at(0) <<
"," <<
data.at(i).at(1) <<
"," <<
data.at(i).at(2) <<
"]";
180 recordingFile <<
"]}\n";
181 recordingFile.flush();
186 if (isRecording && stopRecordingFlag)
188 recordingFile.close();
191 stopRecordingFlag =
false;
194 batchPrx->ice_flushBatchRequests();
223 OptoForceUnit::DaqWrapper::DaqWrapper(
const std::string& deviceName,
const std::string& serialNumber,
const RapidXmlReaderNode& daqNode)
224 : deviceName(deviceName), serialNumber(serialNumber)
229 float counts_at_nc = sensorNode.attribute_as_float(
"counts_at_nc");
230 float nominalCapacity = sensorNode.attribute_as_float(
"nominalCapacity");
231 std::string sensorName = sensorNode.attribute_value_or_default(
"name", serialNumber +
"-" +
to_string(i));
232 countsPerN.push_back(counts_at_nc / nominalCapacity);
233 sensorNames.push_back(sensorName);
234 enableFlags.push_back(sensorNode.attribute_as_optional_bool(
"enabled",
"true",
"false",
true));
235 offsets.push_back({sensorNode.attribute_as_float(
"offsetX"), sensorNode.attribute_as_float(
"offsetY"), sensorNode.attribute_as_float(
"offsetZ")});
240 void OptoForceUnit::DaqWrapper::printInfo()
242 SensorConfig config = daq.getConfig();
243 int state = config.getState();
244 int speed = config.getSpeed();
245 int filter = config.getFilter();
246 int mode = config.getMode();
259 void OptoForceUnit::DaqWrapper::checkSensorCount()
261 if ((
int)countsPerN.size() != daq.getSensorSize())
263 throw LocalException(
"Sensor count mismatch. Configured: ") << ((int)countsPerN.size()) <<
" found: " << daq.getSensorSize();
265 ARMARX_INFO_S <<
"Configured: " << ((int)countsPerN.size()) <<
" found: " << daq.getSensorSize() <<
" sensors";
272 recordingFile.open(filepath);
278 stopRecordingFlag =
true;