33 usingProxy(getProperty<std::string>(
"KinematicUnitName").getValue());
34 usingProxy(getProperty<std::string>(
"RobotStateName").getValue());
35 offeringTopic(getProperty<std::string>(
"DebugObserverName").getValue());
36 usingProxy(getProperty<std::string>(
"ImageSourceSelectionName").getValue());
41 usingTopic(getProperty<std::string>(
"RobotNodeSetName").getValue() +
"State");
49 armar4 = !(getProperty<std::string>(
"KinematicUnitName").getValue().find(
"Armar3") == 0);
51 headIKName = getProperty<std::string>(
"HeadIKName").getValue();
55 eye_pitch_left =
"EyeL_1_joint";
56 eye_pitch_right =
"EyeR_1_joint";
57 eye_yaw_left =
"EyeL_2_joint";
58 eye_yaw_right =
"EyeR_2_joint";
59 neck_roll =
"Neck_2_joint";
63 eye_pitch_left =
"Cameras";
64 eye_yaw_left =
"Eye_Left";
65 eye_yaw_right =
"Eye_Right";
66 neck_roll =
"Neck_2_Roll";
79 velocityBased = getProperty<bool>(
"VelocityBasedControl").getValue();
80 neckPerturbation = getProperty<bool>(
"NeckPerturbation").getValue();
82 if (getProperty<bool>(
"reafferenceCombination").getValue())
91 ARMARX_LOG <<
"Velocity based: " << velocityBased;
93 vorWeight = getProperty<float>(
"VOR").getValue();
94 okrWeight = getProperty<float>(
"OKR").getValue();
95 jointIKWeight = getProperty<float>(
"JointIK").getValue();
99 kp = getProperty<float>(
"kp").getValue();
100 ki = getProperty<float>(
"ki").getValue();
101 kd = getProperty<float>(
"kd").getValue();
104 vor->
setJointNames(eye_pitch_left, eye_pitch_right, eye_yaw_left, eye_yaw_right, neck_roll);
105 vor->
setBools(armar4, velocityBased);
109 jointIK->
setBools(armar4, velocityBased);
112 okr->
setJointNames(eye_pitch_left, eye_pitch_right, eye_yaw_left, eye_yaw_right, neck_roll);
113 okr->
setBools(armar4, velocityBased);
118 execCombineReflexesTask->setDelayWarningTolerance(2);
123 int sampling_size = 20;
125 IMU_GyroFilters[
"X"]->update(0,
new Variant((
float)0.0));
127 IMU_GyroFilters[
"Y"]->update(0,
new Variant((
float)0.0));
129 IMU_GyroFilters[
"Z"]->update(0,
new Variant((
float)0.0));
131 double frequency = 10.;
132 int sampleRate = 100;
133 double resonance = 1.0;
135 std::map<std::string, float> filterProperties {{
"minSampleTimeDelta", 100 * 1000.0}};
137 for (
auto& joint_name : headJointNames)
141 VelocityFilters[joint_name]->setProperties(filterProperties);
142 VelocityFilters[joint_name]->update(0,
new Variant((
double)0.0));
145 PreVelocityFilters[joint_name]->setInitialValue(0.);
146 PreVelocityFilters[joint_name]->update(0,
new Variant((
double)0.0));
154 FlowFilters[
"X"]->setInitialValue(0.);
155 FlowFilters[
"X"]->update(0,
new Variant((
float)0.0));
157 FlowFilters[
"Y"]->setInitialValue(0.);
158 FlowFilters[
"Y"]->update(0,
new Variant((
float)0.0));
169 kinUnitPrx = getProxy<KinematicUnitInterfacePrx>(getProperty<std::string>(
"KinematicUnitName").getValue());
170 robotStateComponent = getProxy<RobotStateComponentInterfacePrx>(getProperty<std::string>(
"RobotStateName").getValue());
172 debugObserver = getTopic<DebugObserverInterfacePrx>(getProperty<std::string>(
"DebugObserverName").getValue());
173 imageSourceSelection = getProxy<ImageSourceSelectionInterfacePrx>(getProperty<std::string>(
"ImageSourceSelectionName").getValue());
175 drawer = getTopic<DebugDrawerInterfacePrx>(
"DebugDrawerUpdates");
178 std::string nodeSetName = getProperty<std::string>(
"RobotNodeSetName").getValue();
180 jointIK->
setRobot(nodeSetName, headIKName, robotStateComponent);
189 if ((combinationMethod ==
Reafference) && !jointIKEnabled)
191 ARMARX_WARNING <<
"Reafference combination cannot work without IK method enabled. Setting to weighted sum imnstead";
195 execCombineReflexesTask->start();
202 execCombineReflexesTask->stop();
224 ARMARX_LOG <<
"new reflex weights vor:" << vorWeight <<
" okr:" << okrWeight <<
" jointik:" << jointIKWeight;
228 debugValues[
"reflexes_vor"] =
new Variant(vorWeight);
229 debugValues[
"reflexes_okr"] =
new Variant(okrWeight);
230 debugValues[
"reflexes_ik"] =
new Variant(jointIKWeight);
232 debugObserver->setDebugChannel(
"ReflexCombination", debugValues);
235 std::scoped_lock lock(mutex);
237 this->vorWeight = vorWeight;
238 this->okrWeight = okrWeight;
239 this->jointIKWeight = jointIKWeight;
241 vorEnabled = (vorWeight > 0);
242 jointIKEnabled = (jointIKWeight > 0);
243 okrEnabled = (okrWeight > 0);
245 ARMARX_LOG <<
"enabled reflexes vor:" << vorEnabled <<
" okr:" << okrEnabled <<
" jointik:" << jointIKEnabled;
266 void ReflexCombination::combineReflexes()
268 std::scoped_lock lock(mutex);
270 if (!(vorEnabled || jointIKEnabled || okrEnabled))
285 NameControlModeMap controlModes;
287 std::map<std::string, float> weights;
288 std::map<std::string, float> jointValuesIK = jointIK->
getJoints();
289 std::map<std::string, float> jointValuesVOR = vor->
getJoints();
290 std::map<std::string, float> jointValuesOKR = okr->
getJoints();
301 mapValues[
"vel_filtered"] =
new Variant(VelocityFilters[eye_yaw_left]->
getValue()->getDouble());
302 mapValues[
"flow_filteredX"] =
new Variant(FlowFilters[
"X"]->
getValue()->getDouble());
303 mapValues[
"flow_filteredY"] =
new Variant(FlowFilters[
"Y"]->
getValue()->getDouble());
304 mapValues[
"imu_filteredX"] =
new Variant(IMU_GyroFilters[
"X"]->
getValue()->getFloat());
305 mapValues[
"imu_filteredY"] =
new Variant(IMU_GyroFilters[
"Y"]->
getValue()->getFloat());
306 mapValues[
"imu_filteredZ"] =
new Variant(IMU_GyroFilters[
"Z"]->
getValue()->getFloat());
307 debugObserver->setDebugChannel(
"ForwardPredictor", mapValues);
332 for (
auto& kv : jointValuesIK)
334 if (!resultJointValues.count(kv.first))
336 resultJointValues[kv.first] = 0;
337 weights[kv.first] = 0;
339 resultJointValues[kv.first] += jointIKWeight * kv.second;
340 weights[kv.first] += jointIKWeight;
343 for (
auto& kv : jointValuesVOR)
345 if (!resultJointValues.count(kv.first))
347 resultJointValues[kv.first] = 0;
348 weights[kv.first] = 0;
350 resultJointValues[kv.first] += vorWeight * kv.second;
351 weights[kv.first] += vorWeight;
354 for (
auto& kv : jointValuesOKR)
356 if (!resultJointValues.count(kv.first))
358 resultJointValues[kv.first] = 0;
359 weights[kv.first] = 0;
361 resultJointValues[kv.first] += okrWeight * kv.second;
362 weights[kv.first] += okrWeight;
370 for (
auto& kv : weights)
372 resultJointValues[kv.first] = resultJointValues[kv.first] / kv.second;
377 if (resultJointValues.empty())
386 for (
auto const& headJoint : headJointNames)
388 if (resultJointValues.find(headJoint) == resultJointValues.end())
390 resultJointValues[headJoint] = 0.;
401 resultJointValues[
"Eye_Left"] = resultJointValues[
"Eye_Right"] + kp * (PreVelocityFilters[
"Eye_Right"]->getValue()->getDouble() - PreVelocityFilters[
"Eye_Left"]->getValue()->getDouble());
410 for (
auto& kv : resultJointValues)
414 controlModes[kv.first] = eVelocityControl;
418 controlModes[kv.first] = ePositionControl;
422 if (neckPerturbation && velocityBased)
424 controlModes[
"Neck_3_Yaw"] = eVelocityControl;
435 for (
auto& kv : resultJointValues)
437 debugValues[kv.first] =
new Variant(kv.second);
440 debugObserver->setDebugChannel(
"ReflexCombination", debugValues);
444 for (
auto& kv : jointValuesIK)
446 debugValuesIK[kv.first] =
new Variant(kv.second);
448 debugObserver->setDebugChannel(
"ReflexCombinationIK", debugValuesIK);
451 for (
auto& kv : jointValuesOKR)
453 debugValuesOKR[kv.first] =
new Variant(kv.second);
455 debugObserver->setDebugChannel(
"ReflexCombinationOKR", debugValuesOKR);
458 for (
auto& kv : jointValuesVOR)
460 debugValuesVOR[kv.first] =
new Variant(kv.second);
462 debugObserver->setDebugChannel(
"ReflexCombinationVOR", debugValuesVOR);
464 kinUnitPrx->switchControlMode(controlModes);
467 kinUnitPrx->setJointVelocities(resultJointValues);
471 kinUnitPrx->setJointAngles(resultJointValues);
496 std::scoped_lock lock(mutex);
503 bool jointAnglesReached =
true;
504 for (std::pair<std::string, Ice::Float> joint : targetJointAngles)
506 jointAnglesReached &= (tmp[joint.first] >= (joint.second - offset));
507 jointAnglesReached &= (tmp[joint.first] <= (joint.second + offset));
510 if (jointAnglesReached)
512 newHeadTarget =
false;
540 vor->reportJointAngles(
values, valueChanged,
c);
544 jointIK->reportJointAngles(
values, valueChanged,
c);
548 okr->reportJointAngles(
values, valueChanged,
c);
552 for (
auto& joint_name : headJointNames)
554 PreVelocityFilters[joint_name]->update(timestamp,
new Variant(tmp[joint_name]));
555 VelocityFilters[joint_name]->update(timestamp,
new Variant(PreVelocityFilters[joint_name]->getValue()->getDouble()));
562 std::scoped_lock lock(mutex);
564 for (
auto& joint_name : headJointNames)
566 if (velocities_filtered.count(joint_name))
568 velocities_filtered[joint_name] = VelocityFilters[joint_name]->getValue()->getDouble();
576 vor->reportJointVelocities(velocities_filtered, valueChanged,
c);
580 jointIK->reportJointVelocities(velocities_filtered, valueChanged,
c);
584 if (combinationMethod ==
Reafference && jointIKEnabled)
586 velocities_filtered[eye_yaw_left] = 0.;
587 velocities_filtered[eye_yaw_right] = 0.;
588 velocities_filtered[eye_pitch_left] = 0.;
590 okr->reportJointVelocities(velocities_filtered, valueChanged, timestamp,
c);
619 std::scoped_lock lock(mutex);
621 FlowFilters[
"X"]->update(timestamp,
new Variant(x));
622 FlowFilters[
"Y"]->update(timestamp,
new Variant(y));
624 double x_flow, y_flow;
626 x_flow = FlowFilters[
"X"]->getValue()->getDouble();
627 y_flow = FlowFilters[
"Y"]->getValue()->getDouble();
633 if (combinationMethod ==
Reafference && jointIKEnabled)
635 x_flow -= jointIK->optFlow_pred[0];
636 y_flow -= jointIK->optFlow_pred[1];
638 if (fabs(x_flow) < 0.04)
646 okr->reportNewOpticalFlow(x_flow, y_flow, deltaT, timestamp);
654 std::scoped_lock lock(mutex);
656 newHeadTarget =
true;
657 this->targetJointAngles = targetJointAngles;
659 ARMARX_INFO <<
" new head target " << targetPosition->output();
665 jointIK->reportHeadTargetChanged(targetJointAngles, targetPosition);
683 std::scoped_lock lock(mutex);
691 if (IMU_GyroFilters[
"X"] && IMU_GyroFilters[
"Y"] && IMU_GyroFilters[
"Z"])
693 IMU_GyroFilters[
"X"]->update(timestamp->timestamp,
new Variant((
float)
values.gyroscopeRotation[0]));
694 IMU_GyroFilters[
"Y"]->update(timestamp->timestamp,
new Variant((
float)
values.gyroscopeRotation[1]));
695 IMU_GyroFilters[
"Z"]->update(timestamp->timestamp,
new Variant((
float)
values.gyroscopeRotation[2]));
700 if (IMU_GyroFilters[
"X"] && IMU_GyroFilters[
"Y"] && IMU_GyroFilters[
"Z"])
702 values2.gyroscopeRotation[0] = IMU_GyroFilters[
"X"]->getValue()->getFloat();
703 values2.gyroscopeRotation[1] = IMU_GyroFilters[
"Y"]->getValue()->getFloat();
704 values2.gyroscopeRotation[2] = IMU_GyroFilters[
"Z"]->getValue()->getFloat();
707 if (combinationMethod ==
Reafference && jointIKEnabled)
709 values2.gyroscopeRotation[0] -= jointIK->gyroscopeRotation_pred[0];
710 values2.gyroscopeRotation[1] -= jointIK->gyroscopeRotation_pred[1];
711 values2.gyroscopeRotation[2] -= jointIK->gyroscopeRotation_pred[2];
713 if (fabs(values2.gyroscopeRotation[2]) < 0.04)
715 values2.gyroscopeRotation[2] = 0.;
721 vor->reportSensorValues(values2);
727 void armarx::ReflexCombination::setImageQuality(
float quality)
729 Ice::StringSeq imageSources = getProperty<Ice::StringSeq>(
"ImageProviders");
733 imageSourceSelection->setImageSource(imageSources[1], 100000000);
737 imageSourceSelection->setImageSource(imageSources[0], 100000000);
743 okr->reportNewTrackingError(pixelX, pixelY, angleX, angleY);