35 #include <Eigen/Geometry>
38 #include <SimoxUtility/math.h>
50 v = std::numeric_limits<float>::infinity();
55 invalidate(Eigen::Vector2f&
v)
57 v.x() = std::numeric_limits<float>::infinity();
58 v.y() = std::numeric_limits<float>::infinity();
63 invalidate(Eigen::Vector3f&
v)
65 v.x() = std::numeric_limits<float>::infinity();
66 v.y() = std::numeric_limits<float>::infinity();
67 v.z() = std::numeric_limits<float>::infinity();
71 void invalidate(std::deque<T>& d)
119 if (!hasRobot(
"robot"))
121 m_robot = addRobot(
"robot", VirtualRobot::RobotIO::eStructure);
124 invalidate(m_control_data.target_vel);
125 invalidate(m_control_data.target_rot_vel);
126 invalidate(m_control_data.target_pos);
127 invalidate(m_control_data.target_ori);
128 invalidate(m_viz.start);
129 invalidate(m_control_data.vel_history);
140 schedule_high_level_control_loop(control_mode::none);
156 const float target_pos_x,
157 const float target_pos_y,
158 const float target_ori,
159 const float pos_reached_threshold,
160 const float ori_reached_threshold,
163 using namespace simox::math;
165 std::scoped_lock l{m_control_data.mutex};
167 m_control_data.target_pos = Eigen::Vector2f{target_pos_x, target_pos_y};
168 m_control_data.target_ori = periodic_clamp<float>(target_ori, -
M_PI,
M_PI);
169 m_control_data.pos_reached_threshold = pos_reached_threshold;
170 m_control_data.ori_reached_threshold = ori_reached_threshold;
173 invalidate(m_control_data.vel_history);
175 invalidate(m_control_data.target_vel);
176 invalidate(m_control_data.target_rot_vel);
178 schedule_high_level_control_loop(control_mode::position);
184 const float target_vel_x,
185 const float target_vel_y,
186 const float target_rot_vel,
189 using namespace simox::math;
191 std::scoped_lock l{m_control_data.mutex};
193 m_control_data.target_vel = Eigen::Vector2f{target_vel_x, target_vel_y};
194 m_control_data.target_rot_vel = periodic_clamp<float>(target_rot_vel, -
M_PI,
M_PI);
196 invalidate(m_control_data.target_pos);
197 invalidate(m_control_data.target_ori);
202 schedule_high_level_control_loop(control_mode::velocity);
208 const float target_pos_delta_x,
209 const float target_pos_delta_y,
210 const float target_delta_ori,
211 const float pos_reached_threshold,
212 const float ori_reached_threshold,
213 const Ice::Current& current)
215 using namespace simox::math;
218 std::unique_lock lock{m_control_data.mutex};
219 synchronizeLocalClone(m_robot);
220 const Eigen::Vector2f agent_pos = m_robot->getGlobalPosition().head<2>();
221 const float agent_ori = mat4f_to_rpy(m_robot->getGlobalPose()).z();
226 agent_pos.x() + target_pos_delta_x,
227 agent_pos.y() + target_pos_delta_y,
228 agent_ori + target_delta_ori,
229 pos_reached_threshold,
230 ori_reached_threshold,
237 const float max_pos_vel,
238 const float max_rot_vel,
241 std::scoped_lock l{m_control_data.mutex};
242 m_control_data.max_vel = max_pos_vel;
243 m_control_data.max_rot_vel = max_rot_vel;
244 m_platform->setMaxVelocities(max_pos_vel, max_rot_vel);
251 schedule_high_level_control_loop(control_mode::none);
252 m_platform->stopPlatform();
257 armarx::ObstacleAvoidingPlatformUnit::schedule_high_level_control_loop(control_mode mode)
259 std::scoped_lock l{m_control_loop.mutex};
262 if (m_control_loop.mode == mode)
268 if (m_control_loop.mode != mode and m_control_loop.task)
271 const bool join =
true;
272 m_control_loop.task->stop(join);
276 if (mode == control_mode::none)
284 m_control_loop.mode = mode;
285 m_control_loop.task =
new RunningTask<ObstacleAvoidingPlatformUnit>(
287 &ObstacleAvoidingPlatformUnit::high_level_control_loop);
288 m_control_loop.task->start();
293 armarx::ObstacleAvoidingPlatformUnit::high_level_control_loop()
295 const control_mode mode = m_control_loop.mode;
300 CycleUtil cu{m_control_loop.cycle_time};
301 while (not m_control_loop.task->isStopped())
303 const velocities vels = get_velocities();
307 m[
"err_dist"] =
new Variant{vels.err_dist};
308 m[
"err_angular_dist"] =
new Variant{vels.err_angular_dist};
310 m[
"target_global_x"] =
new Variant{vels.target_global.x()};
311 m[
"target_global_y"] =
new Variant{vels.target_global.y()};
312 m[
"target_global_abs"] =
new Variant{vels.target_global.norm()};
314 m[
"target_local_x"] =
new Variant{vels.target_local.x()};
315 m[
"target_local_y"] =
new Variant{vels.target_local.y()};
316 m[
"target_local_abs"] =
new Variant(vels.target_local.norm());
317 m[
"target_rot"] =
new Variant{vels.target_rot};
319 m[
"modulated_global_x"] =
new Variant{vels.modulated_global.x()};
320 m[
"modulated_global_y"] =
new Variant{vels.modulated_global.y()};
321 m[
"modulated_global_abs"] =
new Variant{vels.modulated_global.norm()};
323 m[
"modulated_local_x"] =
new Variant{vels.modulated_local.x()};
324 m[
"modulated_local_y"] =
new Variant{vels.modulated_local.y()};
325 m[
"modulated_local_abs"] =
new Variant{vels.modulated_local.norm()};
327 setDebugObserverChannel(
"ObstacleAvoidingPlatformCtrl", m);
329 m_platform->move(vels.modulated_local.x(), vels.modulated_local.y(), vels.target_rot);
331 cu.waitForCycleDuration();
334 catch (
const std::exception& e)
336 ARMARX_ERROR <<
"Error occured while running control loop.\n"
341 ARMARX_ERROR <<
"Unknown error occured while running control loop.";
345 invalidate(m_control_data.vel_history);
347 m_platform->move(0, 0, 0);
348 m_platform->stopPlatform();
349 m_control_loop.mode = control_mode::none;
357 armarx::ObstacleAvoidingPlatformUnit::velocities
358 armarx::ObstacleAvoidingPlatformUnit::get_velocities()
360 using namespace simox::math;
363 std::scoped_lock l{m_control_data.mutex};
366 update_agent_dependent_values();
367 const Eigen::Vector2f target_vel = get_target_velocity();
368 const float target_rot_vel = get_target_rotational_velocity();
371 const Eigen::Vector2f modulated_vel = [
this, &target_vel]
373 obstacleavoidance::Agent agent;
374 agent.safety_margin = m_control_data.agent_safety_margin;
375 agent.pos = Eigen::Vector3f{m_control_data.agent_pos.x(), m_control_data.agent_pos.y(), 0};
376 agent.desired_vel = Eigen::Vector3f{target_vel.x(), target_vel.y(), 0};
378 const Eigen::Vector2f raw = m_obstacle_avoidance->modulateVelocity(agent).head<2>();
379 return post_process_vel(target_vel, norm_max(raw, m_control_data.max_vel));
382 ARMARX_CHECK(modulated_vel.allFinite()) <<
"Velocity contains non-finite values.";
385 ARMARX_DEBUG <<
"Target velocity: " << target_vel.transpose() <<
"; norm: " << target_vel.norm() <<
"; " << target_rot_vel;
386 ARMARX_DEBUG <<
"Modulated velocity: " << modulated_vel.transpose() << modulated_vel.norm();
388 const auto r = Eigen::Rotation2D(m_control_data.agent_ori).toRotationMatrix().inverse();
391 vels.target_local = r * target_vel;
392 vels.target_global = target_vel;
393 vels.modulated_local = r * modulated_vel;
394 vels.modulated_global = modulated_vel;
395 vels.target_rot = target_rot_vel;
396 vels.err_dist = m_control_data.target_dist;
397 vels.err_angular_dist = m_control_data.target_angular_dist;
404 armarx::ObstacleAvoidingPlatformUnit::get_target_velocity()
407 using namespace simox::math;
409 Eigen::Vector2f uncapped_target_vel = Eigen::Vector2f::Zero();
411 if (m_control_loop.mode == control_mode::position )
413 uncapped_target_vel =
414 (m_control_data.target_pos - m_control_data.agent_pos) * m_control_data.kp;
416 else if (m_control_loop.mode == control_mode::velocity)
418 uncapped_target_vel = m_control_data.target_vel;
423 return norm_max(uncapped_target_vel, m_control_data.max_vel);
428 armarx::ObstacleAvoidingPlatformUnit::get_target_rotational_velocity()
431 using namespace simox::math;
433 float uncapped_target_rot_vel = 0;
435 if (m_control_loop.mode == control_mode::position )
437 m_rot_pid_controller.update(m_control_data.target_angular_dist, 0);
438 uncapped_target_rot_vel = -m_rot_pid_controller.getControlValue();
440 else if (m_control_loop.mode == control_mode::velocity)
442 uncapped_target_rot_vel = m_control_data.target_rot_vel;
447 return std::copysign(
std::min(std::fabs(uncapped_target_rot_vel), m_control_data.max_rot_vel),
448 uncapped_target_rot_vel);
453 armarx::ObstacleAvoidingPlatformUnit::update_agent_dependent_values()
455 using namespace simox::math;
457 synchronizeLocalClone(m_robot);
458 m_control_data.agent_pos = m_robot->getGlobalPosition().head<2>();
459 m_control_data.agent_ori =
460 periodic_clamp<float>(mat4f_to_rpy(m_robot->getGlobalPose()).z(), -
M_PI,
M_PI);
466 if (m_control_loop.mode == control_mode::position)
473 m_control_data.target_dist =
474 (m_control_data.target_pos - m_control_data.agent_pos).
norm();
475 m_control_data.target_angular_dist =
476 periodic_clamp<float>(m_control_data.target_ori - m_control_data.agent_ori,
482 ARMARX_DEBUG <<
"Distance to target: " << m_control_data.target_dist <<
" mm and "
483 << m_control_data.target_angular_dist <<
" rad.";
488 invalidate(m_control_data.target_dist);
489 invalidate(m_control_data.target_angular_dist);
495 armarx::ObstacleAvoidingPlatformUnit::target_position_reached()
498 if (m_control_loop.mode == control_mode::position)
500 return m_control_data.target_dist < m_control_data.pos_reached_threshold;
509 armarx::ObstacleAvoidingPlatformUnit::target_orientation_reached()
512 if (m_control_loop.mode == control_mode::position)
514 return std::fabs(m_control_data.target_angular_dist) < m_control_data.ori_reached_threshold;
523 armarx::ObstacleAvoidingPlatformUnit::target_reached()
526 if (m_control_loop.mode == control_mode::position)
528 return target_position_reached() and target_orientation_reached();
536 armarx::ObstacleAvoidingPlatformUnit::post_process_vel(
537 const
Eigen::Vector2f& target_vel,
538 const
Eigen::Vector2f& modulated_vel)
544 m_control_data.vel_history.push_front(std::make_tuple(target_vel, modulated_vel));
545 const unsigned max_real_buffer_size =
546 std::max(m_control_data.amount_smoothing, m_control_data.amount_max_vel);
547 if (m_control_data.vel_history.size() > max_real_buffer_size)
549 m_control_data.vel_history.resize(max_real_buffer_size);
553 const Eigen::Vector2f mean_modulated_vel = calculate_mean_modulated_vel();
555 const bool is_near_target = this->is_near_target(mean_modulated_vel);
558 float min_vel = is_near_target ? m_control_data.min_vel_near_target : m_control_data.min_vel_general;
559 if (target_vel.norm() < min_vel)
561 min_vel = target_vel.norm();
569 const float max_vel = is_near_target ? target_vel.norm() : calculate_adaptive_max_vel();
575 return simox::math::norm_clamp(mean_modulated_vel, min_vel, max_vel);
579 bool armarx::ObstacleAvoidingPlatformUnit::is_near_target(
const Eigen::Vector2f& control_velocity)
const noexcept
581 if (m_control_data.target_dist < m_control_data.pos_near_threshold)
583 const Eigen::Vector2f target_direction = m_control_data.target_pos - m_control_data.agent_pos;
584 const Eigen::Vector2f control_direction = control_velocity / control_velocity.norm();
586 const float sim = simox::math::cosine_similarity(target_direction, control_direction);
589 if (sim > cos(M_PI_4))
600 armarx::ObstacleAvoidingPlatformUnit::calculate_mean_modulated_vel()
603 const unsigned adaptive_buffer_size = calculate_adaptive_smoothing_buffer_size();
604 const unsigned elements =
605 std::min<unsigned>(m_control_data.vel_history.size(), adaptive_buffer_size);
606 auto end = m_control_data.vel_history.begin();
607 std::advance(end, elements);
611 const Eigen::Vector2f & vel,
612 const std::tuple<Eigen::Vector2f, Eigen::Vector2f>& vels)
615 return vel + std::get<1>(vels) * (1. / elements);
618 return std::accumulate(m_control_data.vel_history.begin(),
620 Eigen::Vector2f{0, 0},
626 armarx::ObstacleAvoidingPlatformUnit::calculate_adaptive_smoothing_buffer_size()
630 const float min_buffer_size_dist = 200;
631 const float min_buffer_size = 3;
633 const float max_buffer_size_dist = 1500;
634 const float max_buffer_size = m_control_data.amount_smoothing;
638 if (m_control_loop.mode == control_mode::position)
641 const float m = (max_buffer_size - min_buffer_size) /
642 (max_buffer_size_dist - min_buffer_size_dist);
643 const float b = min_buffer_size - (m * min_buffer_size_dist);
649 return static_cast<unsigned>(
std::clamp(m * m_control_data.target_dist + b,
650 min_buffer_size, max_buffer_size));
655 return min_buffer_size;
661 armarx::ObstacleAvoidingPlatformUnit::calculate_adaptive_max_vel()
664 using namespace simox::math;
666 if (m_control_loop.mode == control_mode::position and m_control_data.adaptive_max_vel_exp > 0)
668 std::vector<float> angular_similarities;
670 const unsigned elements =
671 std::min<unsigned>(m_control_data.vel_history.size(), m_control_data.amount_max_vel);
672 auto end = m_control_data.vel_history.begin();
673 std::advance(end, elements);
676 [elements](
const std::tuple<Eigen::Vector2f, Eigen::Vector2f>& vels) ->
float
678 const auto& [desired_vel, modulated_vel] = vels;
680 if (desired_vel.isZero() and modulated_vel.isZero())
684 else if (desired_vel.isZero() xor modulated_vel.isZero())
689 return angular_similarity(desired_vel, modulated_vel) / elements;
701 std::back_inserter(angular_similarities),
704 const float mean_angular_similarity = std::accumulate(angular_similarities.begin(),
705 angular_similarities.end(),
708 const float max_vel_factor = std::pow(mean_angular_similarity,
709 m_control_data.adaptive_max_vel_exp);
711 return max_vel_factor * m_control_data.max_vel;
715 return m_control_data.max_vel;
721 armarx::ObstacleAvoidingPlatformUnit::visualize()
723 const Eigen::Vector2f zero = Eigen::Vector2f::Zero();
725 vels.target_local = zero;
726 vels.target_global = zero;
727 vels.modulated_local = zero;
728 vels.modulated_global = zero;
736 armarx::ObstacleAvoidingPlatformUnit::visualize(
const velocities& vels)
740 if (not m_viz.enabled)
745 Eigen::Vector3f agent_pos{m_control_data.agent_pos.x(), m_control_data.agent_pos.y(), 0};
748 Layer l_prog = arviz.layer(
"progress");
749 if (m_control_loop.mode == control_mode::position)
751 const float min_keypoint_dist = 50;
754 if (not m_viz.start.allFinite())
756 m_viz.start = agent_pos;
759 const Eigen::Vector3f& last_keypoint_pos =
760 m_viz.path.
size() >= 1 ? m_viz.path.back() : m_viz.start;
763 if ((last_keypoint_pos - agent_pos).
norm() > min_keypoint_dist)
765 m_viz.path.push_back(agent_pos);
769 if (not target_reached())
778 for (
unsigned i = 0; i < m_viz.path.size(); ++i)
781 .position(m_viz.path[i])
787 const Eigen::Vector3f
target{m_control_data.target_pos.x(),
788 m_control_data.target_pos.y(),
792 target + Eigen::Vector3f{0, 0, 40})
799 invalidate(m_viz.start);
804 Layer l_vels = arviz.layer(
"velocities");
805 if (m_control_loop.mode != control_mode::none)
807 const float min_velocity = 15;
808 const Eigen::Vector3f from1{agent_pos + Eigen::Vector3f{0, 0, 2000}};
809 const Eigen::Vector3f from2 = from1 + Eigen::Vector3f{0, 0, 200};
810 const Eigen::Vector3f original{vels.target_global.x(), vels.target_global.y(), 0};
811 const Eigen::Vector3f modulated{vels.modulated_global.x(), vels.modulated_global.y(), 0};
813 if (original.norm() > min_velocity)
816 .
fromTo(from1, from1 + original * 5)
821 if (modulated.norm() > min_velocity)
824 .
fromTo(from2, from2 + modulated * 5)
831 Layer l_agnt = arviz.layer(
"agent");
832 if (m_control_loop.mode != control_mode::none)
840 if (m_control_data.agent_safety_margin > 0)
843 .
pose(simox::math::pos_rpy_to_mat4f(agent_pos, -M_PI_2, 0, 0))
845 .radius(m_control_data.agent_safety_margin)
850 arviz.commit({l_prog, l_vels, l_agnt});
859 def->component(m_platform,
"Platform");
860 def->component(m_obstacle_avoidance,
"PlatformObstacleAvoidance");
863 def->optional(m_control_data.adaptive_max_vel_exp,
"adaptive_max_vel_exponent",
864 "Adaptive max vel exponent. This throttles the max_vel adaptively "
865 "depending on the angle between target velocity and modulated velocity. "
867 def->optional(m_control_data.min_vel_near_target,
"min_vel_near_target",
"Velocity in [mm/s] "
868 "the robot should at least set when near the target");
869 def->optional(m_control_data.min_vel_general,
"min_vel_general",
"Velocity in [mm/s] the robot "
870 "should at least set on general");
871 def->optional(m_control_data.pos_near_threshold,
"pos_near_threshold",
"Distance in [mm] after "
872 "which the robot is considered to be near the target for min velocity, "
876 def->optional(m_control_data.kp,
"control.pos.kp");
877 def->optional(m_rot_pid_controller.Kp,
"control.rot.kp");
878 def->optional(m_rot_pid_controller.Ki,
"control.rot.ki");
879 def->optional(m_rot_pid_controller.Kd,
"control.rot.kd");
880 def->optional(m_control_loop.cycle_time,
"control.pose.cycle_time",
"Control loop cycle time.");
883 def->optional(m_control_data.agent_safety_margin,
"doa.agent_safety_margin");