145 lines
5.3 KiB
C++
145 lines
5.3 KiB
C++
#ifndef VSLAM_TELEOP_MODEL_H
|
|
#define VSLAM_TELEOP_MODEL_H
|
|
|
|
// Mo hinh trang thai teleop.
|
|
//
|
|
// ============ NGUON GOC ============
|
|
// Port tu robot_bringup/scripts/teleop_model.py cua vSLAM_ws, ma ban than no
|
|
// duoc port tu package `virtual_joystick_teleop` (BSD-3-Clause), tac gia
|
|
// duongtd, https://git.pnkr.asia/DuongTD/virtual_joystick_teleop.git
|
|
//
|
|
// LOGIC GIU NGUYEN VEN. Day la ban dich sang C++, khong phai ban viet lai:
|
|
// moi nguong, moi thu tu uu tien, moi phep kep deu giong het ban Python.
|
|
// test_teleop_model.cc so ket qua voi vector sinh tu chinh ban Python do.
|
|
//
|
|
// ============ VI SAO KHONG VIET LAI BANG JS ============
|
|
// Ban dau toi viet mot joystick moi hoan toan trong app.js va bo qua file
|
|
// nay. Do la sai: mo hinh nay da chay that, da duoc chinh theo cam giac lai
|
|
// that, va co nhung thu mot ban viet voi khong co -- nhat la toc do chinh
|
|
// duoc khi dang chay.
|
|
//
|
|
// Trang web chi gui truc joystick da chuan hoa [-1, 1] va su kien phim.
|
|
// TOAN BO viec quy ra van toc nam o day, trong C++.
|
|
//
|
|
// ============ BA NGUON NHAP, CO THU TU UU TIEN ============
|
|
// manual (thanh truot kieu rqt) -- cao nhat
|
|
// joystick (truc analog)
|
|
// phim mui ten -- thap nhat
|
|
// Dat mot nguon se GIAI PHONG cac nguon khac. Khong bao gio tron hai nguon.
|
|
//
|
|
// LUU Y: mo hinh nay KHONG co ramp gia toc. Ramp nam o tang duoi (gioi han
|
|
// gia toc cua base). Do la dung: teleop noi "muon di bao nhanh", con base noi
|
|
// "den do nhanh duoc bao nhieu".
|
|
|
|
#include <set>
|
|
#include <string>
|
|
#include <utility>
|
|
|
|
namespace vslam {
|
|
|
|
enum class TeleopKey { kUp, kDown, kLeft, kRight };
|
|
|
|
// Tra ve false neu ten phim khong duoc ho tro. Ten khop ban Python:
|
|
// "Up", "Down", "Left", "Right".
|
|
bool ParseTeleopKey(const std::string& name, TeleopKey* out);
|
|
const char* TeleopKeyName(TeleopKey k);
|
|
|
|
struct TeleopConfig {
|
|
double linear_speed = 0.30; // scale hien tai, chinh duoc luc chay
|
|
double angular_speed = 0.70;
|
|
double linear_step = 0.05; // buoc chinh
|
|
double angular_step = 0.10;
|
|
double min_linear_speed = 0.05; // gioi han cua scale
|
|
double max_linear_speed = 2.00;
|
|
double min_angular_speed = 0.10;
|
|
double max_angular_speed = 2.00;
|
|
// Gioi han duoi cua LENH (co dau). Mac dinh = -max_*_speed.
|
|
// Tach rieng de gioi han lui khac gioi han tien duoc.
|
|
bool has_min_linear_command = false;
|
|
double min_linear_command = 0.0;
|
|
bool has_min_angular_command = false;
|
|
double min_angular_command = 0.0;
|
|
};
|
|
|
|
class TeleopModel {
|
|
public:
|
|
// Nem std::invalid_argument neu cau hinh vo ly, giong ban Python nem
|
|
// ValueError. Kiem o day chu khong im lang roi ve default: mot gioi han
|
|
// van toc sai la mot robot chay nhanh hon nguoi lai nghi.
|
|
explicit TeleopModel(TeleopConfig cfg = TeleopConfig());
|
|
|
|
// --- Nguon nhap ----------------------------------------------------------
|
|
|
|
// Dat trang thai mot phim. Tra false neu phim khong ho tro.
|
|
bool SetKey(TeleopKey key, bool pressed);
|
|
void ClearKeys();
|
|
|
|
// Truc analog trong [-1, 1] tu joystick chuot/cham.
|
|
void SetJoystick(double horizontal, double vertical);
|
|
void ReleaseJoystick();
|
|
|
|
// Lenh co dau ben vung, kieu thanh truot rqt.
|
|
// Truyen nullptr cho truc khong muon doi.
|
|
std::pair<double, double> SetManualCommand(const double* linear_x,
|
|
const double* angular_z);
|
|
void ReleaseManualControl();
|
|
|
|
// Nha het moi nguon -> lenh ve 0.
|
|
void ClearMotion();
|
|
|
|
// --- Ket qua -------------------------------------------------------------
|
|
|
|
// (linear_x m/s, angular_z rad/s).
|
|
std::pair<double, double> Command() const;
|
|
|
|
// Truc hien thi [-1, 1] cho knob tren trang. Phim mui ten cung lam knob
|
|
// dich -- de nguoi lai thay duoc minh dang bam gi.
|
|
std::pair<double, double> JoystickPosition() const;
|
|
|
|
// --- Chinh toc do luc dang chay -----------------------------------------
|
|
|
|
double AdjustLinearSpeed(double direction);
|
|
double AdjustAngularSpeed(double direction);
|
|
double SetLinearSpeed(double value);
|
|
double SetAngularSpeed(double value);
|
|
double SetMaxLinearSpeed(double value);
|
|
double SetMaxAngularSpeed(double value);
|
|
double SetMinLinearCommand(double value);
|
|
double SetMinAngularCommand(double value);
|
|
|
|
// --- Truy van ------------------------------------------------------------
|
|
|
|
double linear_speed() const { return linear_speed_; }
|
|
double angular_speed() const { return angular_speed_; }
|
|
double max_linear_speed() const { return max_linear_speed_; }
|
|
double max_angular_speed() const { return max_angular_speed_; }
|
|
double min_linear_command() const { return min_linear_command_; }
|
|
double min_angular_command() const { return min_angular_command_; }
|
|
|
|
bool joystick_active() const { return joystick_active_; }
|
|
bool manual_control_active() const { return manual_active_; }
|
|
bool key_pressed(TeleopKey k) const { return keys_.count(k) != 0; }
|
|
|
|
private:
|
|
std::pair<double, double> ClampCommand(double linear_x, double angular_z) const;
|
|
void ClampManualCommand();
|
|
|
|
double linear_step_, angular_step_;
|
|
double min_linear_speed_, max_linear_speed_;
|
|
double min_angular_speed_, max_angular_speed_;
|
|
double min_linear_command_, min_angular_command_;
|
|
double linear_speed_, angular_speed_;
|
|
|
|
std::set<TeleopKey> keys_;
|
|
|
|
bool joystick_active_ = false;
|
|
double joy_h_ = 0.0, joy_v_ = 0.0;
|
|
|
|
bool manual_active_ = false;
|
|
double manual_lin_ = 0.0, manual_ang_ = 0.0;
|
|
};
|
|
|
|
} // namespace vslam
|
|
|
|
#endif // VSLAM_TELEOP_MODEL_H
|