feat: env_monitor_core package with detector plugins
EnvMonitor moves out of move_base2 into its own package. stall, unreachable and contact become boost::dll detector plugins loaded from env_monitor/detectors, each with its own recovery route (route, route_mode step|sequence, on_exhausted lock|abort). Golden test replays the pre-split verdicts bit for bit.
This commit is contained in:
5750
test/data/env_monitor_golden.txt
Normal file
5750
test/data/env_monitor_golden.txt
Normal file
File diff suppressed because it is too large
Load Diff
726
test/env_monitor_golden_test.cpp
Normal file
726
test/env_monitor_golden_test.cpp
Normal file
@@ -0,0 +1,726 @@
|
||||
/*********************************************************************
|
||||
*
|
||||
* Software License Agreement (BSD License)
|
||||
*
|
||||
* Golden test for EnvMonitor: drives the monitor through fixed scenarios and pins EVERY EnvVerdict field on
|
||||
* EVERY cycle against a recorded file. Written before EnvMonitor is moved to env_monitor_core and split into
|
||||
* detector plugins (Test/docs/move_base2/ENV_DETECTOR_PLUGINS_PROMPT.md, sections 6 and 8): the refactor must
|
||||
* reproduce this file bit for bit with the old configuration.
|
||||
*
|
||||
* Modes:
|
||||
* ENV_MONITOR_CORE_GOLDEN_RECORD=1 -> run every scenario and WRITE test/data/env_monitor_golden.txt
|
||||
* (unset) -> run every scenario and COMPARE with that file
|
||||
*
|
||||
* The committed golden file was recorded on 26/11/2026 from the monolithic EnvMonitor that still lived in
|
||||
* move_base2 (commit 605dbc8). Never re-record it to make a refactor pass: a diff here is a behaviour change.
|
||||
*
|
||||
* Only MonitorAdapter touches the EnvMonitor API. After the refactor, only that struct changes.
|
||||
*
|
||||
* Author: DuongTD
|
||||
*********************************************************************/
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include <algorithm>
|
||||
#include <cmath>
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <filesystem>
|
||||
#include <fstream>
|
||||
#include <functional>
|
||||
#include <limits>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
#include <env_monitor_core/env_monitor.h>
|
||||
|
||||
#include "env_monitor_test_config.h"
|
||||
|
||||
#ifndef ENV_MONITOR_CORE_TEST_DATA_DIR
|
||||
#error "ENV_MONITOR_CORE_TEST_DATA_DIR must be defined by CMake"
|
||||
#endif
|
||||
|
||||
namespace
|
||||
{
|
||||
|
||||
using env_monitor_core::EnvSample;
|
||||
using env_monitor_core::EnvVerdict;
|
||||
|
||||
constexpr double kInf = std::numeric_limits<double>::infinity();
|
||||
constexpr double kNaN = std::numeric_limits<double>::quiet_NaN();
|
||||
|
||||
/// Config overrides as YAML paths under `env_monitor:` (e.g. "stall/action") -> value as text. Kept as text
|
||||
/// so that after the refactor the adapter can feed the same list to robot::NodeHandle.
|
||||
using Overrides = std::vector<std::pair<std::string, std::string>>;
|
||||
|
||||
/// Recovery outcome as the scenarios see it; the adapter maps it to the API of the day.
|
||||
enum class Outcome
|
||||
{
|
||||
kSucceededWithPath,
|
||||
kSucceeded,
|
||||
kFailed
|
||||
};
|
||||
|
||||
const char* toString(Outcome o)
|
||||
{
|
||||
switch (o)
|
||||
{
|
||||
case Outcome::kSucceededWithPath:
|
||||
return "succeeded_with_path";
|
||||
case Outcome::kSucceeded:
|
||||
return "succeeded";
|
||||
case Outcome::kFailed:
|
||||
return "failed";
|
||||
}
|
||||
return "?";
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------ adapter
|
||||
|
||||
/// The ONLY place that calls EnvMonitor. Today: env_monitor_core::EnvMonitor built from YAML (detectors as .so).
|
||||
struct MonitorAdapter
|
||||
{
|
||||
bool init(const Overrides& overrides, std::string& error)
|
||||
{
|
||||
env_monitor_core::EnvMonitorConfig cfg;
|
||||
for (const auto& kv : overrides)
|
||||
{
|
||||
if (!apply(cfg, kv.first, kv.second))
|
||||
{
|
||||
error = "unknown override key '" + kv.first + "'";
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return env_monitor_core_test::initializeFrom(monitor, cfg, error);
|
||||
}
|
||||
|
||||
void reset()
|
||||
{
|
||||
monitor.reset();
|
||||
}
|
||||
|
||||
void goalAccepted(const robot::Time& now)
|
||||
{
|
||||
monitor.onGoalAccepted(now);
|
||||
}
|
||||
|
||||
void planApplied(const robot::Time& now)
|
||||
{
|
||||
monitor.onPlanApplied(now);
|
||||
}
|
||||
|
||||
/// @param own_route the finished route was started by the contact flag (today: `from_contact`).
|
||||
void recoveryFinished(const robot::Time& now, Outcome outcome, bool own_route)
|
||||
{
|
||||
env_monitor_core::EnvRecoveryOutcome o = env_monitor_core::EnvRecoveryOutcome::kFailed;
|
||||
switch (outcome)
|
||||
{
|
||||
case Outcome::kSucceededWithPath:
|
||||
o = env_monitor_core::EnvRecoveryOutcome::kSucceededWithPath;
|
||||
break;
|
||||
case Outcome::kSucceeded:
|
||||
o = env_monitor_core::EnvRecoveryOutcome::kSucceeded;
|
||||
break;
|
||||
case Outcome::kFailed:
|
||||
o = env_monitor_core::EnvRecoveryOutcome::kFailed;
|
||||
break;
|
||||
}
|
||||
// control_loop.cpp names the source detector only on success; failure passes none. "contact" is the built-in
|
||||
// detector name the scenarios mean by own_route.
|
||||
monitor.onRecoveryFinished(now, o, outcome != Outcome::kFailed && own_route ? "contact" : "");
|
||||
}
|
||||
|
||||
EnvVerdict update(const EnvSample& s)
|
||||
{
|
||||
return monitor.update(s);
|
||||
}
|
||||
|
||||
static bool apply(env_monitor_core::EnvMonitorConfig& c, const std::string& key, const std::string& value)
|
||||
{
|
||||
const auto num = [&value]() { return std::stod(value); };
|
||||
const auto flag = [&value]() { return value == "true"; };
|
||||
if (key == "enabled") c.enabled = flag();
|
||||
else if (key == "stall/enabled") c.stall.enabled = flag();
|
||||
else if (key == "stall/action") c.stall.action = value;
|
||||
else if (key == "stall/disp_min") c.stall.disp_min = num();
|
||||
else if (key == "stall/disp_window_s") c.stall.disp_window_s = num();
|
||||
else if (key == "stall/disp_min_commanded") c.stall.disp_min_commanded = num();
|
||||
else if (key == "stall/idle_window_s") c.stall.idle_window_s = num();
|
||||
else if (key == "stall/confine_window_s") c.stall.confine_window_s = num();
|
||||
else if (key == "stall/confine_radius") c.stall.confine_radius = num();
|
||||
else if (key == "stall/confine_cmd_factor") c.stall.confine_cmd_factor = num();
|
||||
else if (key == "stall/min_commanded_since_goal") c.stall.min_commanded_since_goal = num();
|
||||
else if (key == "unreachable/enabled") c.unreachable.enabled = flag();
|
||||
else if (key == "unreachable/action") c.unreachable.action = value;
|
||||
else if (key == "contact/enabled") c.contact.enabled = flag();
|
||||
else if (key == "contact/action") c.contact.action = value;
|
||||
else return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
env_monitor_core::EnvMonitor monitor;
|
||||
};
|
||||
|
||||
// ----------------------------------------------------------------------------------------- recording
|
||||
|
||||
std::string num(double d)
|
||||
{
|
||||
if (std::isnan(d))
|
||||
{
|
||||
return "nan"; // glibc may print "-nan"; the sign of a NaN carries no meaning here
|
||||
}
|
||||
if (std::isinf(d))
|
||||
{
|
||||
return d > 0.0 ? "inf" : "-inf";
|
||||
}
|
||||
char buf[40];
|
||||
std::snprintf(buf, sizeof(buf), "%.17g", d); // exact round trip
|
||||
return buf;
|
||||
}
|
||||
|
||||
std::string formatVerdict(const EnvVerdict& v)
|
||||
{
|
||||
std::ostringstream o;
|
||||
const auto b = [](bool x) { return x ? "1" : "0"; };
|
||||
o << "no_progress=" << b(v.no_progress) << " path_blocked=" << b(v.path_blocked) << " collision=" << b(v.collision)
|
||||
<< " brake=" << b(v.brake) << " v_cap=" << num(v.v_cap) << " stall_detected=" << b(v.stall_detected)
|
||||
<< " unreachable_detected=" << b(v.unreachable_detected) << " contact_imminent=" << b(v.contact_imminent)
|
||||
<< " contact_standdown=" << b(v.contact_standdown) << " cost_jump=" << b(v.cost_jump)
|
||||
<< " pose_jump=" << b(v.pose_jump) << " pose_jump_restart=" << b(v.pose_jump_restart)
|
||||
<< " pose_jump_dist=" << num(v.pose_jump_dist) << " progress_proven=" << b(v.progress_proven)
|
||||
<< " stall_muted=" << b(v.stall_muted) << " net_disp=" << num(v.net_disp) << " wandering=" << b(v.wandering)
|
||||
<< " idle=" << b(v.idle) << " confined=" << b(v.confined) << " region_radius=" << num(v.region_radius)
|
||||
<< " unreachable_muted=" << b(v.unreachable_muted) << " eff_geo=" << num(v.eff_geo)
|
||||
<< " cmd_window=" << num(v.cmd_window) << " brake_dist=" << num(v.brake_dist)
|
||||
<< " replan_budget_left=" << num(v.replan_budget_left) << " event=\"" << (v.event ? v.event : "") << "\"";
|
||||
return o.str();
|
||||
}
|
||||
|
||||
struct Recorder
|
||||
{
|
||||
bool enabled = true;
|
||||
std::vector<std::string> lines;
|
||||
|
||||
void verdict(const std::string& scenario, int cycle, const EnvVerdict& v)
|
||||
{
|
||||
if (enabled)
|
||||
{
|
||||
lines.push_back(scenario + " " + std::to_string(cycle) + " " + formatVerdict(v));
|
||||
}
|
||||
}
|
||||
|
||||
void mark(const std::string& scenario, int cycle, const std::string& what)
|
||||
{
|
||||
if (enabled)
|
||||
{
|
||||
lines.push_back(scenario + " " + std::to_string(cycle) + " # " + what);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
// ------------------------------------------------------------------------------------------- driver
|
||||
|
||||
/// Same motion model as the Harness in env_monitor_test.cpp: 20 Hz cycles, heavy tick every 4th cycle (5 Hz),
|
||||
/// robot along +x at v_cmd; `cost_rate` = d(cost_to_go)/dt (-v = full progress, 0 = no progress).
|
||||
class Drive
|
||||
{
|
||||
public:
|
||||
Drive(Recorder& rec, std::string name, const Overrides& overrides) : rec_(rec), name_(std::move(name))
|
||||
{
|
||||
std::string error;
|
||||
if (!adapter_.init(overrides, error))
|
||||
{
|
||||
ADD_FAILURE() << name_ << ": init failed: " << error;
|
||||
}
|
||||
goal();
|
||||
plan();
|
||||
}
|
||||
|
||||
robot::Time now() const
|
||||
{
|
||||
return robot::Time(t);
|
||||
}
|
||||
|
||||
void goal()
|
||||
{
|
||||
adapter_.goalAccepted(now());
|
||||
rec_.mark(name_, cycle, "goal_accepted");
|
||||
}
|
||||
|
||||
void plan()
|
||||
{
|
||||
adapter_.planApplied(now());
|
||||
rec_.mark(name_, cycle, "plan_applied");
|
||||
}
|
||||
|
||||
void recovery(Outcome outcome, bool own_route = false)
|
||||
{
|
||||
adapter_.recoveryFinished(now(), outcome, own_route);
|
||||
rec_.mark(name_, cycle, std::string("recovery_finished ") + toString(outcome) + (own_route ? " own" : ""));
|
||||
}
|
||||
|
||||
void reset()
|
||||
{
|
||||
adapter_.reset();
|
||||
rec_.mark(name_, cycle, "reset");
|
||||
}
|
||||
|
||||
const EnvVerdict& step(double v_cmd, double cost_rate, bool move = true)
|
||||
{
|
||||
t += dt;
|
||||
++cycle;
|
||||
if (orbit_radius > 0.0)
|
||||
{
|
||||
orbit_angle += v_cmd * dt / orbit_radius;
|
||||
x = orbit_x0 + orbit_radius * std::cos(orbit_angle);
|
||||
y = orbit_radius * std::sin(orbit_angle);
|
||||
}
|
||||
else if (wander_span > 0.0)
|
||||
{
|
||||
x = wander_x0 + wander_span * std::sin(2.0 * M_PI * (t - 100.0) / 4.0);
|
||||
}
|
||||
else if (move)
|
||||
{
|
||||
x += v_cmd * dt;
|
||||
}
|
||||
cost += cost_rate * dt;
|
||||
EnvSample s;
|
||||
s.now = now();
|
||||
s.dt = dt;
|
||||
s.costmap_current = costmap_current;
|
||||
s.has_plan = has_plan;
|
||||
s.robot_x = pose_unavailable ? kNaN : x;
|
||||
s.robot_y = pose_unavailable ? kNaN : y;
|
||||
s.v_cmd = v_cmd;
|
||||
s.v_meas = std::isnan(v_meas_override) ? (move ? v_cmd : 0.0) : v_meas_override;
|
||||
s.v_controller = v_controller_override;
|
||||
s.d_goal = d_goal;
|
||||
s.geometry_valid = (cycle % 4 == 0);
|
||||
s.cost_conclusive = conclusive;
|
||||
s.reachable = reachable;
|
||||
s.cost_to_go = reachable ? cost : kInf;
|
||||
s.edt_body_min = edt_body;
|
||||
s.contact_conclusive = contact_conclusive;
|
||||
s.traj_contact_dist = contact_dist;
|
||||
last = adapter_.update(s);
|
||||
rec_.verdict(name_, cycle, last);
|
||||
return last;
|
||||
}
|
||||
|
||||
void run(double seconds, double v_cmd, double cost_rate, bool move = true)
|
||||
{
|
||||
const int n = static_cast<int>(std::lround(seconds / dt));
|
||||
for (int i = 0; i < n; ++i)
|
||||
{
|
||||
step(v_cmd, cost_rate, move);
|
||||
}
|
||||
}
|
||||
|
||||
double t = 100.0;
|
||||
const double dt = 0.05;
|
||||
int cycle = 0;
|
||||
double x = 0.0;
|
||||
double y = 0.0;
|
||||
double cost = 12.0;
|
||||
double edt_body = 1.0;
|
||||
bool reachable = true;
|
||||
bool conclusive = true;
|
||||
bool has_plan = true;
|
||||
bool costmap_current = true;
|
||||
bool pose_unavailable = false;
|
||||
double d_goal = 12.0;
|
||||
double contact_dist = kInf;
|
||||
bool contact_conclusive = true;
|
||||
double v_meas_override = kNaN;
|
||||
double v_controller_override = kNaN;
|
||||
double wander_span = 0.0;
|
||||
double wander_x0 = 0.0;
|
||||
double orbit_radius = 0.0;
|
||||
double orbit_x0 = 0.0;
|
||||
double orbit_angle = 0.0;
|
||||
EnvVerdict last;
|
||||
|
||||
private:
|
||||
Recorder& rec_;
|
||||
std::string name_;
|
||||
MonitorAdapter adapter_;
|
||||
};
|
||||
|
||||
const Overrides kRecoverAll = {
|
||||
{ "stall/action", "recover" },
|
||||
{ "unreachable/action", "recover" },
|
||||
{ "contact/action", "recover" },
|
||||
{ "stall/disp_min", "0.0" },
|
||||
};
|
||||
|
||||
Overrides recoverWithDispGate(Overrides extra = {})
|
||||
{
|
||||
Overrides o = {
|
||||
{ "stall/action", "recover" }, { "unreachable/action", "recover" }, { "contact/action", "recover" },
|
||||
{ "stall/disp_min", "0.6" }, { "stall/disp_window_s", "6.0" }, { "stall/disp_min_commanded", "1.0" },
|
||||
{ "stall/idle_window_s", "10.0" }, { "stall/confine_window_s", "8.0" }, { "stall/confine_radius", "1.2" },
|
||||
{ "stall/confine_cmd_factor", "1.5" },
|
||||
};
|
||||
o.insert(o.end(), extra.begin(), extra.end());
|
||||
return o;
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------------------------- scenarios
|
||||
|
||||
/// First cycle at which stall fires in the base "commanded without progress" run. Used to place a contact brake
|
||||
/// on the very same cycle, so the golden pins which detector's `event` wins.
|
||||
int firstStallCycle()
|
||||
{
|
||||
Recorder silent;
|
||||
silent.enabled = false;
|
||||
Drive d(silent, "probe", kRecoverAll);
|
||||
d.run(3.0, 0.5, -0.5);
|
||||
for (int i = 0; i < 200; ++i)
|
||||
{
|
||||
if (d.step(0.5, 0.0).stall_detected)
|
||||
{
|
||||
return d.cycle;
|
||||
}
|
||||
}
|
||||
ADD_FAILURE() << "probe: stall never fired";
|
||||
return -1;
|
||||
}
|
||||
|
||||
void runAllScenarios(Recorder& rec)
|
||||
{
|
||||
{
|
||||
Drive d(rec, "steady_progress", kRecoverAll);
|
||||
d.run(20.0, 0.5, -0.5);
|
||||
}
|
||||
{
|
||||
Drive d(rec, "stall_geodesic_and_hysteresis", kRecoverAll);
|
||||
d.run(3.0, 0.5, -0.5);
|
||||
d.run(6.0, 0.5, 0.0);
|
||||
d.run(4.0, 0.5, -0.175); // eff 0.35: dead band, holds
|
||||
d.run(4.0, 0.5, -0.4); // eff 0.8: clears
|
||||
d.run(10.0, 0.0, 0.0); // planner hold: waiting, not stalling
|
||||
}
|
||||
{
|
||||
Drive d(rec, "stall_log_action", { { "stall/action", "log" }, { "stall/disp_min", "0.0" } });
|
||||
d.run(3.0, 0.5, -0.5);
|
||||
d.run(6.0, 0.5, 0.0);
|
||||
}
|
||||
{
|
||||
Drive d(rec, "stall_replan_budget_escalation", kRecoverAll);
|
||||
d.run(3.0, 0.5, -0.5);
|
||||
d.run(6.0, 0.5, 0.0);
|
||||
d.recovery(Outcome::kSucceededWithPath);
|
||||
d.plan();
|
||||
d.run(1.5, 0.5, 0.0);
|
||||
d.run(4.0, 0.5, 0.0);
|
||||
d.recovery(Outcome::kSucceededWithPath);
|
||||
d.plan();
|
||||
d.run(1.5, 0.5, 0.0);
|
||||
d.run(3.0, 0.5, 0.0);
|
||||
d.recovery(Outcome::kSucceeded); // non-path success: no budget change, chains reset
|
||||
d.run(3.0, 0.5, 0.0);
|
||||
}
|
||||
{
|
||||
Drive d(rec, "stall_failed_recovery_backoff", kRecoverAll);
|
||||
d.run(3.0, 0.5, -0.5);
|
||||
d.run(6.0, 0.5, 0.0);
|
||||
d.recovery(Outcome::kFailed);
|
||||
d.run(2.9, 0.5, 0.0);
|
||||
d.run(2.0, 0.5, 0.0);
|
||||
}
|
||||
{
|
||||
Drive d(rec, "stall_wheel_slip", kRecoverAll);
|
||||
d.run(3.0, 0.5, -0.5);
|
||||
d.conclusive = false;
|
||||
d.v_meas_override = 0.0;
|
||||
d.run(5.0, 0.4, 0.0, false);
|
||||
}
|
||||
{
|
||||
Drive d(rec, "stall_near_goal_not_measured", kRecoverAll);
|
||||
d.run(3.0, 0.5, -0.5);
|
||||
d.d_goal = 0.3;
|
||||
d.run(8.0, 0.3, 0.0);
|
||||
}
|
||||
{
|
||||
Drive d(rec, "stall_idle_gate", recoverWithDispGate({ { "stall/min_commanded_since_goal", "0.1" } }));
|
||||
d.wander_span = 0.05;
|
||||
d.run(14.0, 0.05, 0.0);
|
||||
}
|
||||
{
|
||||
Drive d(rec, "stall_confined_orbit", recoverWithDispGate({ { "stall/min_commanded_since_goal", "0.1" } }));
|
||||
d.orbit_radius = 0.6;
|
||||
d.run(14.0, 0.3, 0.0);
|
||||
}
|
||||
{
|
||||
Drive d(rec, "stall_wandering_box_and_new_plan", recoverWithDispGate());
|
||||
d.wander_span = 0.25;
|
||||
d.run(12.0, 0.4, 0.0);
|
||||
d.recovery(Outcome::kSucceededWithPath);
|
||||
d.plan();
|
||||
d.run(10.0, 0.4, 0.0);
|
||||
}
|
||||
{
|
||||
Drive d(rec, "stall_displacement_gate_mutes", recoverWithDispGate());
|
||||
d.run(9.0, 0.5, 0.0); // cost flat but x advances
|
||||
}
|
||||
{
|
||||
Drive d(rec, "pose_jump_restart_and_cooldown", recoverWithDispGate());
|
||||
d.run(7.0, 0.3, -0.3);
|
||||
d.x -= 1.2; // relocalization
|
||||
d.step(0.3, -0.3);
|
||||
d.step(0.3, -0.3);
|
||||
d.x += 0.2; // ordinary localization correction, not a jump
|
||||
d.step(0.3, -0.3);
|
||||
d.run(3.0, 0.3, -0.3);
|
||||
d.x -= 1.0; // second jump inside the restart cooldown
|
||||
d.step(0.3, -0.3);
|
||||
d.pose_unavailable = true; // pose gap: the next valid pose may move by elapsed time x speed
|
||||
d.run(1.0, 0.3, -0.3);
|
||||
d.pose_unavailable = false;
|
||||
d.run(2.0, 0.3, -0.3);
|
||||
}
|
||||
{
|
||||
Drive d(rec, "unreachable_fire_and_clear", kRecoverAll);
|
||||
d.run(2.0, 0.5, -0.5);
|
||||
d.reachable = false;
|
||||
d.run(3.0, 0.3, 0.0);
|
||||
d.reachable = true;
|
||||
d.run(0.4, 0.3, -0.3);
|
||||
d.reachable = false;
|
||||
d.edt_body = 0.1; // inside the band: no fire
|
||||
d.run(3.0, 0.3, 0.0);
|
||||
}
|
||||
{
|
||||
Drive d(rec, "unreachable_plan_born_unreachable", kRecoverAll);
|
||||
d.reachable = false;
|
||||
d.run(1.5, 0.3, 0.0);
|
||||
d.run(5.0, 0.3, 0.0);
|
||||
d.reachable = true;
|
||||
d.run(0.4, 0.3, -0.3);
|
||||
d.reachable = false;
|
||||
d.run(2.0, 0.3, 0.0);
|
||||
}
|
||||
{
|
||||
Drive d(rec, "unreachable_replan_budget_and_backoff", kRecoverAll);
|
||||
d.run(2.0, 0.5, -0.5);
|
||||
d.recovery(Outcome::kSucceededWithPath);
|
||||
d.plan();
|
||||
d.run(1.2, 0.3, -0.3);
|
||||
d.reachable = false;
|
||||
d.run(3.0, 0.3, 0.0);
|
||||
d.run(5.0, 0.3, 0.0); // budget used up: fires
|
||||
d.recovery(Outcome::kFailed);
|
||||
d.run(3.0, 0.3, 0.0); // backoff 2 s, then again
|
||||
}
|
||||
{
|
||||
Drive d(rec, "contact_brake_flag_release", kRecoverAll);
|
||||
d.contact_dist = 0.5;
|
||||
d.step(0.5, -0.5);
|
||||
d.step(0.5, -0.5);
|
||||
d.run(0.15, 0.0, 0.0, false);
|
||||
d.step(0.0, 0.0, false);
|
||||
d.contact_dist = 0.15;
|
||||
d.run(1.5, 0.3, 0.0, false);
|
||||
d.contact_dist = kInf;
|
||||
d.run(0.3, 0.0, 0.0, false);
|
||||
d.contact_dist = 0.3; // inconclusive sweep neither brakes nor releases
|
||||
d.run(0.2, 0.5, -0.5);
|
||||
d.contact_conclusive = false;
|
||||
d.run(0.2, 0.0, 0.0, false);
|
||||
d.contact_conclusive = true;
|
||||
d.contact_dist = 2.0; // far contact only caps speed
|
||||
d.run(1.0, 0.5, -0.5);
|
||||
}
|
||||
{
|
||||
Drive d(rec, "contact_jitter_and_pivot", kRecoverAll);
|
||||
d.contact_dist = 0.08;
|
||||
d.run(2.0, 0.07, 0.0, false); // pivoting next to an obstacle: controller's business
|
||||
d.v_controller_override = 0.3;
|
||||
for (int i = 0; i < 40; ++i)
|
||||
{
|
||||
d.contact_dist = (i % 2 == 0) ? 0.08 : 0.25;
|
||||
d.step(0.0, 0.0, false);
|
||||
}
|
||||
d.contact_dist = kInf;
|
||||
d.run(0.5, 0.0, 0.0, false);
|
||||
}
|
||||
{
|
||||
Drive d(rec, "contact_standdown_after_own_detour", kRecoverAll);
|
||||
d.contact_dist = 0.10;
|
||||
d.v_controller_override = 0.3;
|
||||
d.run(1.5, 0.0, 0.0, false);
|
||||
d.recovery(Outcome::kSucceededWithPath, true);
|
||||
d.step(0.0, 0.0, false);
|
||||
d.step(0.0, 0.0, false);
|
||||
d.contact_dist = 0.07;
|
||||
d.run(2.0, 0.0, 0.0, false);
|
||||
d.x += 0.35;
|
||||
d.step(0.2, 0.0, true);
|
||||
d.step(0.2, 0.0, true);
|
||||
d.step(0.2, 0.0, true);
|
||||
d.run(1.5, 0.0, 0.0, false);
|
||||
d.recovery(Outcome::kSucceededWithPath, true);
|
||||
d.step(0.0, 0.0, false);
|
||||
d.step(0.0, 0.0, false);
|
||||
d.contact_dist = kInf; // trajectory stays clear: re-arm
|
||||
d.run(0.3, 0.1, 0.0, false);
|
||||
}
|
||||
{
|
||||
Drive d(rec, "contact_detour_from_other_trigger", kRecoverAll);
|
||||
d.recovery(Outcome::kSucceededWithPath, false);
|
||||
d.contact_dist = 0.10;
|
||||
d.v_controller_override = 0.3;
|
||||
d.run(1.5, 0.0, 0.0, false);
|
||||
}
|
||||
{
|
||||
Drive d(rec, "gate_no_plan_or_stale_costmap", kRecoverAll);
|
||||
d.run(3.0, 0.5, -0.5);
|
||||
d.run(6.0, 0.5, 0.0);
|
||||
d.costmap_current = false;
|
||||
d.run(1.0, 0.5, 0.0);
|
||||
d.costmap_current = true;
|
||||
d.has_plan = false;
|
||||
d.contact_dist = 0.3; // contact still runs without a plan
|
||||
d.run(1.0, 0.5, 0.0);
|
||||
d.has_plan = true;
|
||||
d.contact_dist = kInf;
|
||||
d.run(6.0, 0.5, 0.0);
|
||||
}
|
||||
{
|
||||
// Contact starts braking on the very cycle stall fires: pins which `event` wins (today: stall, it runs later).
|
||||
const int fire = firstStallCycle();
|
||||
Drive d(rec, "event_overlap_contact_and_stall", kRecoverAll);
|
||||
d.run(3.0, 0.5, -0.5);
|
||||
while (d.cycle < fire + 20)
|
||||
{
|
||||
if (d.cycle == fire - 2)
|
||||
{
|
||||
d.contact_dist = 0.5; // imminent at fire-1, brake (confirm 2) at fire
|
||||
}
|
||||
d.step(0.5, 0.0);
|
||||
}
|
||||
}
|
||||
{
|
||||
Drive d(rec, "goal_plan_reset_midrun", kRecoverAll);
|
||||
d.run(3.0, 0.5, -0.5);
|
||||
d.run(2.0, 0.5, 0.0);
|
||||
d.goal();
|
||||
d.run(2.0, 0.5, 0.0);
|
||||
d.plan();
|
||||
d.run(0.8, 0.5, 0.0); // inside grace
|
||||
d.run(5.0, 0.5, 0.0);
|
||||
d.reset();
|
||||
d.run(2.0, 0.5, -0.5);
|
||||
d.goal();
|
||||
d.plan();
|
||||
d.run(3.0, 0.5, -0.5);
|
||||
}
|
||||
{
|
||||
Drive d(rec, "disabled_monitor", { { "enabled", "false" }, { "stall/action", "recover" } });
|
||||
d.contact_dist = 0.1;
|
||||
d.reachable = false;
|
||||
d.run(3.0, 0.5, 0.0);
|
||||
}
|
||||
{
|
||||
Drive d(rec, "only_unreachable_enabled",
|
||||
{ { "stall/action", "recover" }, { "unreachable/action", "recover" }, { "contact/action", "recover" },
|
||||
{ "stall/disp_min", "0.0" }, { "stall/enabled", "false" }, { "contact/enabled", "false" } });
|
||||
d.run(3.0, 0.5, -0.5);
|
||||
d.contact_dist = 0.1;
|
||||
d.reachable = false;
|
||||
d.run(6.0, 0.5, 0.0);
|
||||
}
|
||||
}
|
||||
|
||||
std::string goldenPath()
|
||||
{
|
||||
return std::string(ENV_MONITOR_CORE_TEST_DATA_DIR) + "/env_monitor_golden.txt";
|
||||
}
|
||||
|
||||
/// First differing space-separated token, for a readable failure message.
|
||||
std::string firstDiff(const std::string& expected, const std::string& actual)
|
||||
{
|
||||
std::istringstream e(expected);
|
||||
std::istringstream a(actual);
|
||||
std::string te;
|
||||
std::string ta;
|
||||
while (true)
|
||||
{
|
||||
const bool he = static_cast<bool>(e >> te);
|
||||
const bool ha = static_cast<bool>(a >> ta);
|
||||
if (!he && !ha)
|
||||
{
|
||||
return "(lines differ only in spacing)";
|
||||
}
|
||||
if (!he || !ha || te != ta)
|
||||
{
|
||||
return "expected '" + (he ? te : std::string("<end>")) + "' got '" + (ha ? ta : std::string("<end>")) + "'";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
TEST(EnvMonitorGolden, ScenariosAreDeterministic)
|
||||
{
|
||||
Recorder a;
|
||||
Recorder b;
|
||||
runAllScenarios(a);
|
||||
runAllScenarios(b);
|
||||
ASSERT_EQ(a.lines.size(), b.lines.size());
|
||||
for (std::size_t i = 0; i < a.lines.size(); ++i)
|
||||
{
|
||||
ASSERT_EQ(a.lines[i], b.lines[i]) << "line " << i + 1;
|
||||
}
|
||||
}
|
||||
|
||||
TEST(EnvMonitorGolden, MatchesTheRecordedVerdicts)
|
||||
{
|
||||
Recorder rec;
|
||||
runAllScenarios(rec);
|
||||
ASSERT_FALSE(rec.lines.empty());
|
||||
|
||||
const char* record = std::getenv("ENV_MONITOR_CORE_GOLDEN_RECORD");
|
||||
if (record != nullptr && std::string(record) == "1")
|
||||
{
|
||||
std::filesystem::create_directories(ENV_MONITOR_CORE_TEST_DATA_DIR);
|
||||
std::ofstream out(goldenPath(), std::ios::trunc);
|
||||
ASSERT_TRUE(out.good()) << "cannot write " << goldenPath();
|
||||
for (const auto& line : rec.lines)
|
||||
{
|
||||
out << line << '\n';
|
||||
}
|
||||
std::printf("[golden] recorded %zu lines to %s\n", rec.lines.size(), goldenPath().c_str());
|
||||
return;
|
||||
}
|
||||
|
||||
std::ifstream in(goldenPath());
|
||||
ASSERT_TRUE(in.good()) << "golden file " << goldenPath()
|
||||
<< " is missing: run this test once with ENV_MONITOR_CORE_GOLDEN_RECORD=1 on the reference code";
|
||||
std::vector<std::string> expected;
|
||||
for (std::string line; std::getline(in, line);)
|
||||
{
|
||||
expected.push_back(line);
|
||||
}
|
||||
|
||||
int mismatches = 0;
|
||||
const std::size_t n = std::min(expected.size(), rec.lines.size());
|
||||
for (std::size_t i = 0; i < n && mismatches < 20; ++i)
|
||||
{
|
||||
if (expected[i] != rec.lines[i])
|
||||
{
|
||||
++mismatches;
|
||||
ADD_FAILURE() << "line " << i + 1 << ": " << firstDiff(expected[i], rec.lines[i]) << "\n expected: "
|
||||
<< expected[i] << "\n actual: " << rec.lines[i];
|
||||
}
|
||||
}
|
||||
EXPECT_EQ(expected.size(), rec.lines.size()) << "line count differs";
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
testing::InitGoogleTest(&argc, argv);
|
||||
return RUN_ALL_TESTS();
|
||||
}
|
||||
379
test/env_monitor_loading_test.cpp
Normal file
379
test/env_monitor_loading_test.cpp
Normal file
@@ -0,0 +1,379 @@
|
||||
/*********************************************************************
|
||||
*
|
||||
* Software License Agreement (BSD License)
|
||||
*
|
||||
* env_monitor_core — loading detectors from YAML (Test/docs/move_base2/ENV_DETECTOR_PLUGINS_PROMPT.md §4, §6):
|
||||
* default list, YAML order, unknown type, stale / missing ABI, invalid parameters, merge of brake and v_cap.
|
||||
*
|
||||
* Author: DuongTD
|
||||
*********************************************************************/
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include <cmath>
|
||||
#include <limits>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include <yaml-cpp/yaml.h>
|
||||
|
||||
#include <env_monitor_core/env_monitor.h>
|
||||
|
||||
#include "env_monitor_test_config.h"
|
||||
|
||||
namespace
|
||||
{
|
||||
|
||||
using env_monitor_core::EnvMonitor;
|
||||
using env_monitor_core::EnvMonitorConfig;
|
||||
using env_monitor_core::EnvSample;
|
||||
using env_monitor_core::EnvVerdict;
|
||||
using env_monitor_core_test::freshNodeHandle;
|
||||
using env_monitor_core::toYaml;
|
||||
|
||||
constexpr double kInf = std::numeric_limits<double>::infinity();
|
||||
|
||||
EnvMonitorConfig recoverAll()
|
||||
{
|
||||
EnvMonitorConfig cfg;
|
||||
cfg.stall.action = "recover";
|
||||
cfg.unreachable.action = "recover";
|
||||
cfg.contact.action = "recover";
|
||||
cfg.stall.disp_min = 0.0;
|
||||
return cfg;
|
||||
}
|
||||
|
||||
YAML::Node entry(const std::string& name, const std::string& type)
|
||||
{
|
||||
YAML::Node e;
|
||||
e["name"] = name;
|
||||
e["type"] = type;
|
||||
return e;
|
||||
}
|
||||
|
||||
/// @p env is the `env_monitor` block; @p list the `detectors` entries (empty = leave the key out).
|
||||
bool init(EnvMonitor& m, YAML::Node env, const std::vector<YAML::Node>& list, std::string& error)
|
||||
{
|
||||
robot::NodeHandle nh = freshNodeHandle();
|
||||
YAML::Node lib;
|
||||
lib["library_path"] = "libenv_monitor_core_test_stale_abi_detector";
|
||||
nh.setParam("StaleAbiDetector", lib);
|
||||
nh.setParam("NoAbiDetector", lib);
|
||||
if (!list.empty())
|
||||
{
|
||||
YAML::Node seq(YAML::NodeType::Sequence);
|
||||
for (const auto& e : list)
|
||||
{
|
||||
seq.push_back(e);
|
||||
}
|
||||
env["detectors"] = seq;
|
||||
}
|
||||
nh.setParam("env_monitor", env);
|
||||
return m.initialize(nh, error);
|
||||
}
|
||||
|
||||
/// Minimal cycle driver: 20 Hz, heavy tick every 4th cycle, robot along +x.
|
||||
struct Drive
|
||||
{
|
||||
explicit Drive(EnvMonitor& monitor) : m(monitor)
|
||||
{
|
||||
m.onGoalAccepted(robot::Time(t));
|
||||
m.onPlanApplied(robot::Time(t));
|
||||
}
|
||||
|
||||
const EnvVerdict& step(double v_cmd, double cost_rate)
|
||||
{
|
||||
t += 0.05;
|
||||
++cycle;
|
||||
x += v_cmd * 0.05;
|
||||
cost += cost_rate * 0.05;
|
||||
EnvSample s;
|
||||
s.now = robot::Time(t);
|
||||
s.dt = 0.05;
|
||||
s.has_plan = true;
|
||||
s.robot_x = x;
|
||||
s.v_cmd = v_cmd;
|
||||
s.v_meas = v_cmd;
|
||||
s.d_goal = 12.0;
|
||||
s.geometry_valid = (cycle % 4 == 0);
|
||||
s.cost_conclusive = true;
|
||||
s.reachable = true;
|
||||
s.cost_to_go = cost;
|
||||
s.edt_body_min = 1.0;
|
||||
s.contact_conclusive = true;
|
||||
s.traj_contact_dist = contact;
|
||||
last = m.update(s);
|
||||
return last;
|
||||
}
|
||||
|
||||
EnvMonitor& m;
|
||||
double t = 100.0;
|
||||
int cycle = 0;
|
||||
double x = 0.0;
|
||||
double cost = 12.0;
|
||||
double contact = kInf;
|
||||
EnvVerdict last;
|
||||
};
|
||||
|
||||
std::size_t at(const std::string& text, const std::string& needle)
|
||||
{
|
||||
return text.find(needle);
|
||||
}
|
||||
|
||||
TEST(EnvMonitorLoading, MissingDetectorsListLoadsTheThreeBuiltInsInTheMonolithOrder)
|
||||
{
|
||||
EnvMonitor m;
|
||||
std::string error;
|
||||
ASSERT_TRUE(init(m, toYaml(recoverAll()), {}, error)) << error;
|
||||
EXPECT_EQ(3u, m.detectorCount());
|
||||
const std::string d = m.describe();
|
||||
ASSERT_NE(std::string::npos, at(d, "contact=ContactDetector")) << d;
|
||||
ASSERT_NE(std::string::npos, at(d, "stall=StallDetector")) << d;
|
||||
ASSERT_NE(std::string::npos, at(d, "unreachable=UnreachableDetector")) << d;
|
||||
EXPECT_LT(at(d, "contact=ContactDetector"), at(d, "stall=StallDetector"));
|
||||
EXPECT_LT(at(d, "stall=StallDetector"), at(d, "unreachable=UnreachableDetector"));
|
||||
EXPECT_TRUE(m.armed());
|
||||
EXPECT_TRUE(m.needs().geometry);
|
||||
EXPECT_TRUE(m.needs().sweep);
|
||||
}
|
||||
|
||||
TEST(EnvMonitorLoading, YamlListDecidesWhichDetectorsRunAndInWhatOrder)
|
||||
{
|
||||
EnvMonitor m;
|
||||
std::string error;
|
||||
ASSERT_TRUE(init(m, toYaml(recoverAll()),
|
||||
{ entry("unreachable", "UnreachableDetector"), entry("contact", "ContactDetector") }, error))
|
||||
<< error;
|
||||
EXPECT_EQ(2u, m.detectorCount());
|
||||
const std::string d = m.describe();
|
||||
EXPECT_EQ(std::string::npos, at(d, "StallDetector")) << d;
|
||||
EXPECT_LT(at(d, "unreachable=UnreachableDetector"), at(d, "contact=ContactDetector")) << d;
|
||||
}
|
||||
|
||||
TEST(EnvMonitorLoading, UnknownTypeIsSkippedAndTheOthersStillRun)
|
||||
{
|
||||
EnvMonitor m;
|
||||
std::string error;
|
||||
ASSERT_TRUE(init(m, toYaml(recoverAll()), { entry("ghost", "GhostDetector"), entry("contact", "ContactDetector") },
|
||||
error))
|
||||
<< error;
|
||||
EXPECT_TRUE(error.empty());
|
||||
EXPECT_EQ(1u, m.detectorCount());
|
||||
Drive d(m);
|
||||
d.contact = 0.10;
|
||||
d.step(0.5, -0.5);
|
||||
d.step(0.5, -0.5);
|
||||
EXPECT_TRUE(d.last.brake) << "the loaded contact detector must still brake";
|
||||
}
|
||||
|
||||
TEST(EnvMonitorLoading, StaleOrMissingAbiIsRefused)
|
||||
{
|
||||
EnvMonitor m;
|
||||
std::string error;
|
||||
ASSERT_TRUE(init(m, toYaml(recoverAll()),
|
||||
{ entry("stale", "StaleAbiDetector"), entry("noabi", "NoAbiDetector"),
|
||||
entry("contact", "ContactDetector") },
|
||||
error))
|
||||
<< error;
|
||||
EXPECT_EQ(1u, m.detectorCount());
|
||||
EXPECT_EQ(std::string::npos, at(m.describe(), "Abi")) << m.describe();
|
||||
Drive d(m);
|
||||
d.step(0.0, 0.0);
|
||||
EXPECT_FALSE(d.last.brake) << "a refused plugin (it always brakes) must never run";
|
||||
}
|
||||
|
||||
TEST(EnvMonitorLoading, InvalidDetectorParametersFailTheWholeInitialize)
|
||||
{
|
||||
EnvMonitorConfig cfg = recoverAll();
|
||||
cfg.stall.eff_off = 0.1; // < eff_on 0.2
|
||||
EnvMonitor m;
|
||||
std::string error;
|
||||
EXPECT_FALSE(init(m, toYaml(cfg), {}, error));
|
||||
EXPECT_NE(std::string::npos, error.find("env_monitor/stall")) << error;
|
||||
EXPECT_FALSE(m.configured());
|
||||
EXPECT_EQ(0u, m.detectorCount());
|
||||
}
|
||||
|
||||
TEST(EnvMonitorLoading, DisabledMonitorLoadsNothing)
|
||||
{
|
||||
EnvMonitorConfig cfg = recoverAll();
|
||||
cfg.enabled = false;
|
||||
EnvMonitor m;
|
||||
std::string error;
|
||||
ASSERT_TRUE(init(m, toYaml(cfg), {}, error)) << error;
|
||||
EXPECT_FALSE(m.enabled());
|
||||
EXPECT_EQ(0u, m.detectorCount());
|
||||
}
|
||||
|
||||
TEST(EnvMonitorLoading, BrakeIsOredCapIsMinAndOnlyDetectorsWithAuthorityAct)
|
||||
{
|
||||
// Two contact instances: `far` keeps 0.2 m and is shadow, `near` keeps 0.1 m and acts on the command.
|
||||
YAML::Node env = toYaml(recoverAll());
|
||||
YAML::Node far = YAML::Clone(env["contact"]);
|
||||
far["clean_start_margin"] = "0.2";
|
||||
far["apply_command"] = false;
|
||||
YAML::Node near = YAML::Clone(env["contact"]);
|
||||
near["clean_start_margin"] = "0.1";
|
||||
near["apply_command"] = true;
|
||||
env["far"] = far;
|
||||
env["near"] = near;
|
||||
EnvMonitor m;
|
||||
std::string error;
|
||||
ASSERT_TRUE(init(m, env, { entry("far", "ContactDetector"), entry("near", "ContactDetector") }, error)) << error;
|
||||
ASSERT_EQ(2u, m.detectorCount());
|
||||
|
||||
Drive d(m);
|
||||
d.contact = 1.0;
|
||||
d.step(0.0, 0.0);
|
||||
const double far_cap = std::sqrt(2.0 * 0.35 * (1.0 - 0.2 - 0.1));
|
||||
const double near_cap = std::sqrt(2.0 * 0.35 * (1.0 - 0.1 - 0.1));
|
||||
EXPECT_NEAR(far_cap, d.last.v_cap, 1e-9) << "v_cap = min over detectors";
|
||||
EXPECT_NEAR(near_cap, d.last.v_cap_applied, 1e-9) << "only `near` acts on the command";
|
||||
EXPECT_FALSE(d.last.brake);
|
||||
|
||||
d.contact = 0.15; // inside both margins: both brake after confirm
|
||||
d.step(0.5, 0.0);
|
||||
d.step(0.5, 0.0);
|
||||
EXPECT_TRUE(d.last.brake);
|
||||
EXPECT_TRUE(d.last.brake_applied);
|
||||
}
|
||||
|
||||
TEST(EnvMonitorLoading, ShadowDetectorsReportTheBrakeButDoNotApplyIt)
|
||||
{
|
||||
EnvMonitorConfig cfg = recoverAll();
|
||||
cfg.contact.apply_command = false;
|
||||
EnvMonitor m;
|
||||
std::string error;
|
||||
ASSERT_TRUE(init(m, toYaml(cfg), {}, error)) << error;
|
||||
Drive d(m);
|
||||
d.contact = 0.10;
|
||||
d.step(0.5, -0.5);
|
||||
d.step(0.5, -0.5);
|
||||
EXPECT_TRUE(d.last.brake);
|
||||
EXPECT_FALSE(d.last.brake_applied);
|
||||
EXPECT_TRUE(std::isinf(d.last.v_cap_applied));
|
||||
}
|
||||
|
||||
TEST(EnvMonitorLoading, ALaterDetectorInTheListWinsTheEventOfACycle)
|
||||
{
|
||||
// Find the cycle at which stall fires with the default order, then reverse the order and brake on that cycle.
|
||||
int fire = -1;
|
||||
{
|
||||
EnvMonitor probe;
|
||||
std::string error;
|
||||
ASSERT_TRUE(init(probe, toYaml(recoverAll()), {}, error)) << error;
|
||||
Drive d(probe);
|
||||
for (int i = 0; i < 60; ++i)
|
||||
{
|
||||
d.step(0.5, -0.5);
|
||||
}
|
||||
for (int i = 0; i < 200 && fire < 0; ++i)
|
||||
{
|
||||
if (d.step(0.5, 0.0).stall_detected)
|
||||
{
|
||||
fire = d.cycle;
|
||||
}
|
||||
}
|
||||
}
|
||||
ASSERT_GT(fire, 62);
|
||||
|
||||
EnvMonitor m;
|
||||
std::string error;
|
||||
ASSERT_TRUE(init(m, toYaml(recoverAll()), { entry("stall", "StallDetector"), entry("contact", "ContactDetector") },
|
||||
error))
|
||||
<< error;
|
||||
Drive d(m);
|
||||
for (int i = 0; i < 60; ++i)
|
||||
{
|
||||
d.step(0.5, -0.5);
|
||||
}
|
||||
while (d.cycle < fire)
|
||||
{
|
||||
if (d.cycle == fire - 2)
|
||||
{
|
||||
d.contact = 0.5; // imminent at fire-1, brake at fire
|
||||
}
|
||||
d.step(0.5, 0.0);
|
||||
}
|
||||
ASSERT_TRUE(d.last.stall_detected);
|
||||
ASSERT_TRUE(d.last.brake);
|
||||
EXPECT_STREQ("contact: braking to zero", d.last.event) << "contact is listed after stall, so its event wins";
|
||||
}
|
||||
|
||||
TEST(EnvMonitorLoading, RouteDefaultsComeFromThePluginAndKeysOverrideThem)
|
||||
{
|
||||
YAML::Node env = toYaml(recoverAll());
|
||||
env["contact"]["route"] = "intrusion";
|
||||
env["contact"]["route_mode"] = "sequence";
|
||||
env["contact"]["on_exhausted"] = "abort";
|
||||
EnvMonitor m;
|
||||
std::string error;
|
||||
ASSERT_TRUE(init(m, env, {}, error)) << error;
|
||||
ASSERT_EQ(3u, m.detectors().size());
|
||||
const auto& contact = m.detectors()[0];
|
||||
EXPECT_EQ("contact", contact.name);
|
||||
EXPECT_EQ("intrusion", contact.route.route);
|
||||
EXPECT_TRUE(contact.route.sequence);
|
||||
EXPECT_FALSE(contact.route.lock_on_exhausted);
|
||||
// Defaults of the monolith: stall -> oscillation/abort, unreachable -> path_blocked/lock, both step.
|
||||
EXPECT_EQ("oscillation", m.detectors()[1].route.route);
|
||||
EXPECT_FALSE(m.detectors()[1].route.lock_on_exhausted);
|
||||
EXPECT_FALSE(m.detectors()[1].route.sequence);
|
||||
EXPECT_EQ("path_blocked", m.detectors()[2].route.route);
|
||||
EXPECT_TRUE(m.detectors()[2].route.lock_on_exhausted);
|
||||
}
|
||||
|
||||
TEST(EnvMonitorLoading, InvalidRouteModeOrOnExhaustedFailsTheInitialize)
|
||||
{
|
||||
for (const char* key : { "route_mode", "on_exhausted" })
|
||||
{
|
||||
YAML::Node env = toYaml(recoverAll());
|
||||
env["stall"][key] = "sometimes";
|
||||
EnvMonitor m;
|
||||
std::string error;
|
||||
EXPECT_FALSE(init(m, env, {}, error)) << key;
|
||||
EXPECT_NE(std::string::npos, error.find("env_monitor/stall")) << error;
|
||||
}
|
||||
}
|
||||
|
||||
TEST(EnvMonitorLoading, FiredMaskHasOneBitPerDetectorInListOrder)
|
||||
{
|
||||
EnvMonitor m;
|
||||
std::string error;
|
||||
ASSERT_TRUE(init(m, toYaml(recoverAll()), {}, error)) << error; // contact=bit0, stall=bit1, unreachable=bit2
|
||||
Drive d(m);
|
||||
d.contact = 0.10;
|
||||
d.step(0.0, 0.0);
|
||||
for (int i = 0; i < 30 && !d.last.collision; ++i)
|
||||
{
|
||||
d.step(0.5, 0.0);
|
||||
}
|
||||
ASSERT_TRUE(d.last.collision);
|
||||
EXPECT_EQ(1u, d.last.fired_mask & 1u);
|
||||
EXPECT_EQ(d.last.no_progress, (d.last.fired_mask & 2u) != 0);
|
||||
EXPECT_EQ(d.last.path_blocked, (d.last.fired_mask & 4u) != 0);
|
||||
}
|
||||
|
||||
TEST(EnvMonitorLoading, RemovedDetectorStopsRunningAndTheOthersShiftDown)
|
||||
{
|
||||
EnvMonitor m;
|
||||
std::string error;
|
||||
ASSERT_TRUE(init(m, toYaml(recoverAll()), {}, error)) << error;
|
||||
EXPECT_FALSE(m.remove("ghost"));
|
||||
ASSERT_TRUE(m.remove("contact"));
|
||||
ASSERT_EQ(2u, m.detectors().size());
|
||||
EXPECT_EQ("stall", m.detectors()[0].name);
|
||||
Drive d(m);
|
||||
d.contact = 0.10;
|
||||
d.step(0.5, -0.5);
|
||||
d.step(0.5, -0.5);
|
||||
EXPECT_FALSE(d.last.brake);
|
||||
EXPECT_FALSE(m.needs().sweep);
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
testing::InitGoogleTest(&argc, argv);
|
||||
return RUN_ALL_TESTS();
|
||||
}
|
||||
1439
test/env_monitor_test.cpp
Normal file
1439
test/env_monitor_test.cpp
Normal file
File diff suppressed because it is too large
Load Diff
65
test/env_monitor_test_config.h
Normal file
65
test/env_monitor_test_config.h
Normal file
@@ -0,0 +1,65 @@
|
||||
/*********************************************************************
|
||||
*
|
||||
* Software License Agreement (BSD License)
|
||||
*
|
||||
* env_monitor_core tests — build an EnvMonitor from an EnvMonitorConfig through the real YAML path.
|
||||
*
|
||||
* The struct is written as YAML under a fresh namespace of the global robot::NodeHandle tree (the same keys
|
||||
* env_monitor_params.yaml uses), with `<Type>/library_path` for the three built-in detectors, then
|
||||
* EnvMonitor::initialize() loads the .so files from devel/lib exactly as the runtime does.
|
||||
*
|
||||
* Author: DuongTD
|
||||
*********************************************************************/
|
||||
#ifndef ENV_MONITOR_CORE_TEST_ENV_MONITOR_TEST_CONFIG_H_
|
||||
#define ENV_MONITOR_CORE_TEST_ENV_MONITOR_TEST_CONFIG_H_
|
||||
|
||||
#include <cstdlib>
|
||||
#include <string>
|
||||
|
||||
#include <yaml-cpp/yaml.h>
|
||||
|
||||
#include <robot/node_handle.h>
|
||||
|
||||
#include <env_monitor_core/env_monitor.h>
|
||||
|
||||
#ifndef ENV_MONITOR_CORE_TEST_LIBRARY_DIR
|
||||
#error "ENV_MONITOR_CORE_TEST_LIBRARY_DIR must be defined by CMake"
|
||||
#endif
|
||||
|
||||
namespace env_monitor_core_test
|
||||
{
|
||||
|
||||
/// Fresh namespace per call: the NodeHandle tree is global, two monitors must never share keys.
|
||||
inline robot::NodeHandle freshNodeHandle()
|
||||
{
|
||||
static int counter = 0;
|
||||
// The runtime and every test resolve `<Type>/library_path` against devel/lib; a value left in the user's shell
|
||||
// (see the scenario test 26/11) must not win.
|
||||
setenv("PNKX_NAV_CORE_LIBRARY_PATH", ENV_MONITOR_CORE_TEST_LIBRARY_DIR, 1);
|
||||
robot::NodeHandle nh("env_monitor_core_test_" + std::to_string(++counter));
|
||||
const char* const kLibraries[][2] = {
|
||||
{ "ContactDetector", "libenv_monitor_core_contact_detector" },
|
||||
{ "StallDetector", "libenv_monitor_core_stall_detector" },
|
||||
{ "UnreachableDetector", "libenv_monitor_core_unreachable_detector" },
|
||||
};
|
||||
for (const auto& lib : kLibraries)
|
||||
{
|
||||
YAML::Node node;
|
||||
node["library_path"] = lib[1];
|
||||
nh.setParam(lib[0], node);
|
||||
}
|
||||
return nh;
|
||||
}
|
||||
|
||||
/// EnvMonitor from @p config through YAML; the `detectors` list is left out, so the built-in default is used.
|
||||
inline bool initializeFrom(env_monitor_core::EnvMonitor& monitor, const env_monitor_core::EnvMonitorConfig& config,
|
||||
std::string& error)
|
||||
{
|
||||
robot::NodeHandle nh = freshNodeHandle();
|
||||
nh.setParam("env_monitor", env_monitor_core::toYaml(config));
|
||||
return monitor.initialize(nh, error);
|
||||
}
|
||||
|
||||
} // namespace env_monitor_core_test
|
||||
|
||||
#endif // ENV_MONITOR_CORE_TEST_ENV_MONITOR_TEST_CONFIG_H_
|
||||
74
test/plugins/stale_abi_detector.cpp
Normal file
74
test/plugins/stale_abi_detector.cpp
Normal file
@@ -0,0 +1,74 @@
|
||||
/*********************************************************************
|
||||
*
|
||||
* Software License Agreement (BSD License)
|
||||
*
|
||||
* env_monitor_core tests — detectors that EnvMonitor must REFUSE to load:
|
||||
* - StaleAbiDetector : `_abi` returns a version the runtime does not match;
|
||||
* - NoAbiDetector : no `_abi` symbol at all (plain BOOST_DLL_ALIAS).
|
||||
*
|
||||
* Author: DuongTD
|
||||
*********************************************************************/
|
||||
#include <memory>
|
||||
#include <string>
|
||||
|
||||
#include <boost/dll/alias.hpp>
|
||||
|
||||
#include <env_monitor_core/env_detector.h>
|
||||
#include <env_monitor_core/env_detector_abi.h>
|
||||
|
||||
namespace env_monitor_core
|
||||
{
|
||||
namespace testing
|
||||
{
|
||||
|
||||
class StaleAbiDetector final : public EnvDetector
|
||||
{
|
||||
public:
|
||||
static EnvDetector::Ptr create()
|
||||
{
|
||||
return std::make_shared<StaleAbiDetector>();
|
||||
}
|
||||
|
||||
bool initialize(const std::string& /*name*/, robot::NodeHandle& /*nh*/, std::string& /*error*/) override
|
||||
{
|
||||
return true;
|
||||
}
|
||||
std::string describe() const override
|
||||
{
|
||||
return "stale";
|
||||
}
|
||||
bool armed() const override
|
||||
{
|
||||
return true;
|
||||
}
|
||||
EnvRouteSpec defaultRoute() const override
|
||||
{
|
||||
EnvRouteSpec spec;
|
||||
spec.route = "stale";
|
||||
return spec;
|
||||
}
|
||||
EnvNeeds needs() const override
|
||||
{
|
||||
return EnvNeeds();
|
||||
}
|
||||
void reset() override
|
||||
{
|
||||
}
|
||||
void update(const EnvFrame& /*frame*/, EnvDetectorOutput& out, EnvVerdict& /*verdict*/) override
|
||||
{
|
||||
out.brake = true; // would be visible if the monitor ever ran it
|
||||
}
|
||||
};
|
||||
|
||||
/// ABI version "from the future": never equal to the runtime's.
|
||||
int staleAbiVersion()
|
||||
{
|
||||
return kEnvDetectorAbiVersion + 1;
|
||||
}
|
||||
|
||||
} // namespace testing
|
||||
} // namespace env_monitor_core
|
||||
|
||||
BOOST_DLL_ALIAS(env_monitor_core::testing::StaleAbiDetector::create, StaleAbiDetector)
|
||||
BOOST_DLL_ALIAS(env_monitor_core::testing::staleAbiVersion, StaleAbiDetector_abi)
|
||||
BOOST_DLL_ALIAS(env_monitor_core::testing::StaleAbiDetector::create, NoAbiDetector)
|
||||
Reference in New Issue
Block a user