263 lines
8.7 KiB
C++
263 lines
8.7 KiB
C++
#include "vslam/teleop_model.h"
|
|
|
|
#include <algorithm>
|
|
#include <cmath>
|
|
#include <stdexcept>
|
|
|
|
namespace vslam {
|
|
namespace {
|
|
|
|
double Clamp(double v, double lo, double hi) { return std::max(lo, std::min(v, hi)); }
|
|
|
|
void ValidateFinite(const char* name, double v) {
|
|
if (!std::isfinite(v)) {
|
|
throw std::invalid_argument(std::string(name) + " must be a finite number");
|
|
}
|
|
}
|
|
|
|
void ValidateRange(const char* name, double minimum, double maximum, double step) {
|
|
ValidateFinite(name, minimum);
|
|
ValidateFinite(name, maximum);
|
|
ValidateFinite(name, step);
|
|
if (minimum < 0.0) {
|
|
throw std::invalid_argument(std::string("minimum ") + name +
|
|
" must be non-negative");
|
|
}
|
|
if (maximum < minimum) {
|
|
throw std::invalid_argument(std::string("maximum ") + name +
|
|
" must not be less than its minimum");
|
|
}
|
|
if (step <= 0.0) {
|
|
throw std::invalid_argument(std::string(name) + " step must be positive");
|
|
}
|
|
}
|
|
|
|
void ValidateCommandRange(const char* name, double minimum, double maximum) {
|
|
ValidateFinite(name, minimum);
|
|
ValidateFinite(name, maximum);
|
|
if (minimum > 0.0) {
|
|
throw std::invalid_argument(std::string("minimum ") + name +
|
|
" must be zero or negative");
|
|
}
|
|
if (maximum < 0.0) {
|
|
throw std::invalid_argument(std::string("maximum ") + name +
|
|
" must be zero or positive");
|
|
}
|
|
if (minimum >= maximum) {
|
|
throw std::invalid_argument(std::string("minimum ") + name +
|
|
" must be less than its maximum");
|
|
}
|
|
}
|
|
|
|
} // namespace
|
|
|
|
bool ParseTeleopKey(const std::string& name, TeleopKey* out) {
|
|
if (name == "Up") { *out = TeleopKey::kUp; return true; }
|
|
if (name == "Down") { *out = TeleopKey::kDown; return true; }
|
|
if (name == "Left") { *out = TeleopKey::kLeft; return true; }
|
|
if (name == "Right") { *out = TeleopKey::kRight; return true; }
|
|
return false;
|
|
}
|
|
|
|
const char* TeleopKeyName(TeleopKey k) {
|
|
switch (k) {
|
|
case TeleopKey::kUp: return "Up";
|
|
case TeleopKey::kDown: return "Down";
|
|
case TeleopKey::kLeft: return "Left";
|
|
case TeleopKey::kRight: return "Right";
|
|
}
|
|
return "?";
|
|
}
|
|
|
|
TeleopModel::TeleopModel(TeleopConfig cfg) {
|
|
ValidateRange("linear speed", cfg.min_linear_speed, cfg.max_linear_speed,
|
|
cfg.linear_step);
|
|
ValidateRange("angular speed", cfg.min_angular_speed, cfg.max_angular_speed,
|
|
cfg.angular_step);
|
|
ValidateFinite("linear_speed", cfg.linear_speed);
|
|
ValidateFinite("angular_speed", cfg.angular_speed);
|
|
|
|
// Mac dinh: gioi han duoi doi xung voi gioi han tren.
|
|
const double min_lin_cmd =
|
|
cfg.has_min_linear_command ? cfg.min_linear_command : -cfg.max_linear_speed;
|
|
const double min_ang_cmd =
|
|
cfg.has_min_angular_command ? cfg.min_angular_command : -cfg.max_angular_speed;
|
|
|
|
ValidateCommandRange("linear command", min_lin_cmd, cfg.max_linear_speed);
|
|
ValidateCommandRange("angular command", min_ang_cmd, cfg.max_angular_speed);
|
|
|
|
linear_step_ = cfg.linear_step;
|
|
angular_step_ = cfg.angular_step;
|
|
min_linear_speed_ = cfg.min_linear_speed;
|
|
max_linear_speed_ = cfg.max_linear_speed;
|
|
min_angular_speed_ = cfg.min_angular_speed;
|
|
max_angular_speed_ = cfg.max_angular_speed;
|
|
min_linear_command_ = min_lin_cmd;
|
|
min_angular_command_ = min_ang_cmd;
|
|
linear_speed_ = Clamp(cfg.linear_speed, min_linear_speed_, max_linear_speed_);
|
|
angular_speed_ = Clamp(cfg.angular_speed, min_angular_speed_, max_angular_speed_);
|
|
}
|
|
|
|
// --- Nguon nhap ------------------------------------------------------------
|
|
|
|
bool TeleopModel::SetKey(TeleopKey key, bool pressed) {
|
|
if (pressed) {
|
|
// Bam phim GIAI PHONG lenh manual. Khong bao gio tron hai nguon.
|
|
ReleaseManualControl();
|
|
keys_.insert(key);
|
|
} else {
|
|
keys_.erase(key);
|
|
}
|
|
return true;
|
|
}
|
|
|
|
void TeleopModel::ClearKeys() { keys_.clear(); }
|
|
|
|
void TeleopModel::SetJoystick(double horizontal, double vertical) {
|
|
ValidateFinite("joystick horizontal axis", horizontal);
|
|
ValidateFinite("joystick vertical axis", vertical);
|
|
joy_h_ = Clamp(horizontal, -1.0, 1.0);
|
|
joy_v_ = Clamp(vertical, -1.0, 1.0);
|
|
joystick_active_ = true;
|
|
ReleaseManualControl();
|
|
}
|
|
|
|
void TeleopModel::ReleaseJoystick() { joystick_active_ = false; }
|
|
|
|
void TeleopModel::ClearMotion() {
|
|
ClearKeys();
|
|
ReleaseJoystick();
|
|
ReleaseManualControl();
|
|
}
|
|
|
|
std::pair<double, double> TeleopModel::SetManualCommand(const double* linear_x,
|
|
const double* angular_z) {
|
|
double cur_lin, cur_ang;
|
|
if (!manual_active_) {
|
|
const auto c = Command();
|
|
cur_lin = c.first;
|
|
cur_ang = c.second;
|
|
} else {
|
|
cur_lin = manual_lin_;
|
|
cur_ang = manual_ang_;
|
|
}
|
|
|
|
if (linear_x != nullptr) {
|
|
ValidateFinite("manual linear command", *linear_x);
|
|
cur_lin = *linear_x;
|
|
}
|
|
if (angular_z != nullptr) {
|
|
ValidateFinite("manual angular command", *angular_z);
|
|
cur_ang = *angular_z;
|
|
}
|
|
|
|
ClearKeys();
|
|
ReleaseJoystick();
|
|
const auto clamped = ClampCommand(cur_lin, cur_ang);
|
|
manual_lin_ = clamped.first;
|
|
manual_ang_ = clamped.second;
|
|
manual_active_ = true;
|
|
return clamped;
|
|
}
|
|
|
|
void TeleopModel::ReleaseManualControl() { manual_active_ = false; }
|
|
|
|
// --- Ket qua ---------------------------------------------------------------
|
|
|
|
std::pair<double, double> TeleopModel::ClampCommand(double linear_x,
|
|
double angular_z) const {
|
|
return {Clamp(linear_x, min_linear_command_, max_linear_speed_),
|
|
Clamp(angular_z, min_angular_command_, max_angular_speed_)};
|
|
}
|
|
|
|
void TeleopModel::ClampManualCommand() {
|
|
if (!manual_active_) return;
|
|
const auto c = ClampCommand(manual_lin_, manual_ang_);
|
|
manual_lin_ = c.first;
|
|
manual_ang_ = c.second;
|
|
}
|
|
|
|
std::pair<double, double> TeleopModel::Command() const {
|
|
// Thu tu uu tien: manual > joystick > phim.
|
|
if (manual_active_) return {manual_lin_, manual_ang_};
|
|
|
|
if (joystick_active_) {
|
|
return ClampCommand(-joy_v_ * linear_speed_, -joy_h_ * angular_speed_);
|
|
}
|
|
|
|
const int linear_axis = static_cast<int>(keys_.count(TeleopKey::kUp)) -
|
|
static_cast<int>(keys_.count(TeleopKey::kDown));
|
|
const int angular_axis = static_cast<int>(keys_.count(TeleopKey::kLeft)) -
|
|
static_cast<int>(keys_.count(TeleopKey::kRight));
|
|
return ClampCommand(linear_axis * linear_speed_, angular_axis * angular_speed_);
|
|
}
|
|
|
|
std::pair<double, double> TeleopModel::JoystickPosition() const {
|
|
if (joystick_active_) return {joy_h_, joy_v_};
|
|
const int h = static_cast<int>(keys_.count(TeleopKey::kRight)) -
|
|
static_cast<int>(keys_.count(TeleopKey::kLeft));
|
|
const int v = static_cast<int>(keys_.count(TeleopKey::kDown)) -
|
|
static_cast<int>(keys_.count(TeleopKey::kUp));
|
|
return {static_cast<double>(h), static_cast<double>(v)};
|
|
}
|
|
|
|
// --- Chinh toc do ----------------------------------------------------------
|
|
|
|
double TeleopModel::AdjustLinearSpeed(double direction) {
|
|
return SetLinearSpeed(linear_speed_ + direction * linear_step_);
|
|
}
|
|
|
|
double TeleopModel::AdjustAngularSpeed(double direction) {
|
|
return SetAngularSpeed(angular_speed_ + direction * angular_step_);
|
|
}
|
|
|
|
double TeleopModel::SetLinearSpeed(double value) {
|
|
ValidateFinite("linear speed", value);
|
|
linear_speed_ = Clamp(value, min_linear_speed_, max_linear_speed_);
|
|
return linear_speed_;
|
|
}
|
|
|
|
double TeleopModel::SetAngularSpeed(double value) {
|
|
ValidateFinite("angular speed", value);
|
|
angular_speed_ = Clamp(value, min_angular_speed_, max_angular_speed_);
|
|
return angular_speed_;
|
|
}
|
|
|
|
double TeleopModel::SetMaxLinearSpeed(double value) {
|
|
ValidateFinite("maximum linear speed", value);
|
|
max_linear_speed_ = std::max(min_linear_speed_, value);
|
|
linear_speed_ = std::min(linear_speed_, max_linear_speed_);
|
|
ClampManualCommand();
|
|
return max_linear_speed_;
|
|
}
|
|
|
|
double TeleopModel::SetMaxAngularSpeed(double value) {
|
|
ValidateFinite("maximum angular speed", value);
|
|
max_angular_speed_ = std::max(min_angular_speed_, value);
|
|
angular_speed_ = std::min(angular_speed_, max_angular_speed_);
|
|
ClampManualCommand();
|
|
return max_angular_speed_;
|
|
}
|
|
|
|
double TeleopModel::SetMinLinearCommand(double value) {
|
|
ValidateFinite("minimum linear command", value);
|
|
min_linear_command_ = std::min(0.0, value);
|
|
if (min_linear_command_ >= max_linear_speed_) {
|
|
min_linear_command_ = std::min(0.0, max_linear_speed_ - linear_step_);
|
|
}
|
|
ClampManualCommand();
|
|
return min_linear_command_;
|
|
}
|
|
|
|
double TeleopModel::SetMinAngularCommand(double value) {
|
|
ValidateFinite("minimum angular command", value);
|
|
min_angular_command_ = std::min(0.0, value);
|
|
if (min_angular_command_ >= max_angular_speed_) {
|
|
min_angular_command_ = std::min(0.0, max_angular_speed_ - angular_step_);
|
|
}
|
|
ClampManualCommand();
|
|
return min_angular_command_;
|
|
}
|
|
|
|
} // namespace vslam
|