update
This commit is contained in:
76
libs/vslam_drivers/thirdparty/zlac/include/zlac_ros/differential_drive_kinematics.hpp
vendored
Normal file
76
libs/vslam_drivers/thirdparty/zlac/include/zlac_ros/differential_drive_kinematics.hpp
vendored
Normal file
@@ -0,0 +1,76 @@
|
||||
#ifndef ZLAC_ROS__DIFFERENTIAL_DRIVE_KINEMATICS_HPP_
|
||||
#define ZLAC_ROS__DIFFERENTIAL_DRIVE_KINEMATICS_HPP_
|
||||
|
||||
#include <cmath>
|
||||
#include <cstdint>
|
||||
|
||||
namespace zlac_ros {
|
||||
|
||||
/**
|
||||
* @brief Kinematic parameters loaded from configuration file.
|
||||
*/
|
||||
struct KinematicsConfig {
|
||||
double wheel_diameter_m{0.173}; // 173 mm (r = 0.0865 m)
|
||||
double wheel_base_m{0.400}; // 400 mm (L = 0.400 m)
|
||||
bool invert_left_wheel{false};
|
||||
bool invert_right_wheel{true}; // Invert right wheel direction by default
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief Individual wheel RPM target output.
|
||||
*/
|
||||
struct WheelSpeedRPM {
|
||||
int16_t left{0};
|
||||
int16_t right{0};
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief 2D Velocity Twist (linear_x m/s, angular_z rad/s).
|
||||
*/
|
||||
struct Twist2D {
|
||||
double linear_x{0.0}; // m/s
|
||||
double angular_z{0.0}; // rad/s
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief 2D Robot Pose (x m, y m, theta rad).
|
||||
*/
|
||||
struct Pose2D {
|
||||
double x{0.0}; // m
|
||||
double y{0.0}; // m
|
||||
double theta{0.0}; // rad
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief Standalone C++ Differential Drive Kinematics & Odometry Class.
|
||||
* Implements exact Forward/Inverse kinematics from odometry_kinematics_summary.md.
|
||||
*/
|
||||
class DifferentialDriveKinematics {
|
||||
public:
|
||||
explicit DifferentialDriveKinematics(const KinematicsConfig& config = KinematicsConfig());
|
||||
|
||||
/// Inverse Kinematics: Converts (v, omega) -> (RPM_left, RPM_right)
|
||||
WheelSpeedRPM computeInverseKinematics(double linear_x, double angular_z) const;
|
||||
|
||||
/// Forward Kinematics: Converts (RPM_left, RPM_right) -> (v, omega)
|
||||
Twist2D computeForwardKinematics(double rpm_left, double rpm_right) const;
|
||||
|
||||
/// Odometry Integration: Updates 2D pose (x, y, theta) over time step dt
|
||||
void updateOdometry(double rpm_left, double rpm_right, double dt, bool use_imu = false, double imu_angular_z = 0.0);
|
||||
|
||||
/// Pose Accessors & Reset
|
||||
Pose2D getPose() const;
|
||||
void resetPose(double x = 0.0, double y = 0.0, double theta = 0.0);
|
||||
|
||||
const KinematicsConfig& getConfig() const { return config_; }
|
||||
void setConfig(const KinematicsConfig& config);
|
||||
|
||||
private:
|
||||
KinematicsConfig config_;
|
||||
double wheel_radius_m_;
|
||||
Pose2D current_pose_;
|
||||
};
|
||||
|
||||
} // namespace zlac_ros
|
||||
|
||||
#endif // ZLAC_ROS__DIFFERENTIAL_DRIVE_KINEMATICS_HPP_
|
||||
167
libs/vslam_drivers/thirdparty/zlac/include/zlac_ros/zlac_motor_driver.hpp
vendored
Normal file
167
libs/vslam_drivers/thirdparty/zlac/include/zlac_ros/zlac_motor_driver.hpp
vendored
Normal file
@@ -0,0 +1,167 @@
|
||||
#ifndef ZLAC_ROS__ZLAC_MOTOR_DRIVER_HPP_
|
||||
#define ZLAC_ROS__ZLAC_MOTOR_DRIVER_HPP_
|
||||
|
||||
#include <lely/coapp/driver.hpp>
|
||||
#include <lely/coapp/master.hpp>
|
||||
#include <lely/ev/loop.hpp>
|
||||
#include <lely/io2/linux/can.hpp>
|
||||
#include <lely/io2/sys/io.hpp>
|
||||
#include <lely/io2/sys/timer.hpp>
|
||||
#include <lely/io2/posix/poll.hpp>
|
||||
|
||||
#include <iostream>
|
||||
#include <memory>
|
||||
#include <atomic>
|
||||
#include <thread>
|
||||
#include <chrono>
|
||||
#include <string>
|
||||
#include <future>
|
||||
#include <optional>
|
||||
#include <system_error>
|
||||
|
||||
namespace zlac_ros {
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @brief Synchronous SDO write helper for thread-safe sequential execution.
|
||||
*/
|
||||
template <typename T>
|
||||
bool sdo_write_sync(lely::canopen::AsyncMaster& master, uint8_t node_id, uint16_t idx, uint8_t subidx, T val) {
|
||||
std::promise<bool> prom;
|
||||
auto fut = prom.get_future();
|
||||
master.SubmitWrite(master.get_executor(), node_id, idx, subidx, val,
|
||||
[&prom](uint8_t, uint16_t, uint8_t, std::error_code ec) {
|
||||
prom.set_value(!ec);
|
||||
});
|
||||
if (fut.wait_for(std::chrono::seconds(2)) == std::future_status::ready) {
|
||||
return fut.get();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Synchronous SDO read helper.
|
||||
*/
|
||||
template <typename T>
|
||||
std::optional<T> sdo_read_sync(lely::canopen::AsyncMaster& master, uint8_t node_id, uint16_t idx, uint8_t subidx) {
|
||||
std::promise<std::optional<T>> prom;
|
||||
auto fut = prom.get_future();
|
||||
master.SubmitRead<T>(master.get_executor(), node_id, idx, subidx,
|
||||
[&prom](uint8_t, uint16_t, uint8_t, std::error_code ec, T val) {
|
||||
if (ec) {
|
||||
prom.set_value(std::nullopt);
|
||||
} else {
|
||||
prom.set_value(val);
|
||||
}
|
||||
});
|
||||
if (fut.wait_for(std::chrono::seconds(2)) == std::future_status::ready) {
|
||||
return fut.get();
|
||||
}
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
class ZlacMotorDriver; // Forward declaration
|
||||
|
||||
/**
|
||||
* @brief Lely CANopen Basic Driver implementation for ZLAC8015D.
|
||||
*/
|
||||
class ZlacLelyDriver : public lely::canopen::BasicDriver {
|
||||
public:
|
||||
ZlacLelyDriver(lely::canopen::AsyncMaster& master, uint8_t id, ZlacMotorDriver* parent)
|
||||
: BasicDriver(master, id), parent_(parent) {}
|
||||
|
||||
protected:
|
||||
void OnBoot(lely::canopen::NmtState st, char es, const std::string& what) noexcept override;
|
||||
void OnHeartbeat(bool occurred) noexcept override;
|
||||
|
||||
private:
|
||||
ZlacMotorDriver* parent_{nullptr};
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief Standalone Core C++ Motor Driver class for ZLAC8015D.
|
||||
* Decoupled from ROS2 Node logic. Encapsulates all Lely CANopen communication.
|
||||
*/
|
||||
class ZlacMotorDriver {
|
||||
public:
|
||||
ZlacMotorDriver(const std::string& can_channel, uint8_t node_id, const std::string& dcf_path);
|
||||
~ZlacMotorDriver();
|
||||
|
||||
/// Initialize CANopen master stack and start Lely event loop
|
||||
bool initMaster();
|
||||
|
||||
/// Configure DS402 Profile Velocity Mode (Mode 3) and enable power stage
|
||||
bool enableHardware(uint32_t acc_dec = 500);
|
||||
|
||||
/// Safely disable motor power stage and send NMT STOP
|
||||
void disableHardware();
|
||||
|
||||
/// Quick Stop - Stop motors using Quick Stop command (0x0006)
|
||||
/// Slightly safer than emergency stop as it follows deceleration ramp
|
||||
void quickStop();
|
||||
|
||||
/// Send Target Velocities (RPM) to Left and Right motors
|
||||
bool setTargetRPM(int16_t left_rpm, int16_t right_rpm);
|
||||
|
||||
/// Read actual velocity telemetry (RPM) from driver
|
||||
bool getActualRPM(double& out_left_rpm, double& out_right_rpm);
|
||||
|
||||
// [vslam] Doc vi tri TICH LUY tu 0x6064 (Position actual value), don vi
|
||||
// "count". Ban goc chi co getActualRPM() -- tuc chi co van toc, va tich
|
||||
// phan van toc thi lo mot mau telemetry la mat vinh vien mot doan quang
|
||||
// duong ma khong phat hien duoc. Xem thirdparty/PROVENANCE.md.
|
||||
bool getPositionCounts(int32_t& out_left, int32_t& out_right);
|
||||
|
||||
bool isReady() const { return hardware_ready_.load(); }
|
||||
bool hasConnectionLost() const { return connection_lost_.load(); }
|
||||
void recoverConnection();
|
||||
void notifyConnectionLost();
|
||||
void notifyHeartbeatStatus(bool is_lost);
|
||||
|
||||
using ConnectionCallback = std::function<void(bool)>;
|
||||
void setConnectionCallback(ConnectionCallback cb) { connection_cb_ = cb; }
|
||||
|
||||
private:
|
||||
ConnectionCallback connection_cb_;
|
||||
std::string can_channel_;
|
||||
uint8_t node_id_;
|
||||
std::string dcf_path_;
|
||||
|
||||
std::unique_ptr<lely::io::IoGuard> io_guard_;
|
||||
std::unique_ptr<lely::io::Context> ctx_;
|
||||
std::unique_ptr<lely::io::Poll> poll_;
|
||||
std::unique_ptr<lely::ev::Loop> loop_;
|
||||
|
||||
std::unique_ptr<lely::io::Timer> timer_;
|
||||
std::unique_ptr<lely::io::CanController> ctrl_;
|
||||
std::unique_ptr<lely::io::CanChannel> chan_;
|
||||
|
||||
std::unique_ptr<lely::canopen::AsyncMaster> master_;
|
||||
std::unique_ptr<ZlacLelyDriver> driver_;
|
||||
std::thread lely_thread_;
|
||||
|
||||
std::atomic<bool> master_initialized_{false};
|
||||
std::atomic<bool> hardware_ready_{false};
|
||||
std::atomic<bool> connection_lost_{false};
|
||||
|
||||
// Telemetry caching
|
||||
std::thread telemetry_thread_;
|
||||
std::atomic<int32_t> cached_left_vel_raw_{0};
|
||||
std::atomic<int32_t> cached_right_vel_raw_{0};
|
||||
std::atomic<bool> cached_vel_valid_{false};
|
||||
// [vslam] Cache vi tri, cap nhat trong cung telemetryLoop voi van toc.
|
||||
std::atomic<int32_t> cached_left_pos_raw_{0};
|
||||
std::atomic<int32_t> cached_right_pos_raw_{0};
|
||||
std::atomic<bool> cached_pos_valid_{false};
|
||||
void telemetryLoop();
|
||||
|
||||
// Target caching
|
||||
int16_t last_target_left_rpm_{-1};
|
||||
int16_t last_target_right_rpm_{-1};
|
||||
bool pdo_mapped_{false};
|
||||
};
|
||||
|
||||
} // namespace zlac_ros
|
||||
|
||||
#endif // ZLAC_ROS__ZLAC_MOTOR_DRIVER_HPP_
|
||||
Reference in New Issue
Block a user