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";
69 "Neck_1_Pitch", neck_roll,
"Neck_3_Yaw", eye_pitch_left, eye_yaw_right, eye_yaw_left};
72 velocityBased = getProperty<bool>(
"VelocityBasedControl").getValue();
73 neckPerturbation = getProperty<bool>(
"NeckPerturbation").getValue();
75 if (getProperty<bool>(
"reafferenceCombination").getValue())
84 ARMARX_LOG <<
"Velocity based: " << velocityBased;
86 vorWeight = getProperty<float>(
"VOR").getValue();
87 okrWeight = getProperty<float>(
"OKR").getValue();
88 jointIKWeight = getProperty<float>(
"JointIK").getValue();
91 kp = getProperty<float>(
"kp").getValue();
92 ki = getProperty<float>(
"ki").getValue();
93 kd = getProperty<float>(
"kd").getValue();
96 vor->
setJointNames(eye_pitch_left, eye_pitch_right, eye_yaw_left, eye_yaw_right, neck_roll);
97 vor->
setBools(armar4, velocityBased);
101 jointIK->
setBools(armar4, velocityBased);
104 okr->
setJointNames(eye_pitch_left, eye_pitch_right, eye_yaw_left, eye_yaw_right, neck_roll);
105 okr->
setBools(armar4, velocityBased);
110 this, &ReflexCombination::combineReflexes, 5,
false,
"ReflexCombinationTask");
111 execCombineReflexesTask->setDelayWarningTolerance(2);
116 int sampling_size = 20;
118 IMU_GyroFilters[
"X"]->update(0,
new Variant((
float)0.0));
120 IMU_GyroFilters[
"Y"]->update(0,
new Variant((
float)0.0));
122 IMU_GyroFilters[
"Z"]->update(0,
new Variant((
float)0.0));
124 double frequency = 10.;
125 int sampleRate = 100;
126 double resonance = 1.0;
128 std::map<std::string, float> filterProperties{{
"minSampleTimeDelta", 100 * 1000.0}};
130 for (
auto& joint_name : headJointNames)
134 VelocityFilters[joint_name]->setProperties(filterProperties);
135 VelocityFilters[joint_name]->update(0,
new Variant((
double)0.0));
137 PreVelocityFilters[joint_name] =
139 PreVelocityFilters[joint_name]->setInitialValue(0.);
140 PreVelocityFilters[joint_name]->update(0,
new Variant((
double)0.0));
148 FlowFilters[
"X"]->setInitialValue(0.);
149 FlowFilters[
"X"]->update(0,
new Variant((
float)0.0));
152 FlowFilters[
"Y"]->setInitialValue(0.);
153 FlowFilters[
"Y"]->update(0,
new Variant((
float)0.0));
162 kinUnitPrx = getProxy<KinematicUnitInterfacePrx>(
163 getProperty<std::string>(
"KinematicUnitName").getValue());
164 robotStateComponent = getProxy<RobotStateComponentInterfacePrx>(
165 getProperty<std::string>(
"RobotStateName").getValue());
167 debugObserver = getTopic<DebugObserverInterfacePrx>(
168 getProperty<std::string>(
"DebugObserverName").getValue());
169 imageSourceSelection = getProxy<ImageSourceSelectionInterfacePrx>(
170 getProperty<std::string>(
"ImageSourceSelectionName").getValue());
172 drawer = getTopic<DebugDrawerInterfacePrx>(
"DebugDrawerUpdates");
175 std::string nodeSetName = getProperty<std::string>(
"RobotNodeSetName").getValue();
177 jointIK->
setRobot(nodeSetName, headIKName, robotStateComponent);
186 if ((combinationMethod ==
Reafference) && !jointIKEnabled)
188 ARMARX_WARNING <<
"Reafference combination cannot work without IK method enabled. Setting "
189 "to weighted sum imnstead";
193 execCombineReflexesTask->start();
199 execCombineReflexesTask->stop();
221 const Ice::Current&
c)
225 ARMARX_LOG <<
"new reflex weights vor:" << vorWeight <<
" okr:" << okrWeight
226 <<
" jointik:" << jointIKWeight;
230 debugValues[
"reflexes_vor"] =
new Variant(vorWeight);
231 debugValues[
"reflexes_okr"] =
new Variant(okrWeight);
232 debugValues[
"reflexes_ik"] =
new Variant(jointIKWeight);
234 debugObserver->setDebugChannel(
"ReflexCombination", debugValues);
237 std::scoped_lock lock(mutex);
239 this->vorWeight = vorWeight;
240 this->okrWeight = okrWeight;
241 this->jointIKWeight = jointIKWeight;
243 vorEnabled = (vorWeight > 0);
244 jointIKEnabled = (jointIKWeight > 0);
245 okrEnabled = (okrWeight > 0);
247 ARMARX_LOG <<
"enabled reflexes vor:" << vorEnabled <<
" okr:" << okrEnabled
248 <<
" jointik:" << jointIKEnabled;
271 ReflexCombination::combineReflexes()
273 std::scoped_lock lock(mutex);
275 if (!(vorEnabled || jointIKEnabled || okrEnabled))
278 <<
"at least one reflex must be enabled to stabilize the gaze!";
291 NameControlModeMap controlModes;
293 std::map<std::string, float> weights;
294 std::map<std::string, float> jointValuesIK = jointIK->
getJoints();
295 std::map<std::string, float> jointValuesVOR = vor->
getJoints();
296 std::map<std::string, float> jointValuesOKR = okr->
getJoints();
307 mapValues[
"vel_filtered"] =
new Variant(VelocityFilters[eye_yaw_left]->
getValue()->getDouble());
308 mapValues[
"flow_filteredX"] =
new Variant(FlowFilters[
"X"]->
getValue()->getDouble());
309 mapValues[
"flow_filteredY"] =
new Variant(FlowFilters[
"Y"]->
getValue()->getDouble());
310 mapValues[
"imu_filteredX"] =
new Variant(IMU_GyroFilters[
"X"]->
getValue()->getFloat());
311 mapValues[
"imu_filteredY"] =
new Variant(IMU_GyroFilters[
"Y"]->
getValue()->getFloat());
312 mapValues[
"imu_filteredZ"] =
new Variant(IMU_GyroFilters[
"Z"]->
getValue()->getFloat());
313 debugObserver->setDebugChannel(
"ForwardPredictor", mapValues);
338 for (
auto& kv : jointValuesIK)
340 if (!resultJointValues.count(kv.first))
342 resultJointValues[kv.first] = 0;
343 weights[kv.first] = 0;
345 resultJointValues[kv.first] += jointIKWeight * kv.second;
346 weights[kv.first] += jointIKWeight;
349 for (
auto& kv : jointValuesVOR)
351 if (!resultJointValues.count(kv.first))
353 resultJointValues[kv.first] = 0;
354 weights[kv.first] = 0;
356 resultJointValues[kv.first] += vorWeight * kv.second;
357 weights[kv.first] += vorWeight;
360 for (
auto& kv : jointValuesOKR)
362 if (!resultJointValues.count(kv.first))
364 resultJointValues[kv.first] = 0;
365 weights[kv.first] = 0;
367 resultJointValues[kv.first] += okrWeight * kv.second;
368 weights[kv.first] += okrWeight;
376 for (
auto& kv : weights)
378 resultJointValues[kv.first] = resultJointValues[kv.first] / kv.second;
383 if (resultJointValues.empty())
392 for (
auto const& headJoint : headJointNames)
394 if (resultJointValues.find(headJoint) == resultJointValues.end())
396 resultJointValues[headJoint] = 0.;
407 resultJointValues[
"Eye_Left"] =
408 resultJointValues[
"Eye_Right"] +
409 kp * (PreVelocityFilters[
"Eye_Right"]->getValue()->getDouble() -
410 PreVelocityFilters[
"Eye_Left"]->getValue()->getDouble());
417 for (
auto& kv : resultJointValues)
421 controlModes[kv.first] = eVelocityControl;
425 controlModes[kv.first] = ePositionControl;
429 if (neckPerturbation && velocityBased)
431 controlModes[
"Neck_3_Yaw"] = eVelocityControl;
432 resultJointValues[
"Neck_3_Yaw"] =
443 for (
auto& kv : resultJointValues)
445 debugValues[kv.first] =
new Variant(kv.second);
448 debugObserver->setDebugChannel(
"ReflexCombination", debugValues);
452 for (
auto& kv : jointValuesIK)
454 debugValuesIK[kv.first] =
new Variant(kv.second);
456 debugObserver->setDebugChannel(
"ReflexCombinationIK", debugValuesIK);
459 for (
auto& kv : jointValuesOKR)
461 debugValuesOKR[kv.first] =
new Variant(kv.second);
463 debugObserver->setDebugChannel(
"ReflexCombinationOKR", debugValuesOKR);
466 for (
auto& kv : jointValuesVOR)
468 debugValuesVOR[kv.first] =
new Variant(kv.second);
470 debugObserver->setDebugChannel(
"ReflexCombinationVOR", debugValuesVOR);
472 kinUnitPrx->switchControlMode(controlModes);
475 kinUnitPrx->setJointVelocities(resultJointValues);
479 kinUnitPrx->setJointAngles(resultJointValues);
492 const Ice::Current&
c)
512 const Ice::Current&
c)
514 std::scoped_lock lock(mutex);
521 bool jointAnglesReached =
true;
522 for (std::pair<std::string, Ice::Float> joint : targetJointAngles)
524 jointAnglesReached &= (tmp[joint.first] >= (joint.second - offset));
525 jointAnglesReached &= (tmp[joint.first] <= (joint.second + offset));
528 if (jointAnglesReached)
530 newHeadTarget =
false;
557 vor->reportJointAngles(
values, valueChanged,
c);
561 jointIK->reportJointAngles(
values, valueChanged,
c);
565 okr->reportJointAngles(
values, valueChanged,
c);
569 for (
auto& joint_name : headJointNames)
571 PreVelocityFilters[joint_name]->update(timestamp,
new Variant(tmp[joint_name]));
572 VelocityFilters[joint_name]->update(
573 timestamp,
new Variant(PreVelocityFilters[joint_name]->getValue()->getDouble()));
581 const Ice::Current&
c)
584 std::scoped_lock lock(mutex);
586 for (
auto& joint_name : headJointNames)
588 if (velocities_filtered.count(joint_name))
590 velocities_filtered[joint_name] = VelocityFilters[joint_name]->getValue()->getDouble();
597 vor->reportJointVelocities(velocities_filtered, valueChanged,
c);
601 jointIK->reportJointVelocities(velocities_filtered, valueChanged,
c);
605 if (combinationMethod ==
Reafference && jointIKEnabled)
607 velocities_filtered[eye_yaw_left] =
609 velocities_filtered[eye_yaw_right] = 0.;
610 velocities_filtered[eye_pitch_left] = 0.;
612 okr->reportJointVelocities(velocities_filtered, valueChanged, timestamp,
c);
661 const Ice::Current&
c)
664 std::scoped_lock lock(mutex);
666 FlowFilters[
"X"]->update(timestamp,
new Variant(x));
667 FlowFilters[
"Y"]->update(timestamp,
new Variant(y));
669 double x_flow, y_flow;
671 x_flow = FlowFilters[
"X"]->getValue()->getDouble();
672 y_flow = FlowFilters[
"Y"]->getValue()->getDouble();
678 if (combinationMethod ==
Reafference && jointIKEnabled)
680 x_flow -= jointIK->optFlow_pred[0];
681 y_flow -= jointIK->optFlow_pred[1];
683 if (fabs(x_flow) < 0.04)
691 okr->reportNewOpticalFlow(x_flow, y_flow, deltaT, timestamp);
697 const FramedPositionBasePtr& targetPosition,
701 std::scoped_lock lock(mutex);
703 newHeadTarget =
true;
704 this->targetJointAngles = targetJointAngles;
706 ARMARX_INFO <<
" new head target " << targetPosition->output();
712 jointIK->reportHeadTargetChanged(targetJointAngles, targetPosition);
726 const std::string& name,
728 const TimestampBasePtr& timestamp,
729 const Ice::Current&
c)
733 std::scoped_lock lock(mutex);
741 if (IMU_GyroFilters[
"X"] && IMU_GyroFilters[
"Y"] && IMU_GyroFilters[
"Z"])
743 IMU_GyroFilters[
"X"]->update(timestamp->timestamp,
745 IMU_GyroFilters[
"Y"]->update(timestamp->timestamp,
747 IMU_GyroFilters[
"Z"]->update(timestamp->timestamp,
753 if (IMU_GyroFilters[
"X"] && IMU_GyroFilters[
"Y"] && IMU_GyroFilters[
"Z"])
755 values2.gyroscopeRotation[0] = IMU_GyroFilters[
"X"]->getValue()->getFloat();
756 values2.gyroscopeRotation[1] = IMU_GyroFilters[
"Y"]->getValue()->getFloat();
757 values2.gyroscopeRotation[2] = IMU_GyroFilters[
"Z"]->getValue()->getFloat();
760 if (combinationMethod ==
Reafference && jointIKEnabled)
762 values2.gyroscopeRotation[0] -= jointIK->gyroscopeRotation_pred[0];
763 values2.gyroscopeRotation[1] -= jointIK->gyroscopeRotation_pred[1];
764 values2.gyroscopeRotation[2] -= jointIK->gyroscopeRotation_pred[2];
766 if (fabs(values2.gyroscopeRotation[2]) < 0.04)
768 values2.gyroscopeRotation[2] = 0.;
774 vor->reportSensorValues(values2);
779 armarx::ReflexCombination::setImageQuality(
float quality)
781 Ice::StringSeq imageSources = getProperty<Ice::StringSeq>(
"ImageProviders");
785 imageSourceSelection->setImageSource(imageSources[1], 100000000);
789 imageSourceSelection->setImageSource(imageSources[0], 100000000);
800 okr->reportNewTrackingError(pixelX, pixelY, angleX, angleY);