17 sampleIndexRotation = 0;
18 sampleIndexPressure = 0;
19 sampleIndexAcceleration = 0;
20 sampleIndexPressureRate = 0;
26 if (getProperty<bool>(
"logData").getValue())
29 time_t timer = now.toSeconds();
32 ts = localtime(&timer);
33 strftime(buffer, 80,
"%Y-%m-%d-%H-%M-%S", ts);
34 std::string packageName =
"RobotAPI";
36 std::string dataDir = finder.
getDataDir() +
"/" + packageName +
"/logs/";
37 std::string
filename = dataDir + buffer + std::string(
"_data") +
".json";
41 prefix = std::string(buffer);
43 maxSamplesRotation = stoi(getProperty<std::string>(
"SamplesRotation").getValue());
44 maxSamplesPressure = stoi(getProperty<std::string>(
"SamplesPressure").getValue());
45 maxSamplesAcceleration = stoi(getProperty<std::string>(
"SamplesAcceleration").getValue());
47 std::string topicName = getProperty<std::string>(
"TopicName").getValue();
51 std::string portname = getProperty<std::string>(
"SerialInterfaceDevice").getValue();
52 arduinoIn.open(getProperty<std::string>(
"SerialInterfaceDevice").getValue(), std::ios::in);
53 arduinoOut.open(getProperty<std::string>(
"SerialInterfaceDevice").getValue(), std::ios::out);
55 fd = open(portname.c_str(), O_RDWR | O_NOCTTY);
56 struct termios toptions;
59 tcgetattr(fd, &toptions);
62 cfsetispeed(&toptions, B115200);
63 cfsetospeed(&toptions, B115200);
71 tcsetattr(fd, TCSANOW, &toptions);
77 tcflush(fd, TCIFLUSH);
79 ARMARX_INFO <<
"opening device " << getProperty<std::string>(
"SerialInterfaceDevice").getValue();
81 if (!arduinoIn.is_open())
84 throw LocalException(
"Cannot open Arduino on ") << getProperty<std::string>(
"SerialInterfaceDevice").getValue();
91 std::string arduinoLine;
94 ARMARX_INFO <<
"waiting for IMU calibration - this can take some time";
95 if (getProperty<bool>(
"calibrateSensor").getValue())
99 while (arduinoLine.find(
"mode") == std::string::npos)
101 getline(arduinoIn, arduinoLine,
'\n');
104 arduinoOut <<
"calibrate";
107 while (arduinoLine.find(
"Calibration Sucessfull") == std::string::npos)
109 getline(arduinoIn, arduinoLine,
'\n');
112 getline(arduinoIn, arduinoLine,
'\n');
113 if (getCalibrationValues(arduinoLine))
121 ARMARX_INFO <<
"load calibration data " << getProperty<std::string>(
"CalibrationData").getValue();
122 if (loadCalibration())
142 debugDrawerTopic = getTopic<DebugDrawerInterfacePrx>(getProperty<std::string>(
"DebugDrawerTopicName").getValue());
143 std::string topicName = getProperty<std::string>(
"TopicName").getValue();
144 topicPrx = getTopic<OrientedTactileSensorUnitListenerPrx>(topicName);
153 void OrientedTactileSensorUnit::run()
155 while (readTask->isRunning())
158 getline(arduinoIn, line,
'\n');
164 float rotationRate = 0;
167 if ((pow(
data.qw, 2) + pow(
data.qx, 2) + pow(
data.qy, 2) + pow(
data.qz, 2)) != 0)
169 RotationRate sampleRotation;
170 sampleRotation.timestamp = now;
173 if (0 < maxSamplesRotation && samplesRotation.size() <
static_cast<std::size_t
>(maxSamplesRotation))
175 samplesRotation.push_back(sampleRotation);
179 samplesRotation[sampleIndexRotation].timestamp = sampleRotation.timestamp;
180 samplesRotation[sampleIndexRotation].orientation = sampleRotation.orientation;
181 sampleIndexRotation = (sampleIndexRotation + 1) % maxSamplesRotation;
182 RotationRate oldsampleRotation;
183 oldsampleRotation.timestamp = samplesRotation.at(sampleIndexRotation).timestamp;
184 oldsampleRotation.orientation = samplesRotation.at(sampleIndexRotation).orientation;
185 Eigen::AngleAxisf aa(sampleRotation.orientation * oldsampleRotation.orientation.inverse());
187 rotationRate = aa.angle() / (sampleRotation.timestamp - oldsampleRotation.timestamp).toSecondsDouble();
191 float pressureRate = 0;
192 PressureRate samplePressure;
193 samplePressure.timestamp = now;
194 samplePressure.pressure =
data.pressure;
195 if (0 < maxSamplesPressure && samplesPressure.size() <
static_cast<std::size_t
>(maxSamplesPressure))
197 samplesPressure.push_back(samplePressure);
201 samplesPressure[sampleIndexPressure] = samplePressure;
202 sampleIndexPressure = (sampleIndexPressure + 1) % maxSamplesPressure;
203 PressureRate oldsamplePressure;
204 oldsamplePressure.timestamp = samplesPressure.at(sampleIndexPressure).timestamp;
205 oldsamplePressure.pressure = samplesPressure.at(sampleIndexPressure).pressure;
206 pressureRate = (samplePressure.pressure - oldsamplePressure.pressure) / (samplePressure.timestamp - oldsamplePressure.timestamp).toSecondsDouble();
210 float accelerationRate = 0;
211 AccelerationRate sampleAcceleration;
212 sampleAcceleration.timestamp = now;
213 sampleAcceleration.rotationRate = rotationRate;
214 if (0 < maxSamplesAcceleration && samplesAcceleration.size() <
static_cast<std::size_t
>(maxSamplesAcceleration))
216 samplesAcceleration.push_back(sampleAcceleration);
220 samplesAcceleration[sampleIndexAcceleration] = sampleAcceleration;
221 sampleIndexAcceleration = (sampleIndexAcceleration + 1) % maxSamplesAcceleration;
222 AccelerationRate oldsampleAcceleration;
223 oldsampleAcceleration.timestamp = samplesAcceleration.at(sampleIndexAcceleration).timestamp;
224 oldsampleAcceleration.rotationRate = samplesAcceleration.at(sampleIndexAcceleration).rotationRate;
225 accelerationRate = (sampleAcceleration.rotationRate - oldsampleAcceleration.rotationRate) / (sampleAcceleration.timestamp - oldsampleAcceleration.timestamp).toSecondsDouble();
227 if (0 < maxSamplesPressure && pressureRates.size() <
static_cast<std::size_t
>(maxSamplesPressure))
229 pressureRates.push_back(pressureRate);
233 pressureRates[sampleIndexPressureRate] = pressureRate;
234 sampleIndexPressureRate = (sampleIndexPressureRate + 1) % maxSamplesPressure;
236 if (pressureRate > 50)
242 if (getProperty<bool>(
"logData").
getValue())
247 inverseOrientation = orientationQuaternion.inverse();
253 quat4Matrix.block(0, 0, 3, 3) = quatMatrix;
255 Eigen::Vector3f linearAcceleration(
data.accelx,
data.accely,
data.accelz);
258 e.
Add(
"Pressure",
data.pressure);
259 e.
Add(
"PressureRate", pressureRate);
260 e.
Add(
"RotationRate", rotationRate);
261 e.
AddAsArr(
"Orientation", quat4Matrix);
262 e.
AddAsArr(
"Linear Acceleration", linearAcceleration);
295 Eigen::Matrix3f rawOrientation = orientationQuaternion.toRotationMatrix();
297 PosePtr pose =
new Pose(rawOrientation, Eigen::Vector3f(100.0, 200.0, 0.0));
298 if (debugDrawerTopic)
300 debugDrawerTopic->setPoseVisu(
"debugdrawerlayer",
"pose", pose);
303 data.qw = quaternion.w();
304 data.qx = quaternion.x();
305 data.qy = quaternion.y();
306 data.qz = quaternion.z();
307 ARMARX_IMPORTANT <<
"or " << orientationQuaternion.w() <<
" " << orientationQuaternion.x() <<
" " << orientationQuaternion.y() <<
" " << orientationQuaternion.z();
310 topicPrx->reportSensorValues(
data.id,
data.pressure,
data.qw,
data.qx,
data.qy,
data.qz, pressureRate, rotationRate, accelerationRate,
data.accelx,
data.accely,
data.accelz, nowTimestamp);
320 std::vector<std::string> splitValues =
Split(line,
" ");
321 data.id = stoi(splitValues.at(0));
322 data.pressure = std::stof(splitValues.at(1));
323 data.qw = std::stof(splitValues.at(2));
324 data.qx = std::stof(splitValues.at(3));
325 data.qy = std::stof(splitValues.at(4));
326 data.qz = std::stof(splitValues.at(5));
327 data.accelx = std::stof(splitValues.at(6));
328 data.accely = std::stof(splitValues.at(7));
329 data.accelz = std::stof(splitValues.at(8));
334 bool OrientedTactileSensorUnit::loadCalibration()
336 std::string calibrationStream = getProperty<std::string>(
"CalibrationData").getValue();
337 std::string arduinoLine;
338 while (arduinoLine.find(
"mode") == std::string::npos)
340 getline(arduinoIn, arduinoLine,
'\n');
342 arduinoOut <<
"load";
344 while (arduinoLine.find(
"calibration data") == std::string::npos)
346 getline(arduinoIn, arduinoLine,
'\n');
349 arduinoOut << calibrationStream;
352 while (arduinoLine.find(
"Calibration Sucessfull") == std::string::npos)
354 getline(arduinoIn, arduinoLine,
'\n');
359 bool OrientedTactileSensorUnit::getCalibrationValues(std::string line)
361 std::vector<std::string> splitValues =
Split(line,
" ");
372 calibration.
mag_radius = stoi(splitValues.at(10));
373 std::string
space =
" ";
374 std::string calibrationStream =
"";