slope: test doc tu dong (--slope-test) -- dinh vi lai xong robot tu tim truc doc tren ban do (do cao san cua node), quay mat xuong doc, cho nut Y (gamepad.button_test) roi chay N luot xuong/len, moi luot mot van toc (mac dinh 10 luot 0.10..0.46 m/s)
Lenh di qua FSM an toan nhu moi lenh khac; dung han khi lech duong > 0.4 m, scan mat khop 1.5 s, pose nhay > 0.3 m, nghieng > 20 do. Do lech dinh vi 2 lan/giay (khop scan voi likelihood field, doc lap RTAB-Map) ra logs/slope_test_<gio>.csv + bang tong. Sim 08/10 (slope_two_ramps, 10 luot): lech that trung vi 0.9 cm, max 3.0 cm, sai z trung vi 0.3 cm, khong troi tu luot 1 den 10.
This commit is contained in:
@@ -100,6 +100,9 @@ struct GamepadConfig {
|
||||
int button_zero = 1; // B -- ve 0 ngay
|
||||
int button_turbo = 5; // RB
|
||||
int button_slow = 4; // LB
|
||||
// Y -- test doc tu dong (--slope-test): bat dau / chay tiep sau tam dung.
|
||||
// Bat ke dang bat dieu khien hay khong.
|
||||
int button_test = 3;
|
||||
|
||||
// Duong thoat khi bo tay cam nguoc chieu. Doi mot dong YAML thay vi sua code
|
||||
// roi build lai -- chieu truc chi kiem duoc bang cach cam tay cam vao robot
|
||||
@@ -191,6 +194,9 @@ struct GamepadCommand {
|
||||
// Chuyen trang thai, chi de LOG. Mapper khong tu log: no thuan.
|
||||
bool enabled_changed = false;
|
||||
bool connected_changed = false;
|
||||
|
||||
// Vua BAM nut test (canh len) -- runtime dem so lan bam.
|
||||
bool test_pressed = false;
|
||||
};
|
||||
|
||||
// Tho -> van toc. THUAN: khong doc dong ho, khong doc file, khong log.
|
||||
@@ -215,6 +221,7 @@ class GamepadMapper {
|
||||
bool enable_pressed_ = false; // chong dap phim, = start_pressed_ ben kia
|
||||
bool was_connected_ = false;
|
||||
bool have_active_ = false; // da tung co lenh chuyen dong
|
||||
bool test_prev_ = false; // nut test vong truoc (bat canh len)
|
||||
UnixTime last_active_;
|
||||
};
|
||||
|
||||
|
||||
@@ -91,6 +91,7 @@ GamepadCommand GamepadMapper::Step(const GamepadState& s, UnixTime now) {
|
||||
enabled_ = false;
|
||||
enable_pressed_ = false;
|
||||
was_connected_ = false;
|
||||
test_prev_ = false;
|
||||
}
|
||||
out.enabled = false;
|
||||
// Van gui 0 trong cua so idle: rut day giua luc dang chay thi lenh cuoi
|
||||
@@ -108,6 +109,12 @@ GamepadCommand GamepadMapper::Step(const GamepadState& s, UnixTime now) {
|
||||
out.connected_changed = true;
|
||||
}
|
||||
|
||||
// --- Nut test (Y): canh len, BAT KE dang bat dieu khien hay khong ---------
|
||||
// Dat TRUOC moi nhanh `return` ben duoi (nut B, coc LT, chua bat).
|
||||
const bool test_now = s.Button(cfg_.button_test);
|
||||
out.test_pressed = test_now && !test_prev_;
|
||||
test_prev_ = test_now;
|
||||
|
||||
// --- Nut Start: BAT/TAT dieu khien --------------------------------------
|
||||
// joy_control_node.cpp:180-194. Chong dap phim bang co canh len.
|
||||
if (s.Button(cfg_.button_enable)) {
|
||||
|
||||
@@ -38,6 +38,7 @@ vslam_add_library(vslam_slam
|
||||
sensor_data_builder.h
|
||||
occupancy_export.h
|
||||
voxel_export.h
|
||||
slope_test.h
|
||||
height_map.h
|
||||
global_localizer.h
|
||||
vpr_encoder.h
|
||||
|
||||
159
libs/vslam_slam/include/vslam/slope_test.h
Normal file
159
libs/vslam_slam/include/vslam/slope_test.h
Normal file
@@ -0,0 +1,159 @@
|
||||
#ifndef VSLAM_SLOPE_TEST_H
|
||||
#define VSLAM_SLOPE_TEST_H
|
||||
|
||||
// Test DOC tu dong (08/10): dat robot o DINH doc, dinh vi lai xong robot tu tim
|
||||
// huong doc tren ban do, quay mat xuong, roi XUONG / LEN nhieu luot -- moi luot
|
||||
// MOT van toc -- de do sai so dinh vi (va z) theo van toc va theo thoi gian.
|
||||
//
|
||||
// Chi la LOGIC: lap duong thu tu ban do (PlanSlopePath) va may trang thai lai
|
||||
// (SlopeTest). Khong cham phan cung: runtime dua pose / co / nut vao, lay lenh
|
||||
// (v, w) ra va dua qua DUNG duong FSM an toan nhu moi lenh khac (tay cam, web,
|
||||
// dinh vi lai tu dong).
|
||||
//
|
||||
// A (cho robot dung) ──XUONG, v_k──> B (chan doc da phang + 0.5 m)
|
||||
// dung do, quay dau dung do, quay dau
|
||||
// <──LEN, v_k── x loops luot, v_k = speeds[k]
|
||||
|
||||
#include <functional>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "vslam/vslam_common.h"
|
||||
|
||||
namespace vslam {
|
||||
|
||||
struct SlopePathConfig {
|
||||
double max_len = 15.0; // m, xa nhat tu A
|
||||
double step = 0.10; // m, buoc do doc duong
|
||||
double heading_step_deg = 2.0;
|
||||
double min_drop = 0.20; // m, san phai xuong it nhat chung nay
|
||||
double flat_tol = 0.04; // m, "phang lai": z lech z thap nhat < nguong nay ...
|
||||
double flat_len = 1.0; // m, ... lien tiep chung nay
|
||||
double past_bottom = 0.5; // m, B = cho bat dau phang + doan nay
|
||||
double stop_margin = 0.6; // m, B cach o khong trong / khong biet san it nhat
|
||||
double level_deg = 3.0; // san 'phang': do doc mat san duoi nguong nay
|
||||
double level_h = 0.25; // m, buoc sai phan do do doc
|
||||
};
|
||||
|
||||
struct SlopePath {
|
||||
Pose2D a; // x, y; theta = huong A -> B (XUONG doc)
|
||||
double bx = 0.0, by = 0.0;
|
||||
double length = 0.0; // m
|
||||
double drop = 0.0; // m, z(A) - z(B) > 0
|
||||
};
|
||||
|
||||
// floor(x, y, &z): do cao san trong ban do; false = khong biet san o do.
|
||||
// free(x, y): o TRONG va du cho than robot (nguoi goi da tinh lan).
|
||||
using FloorFn = std::function<bool(double, double, double*)>;
|
||||
using FreeFn = std::function<bool(double, double)>;
|
||||
|
||||
// Thu moi huong quanh (x, y): di thang tren ban do, buoc `step`, toi khi gap o
|
||||
// khong trong hoac khong biet san. Huong hop le: san XUONG >= min_drop roi PHANG
|
||||
// lai >= flat_len truoc khi het duong. Chon huong xuong het doc NGAN NHAT -- tuc
|
||||
// la doc theo truc doc, khong xien. false + `why` neu khong co huong nao.
|
||||
//
|
||||
// A = DINH doc phang (robot dang giua doc thi di nguoc len theo truc toi cho
|
||||
// phang), B = CHAN doc phang + past_bottom. `diag` (tuy chon): moi huong da
|
||||
// thu, "huong:duong trong:dung vi:ket qua" -- de doc khi khong ra duong.
|
||||
bool PlanSlopePath(double x, double y, const FloorFn& floor, const FreeFn& free,
|
||||
const SlopePathConfig& cfg, SlopePath* out, std::string* why,
|
||||
std::string* diag = nullptr);
|
||||
|
||||
struct SlopeTestConfig {
|
||||
int loops = 10;
|
||||
// Van toc MOI LUOT (xuong + len cung mot van toc), tang dan: luot dau cham,
|
||||
// an toan nhat. Het danh sach thi quay vong.
|
||||
std::vector<double> speeds = {0.10, 0.14, 0.18, 0.22, 0.26, 0.30, 0.34, 0.38, 0.42, 0.46};
|
||||
double dwell_s = 3.0; // dung o moi dau de do
|
||||
double turn_rate = 0.5; // rad/s, quay tai cho toi da
|
||||
double turn_min = 0.15; // rad/s, toi thieu (vuot vung chet cua base)
|
||||
double turn_tol_deg = 3.0;
|
||||
double arrive_tol = 0.10; // m
|
||||
double slow_zone = 0.8; // m, giam toc truoc dich
|
||||
double min_speed = 0.06; // m/s
|
||||
double k_heading = 1.5; // w = k * loi huong
|
||||
double k_cross = 1.0; // huong dich = huong duong - atan(k * lech ngang)
|
||||
double max_w = 0.6; // rad/s khi dang chay
|
||||
double drive_turn_deg = 25.0; // loi huong lon hon -> dung lai quay truoc
|
||||
double cross_max = 0.40; // m, lech khoi duong -> DUNG HAN
|
||||
double jump_max = 0.30; // m giua hai buoc, pose nhay -> DUNG HAN
|
||||
double tilt_max_deg = 20.0; // nghieng hon -> DUNG HAN
|
||||
double leg_timeout_extra = 20.0; // s, cong vao 2 * L / v
|
||||
// Robot khong o A (dinh vi lai tu chay ra giua doc): chay CHAM ve A truoc.
|
||||
double approach_speed = 0.15; // m/s
|
||||
};
|
||||
|
||||
enum class SlopeTestPhase {
|
||||
kIdle, // chua co duong
|
||||
kFace, // quay mat xuong doc (truoc khi cho nut)
|
||||
kWaitStart, // da quay mat, cho nguoi bam bat dau
|
||||
kTurn, // quay theo huong luot dang chay
|
||||
kDrive, // dang chay tren duong
|
||||
kDwell, // dung o mot dau de do
|
||||
kPaused, // nguoi vua lai -- im, cho bam tiep tuc
|
||||
kHalted, // vi pham an toan -- dung han
|
||||
kDone, // xong moi luot
|
||||
};
|
||||
|
||||
const char* SlopeTestPhaseName(SlopeTestPhase p);
|
||||
|
||||
struct SlopeTestInput {
|
||||
double t = 0.0; // s, dong ho don dieu
|
||||
Pose2D pose; // pose MAP
|
||||
bool localized = false; // false = mat dinh vi / scan khong khop ban do
|
||||
double roll = 0.0, pitch = 0.0; // rad, nghieng than
|
||||
bool human = false; // nguon khac (tay cam, web) vua ra lenh
|
||||
bool start = false; // vua bam nut bat dau / chay tiep
|
||||
};
|
||||
|
||||
struct SlopeTestOutput {
|
||||
bool command = false; // ghi lenh khong; false = im, nhuong nguon khac
|
||||
double v = 0.0, w = 0.0;
|
||||
std::string event; // dong log moi; rong = khong co gi moi
|
||||
};
|
||||
|
||||
class SlopeTest {
|
||||
public:
|
||||
explicit SlopeTest(SlopeTestConfig cfg = SlopeTestConfig());
|
||||
|
||||
// Dat duong; vao kFace. autostart: quay mat xong thi chay luon (mo phong --
|
||||
// khong co tay cam), khong thi cho `start`.
|
||||
void SetPath(const SlopePath& p, bool autostart);
|
||||
SlopeTestOutput Step(const SlopeTestInput& in);
|
||||
|
||||
SlopeTestPhase phase() const { return phase_; }
|
||||
const SlopePath& path() const { return path_; }
|
||||
int loops() const { return cfg_.loops; }
|
||||
int loop() const { return loop_; } // 1-based; 0 = chua chay
|
||||
bool down() const { return down_; } // luot hien tai dang XUONG
|
||||
double speed() const; // van toc luot hien tai
|
||||
bool approaching() const { return approach_; } // dang ve A (khong phai luot do)
|
||||
double along() const { return along_; } // m, da di tu dau luot
|
||||
double cross() const { return cross_; } // m, lech ngang (+ = ben trai)
|
||||
const std::string& halt_reason() const { return halt_; }
|
||||
|
||||
private:
|
||||
void BeginLeg(double t);
|
||||
SlopeTestOutput Halt(const std::string& why);
|
||||
bool Unsafe(const SlopeTestInput& in, std::string* why) const;
|
||||
|
||||
SlopeTestConfig cfg_;
|
||||
SlopePath path_;
|
||||
SlopeTestPhase phase_ = SlopeTestPhase::kIdle;
|
||||
SlopeTestPhase paused_from_ = SlopeTestPhase::kIdle;
|
||||
bool autostart_ = false;
|
||||
bool approach_ = false; // dang chay ve A
|
||||
bool approached_ = false; // da kiem can ve A chua
|
||||
int loop_ = 0;
|
||||
bool down_ = true;
|
||||
double from_x_ = 0.0, from_y_ = 0.0, to_x_ = 0.0, to_y_ = 0.0, heading_ = 0.0, len_ = 0.0;
|
||||
double leg_t0_ = 0.0, dwell_t0_ = 0.0;
|
||||
double along_ = 0.0, cross_ = 0.0;
|
||||
bool have_prev_ = false;
|
||||
Pose2D prev_;
|
||||
std::string halt_;
|
||||
};
|
||||
|
||||
} // namespace vslam
|
||||
|
||||
#endif // VSLAM_SLOPE_TEST_H
|
||||
480
libs/vslam_slam/src/slope_test.cc
Normal file
480
libs/vslam_slam/src/slope_test.cc
Normal file
@@ -0,0 +1,480 @@
|
||||
#include "vslam/slope_test.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <cmath>
|
||||
#include <sstream>
|
||||
|
||||
#include "vslam/geometry.h"
|
||||
|
||||
namespace vslam {
|
||||
|
||||
const char* SlopeTestPhaseName(SlopeTestPhase p) {
|
||||
switch (p) {
|
||||
case SlopeTestPhase::kIdle: return "chua co duong";
|
||||
case SlopeTestPhase::kFace: return "quay mat xuong doc";
|
||||
case SlopeTestPhase::kWaitStart: return "cho bam bat dau";
|
||||
case SlopeTestPhase::kTurn: return "quay theo luot";
|
||||
case SlopeTestPhase::kDrive: return "dang chay";
|
||||
case SlopeTestPhase::kDwell: return "dung do";
|
||||
case SlopeTestPhase::kPaused: return "tam dung";
|
||||
case SlopeTestPhase::kHalted: return "dung han";
|
||||
case SlopeTestPhase::kDone: return "xong";
|
||||
}
|
||||
return "?";
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Lap duong
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
bool PlanSlopePath(double x, double y, const FloorFn& floor, const FreeFn& free,
|
||||
const SlopePathConfig& cfg, SlopePath* out, std::string* why,
|
||||
std::string* diag) {
|
||||
double z0 = 0.0;
|
||||
if (!floor(x, y, &z0)) {
|
||||
*why = "khong biet do cao san cho robot dung (ban do khong co z node? slope.enabled?)";
|
||||
return false;
|
||||
}
|
||||
// San PHANG tai (px, py): do doc bang sai phan +-level_h theo x va y. Chi nhin
|
||||
// z doc theo tia la khong du: di CHEO tren doc 10 do thi z doi cham, trong
|
||||
// nhu phang (sim 08/10: chon duong ngang doc, xuong 0.2 m).
|
||||
const double level_tan = std::tan(cfg.level_deg * M_PI / 180.0);
|
||||
auto level = [&](double px, double py) {
|
||||
double zx0 = 0.0, zx1 = 0.0, zy0 = 0.0, zy1 = 0.0;
|
||||
const double h = cfg.level_h;
|
||||
if (!floor(px - h, py, &zx0) || !floor(px + h, py, &zx1) || !floor(px, py - h, &zy0) ||
|
||||
!floor(px, py + h, &zy1)) {
|
||||
return false;
|
||||
}
|
||||
return std::hypot(zx1 - zx0, zy1 - zy0) / (2.0 * h) < level_tan;
|
||||
};
|
||||
|
||||
struct Cand {
|
||||
double th, len, drop;
|
||||
};
|
||||
std::vector<Cand> ok;
|
||||
std::ostringstream fan;
|
||||
double best_drop_seen = 0.0;
|
||||
const int nh = std::max(8, static_cast<int>(std::lround(360.0 / cfg.heading_step_deg)));
|
||||
for (int k = 0; k < nh; ++k) {
|
||||
const double th = NormalizeAngle(2.0 * M_PI * k / nh);
|
||||
const double c = std::cos(th), s = std::sin(th);
|
||||
std::vector<double> ss, zs;
|
||||
const char* stop = "max";
|
||||
for (double d = cfg.step; d <= cfg.max_len + 1e-9; d += cfg.step) {
|
||||
const double px = x + c * d, py = y + s * d;
|
||||
if (!free(px, py)) {
|
||||
stop = "vc";
|
||||
break;
|
||||
}
|
||||
double z = 0.0;
|
||||
if (!floor(px, py, &z)) {
|
||||
stop = "ks";
|
||||
break;
|
||||
}
|
||||
ss.push_back(d);
|
||||
zs.push_back(z);
|
||||
}
|
||||
if (ss.empty()) continue;
|
||||
const double s_end = ss.back();
|
||||
double drop_here = 0.0;
|
||||
for (const double z : zs) drop_here = std::max(drop_here, z0 - z);
|
||||
best_drop_seen = std::max(best_drop_seen, drop_here);
|
||||
if (drop_here < cfg.min_drop) continue;
|
||||
// Chan doc: diem DAU TIEN da xuong >= min_drop ma flat_len tiep theo PHANG.
|
||||
const char* res = "khong phang";
|
||||
for (size_t i = 0; i < ss.size(); ++i) {
|
||||
if (z0 - zs[i] < cfg.min_drop) continue;
|
||||
if (ss[i] + cfg.flat_len > s_end) {
|
||||
res = "het duong";
|
||||
break;
|
||||
}
|
||||
bool flat = true;
|
||||
for (size_t j = i; j < ss.size() && ss[j] <= ss[i] + cfg.flat_len + 1e-9; ++j) {
|
||||
if (!level(x + c * ss[j], y + s * ss[j])) {
|
||||
flat = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!flat) continue;
|
||||
const double len = ss[i] + cfg.past_bottom;
|
||||
if (len > s_end - cfg.stop_margin) {
|
||||
res = "B sat vat can";
|
||||
break;
|
||||
}
|
||||
double zb = zs[i];
|
||||
floor(x + c * len, y + s * len, &zb);
|
||||
ok.push_back(Cand{th, len, z0 - zb});
|
||||
res = "OK";
|
||||
break;
|
||||
}
|
||||
fan << " " << std::lround(th * 180.0 / M_PI) << "d:" << s_end << "m:" << stop << ":" << res;
|
||||
}
|
||||
if (diag != nullptr) *diag = fan.str();
|
||||
if (ok.empty()) {
|
||||
std::ostringstream os;
|
||||
os << "khong thay doc tu cho robot dung (xuong toi da " << best_drop_seen << " m, can >= "
|
||||
<< cfg.min_drop << " m roi PHANG " << cfg.flat_len << " m truoc vat can) -- dat robot gan "
|
||||
<< "dinh doc hon. Huong:duong trong:dung vi (vc vat can, ks khong biet san):ket qua --"
|
||||
<< fan.str();
|
||||
*why = os.str();
|
||||
return false;
|
||||
}
|
||||
// Uu tien xuong HET doc (>= 80% do sut lon nhat) -- loai duong cheo / ngang chi
|
||||
// cat qua mep doc; roi NGAN NHAT = truc doc. Doc rong: mot dai huong gan truc
|
||||
// ngan bang nhau (luong tu 10 cm) -- lay huong GIUA cua dai do.
|
||||
double max_drop = 0.0;
|
||||
for (const Cand& c : ok) max_drop = std::max(max_drop, c.drop);
|
||||
double min_len = 1e9;
|
||||
for (const Cand& c : ok) {
|
||||
if (c.drop >= 0.8 * max_drop) min_len = std::min(min_len, c.len);
|
||||
}
|
||||
double sx = 0.0, sy = 0.0;
|
||||
for (const Cand& c : ok) {
|
||||
if (c.drop < 0.8 * max_drop || c.len > min_len + 0.05) continue;
|
||||
sx += std::cos(c.th);
|
||||
sy += std::sin(c.th);
|
||||
}
|
||||
const double th = std::atan2(sy, sx);
|
||||
const double ux = std::cos(th), uy = std::sin(th);
|
||||
|
||||
// Day doc B: di lai theo HUONG DA CHON (huong trung binh co the lech vai do).
|
||||
double b_len = -1.0;
|
||||
{
|
||||
std::vector<double> ss, zs;
|
||||
for (double d = cfg.step; d <= cfg.max_len + 1e-9; d += cfg.step) {
|
||||
double z = 0.0;
|
||||
if (!free(x + ux * d, y + uy * d) || !floor(x + ux * d, y + uy * d, &z)) break;
|
||||
ss.push_back(d);
|
||||
zs.push_back(z);
|
||||
}
|
||||
for (size_t i = 0; i < ss.size() && b_len < 0.0; ++i) {
|
||||
if (z0 - zs[i] < cfg.min_drop || ss[i] + cfg.flat_len > ss.back()) continue;
|
||||
bool flat = true;
|
||||
for (size_t j = i; j < ss.size() && ss[j] <= ss[i] + cfg.flat_len + 1e-9; ++j) {
|
||||
if (!level(x + ux * ss[j], y + uy * ss[j])) {
|
||||
flat = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (flat && ss[i] + cfg.past_bottom <= ss.back() - cfg.stop_margin) {
|
||||
b_len = ss[i] + cfg.past_bottom;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (b_len < 0.0) {
|
||||
*why = "huong trung binh cua truc doc khong toi duoc chan doc phang -- dat robot thang truc doc hon";
|
||||
return false;
|
||||
}
|
||||
|
||||
// Dinh doc A: robot co the DANG o giua doc (dinh vi lai tu chay toi 1 m moi
|
||||
// lan, sim 08/10: 3.5 m). Di NGUOC len theo truc toi cho phang flat_len, roi
|
||||
// them past_bottom. Robot da o cho phang thi A = cho robot.
|
||||
double a_back = 0.0;
|
||||
bool at_top = true;
|
||||
for (double d = 0.0; d <= cfg.flat_len + 1e-9; d += cfg.step) {
|
||||
if (!level(x - ux * d, y - uy * d)) {
|
||||
at_top = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!at_top) {
|
||||
bool found = false;
|
||||
for (double d = cfg.step; d <= cfg.max_len + 1e-9 && !found; d += cfg.step) {
|
||||
if (!free(x - ux * d, y - uy * d)) break;
|
||||
bool flat = true;
|
||||
for (double e = d; e <= d + cfg.flat_len + 1e-9; e += cfg.step) {
|
||||
if (!level(x - ux * e, y - uy * e)) {
|
||||
flat = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (flat) {
|
||||
a_back = d + cfg.past_bottom;
|
||||
found = true;
|
||||
}
|
||||
}
|
||||
if (!found) {
|
||||
*why = "dang o giua doc ma khong tim thay dinh doc phang phia tren -- dat robot o dinh doc";
|
||||
return false;
|
||||
}
|
||||
for (double d = cfg.step; d <= a_back + 1e-9; d += cfg.step) {
|
||||
if (!free(x - ux * d, y - uy * d)) {
|
||||
*why = "duong len dinh doc vuong vat can -- dat robot o dinh doc";
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
out->a = Pose2D{x - ux * a_back, y - uy * a_back, th};
|
||||
out->bx = x + ux * b_len;
|
||||
out->by = y + uy * b_len;
|
||||
out->length = a_back + b_len;
|
||||
double za = z0, zb = z0;
|
||||
floor(out->a.x, out->a.y, &za);
|
||||
floor(out->bx, out->by, &zb);
|
||||
out->drop = za - zb;
|
||||
return true;
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// May trang thai
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
SlopeTest::SlopeTest(SlopeTestConfig cfg) : cfg_(std::move(cfg)) {
|
||||
if (cfg_.speeds.empty()) cfg_.speeds.push_back(0.2);
|
||||
if (cfg_.loops < 1) cfg_.loops = 1;
|
||||
}
|
||||
|
||||
void SlopeTest::SetPath(const SlopePath& p, bool autostart) {
|
||||
path_ = p;
|
||||
autostart_ = autostart;
|
||||
phase_ = SlopeTestPhase::kFace;
|
||||
loop_ = 0;
|
||||
down_ = true;
|
||||
have_prev_ = false;
|
||||
approach_ = false;
|
||||
approached_ = false;
|
||||
leg_t0_ = 0.0;
|
||||
halt_.clear();
|
||||
}
|
||||
|
||||
double SlopeTest::speed() const {
|
||||
if (approach_) return cfg_.approach_speed;
|
||||
const size_t i = loop_ > 0 ? static_cast<size_t>(loop_ - 1) % cfg_.speeds.size() : 0;
|
||||
return cfg_.speeds[i];
|
||||
}
|
||||
|
||||
void SlopeTest::BeginLeg(double t) {
|
||||
if (down_) {
|
||||
from_x_ = path_.a.x;
|
||||
from_y_ = path_.a.y;
|
||||
to_x_ = path_.bx;
|
||||
to_y_ = path_.by;
|
||||
} else {
|
||||
from_x_ = path_.bx;
|
||||
from_y_ = path_.by;
|
||||
to_x_ = path_.a.x;
|
||||
to_y_ = path_.a.y;
|
||||
}
|
||||
heading_ = std::atan2(to_y_ - from_y_, to_x_ - from_x_);
|
||||
len_ = std::hypot(to_x_ - from_x_, to_y_ - from_y_);
|
||||
along_ = 0.0;
|
||||
cross_ = 0.0;
|
||||
phase_ = SlopeTestPhase::kTurn;
|
||||
leg_t0_ = t;
|
||||
}
|
||||
|
||||
SlopeTestOutput SlopeTest::Halt(const std::string& why) {
|
||||
phase_ = SlopeTestPhase::kHalted;
|
||||
halt_ = why;
|
||||
SlopeTestOutput o;
|
||||
o.command = true; // MOT lenh dung, roi im
|
||||
o.event = "DUNG HAN -- " + why;
|
||||
return o;
|
||||
}
|
||||
|
||||
bool SlopeTest::Unsafe(const SlopeTestInput& in, std::string* why) const {
|
||||
if (!in.localized) {
|
||||
*why = "mat dinh vi / scan khong khop ban do";
|
||||
return true;
|
||||
}
|
||||
const double tilt = cfg_.tilt_max_deg * M_PI / 180.0;
|
||||
if (std::fabs(in.roll) > tilt || std::fabs(in.pitch) > tilt) {
|
||||
std::ostringstream os;
|
||||
os << "nghieng qua " << cfg_.tilt_max_deg << " do (roll " << in.roll * 180.0 / M_PI
|
||||
<< ", pitch " << in.pitch * 180.0 / M_PI << ")";
|
||||
*why = os.str();
|
||||
return true;
|
||||
}
|
||||
if (have_prev_) {
|
||||
const double j = std::hypot(in.pose.x - prev_.x, in.pose.y - prev_.y);
|
||||
if (j > cfg_.jump_max) {
|
||||
std::ostringstream os;
|
||||
os << "pose nhay " << j << " m trong mot buoc (dinh vi sai?)";
|
||||
*why = os.str();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
SlopeTestOutput SlopeTest::Step(const SlopeTestInput& in) {
|
||||
SlopeTestOutput o;
|
||||
const double tol = cfg_.turn_tol_deg * M_PI / 180.0;
|
||||
auto turn = [&](double err) {
|
||||
double w = std::max(-cfg_.turn_rate, std::min(cfg_.turn_rate, cfg_.k_heading * err));
|
||||
if (std::fabs(w) < cfg_.turn_min) w = std::copysign(cfg_.turn_min, err);
|
||||
return w;
|
||||
};
|
||||
auto leg_name = [&]() {
|
||||
std::ostringstream os;
|
||||
if (approach_) {
|
||||
os << "ve dinh doc (A) " << speed() << " m/s";
|
||||
return os.str();
|
||||
}
|
||||
os << "luot " << loop_ << "/" << cfg_.loops << " " << (down_ ? "XUONG" : "LEN") << " "
|
||||
<< speed() << " m/s";
|
||||
return os.str();
|
||||
};
|
||||
|
||||
switch (phase_) {
|
||||
case SlopeTestPhase::kIdle:
|
||||
case SlopeTestPhase::kHalted:
|
||||
case SlopeTestPhase::kDone:
|
||||
return o; // im
|
||||
case SlopeTestPhase::kPaused:
|
||||
if (in.start) {
|
||||
// Chay tiep: dang chay / quay thi quay lai theo huong luot roi chay tiep.
|
||||
phase_ = paused_from_ == SlopeTestPhase::kDrive ? SlopeTestPhase::kTurn : paused_from_;
|
||||
if (phase_ == SlopeTestPhase::kDwell) dwell_t0_ = in.t;
|
||||
leg_t0_ = in.t;
|
||||
have_prev_ = false;
|
||||
o.event = "chay tiep (" + std::string(SlopeTestPhaseName(phase_)) + ").";
|
||||
}
|
||||
return o;
|
||||
case SlopeTestPhase::kWaitStart:
|
||||
if (in.start) {
|
||||
loop_ = 1;
|
||||
down_ = true;
|
||||
BeginLeg(in.t);
|
||||
have_prev_ = false;
|
||||
o.event = "BAT DAU " + leg_name();
|
||||
}
|
||||
return o;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
// Cac pha DANG ra lenh: nguoi lai thi nhuong ngay, im cho toi khi bam tiep.
|
||||
if (in.human) {
|
||||
paused_from_ = phase_;
|
||||
phase_ = SlopeTestPhase::kPaused;
|
||||
o.event = "nguoi dang lai -- TAM DUNG. Bam Y de chay tiep.";
|
||||
return o;
|
||||
}
|
||||
std::string why;
|
||||
if (Unsafe(in, &why)) return Halt(why);
|
||||
prev_ = in.pose;
|
||||
have_prev_ = true;
|
||||
|
||||
o.command = true; // tu day: luon ghi lenh (ke ca 0 = giu dung)
|
||||
switch (phase_) {
|
||||
case SlopeTestPhase::kFace: {
|
||||
// Lan dau: robot khong o A (dinh vi lai tu chay ra giua doc) -> ve A truoc.
|
||||
if (!approached_) {
|
||||
approached_ = true;
|
||||
const double d = std::hypot(path_.a.x - in.pose.x, path_.a.y - in.pose.y);
|
||||
if (d > 1.5 * cfg_.arrive_tol) {
|
||||
approach_ = true;
|
||||
from_x_ = in.pose.x;
|
||||
from_y_ = in.pose.y;
|
||||
to_x_ = path_.a.x;
|
||||
to_y_ = path_.a.y;
|
||||
heading_ = std::atan2(to_y_ - from_y_, to_x_ - from_x_);
|
||||
len_ = d;
|
||||
along_ = cross_ = 0.0;
|
||||
phase_ = SlopeTestPhase::kTurn;
|
||||
leg_t0_ = in.t;
|
||||
std::ostringstream os;
|
||||
os << "robot cach dinh doc (A) " << d << " m -- chay CHAM ve A truoc.";
|
||||
o.event = os.str();
|
||||
return o;
|
||||
}
|
||||
}
|
||||
const double err = NormalizeAngle(path_.a.theta - in.pose.theta);
|
||||
if (std::fabs(err) < tol) {
|
||||
std::ostringstream os;
|
||||
os << "da quay mat xuong doc (xuong " << path_.drop << " m, dai " << path_.length << " m). ";
|
||||
if (autostart_) {
|
||||
loop_ = 1;
|
||||
down_ = true;
|
||||
BeginLeg(in.t);
|
||||
os << "Tu chay (mo phong): " << leg_name();
|
||||
} else {
|
||||
phase_ = SlopeTestPhase::kWaitStart;
|
||||
os << "Bam Y de chay " << cfg_.loops << " luot.";
|
||||
}
|
||||
o.event = os.str();
|
||||
return o;
|
||||
}
|
||||
if (in.t - leg_t0_ > 30.0 && leg_t0_ > 0.0) return Halt("quay mat xuong doc qua 30 s");
|
||||
if (leg_t0_ <= 0.0) leg_t0_ = in.t;
|
||||
o.w = turn(err);
|
||||
return o;
|
||||
}
|
||||
case SlopeTestPhase::kTurn: {
|
||||
const double err = NormalizeAngle(heading_ - in.pose.theta);
|
||||
if (std::fabs(err) < tol) {
|
||||
phase_ = SlopeTestPhase::kDrive;
|
||||
leg_t0_ = in.t;
|
||||
return o;
|
||||
}
|
||||
if (in.t - leg_t0_ > 30.0) return Halt("quay dau qua 30 s (vat can chan quay?)");
|
||||
o.w = turn(err);
|
||||
return o;
|
||||
}
|
||||
case SlopeTestPhase::kDrive: {
|
||||
const double ux = std::cos(heading_), uy = std::sin(heading_);
|
||||
const double rx = in.pose.x - from_x_, ry = in.pose.y - from_y_;
|
||||
along_ = rx * ux + ry * uy;
|
||||
cross_ = ux * ry - uy * rx;
|
||||
if (std::fabs(cross_) > cfg_.cross_max) {
|
||||
std::ostringstream os;
|
||||
os << "lech khoi duong " << cross_ << " m (" << leg_name() << ")";
|
||||
return Halt(os.str());
|
||||
}
|
||||
const double timeout = 2.0 * len_ / std::max(speed(), 0.05) + cfg_.leg_timeout_extra;
|
||||
if (in.t - leg_t0_ > timeout) {
|
||||
std::ostringstream os;
|
||||
os << "qua " << timeout << " s ma chua toi dau doc (" << leg_name() << ")";
|
||||
return Halt(os.str());
|
||||
}
|
||||
const double d = len_ - along_;
|
||||
if (d <= cfg_.arrive_tol && approach_) {
|
||||
approach_ = false;
|
||||
phase_ = SlopeTestPhase::kFace;
|
||||
leg_t0_ = in.t;
|
||||
o.event = "da ve dinh doc (A).";
|
||||
return o;
|
||||
}
|
||||
if (d <= cfg_.arrive_tol) {
|
||||
phase_ = SlopeTestPhase::kDwell;
|
||||
dwell_t0_ = in.t;
|
||||
std::ostringstream os;
|
||||
os << leg_name() << ": toi " << (down_ ? "CHAN" : "DINH") << " doc sau " << in.t - leg_t0_
|
||||
<< " s, lech ngang " << cross_ * 100.0 << " cm";
|
||||
o.event = os.str();
|
||||
return o;
|
||||
}
|
||||
const double psi_d = heading_ - std::atan(cfg_.k_cross * cross_);
|
||||
const double err = NormalizeAngle(psi_d - in.pose.theta);
|
||||
o.w = std::max(-cfg_.max_w, std::min(cfg_.max_w, cfg_.k_heading * err));
|
||||
if (std::fabs(err) < cfg_.drive_turn_deg * M_PI / 180.0) {
|
||||
o.v = std::max(cfg_.min_speed, speed() * std::min(1.0, d / cfg_.slow_zone));
|
||||
}
|
||||
return o;
|
||||
}
|
||||
case SlopeTestPhase::kDwell: {
|
||||
if (in.t - dwell_t0_ < cfg_.dwell_s) return o; // dung yen
|
||||
if (down_) {
|
||||
down_ = false;
|
||||
} else if (loop_ >= cfg_.loops) {
|
||||
phase_ = SlopeTestPhase::kDone;
|
||||
std::ostringstream os;
|
||||
os << "XONG " << cfg_.loops << " luot.";
|
||||
o.event = os.str();
|
||||
return o;
|
||||
} else {
|
||||
++loop_;
|
||||
down_ = true;
|
||||
}
|
||||
BeginLeg(in.t);
|
||||
o.event = "bat dau " + leg_name();
|
||||
return o;
|
||||
}
|
||||
default:
|
||||
return o;
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace vslam
|
||||
Reference in New Issue
Block a user