From 2fdab682b4160c39eb33808f84e0b269333f63ea Mon Sep 17 00:00:00 2001 From: Yukikaze2233 Date: Sat, 9 May 2026 00:51:32 +0800 Subject: [PATCH 01/52] refactor(deformable-chassis): merge refactored implementation and clean up dead code - Replace deformable_chassis.cpp with refactored version from refactor/deformable-infantry branch - Add IMU auto-calibration for pitch/roll offset - Inline JointFeedbackSource/JointIndex/JointFeedbackFrame types, removing dependency on deformable_joint_layer.hpp - Rename suspension PID variables to original naming convention (pitch_kp_, pitch_ki_, pitch_kd_, roll_kp_, roll_ki_, roll_kd_) - Remove unused SuspensionPhase enum - Remove 8 dead suspension output interfaces (suspension_mode/suspension_torque always false/NaN) - Fix config parameter names to match existing YAML config - Delete deformable_joint_layer.hpp (merged into deformable_chassis.cpp) --- .../controller/chassis/deformable_chassis.cpp | 1860 +++++++++-------- .../chassis/deformable_joint_layer.hpp | 172 -- 2 files changed, 1000 insertions(+), 1032 deletions(-) delete mode 100644 rmcs_ws/src/rmcs_core/src/controller/chassis/deformable_joint_layer.hpp diff --git a/rmcs_ws/src/rmcs_core/src/controller/chassis/deformable_chassis.cpp b/rmcs_ws/src/rmcs_core/src/controller/chassis/deformable_chassis.cpp index c9c61ace..0d3b36ca 100644 --- a/rmcs_ws/src/rmcs_core/src/controller/chassis/deformable_chassis.cpp +++ b/rmcs_ws/src/rmcs_core/src/controller/chassis/deformable_chassis.cpp @@ -2,7 +2,7 @@ #include #include #include -#include +#include #include #include #include @@ -19,997 +19,1137 @@ #include #include "controller/pid/pid_calculator.hpp" -#include "deformable_joint_layer.hpp" namespace rmcs_core::controller::chassis { -enum class SuspensionPhase : uint8_t { kInactive, kArming, kActive, kReleasing }; +class DeformableChassis + : public rmcs_executor::Component + , public rclcpp::Node { +public: + enum class JointFeedbackSource : uint8_t { kLegacyEncoderAngle, kMotorAngle }; + enum JointIndex : size_t { + kLeftFront = 0, + kLeftBack = 1, + kRightBack = 2, + kRightFront = 3, + kJointCount = 4, + }; -struct AttitudeBias { - double pitch_force = 0.0; - double roll_force = 0.0; -}; + struct JointFeedbackFrame { + std::array motor_angles{}; + std::array physical_angles{}; + std::array physical_velocities{}; + std::array joint_torques{}; + std::array eso_z2{}; + std::array eso_z3{}; + }; -struct LegControlState { - SuspensionPhase phase = SuspensionPhase::kInactive; - double support_force = 0.0; - double contact_confidence = 1.0; - double filtered_contact_confidence = 1.0; - double phase_elapsed = 0.0; - bool requested_deploy = false; - bool output_active = false; - bool contact_latched = false; -}; + struct AttitudePidAxis { + double kp = 20.0; + double ki = 0.0; + double kd = 0.0; + double integral = 0.0; + double integral_limit = std::numeric_limits::infinity(); + double output_limit = std::numeric_limits::infinity(); -struct LegCommand { - double requested_target_angle = std::numeric_limits::quiet_NaN(); - double final_target_angle = std::numeric_limits::quiet_NaN(); - double target_velocity = 0.0; - double target_acceleration = 0.0; - bool suspension_mode = false; - double suspension_torque = std::numeric_limits::quiet_NaN(); -}; + void reset() { integral = 0.0; } -struct AttitudePidAxis { - double kp = 20.0; - double ki = 0.0; - double kd = 0.0; - double integral = 0.0; - double integral_limit = std::numeric_limits::infinity(); - double output_limit = std::numeric_limits::infinity(); - - void reset() { integral = 0.0; } + double update(double error, double rate, double dt) { + if (!std::isfinite(error) || !std::isfinite(rate) || !std::isfinite(dt) || dt <= 0.0) { + reset(); + return std::numeric_limits::quiet_NaN(); + } - double update(double error, double rate, double dt) { - if (!std::isfinite(error) || !std::isfinite(rate) || !std::isfinite(dt) || dt <= 0.0) { - reset(); - return std::numeric_limits::quiet_NaN(); + integral = std::clamp(integral + error * dt, -integral_limit, integral_limit); + const double output = kp * error + ki * integral - kd * rate; + return std::clamp(output, -output_limit, output_limit); } - integral = std::clamp(integral + error * dt, -integral_limit, integral_limit); - return std::clamp(kp * error + ki * integral - kd * rate, -output_limit, output_limit); - } -}; + }; -struct SuspensionParams { - double mass, rod_length, Kz, pitch_kp, pitch_ki, pitch_kd, roll_kp, roll_ki, roll_kd, D_leg; - double com_height, wheel_base_half_x, wheel_base_half_y; - double gravity_comp_gain, control_acceleration_limit; - double preload_angle, entry_offset, ride_height_offset, hold_travel; - double activation_velocity_threshold; - double target_physical_velocity_limit, target_physical_acceleration_limit; - double torque_limit; - double pitch_angle_diff_limit, roll_angle_diff_limit, pid_integral_limit; -}; + DeformableChassis() + : Node( + get_component_name(), + rclcpp::NodeOptions{}.automatically_declare_parameters_from_overrides(true)) + , following_velocity_controller_(10.0, 0.0, 0.0) + , spin_ratio_(std::clamp(get_parameter_or("spin_ratio", 0.6), 0.0, 1.0)) + + , min_angle_(get_parameter_or("min_angle", 15.0)) + , max_angle_(get_parameter_or("max_angle", 55.0)) + , left_front_joint_offset_(get_parameter_or("left_front_joint_offset", 0.0)) + , left_back_joint_offset_(get_parameter_or("left_back_joint_offset", 0.0)) + , right_front_joint_offset_(get_parameter_or("right_front_joint_offset", 0.0)) + , right_back_joint_offset_(get_parameter_or("right_back_joint_offset", 0.0)) + , target_physical_velocity_limit_( + std::max( + deg_to_rad(std::abs(get_parameter_or("target_physical_velocity_limit", 180.0))), + 1e-6)) + , target_physical_acceleration_limit_( + std::max( + deg_to_rad( + std::abs(get_parameter_or("target_physical_acceleration_limit", 720.0))), + 1e-6)) + , active_suspension_enable_(get_parameter_or("active_suspension_enable", false)) + , pitch_kp_(get_parameter_or("active_suspension_pitch_kp", 200.0)) + , pitch_ki_(get_parameter_or("active_suspension_pitch_ki", 0.0)) + , pitch_kd_(get_parameter_or("active_suspension_pitch_kd", 20.0)) + , roll_kp_(get_parameter_or("active_suspension_roll_kp", 200.0)) + , roll_ki_(get_parameter_or("active_suspension_roll_ki", 0.0)) + , roll_kd_(get_parameter_or("active_suspension_roll_kd", 20.0)) + , suspension_velocity_limit_( + std::max( + deg_to_rad( + std::abs(get_parameter_or( + "active_suspension_target_velocity_limit_deg", + get_parameter_or("target_physical_velocity_limit", 180.0)))), + 1e-6)) + , suspension_acceleration_limit_( + std::max( + deg_to_rad( + std::abs(get_parameter_or( + "active_suspension_target_acceleration_limit_deg", + get_parameter_or("target_physical_acceleration_limit", 720.0)))), + 1e-6)) + , pitch_diff_limit_( + std::abs(get_parameter_or( + "active_suspension_pitch_angle_diff_limit_deg", max_angle_ - min_angle_)) + * std::numbers::pi / 180.0) + , roll_diff_limit_( + std::abs(get_parameter_or( + "active_suspension_roll_angle_diff_limit_deg", max_angle_ - min_angle_)) + * std::numbers::pi / 180.0) + , pid_integral_limit_( + std::abs(get_parameter_or( + "active_suspension_pid_integral_limit_deg", max_angle_ - min_angle_)) + * std::numbers::pi / 180.0) + , chassis_imu_calibration_wait_time_( + std::max(get_parameter_or("chassis_imu_calibration_wait_s", 2.0), 0.0)) + , chassis_imu_calibration_sample_time_( + std::max(get_parameter_or("chassis_imu_calibration_sample_s", 3.0), 1e-6)) { + + following_velocity_controller_.output_max = angular_velocity_max_; + following_velocity_controller_.output_min = -angular_velocity_max_; + pitch_pid_.kp = pitch_kp_; + pitch_pid_.ki = pitch_ki_; + pitch_pid_.kd = pitch_kd_; + pitch_pid_.integral_limit = pid_integral_limit_; + pitch_pid_.output_limit = pitch_diff_limit_; + roll_pid_.kp = roll_kp_; + roll_pid_.ki = roll_ki_; + roll_pid_.kd = roll_kd_; + roll_pid_.integral_limit = pid_integral_limit_; + roll_pid_.output_limit = roll_diff_limit_; -// --- ChassisVelocityControl --- -struct ChassisVelocityControl { - static constexpr double kTranslationalVelocityMax = 10.0; - static constexpr double kAngularVelocityMax = 10.0; + register_input("/remote/joystick/right", joystick_right_); + register_input("/remote/joystick/left", joystick_left_); + register_input("/remote/switch/right", switch_right_); + register_input("/remote/switch/left", switch_left_); + register_input("/remote/mouse/velocity", mouse_velocity_); + register_input("/remote/mouse", mouse_); + register_input("/remote/keyboard", keyboard_); + register_input("/remote/rotary_knob", rotary_knob_); + register_input("/predefined/update_rate", update_rate_); - void configure(double spin_ratio) { - spin_ratio_ = std::clamp(spin_ratio, 0.0, 1.0); - following_velocity_controller_.output_max = kAngularVelocityMax; - following_velocity_controller_.output_min = -kAngularVelocityMax; - } + register_input("/gimbal/yaw/angle", gimbal_yaw_angle_, false); + register_input("/gimbal/yaw/control_angle_error", gimbal_yaw_angle_error_, false); - void set_spin_forward(bool forward) { spinning_forward_ = forward; } + register_input("/chassis/left_front_joint/angle", left_front_joint_angle_, false); + register_input("/chassis/left_back_joint/angle", left_back_joint_angle_, false); + register_input("/chassis/right_front_joint/angle", right_front_joint_angle_, false); + register_input("/chassis/right_back_joint/angle", right_back_joint_angle_, false); + + register_input( + "/chassis/left_front_joint/physical_angle", left_front_joint_physical_angle_, false); + register_input( + "/chassis/left_back_joint/physical_angle", left_back_joint_physical_angle_, false); + register_input( + "/chassis/right_front_joint/physical_angle", right_front_joint_physical_angle_, false); + register_input( + "/chassis/right_back_joint/physical_angle", right_back_joint_physical_angle_, false); + register_input( + "/chassis/left_front_joint/physical_velocity", left_front_joint_physical_velocity_, + false); + register_input( + "/chassis/left_back_joint/physical_velocity", left_back_joint_physical_velocity_, + false); + register_input( + "/chassis/right_back_joint/physical_velocity", right_back_joint_physical_velocity_, + false); + register_input( + "/chassis/right_front_joint/physical_velocity", right_front_joint_physical_velocity_, + false); + register_input("/chassis/left_front_joint/torque", left_front_joint_torque_, false); + register_input("/chassis/left_back_joint/torque", left_back_joint_torque_, false); + register_input("/chassis/right_back_joint/torque", right_back_joint_torque_, false); + register_input("/chassis/right_front_joint/torque", right_front_joint_torque_, false); + + register_input( + "/chassis/left_front_joint/encoder_angle", left_front_joint_encoder_angle_, false); + register_input( + "/chassis/left_back_joint/encoder_angle", left_back_joint_encoder_angle_, false); + register_input( + "/chassis/right_front_joint/encoder_angle", right_front_joint_encoder_angle_, false); + register_input( + "/chassis/right_back_joint/encoder_angle", right_back_joint_encoder_angle_, false); + register_input("/chassis/imu/pitch", chassis_imu_pitch_, false); + register_input("/chassis/imu/roll", chassis_imu_roll_, false); + register_input("/chassis/imu/pitch_rate", chassis_imu_pitch_rate_, false); + register_input("/chassis/imu/roll_rate", chassis_imu_roll_rate_, false); - Eigen::Vector2d compute_translational( - const Eigen::Vector2d& joystick_right, const rmcs_msgs::Keyboard& keyboard, - double gimbal_yaw_angle) { - Eigen::Vector2d tv = - Eigen::Rotation2Dd{gimbal_yaw_angle} - * (joystick_right + Eigen::Vector2d{keyboard.w - keyboard.s, keyboard.a - keyboard.d}); - if (tv.norm() > 1.0) - tv.normalize(); - return tv * kTranslationalVelocityMax; - } + register_output("/gimbal/scope/control_torque", scope_motor_control_torque, nan_); - struct AngularResult { - double angular_velocity = 0.0; - double chassis_angle = std::numeric_limits::quiet_NaN(); - double chassis_control_angle = std::numeric_limits::quiet_NaN(); - }; + register_output("/chassis/angle", chassis_angle_, nan_); + register_output("/chassis/control_angle", chassis_control_angle_, nan_); - AngularResult compute_angular( - rmcs_msgs::ChassisMode mode, double gimbal_yaw_angle, double gimbal_yaw_angle_error, - bool apply_toggle_forward) { - AngularResult result; - switch (mode) { - case rmcs_msgs::ChassisMode::AUTO: break; - case rmcs_msgs::ChassisMode::SPIN: - if (apply_toggle_forward) - spinning_forward_ = !spinning_forward_; - result.angular_velocity = std::clamp( - spin_ratio_ * (spinning_forward_ ? kAngularVelocityMax : -kAngularVelocityMax), - -kAngularVelocityMax, kAngularVelocityMax); - break; - case rmcs_msgs::ChassisMode::STEP_DOWN: - result.angular_velocity = following_velocity_controller_.update(calc_angle_err_( - result.chassis_control_angle, gimbal_yaw_angle_error, gimbal_yaw_angle, - std::numbers::pi)); - break; - case rmcs_msgs::ChassisMode::LAUNCH_RAMP: { - double e = calc_angle_err_( - result.chassis_control_angle, gimbal_yaw_angle_error, gimbal_yaw_angle, - 2 * std::numbers::pi); - if (e > std::numbers::pi) - e -= 2 * std::numbers::pi; - result.angular_velocity = following_velocity_controller_.update(e); - break; - } - default: break; - } - result.chassis_angle = 2 * std::numbers::pi - gimbal_yaw_angle; - return result; + register_output("/chassis/control_mode", mode_); + register_output("/chassis/control_velocity", chassis_control_velocity_); + + register_output("/chassis/left_front_joint/control_angle_error", lf_angle_error_, nan_); + register_output("/chassis/left_back_joint/control_angle_error", lb_angle_error_, nan_); + register_output("/chassis/right_front_joint/control_angle_error", rf_angle_error_, nan_); + register_output("/chassis/right_back_joint/control_angle_error", rb_angle_error_, nan_); + + register_output( + "/chassis/left_front_joint/target_angle", left_front_joint_target_angle_, nan_); + register_output( + "/chassis/left_back_joint/target_angle", left_back_joint_target_angle_, nan_); + register_output( + "/chassis/right_back_joint/target_angle", right_back_joint_target_angle_, nan_); + register_output( + "/chassis/right_front_joint/target_angle", right_front_joint_target_angle_, nan_); + + register_output( + "/chassis/left_front_joint/target_physical_angle", + left_front_joint_target_physical_angle_, nan_); + register_output( + "/chassis/left_back_joint/target_physical_angle", + left_back_joint_target_physical_angle_, nan_); + register_output( + "/chassis/right_back_joint/target_physical_angle", + right_back_joint_target_physical_angle_, nan_); + register_output( + "/chassis/right_front_joint/target_physical_angle", + right_front_joint_target_physical_angle_, nan_); + register_output( + "/chassis/left_front_joint/target_physical_velocity", + left_front_joint_target_physical_velocity_, nan_); + register_output( + "/chassis/left_back_joint/target_physical_velocity", + left_back_joint_target_physical_velocity_, nan_); + register_output( + "/chassis/right_back_joint/target_physical_velocity", + right_back_joint_target_physical_velocity_, nan_); + register_output( + "/chassis/right_front_joint/target_physical_velocity", + right_front_joint_target_physical_velocity_, nan_); + register_output( + "/chassis/left_front_joint/target_physical_acceleration", + left_front_joint_target_physical_acceleration_, nan_); + register_output( + "/chassis/left_back_joint/target_physical_acceleration", + left_back_joint_target_physical_acceleration_, nan_); + register_output( + "/chassis/right_back_joint/target_physical_acceleration", + right_back_joint_target_physical_acceleration_, nan_); + register_output( + "/chassis/right_front_joint/target_physical_acceleration", + right_front_joint_target_physical_acceleration_, nan_); + register_output("/chassis/processed_encoder/angle", processed_encoder_angle_, nan_); + + *mode_ = rmcs_msgs::ChassisMode::AUTO; + chassis_control_velocity_->vector << nan_, nan_, nan_; + + current_target_angle_ = max_angle_; + lf_current_target_angle_ = max_angle_; + lb_current_target_angle_ = max_angle_; + rf_current_target_angle_ = max_angle_; + rb_current_target_angle_ = max_angle_; + + const bool left_front_joint_offset = has_parameter("left_front_joint_offset"); + const bool left_back_joint_offset = has_parameter("left_back_joint_offset"); + const bool right_front_joint_offset = has_parameter("right_front_joint_offset"); + const bool right_back_joint_offset = has_parameter("right_back_joint_offset"); + + const bool has_any_joint_offset = left_front_joint_offset || left_back_joint_offset + || right_front_joint_offset || right_back_joint_offset; + const bool has_all_joint_offsets = left_front_joint_offset && left_back_joint_offset + && right_front_joint_offset && right_back_joint_offset; + if (has_any_joint_offset && !has_all_joint_offsets) + throw std::runtime_error( + "deformable chassis joint offsets must be configured for all four joints or " + "removed entirely"); + + joint_feedback_source_ = has_all_joint_offsets ? JointFeedbackSource::kLegacyEncoderAngle + : JointFeedbackSource::kMotorAngle; } - void update_acceleration_estimate( - const Eigen::Vector2d& translational_velocity, double dt, double limit) { - if (!translational_velocity.array().isFinite().all()) { - control_acceleration_estimate_.setZero(); - last_translational_velocity_.setZero(); - last_valid_ = false; - return; + void before_updating() override { + if (!gimbal_yaw_angle_.ready()) { + gimbal_yaw_angle_.make_and_bind_directly(0.0); + RCLCPP_WARN(get_logger(), "Failed to fetch \"/gimbal/yaw/angle\". Set to 0.0."); } - if (!last_valid_) { - last_translational_velocity_ = translational_velocity; - control_acceleration_estimate_.setZero(); - last_valid_ = true; - return; + if (!gimbal_yaw_angle_error_.ready()) { + gimbal_yaw_angle_error_.make_and_bind_directly(0.0); + RCLCPP_WARN( + get_logger(), "Failed to fetch \"/gimbal/yaw/control_angle_error\". Set to 0.0."); } - const Eigen::Vector2d cap = Eigen::Vector2d::Constant(limit); - control_acceleration_estimate_ = - ((translational_velocity - last_translational_velocity_) / dt) - .cwiseMax(-cap) - .cwiseMin(cap); - last_translational_velocity_ = translational_velocity; + if (!left_front_joint_torque_.ready()) + left_front_joint_torque_.make_and_bind_directly(0.0); + if (!left_back_joint_torque_.ready()) + left_back_joint_torque_.make_and_bind_directly(0.0); + if (!right_back_joint_torque_.ready()) + right_back_joint_torque_.make_and_bind_directly(0.0); + if (!right_front_joint_torque_.ready()) + right_front_joint_torque_.make_and_bind_directly(0.0); + if (!chassis_imu_pitch_.ready()) + chassis_imu_pitch_.make_and_bind_directly(0.0); + if (!chassis_imu_roll_.ready()) + chassis_imu_roll_.make_and_bind_directly(0.0); + if (!chassis_imu_pitch_rate_.ready()) + chassis_imu_pitch_rate_.make_and_bind_directly(0.0); + if (!chassis_imu_roll_rate_.ready()) + chassis_imu_roll_rate_.make_and_bind_directly(0.0); + validate_joint_feedback_inputs(); } - void reset_acceleration_estimate() { - control_acceleration_estimate_.setZero(); - last_translational_velocity_.setZero(); - last_valid_ = false; - } + void update() override { + using rmcs_msgs::Switch; + + const auto switch_right = *switch_right_; + const auto switch_left = *switch_left_; + const auto keyboard = *keyboard_; - Eigen::Vector2d control_acceleration_estimate() const { return control_acceleration_estimate_; } - bool spinning_forward() const { return spinning_forward_; } + do { + if ((switch_left == Switch::UNKNOWN || switch_right == Switch::UNKNOWN) + || (switch_left == Switch::DOWN && switch_right == Switch::DOWN)) { + reset_all_controls(); + break; + } + + update_mode_from_inputs_(switch_left, switch_right, keyboard); + update_velocity_control(); + update_lift_target_toggle(keyboard); + run_joint_intent_pipeline_(); + } while (false); + + last_switch_right_ = switch_right; + last_switch_left_ = switch_left; + last_keyboard_ = keyboard; + } private: - double spin_ratio_ = 1.0; - bool spinning_forward_ = true; - pid::PidCalculator following_velocity_controller_{10.0, 0.0, 0.0}; - Eigen::Vector2d control_acceleration_estimate_ = Eigen::Vector2d::Zero(); - Eigen::Vector2d last_translational_velocity_ = Eigen::Vector2d::Zero(); - bool last_valid_ = false; - - double - calc_angle_err_(double& cca, double yaw_error, double yaw_angle, double alignment) const { - cca = yaw_error; - if (cca < 0) - cca += 2 * std::numbers::pi; - double e = cca + yaw_angle; - if (e >= 2 * std::numbers::pi) - e -= 2 * std::numbers::pi; - while (e > alignment / 2) { - cca -= alignment; - if (cca < 0) - cca += 2 * std::numbers::pi; - e -= alignment; - } - return e; + static constexpr double inf_ = std::numeric_limits::infinity(); + static constexpr double nan_ = std::numeric_limits::quiet_NaN(); + static constexpr double translational_velocity_max_ = 10.0; + static constexpr double angular_velocity_max_ = 30.0; + static constexpr double rad_to_deg_ = 180.0 / std::numbers::pi; + + static double wrap_deg(double deg) { + deg = std::fmod(deg, 360.0); + if (deg >= 180.0) + deg -= 360.0; + if (deg < -180.0) + deg += 360.0; + return deg; } -}; -// --- ActiveSuspension --- -struct ActiveSuspension { - static constexpr double kNaN = std::numeric_limits::quiet_NaN(); - static constexpr double kGravity = 9.81; - static constexpr double kMaxAttitudeRad = 30.0 * std::numbers::pi / 180.0; - static constexpr double kMinForceArmSin = 0.1; - static constexpr double kContactConfidenceEnterThreshold = 0.55; - static constexpr double kContactConfidenceExitThreshold = 0.35; - static constexpr double kContactConfidenceFilterAlpha = 0.25; - static constexpr double kMinimumArmingTime = 0.02; - static constexpr std::array kPitchSigns = {-1.0, 1.0, 1.0, -1.0}; - static constexpr std::array kRollSigns = {1.0, 1.0, -1.0, -1.0}; - - void load_params(rclcpp::Node& node, double min_angle, double max_angle) { - params_ = SuspensionParams{ - .mass = node.get_parameter_or("active_suspension_mass", 22.5), - .rod_length = node.get_parameter_or("active_suspension_rod_length", 0.150), - .Kz = node.get_parameter_or("active_suspension_Kz", 150.0), - - .pitch_kp = node.get_parameter_or("active_suspension_pitch_kp", 200.0), - .pitch_ki = node.get_parameter_or("active_suspension_pitch_ki", 0.0), - .pitch_kd = node.get_parameter_or("active_suspension_pitch_kd", 20.0), - - .roll_kp = node.get_parameter_or("active_suspension_roll_kp", 200.0), - .roll_ki = node.get_parameter_or("active_suspension_roll_ki", 0.0), - .roll_kd = node.get_parameter_or("active_suspension_roll_kd", 20.0), - - .D_leg = node.get_parameter_or("active_suspension_D_leg", 10.0), - .com_height = node.get_parameter_or("active_suspension_com_height", 0.15), - .wheel_base_half_x = node.get_parameter_or( - "active_suspension_wheel_base_half_x", 0.2341741 / std::numbers::sqrt2), - .wheel_base_half_y = node.get_parameter_or( - "active_suspension_wheel_base_half_y", 0.2341741 / std::numbers::sqrt2), - .gravity_comp_gain = node.get_parameter_or("active_suspension_gravity_comp_gain", 1.0), - .control_acceleration_limit = std::abs( - node.get_parameter_or("active_suspension_control_acceleration_limit", 6.0)), - .preload_angle = - std::abs(node.get_parameter_or("active_suspension_preload_angle_deg", 8.0)) - * std::numbers::pi / 180.0, - .entry_offset = - std::abs(node.get_parameter_or( - "active_suspension_entry_offset_deg", - node.get_parameter_or("active_suspension_enter_deploy_tolerance_deg", 1.5))) - * std::numbers::pi / 180.0, - .ride_height_offset = - std::abs(node.get_parameter_or("active_suspension_ride_height_offset_deg", 0.0)) - * std::numbers::pi / 180.0, - .hold_travel = - std::abs(node.get_parameter_or( - "active_suspension_hold_travel_deg", - node.get_parameter_or("active_suspension_exit_deploy_tolerance_deg", 3.0))) - * std::numbers::pi / 180.0, - .activation_velocity_threshold = - node.get_parameter_or("active_suspension_activation_velocity_threshold_deg", 15.0) - * std::numbers::pi / 180.0, - .target_physical_velocity_limit = - std::max( - node.get_parameter_or( - "active_suspension_target_velocity_limit_deg", - node.get_parameter_or("target_physical_velocity_limit", 180.0)), - 1e-6) - * std::numbers::pi / 180.0, - .target_physical_acceleration_limit = - std::max( - node.get_parameter_or( - "active_suspension_target_acceleration_limit_deg", - node.get_parameter_or("target_physical_acceleration_limit", 720.0)), - 1e-6) - * std::numbers::pi / 180.0, - .torque_limit = std::abs(node.get_parameter_or("active_suspension_torque_limit", 80.0)), - .pitch_angle_diff_limit = - std::abs(node.get_parameter_or( - "active_suspension_pitch_angle_diff_limit_deg", max_angle - min_angle)) - * std::numbers::pi / 180.0, - .roll_angle_diff_limit = - std::abs(node.get_parameter_or( - "active_suspension_roll_angle_diff_limit_deg", max_angle - min_angle)) - * std::numbers::pi / 180.0, - .pid_integral_limit = - std::abs(node.get_parameter_or( - "active_suspension_pid_integral_limit_deg", max_angle - min_angle)) - * std::numbers::pi / 180.0, - }; - - pitch_pid_.kp = params_.pitch_kp; - pitch_pid_.ki = params_.pitch_ki; - pitch_pid_.kd = params_.pitch_kd; - - roll_pid_.kp = params_.roll_kp; - roll_pid_.ki = params_.roll_ki; - roll_pid_.kd = params_.roll_kd; - - pitch_pid_.integral_limit = params_.pid_integral_limit; - pitch_pid_.output_limit = params_.pitch_angle_diff_limit; - roll_pid_.integral_limit = params_.pid_integral_limit; - roll_pid_.output_limit = params_.roll_angle_diff_limit; - - enabled_ = node.get_parameter_or("active_suspension_enable", false); - calib_wait_ = std::max(node.get_parameter_or("chassis_imu_calibration_wait_s", 2.0), 0.0); - calib_sample_ = - std::max(node.get_parameter_or("chassis_imu_calibration_sample_s", 3.0), 1e-6); - } - - bool enabled() const { return enabled_; } - void set_enabled(bool v) { enabled_ = v; } - - void update( - const JointFeedbackFrame& fb, double imu_pitch, double imu_roll, double imu_pitch_rate, - double imu_roll_rate, double dt, bool requested, double min_angle_deg, double max_angle_deg, - const Eigen::Vector2d& control_accel, - std::array& current_target_physical_angles, - std::array& out_suspension_mode, - std::array& out_suspension_torque) { - static constexpr auto deg_to_rad = [](double d) { return d * std::numbers::pi / 180.0; }; - - clear_outputs_(out_suspension_mode, out_suspension_torque); - prepare_commands_(); - - if (!requested) { - reset_state_(); - current_target_physical_angles = requested_target_angles_; - publish_outputs_(out_suspension_mode, out_suspension_torque); - return; - } + void validate_joint_feedback_inputs() const { + const bool ready = + joint_feedback_source_ == JointFeedbackSource::kMotorAngle + ? left_front_joint_angle_.ready() && left_back_joint_angle_.ready() + && right_front_joint_angle_.ready() && right_back_joint_angle_.ready() + : left_front_joint_encoder_angle_.ready() && left_back_joint_encoder_angle_.ready() + && right_front_joint_encoder_angle_.ready() + && right_back_joint_encoder_angle_.ready(); - const double deploy = deg_to_rad(min_angle_deg); - const double entry = deploy + params_.entry_offset; - const double ride_height = - std::clamp(deploy + params_.ride_height_offset, deploy, deg_to_rad(max_angle_deg)); - const double support_zero_angle = deploy - params_.preload_angle; - const double release = ride_height + params_.hold_travel; - - AttitudeBias bias = - compute_attitude_bias_(imu_pitch, imu_roll, imu_pitch_rate, imu_roll_rate, dt); - if (!std::isfinite(bias.pitch_force) || !std::isfinite(bias.roll_force)) { - reset_state_(); - current_target_physical_angles.fill(deploy); - publish_outputs_(out_suspension_mode, out_suspension_torque); + if (ready) return; - } - update_leg_contact_estimates_(fb); - update_leg_states_(fb, entry, release, dt); - compute_leg_support_intents_(fb, bias, support_zero_angle, ride_height, control_accel); - current_target_physical_angles = target_angles_; - publish_outputs_(out_suspension_mode, out_suspension_torque); - } - - void update_imu_calibration( - bool symmetric_targets, double imu_pitch, double imu_roll, double dt) { - if (!symmetric_targets) { - calib_hold_ = 0.0; - calib_count_ = 0; - calib_pitch_sum_ = 0.0; - calib_roll_sum_ = 0.0; - calib_done_ = false; - return; - } - if (!std::isfinite(imu_pitch) || !std::isfinite(imu_roll)) - return; - calib_hold_ += dt; - if (calib_hold_ < calib_wait_) - return; - double end = calib_wait_ + calib_sample_; - if (calib_hold_ < end) { - calib_pitch_sum_ += imu_pitch; - calib_roll_sum_ += imu_roll; - ++calib_count_; - return; - } - if (calib_done_) - return; - calib_done_ = true; - if (calib_count_ == 0) - return; - imu_pitch_offset_ = calib_pitch_sum_ / static_cast(calib_count_); - imu_roll_offset_ = calib_roll_sum_ / static_cast(calib_count_); + throw std::runtime_error( + joint_feedback_source_ == JointFeedbackSource::kMotorAngle + ? "missing V2 joint feedback interfaces: expected /chassis/*_joint/angle" + : "missing legacy joint feedback interfaces: expected /chassis/*_joint/" + "encoder_angle"); } - void reset_calibration() { - calib_hold_ = 0.0; - calib_count_ = 0; - calib_pitch_sum_ = 0.0; - calib_roll_sum_ = 0.0; - calib_done_ = false; - } + double joint_angle_deg( + const InputInterface& joint_angle, + const InputInterface& joint_encoder_angle, double joint_offset, + double legacy_fixed_compensation) const { + if (joint_feedback_source_ == JointFeedbackSource::kMotorAngle) + return wrap_deg(*joint_angle * rad_to_deg_); - void reset_all() { - reset_state_(); - reset_calibration(); + return wrap_deg(joint_offset) - wrap_deg(*joint_encoder_angle) + legacy_fixed_compensation; } - double target_vel_limit() const { return params_.target_physical_velocity_limit; } - double target_accel_limit() const { return params_.target_physical_acceleration_limit; } - double control_accel_limit() const { return params_.control_acceleration_limit; } + void update_mode_from_inputs_( + rmcs_msgs::Switch switch_left, rmcs_msgs::Switch switch_right, + const rmcs_msgs::Keyboard& keyboard) { + auto mode = *mode_; + if (switch_left == rmcs_msgs::Switch::DOWN) + return; -private: - SuspensionParams params_{}; - bool enabled_ = false; - AttitudePidAxis pitch_pid_, roll_pid_; - double imu_pitch_offset_ = 0.0, imu_roll_offset_ = 0.0; - double calib_wait_ = 0.0, calib_sample_ = 0.0; - double calib_hold_ = 0.0; - size_t calib_count_ = 0; - double calib_pitch_sum_ = 0.0, calib_roll_sum_ = 0.0; - bool calib_done_ = false; - std::array suspension_active_{}; - std::array leg_states_{}; - std::array leg_commands_{}; - std::array requested_target_angles_{}; - std::array target_angles_{}; - - static double deg_to_rad_(double d) { return d * std::numbers::pi / 180.0; } - - void clear_outputs_( - std::array& modes, std::array& torques) { - modes.fill(false); - torques.fill(kNaN); - } - void publish_outputs_( - std::array& modes, std::array& torques) { - for (size_t i = 0; i < kJointCount; ++i) { - modes[i] = leg_commands_[i].suspension_mode; - torques[i] = leg_commands_[i].suspension_torque; + if (last_switch_right_ == rmcs_msgs::Switch::MIDDLE + && switch_right == rmcs_msgs::Switch::DOWN) { + if (mode == rmcs_msgs::ChassisMode::SPIN) { + mode = rmcs_msgs::ChassisMode::STEP_DOWN; + } else { + mode = rmcs_msgs::ChassisMode::SPIN; + spinning_forward_ = !spinning_forward_; + } + } else if (!last_keyboard_.c && keyboard.c) { + if (mode == rmcs_msgs::ChassisMode::SPIN) { + mode = rmcs_msgs::ChassisMode::AUTO; + } else { + mode = rmcs_msgs::ChassisMode::SPIN; + spinning_forward_ = !spinning_forward_; + } + } else if (!last_keyboard_.x && keyboard.x) { + mode = mode == rmcs_msgs::ChassisMode::LAUNCH_RAMP + ? rmcs_msgs::ChassisMode::AUTO + : rmcs_msgs::ChassisMode::LAUNCH_RAMP; + } else if (!last_keyboard_.z && keyboard.z) { + mode = mode == rmcs_msgs::ChassisMode::STEP_DOWN ? rmcs_msgs::ChassisMode::AUTO + : rmcs_msgs::ChassisMode::STEP_DOWN; } - } - void reset_state_() { - suspension_active_.fill(false); - for (size_t i = 0; i < kJointCount; ++i) { - leg_states_[i] = LegControlState{}; - leg_commands_[i] = LegCommand{}; - } + *mode_ = mode; } - void prepare_commands_() { - for (size_t i = 0; i < kJointCount; ++i) { - leg_commands_[i] = LegCommand{ - .requested_target_angle = requested_target_angles_[i], - .final_target_angle = target_angles_[i], - }; - leg_states_[i].output_active = false; - leg_states_[i].support_force = 0.0; - } + // JointFeedbackAdapter: normalize motor / physical / legacy encoder feedback into one frame. + JointFeedbackFrame read_joint_feedback_frame_() const { + JointFeedbackFrame joint_feedback; + update_current_joint_feedback( + joint_feedback.motor_angles, joint_feedback.physical_angles, + joint_feedback.physical_velocities, joint_feedback.joint_torques); + return joint_feedback; } - static LegFeedback leg_feedback_at_(const JointFeedbackFrame& fb, size_t i) { - return { - .motor_angle = fb.motor_angles[i], - .physical_angle = fb.physical_angles[i], - .physical_velocity = fb.physical_velocities[i], - .joint_torque = fb.joint_torques[i], - .eso_z2 = fb.eso_z2[i], - .eso_z3 = fb.eso_z3[i]}; + bool prone_override_requested_() const { return keyboard_.ready() && keyboard_->ctrl; } + + bool suspension_requested_by_switch_() const { + return switch_left_.ready() && switch_right_.ready() + && *switch_left_ == rmcs_msgs::Switch::DOWN && *switch_right_ == rmcs_msgs::Switch::UP; } - double estimate_contact_(const LegFeedback& lf) const { - double c = 1.0; - if (std::isfinite(lf.eso_z3)) - c -= std::clamp(std::abs(lf.eso_z3) / 80.0, 0.0, 0.5); - if (std::isfinite(lf.joint_torque)) - c += std::clamp(std::abs(lf.joint_torque) / 20.0, 0.0, 0.3); - if (std::isfinite(lf.physical_velocity)) - c -= std::clamp(std::abs(lf.physical_velocity) / 10.0, 0.0, 0.2); - return std::clamp(c, 0.0, 1.0); + bool suspension_requested_by_input_() const { + return active_suspension_enable_ + && (prone_override_requested_() || suspension_requested_by_switch_()); } - bool contact_ready_(const LegControlState& s) const { - return s.contact_latched - || s.filtered_contact_confidence >= kContactConfidenceEnterThreshold; + bool symmetric_joint_target_requested_() const { + constexpr double epsilon = 1e-6; + return std::abs(lf_current_target_angle_ - lb_current_target_angle_) <= epsilon + && std::abs(lf_current_target_angle_ - rb_current_target_angle_) <= epsilon + && std::abs(lf_current_target_angle_ - rf_current_target_angle_) <= epsilon; } - void update_leg_contact_estimates_(const JointFeedbackFrame& fb) { - for (size_t i = 0; i < kJointCount; ++i) { - auto& s = leg_states_[i]; - s.contact_confidence = estimate_contact_(leg_feedback_at_(fb, i)); - s.filtered_contact_confidence = std::clamp( - (1.0 - kContactConfidenceFilterAlpha) * s.filtered_contact_confidence - + kContactConfidenceFilterAlpha * s.contact_confidence, - 0.0, 1.0); - if (s.filtered_contact_confidence >= kContactConfidenceEnterThreshold) - s.contact_latched = true; - else if (s.filtered_contact_confidence <= kContactConfidenceExitThreshold) - s.contact_latched = false; + bool refresh_requested_joint_targets_from_deploy_state_() { + requested_target_physical_angles_rad_[kLeftFront] = deg_to_rad(lf_current_target_angle_); + requested_target_physical_angles_rad_[kLeftBack] = deg_to_rad(lb_current_target_angle_); + requested_target_physical_angles_rad_[kRightBack] = deg_to_rad(rb_current_target_angle_); + requested_target_physical_angles_rad_[kRightFront] = deg_to_rad(rf_current_target_angle_); + + const bool prone_override = prone_override_requested_(); + if (suspension_requested_by_input_()) { + requested_target_physical_angles_rad_.fill(deg_to_rad(min_angle_)); } + + current_target_physical_angles_rad_ = requested_target_physical_angles_rad_; + return prone_override; } - void update_leg_states_(const JointFeedbackFrame& fb, double entry, double release, double dt) { - for (size_t i = 0; i < kJointCount; ++i) { - auto lf = leg_feedback_at_(fb, i); - bool rd = - std::isfinite(requested_target_angles_[i]) && requested_target_angles_[i] <= entry; - update_suspension_state_(i, lf, rd, entry, release, dt); - } + void reset_attitude_correction_state_() { + pitch_pid_.reset(); + roll_pid_.reset(); + joint_suspension_active_.fill(false); } - void compute_leg_support_intents_( - const JointFeedbackFrame& fb, const AttitudeBias& bias, double support_zero_angle, - double ride_height, const Eigen::Vector2d& control_accel) { - for (size_t i = 0; i < kJointCount; ++i) { - auto lf = leg_feedback_at_(fb, i); - auto& s = leg_states_[i]; - if (s.phase != SuspensionPhase::kActive) - continue; - s.support_force = - compute_leg_support_force_(i, lf, bias, support_zero_angle, control_accel); - leg_commands_[i].final_target_angle = ride_height; - leg_commands_[i].suspension_mode = true; - leg_commands_[i].suspension_torque = - leg_force_to_torque_(s.support_force, lf.physical_angle); - } + void reset_chassis_imu_calibration_window_() { + chassis_imu_calibration_hold_elapsed_ = 0.0; + chassis_imu_calibration_sample_count_ = 0; + chassis_imu_pitch_sum_ = 0.0; + chassis_imu_roll_sum_ = 0.0; + chassis_imu_calibration_completed_for_window_ = false; } - AttitudeBias compute_attitude_bias_( - double pitch, double roll, double pitch_rate, double roll_rate, double dt) { - double corrected_pitch = - std::clamp(pitch - imu_pitch_offset_, -kMaxAttitudeRad, kMaxAttitudeRad); - double corrected_roll = - std::clamp(roll - imu_roll_offset_, -kMaxAttitudeRad, kMaxAttitudeRad); - double pitch_force = pitch_pid_.update(-corrected_pitch, pitch_rate, dt); - double roll_force = roll_pid_.update(corrected_roll, -roll_rate, dt); - if (!std::isfinite(pitch_force) || !std::isfinite(roll_force)) - return {kNaN, kNaN}; - return {pitch_force, roll_force}; - } - - double compute_leg_support_force_( - size_t i, const LegFeedback& lf, const AttitudeBias& bias, double support_zero_angle, - const Eigen::Vector2d& control_accel) const { - double f = params_.gravity_comp_gain * params_.mass * kGravity / 4.0 - + params_.Kz * (lf.physical_angle - support_zero_angle) - + params_.D_leg * lf.physical_velocity; - f += kPitchSigns[i] * bias.pitch_force + kRollSigns[i] * bias.roll_force; - if (params_.com_height > 0.0 && params_.wheel_base_half_x > 1e-6 - && params_.wheel_base_half_y > 1e-6) { - f += kPitchSigns[i] * params_.mass * control_accel.x() * params_.com_height - / (4.0 * params_.wheel_base_half_x); - f += kRollSigns[i] * params_.mass * control_accel.y() * params_.com_height - / (4.0 * params_.wheel_base_half_y); + void update_chassis_imu_calibration_() { + if (!symmetric_joint_target_requested_()) { + reset_chassis_imu_calibration_window_(); + return; } - return std::max(f, 0.0); - } - - double leg_force_to_torque_(double force, double angle) const { - return std::clamp( - force * params_.rod_length * std::max(std::sin(angle), kMinForceArmSin), - -params_.torque_limit, params_.torque_limit); - } - - void update_suspension_state_( - size_t i, const LegFeedback& lf, bool rd, double entry, double release, double dt) { - auto& s = leg_states_[i]; - s.phase_elapsed += (s.requested_deploy != rd) ? 0.0 : dt; - s.requested_deploy = rd; - if (!rd || !std::isfinite(lf.physical_angle) || !std::isfinite(lf.physical_velocity)) { - s.phase = SuspensionPhase::kInactive; - suspension_active_[i] = false; - s.output_active = false; - s.phase_elapsed = 0.0; - s.contact_latched = false; + + const double raw_pitch = *chassis_imu_pitch_; + const double raw_roll = *chassis_imu_roll_; + if (!std::isfinite(raw_pitch) || !std::isfinite(raw_roll)) + return; + + chassis_imu_calibration_hold_elapsed_ += update_dt(); + if (chassis_imu_calibration_hold_elapsed_ < chassis_imu_calibration_wait_time_) + return; + + const double calibration_end_time = + chassis_imu_calibration_wait_time_ + chassis_imu_calibration_sample_time_; + if (chassis_imu_calibration_hold_elapsed_ < calibration_end_time) { + chassis_imu_pitch_sum_ += raw_pitch; + chassis_imu_roll_sum_ += raw_roll; + ++chassis_imu_calibration_sample_count_; return; } - bool eok = lf.physical_angle <= entry; - bool vok = std::abs(lf.physical_velocity) <= params_.activation_velocity_threshold; - bool cok = contact_ready_(s); - switch (s.phase) { - case SuspensionPhase::kInactive: - suspension_active_[i] = false; - s.output_active = false; - if (eok) { - s.phase = SuspensionPhase::kArming; - s.phase_elapsed = 0.0; - } - break; - case SuspensionPhase::kArming: - suspension_active_[i] = false; - s.output_active = false; - if (!eok) { - s.phase = SuspensionPhase::kInactive; - s.phase_elapsed = 0.0; - break; + + if (chassis_imu_calibration_completed_for_window_) + return; + + chassis_imu_calibration_completed_for_window_ = true; + if (chassis_imu_calibration_sample_count_ == 0) { + RCLCPP_WARN( + get_logger(), + "[chassis imu calibration] skipped because no valid samples were collected"); + return; + } + + chassis_imu_pitch_offset_ = + chassis_imu_pitch_sum_ / static_cast(chassis_imu_calibration_sample_count_); + chassis_imu_roll_offset_ = + chassis_imu_roll_sum_ / static_cast(chassis_imu_calibration_sample_count_); + RCLCPP_INFO( + get_logger(), + "[chassis imu calibration] pitch_offset=% .3f deg roll_offset=% .3f deg " + "(samples=%zu)", + chassis_imu_pitch_offset_ * rad_to_deg_, chassis_imu_roll_offset_ * rad_to_deg_, + chassis_imu_calibration_sample_count_); + } + + void update_current_joint_feedback( + std::array& current_motor_angles, + std::array& current_physical_angles, + std::array& current_physical_velocities, + std::array& current_joint_torques) const { + const std::array*, kJointCount> motor_angle_inputs{ + &left_front_joint_angle_, &left_back_joint_angle_, &right_back_joint_angle_, + &right_front_joint_angle_}; + const std::array*, kJointCount> physical_angle_inputs{ + &left_front_joint_physical_angle_, &left_back_joint_physical_angle_, + &right_back_joint_physical_angle_, &right_front_joint_physical_angle_}; + const std::array*, kJointCount> physical_velocity_inputs{ + &left_front_joint_physical_velocity_, &left_back_joint_physical_velocity_, + &right_back_joint_physical_velocity_, &right_front_joint_physical_velocity_}; + const std::array*, kJointCount> torque_inputs{ + &left_front_joint_torque_, &left_back_joint_torque_, &right_back_joint_torque_, + &right_front_joint_torque_}; + current_motor_angles.fill(nan_); + current_physical_angles.fill(nan_); + current_physical_velocities.fill(nan_); + current_joint_torques.fill(nan_); + + for (size_t i = 0; i < kJointCount; ++i) { + if (motor_angle_inputs[i]->ready() && std::isfinite(*(*motor_angle_inputs[i]))) { + current_motor_angles[i] = *(*motor_angle_inputs[i]); + current_physical_angles[i] = motor_to_physical_angle(current_motor_angles[i]); } - if (vok && std::isfinite(lf.motor_angle) - && (cok || s.phase_elapsed >= kMinimumArmingTime)) { - suspension_active_[i] = true; - s.phase = SuspensionPhase::kActive; - s.output_active = true; - s.phase_elapsed = 0.0; + + if (physical_angle_inputs[i]->ready() && std::isfinite(*(*physical_angle_inputs[i]))) { + current_physical_angles[i] = *(*physical_angle_inputs[i]); } - break; - case SuspensionPhase::kActive: - if (lf.physical_angle > release || (!cok && s.phase_elapsed >= kMinimumArmingTime)) { - suspension_active_[i] = false; - s.phase = SuspensionPhase::kReleasing; - s.output_active = false; - s.phase_elapsed = 0.0; - break; + if (physical_velocity_inputs[i]->ready() + && std::isfinite(*(*physical_velocity_inputs[i]))) { + current_physical_velocities[i] = *(*physical_velocity_inputs[i]); } - suspension_active_[i] = true; - s.output_active = true; - break; - case SuspensionPhase::kReleasing: - suspension_active_[i] = false; - s.output_active = false; - if (!eok || s.phase_elapsed >= kMinimumArmingTime) { - s.phase = SuspensionPhase::kInactive; - s.phase_elapsed = 0.0; + if (torque_inputs[i]->ready() && std::isfinite(*(*torque_inputs[i]))) { + current_joint_torques[i] = *(*torque_inputs[i]); } - break; } } -}; -// ============================================================ -// DeformableChassis — thin orchestrator -// ============================================================ -class DeformableChassis - : public rmcs_executor::Component - , public rclcpp::Node { -public: - DeformableChassis() - : Node( - get_component_name(), - rclcpp::NodeOptions{}.automatically_declare_parameters_from_overrides(true)) { - - const double spin_ratio = std::clamp(get_parameter_or("spin_ratio", 0.6), 0.0, 1.0); - const double min_angle = get_parameter_or("min_angle", 15.0); - const double max_angle = get_parameter_or("max_angle", 55.0); - const double vel_limit = std::max( - std::abs(get_parameter_or("target_physical_velocity_limit", 180.0)) * std::numbers::pi - / 180.0, - 1e-6); - const double accel_limit = std::max( - std::abs(get_parameter_or("target_physical_acceleration_limit", 720.0)) - * std::numbers::pi / 180.0, - 1e-6); - - velocity_control_.configure(spin_ratio); - suspension_.load_params(*this, min_angle, max_angle); - trajectory_.init(min_angle, max_angle, vel_limit, accel_limit); - - for (size_t i = 0; i < kJointCount; ++i) - joint_offsets_[i] = - get_parameter_or(std::string(kJointNames[i]) + "_joint_offset", 0.0); - - register_input("/remote/joystick/right", joystick_right_); - register_input("/remote/joystick/left", joystick_left_); - register_input("/remote/switch/right", switch_right_); - register_input("/remote/switch/left", switch_left_); - register_input("/remote/mouse/velocity", mouse_velocity_); - register_input("/remote/mouse", mouse_); - register_input("/remote/keyboard", keyboard_); - register_input("/remote/rotary_knob", rotary_knob_); - register_input("/predefined/update_rate", update_rate_); - register_input("/gimbal/yaw/angle", gimbal_yaw_angle_, false); - register_input("/gimbal/yaw/control_angle_error", gimbal_yaw_angle_error_, false); - - auto reg_joint_input = [this](size_t i) { - const auto& name = kJointNames[i]; - auto& j = joints_[i]; - register_input(joint_path_(name, "angle"), j.angle, false); - register_input(joint_path_(name, "physical_angle"), j.physical_angle, false); - register_input(joint_path_(name, "physical_velocity"), j.physical_velocity, false); - register_input(joint_path_(name, "torque"), j.torque, false); - register_input(joint_path_(name, "encoder_angle"), j.encoder_angle, false); - register_input(joint_path_(name, "eso_z2"), j.eso_z2, false); - register_input(joint_path_(name, "eso_z3"), j.eso_z3, false); - }; - for (size_t i = 0; i < kJointCount; ++i) - reg_joint_input(i); + bool initialize_joint_target_states_from_feedback( + const std::array& current_motor_angles, + const std::array& current_physical_angles) { + for (size_t i = 0; i < kJointCount; ++i) { + if (!std::isfinite(current_motor_angles[i]) + || !std::isfinite(current_physical_angles[i])) + return false; + } - register_input("/chassis/imu/pitch", chassis_imu_pitch_, false); - register_input("/chassis/imu/roll", chassis_imu_roll_, false); - register_input("/chassis/imu/pitch_rate", chassis_imu_pitch_rate_, false); - register_input("/chassis/imu/roll_rate", chassis_imu_roll_rate_, false); + joint_target_angle_state_rad_ = current_motor_angles; + joint_target_physical_angle_state_rad_ = current_physical_angles; + joint_target_physical_velocity_state_rad_ = {0.0, 0.0, 0.0, 0.0}; + joint_target_physical_acceleration_state_rad_ = {0.0, 0.0, 0.0, 0.0}; + requested_target_physical_angles_rad_ = current_physical_angles; + current_target_physical_angles_rad_ = current_physical_angles; + joint_target_active_ = true; + return true; + } - register_output("/gimbal/scope/control_torque", scope_motor_control_torque_, kNaN); - register_output("/chassis/angle", chassis_angle_, kNaN); - register_output("/chassis/control_angle", chassis_control_angle_, kNaN); - register_output("/chassis/control_mode", mode_); - register_output("/chassis/control_velocity", chassis_control_velocity_); + void update_active_suspension_(const JointFeedbackFrame&) { + if (!suspension_requested_by_input_()) { + reset_attitude_correction_state_(); + return; + } - auto reg_joint_output = [this](size_t i) { - const auto& name = kJointNames[i]; - auto& j = joints_[i]; - register_output(joint_path_(name, "control_angle_error"), angle_errors_[i], kNaN); - register_output(joint_path_(name, "target_angle"), j.target_angle, kNaN); - register_output( - joint_path_(name, "target_physical_angle"), j.target_physical_angle, kNaN); - register_output( - joint_path_(name, "target_physical_velocity"), j.target_physical_velocity, kNaN); - register_output( - joint_path_(name, "target_physical_acceleration"), j.target_physical_acceleration, - kNaN); - register_output(joint_path_(name, "suspension_torque"), j.suspension_torque, kNaN); - }; - for (size_t i = 0; i < kJointCount; ++i) { - reg_joint_output(i); - register_output( - joint_path_(kJointNames[i], "suspension_mode"), suspension_modes_[i], false); + constexpr double max_attitude = 30.0 * std::numbers::pi / 180.0; + const double base_target_angle = deg_to_rad(min_angle_); + const double max_target_angle = deg_to_rad(max_angle_); + const double corrected_pitch = + std::clamp(*chassis_imu_pitch_ - chassis_imu_pitch_offset_, -max_attitude, max_attitude); + const double corrected_roll = + std::clamp(*chassis_imu_roll_ - chassis_imu_roll_offset_, -max_attitude, max_attitude); + const double corrected_pitch_rate = *chassis_imu_pitch_rate_; + const double corrected_roll_rate = *chassis_imu_roll_rate_; + + const double dt = update_dt(); + const double pitch_angle_diff = + pitch_pid_.update(-corrected_pitch, corrected_pitch_rate, dt); + const double roll_angle_diff = + roll_pid_.update(corrected_roll, -corrected_roll_rate, dt); + if (!std::isfinite(pitch_angle_diff) || !std::isfinite(roll_angle_diff)) { + reset_attitude_correction_state_(); + current_target_physical_angles_rad_.fill(base_target_angle); + return; } - register_output("/chassis/processed_encoder/angle", processed_encoder_angle_, kNaN); + // Positive pitch_angle_diff raises the rear pair. Positive roll_angle_diff raises the left + // pair. Every leg starts from min_angle and only receives additive corrections so at least + // one leg always stays at min_angle. + const double front_pitch_add = std::max(-pitch_angle_diff, 0.0); + const double back_pitch_add = std::max(pitch_angle_diff, 0.0); + const double left_roll_add = std::max(roll_angle_diff, 0.0); + const double right_roll_add = std::max(-roll_angle_diff, 0.0); + + current_target_physical_angles_rad_[kLeftFront] = + std::clamp(base_target_angle + front_pitch_add + left_roll_add, base_target_angle, + max_target_angle); + current_target_physical_angles_rad_[kLeftBack] = + std::clamp(base_target_angle + back_pitch_add + left_roll_add, base_target_angle, + max_target_angle); + current_target_physical_angles_rad_[kRightBack] = + std::clamp(base_target_angle + back_pitch_add + right_roll_add, base_target_angle, + max_target_angle); + current_target_physical_angles_rad_[kRightFront] = + std::clamp(base_target_angle + front_pitch_add + right_roll_add, base_target_angle, + max_target_angle); + + joint_suspension_active_.fill(true); + } + + void reset_all_controls() { *mode_ = rmcs_msgs::ChassisMode::AUTO; - chassis_control_velocity_->vector << kNaN, kNaN, kNaN; + reset_attitude_correction_state_(); + reset_chassis_imu_calibration_window_(); + + chassis_control_velocity_->vector << nan_, nan_, nan_; + *chassis_angle_ = nan_; + *chassis_control_angle_ = nan_; + + current_target_angle_ = max_angle_; + lf_current_target_angle_ = current_target_angle_; + lb_current_target_angle_ = current_target_angle_; + rb_current_target_angle_ = current_target_angle_; + rf_current_target_angle_ = current_target_angle_; + joint_target_active_ = false; + + *scope_motor_control_torque = nan_; + + *lf_angle_error_ = nan_; + *lb_angle_error_ = nan_; + *rf_angle_error_ = nan_; + *rb_angle_error_ = nan_; + + *left_front_joint_target_angle_ = nan_; + *left_back_joint_target_angle_ = nan_; + *right_back_joint_target_angle_ = nan_; + *right_front_joint_target_angle_ = nan_; + + *left_front_joint_target_physical_angle_ = nan_; + *left_back_joint_target_physical_angle_ = nan_; + *right_back_joint_target_physical_angle_ = nan_; + *right_front_joint_target_physical_angle_ = nan_; + *left_front_joint_target_physical_velocity_ = nan_; + *left_back_joint_target_physical_velocity_ = nan_; + *right_back_joint_target_physical_velocity_ = nan_; + *right_front_joint_target_physical_velocity_ = nan_; + *left_front_joint_target_physical_acceleration_ = nan_; + *left_back_joint_target_physical_acceleration_ = nan_; + *right_back_joint_target_physical_acceleration_ = nan_; + *right_front_joint_target_physical_acceleration_ = nan_; + + *processed_encoder_angle_ = nan_; + } - int offset_count = 0; - for (size_t i = 0; i < kJointCount; ++i) - if (has_parameter(std::string(kJointNames[i]) + "_joint_offset")) - ++offset_count; - if (offset_count > 0 && offset_count != static_cast(kJointCount)) - throw std::runtime_error( - "joint offsets must be configured for all four joints or removed entirely"); - joint_feedback_source_ = (offset_count == static_cast(kJointCount)) - ? JointFeedbackSource::kLegacyEncoderAngle - : JointFeedbackSource::kMotorAngle; + void update_velocity_control() { + const Eigen::Vector2d translational_velocity = update_translational_velocity_control(); + const double angular_velocity = update_angular_velocity_control(); + chassis_control_velocity_->vector << translational_velocity, angular_velocity; } - void before_updating() override { - auto ensure = [this](auto& field, double value, const char* name) { - if (!field.ready()) { - field.make_and_bind_directly(value); - RCLCPP_WARN(get_logger(), "Failed to fetch \"%s\". Set to %.1f.", name, value); - } - }; - ensure(gimbal_yaw_angle_, 0.0, "/gimbal/yaw/angle"); - ensure(gimbal_yaw_angle_error_, 0.0, "/gimbal/yaw/control_angle_error"); - for (auto& j : joints_) - ensure(j.torque, 0.0, "joint torque"); - ensure(chassis_imu_pitch_, 0.0, "chassis imu pitch"); - ensure(chassis_imu_roll_, 0.0, "chassis imu roll"); - ensure(chassis_imu_pitch_rate_, 0.0, "chassis imu pitch_rate"); - ensure(chassis_imu_roll_rate_, 0.0, "chassis imu roll_rate"); - validate_joint_feedback_inputs_(); + double update_dt() const { + if (update_rate_.ready() && std::isfinite(*update_rate_) && *update_rate_ > 1e-6) + return 1.0 / *update_rate_; + return default_dt_; } - void update() override { - using rmcs_msgs::Switch; - const auto switch_right = *switch_right_; - const auto switch_left = *switch_left_; + Eigen::Vector2d update_translational_velocity_control() { const auto keyboard = *keyboard_; - do { - if ((switch_left == Switch::UNKNOWN || switch_right == Switch::UNKNOWN) - || (switch_left == Switch::DOWN && switch_right == Switch::DOWN)) { - reset_all_controls_(); - break; - } - update_mode_from_inputs_(switch_left, switch_right, keyboard); - update_velocity_control_(keyboard); - update_lift_target_toggle_(keyboard); - run_joint_intent_pipeline_(); - } while (false); - last_switch_right_ = switch_right; - last_switch_left_ = switch_left; - last_keyboard_ = keyboard; - } + const Eigen::Vector2d keyboard_move{keyboard.w - keyboard.s, keyboard.a - keyboard.d}; -private: - static constexpr double kNaN = std::numeric_limits::quiet_NaN(); - static constexpr double kRadToDeg = 180.0 / std::numbers::pi; + Eigen::Vector2d translational_velocity = + Eigen::Rotation2Dd{*gimbal_yaw_angle_} * ((*joystick_right_) + keyboard_move); - static std::string joint_path_(const char* name, const char* suffix) { - char b[128]; - std::snprintf(b, sizeof(b), "/chassis/%s_joint/%s", name, suffix); - return {b}; + if (translational_velocity.norm() > 1.0) + translational_velocity.normalize(); + + translational_velocity *= translational_velocity_max_; + return translational_velocity; } - void validate_joint_feedback_inputs_() const { - bool ok = true; - for (const auto& j : joints_) { - if (joint_feedback_source_ == JointFeedbackSource::kMotorAngle) { - if (!j.angle.ready()) - ok = false; - } else { - if (!j.encoder_angle.ready()) - ok = false; + double update_angular_velocity_control() { + double angular_velocity = 0.0; + double chassis_control_angle = nan_; + + switch (*mode_) { + case rmcs_msgs::ChassisMode::AUTO: break; + + case rmcs_msgs::ChassisMode::SPIN: { + angular_velocity = + spin_ratio_ * (spinning_forward_ ? angular_velocity_max_ : -angular_velocity_max_); + angular_velocity = + std::clamp(angular_velocity, -angular_velocity_max_, angular_velocity_max_); + } break; + + case rmcs_msgs::ChassisMode::STEP_DOWN: { + double err = calculate_unsigned_chassis_angle_error(chassis_control_angle); + + // In step-down mode, front/back can both be used for alignment. + constexpr double alignment = std::numbers::pi; + while (err > alignment / 2) { + chassis_control_angle -= alignment; + if (chassis_control_angle < 0) + chassis_control_angle += 2 * std::numbers::pi; + err -= alignment; } + + angular_velocity = following_velocity_controller_.update(err); + } break; + + case rmcs_msgs::ChassisMode::LAUNCH_RAMP: { + double err = calculate_unsigned_chassis_angle_error(chassis_control_angle); + + constexpr double alignment = 2 * std::numbers::pi; + if (err > alignment / 2) + err -= alignment; + + angular_velocity = following_velocity_controller_.update(err); + } break; + + default: break; } - if (ok) - return; - throw std::runtime_error( - joint_feedback_source_ == JointFeedbackSource::kMotorAngle - ? "missing V2 joint feedback inputs: /chassis/*_joint/angle" - : "missing legacy joint feedback inputs: /chassis/*_joint/encoder_angle"); + + *chassis_angle_ = 2 * std::numbers::pi - *gimbal_yaw_angle_; + *chassis_control_angle_ = chassis_control_angle; + + return angular_velocity; } - // --- helpers --- - bool suspension_requested_() const { - return suspension_.enabled() - && (keyboard_->ctrl - || (switch_left_.ready() && switch_right_.ready() - && *switch_left_ == rmcs_msgs::Switch::DOWN - && *switch_right_ == rmcs_msgs::Switch::UP)); + double calculate_unsigned_chassis_angle_error(double& chassis_control_angle) { + chassis_control_angle = *gimbal_yaw_angle_error_; + if (chassis_control_angle < 0) + chassis_control_angle += 2 * std::numbers::pi; + + double err = chassis_control_angle + *gimbal_yaw_angle_; + if (err >= 2 * std::numbers::pi) + err -= 2 * std::numbers::pi; + + return err; } - // --- mode --- - void update_mode_from_inputs_( - rmcs_msgs::Switch sl, rmcs_msgs::Switch sr, const rmcs_msgs::Keyboard& kb) { - auto m = *mode_; - if (sl == rmcs_msgs::Switch::DOWN) - return; - if (last_switch_right_ == rmcs_msgs::Switch::MIDDLE && sr == rmcs_msgs::Switch::DOWN) { - m = (m == rmcs_msgs::ChassisMode::SPIN) ? rmcs_msgs::ChassisMode::STEP_DOWN - : rmcs_msgs::ChassisMode::SPIN; - } else if (!last_keyboard_.c && kb.c) { - m = (m == rmcs_msgs::ChassisMode::SPIN) ? rmcs_msgs::ChassisMode::AUTO - : rmcs_msgs::ChassisMode::SPIN; - } else if (!last_keyboard_.x && kb.x) { - m = (m == rmcs_msgs::ChassisMode::LAUNCH_RAMP) ? rmcs_msgs::ChassisMode::AUTO - : rmcs_msgs::ChassisMode::LAUNCH_RAMP; - } else if (!last_keyboard_.z && kb.z) { - m = (m == rmcs_msgs::ChassisMode::STEP_DOWN) ? rmcs_msgs::ChassisMode::AUTO - : rmcs_msgs::ChassisMode::STEP_DOWN; + void update_lift_target_toggle(rmcs_msgs::Keyboard keyboard) { + constexpr double rotary_knob_edge_threshold = 0.7; + + const bool keyboard_toggle_condition = !last_keyboard_.q && keyboard.q; + const bool rotary_knob_toggle_condition = + last_rotary_knob_ < rotary_knob_edge_threshold + && *rotary_knob_ >= rotary_knob_edge_threshold; + const bool front_high_rear_low = !last_keyboard_.b && keyboard.b; + const bool front_low_rear_high = !last_keyboard_.g && keyboard.g; + + if (apply_symmetric_target) { + lf_current_target_angle_ = current_target_angle_; + lb_current_target_angle_ = current_target_angle_; + rb_current_target_angle_ = current_target_angle_; + rf_current_target_angle_ = current_target_angle_; } - *mode_ = m; - } - // --- velocity --- - void update_velocity_control_(const rmcs_msgs::Keyboard& kb) { - auto tv = velocity_control_.compute_translational(*joystick_right_, kb, *gimbal_yaw_angle_); - bool toggle = (last_keyboard_.c != kb.c && *mode_ != rmcs_msgs::ChassisMode::SPIN); - auto ar = velocity_control_.compute_angular( - *mode_, *gimbal_yaw_angle_, *gimbal_yaw_angle_error_, toggle); - double dt = update_dt_(); - velocity_control_.update_acceleration_estimate(tv, dt, suspension_.control_accel_limit()); - *chassis_angle_ = ar.chassis_angle; - *chassis_control_angle_ = ar.chassis_control_angle; - chassis_control_velocity_->vector << tv, ar.angular_velocity; - } + if (rotary_knob_toggle_condition || keyboard_toggle_condition) { + current_target_angle_ = + (std::abs(current_target_angle_ - max_angle_) < 1e-6) ? min_angle_ : max_angle_; + apply_symmetric_target = true; + } else if (front_high_rear_low) { + lf_current_target_angle_ = max_angle_; + rf_current_target_angle_ = max_angle_; + lb_current_target_angle_ = min_angle_; + rb_current_target_angle_ = min_angle_; + apply_symmetric_target = false; + } else if (front_low_rear_high) { + lf_current_target_angle_ = min_angle_; + rf_current_target_angle_ = min_angle_; + lb_current_target_angle_ = max_angle_; + rb_current_target_angle_ = max_angle_; + apply_symmetric_target = false; + } - double update_dt_() const { - if (update_rate_.ready() && std::isfinite(*update_rate_) && *update_rate_ > 1e-6) - return 1.0 / *update_rate_; - return 1e-3; + last_rotary_knob_ = *rotary_knob_; } - // --- lift toggle --- - void update_lift_target_toggle_(rmcs_msgs::Keyboard keyboard) { - constexpr double kRotaryKnobEdgeThreshold = 0.7; + // Chassis owns the high-level joint intent pipeline: read feedback, generate deploy targets, + // coordinate suspension overrides, then publish the resulting joint intent for the servo layer. + void run_joint_intent_pipeline_() { + const auto joint_feedback = read_joint_feedback_frame_(); + + if (!joint_target_active_ + && !initialize_joint_target_states_from_feedback( + joint_feedback.motor_angles, joint_feedback.physical_angles)) { + publish_nan_joint_targets(); + return; + } - const bool keyboard_toggle = !last_keyboard_.q && keyboard.q; - const bool rotary_knob_toggle = last_rotary_knob_ < kRotaryKnobEdgeThreshold - && *rotary_knob_ >= kRotaryKnobEdgeThreshold; + update_chassis_imu_calibration_(); + const bool prone_override = refresh_requested_joint_targets_from_deploy_state_(); + scope_motor_control(prone_override); + update_active_suspension_(joint_feedback); + update_joint_target_trajectory(); + publish_joint_target_angles(joint_feedback.physical_angles); + } - if (apply_symmetric_target_) - trajectory_.fill_symmetric_targets(); + static double deg_to_rad(double deg) { return deg * std::numbers::pi / 180.0; } - if (rotary_knob_toggle || keyboard_toggle) { - trajectory_.set_target_angle( - std::abs(trajectory_.target_angle() - trajectory_.max_angle()) < 1e-6 - ? trajectory_.min_angle() - : trajectory_.max_angle()); - apply_symmetric_target_ = true; - } + static double physical_to_motor_angle(double physical_angle_rad) { + return joint_zero_physical_angle_rad_ - physical_angle_rad; + } - last_rotary_knob_ = *rotary_knob_; + static double motor_to_physical_angle(double motor_angle_rad) { + return joint_zero_physical_angle_rad_ - motor_angle_rad; } - // --- reset --- - void reset_all_controls_() { - *mode_ = rmcs_msgs::ChassisMode::AUTO; - velocity_control_.reset_acceleration_estimate(); - suspension_.reset_all(); - chassis_control_velocity_->vector << kNaN, kNaN, kNaN; - *chassis_angle_ = kNaN; - *chassis_control_angle_ = kNaN; - trajectory_.reset(trajectory_.max_angle()); - *scope_motor_control_torque_ = kNaN; - for (auto& e : angle_errors_) - *e = kNaN; - for (auto& j : joints_) { - *j.target_angle = kNaN; - *j.target_physical_angle = kNaN; - *j.target_physical_velocity = kNaN; - *j.target_physical_acceleration = kNaN; - *j.suspension_torque = kNaN; + void scope_motor_control(bool prone_override = false) { + const bool prone_target_active = prone_override; + if (prone_target_active && *mode_ != rmcs_msgs::ChassisMode::SPIN) { + *scope_motor_control_torque = -0.3; + // if (*scope_motor_velocity <= std::abs(0.1)){ + // *scope_motor_control_torque = 0.18 * 1.0 / 36.0; + // } + } else { + *scope_motor_control_torque = 0.3; + // if (*scope_motor_velocity <= std::abs(0.1)){ + // *scope_motor_control_torque = -0.18 * 1.0 / 36.0; + // } } - for (auto& m : suspension_modes_) - *m = false; - *processed_encoder_angle_ = kNaN; - } - - // --- feedback --- - JointFeedbackFrame read_joint_feedback_() const { - JointFeedbackFrame f; - f.motor_angles.fill(kNaN); - f.physical_angles.fill(kNaN); - f.physical_velocities.fill(kNaN); - f.joint_torques.fill(kNaN); - f.eso_z2.fill(kNaN); - f.eso_z3.fill(kNaN); + } + + bool publish_current_joint_target_angles() { + const std::array*, kJointCount> motor_angle_inputs{ + &left_front_joint_angle_, &left_back_joint_angle_, &right_back_joint_angle_, + &right_front_joint_angle_}; + + std::array current_motor_angles{}; + std::array current_physical_angles{}; for (size_t i = 0; i < kJointCount; ++i) { - const auto& j = joints_[i]; - if (j.angle.ready() && std::isfinite(*j.angle)) { - f.motor_angles[i] = *j.angle; - f.physical_angles[i] = 1.090830782496456 - f.motor_angles[i]; + if (!motor_angle_inputs[i]->ready() || !std::isfinite(*(*motor_angle_inputs[i]))) { + return false; } - if (j.physical_angle.ready() && std::isfinite(*j.physical_angle)) - f.physical_angles[i] = *j.physical_angle; - if (j.physical_velocity.ready() && std::isfinite(*j.physical_velocity)) - f.physical_velocities[i] = *j.physical_velocity; - if (j.torque.ready() && std::isfinite(*j.torque)) - f.joint_torques[i] = *j.torque; - if (j.eso_z2.ready() && std::isfinite(*j.eso_z2)) - f.eso_z2[i] = *j.eso_z2; - if (j.eso_z3.ready() && std::isfinite(*j.eso_z3)) - f.eso_z3[i] = *j.eso_z3; + current_motor_angles[i] = *(*motor_angle_inputs[i]); + current_physical_angles[i] = motor_to_physical_angle(current_motor_angles[i]); } - return f; + + joint_target_angle_state_rad_ = current_motor_angles; + joint_target_physical_angle_state_rad_ = current_physical_angles; + joint_target_physical_velocity_state_rad_ = {0.0, 0.0, 0.0, 0.0}; + joint_target_physical_acceleration_state_rad_ = {0.0, 0.0, 0.0, 0.0}; + requested_target_physical_angles_rad_ = current_physical_angles; + current_target_physical_angles_rad_ = current_physical_angles; + joint_target_active_ = true; + return true; } - // --- main pipeline --- - void run_joint_intent_pipeline_() { - auto feedback = read_joint_feedback_(); + void update_joint_target_trajectory() { + const double dt = update_dt(); + for (size_t i = 0; i < kJointCount; ++i) { + double& angle_state = joint_target_physical_angle_state_rad_[i]; + double& velocity_state = joint_target_physical_velocity_state_rad_[i]; + double& acceleration_state = joint_target_physical_acceleration_state_rad_[i]; + const double target_angle = current_target_physical_angles_rad_[i]; + const double velocity_limit = joint_suspension_active_[i] + ? suspension_velocity_limit_ + : target_physical_velocity_limit_; + const double acceleration_limit = + joint_suspension_active_[i] ? suspension_acceleration_limit_ + : target_physical_acceleration_limit_; + + if (!std::isfinite(target_angle) || !std::isfinite(angle_state)) { + continue; + } - suspension_.update_imu_calibration( - trajectory_.symmetric_requested(), *chassis_imu_pitch_, *chassis_imu_roll_, - update_dt_()); + const double position_error = target_angle - angle_state; + const double stopping_distance = + velocity_state * velocity_state / (2.0 * acceleration_limit); - if (!trajectory_.active() - && !trajectory_.initialize_from_feedback( - feedback.motor_angles, feedback.physical_angles)) { - reset_all_controls_(); + double desired_velocity = 0.0; + if (std::abs(position_error) > 1e-6 && std::abs(position_error) > stopping_distance) { + desired_velocity = std::copysign(velocity_limit, position_error); + } + + const double velocity_error = desired_velocity - velocity_state; + acceleration_state = + std::clamp(velocity_error / dt, -acceleration_limit, acceleration_limit); + + velocity_state += acceleration_state * dt; + velocity_state = std::clamp(velocity_state, -velocity_limit, velocity_limit); + angle_state += velocity_state * dt; + + const double next_error = target_angle - angle_state; + if ((position_error > 0.0 && next_error < 0.0) + || (position_error < 0.0 && next_error > 0.0) + || (std::abs(next_error) < 1e-5 && std::abs(velocity_state) < 1e-3)) { + angle_state = target_angle; + velocity_state = 0.0; + acceleration_state = 0.0; + } + + joint_target_angle_state_rad_[i] = physical_to_motor_angle(angle_state); + } + } + + void publish_joint_target_angles( + const std::array& current_physical_angles) { + if (!joint_target_active_) { + publish_nan_joint_targets(); return; } - if (apply_symmetric_target_) - trajectory_.fill_symmetric_targets(); - trajectory_.refresh_deploy_targets( - suspension_requested_(), keyboard_->ctrl, trajectory_.min_angle()); + *left_front_joint_target_angle_ = joint_target_angle_state_rad_[kLeftFront]; + *left_back_joint_target_angle_ = joint_target_angle_state_rad_[kLeftBack]; + *right_back_joint_target_angle_ = joint_target_angle_state_rad_[kRightBack]; + *right_front_joint_target_angle_ = joint_target_angle_state_rad_[kRightFront]; + + *left_front_joint_target_physical_angle_ = + joint_target_physical_angle_state_rad_[kLeftFront]; + *left_back_joint_target_physical_angle_ = joint_target_physical_angle_state_rad_[kLeftBack]; + *right_back_joint_target_physical_angle_ = + joint_target_physical_angle_state_rad_[kRightBack]; + *right_front_joint_target_physical_angle_ = + joint_target_physical_angle_state_rad_[kRightFront]; + + *left_front_joint_target_physical_velocity_ = + joint_target_physical_velocity_state_rad_[kLeftFront]; + *left_back_joint_target_physical_velocity_ = + joint_target_physical_velocity_state_rad_[kLeftBack]; + *right_back_joint_target_physical_velocity_ = + joint_target_physical_velocity_state_rad_[kRightBack]; + *right_front_joint_target_physical_velocity_ = + joint_target_physical_velocity_state_rad_[kRightFront]; + + *left_front_joint_target_physical_acceleration_ = + joint_target_physical_acceleration_state_rad_[kLeftFront]; + *left_back_joint_target_physical_acceleration_ = + joint_target_physical_acceleration_state_rad_[kLeftBack]; + *right_back_joint_target_physical_acceleration_ = + joint_target_physical_acceleration_state_rad_[kRightBack]; + *right_front_joint_target_physical_acceleration_ = + joint_target_physical_acceleration_state_rad_[kRightFront]; + + *lf_angle_error_ = std::isfinite(current_physical_angles[kLeftFront]) + ? current_physical_angles[kLeftFront] + - joint_target_physical_angle_state_rad_[kLeftFront] + : nan_; + *lb_angle_error_ = std::isfinite(current_physical_angles[kLeftBack]) + ? current_physical_angles[kLeftBack] + - joint_target_physical_angle_state_rad_[kLeftBack] + : nan_; + *rb_angle_error_ = std::isfinite(current_physical_angles[kRightBack]) + ? current_physical_angles[kRightBack] + - joint_target_physical_angle_state_rad_[kRightBack] + : nan_; + *rf_angle_error_ = std::isfinite(current_physical_angles[kRightFront]) + ? current_physical_angles[kRightFront] + - joint_target_physical_angle_state_rad_[kRightFront] + : nan_; + + bool all_joint_angles_finite = true; + double physical_angle_sum = 0.0; + for (double current_physical_angle : current_physical_angles) { + if (!std::isfinite(current_physical_angle)) { + all_joint_angles_finite = false; + break; + } + physical_angle_sum += current_physical_angle; + } - scope_motor_control_(keyboard_->ctrl); + *processed_encoder_angle_ = all_joint_angles_finite ? rad_to_deg_ * physical_angle_sum + / static_cast(kJointCount) + : nan_; + } - auto target_physical = trajectory_.current_physical(); - std::array sus_modes, sus_torques; - sus_modes.fill(false); - sus_torques.fill(kNaN); + void publish_nan_joint_targets() { + reset_attitude_correction_state_(); - suspension_.update( - feedback, *chassis_imu_pitch_, *chassis_imu_roll_, *chassis_imu_pitch_rate_, - *chassis_imu_roll_rate_, update_dt_(), suspension_requested_(), trajectory_.min_angle(), - trajectory_.max_angle(), velocity_control_.control_acceleration_estimate(), - target_physical, sus_modes, sus_torques); + *left_front_joint_target_angle_ = nan_; + *left_back_joint_target_angle_ = nan_; + *right_back_joint_target_angle_ = nan_; + *right_front_joint_target_angle_ = nan_; - trajectory_.update_trajectory( - update_dt_(), suspension_requested_(), suspension_.target_vel_limit(), - suspension_.target_accel_limit()); + *left_front_joint_target_physical_angle_ = nan_; + *left_back_joint_target_physical_angle_ = nan_; + *right_back_joint_target_physical_angle_ = nan_; + *right_front_joint_target_physical_angle_ = nan_; - for (size_t i = 0; i < kJointCount; ++i) { - *joints_[i].target_angle = trajectory_.target_angles()[i]; - *joints_[i].target_physical_angle = trajectory_.target_physical_angles()[i]; - *joints_[i].target_physical_velocity = trajectory_.target_velocities()[i]; - *joints_[i].target_physical_acceleration = trajectory_.target_accelerations()[i]; - *joints_[i].suspension_torque = sus_torques[i]; - *suspension_modes_[i] = static_cast(sus_modes[i]); - } + *left_front_joint_target_physical_velocity_ = nan_; + *left_back_joint_target_physical_velocity_ = nan_; + *right_back_joint_target_physical_velocity_ = nan_; + *right_front_joint_target_physical_velocity_ = nan_; - if (apply_symmetric_target_ && trajectory_.symmetric_requested()) - trajectory_.fill_symmetric_targets(); + *left_front_joint_target_physical_acceleration_ = nan_; + *left_back_joint_target_physical_acceleration_ = nan_; + *right_back_joint_target_physical_acceleration_ = nan_; + *right_front_joint_target_physical_acceleration_ = nan_; - double sum = 0.0; - int cnt = 0; - for (const auto& j : joints_) { - if (j.physical_angle.ready() && std::isfinite(*j.physical_angle)) { - sum += *j.physical_angle; - ++cnt; - } - } - *processed_encoder_angle_ = (cnt > 0) ? kRadToDeg * sum / cnt : kNaN; - } + *lf_angle_error_ = nan_; + *lb_angle_error_ = nan_; + *rb_angle_error_ = nan_; + *rf_angle_error_ = nan_; - void scope_motor_control_(bool prone_override) { - if (prone_override && *mode_ != rmcs_msgs::ChassisMode::SPIN) - *scope_motor_control_torque_ = -0.3; - else - *scope_motor_control_torque_ = 0.3; } - // --- member variables --- - InputInterface joystick_right_, joystick_left_; - InputInterface switch_right_, switch_left_; +private: + InputInterface joystick_right_; + InputInterface joystick_left_; + InputInterface switch_right_; + InputInterface switch_left_; InputInterface mouse_velocity_; InputInterface mouse_; InputInterface keyboard_; - InputInterface rotary_knob_, update_rate_; - double last_rotary_knob_ = 0.0; + InputInterface rotary_knob_; + InputInterface update_rate_; + rmcs_msgs::Switch last_switch_right_ = rmcs_msgs::Switch::UNKNOWN; rmcs_msgs::Switch last_switch_left_ = rmcs_msgs::Switch::UNKNOWN; rmcs_msgs::Keyboard last_keyboard_ = rmcs_msgs::Keyboard::zero(); + double last_rotary_knob_ = 0.0; InputInterface gimbal_yaw_angle_, gimbal_yaw_angle_error_; OutputInterface chassis_angle_, chassis_control_angle_; + OutputInterface mode_; OutputInterface chassis_control_velocity_; - ChassisVelocityControl velocity_control_; - ActiveSuspension suspension_; - JointTrajectoryPlanner trajectory_; - bool apply_symmetric_target_ = true; - - std::array joints_{}; - std::array angle_errors_; - std::array, kJointCount> suspension_modes_; - InputInterface chassis_imu_pitch_, chassis_imu_roll_, chassis_imu_pitch_rate_, - chassis_imu_roll_rate_; - OutputInterface scope_motor_control_torque_, processed_encoder_angle_; - - std::array joint_offsets_{}; // FIXME: Unused Var + bool spinning_forward_ = true; + bool apply_symmetric_target = true; + pid::PidCalculator following_velocity_controller_; + const double spin_ratio_; + + InputInterface left_front_joint_angle_; + InputInterface left_back_joint_angle_; + InputInterface right_front_joint_angle_; + InputInterface right_back_joint_angle_; + + InputInterface left_front_joint_physical_angle_; + InputInterface left_back_joint_physical_angle_; + InputInterface right_front_joint_physical_angle_; + InputInterface right_back_joint_physical_angle_; + InputInterface left_front_joint_physical_velocity_; + InputInterface left_back_joint_physical_velocity_; + InputInterface right_front_joint_physical_velocity_; + InputInterface right_back_joint_physical_velocity_; + InputInterface left_front_joint_torque_; + InputInterface left_back_joint_torque_; + InputInterface right_front_joint_torque_; + InputInterface right_back_joint_torque_; + + InputInterface left_front_joint_encoder_angle_; + InputInterface left_back_joint_encoder_angle_; + InputInterface right_front_joint_encoder_angle_; + InputInterface right_back_joint_encoder_angle_; + InputInterface chassis_imu_pitch_; + InputInterface chassis_imu_roll_; + InputInterface chassis_imu_pitch_rate_; + InputInterface chassis_imu_roll_rate_; + + OutputInterface scope_motor_control_torque; + + OutputInterface lf_angle_error_; + OutputInterface lb_angle_error_; + OutputInterface rf_angle_error_; + OutputInterface rb_angle_error_; + + OutputInterface left_front_joint_target_angle_; + OutputInterface left_back_joint_target_angle_; + OutputInterface right_back_joint_target_angle_; + OutputInterface right_front_joint_target_angle_; + + OutputInterface left_front_joint_target_physical_angle_; + OutputInterface left_back_joint_target_physical_angle_; + OutputInterface right_back_joint_target_physical_angle_; + OutputInterface right_front_joint_target_physical_angle_; + OutputInterface left_front_joint_target_physical_velocity_; + OutputInterface left_back_joint_target_physical_velocity_; + OutputInterface right_back_joint_target_physical_velocity_; + OutputInterface right_front_joint_target_physical_velocity_; + OutputInterface left_front_joint_target_physical_acceleration_; + OutputInterface left_back_joint_target_physical_acceleration_; + OutputInterface right_back_joint_target_physical_acceleration_; + OutputInterface right_front_joint_target_physical_acceleration_; + + OutputInterface processed_encoder_angle_; + + double min_angle_; + double max_angle_; + double left_front_joint_offset_; + double left_back_joint_offset_; + double right_front_joint_offset_; + double right_back_joint_offset_; JointFeedbackSource joint_feedback_source_ = JointFeedbackSource::kLegacyEncoderAngle; + + double current_target_angle_; + double lf_current_target_angle_, lb_current_target_angle_, rb_current_target_angle_, + rf_current_target_angle_; + + std::array requested_target_physical_angles_rad_ = {0.0, 0.0, 0.0, 0.0}; + std::array current_target_physical_angles_rad_ = {0.0, 0.0, 0.0, 0.0}; + + bool joint_target_active_ = false; + std::array joint_target_angle_state_rad_ = {0.0, 0.0, 0.0, 0.0}; + std::array joint_target_physical_angle_state_rad_ = {0.0, 0.0, 0.0, 0.0}; + std::array joint_target_physical_velocity_state_rad_ = { + 0.0, 0.0, 0.0, 0.0}; + std::array joint_target_physical_acceleration_state_rad_ = { + 0.0, 0.0, 0.0, 0.0}; + + double target_physical_velocity_limit_; + double target_physical_acceleration_limit_; + bool active_suspension_enable_; + double pitch_kp_; + double pitch_ki_; + double pitch_kd_; + double roll_kp_; + double roll_ki_; + double roll_kd_; + double suspension_velocity_limit_; + double suspension_acceleration_limit_; + double pitch_diff_limit_; + double roll_diff_limit_; + double pid_integral_limit_; + std::array joint_suspension_active_ = {false, false, false, false}; + AttitudePidAxis pitch_pid_; + AttitudePidAxis roll_pid_; + double chassis_imu_pitch_offset_ = 0.0; + double chassis_imu_roll_offset_ = 0.0; + double chassis_imu_calibration_wait_time_; + double chassis_imu_calibration_sample_time_; + double chassis_imu_calibration_hold_elapsed_ = 0.0; + size_t chassis_imu_calibration_sample_count_ = 0; + double chassis_imu_pitch_sum_ = 0.0; + double chassis_imu_roll_sum_ = 0.0; + bool chassis_imu_calibration_completed_for_window_ = false; + static constexpr double default_dt_ = 1e-3; + static constexpr double joint_zero_physical_angle_rad_ = 1.090830782496456; + + static constexpr double pi_ = std::numbers::pi; }; } // namespace rmcs_core::controller::chassis diff --git a/rmcs_ws/src/rmcs_core/src/controller/chassis/deformable_joint_layer.hpp b/rmcs_ws/src/rmcs_core/src/controller/chassis/deformable_joint_layer.hpp deleted file mode 100644 index beeb1d2f..00000000 --- a/rmcs_ws/src/rmcs_core/src/controller/chassis/deformable_joint_layer.hpp +++ /dev/null @@ -1,172 +0,0 @@ -#pragma once - -#include -#include -#include -#include -#include - -#include - -namespace rmcs_core::controller::chassis { - -enum class JointFeedbackSource : uint8_t { kLegacyEncoderAngle, kMotorAngle }; - -enum JointIndex : size_t { - kLeftFront = 0, - kLeftBack = 1, - kRightBack = 2, - kRightFront = 3, - kJointCount = 4 -}; - -inline constexpr std::array kJointNames{ - "left_front", "left_back", "right_back", "right_front"}; - -struct JointFeedbackFrame { - std::array motor_angles{}; - std::array physical_angles{}; - std::array physical_velocities{}; - std::array joint_torques{}; - std::array eso_z2{}; - std::array eso_z3{}; -}; - -struct LegFeedback { - double motor_angle = std::numeric_limits::quiet_NaN(); - double physical_angle = std::numeric_limits::quiet_NaN(); - double physical_velocity = std::numeric_limits::quiet_NaN(); - double joint_torque = std::numeric_limits::quiet_NaN(); - double eso_z2 = std::numeric_limits::quiet_NaN(); - double eso_z3 = std::numeric_limits::quiet_NaN(); -}; - -struct JointIO { - using In = rmcs_executor::Component::InputInterface; - using Out = rmcs_executor::Component::OutputInterface; - In angle, physical_angle, physical_velocity, torque, encoder_angle, eso_z2, eso_z3; - Out target_angle, target_physical_angle, target_physical_velocity, target_physical_acceleration; - Out suspension_torque; -}; - -struct JointTrajectoryPlanner { - static constexpr double kJointZeroPhysicalAngleRad = 1.090830782496456; - - void init(double min_angle, double max_angle, double velocity_limit, double acceleration_limit) { - min_angle_ = min_angle; - max_angle_ = max_angle; - velocity_limit_ = velocity_limit; - acceleration_limit_ = acceleration_limit; - } - - void set_target_angle(double angle) { current_target_angle_ = angle; } - double target_angle() const { return current_target_angle_; } - - bool initialize_from_feedback( - const std::array& motor_angles, - const std::array& physical_angles) { - for (size_t i = 0; i < kJointCount; ++i) - if (!std::isfinite(motor_angles[i]) || !std::isfinite(physical_angles[i])) - return false; - target_motor_state_ = motor_angles; - target_physical_state_ = physical_angles; - target_velocity_state_.fill(0.0); - target_acceleration_state_.fill(0.0); - requested_physical_ = physical_angles; - current_physical_ = physical_angles; - active_ = true; - return true; - } - - void sync_from_feedback(size_t index, double motor_angle, double physical_angle) { - target_motor_state_[index] = motor_angle; - target_physical_state_[index] = physical_angle; - target_velocity_state_[index] = 0.0; - target_acceleration_state_[index] = 0.0; - } - - bool active() const { return active_; } - void set_active(bool value) { active_ = value; } - - void fill_symmetric_targets() { per_joint_targets_.fill(current_target_angle_); } - - bool symmetric_requested() const { - for (size_t i = 1; i < kJointCount; ++i) - if (std::abs(per_joint_targets_[0] - per_joint_targets_[i]) > 1e-6) - return false; - return true; - } - - void refresh_deploy_targets(bool deploy_requested, bool /*prone_override*/, double deploy_angle) { - for (size_t i = 0; i < kJointCount; ++i) - requested_physical_[i] = per_joint_targets_[i] * std::numbers::pi / 180.0; - if (deploy_requested) - requested_physical_.fill(deploy_angle * std::numbers::pi / 180.0); - current_physical_ = requested_physical_; - } - - void update_trajectory( - double delta_time, bool use_suspension_limits, - double suspension_velocity_limit, double suspension_acceleration_limit) { - double velocity_limit = use_suspension_limits ? suspension_velocity_limit : velocity_limit_; - double acceleration_limit = - use_suspension_limits ? suspension_acceleration_limit : acceleration_limit_; - for (size_t i = 0; i < kJointCount; ++i) { - double target = current_physical_[i]; - double current_position = target_physical_state_[i]; - double current_velocity = target_velocity_state_[i]; - double error = target - current_position; - double max_velocity = std::sqrt(2.0 * acceleration_limit * std::abs(error)); - double command_velocity = std::copysign(std::min(max_velocity, velocity_limit), error); - double delta_velocity = command_velocity - current_velocity; - double command_acceleration = std::clamp(delta_velocity / delta_time, -acceleration_limit, acceleration_limit); - target_acceleration_state_[i] = command_acceleration; - target_velocity_state_[i] = - std::clamp(current_velocity + command_acceleration * delta_time, -velocity_limit, velocity_limit); - target_physical_state_[i] += target_velocity_state_[i] * delta_time; - target_motor_state_[i] = kJointZeroPhysicalAngleRad - target_physical_state_[i]; - } - } - - const std::array& target_angles() const { return target_motor_state_; } - const std::array& target_physical_angles() const { - return target_physical_state_; - } - const std::array& target_velocities() const { - return target_velocity_state_; - } - const std::array& target_accelerations() const { - return target_acceleration_state_; - } - const std::array& current_physical() const { return current_physical_; } - - void reset(double angle) { - current_target_angle_ = angle; - per_joint_targets_.fill(angle); - active_ = false; - target_motor_state_.fill(0.0); - target_physical_state_.fill(0.0); - target_velocity_state_.fill(0.0); - target_acceleration_state_.fill(0.0); - } - - double min_angle() const { return min_angle_; } - double max_angle() const { return max_angle_; } - -private: - double min_angle_ = 15.0; - double max_angle_ = 55.0; - double velocity_limit_ = 1.0; - double acceleration_limit_ = 1.0; - double current_target_angle_ = 55.0; - std::array per_joint_targets_{55.0, 55.0, 55.0, 55.0}; - bool active_ = false; - std::array target_motor_state_{}; - std::array target_physical_state_{}; - std::array target_velocity_state_{}; - std::array target_acceleration_state_{}; - std::array requested_physical_{}; - std::array current_physical_{}; -}; - -} // namespace rmcs_core::controller::chassis From e33df491d9414cf52f9556a185a794948079de77 Mon Sep 17 00:00:00 2001 From: Yukikaze2233 Date: Sun, 10 May 2026 14:43:32 +0800 Subject: [PATCH 02/52] feat(ui): integrate pitch HUD into deformable infantry referee display - Add pitch_hud.hpp widget (pitch scale with gimbal/chassis indicators) - Register /chassis/imu/pitch and /gimbal/pitch/angle inputs - Call pitch_hud_.update() in deformable_infantry_ui update loop --- .../referee/app/ui/deformable_infantry_ui.cpp | 18 ++ .../src/referee/app/ui/widget/pitch_hud.hpp | 207 ++++++++++++++++++ 2 files changed, 225 insertions(+) create mode 100644 rmcs_ws/src/rmcs_core/src/referee/app/ui/widget/pitch_hud.hpp diff --git a/rmcs_ws/src/rmcs_core/src/referee/app/ui/deformable_infantry_ui.cpp b/rmcs_ws/src/rmcs_core/src/referee/app/ui/deformable_infantry_ui.cpp index 269a5682..8e7a9393 100644 --- a/rmcs_ws/src/rmcs_core/src/referee/app/ui/deformable_infantry_ui.cpp +++ b/rmcs_ws/src/rmcs_core/src/referee/app/ui/deformable_infantry_ui.cpp @@ -2,6 +2,7 @@ #include #include #include +#include #include #include @@ -12,6 +13,7 @@ #include "referee/app/ui/shape/shape.hpp" #include "referee/app/ui/widget/crosshair_circle.hpp" #include "referee/app/ui/widget/deformable_chassis_top_view.hpp" +#include "referee/app/ui/widget/pitch_hud.hpp" #include "referee/app/ui/widget/status_ring.hpp" namespace rmcs_core::referee::app::ui { @@ -32,6 +34,7 @@ class DeformableInfantry {Shape::Color::WHITE, 2, x_center, 800, x_center, y_center + 110}, {Shape::Color::WHITE, 2, x_center, y_center - 110, x_center, 200}) , chassis_direction_indicator_(Shape::Color::PINK, 8, x_center, y_center, 0, 0, 84, 84) + , pitch_hud_(PitchHud::Config{1540, y_center, 180, 30.0, 5.0}) , time_reminder_(Shape::Color::PINK, 50, 5, x_center + 150, y_center + 65, 0, false) { double deformable_leg_min_angle_deg = 8.0; @@ -48,6 +51,7 @@ class DeformableInfantry register_input("/chassis/control_mode", chassis_mode_); register_input("/chassis/angle", chassis_angle_); + register_input("/chassis/imu/pitch", chassis_pitch_, false); register_input("/chassis/supercap/voltage", supercap_voltage_); register_input("/chassis/supercap/enabled", supercap_enabled_); @@ -76,6 +80,8 @@ class DeformableInfantry register_input("/remote/mouse", mouse_); + register_input("/gimbal/pitch/angle", gimbal_pitch_angle_, false); + register_input("/referee/game/stage", game_stage_); } @@ -91,6 +97,13 @@ class DeformableInfantry status_ring_.update_battery_power(*chassis_voltage_); status_ring_.update_auto_aim_enable(mouse_->right == 1); + pitch_hud_.update( + (gimbal_pitch_angle_.ready() && std::isfinite(*gimbal_pitch_angle_)) + ? *gimbal_pitch_angle_ + : std::numeric_limits::quiet_NaN(), + (chassis_pitch_.ready() && std::isfinite(*chassis_pitch_)) + ? *chassis_pitch_ + : std::numeric_limits::quiet_NaN()); } private: @@ -160,6 +173,9 @@ class DeformableInfantry InputInterface mouse_; + InputInterface gimbal_pitch_angle_; + InputInterface chassis_pitch_; + InputInterface game_stage_; CrossHairCircle crosshair_circle_; @@ -171,6 +187,8 @@ class DeformableInfantry Arc chassis_direction_indicator_; DeformableChassisLegArcs deformable_chassis_leg_arcs_; + PitchHud pitch_hud_; + Integer time_reminder_; }; diff --git a/rmcs_ws/src/rmcs_core/src/referee/app/ui/widget/pitch_hud.hpp b/rmcs_ws/src/rmcs_core/src/referee/app/ui/widget/pitch_hud.hpp new file mode 100644 index 00000000..ebeea1e4 --- /dev/null +++ b/rmcs_ws/src/rmcs_core/src/referee/app/ui/widget/pitch_hud.hpp @@ -0,0 +1,207 @@ +#pragma once + +#include +#include +#include +#include +#include + +#include "referee/app/ui/shape/shape.hpp" + +namespace rmcs_core::referee::app::ui { + +class PitchHud { +public: + struct Config { + uint16_t center_x = 1540; + uint16_t center_y = y_center; + uint16_t half_height_px = 180; + double half_span_deg = 30.0; + double tick_step_deg = 5.0; + }; + + PitchHud() { set_config(Config{}); } + + explicit PitchHud(Config config) { set_config(config); } + + void set_config(Config config) { + config.tick_step_deg = std::max(config.tick_step_deg, 1.0); + config.half_span_deg = std::max(config.half_span_deg, config.tick_step_deg); + config.half_height_px = std::max(config.half_height_px, 40); + config_ = config; + initialize_(); + } + + void update(double gimbal_pitch, double chassis_pitch) { + if (std::isfinite(gimbal_pitch)) + update_gimbal_pitch_indicator_(pitch_to_hud_y_(gimbal_pitch)); + else + set_indicator_visible_(gimbal_pitch_indicator_, false); + + if (std::isfinite(chassis_pitch)) + update_chassis_pitch_indicator_(pitch_to_hud_y_(chassis_pitch)); + else + set_indicator_visible_(chassis_pitch_indicator_, false); + } + +private: + static constexpr std::size_t tick_capacity_ = 25; + + void initialize_() { + tick_count_ = std::clamp( + static_cast( + std::floor(2.0 * config_.half_span_deg / config_.tick_step_deg + 1.0e-6)) + + 1, + std::size_t{1}, tick_capacity_); + + axis_.set_color(Shape::Color::YELLOW); + axis_.set_width(2); + axis_.set_x(config_.center_x); + axis_.set_y(axis_top_y_()); + axis_.set_x2(config_.center_x); + axis_.set_y2(axis_bottom_y_()); + axis_.set_visible(true); + + for (std::size_t i = 0; i < ticks_.size(); ++i) { + auto& tick = ticks_[i]; + if (i >= tick_count_) { + tick.set_visible(false); + continue; + } + + const double tick_deg = -config_.half_span_deg + static_cast(i) * config_.tick_step_deg; + const int rounded_tick_deg = static_cast(std::lround(tick_deg)); + if (rounded_tick_deg == 0) { + tick.set_visible(false); + continue; + } + + const bool is_major = (std::abs(rounded_tick_deg) % 10 == 0); + const uint16_t tick_length = is_major ? 18 : 10; + const uint16_t tick_y = pitch_to_hud_y_(deg_to_rad_(tick_deg)); + + tick.set_color(Shape::Color::YELLOW); + tick.set_width(2); + tick.set_x(config_.center_x); + tick.set_y(tick_y); + tick.set_x2(config_.center_x + tick_length); + tick.set_y2(tick_y); + tick.set_visible(true); + } + + for (auto& line : gimbal_pitch_indicator_) { + line.set_color(Shape::Color::YELLOW); + line.set_width(2); + line.set_visible(false); + } + for (auto& line : chassis_pitch_indicator_) { + line.set_color(Shape::Color::YELLOW); + line.set_width(2); + line.set_visible(false); + } + } + + uint16_t axis_top_y_() const { + return clamp_to_screen_y_( + static_cast(config_.center_y) - static_cast(config_.half_height_px)); + } + + uint16_t axis_bottom_y_() const { + return clamp_to_screen_y_( + static_cast(config_.center_y) + static_cast(config_.half_height_px)); + } + + static constexpr double deg_to_rad_(double degrees) { + return degrees * std::numbers::pi / 180.0; + } + + static uint16_t clamp_to_screen_y_(double y) { + return static_cast(std::clamp( + std::lround(y), 0l, static_cast(screen_height - 1))); + } + + uint16_t pitch_to_hud_y_(double pitch_rad) const { + const double clamped_pitch = + std::clamp(pitch_rad, -deg_to_rad_(config_.half_span_deg), + deg_to_rad_(config_.half_span_deg)); + const double normalized = clamped_pitch / deg_to_rad_(config_.half_span_deg); + return clamp_to_screen_y_( + static_cast(config_.center_y) + + normalized * static_cast(config_.half_height_px)); + } + + void update_gimbal_pitch_indicator_(uint16_t y) { + const uint16_t tip_x = config_.center_x - 8; + const uint16_t back_x = config_.center_x - 20; + constexpr uint16_t half_height = 8; + const uint16_t top_y = clamp_to_screen_y_(static_cast(y) - half_height); + const uint16_t bottom_y = clamp_to_screen_y_(static_cast(y) + half_height); + + gimbal_pitch_indicator_[0].set_x(back_x); + gimbal_pitch_indicator_[0].set_y(top_y); + gimbal_pitch_indicator_[0].set_x2(tip_x); + gimbal_pitch_indicator_[0].set_y2(y); + + gimbal_pitch_indicator_[1].set_x(back_x); + gimbal_pitch_indicator_[1].set_y(bottom_y); + gimbal_pitch_indicator_[1].set_x2(tip_x); + gimbal_pitch_indicator_[1].set_y2(y); + + gimbal_pitch_indicator_[2].set_x(back_x); + gimbal_pitch_indicator_[2].set_y(top_y); + gimbal_pitch_indicator_[2].set_x2(back_x); + gimbal_pitch_indicator_[2].set_y2(bottom_y); + + set_indicator_visible_(gimbal_pitch_indicator_, true); + } + + void update_chassis_pitch_indicator_(uint16_t y) { + const uint16_t front_x = config_.center_x - 20; + const uint16_t rear_x = config_.center_x - 34; + constexpr uint16_t front_half_height = 12; + constexpr uint16_t rear_half_height = 8; + const uint16_t front_top_y = clamp_to_screen_y_(static_cast(y) - front_half_height); + const uint16_t front_bottom_y = + clamp_to_screen_y_(static_cast(y) + front_half_height); + const uint16_t rear_top_y = clamp_to_screen_y_(static_cast(y) - rear_half_height); + const uint16_t rear_bottom_y = + clamp_to_screen_y_(static_cast(y) + rear_half_height); + + chassis_pitch_indicator_[0].set_x(rear_x); + chassis_pitch_indicator_[0].set_y(rear_top_y); + chassis_pitch_indicator_[0].set_x2(front_x); + chassis_pitch_indicator_[0].set_y2(front_top_y); + + chassis_pitch_indicator_[1].set_x(front_x); + chassis_pitch_indicator_[1].set_y(front_top_y); + chassis_pitch_indicator_[1].set_x2(front_x); + chassis_pitch_indicator_[1].set_y2(front_bottom_y); + + chassis_pitch_indicator_[2].set_x(front_x); + chassis_pitch_indicator_[2].set_y(front_bottom_y); + chassis_pitch_indicator_[2].set_x2(rear_x); + chassis_pitch_indicator_[2].set_y2(rear_bottom_y); + + chassis_pitch_indicator_[3].set_x(rear_x); + chassis_pitch_indicator_[3].set_y(rear_bottom_y); + chassis_pitch_indicator_[3].set_x2(rear_x); + chassis_pitch_indicator_[3].set_y2(rear_top_y); + + set_indicator_visible_(chassis_pitch_indicator_, true); + } + + template + static void set_indicator_visible_(std::array& indicator, bool visible) { + for (auto& line : indicator) + line.set_visible(visible); + } + + Config config_{}; + std::size_t tick_count_ = 0; + Line axis_; + std::array ticks_; + std::array gimbal_pitch_indicator_; + std::array chassis_pitch_indicator_; +}; + +} // namespace rmcs_core::referee::app::ui From b9acd1ffb10dad035ac086ff337bb8e34ce11f2c Mon Sep 17 00:00:00 2001 From: Yukikaze2233 Date: Sun, 10 May 2026 22:03:53 +0800 Subject: [PATCH 03/52] feat: add omni-b variant, unify calibration and CAN strategy across 3 chassis - Add deformable-infantry-omni-b: omni chassis + steering gimbal, no separate IMU board - Replace per-chassis calibrate topics with unified /rmcs/service/robot_status - Adopt two-frame alternating CAN transmission (even: 0x200+0x142, odd: 0x141) - Align BottomBoard constructor formatting across omni/omni-b/steering - Add --link-default to build-rmcs-cross for convenience symlinks - Add pitch HUD widget to referee UI --- .script/build-rmcs-cross | 42 +- .script/complete/_build-rmcs-cross | 1 + docs/zh-cn/cross_build.md | 28 +- .../config/deformable-infantry-omni-b.yaml | 345 ++++++++ .../config/deformable-infantry-omni.yaml | 2 +- .../config/deformable-infantry-steering.yaml | 19 +- rmcs_ws/src/rmcs_core/plugins.xml | 1 + .../controller/chassis/deformable_chassis.cpp | 52 ++ .../hardware/deformable-infantry-omni-b.cpp | 777 ++++++++++++++++++ .../src/hardware/deformable-infantry-omni.cpp | 3 +- .../hardware/deformable-infantry-steering.cpp | 174 ++-- 11 files changed, 1330 insertions(+), 114 deletions(-) create mode 100644 rmcs_ws/src/rmcs_bringup/config/deformable-infantry-omni-b.yaml create mode 100644 rmcs_ws/src/rmcs_core/src/hardware/deformable-infantry-omni-b.cpp diff --git a/.script/build-rmcs-cross b/.script/build-rmcs-cross index 21c987ec..488054da 100755 --- a/.script/build-rmcs-cross +++ b/.script/build-rmcs-cross @@ -7,15 +7,17 @@ set -euo pipefail usage() { cat <<'EOF' Usage: - build-rmcs-cross --target-arch [colcon build args...] + build-rmcs-cross --target-arch [--link-default] [colcon build args...] Examples: build-rmcs-cross --target-arch arm64 + build-rmcs-cross --target-arch arm64 --link-default build-rmcs-cross --target-arch amd64 --packages-up-to rmcs_executor EOF } target_arch="" +link_default=0 colcon_args=() while (($# > 0)); do @@ -33,6 +35,10 @@ while (($# > 0)); do target_arch="${1#*=}" shift ;; + --link-default) + link_default=1 + shift + ;; -h | --help) usage exit 0 @@ -150,6 +156,30 @@ build_base="build-cross-${RMCS_TARGET_ARCH}" install_base="install-cross-${RMCS_TARGET_ARCH}" log_base="log-cross-${RMCS_TARGET_ARCH}" +check_default_linkable() { + local target="$1" + local link_name="$2" + + if [[ ! -d "${target}" ]]; then + echo "> ERROR: Cross build output not found: ${workspace}/${target}" + exit 1 + fi + + if [[ -e "${link_name}" && ! -L "${link_name}" ]]; then + echo "> ERROR: Cannot link ${workspace}/${link_name} -> ${target}." + echo "> ${workspace}/${link_name} exists and is not a symlink. Move or remove it first." + exit 1 + fi +} + +link_default_base() { + local target="$1" + local link_name="$2" + + ln -sfnT "${target}" "${link_name}" + echo "> Linked ${workspace}/${link_name} -> ${target}" +} + CLICOLOR_FORCE=1 NINJA_STATUS="" \ colcon \ --log-base "${log_base}" \ @@ -163,3 +193,13 @@ CLICOLOR_FORCE=1 NINJA_STATUS="" \ -DRMCS_TARGET_ARCH="${RMCS_TARGET_ARCH}" \ -DRMCS_SYSROOT="${RMCS_SYSROOT}" \ -DRMCS_TARGET_TRIPLET="${RMCS_TARGET_TRIPLET}" + +if ((link_default)); then + check_default_linkable "${build_base}" build + check_default_linkable "${install_base}" install + check_default_linkable "${log_base}" log + + link_default_base "${build_base}" build + link_default_base "${install_base}" install + link_default_base "${log_base}" log +fi diff --git a/.script/complete/_build-rmcs-cross b/.script/complete/_build-rmcs-cross index 18004636..34d609c2 100644 --- a/.script/complete/_build-rmcs-cross +++ b/.script/complete/_build-rmcs-cross @@ -2,4 +2,5 @@ _arguments \ '--target-arch=[Cross compile target architecture]:target:(arm64 amd64)' \ + '--link-default[Link build/install/log to cross build output directories]' \ '*:colcon build args:' diff --git a/docs/zh-cn/cross_build.md b/docs/zh-cn/cross_build.md index 226a3aa7..b197541b 100644 --- a/docs/zh-cn/cross_build.md +++ b/docs/zh-cn/cross_build.md @@ -31,13 +31,13 @@ build-rmcs-cross --target-arch arm64 ``` -适用于 `linux/amd64` 的 `latest-full` 变体。 +适用于 `linux/arm64` 的 `latest-full` 变体。 ```bash build-rmcs-cross --target-arch amd64 ``` -适用于 `linux/arm64` 的 `latest-full` 变体。 +适用于 `linux/amd64` 的 `latest-full` 变体。 例如,可追加常见 `colcon build` 参数: @@ -45,6 +45,30 @@ build-rmcs-cross --target-arch amd64 build-rmcs-cross --target-arch arm64 --packages-up-to rmcs_executor ``` +若需要让现有 `sync-remote`、`env_setup` 等继续使用默认目录,可在 cross 构建成功后自动链接默认目录: + +```bash +build-rmcs-cross --target-arch arm64 --link-default +``` + +链接方向等价于在 `rmcs_ws` 下执行: + +```bash +ln -sfn build-cross-arm64 build +ln -sfn install-cross-arm64 install +ln -sfn log-cross-arm64 log +``` + +也就是创建或更新默认目录名,让 `build`、`install`、`log` 这三个软链接分别指向对应的 `*-cross-arm64` 目录。 + +切回 native 构建时,直接运行: + +```bash +build-rmcs +``` + +`build-rmcs` 会自动识别上述 cross 默认软链接,删除软链接并恢复成普通目录。 + ## 4. 构建环境隔离约束 `build-rmcs-cross` 会显式清理并重建以下环境,避免 host/target 串用: diff --git a/rmcs_ws/src/rmcs_bringup/config/deformable-infantry-omni-b.yaml b/rmcs_ws/src/rmcs_bringup/config/deformable-infantry-omni-b.yaml new file mode 100644 index 00000000..18d5bd50 --- /dev/null +++ b/rmcs_ws/src/rmcs_bringup/config/deformable-infantry-omni-b.yaml @@ -0,0 +1,345 @@ +rmcs_executor: + ros__parameters: + update_rate: 1000.0 + components: + - rmcs_core::hardware::DeformableInfantryOmniB -> deformable_infantry + + - rmcs_core::referee::Status -> referee_status + - rmcs_core::referee::Command -> referee_command + + - rmcs_core::referee::command::Interaction -> referee_interaction + - rmcs_core::referee::command::interaction::Ui -> referee_ui + - rmcs_core::referee::app::ui::DeformableInfantry -> referee_ui_infantry + + - rmcs_core::controller::gimbal::DeformableInfantryGimbalController -> gimbal_controller + + - rmcs_core::controller::shooting::FrictionWheelController -> friction_wheel_controller + - rmcs_core::controller::shooting::HeatController -> heat_controller + - rmcs_core::controller::shooting::BulletFeederController17mm -> bullet_feeder_controller + - rmcs_core::controller::pid::PidController -> left_friction_velocity_pid_controller + - rmcs_core::controller::pid::PidController -> right_friction_velocity_pid_controller + - rmcs_core::controller::pid::PidController -> bullet_feeder_velocity_pid_controller + + - rmcs_core::controller::chassis::DeformableChassis -> chassis_controller + - rmcs_core::controller::chassis::ChassisPowerController -> chassis_power_controller + - rmcs_core::controller::chassis::DeformableOmniWheelController -> deformable_chassis_controller + + - rmcs_core::controller::chassis::DeformableJointController -> lf_joint_controller + - rmcs_core::controller::chassis::DeformableJointController -> lb_joint_controller + - rmcs_core::controller::chassis::DeformableJointController -> rb_joint_controller + - rmcs_core::controller::chassis::DeformableJointController -> rf_joint_controller + + # - rmcs_core::broadcaster::ValueBroadcaster -> value_broadcaster + +value_broadcaster: + ros__parameters: + forward_list: + - /gimbal/yaw/angle + - /gimbal/yaw/velocity + + +deformable_infantry: + ros__parameters: + serial_filter_rmcs_board: "AF-23FB-EE32-B892-1302-AE70-D640-7B4E-0CBF" + serial_filter_top_board: "AF-ABAC-786D-1B53-99F6-00A2-42A6-AA95-9D69" + left_front_zero_point: 7173 + left_back_zero_point: 5167 + right_back_zero_point: 3098 + right_front_zero_point: 6485 + yaw_motor_zero_point: 39442 + pitch_motor_zero_point: 56556 + debug_log_supercap: false + debug_log_wheel_motor: false + debug_log_deformable_joint_motor: false + +chassis_controller: + ros__parameters: + # Deploy geometry / chassis-owned joint intent + min_angle: 20.0 + max_angle: 50.0 + active_suspension_enable: true + spin_ratio: 1.0 + + # IMU attitude correction at min-angle stance. + active_suspension_pitch_kp: 8.0 + active_suspension_pitch_ki: 0.35 + active_suspension_pitch_kd: 0.28 + + active_suspension_roll_kp: 8.0 + active_suspension_roll_ki: 0.35 + active_suspension_roll_kd: 0.28 + + active_suspension_pitch_angle_diff_limit_deg: 45.0 + active_suspension_roll_angle_diff_limit_deg: 45.0 + active_suspension_pid_integral_limit_deg: 20.0 + + # Chassis-owned joint intent trajectory limits while attitude correction is active. + active_suspension_target_velocity_limit_deg: 80.0 + active_suspension_target_acceleration_limit_deg: 360.0 + + # Automatic IMU mounting-error calibration. + # When all four requested joint targets stay equal for 2s, average pitch/roll from 2s to 5s. + chassis_imu_calibration_wait_s: 2.0 + chassis_imu_calibration_sample_s: 3.0 + +gimbal_controller: + ros__parameters: + inertia: 1.0 # kg·m² + friction: 1.65 # Nm/(rad/s) + + upper_limit: -0.61 # -35 deg + lower_limit: 0.05 # 6 deg + use_encoder_pitch: true + + yaw_angle_kp: 10.0 + yaw_angle_ki: 0.0 + yaw_angle_kd: 0.0 + + yaw_velocity_kp: 8.0 + yaw_velocity_ki: 0.0 + yaw_velocity_kd: 0.0 + + pitch_angle_kp: 40.0 + pitch_angle_ki: 0.0 + pitch_angle_kd: 0.0 + + pitch_velocity_kp: 3.0 + pitch_velocity_ki: 0.0 + pitch_velocity_kd: 0.0 + + pitch_torque_control: true + +friction_wheel_controller: + ros__parameters: + friction_wheels: + - /gimbal/left_friction + - /gimbal/right_friction + friction_velocities: + - 600.0 + - 600.0 + friction_soft_start_stop_time: 1.0 + +heat_controller: + ros__parameters: + heat_per_shot: 10000 + reserved_heat: 0 + +bullet_feeder_controller: + ros__parameters: + bullets_per_feeder_turn: 8.0 + shot_frequency: 30.0 + safe_shot_frequency: 10.0 + eject_frequency: 10.0 + eject_time: 0.05 + deep_eject_frequency: 5.0 + deep_eject_time: 0.2 + single_shot_max_stop_delay: 2.0 + +left_friction_velocity_pid_controller: + ros__parameters: + measurement: /gimbal/left_friction/velocity + setpoint: /gimbal/left_friction/control_velocity + control: /gimbal/left_friction/control_torque + kp: 0.003436926 + ki: 0.00 + kd: 0.009373434 + +right_friction_velocity_pid_controller: + ros__parameters: + measurement: /gimbal/right_friction/velocity + setpoint: /gimbal/right_friction/control_velocity + control: /gimbal/right_friction/control_torque + kp: 0.003436926 + ki: 0.00 + kd: 0.009373434 + +bullet_feeder_velocity_pid_controller: + ros__parameters: + measurement: /gimbal/bullet_feeder/velocity + setpoint: /gimbal/bullet_feeder/control_velocity + control: /gimbal/bullet_feeder/control_torque + kp: 1.4 + ki: 0.0 + kd: 0.0 + +deformable_chassis_controller: + ros__parameters: + mass: 23.0 + moment_of_inertia: 1.0 + chassis_radius: 0.2341741 + rod_length: 0.150 + wheel_radius: 0.055 + friction_coefficient: 0.6 + k1: 2.958580e+00 + k2: 3.082190e-03 + no_load_power: 11.37 + +lf_joint_controller: + ros__parameters: + # Joint-local servo inputs produced by chassis intent generation + measurement_angle: /chassis/left_front_joint/physical_angle + measurement_velocity: /chassis/left_front_joint/physical_velocity + setpoint_angle: /chassis/left_front_joint/target_physical_angle + setpoint_velocity: /chassis/left_front_joint/target_physical_velocity + mode_input: /chassis/left_front_joint/suspension_mode + suspension_torque: /chassis/left_front_joint/suspension_torque + control: /chassis/left_front_joint/control_torque + + # Normal ADRC servo mode + dt: 0.001 + b0: -1.0 + kt: 1.0 + td_h: 0.001 + td_r: 50.0 + eso_w0: 250.0 + eso_auto_beta: true + k1: 30.0 + k2: 17.0 + alpha1: 0.75 + alpha2: 0.7 + delta: 0.02 + u_min: -200.0 + u_max: 200.0 + output_min: -200.0 + output_max: 200.0 + + # Suspension ADRC servo mode + suspension_td_h: 0.001 + suspension_td_r: 12.0 + suspension_eso_w0: 80.0 + suspension_k1: 6.0 + suspension_k2: 3.0 + suspension_alpha1: 0.75 + suspension_alpha2: 0.7 + suspension_delta: 0.02 + suspension_u_min: -35.0 + suspension_u_max: 35.0 + suspension_output_min: -35.0 + suspension_output_max: 35.0 + + # Joint-local feedforward / limit shaping + torque_feedforward_gain: 0.0 + suspension_torque_feedforward_gain: -1.0 + +lb_joint_controller: + ros__parameters: + # Same joint-servo layout as lf_joint_controller + measurement_angle: /chassis/left_back_joint/physical_angle + measurement_velocity: /chassis/left_back_joint/physical_velocity + setpoint_angle: /chassis/left_back_joint/target_physical_angle + setpoint_velocity: /chassis/left_back_joint/target_physical_velocity + mode_input: /chassis/left_back_joint/suspension_mode + suspension_torque: /chassis/left_back_joint/suspension_torque + control: /chassis/left_back_joint/control_torque + dt: 0.001 + b0: -1.0 + kt: 1.0 + td_h: 0.001 + td_r: 50.0 + eso_w0: 250.0 + eso_auto_beta: true + k1: 30.0 + k2: 17.0 + alpha1: 0.75 + alpha2: 0.7 + delta: 0.02 + u_min: -200.0 + u_max: 200.0 + output_min: -200.0 + output_max: 200.0 + suspension_td_h: 0.001 + suspension_td_r: 12.0 + suspension_eso_w0: 80.0 + suspension_k1: 6.0 + suspension_k2: 3.0 + suspension_alpha1: 0.75 + suspension_alpha2: 0.7 + suspension_delta: 0.02 + suspension_u_min: -35.0 + suspension_u_max: 35.0 + suspension_output_min: -35.0 + suspension_output_max: 35.0 + torque_feedforward_gain: 0.0 + suspension_torque_feedforward_gain: -1.0 + +rb_joint_controller: + ros__parameters: + # Same joint-servo layout as lf_joint_controller + measurement_angle: /chassis/right_back_joint/physical_angle + measurement_velocity: /chassis/right_back_joint/physical_velocity + setpoint_angle: /chassis/right_back_joint/target_physical_angle + setpoint_velocity: /chassis/right_back_joint/target_physical_velocity + mode_input: /chassis/right_back_joint/suspension_mode + suspension_torque: /chassis/right_back_joint/suspension_torque + control: /chassis/right_back_joint/control_torque + dt: 0.001 + b0: -1.0 + kt: 1.0 + td_h: 0.001 + td_r: 50.0 + eso_w0: 250.0 + eso_auto_beta: true + k1: 30.0 + k2: 17.0 + alpha1: 0.75 + alpha2: 0.7 + delta: 0.02 + u_min: -200.0 + u_max: 200.0 + output_min: -200.0 + output_max: 200.0 + suspension_td_h: 0.001 + suspension_td_r: 12.0 + suspension_eso_w0: 80.0 + suspension_k1: 6.0 + suspension_k2: 3.0 + suspension_alpha1: 0.75 + suspension_alpha2: 0.7 + suspension_delta: 0.02 + suspension_u_min: -35.0 + suspension_u_max: 35.0 + suspension_output_min: -35.0 + suspension_output_max: 35.0 + torque_feedforward_gain: 0.0 + suspension_torque_feedforward_gain: -1.0 + +rf_joint_controller: + ros__parameters: + # Same joint-servo layout as lf_joint_controller + measurement_angle: /chassis/right_front_joint/physical_angle + measurement_velocity: /chassis/right_front_joint/physical_velocity + setpoint_angle: /chassis/right_front_joint/target_physical_angle + setpoint_velocity: /chassis/right_front_joint/target_physical_velocity + mode_input: /chassis/right_front_joint/suspension_mode + suspension_torque: /chassis/right_front_joint/suspension_torque + control: /chassis/right_front_joint/control_torque + dt: 0.001 + b0: -1.0 + kt: 1.0 + td_h: 0.001 + td_r: 50.0 + eso_w0: 250.0 + eso_auto_beta: true + k1: 30.0 + k2: 17.0 + alpha1: 0.75 + alpha2: 0.7 + delta: 0.02 + u_min: -200.0 + u_max: 200.0 + output_min: -200.0 + output_max: 200.0 + suspension_td_h: 0.001 + suspension_td_r: 12.0 + suspension_eso_w0: 80.0 + suspension_k1: 6.0 + suspension_k2: 3.0 + suspension_alpha1: 0.75 + suspension_alpha2: 0.7 + suspension_delta: 0.02 + suspension_u_min: -35.0 + suspension_u_max: 35.0 + suspension_output_min: -35.0 + suspension_output_max: 35.0 + torque_feedforward_gain: 0.0 + suspension_torque_feedforward_gain: -1.0 diff --git a/rmcs_ws/src/rmcs_bringup/config/deformable-infantry-omni.yaml b/rmcs_ws/src/rmcs_bringup/config/deformable-infantry-omni.yaml index ac97a8e3..dd76a55d 100644 --- a/rmcs_ws/src/rmcs_bringup/config/deformable-infantry-omni.yaml +++ b/rmcs_ws/src/rmcs_bringup/config/deformable-infantry-omni.yaml @@ -9,7 +9,7 @@ rmcs_executor: - rmcs_core::referee::command::Interaction -> referee_interaction - rmcs_core::referee::command::interaction::Ui -> referee_ui - - rmcs_core::referee::app::ui::Infantry -> referee_ui_infantry + - rmcs_core::referee::app::ui::DeformableInfantry -> referee_ui_infantry - rmcs_core::controller::gimbal::DeformableInfantryGimbalController -> gimbal_controller diff --git a/rmcs_ws/src/rmcs_bringup/config/deformable-infantry-steering.yaml b/rmcs_ws/src/rmcs_bringup/config/deformable-infantry-steering.yaml index b1ec953d..761d2265 100644 --- a/rmcs_ws/src/rmcs_bringup/config/deformable-infantry-steering.yaml +++ b/rmcs_ws/src/rmcs_bringup/config/deformable-infantry-steering.yaml @@ -34,22 +34,9 @@ rmcs_executor: value_broadcaster: ros__parameters: forward_list: - - /chassis/left_front_joint/torque - - /chassis/left_back_joint/torque - - /chassis/right_front_joint/torque - - /chassis/right_back_joint/torque - - /chassis/left_front_joint/suspension_mode - - /chassis/left_back_joint/suspension_mode - - /chassis/right_front_joint/suspension_mode - - /chassis/right_back_joint/suspension_mode - - /chassis/left_front_joint/suspension_torque - - /chassis/left_back_joint/suspension_torque - - /chassis/right_front_joint/suspension_torque - - /chassis/right_back_joint/suspension_torque - - /chassis/left_front_joint/control_torque - - /chassis/left_back_joint/control_torque - - /chassis/right_front_joint/control_torque - - /chassis/right_back_joint/control_torque + - /gimbal/yaw/angle + - /gimbal/yaw/velocity + deformable_infantry: ros__parameters: diff --git a/rmcs_ws/src/rmcs_core/plugins.xml b/rmcs_ws/src/rmcs_core/plugins.xml index f314d28f..1438f8a8 100644 --- a/rmcs_ws/src/rmcs_core/plugins.xml +++ b/rmcs_ws/src/rmcs_core/plugins.xml @@ -6,6 +6,7 @@ + diff --git a/rmcs_ws/src/rmcs_core/src/controller/chassis/deformable_chassis.cpp b/rmcs_ws/src/rmcs_core/src/controller/chassis/deformable_chassis.cpp index 0d3b36ca..96828c2c 100644 --- a/rmcs_ws/src/rmcs_core/src/controller/chassis/deformable_chassis.cpp +++ b/rmcs_ws/src/rmcs_core/src/controller/chassis/deformable_chassis.cpp @@ -255,6 +255,27 @@ class DeformableChassis right_front_joint_target_physical_acceleration_, nan_); register_output("/chassis/processed_encoder/angle", processed_encoder_angle_, nan_); + register_output( + "/chassis/left_front_joint/suspension_mode", left_front_joint_suspension_mode_, false); + register_output( + "/chassis/left_back_joint/suspension_mode", left_back_joint_suspension_mode_, false); + register_output( + "/chassis/right_front_joint/suspension_mode", right_front_joint_suspension_mode_, false); + register_output( + "/chassis/right_back_joint/suspension_mode", right_back_joint_suspension_mode_, false); + + register_output( + "/chassis/left_front_joint/suspension_torque", left_front_joint_suspension_torque_, + nan_); + register_output( + "/chassis/left_back_joint/suspension_torque", left_back_joint_suspension_torque_, nan_); + register_output( + "/chassis/right_back_joint/suspension_torque", right_back_joint_suspension_torque_, + nan_); + register_output( + "/chassis/right_front_joint/suspension_torque", right_front_joint_suspension_torque_, + nan_); + *mode_ = rmcs_msgs::ChassisMode::AUTO; chassis_control_velocity_->vector << nan_, nan_, nan_; @@ -381,6 +402,18 @@ class DeformableChassis return wrap_deg(joint_offset) - wrap_deg(*joint_encoder_angle) + legacy_fixed_compensation; } + void clear_suspension_output_interfaces_() { + *left_front_joint_suspension_mode_ = false; + *left_back_joint_suspension_mode_ = false; + *right_back_joint_suspension_mode_ = false; + *right_front_joint_suspension_mode_ = false; + + *left_front_joint_suspension_torque_ = 0.0; + *left_back_joint_suspension_torque_ = 0.0; + *right_back_joint_suspension_torque_ = 0.0; + *right_front_joint_suspension_torque_ = 0.0; + } + void update_mode_from_inputs_( rmcs_msgs::Switch switch_left, rmcs_msgs::Switch switch_right, const rmcs_msgs::Keyboard& keyboard) { @@ -580,6 +613,7 @@ class DeformableChassis } void update_active_suspension_(const JointFeedbackFrame&) { + clear_suspension_output_interfaces_(); if (!suspension_requested_by_input_()) { reset_attitude_correction_state_(); return; @@ -672,6 +706,8 @@ class DeformableChassis *right_front_joint_target_physical_acceleration_ = nan_; *processed_encoder_angle_ = nan_; + + clear_suspension_output_interfaces_(); } void update_velocity_control() { @@ -814,6 +850,12 @@ class DeformableChassis const bool prone_override = refresh_requested_joint_targets_from_deploy_state_(); scope_motor_control(prone_override); update_active_suspension_(joint_feedback); + + *left_front_joint_suspension_mode_ = joint_suspension_active_[kLeftFront]; + *left_back_joint_suspension_mode_ = joint_suspension_active_[kLeftBack]; + *right_front_joint_suspension_mode_ = joint_suspension_active_[kRightFront]; + *right_back_joint_suspension_mode_ = joint_suspension_active_[kRightBack]; + update_joint_target_trajectory(); publish_joint_target_angles(joint_feedback.physical_angles); } @@ -1014,6 +1056,7 @@ class DeformableChassis *rb_angle_error_ = nan_; *rf_angle_error_ = nan_; + clear_suspension_output_interfaces_(); } private: @@ -1097,6 +1140,15 @@ class DeformableChassis OutputInterface processed_encoder_angle_; + OutputInterface left_front_joint_suspension_mode_; + OutputInterface left_back_joint_suspension_mode_; + OutputInterface right_front_joint_suspension_mode_; + OutputInterface right_back_joint_suspension_mode_; + OutputInterface left_front_joint_suspension_torque_; + OutputInterface left_back_joint_suspension_torque_; + OutputInterface right_front_joint_suspension_torque_; + OutputInterface right_back_joint_suspension_torque_; + double min_angle_; double max_angle_; double left_front_joint_offset_; diff --git a/rmcs_ws/src/rmcs_core/src/hardware/deformable-infantry-omni-b.cpp b/rmcs_ws/src/rmcs_core/src/hardware/deformable-infantry-omni-b.cpp new file mode 100644 index 00000000..f07b1de3 --- /dev/null +++ b/rmcs_ws/src/rmcs_core/src/hardware/deformable-infantry-omni-b.cpp @@ -0,0 +1,777 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include + +#include + +#include "hardware/device/bmi088.hpp" +#include "hardware/device/can_packet.hpp" +#include "hardware/device/dji_motor.hpp" +#include "hardware/device/dr16.hpp" +#include "hardware/device/lk_motor.hpp" +#include "hardware/device/supercap.hpp" + +namespace rmcs_core::hardware { + +using Clock = std::chrono::steady_clock; + +class DeformableInfantryOmniB + : public rmcs_executor::Component + , public rclcpp::Node { +public: + DeformableInfantryOmniB() + : Node( + get_component_name(), + rclcpp::NodeOptions().automatically_declare_parameters_from_overrides(true)) + , deformable_infantry_command_( + create_partner_component( + get_component_name() + "_command", *this)) { + using namespace rmcs_description; + + register_input("/predefined/timestamp", timestamp_); + register_output("/tf", tf_); + + tf_->set_transform(Eigen::Translation3d{0.16, 0.0, 0.15}); + + // For command: remote-status + using Srv = std_srvs::srv::Trigger; + status_service_ = create_service( + "/rmcs/service/robot_status", + [this](const Srv::Request::SharedPtr&, const Srv::Response::SharedPtr& response) { + status_service_callback(response); + }); + + rmcs_board_lite = std::make_unique( + *this, *deformable_infantry_command_, + get_parameter("serial_filter_rmcs_board").as_string()); + top_board_ = std::make_unique( + *this, *deformable_infantry_command_, + get_parameter("serial_filter_top_board").as_string()); + } + + ~DeformableInfantryOmniB() override = default; + + void before_updating() override { + top_board_->request_hard_sync_read(); + next_hard_sync_log_time_ = Clock::now() + std::chrono::seconds(1); + } + + void update() override { + rmcs_board_lite->update(); + top_board_->update(); + } + + void command_update() { + const bool even = ((cmd_tick_++ & 1u) == 0u); + rmcs_board_lite->command_update(even); + top_board_->command_update(); + } + +private: + class DeformableInfantryOmniBCommand; + class BottomBoard; + class TopBoard; + + class DeformableInfantryOmniBCommand : public rmcs_executor::Component { + public: + explicit DeformableInfantryOmniBCommand(DeformableInfantryOmniB& deformableInfantry) + : deformableInfantry(deformableInfantry) {} + + void update() override { deformableInfantry.command_update(); } + + DeformableInfantryOmniB& deformableInfantry; + }; + + class BottomBoard final : private librmcs::agent::RmcsBoardLite { + public: + friend class DeformableInfantryOmniB; + + static constexpr double nan_ = std::numeric_limits::quiet_NaN(); + + explicit BottomBoard( + DeformableInfantryOmniB& deformableInfantry, + DeformableInfantryOmniBCommand& deformableInfantry_command, + const std::string& serial_filter = {}) + : RmcsBoardLite{ + serial_filter, + librmcs::agent::AdvancedOptions{.dangerously_skip_version_checks = true}} + , deformable_infantry_{deformableInfantry} + , command_{deformableInfantry_command} + , tf_{deformableInfantry.tf_} { + + deformableInfantry.register_output("/referee/serial", referee_serial_); + referee_serial_->read = [this](std::byte* buffer, size_t size) { + return referee_ring_buffer_receive_.pop_front_n( + [&buffer](std::byte byte) noexcept { *buffer++ = byte; }, size); + }; + referee_serial_->write = [this](const std::byte* buffer, size_t size) { + start_transmit().uart0_transmit( + {.uart_data = std::span{buffer, size}}); + return size; + }; + + gimbal_yaw_motor_.configure( + device::LkMotor::Config{device::LkMotor::Type::kMG4010Ei10}.set_encoder_zero_point( + static_cast( + deformableInfantry.get_parameter("yaw_motor_zero_point").as_int()))); + + for (auto& motor : chassis_wheel_motors_) + motor.configure( + device::DjiMotor::Config{device::DjiMotor::Type::kM3508} + .set_reduction_ratio(13.0) + .enable_multi_turn_angle() + .set_reversed()); + + // V2: LK MG5010 i36 direct-drive joint motors, built-in encoder zero point + for (auto& motor : chassis_joint_motors_) + motor.configure( + device::LkMotor::Config{device::LkMotor::Type::kMG5010Ei36} + .set_reversed() + .enable_multi_turn_angle()); + + imu_.set_coordinate_mapping([](double x, double y, double z) { + // Keep the existing chassis yaw axis mapping explicit until the bottom-board IMU + // installation is re-validated on hardware. + return std::make_tuple(-y, x, z); + }); + + gimbal_bullet_feeder_.configure( + device::DjiMotor::Config{device::DjiMotor::Type::kM2006}.enable_multi_turn_angle()); + + deformableInfantry.register_output( + "/chassis/yaw/velocity_imu", chassis_yaw_velocity_imu_, 0); + deformableInfantry.register_output("/chassis/imu/pitch", chassis_imu_pitch_, 0.0); + deformableInfantry.register_output("/chassis/imu/roll", chassis_imu_roll_, 0.0); + deformableInfantry.register_output( + "/chassis/imu/pitch_rate", chassis_imu_pitch_rate_, 0.0); + deformableInfantry.register_output( + "/chassis/imu/roll_rate", chassis_imu_roll_rate_, 0.0); + deformableInfantry.register_output( + "/chassis/left_front_joint/physical_angle", left_front_joint_physical_angle_, nan_); + deformableInfantry.register_output( + "/chassis/left_back_joint/physical_angle", left_back_joint_physical_angle_, nan_); + deformableInfantry.register_output( + "/chassis/right_back_joint/physical_angle", right_back_joint_physical_angle_, nan_); + deformableInfantry.register_output( + "/chassis/right_front_joint/physical_angle", right_front_joint_physical_angle_, + nan_); + deformableInfantry.register_output( + "/chassis/left_front_joint/physical_velocity", left_front_joint_physical_velocity_, + nan_); + deformableInfantry.register_output( + "/chassis/left_back_joint/physical_velocity", left_back_joint_physical_velocity_, + nan_); + deformableInfantry.register_output( + "/chassis/right_back_joint/physical_velocity", right_back_joint_physical_velocity_, + nan_); + deformableInfantry.register_output( + "/chassis/right_front_joint/physical_velocity", + right_front_joint_physical_velocity_, nan_); + deformableInfantry.register_output("/chassis/encoder/alpha", encoder_alpha_, nan_); + deformableInfantry.register_output( + "/chassis/encoder/alpha_dot", encoder_alpha_dot_, nan_); + deformableInfantry.register_output("/chassis/radius", radius_, nan_); + + deformableInfantry.get_parameter_or("debug_log_supercap", debug_log_supercap_, false); + deformableInfantry.get_parameter_or( + "debug_log_wheel_motor", debug_log_wheel_motor_, false); + deformableInfantry.get_parameter_or( + "debug_log_deformable_joint_motor", debug_log_deformable_joint_motor_, false); + } + + ~BottomBoard() override = default; + + void update() { + imu_.update_status(); + *chassis_yaw_velocity_imu_ = imu_.gz(); + { + const double q0 = imu_.q0(); + const double q1 = imu_.q1(); + const double q2 = imu_.q2(); + const double q3 = imu_.q3(); + + double sin_pitch = 2.0 * (q0 * q2 - q3 * q1); + sin_pitch = std::clamp(sin_pitch, -1.0, 1.0); + + const double standard_pitch = std::asin(sin_pitch); + const double standard_roll = + std::atan2(2.0 * (q0 * q1 + q2 * q3), 1.0 - 2.0 * (q1 * q1 + q2 * q2)); + + // Export chassis attitude using the requested convention: + // pitch < 0 when the front is higher, roll > 0 when the left side is higher. + *chassis_imu_pitch_ = -standard_pitch; + *chassis_imu_roll_ = standard_roll; + *chassis_imu_pitch_rate_ = -imu_.gy(); + *chassis_imu_roll_rate_ = imu_.gx(); + } + + for (auto& motor : chassis_wheel_motors_) + motor.update_status(); + for (auto& motor : chassis_joint_motors_) + motor.update_status(); + + update_joint_physical_feedback_( + 0, left_front_joint_physical_angle_, left_front_joint_physical_velocity_); + update_joint_physical_feedback_( + 1, left_back_joint_physical_angle_, left_back_joint_physical_velocity_); + update_joint_physical_feedback_( + 2, right_back_joint_physical_angle_, right_back_joint_physical_velocity_); + update_joint_physical_feedback_( + 3, right_front_joint_physical_angle_, right_front_joint_physical_velocity_); + + update_geometry_feedback_(); + if (debug_log_wheel_motor_ || debug_log_deformable_joint_motor_) + log_chassis_feedback_once_per_second_(); + + dr16_.update_status(); + gimbal_yaw_motor_.update_status(); + if (supercap_status_received_.load(std::memory_order_relaxed)) + supercap_.update_status(); + if (debug_log_supercap_) + log_supercap_feedback_once_per_second_(); + gimbal_bullet_feeder_.update_status(); + + tf_->set_state( + gimbal_yaw_motor_.angle()); + } + + void command_update(bool even) { + auto builder = start_transmit(); + if (even) { + builder.can0_transmit({ + .can_id = 0x200, + .can_data = + device::CanPacket8{ + chassis_wheel_motors_[0].generate_command(), + device::CanPacket8::PaddingQuarter{}, + device::CanPacket8::PaddingQuarter{}, + device::CanPacket8::PaddingQuarter{}, + } + .as_bytes(), + }); + builder.can1_transmit({ + .can_id = 0x200, + .can_data = + device::CanPacket8{ + chassis_wheel_motors_[1].generate_command(), + device::CanPacket8::PaddingQuarter{}, + supercap_.generate_command(), + device::CanPacket8::PaddingQuarter{}, + } + .as_bytes(), + }); + builder.can2_transmit({ + .can_id = 0x200, + .can_data = + device::CanPacket8{ + chassis_wheel_motors_[2].generate_command(), + device::CanPacket8::PaddingQuarter{}, + gimbal_bullet_feeder_.generate_command(), + device::CanPacket8::PaddingQuarter{}, + } + .as_bytes(), + }); + builder.can3_transmit({ + .can_id = 0x200, + .can_data = + device::CanPacket8{ + chassis_wheel_motors_[3].generate_command(), + device::CanPacket8::PaddingQuarter{}, + device::CanPacket8::PaddingQuarter{}, + device::CanPacket8::PaddingQuarter{}, + } + .as_bytes(), + }); + builder.can2_transmit({ + .can_id = 0x142, + .can_data = gimbal_yaw_motor_.generate_torque_command().as_bytes(), + }); + } else { + builder.can0_transmit({ + .can_id = 0x141, + .can_data = chassis_joint_motors_[0].generate_command().as_bytes(), + }); + builder.can1_transmit({ + .can_id = 0x141, + .can_data = chassis_joint_motors_[1].generate_command().as_bytes(), + }); + builder.can2_transmit({ + .can_id = 0x141, + .can_data = chassis_joint_motors_[2].generate_command().as_bytes(), + }); + builder.can3_transmit({ + .can_id = 0x141, + .can_data = chassis_joint_motors_[3].generate_command().as_bytes(), + }); + } + } + + private: + DeformableInfantryOmniB& deformable_infantry_; + rmcs_executor::Component& command_; + + static constexpr double joint_zero_physical_angle_rad_ = 62.5 * std::numbers::pi / 180.0; + static constexpr double chassis_radius_base_ = 0.2341741; + static constexpr double rod_length_ = 0.150; + + static double to_physical_angle_(double motor_angle) { + return joint_zero_physical_angle_rad_ - motor_angle; + } + + static double to_physical_velocity_(double motor_velocity) { return -motor_velocity; } + + void update_joint_physical_feedback_( + size_t index, OutputInterface& angle_output, + OutputInterface& velocity_output) { + if (!joint_status_received_[index].load(std::memory_order_relaxed)) { + *angle_output = nan_; + *velocity_output = nan_; + return; + } + + *angle_output = to_physical_angle_(chassis_joint_motors_[index].angle()); + *velocity_output = to_physical_velocity_(chassis_joint_motors_[index].velocity()); + } + + void update_geometry_feedback_() { + const Eigen::Vector4d alpha_rad{ + *left_front_joint_physical_angle_, *left_back_joint_physical_angle_, + *right_back_joint_physical_angle_, *right_front_joint_physical_angle_}; + const Eigen::Vector4d alpha_dot_rad{ + *left_front_joint_physical_velocity_, *left_back_joint_physical_velocity_, + *right_back_joint_physical_velocity_, *right_front_joint_physical_velocity_}; + + if (!alpha_rad.array().isFinite().all() || !alpha_dot_rad.array().isFinite().all()) { + *encoder_alpha_ = nan_; + *encoder_alpha_dot_ = nan_; + *radius_ = nan_; + return; + } + + *encoder_alpha_ = alpha_rad.mean(); + *encoder_alpha_dot_ = alpha_dot_rad.mean(); + *radius_ = (chassis_radius_base_ + rod_length_ * alpha_rad.array().cos()).mean(); + } + + void log_chassis_feedback_once_per_second_() { + const auto now = Clock::now(); + if (now < next_chassis_feedback_log_time_) + return; + + const auto wheel_rx = [this](size_t index) { + return wheel_status_received_[index].load(std::memory_order_relaxed) ? 'Y' : 'N'; + }; + const auto joint_rx = [this](size_t index) { + return joint_status_received_[index].load(std::memory_order_relaxed) ? 'Y' : 'N'; + }; + + if (debug_log_wheel_motor_) { + RCLCPP_INFO( + deformable_infantry_.get_logger(), + "[wheel motor] angle(rad) lf=% .3f lb=% .3f rb=% .3f rf=% .3f | " + "encoder(deg) lf=% .1f lb=% .1f rb=% .1f rf=% .1f | " + "rx=[%c %c %c %c]", + chassis_wheel_motors_[0].angle(), chassis_wheel_motors_[1].angle(), + chassis_wheel_motors_[2].angle(), chassis_wheel_motors_[3].angle(), + chassis_wheel_motors_[0].angle(), chassis_wheel_motors_[1].angle(), + chassis_wheel_motors_[2].angle(), chassis_wheel_motors_[3].angle(), wheel_rx(0), + wheel_rx(1), wheel_rx(2), wheel_rx(3)); + } + + if (debug_log_deformable_joint_motor_) { + RCLCPP_INFO( + deformable_infantry_.get_logger(), + "[deformable joint motor] angle(rad) lf=% .3f lb=% .3f rb=% .3f rf=% .3f | " + "velocity(rad/s) lf=% .3f lb=% .3f rb=% .3f rf=% .3f | " + "rx=[%c %c %c %c]", + *left_front_joint_physical_angle_, *left_back_joint_physical_angle_, + *right_back_joint_physical_angle_, *right_front_joint_physical_angle_, + *left_front_joint_physical_velocity_, *left_back_joint_physical_velocity_, + *right_back_joint_physical_velocity_, *right_front_joint_physical_velocity_, + joint_rx(0), joint_rx(1), joint_rx(2), joint_rx(3)); + } + + next_chassis_feedback_log_time_ = now + std::chrono::seconds(1); + } + + void log_supercap_feedback_once_per_second_() { + const auto now = Clock::now(); + if (now < next_supercap_feedback_log_time_) + return; + + const bool supercap_rx = supercap_status_received_.load(std::memory_order_relaxed); + auto supercap_raw_packet = latest_supercap_status_.load(std::memory_order_relaxed); + const auto supercap_raw_bytes = supercap_raw_packet.as_bytes(); + + RCLCPP_INFO( + deformable_infantry_.get_logger(), + "[supercap] can1 rx=%c id=0x300 enabled=%d supercap_v=% .3f chassis_v=% .3f " + "power=% .3f raw=[%02X %02X %02X %02X %02X %02X %02X %02X]", + supercap_rx ? 'Y' : 'N', + supercap_rx ? (supercap_.supercap_enabled() ? 1 : 0) : -1, + supercap_rx ? supercap_.supercap_voltage() : nan_, + supercap_rx ? supercap_.chassis_voltage() : nan_, + supercap_rx ? supercap_.chassis_power() : nan_, + std::to_integer(supercap_raw_bytes[0]), + std::to_integer(supercap_raw_bytes[1]), + std::to_integer(supercap_raw_bytes[2]), + std::to_integer(supercap_raw_bytes[3]), + std::to_integer(supercap_raw_bytes[4]), + std::to_integer(supercap_raw_bytes[5]), + std::to_integer(supercap_raw_bytes[6]), + std::to_integer(supercap_raw_bytes[7])); + + next_supercap_feedback_log_time_ = now + std::chrono::seconds(1); + } + + void dbus_receive_callback(const librmcs::data::UartDataView& data) override { + dr16_.store_status(data.uart_data.data(), data.uart_data.size()); + } + + void can0_receive_callback(const librmcs::data::CanDataView& data) override { + if (data.is_extended_can_id || data.is_remote_transmission) + return; + if (data.can_id == 0x201) { + chassis_wheel_motors_[0].store_status(data.can_data); + wheel_status_received_[0].store(true, std::memory_order_relaxed); + } else if (data.can_id == 0x141) { + chassis_joint_motors_[0].store_status(data.can_data); + joint_status_received_[0].store(true, std::memory_order_relaxed); + } + } + + void can1_receive_callback(const librmcs::data::CanDataView& data) override { + if (data.is_extended_can_id || data.is_remote_transmission) + return; + if (data.can_id == 0x201) { + chassis_wheel_motors_[1].store_status(data.can_data); + wheel_status_received_[1].store(true, std::memory_order_relaxed); + } else if (data.can_id == 0x141) { + chassis_joint_motors_[1].store_status(data.can_data); + joint_status_received_[1].store(true, std::memory_order_relaxed); + } else if (data.can_id == 0x300) { + if (data.can_data.size() == 8) + latest_supercap_status_.store( + device::CanPacket8{data.can_data}, std::memory_order_relaxed); + supercap_.store_status(data.can_data); + supercap_status_received_.store(true, std::memory_order_relaxed); + } + } + + void can2_receive_callback(const librmcs::data::CanDataView& data) override { + if (data.is_extended_can_id || data.is_remote_transmission) + return; + if (data.can_id == 0x201) { + chassis_wheel_motors_[2].store_status(data.can_data); + wheel_status_received_[2].store(true, std::memory_order_relaxed); + } else if (data.can_id == 0x141) { + chassis_joint_motors_[2].store_status(data.can_data); + joint_status_received_[2].store(true, std::memory_order_relaxed); + } else if (data.can_id == 0x142) { + gimbal_yaw_motor_.store_status(data.can_data); + } else if (data.can_id == 0x203) { + gimbal_bullet_feeder_.store_status(data.can_data); + } + } + + void can3_receive_callback(const librmcs::data::CanDataView& data) override { + if (data.is_extended_can_id || data.is_remote_transmission) + return; + if (data.can_id == 0x201) { + chassis_wheel_motors_[3].store_status(data.can_data); + wheel_status_received_[3].store(true, std::memory_order_relaxed); + } else if (data.can_id == 0x141) { + chassis_joint_motors_[3].store_status(data.can_data); + joint_status_received_[3].store(true, std::memory_order_relaxed); + } + } + + void uart0_receive_callback(const librmcs::data::UartDataView& data) override { + const std::byte* ptr = data.uart_data.data(); + referee_ring_buffer_receive_.emplace_back_n( + [&ptr](std::byte* storage) noexcept { *storage = *ptr++; }, data.uart_data.size()); + } + + void accelerometer_receive_callback( + const librmcs::data::AccelerometerDataView& data) override { + imu_.store_accelerometer_status(data.x, data.y, data.z); + } + + void gyroscope_receive_callback(const librmcs::data::GyroscopeDataView& data) override { + imu_.store_gyroscope_status(data.x, data.y, data.z); + } + + OutputInterface& tf_; + + device::Bmi088 imu_{1000, 0.2, 0.0}; + device::LkMotor gimbal_yaw_motor_{ + deformable_infantry_, command_, "/gimbal/yaw"}; + device::Dr16 dr16_{deformable_infantry_}; + + device::DjiMotor chassis_wheel_motors_[4]{ + device::DjiMotor{ + deformable_infantry_, command_, "/chassis/left_front_wheel"}, + device::DjiMotor{ + deformable_infantry_, command_, "/chassis/left_back_wheel"}, + device::DjiMotor{ + deformable_infantry_, command_, "/chassis/right_back_wheel"}, + device::DjiMotor{ + deformable_infantry_, command_, "/chassis/right_front_wheel"}, + }; + device::LkMotor chassis_joint_motors_[4]{ + device::LkMotor{ + deformable_infantry_, command_, "/chassis/left_front_joint"}, + device::LkMotor{ + deformable_infantry_, command_, "/chassis/left_back_joint"}, + device::LkMotor{ + deformable_infantry_, command_, "/chassis/right_back_joint"}, + device::LkMotor{ + deformable_infantry_, command_, "/chassis/right_front_joint"}, + }; + + std::atomic wheel_status_received_[4] = {false, false, false, false}; + std::atomic joint_status_received_[4] = {false, false, false, false}; + bool debug_log_supercap_ = false; + bool debug_log_wheel_motor_ = false; + bool debug_log_deformable_joint_motor_ = false; + Clock::time_point next_chassis_feedback_log_time_{ + Clock::now() + std::chrono::seconds(1)}; + Clock::time_point next_supercap_feedback_log_time_{ + Clock::now() + std::chrono::seconds(1)}; + device::Supercap supercap_{deformable_infantry_, command_}; + std::atomic latest_supercap_status_{ + device::CanPacket8{uint64_t{0}}}; + std::atomic supercap_status_received_{false}; + device::DjiMotor gimbal_bullet_feeder_{ + deformable_infantry_, command_, "/gimbal/bullet_feeder"}; + + rmcs_utility::RingBuffer referee_ring_buffer_receive_{256}; + OutputInterface referee_serial_; + + OutputInterface chassis_yaw_velocity_imu_; + OutputInterface chassis_imu_pitch_; + OutputInterface chassis_imu_roll_; + OutputInterface chassis_imu_pitch_rate_; + OutputInterface chassis_imu_roll_rate_; + OutputInterface left_front_joint_physical_angle_; + OutputInterface left_back_joint_physical_angle_; + OutputInterface right_back_joint_physical_angle_; + OutputInterface right_front_joint_physical_angle_; + OutputInterface left_front_joint_physical_velocity_; + OutputInterface left_back_joint_physical_velocity_; + OutputInterface right_back_joint_physical_velocity_; + OutputInterface right_front_joint_physical_velocity_; + OutputInterface encoder_alpha_; + OutputInterface encoder_alpha_dot_; + OutputInterface radius_; + }; + + class TopBoard final : private librmcs::agent::RmcsBoardLite { + public: + friend class DeformableInfantryOmniB; + + explicit TopBoard( + DeformableInfantryOmniB& deformableInfantry, + DeformableInfantryOmniBCommand& deformableInfantry_command, + std::string serial_filter = {}) + : librmcs::agent::RmcsBoardLite( + serial_filter, + librmcs::agent::AdvancedOptions{.dangerously_skip_version_checks = true}) + , hard_sync_pending_(deformableInfantry.hard_sync_pending_) + , tf_(deformableInfantry.tf_) + , bmi088_(1000, 0.2, 0.0) + , gimbal_pitch_motor_(deformableInfantry, deformableInfantry_command, "/gimbal/pitch") + , gimbal_left_friction_( + deformableInfantry, deformableInfantry_command, "/gimbal/left_friction") + , gimbal_right_friction_( + deformableInfantry, deformableInfantry_command, "/gimbal/right_friction") + , scope_motor_(deformableInfantry, deformableInfantry_command, "/gimbal/scope") { + + gimbal_pitch_motor_.configure( + device::LkMotor::Config{device::LkMotor::Type::kMG4010Ei10} + .set_reversed() + .set_encoder_zero_point( + static_cast( + deformableInfantry.get_parameter("pitch_motor_zero_point").as_int()))); + + gimbal_left_friction_.configure( + device::DjiMotor::Config{device::DjiMotor::Type::kM3508} + .set_reduction_ratio(1.) + .set_reversed()); + gimbal_right_friction_.configure( + device::DjiMotor::Config{device::DjiMotor::Type::kM3508}.set_reduction_ratio(1.)); + + scope_motor_.configure( + device::DjiMotor::Config{device::DjiMotor::Type::kM2006}.enable_multi_turn_angle()); + + deformableInfantry.register_output( + "/gimbal/yaw/velocity_imu", gimbal_yaw_velocity_bmi088_); + deformableInfantry.register_output( + "/gimbal/pitch/velocity_imu", gimbal_pitch_velocity_encoder_); + + bmi088_.set_coordinate_mapping([](double x, double y, double z) { + // Top board BMI088 maps to gimbal frame as (-x, -y, z). + return std::make_tuple(y, -x, z); + }); + } + + ~TopBoard() override = default; + + void request_hard_sync_read() { + // RMCS-lite top board variant currently has no GPIO hard-sync request + // path. + } + + void update() { + bmi088_.update_status(); + + gimbal_pitch_motor_.update_status(); + gimbal_left_friction_.update_status(); + gimbal_right_friction_.update_status(); + scope_motor_.update_status(); + + const double pitch_encoder_angle = gimbal_pitch_motor_.angle(); + Eigen::Quaterniond const odom_imu_to_yaw_link{ + bmi088_.q0(), bmi088_.q1(), bmi088_.q2(), bmi088_.q3()}; + Eigen::Quaterniond const yaw_link_to_odom_imu = odom_imu_to_yaw_link.conjugate(); + Eigen::Quaterniond pitch_link_to_odom_imu = + Eigen::Quaterniond{ + Eigen::AngleAxisd{-pitch_encoder_angle, Eigen::Vector3d::UnitY()}} + * yaw_link_to_odom_imu; + pitch_link_to_odom_imu.normalize(); + + *gimbal_yaw_velocity_bmi088_ = bmi088_.gz(); + *gimbal_pitch_velocity_encoder_ = gimbal_pitch_motor_.velocity(); + // The BMI088 is mounted on the yaw link. fast_tf stores PitchLink -> + // OdomImu, so use the encoder pitch from the TF tree to move the + // yaw-link pose back into PitchLink. + tf_->set_transform( + pitch_link_to_odom_imu); + + tf_->set_state( + pitch_encoder_angle); + } + + void command_update() { + auto builder = start_transmit(); + builder.can0_transmit({ + .can_id = 0x141, + .can_data = gimbal_pitch_motor_.generate_command().as_bytes(), + }); + + builder.can1_transmit({ + .can_id = 0x200, + .can_data = + device::CanPacket8{ + gimbal_left_friction_.generate_command(), + gimbal_right_friction_.generate_command(), + scope_motor_.generate_command(), + device::CanPacket8::PaddingQuarter{}, + } + .as_bytes(), + }); + } + + private: + void uart1_receive_callback(const librmcs::data::UartDataView&) override {} + + void can0_receive_callback(const librmcs::data::CanDataView& data) override { + if (data.is_extended_can_id || data.is_remote_transmission) [[unlikely]] + return; + if (data.can_id == 0x141) + gimbal_pitch_motor_.store_status(data.can_data); + } + + void can1_receive_callback(const librmcs::data::CanDataView& data) override { + if (data.is_extended_can_id || data.is_remote_transmission) [[unlikely]] + return; + if (data.can_id == 0x201) + gimbal_left_friction_.store_status(data.can_data); + else if (data.can_id == 0x202) + gimbal_right_friction_.store_status(data.can_data); + else if (data.can_id == 0x203) + scope_motor_.store_status(data.can_data); + } + + void accelerometer_receive_callback( + const librmcs::data::AccelerometerDataView& data) override { + bmi088_.store_accelerometer_status(data.x, data.y, data.z); + } + + void gyroscope_receive_callback(const librmcs::data::GyroscopeDataView& data) override { + bmi088_.store_gyroscope_status(data.x, data.y, data.z); + } + + std::atomic& hard_sync_pending_; + OutputInterface& tf_; + + OutputInterface gimbal_yaw_velocity_bmi088_; + OutputInterface gimbal_pitch_velocity_encoder_; + + device::Bmi088 bmi088_; + device::LkMotor gimbal_pitch_motor_; + device::DjiMotor gimbal_left_friction_; + device::DjiMotor gimbal_right_friction_; + device::DjiMotor scope_motor_; + }; + + auto status_service_callback(const std::shared_ptr& response) + -> void { + response->success = true; + + auto feedback_message = std::ostringstream{}; + auto text = [&](std::format_string format, Args&&... args) { + std::println(feedback_message, format, std::forward(args)...); + }; + + text("Gimbal Status"); + text("- Yaw: {}", rmcs_board_lite->gimbal_yaw_motor_.last_raw_angle()); + text("- Pitch: {}", top_board_->gimbal_pitch_motor_.last_raw_angle()); + + text("Chassis Status"); + text("- left front: {}", rmcs_board_lite->chassis_joint_motors_[0].last_raw_angle()); + text("- left back: {}", rmcs_board_lite->chassis_joint_motors_[1].last_raw_angle()); + text("- right back: {}", rmcs_board_lite->chassis_joint_motors_[2].last_raw_angle()); + text("- right front: {}", rmcs_board_lite->chassis_joint_motors_[3].last_raw_angle()); + + response->message = feedback_message.str(); + } + + OutputInterface tf_; + InputInterface timestamp_; + std::atomic hard_sync_pending_{false}; + size_t hard_sync_snapshot_count_ = 0; + Clock::time_point next_hard_sync_log_time_{}; + + std::shared_ptr deformable_infantry_command_; + std::unique_ptr rmcs_board_lite; + std::unique_ptr top_board_; + + std::shared_ptr> status_service_; + uint32_t cmd_tick_ = 0; +}; + +} // namespace rmcs_core::hardware + +#include +PLUGINLIB_EXPORT_CLASS(rmcs_core::hardware::DeformableInfantryOmniB, rmcs_executor::Component) diff --git a/rmcs_ws/src/rmcs_core/src/hardware/deformable-infantry-omni.cpp b/rmcs_ws/src/rmcs_core/src/hardware/deformable-infantry-omni.cpp index 3b656dd6..6a80eff4 100644 --- a/rmcs_ws/src/rmcs_core/src/hardware/deformable-infantry-omni.cpp +++ b/rmcs_ws/src/rmcs_core/src/hardware/deformable-infantry-omni.cpp @@ -468,7 +468,8 @@ class DeformableInfantryOmni status_.get_logger(), "[supercap] can1 rx=%c id=0x300 enabled=%d supercap_v=% .3f chassis_v=% .3f " "power=% .3f raw=[%02X %02X %02X %02X %02X %02X %02X %02X]", - supercap_rx ? 'Y' : 'N', supercap_rx ? (supercap_.supercap_enabled() ? 1 : 0) : -1, + supercap_rx ? 'Y' : 'N', + supercap_rx ? (supercap_.supercap_enabled() ? 1 : 0) : -1, supercap_rx ? supercap_.supercap_voltage() : kNaN, supercap_rx ? supercap_.chassis_voltage() : kNaN, supercap_rx ? supercap_.chassis_power() : kNaN, diff --git a/rmcs_ws/src/rmcs_core/src/hardware/deformable-infantry-steering.cpp b/rmcs_ws/src/rmcs_core/src/hardware/deformable-infantry-steering.cpp index 6514c58b..5a833272 100644 --- a/rmcs_ws/src/rmcs_core/src/hardware/deformable-infantry-steering.cpp +++ b/rmcs_ws/src/rmcs_core/src/hardware/deformable-infantry-steering.cpp @@ -20,6 +20,7 @@ #include #include #include +#include #include @@ -52,19 +53,12 @@ class DeformableInfantryV2 tf_->set_transform(Eigen::Translation3d{0.16, 0.0, 0.15}); - steers_calibrate_subscription_ = create_subscription( - "/steers/calibrate", rclcpp::QoS(1), [this](std_msgs::msg::Int32::UniquePtr msg) { - steers_calibrate_subscription_callback(std::move(msg)); - }); - - joints_calibrate_subscription_ = create_subscription( - "/joints/calibrate", rclcpp::QoS(1), [this](std_msgs::msg::Int32::UniquePtr msg) { - joints_calibrate_subscription_callback(std::move(msg)); - }); - - gimbal_calibrate_subscription_ = create_subscription( - "/gimbal/calibrate", rclcpp::QoS{0}, [this](std_msgs::msg::Int32::UniquePtr&& msg) { - gimbal_calibrate_subscription_callback(std::move(msg)); + // For command: remote-status + using Srv = std_srvs::srv::Trigger; + status_service_ = create_service( + "/rmcs/service/robot_status", + [this](const Srv::Request::SharedPtr&, const Srv::Response::SharedPtr& response) { + status_service_callback(response); }); rmcs_board_lite = std::make_unique( @@ -98,54 +92,6 @@ class DeformableInfantryV2 class BottomBoard; class TopBoard; - void steers_calibrate_subscription_callback(std_msgs::msg::Int32::UniquePtr) { - if (!rmcs_board_lite) - return; - - RCLCPP_INFO( - get_logger(), "New left front offset: %d", - rmcs_board_lite->chassis_steer_motors_[0].calibrate_zero_point()); - RCLCPP_INFO( - get_logger(), "New left back offset: %d", - rmcs_board_lite->chassis_steer_motors_[1].calibrate_zero_point()); - RCLCPP_INFO( - get_logger(), "New right back offset: %d", - rmcs_board_lite->chassis_steer_motors_[2].calibrate_zero_point()); - RCLCPP_INFO( - get_logger(), "New right front offset: %d", - rmcs_board_lite->chassis_steer_motors_[3].calibrate_zero_point()); - } - - void joints_calibrate_subscription_callback(std_msgs::msg::Int32::UniquePtr) { - if (!rmcs_board_lite) - return; - - RCLCPP_INFO( - get_logger(), "New left front offset: %ld", - rmcs_board_lite->chassis_joint_motors_[0].calibrate_zero_point()); - RCLCPP_INFO( - get_logger(), "New left back offset: %ld", - rmcs_board_lite->chassis_joint_motors_[1].calibrate_zero_point()); - RCLCPP_INFO( - get_logger(), "New right back offset: %ld", - rmcs_board_lite->chassis_joint_motors_[2].calibrate_zero_point()); - RCLCPP_INFO( - get_logger(), "New right front offset: %ld", - rmcs_board_lite->chassis_joint_motors_[3].calibrate_zero_point()); - } - - void gimbal_calibrate_subscription_callback(std_msgs::msg::Int32::UniquePtr) { - if (!rmcs_board_lite || !top_board_) - return; - - RCLCPP_INFO( - get_logger(), "[gimbal calibration] New yaw offset: %ld", - rmcs_board_lite->gimbal_yaw_motor_.calibrate_zero_point()); - RCLCPP_INFO( - get_logger(), "[gimbal calibration] New pitch offset: %ld", - top_board_->gimbal_pitch_motor_.calibrate_zero_point()); - } - class DeformableInfantryV2Command : public rmcs_executor::Component { public: explicit DeformableInfantryV2Command(DeformableInfantryV2& deformableInfantry) @@ -164,23 +110,14 @@ class DeformableInfantryV2 explicit BottomBoard( DeformableInfantryV2& deformableInfantry, - DeformableInfantryV2Command& deformableInfantry_command, std::string serial_filter = {}) - : librmcs::agent::RmcsBoardLite( + DeformableInfantryV2Command& deformableInfantry_command, + const std::string& serial_filter = {}) + : RmcsBoardLite{ serial_filter, - librmcs::agent::AdvancedOptions{.dangerously_skip_version_checks = true}) - , deformable_infantry_(deformableInfantry) - , tf_(deformableInfantry.tf_) - , imu_(1000, 0.2, 0.0) - , gimbal_yaw_motor_(deformableInfantry, deformableInfantry_command, "/gimbal/yaw") - , dr16_(deformableInfantry) - , chassis_wheel_motors_{device::DjiMotor{deformableInfantry, deformableInfantry_command, "/chassis/left_front_wheel"}, device::DjiMotor{deformableInfantry, deformableInfantry_command, "/chassis/left_back_wheel"}, device::DjiMotor{deformableInfantry, deformableInfantry_command, "/chassis/right_back_wheel"}, device::DjiMotor{deformableInfantry, deformableInfantry_command, "/chassis/right_front_wheel"},} - , chassis_steer_motors_{device::DjiMotor{deformableInfantry, deformableInfantry_command, "/chassis/left_front_steering"}, device::DjiMotor{deformableInfantry, deformableInfantry_command, "/chassis/left_back_steering"}, device::DjiMotor{deformableInfantry, deformableInfantry_command, "/chassis/right_back_steering"}, device::DjiMotor{deformableInfantry, deformableInfantry_command, "/chassis/right_front_steering"}} - , chassis_joint_motors_{device::LkMotor{deformableInfantry, deformableInfantry_command, "/chassis/left_front_joint"}, device::LkMotor{deformableInfantry, deformableInfantry_command, "/chassis/left_back_joint"}, device::LkMotor{deformableInfantry, deformableInfantry_command, "/chassis/right_back_joint"}, device::LkMotor{deformableInfantry, deformableInfantry_command, "/chassis/right_front_joint"}} - , next_chassis_feedback_log_time_(Clock::now() + std::chrono::seconds(1)) - , next_supercap_feedback_log_time_(Clock::now() + std::chrono::seconds(1)) - , supercap_(deformableInfantry, deformableInfantry_command) - , gimbal_bullet_feeder_( - deformableInfantry, deformableInfantry_command, "/gimbal/bullet_feeder") { + librmcs::agent::AdvancedOptions{.dangerously_skip_version_checks = true}} + , deformable_infantry_{deformableInfantry} + , command_{deformableInfantry_command} + , tf_{deformableInfantry.tf_} { deformableInfantry.register_output("/referee/serial", referee_serial_); referee_serial_->read = [this](std::byte* buffer, size_t size) { @@ -568,7 +505,8 @@ class DeformableInfantryV2 deformable_infantry_.get_logger(), "[supercap] can1 rx=%c id=0x300 enabled=%d supercap_v=% .3f chassis_v=% .3f " "power=% .3f raw=[%02X %02X %02X %02X %02X %02X %02X %02X]", - supercap_rx ? 'Y' : 'N', supercap_rx ? (supercap_.supercap_enabled() ? 1 : 0) : -1, + supercap_rx ? 'Y' : 'N', + supercap_rx ? (supercap_.supercap_enabled() ? 1 : 0) : -1, supercap_rx ? supercap_.supercap_voltage() : nan_, supercap_rx ? supercap_.chassis_voltage() : nan_, supercap_rx ? supercap_.chassis_power() : nan_, @@ -666,25 +604,48 @@ class DeformableInfantryV2 imu_.store_gyroscope_status(data.x, data.y, data.z); } + rmcs_executor::Component& command_; + OutputInterface& tf_; - device::Bmi088 imu_; - device::LkMotor gimbal_yaw_motor_; - device::Dr16 dr16_; - device::DjiMotor chassis_wheel_motors_[4]; - device::DjiMotor chassis_steer_motors_[4]; - device::LkMotor chassis_joint_motors_[4]; + device::Bmi088 imu_{1000, 0.2, 0.0}; + device::LkMotor gimbal_yaw_motor_{deformable_infantry_, command_, "/gimbal/yaw"}; + device::Dr16 dr16_{deformable_infantry_}; + + device::DjiMotor chassis_wheel_motors_[4]{ + device::DjiMotor{deformable_infantry_, command_, "/chassis/left_front_wheel"}, + device::DjiMotor{deformable_infantry_, command_, "/chassis/left_back_wheel"}, + device::DjiMotor{deformable_infantry_, command_, "/chassis/right_back_wheel"}, + device::DjiMotor{deformable_infantry_, command_, "/chassis/right_front_wheel"}, + }; + device::DjiMotor chassis_steer_motors_[4]{ + device::DjiMotor{deformable_infantry_, command_, "/chassis/left_front_steering"}, + device::DjiMotor{deformable_infantry_, command_, "/chassis/left_back_steering"}, + device::DjiMotor{deformable_infantry_, command_, "/chassis/right_back_steering"}, + device::DjiMotor{deformable_infantry_, command_, "/chassis/right_front_steering"}, + }; + device::LkMotor chassis_joint_motors_[4]{ + device::LkMotor{deformable_infantry_, command_, "/chassis/left_front_joint"}, + device::LkMotor{deformable_infantry_, command_, "/chassis/left_back_joint"}, + device::LkMotor{deformable_infantry_, command_, "/chassis/right_back_joint"}, + device::LkMotor{deformable_infantry_, command_, "/chassis/right_front_joint"}, + }; + std::atomic wheel_status_received_[4] = {false, false, false, false}; std::atomic joint_status_received_[4] = {false, false, false, false}; bool debug_log_supercap_ = false; bool debug_log_wheel_motor_ = false; bool debug_log_deformable_joint_motor_ = false; - Clock::time_point next_chassis_feedback_log_time_; - Clock::time_point next_supercap_feedback_log_time_; - device::Supercap supercap_; - std::atomic latest_supercap_status_{device::CanPacket8{uint64_t{0}}}; + Clock::time_point next_chassis_feedback_log_time_{ + Clock::now() + std::chrono::seconds(1)}; + Clock::time_point next_supercap_feedback_log_time_{ + Clock::now() + std::chrono::seconds(1)}; + device::Supercap supercap_{deformable_infantry_, command_}; + std::atomic latest_supercap_status_{ + device::CanPacket8{uint64_t{0}}}; std::atomic supercap_status_received_{false}; - device::DjiMotor gimbal_bullet_feeder_; + device::DjiMotor gimbal_bullet_feeder_{ + deformable_infantry_, command_, "/gimbal/bullet_feeder"}; rmcs_utility::RingBuffer referee_ring_buffer_receive_{256}; OutputInterface referee_serial_; @@ -853,6 +814,36 @@ class DeformableInfantryV2 device::DjiMotor scope_motor_; }; + auto status_service_callback(const std::shared_ptr& response) + -> void { + response->success = true; + + auto feedback_message = std::ostringstream{}; + auto text = [&](std::format_string format, Args&&... args) { + std::println(feedback_message, format, std::forward(args)...); + }; + + text("Gimbal Status"); + text("- Yaw: {}", rmcs_board_lite->gimbal_yaw_motor_.last_raw_angle()); + text("- Pitch: {}", top_board_->gimbal_pitch_motor_.last_raw_angle()); + + text("Chassis Status"); + text("- left front wheel: {}", rmcs_board_lite->chassis_wheel_motors_[0].last_raw_angle()); + text("- left back wheel: {}", rmcs_board_lite->chassis_wheel_motors_[1].last_raw_angle()); + text("- right back wheel: {}", rmcs_board_lite->chassis_wheel_motors_[2].last_raw_angle()); + text("- right front wheel: {}", rmcs_board_lite->chassis_wheel_motors_[3].last_raw_angle()); + text("- left front steer: {}", rmcs_board_lite->chassis_steer_motors_[0].last_raw_angle()); + text("- left back steer: {}", rmcs_board_lite->chassis_steer_motors_[1].last_raw_angle()); + text("- right back steer: {}", rmcs_board_lite->chassis_steer_motors_[2].last_raw_angle()); + text("- right front steer: {}", rmcs_board_lite->chassis_steer_motors_[3].last_raw_angle()); + text("- left front joint: {}", rmcs_board_lite->chassis_joint_motors_[0].last_raw_angle()); + text("- left back joint: {}", rmcs_board_lite->chassis_joint_motors_[1].last_raw_angle()); + text("- right back joint: {}", rmcs_board_lite->chassis_joint_motors_[2].last_raw_angle()); + text("- right front joint: {}", rmcs_board_lite->chassis_joint_motors_[3].last_raw_angle()); + + response->message = feedback_message.str(); + } + OutputInterface tf_; InputInterface timestamp_; std::atomic hard_sync_pending_{false}; @@ -863,10 +854,7 @@ class DeformableInfantryV2 std::unique_ptr rmcs_board_lite; std::unique_ptr top_board_; - rclcpp::Subscription::SharedPtr steers_calibrate_subscription_; - rclcpp::Subscription::SharedPtr joints_calibrate_subscription_; - rclcpp::Subscription::SharedPtr gimbal_calibrate_subscription_; - + std::shared_ptr> status_service_; uint32_t cmd_tick_ = 0; }; From ddf8478f620a6cc9364a8c3a7c9763a89736557b Mon Sep 17 00:00:00 2001 From: ZGZ713912 Date: Mon, 11 May 2026 06:04:02 +0800 Subject: [PATCH 04/52] feat: add suspension toggle functionality and adjust leg radius parameters --- .../controller/chassis/deformable_chassis.cpp | 19 ++++++++++++++----- .../ui/widget/deformable_chassis_top_view.hpp | 4 ++-- 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/rmcs_ws/src/rmcs_core/src/controller/chassis/deformable_chassis.cpp b/rmcs_ws/src/rmcs_core/src/controller/chassis/deformable_chassis.cpp index 96828c2c..7c5ca742 100644 --- a/rmcs_ws/src/rmcs_core/src/controller/chassis/deformable_chassis.cpp +++ b/rmcs_ws/src/rmcs_core/src/controller/chassis/deformable_chassis.cpp @@ -349,6 +349,7 @@ class DeformableChassis update_mode_from_inputs_(switch_left, switch_right, keyboard); update_velocity_control(); update_lift_target_toggle(keyboard); + update_suspension_toggle_from_inputs_(switch_left, switch_right); run_joint_intent_pipeline_(); } while (false); @@ -459,14 +460,21 @@ class DeformableChassis bool prone_override_requested_() const { return keyboard_.ready() && keyboard_->ctrl; } - bool suspension_requested_by_switch_() const { - return switch_left_.ready() && switch_right_.ready() - && *switch_left_ == rmcs_msgs::Switch::DOWN && *switch_right_ == rmcs_msgs::Switch::UP; + bool suspension_toggle_requested_by_switch_( + rmcs_msgs::Switch switch_left, rmcs_msgs::Switch switch_right) const { + return switch_left == rmcs_msgs::Switch::DOWN && switch_right == rmcs_msgs::Switch::UP + && last_switch_right_ == rmcs_msgs::Switch::MIDDLE; + } + + void update_suspension_toggle_from_inputs_( + rmcs_msgs::Switch switch_left, rmcs_msgs::Switch switch_right) { + if (suspension_toggle_requested_by_switch_(switch_left, switch_right)) { + suspension_on = !suspension_on; + } } bool suspension_requested_by_input_() const { - return active_suspension_enable_ - && (prone_override_requested_() || suspension_requested_by_switch_()); + return active_suspension_enable_ && (prone_override_requested_() || suspension_on); } bool symmetric_joint_target_requested_() const { @@ -1175,6 +1183,7 @@ class DeformableChassis double target_physical_velocity_limit_; double target_physical_acceleration_limit_; bool active_suspension_enable_; + bool suspension_on = false; double pitch_kp_; double pitch_ki_; double pitch_kd_; diff --git a/rmcs_ws/src/rmcs_core/src/referee/app/ui/widget/deformable_chassis_top_view.hpp b/rmcs_ws/src/rmcs_core/src/referee/app/ui/widget/deformable_chassis_top_view.hpp index e5ea754b..72c81891 100644 --- a/rmcs_ws/src/rmcs_core/src/referee/app/ui/widget/deformable_chassis_top_view.hpp +++ b/rmcs_ws/src/rmcs_core/src/referee/app/ui/widget/deformable_chassis_top_view.hpp @@ -50,9 +50,9 @@ class DeformableChassisLegArcs { static constexpr uint16_t center_y_ = 1080 / 2; static constexpr uint16_t front_leg_radius_near_ = 102; - static constexpr uint16_t rear_leg_radius_near_ = 112; + static constexpr uint16_t rear_leg_radius_near_ = 102; static constexpr uint16_t front_leg_radius_far_ = 132; - static constexpr uint16_t rear_leg_radius_far_ = 142; + static constexpr uint16_t rear_leg_radius_far_ = 132; static constexpr uint16_t prone_leg_width_ = 6; static constexpr uint16_t upright_leg_width_ = 16; From 78c6d39a33fcf6772975768f6ed40186ff605c9c Mon Sep 17 00:00:00 2001 From: ZGZ713912 Date: Mon, 11 May 2026 21:30:40 +0800 Subject: [PATCH 05/52] feat: rename prone override method and update suspension toggle logic --- .../src/controller/chassis/deformable_chassis.cpp | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/rmcs_ws/src/rmcs_core/src/controller/chassis/deformable_chassis.cpp b/rmcs_ws/src/rmcs_core/src/controller/chassis/deformable_chassis.cpp index 7c5ca742..f4cf3174 100644 --- a/rmcs_ws/src/rmcs_core/src/controller/chassis/deformable_chassis.cpp +++ b/rmcs_ws/src/rmcs_core/src/controller/chassis/deformable_chassis.cpp @@ -458,7 +458,7 @@ class DeformableChassis return joint_feedback; } - bool prone_override_requested_() const { return keyboard_.ready() && keyboard_->ctrl; } + bool prone_override_requested_by_keyboard() const { return keyboard_.ready() && keyboard_->ctrl; } bool suspension_toggle_requested_by_switch_( rmcs_msgs::Switch switch_left, rmcs_msgs::Switch switch_right) const { @@ -469,12 +469,12 @@ class DeformableChassis void update_suspension_toggle_from_inputs_( rmcs_msgs::Switch switch_left, rmcs_msgs::Switch switch_right) { if (suspension_toggle_requested_by_switch_(switch_left, switch_right)) { - suspension_on = !suspension_on; + suspension_on_by_switch = !suspension_on_by_switch; } } bool suspension_requested_by_input_() const { - return active_suspension_enable_ && (prone_override_requested_() || suspension_on); + return active_suspension_enable_ && (prone_override_requested_by_keyboard() || suspension_on_by_switch); } bool symmetric_joint_target_requested_() const { @@ -490,7 +490,7 @@ class DeformableChassis requested_target_physical_angles_rad_[kRightBack] = deg_to_rad(rb_current_target_angle_); requested_target_physical_angles_rad_[kRightFront] = deg_to_rad(rf_current_target_angle_); - const bool prone_override = prone_override_requested_(); + const bool prone_override = prone_override_requested_by_keyboard(); if (suspension_requested_by_input_()) { requested_target_physical_angles_rad_.fill(deg_to_rad(min_angle_)); } @@ -687,6 +687,7 @@ class DeformableChassis rb_current_target_angle_ = current_target_angle_; rf_current_target_angle_ = current_target_angle_; joint_target_active_ = false; + suspension_on_by_switch = false; *scope_motor_control_torque = nan_; @@ -1183,7 +1184,7 @@ class DeformableChassis double target_physical_velocity_limit_; double target_physical_acceleration_limit_; bool active_suspension_enable_; - bool suspension_on = false; + bool suspension_on_by_switch = false; double pitch_kp_; double pitch_ki_; double pitch_kd_; From 32bb48bda4a49dc4bb9539b178648bbc70b13440 Mon Sep 17 00:00:00 2001 From: ZGZ713912 Date: Tue, 12 May 2026 07:10:48 +0800 Subject: [PATCH 06/52] feat: remove suspension mode and torque parameters from joint controllers and add auto aim for deformable infantry --- .../config/deformable-infantry-omni-b.yaml | 15 ------ .../config/deformable-infantry-omni.yaml | 17 +------ .../config/deformable-infantry-steering.yaml | 15 ------ .../src/rmcs_bringup/launch/rmcs.launch.py | 13 ++++- rmcs_ws/src/rmcs_bringup/package.xml | 1 + .../controller/chassis/deformable_chassis.cpp | 50 ------------------- .../bullet_feeder_controller_17mm.cpp | 9 ++-- 7 files changed, 19 insertions(+), 101 deletions(-) diff --git a/rmcs_ws/src/rmcs_bringup/config/deformable-infantry-omni-b.yaml b/rmcs_ws/src/rmcs_bringup/config/deformable-infantry-omni-b.yaml index 18d5bd50..840fd485 100644 --- a/rmcs_ws/src/rmcs_bringup/config/deformable-infantry-omni-b.yaml +++ b/rmcs_ws/src/rmcs_bringup/config/deformable-infantry-omni-b.yaml @@ -181,8 +181,6 @@ lf_joint_controller: measurement_velocity: /chassis/left_front_joint/physical_velocity setpoint_angle: /chassis/left_front_joint/target_physical_angle setpoint_velocity: /chassis/left_front_joint/target_physical_velocity - mode_input: /chassis/left_front_joint/suspension_mode - suspension_torque: /chassis/left_front_joint/suspension_torque control: /chassis/left_front_joint/control_torque # Normal ADRC servo mode @@ -219,8 +217,6 @@ lf_joint_controller: # Joint-local feedforward / limit shaping torque_feedforward_gain: 0.0 - suspension_torque_feedforward_gain: -1.0 - lb_joint_controller: ros__parameters: # Same joint-servo layout as lf_joint_controller @@ -228,8 +224,6 @@ lb_joint_controller: measurement_velocity: /chassis/left_back_joint/physical_velocity setpoint_angle: /chassis/left_back_joint/target_physical_angle setpoint_velocity: /chassis/left_back_joint/target_physical_velocity - mode_input: /chassis/left_back_joint/suspension_mode - suspension_torque: /chassis/left_back_joint/suspension_torque control: /chassis/left_back_joint/control_torque dt: 0.001 b0: -1.0 @@ -260,8 +254,6 @@ lb_joint_controller: suspension_output_min: -35.0 suspension_output_max: 35.0 torque_feedforward_gain: 0.0 - suspension_torque_feedforward_gain: -1.0 - rb_joint_controller: ros__parameters: # Same joint-servo layout as lf_joint_controller @@ -269,8 +261,6 @@ rb_joint_controller: measurement_velocity: /chassis/right_back_joint/physical_velocity setpoint_angle: /chassis/right_back_joint/target_physical_angle setpoint_velocity: /chassis/right_back_joint/target_physical_velocity - mode_input: /chassis/right_back_joint/suspension_mode - suspension_torque: /chassis/right_back_joint/suspension_torque control: /chassis/right_back_joint/control_torque dt: 0.001 b0: -1.0 @@ -301,8 +291,6 @@ rb_joint_controller: suspension_output_min: -35.0 suspension_output_max: 35.0 torque_feedforward_gain: 0.0 - suspension_torque_feedforward_gain: -1.0 - rf_joint_controller: ros__parameters: # Same joint-servo layout as lf_joint_controller @@ -310,8 +298,6 @@ rf_joint_controller: measurement_velocity: /chassis/right_front_joint/physical_velocity setpoint_angle: /chassis/right_front_joint/target_physical_angle setpoint_velocity: /chassis/right_front_joint/target_physical_velocity - mode_input: /chassis/right_front_joint/suspension_mode - suspension_torque: /chassis/right_front_joint/suspension_torque control: /chassis/right_front_joint/control_torque dt: 0.001 b0: -1.0 @@ -342,4 +328,3 @@ rf_joint_controller: suspension_output_min: -35.0 suspension_output_max: 35.0 torque_feedforward_gain: 0.0 - suspension_torque_feedforward_gain: -1.0 diff --git a/rmcs_ws/src/rmcs_bringup/config/deformable-infantry-omni.yaml b/rmcs_ws/src/rmcs_bringup/config/deformable-infantry-omni.yaml index dd76a55d..6a871259 100644 --- a/rmcs_ws/src/rmcs_bringup/config/deformable-infantry-omni.yaml +++ b/rmcs_ws/src/rmcs_bringup/config/deformable-infantry-omni.yaml @@ -30,7 +30,7 @@ rmcs_executor: - rmcs_core::controller::chassis::DeformableJointController -> rf_joint_controller - rmcs_core::broadcaster::ValueBroadcaster -> value_broadcaster - # - rmcs::AutoAimComponent -> auto_aim_component + - rmcs::AutoAimComponent -> auto_aim_component value_broadcaster: ros__parameters: @@ -200,8 +200,6 @@ lf_joint_controller: measurement_velocity: /chassis/left_front_joint/physical_velocity setpoint_angle: /chassis/left_front_joint/target_physical_angle setpoint_velocity: /chassis/left_front_joint/target_physical_velocity - mode_input: /chassis/left_front_joint/suspension_mode - suspension_torque: /chassis/left_front_joint/suspension_torque control: /chassis/left_front_joint/control_torque # Normal ADRC servo mode @@ -238,8 +236,6 @@ lf_joint_controller: # Joint-local feedforward / limit shaping torque_feedforward_gain: 0.0 - suspension_torque_feedforward_gain: -1.0 - lb_joint_controller: ros__parameters: # Same joint-servo layout as lf_joint_controller @@ -247,8 +243,6 @@ lb_joint_controller: measurement_velocity: /chassis/left_back_joint/physical_velocity setpoint_angle: /chassis/left_back_joint/target_physical_angle setpoint_velocity: /chassis/left_back_joint/target_physical_velocity - mode_input: /chassis/left_back_joint/suspension_mode - suspension_torque: /chassis/left_back_joint/suspension_torque control: /chassis/left_back_joint/control_torque dt: 0.001 b0: -0.60 @@ -279,8 +273,6 @@ lb_joint_controller: suspension_output_min: -35.0 suspension_output_max: 35.0 torque_feedforward_gain: 0.0 - suspension_torque_feedforward_gain: -1.0 - rb_joint_controller: ros__parameters: # Same joint-servo layout as lf_joint_controller @@ -288,8 +280,6 @@ rb_joint_controller: measurement_velocity: /chassis/right_back_joint/physical_velocity setpoint_angle: /chassis/right_back_joint/target_physical_angle setpoint_velocity: /chassis/right_back_joint/target_physical_velocity - mode_input: /chassis/right_back_joint/suspension_mode - suspension_torque: /chassis/right_back_joint/suspension_torque control: /chassis/right_back_joint/control_torque dt: 0.001 b0: -0.60 @@ -320,8 +310,6 @@ rb_joint_controller: suspension_output_min: -35.0 suspension_output_max: 35.0 torque_feedforward_gain: 0.0 - suspension_torque_feedforward_gain: -1.0 - rf_joint_controller: ros__parameters: # Same joint-servo layout as lf_joint_controller @@ -329,8 +317,6 @@ rf_joint_controller: measurement_velocity: /chassis/right_front_joint/physical_velocity setpoint_angle: /chassis/right_front_joint/target_physical_angle setpoint_velocity: /chassis/right_front_joint/target_physical_velocity - mode_input: /chassis/right_front_joint/suspension_mode - suspension_torque: /chassis/right_front_joint/suspension_torque control: /chassis/right_front_joint/control_torque dt: 0.001 b0: -0.60 @@ -361,4 +347,3 @@ rf_joint_controller: suspension_output_min: -35.0 suspension_output_max: 35.0 torque_feedforward_gain: 0.0 - suspension_torque_feedforward_gain: -1.0 diff --git a/rmcs_ws/src/rmcs_bringup/config/deformable-infantry-steering.yaml b/rmcs_ws/src/rmcs_bringup/config/deformable-infantry-steering.yaml index 761d2265..51a8fc1d 100644 --- a/rmcs_ws/src/rmcs_bringup/config/deformable-infantry-steering.yaml +++ b/rmcs_ws/src/rmcs_bringup/config/deformable-infantry-steering.yaml @@ -181,8 +181,6 @@ lf_joint_controller: measurement_velocity: /chassis/left_front_joint/physical_velocity setpoint_angle: /chassis/left_front_joint/target_physical_angle setpoint_velocity: /chassis/left_front_joint/target_physical_velocity - mode_input: /chassis/left_front_joint/suspension_mode - suspension_torque: /chassis/left_front_joint/suspension_torque control: /chassis/left_front_joint/control_torque # Normal ADRC servo mode @@ -219,8 +217,6 @@ lf_joint_controller: # Joint-local feedforward / limit shaping torque_feedforward_gain: 0.0 - suspension_torque_feedforward_gain: -1.0 - lb_joint_controller: ros__parameters: # Same joint-servo layout as lf_joint_controller @@ -228,8 +224,6 @@ lb_joint_controller: measurement_velocity: /chassis/left_back_joint/physical_velocity setpoint_angle: /chassis/left_back_joint/target_physical_angle setpoint_velocity: /chassis/left_back_joint/target_physical_velocity - mode_input: /chassis/left_back_joint/suspension_mode - suspension_torque: /chassis/left_back_joint/suspension_torque control: /chassis/left_back_joint/control_torque dt: 0.001 b0: -1.0 @@ -260,8 +254,6 @@ lb_joint_controller: suspension_output_min: -35.0 suspension_output_max: 35.0 torque_feedforward_gain: 0.0 - suspension_torque_feedforward_gain: -1.0 - rb_joint_controller: ros__parameters: # Same joint-servo layout as lf_joint_controller @@ -269,8 +261,6 @@ rb_joint_controller: measurement_velocity: /chassis/right_back_joint/physical_velocity setpoint_angle: /chassis/right_back_joint/target_physical_angle setpoint_velocity: /chassis/right_back_joint/target_physical_velocity - mode_input: /chassis/right_back_joint/suspension_mode - suspension_torque: /chassis/right_back_joint/suspension_torque control: /chassis/right_back_joint/control_torque dt: 0.001 b0: -1.0 @@ -301,8 +291,6 @@ rb_joint_controller: suspension_output_min: -35.0 suspension_output_max: 35.0 torque_feedforward_gain: 0.0 - suspension_torque_feedforward_gain: -1.0 - rf_joint_controller: ros__parameters: # Same joint-servo layout as lf_joint_controller @@ -310,8 +298,6 @@ rf_joint_controller: measurement_velocity: /chassis/right_front_joint/physical_velocity setpoint_angle: /chassis/right_front_joint/target_physical_angle setpoint_velocity: /chassis/right_front_joint/target_physical_velocity - mode_input: /chassis/right_front_joint/suspension_mode - suspension_torque: /chassis/right_front_joint/suspension_torque control: /chassis/right_front_joint/control_torque dt: 0.001 b0: -1.0 @@ -342,4 +328,3 @@ rf_joint_controller: suspension_output_min: -35.0 suspension_output_max: 35.0 torque_feedforward_gain: 0.0 - suspension_torque_feedforward_gain: -1.0 diff --git a/rmcs_ws/src/rmcs_bringup/launch/rmcs.launch.py b/rmcs_ws/src/rmcs_bringup/launch/rmcs.launch.py index 976cb9e7..d810a08d 100644 --- a/rmcs_ws/src/rmcs_bringup/launch/rmcs.launch.py +++ b/rmcs_ws/src/rmcs_bringup/launch/rmcs.launch.py @@ -6,7 +6,8 @@ LaunchDescription, LaunchDescriptionEntity, ) -from launch.actions import LogInfo +from launch.actions import IncludeLaunchDescription,LogInfo +from launch.launch_description_sources import PythonLaunchDescriptionSource from launch.substitutions import LaunchConfiguration from launch_ros.actions import Node @@ -52,6 +53,16 @@ def visit( if is_automatic: pass + + entities.append( + IncludeLaunchDescription( + PythonLaunchDescriptionSource([ + FindPackageShare('rmcs_auto_aim_v2'), '/launch.py' + ]) + ) + ) + + return entities diff --git a/rmcs_ws/src/rmcs_bringup/package.xml b/rmcs_ws/src/rmcs_bringup/package.xml index c64c661f..9c8cc981 100644 --- a/rmcs_ws/src/rmcs_bringup/package.xml +++ b/rmcs_ws/src/rmcs_bringup/package.xml @@ -11,6 +11,7 @@ ament_cmake joint_state_broadcaster + rmcs_auto_aim_v2 robot_state_publisher rviz2 xacro diff --git a/rmcs_ws/src/rmcs_core/src/controller/chassis/deformable_chassis.cpp b/rmcs_ws/src/rmcs_core/src/controller/chassis/deformable_chassis.cpp index f4cf3174..10aabcfa 100644 --- a/rmcs_ws/src/rmcs_core/src/controller/chassis/deformable_chassis.cpp +++ b/rmcs_ws/src/rmcs_core/src/controller/chassis/deformable_chassis.cpp @@ -255,27 +255,6 @@ class DeformableChassis right_front_joint_target_physical_acceleration_, nan_); register_output("/chassis/processed_encoder/angle", processed_encoder_angle_, nan_); - register_output( - "/chassis/left_front_joint/suspension_mode", left_front_joint_suspension_mode_, false); - register_output( - "/chassis/left_back_joint/suspension_mode", left_back_joint_suspension_mode_, false); - register_output( - "/chassis/right_front_joint/suspension_mode", right_front_joint_suspension_mode_, false); - register_output( - "/chassis/right_back_joint/suspension_mode", right_back_joint_suspension_mode_, false); - - register_output( - "/chassis/left_front_joint/suspension_torque", left_front_joint_suspension_torque_, - nan_); - register_output( - "/chassis/left_back_joint/suspension_torque", left_back_joint_suspension_torque_, nan_); - register_output( - "/chassis/right_back_joint/suspension_torque", right_back_joint_suspension_torque_, - nan_); - register_output( - "/chassis/right_front_joint/suspension_torque", right_front_joint_suspension_torque_, - nan_); - *mode_ = rmcs_msgs::ChassisMode::AUTO; chassis_control_velocity_->vector << nan_, nan_, nan_; @@ -403,18 +382,6 @@ class DeformableChassis return wrap_deg(joint_offset) - wrap_deg(*joint_encoder_angle) + legacy_fixed_compensation; } - void clear_suspension_output_interfaces_() { - *left_front_joint_suspension_mode_ = false; - *left_back_joint_suspension_mode_ = false; - *right_back_joint_suspension_mode_ = false; - *right_front_joint_suspension_mode_ = false; - - *left_front_joint_suspension_torque_ = 0.0; - *left_back_joint_suspension_torque_ = 0.0; - *right_back_joint_suspension_torque_ = 0.0; - *right_front_joint_suspension_torque_ = 0.0; - } - void update_mode_from_inputs_( rmcs_msgs::Switch switch_left, rmcs_msgs::Switch switch_right, const rmcs_msgs::Keyboard& keyboard) { @@ -621,7 +588,6 @@ class DeformableChassis } void update_active_suspension_(const JointFeedbackFrame&) { - clear_suspension_output_interfaces_(); if (!suspension_requested_by_input_()) { reset_attitude_correction_state_(); return; @@ -716,7 +682,6 @@ class DeformableChassis *processed_encoder_angle_ = nan_; - clear_suspension_output_interfaces_(); } void update_velocity_control() { @@ -860,11 +825,6 @@ class DeformableChassis scope_motor_control(prone_override); update_active_suspension_(joint_feedback); - *left_front_joint_suspension_mode_ = joint_suspension_active_[kLeftFront]; - *left_back_joint_suspension_mode_ = joint_suspension_active_[kLeftBack]; - *right_front_joint_suspension_mode_ = joint_suspension_active_[kRightFront]; - *right_back_joint_suspension_mode_ = joint_suspension_active_[kRightBack]; - update_joint_target_trajectory(); publish_joint_target_angles(joint_feedback.physical_angles); } @@ -1065,7 +1025,6 @@ class DeformableChassis *rb_angle_error_ = nan_; *rf_angle_error_ = nan_; - clear_suspension_output_interfaces_(); } private: @@ -1149,15 +1108,6 @@ class DeformableChassis OutputInterface processed_encoder_angle_; - OutputInterface left_front_joint_suspension_mode_; - OutputInterface left_back_joint_suspension_mode_; - OutputInterface right_front_joint_suspension_mode_; - OutputInterface right_back_joint_suspension_mode_; - OutputInterface left_front_joint_suspension_torque_; - OutputInterface left_back_joint_suspension_torque_; - OutputInterface right_front_joint_suspension_torque_; - OutputInterface right_back_joint_suspension_torque_; - double min_angle_; double max_angle_; double left_front_joint_offset_; diff --git a/rmcs_ws/src/rmcs_core/src/controller/shooting/bullet_feeder_controller_17mm.cpp b/rmcs_ws/src/rmcs_core/src/controller/shooting/bullet_feeder_controller_17mm.cpp index 0c97c98a..556762af 100644 --- a/rmcs_ws/src/rmcs_core/src/controller/shooting/bullet_feeder_controller_17mm.cpp +++ b/rmcs_ws/src/rmcs_core/src/controller/shooting/bullet_feeder_controller_17mm.cpp @@ -53,7 +53,7 @@ class BulletFeederController17mm register_input("/remote/mouse", mouse_); register_input("/remote/keyboard", keyboard_); - register_input("/gimbal/auto_aim/fire_control", fire_control_, false); + register_input("/auto_aim/should_shoot", fire_control_, false); register_input("/gimbal/bullet_feeder/velocity", bullet_feeder_velocity_); register_output( @@ -102,8 +102,9 @@ class BulletFeederController17mm if (*friction_ready_) { if (shoot_mode == ShootMode::AUTOMATIC) { - bool triggered = mouse.left || switch_left == Switch::DOWN - || (switch_right == Switch::UP && *fire_control_); + const bool auto_aim_enabled = mouse.right || switch_right == Switch::UP; + bool triggered = (mouse.left && !mouse.right) || switch_left == Switch::DOWN + || (mouse.left && auto_aim_enabled && *fire_control_); bullet_allowance = triggered ? *control_bullet_allowance_limited_by_heat_ : 0; } else { @@ -232,4 +233,4 @@ class BulletFeederController17mm #include PLUGINLIB_EXPORT_CLASS( - rmcs_core::controller::shooting::BulletFeederController17mm, rmcs_executor::Component) \ No newline at end of file + rmcs_core::controller::shooting::BulletFeederController17mm, rmcs_executor::Component) From 0103ef7a0fdb3403d6741e987acb769fe48f3c85 Mon Sep 17 00:00:00 2001 From: eye-on Date: Tue, 12 May 2026 01:09:00 +0800 Subject: [PATCH 07/52] feat: fix the correct supercap boost mode --- .../src/controller/chassis/chassis_power_controller.cpp | 8 ++++++-- rmcs_ws/src/rmcs_core/src/hardware/device/supercap.hpp | 5 ++++- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/rmcs_ws/src/rmcs_core/src/controller/chassis/chassis_power_controller.cpp b/rmcs_ws/src/rmcs_core/src/controller/chassis/chassis_power_controller.cpp index 2d084f8b..2d9fa58b 100644 --- a/rmcs_ws/src/rmcs_core/src/controller/chassis/chassis_power_controller.cpp +++ b/rmcs_ws/src/rmcs_core/src/controller/chassis/chassis_power_controller.cpp @@ -34,6 +34,7 @@ class ChassisPowerController register_input("/referee/chassis/power_limit", chassis_power_limit_referee_); register_input("/referee/chassis/buffer_energy", chassis_buffer_energy_referee_); + register_output("/chassis/supercap/control_enable", supercap_control_enabled_, false); register_output("/chassis/supercap/charge_power_limit", supercap_charge_power_limit_, 0.0); register_output("/chassis/control_power_limit", chassis_control_power_limit_, 0.0); @@ -64,6 +65,7 @@ class ChassisPowerController update_virtual_buffer_energy(); boost_mode_ = keyboard.shift || rotary_knob < -0.9; + *supercap_control_enabled_ = boost_mode_; update_control_power_limit(); } @@ -93,6 +95,7 @@ class ChassisPowerController void reset_power_control() { virtual_buffer_energy_ = virtual_buffer_energy_limit_; boost_mode_ = false; + *supercap_control_enabled_ = false; *chassis_control_power_limit_ = 0.0; } @@ -107,7 +110,7 @@ class ChassisPowerController void update_control_power_limit() { double power_limit; - if (boost_mode_ && *supercap_enabled_) + if (*supercap_control_enabled_ && *supercap_enabled_) power_limit = *mode_ == rmcs_msgs::ChassisMode::LAUNCH_RAMP ? inf_ : *chassis_power_limit_referee_ + 80.0; @@ -161,6 +164,7 @@ class ChassisPowerController InputInterface chassis_buffer_energy_referee_; bool boost_mode_ = false; + OutputInterface supercap_control_enabled_; OutputInterface supercap_charge_power_limit_; double chassis_power_limit_expected_; OutputInterface chassis_control_power_limit_; @@ -179,4 +183,4 @@ class ChassisPowerController #include PLUGINLIB_EXPORT_CLASS( - rmcs_core::controller::chassis::ChassisPowerController, rmcs_executor::Component) \ No newline at end of file + rmcs_core::controller::chassis::ChassisPowerController, rmcs_executor::Component) diff --git a/rmcs_ws/src/rmcs_core/src/hardware/device/supercap.hpp b/rmcs_ws/src/rmcs_core/src/hardware/device/supercap.hpp index 8ac884f0..e88fc018 100644 --- a/rmcs_ws/src/rmcs_core/src/hardware/device/supercap.hpp +++ b/rmcs_ws/src/rmcs_core/src/hardware/device/supercap.hpp @@ -28,6 +28,8 @@ class Supercap { status_component.register_output("/chassis/supercap/voltage", supercap_voltage_, 0.0); status_component.register_output("/chassis/supercap/enabled", supercap_enabled_, false); + command_component.register_input( + "/chassis/supercap/control_enable", supercap_control_enabled_); command_component.register_input("/referee/chassis/output_status", chassis_output_status_); command_component.register_input( "/chassis/supercap/charge_power_limit", supercap_charge_power_limit_); @@ -52,7 +54,7 @@ class Supercap { CanPacket8::Quarter generate_command() const { SupercapCommand command; - command.enabled = *chassis_output_status_; + command.enabled = *chassis_output_status_ && *supercap_control_enabled_; const double power_limit = *supercap_charge_power_limit_; if (std::isnan(power_limit)) @@ -111,6 +113,7 @@ class Supercap { Component::OutputInterface supercap_voltage_; Component::OutputInterface supercap_enabled_; + Component::InputInterface supercap_control_enabled_; Component::InputInterface chassis_output_status_; Component::InputInterface supercap_charge_power_limit_; }; From b660087f8e73eab4353c9e9dcb3d00b234921f1a Mon Sep 17 00:00:00 2001 From: eye-on Date: Tue, 12 May 2026 01:40:56 +0800 Subject: [PATCH 08/52] feat:supercup fixed --- .codex | 0 rmcs_ws/build | 1 + rmcs_ws/install | 1 + rmcs_ws/log | 1 + .../config/deformable-infantry-omni-b.yaml | 2 +- rmcs_ws/src/rmcs_bringup/launch/rmcs.launch.py | 14 +++++++------- rmcs_ws/src/serial | 1 + 7 files changed, 12 insertions(+), 8 deletions(-) create mode 100644 .codex create mode 120000 rmcs_ws/build create mode 120000 rmcs_ws/install create mode 120000 rmcs_ws/log create mode 160000 rmcs_ws/src/serial diff --git a/.codex b/.codex new file mode 100644 index 00000000..e69de29b diff --git a/rmcs_ws/build b/rmcs_ws/build new file mode 120000 index 00000000..2ec6ad5a --- /dev/null +++ b/rmcs_ws/build @@ -0,0 +1 @@ +build-cross-arm64 \ No newline at end of file diff --git a/rmcs_ws/install b/rmcs_ws/install new file mode 120000 index 00000000..4ed00624 --- /dev/null +++ b/rmcs_ws/install @@ -0,0 +1 @@ +install-cross-arm64 \ No newline at end of file diff --git a/rmcs_ws/log b/rmcs_ws/log new file mode 120000 index 00000000..4c7d622b --- /dev/null +++ b/rmcs_ws/log @@ -0,0 +1 @@ +log-cross-arm64 \ No newline at end of file diff --git a/rmcs_ws/src/rmcs_bringup/config/deformable-infantry-omni-b.yaml b/rmcs_ws/src/rmcs_bringup/config/deformable-infantry-omni-b.yaml index 840fd485..b968f194 100644 --- a/rmcs_ws/src/rmcs_bringup/config/deformable-infantry-omni-b.yaml +++ b/rmcs_ws/src/rmcs_bringup/config/deformable-infantry-omni-b.yaml @@ -46,7 +46,7 @@ deformable_infantry: left_back_zero_point: 5167 right_back_zero_point: 3098 right_front_zero_point: 6485 - yaw_motor_zero_point: 39442 + yaw_motor_zero_point: 25406 pitch_motor_zero_point: 56556 debug_log_supercap: false debug_log_wheel_motor: false diff --git a/rmcs_ws/src/rmcs_bringup/launch/rmcs.launch.py b/rmcs_ws/src/rmcs_bringup/launch/rmcs.launch.py index d810a08d..58abbbcc 100644 --- a/rmcs_ws/src/rmcs_bringup/launch/rmcs.launch.py +++ b/rmcs_ws/src/rmcs_bringup/launch/rmcs.launch.py @@ -54,13 +54,13 @@ def visit( if is_automatic: pass - entities.append( - IncludeLaunchDescription( - PythonLaunchDescriptionSource([ - FindPackageShare('rmcs_auto_aim_v2'), '/launch.py' - ]) - ) - ) + # entities.append( + # IncludeLaunchDescription( + # PythonLaunchDescriptionSource([ + # FindPackageShare('rmcs_auto_aim_v2'), '/launch.py' + # ]) + # ) + # ) diff --git a/rmcs_ws/src/serial b/rmcs_ws/src/serial new file mode 160000 index 00000000..d50c4018 --- /dev/null +++ b/rmcs_ws/src/serial @@ -0,0 +1 @@ +Subproject commit d50c401809f17cc5862bdbe37cc6e15737ba7c49 From eeb2099ea52bf084a8dde64c723b0a2b3b95c341 Mon Sep 17 00:00:00 2001 From: ZGZ713912 Date: Tue, 12 May 2026 09:08:25 +0800 Subject: [PATCH 09/52] Refactor deformable chassis and joint controller configurations - Removed unused parameters and inputs related to joint velocity and torque feedforward from the deformable infantry steering configuration. - Simplified the DeformableChassis class by eliminating legacy joint feedback structures and inputs, focusing on physical angles only. - Updated the DeformableJointController to streamline configuration loading and mode handling, consolidating mode configurations into a single structure. - Enhanced the control logic to directly use the new configuration structure, removing unnecessary complexity and improving clarity. - Ensured that output limits are consistently applied across the control logic, enhancing robustness. --- .devcontainer/state/codex/.tmp/plugins | 1 + rmcs_ws/src/hikcamera | 1 + rmcs_ws/src/rmcs_auto_aim_v2 | 1 + .../config/deformable-infantry-omni-b.yaml | 65 ---- .../config/deformable-infantry-omni.yaml | 81 +---- .../config/deformable-infantry-steering.yaml | 65 ---- .../controller/chassis/deformable_chassis.cpp | 293 ++---------------- .../chassis/deformable_joint_controller.cpp | 258 +++------------ 8 files changed, 70 insertions(+), 695 deletions(-) create mode 160000 .devcontainer/state/codex/.tmp/plugins create mode 160000 rmcs_ws/src/hikcamera create mode 160000 rmcs_ws/src/rmcs_auto_aim_v2 diff --git a/.devcontainer/state/codex/.tmp/plugins b/.devcontainer/state/codex/.tmp/plugins new file mode 160000 index 00000000..3c463363 --- /dev/null +++ b/.devcontainer/state/codex/.tmp/plugins @@ -0,0 +1 @@ +Subproject commit 3c46336323290cda7c90d78e7a0134f47cc73713 diff --git a/rmcs_ws/src/hikcamera b/rmcs_ws/src/hikcamera new file mode 160000 index 00000000..f0077f03 --- /dev/null +++ b/rmcs_ws/src/hikcamera @@ -0,0 +1 @@ +Subproject commit f0077f034800bcd0dde4fffeff270b733772a57e diff --git a/rmcs_ws/src/rmcs_auto_aim_v2 b/rmcs_ws/src/rmcs_auto_aim_v2 new file mode 160000 index 00000000..8f25103a --- /dev/null +++ b/rmcs_ws/src/rmcs_auto_aim_v2 @@ -0,0 +1 @@ +Subproject commit 8f25103a42fd601bd8259a3da51ad8ef870a5b6b diff --git a/rmcs_ws/src/rmcs_bringup/config/deformable-infantry-omni-b.yaml b/rmcs_ws/src/rmcs_bringup/config/deformable-infantry-omni-b.yaml index b968f194..489ee300 100644 --- a/rmcs_ws/src/rmcs_bringup/config/deformable-infantry-omni-b.yaml +++ b/rmcs_ws/src/rmcs_bringup/config/deformable-infantry-omni-b.yaml @@ -178,12 +178,9 @@ lf_joint_controller: ros__parameters: # Joint-local servo inputs produced by chassis intent generation measurement_angle: /chassis/left_front_joint/physical_angle - measurement_velocity: /chassis/left_front_joint/physical_velocity setpoint_angle: /chassis/left_front_joint/target_physical_angle - setpoint_velocity: /chassis/left_front_joint/target_physical_velocity control: /chassis/left_front_joint/control_torque - # Normal ADRC servo mode dt: 0.001 b0: -1.0 kt: 1.0 @@ -200,30 +197,11 @@ lf_joint_controller: u_max: 200.0 output_min: -200.0 output_max: 200.0 - - # Suspension ADRC servo mode - suspension_td_h: 0.001 - suspension_td_r: 12.0 - suspension_eso_w0: 80.0 - suspension_k1: 6.0 - suspension_k2: 3.0 - suspension_alpha1: 0.75 - suspension_alpha2: 0.7 - suspension_delta: 0.02 - suspension_u_min: -35.0 - suspension_u_max: 35.0 - suspension_output_min: -35.0 - suspension_output_max: 35.0 - - # Joint-local feedforward / limit shaping - torque_feedforward_gain: 0.0 lb_joint_controller: ros__parameters: # Same joint-servo layout as lf_joint_controller measurement_angle: /chassis/left_back_joint/physical_angle - measurement_velocity: /chassis/left_back_joint/physical_velocity setpoint_angle: /chassis/left_back_joint/target_physical_angle - setpoint_velocity: /chassis/left_back_joint/target_physical_velocity control: /chassis/left_back_joint/control_torque dt: 0.001 b0: -1.0 @@ -241,26 +219,11 @@ lb_joint_controller: u_max: 200.0 output_min: -200.0 output_max: 200.0 - suspension_td_h: 0.001 - suspension_td_r: 12.0 - suspension_eso_w0: 80.0 - suspension_k1: 6.0 - suspension_k2: 3.0 - suspension_alpha1: 0.75 - suspension_alpha2: 0.7 - suspension_delta: 0.02 - suspension_u_min: -35.0 - suspension_u_max: 35.0 - suspension_output_min: -35.0 - suspension_output_max: 35.0 - torque_feedforward_gain: 0.0 rb_joint_controller: ros__parameters: # Same joint-servo layout as lf_joint_controller measurement_angle: /chassis/right_back_joint/physical_angle - measurement_velocity: /chassis/right_back_joint/physical_velocity setpoint_angle: /chassis/right_back_joint/target_physical_angle - setpoint_velocity: /chassis/right_back_joint/target_physical_velocity control: /chassis/right_back_joint/control_torque dt: 0.001 b0: -1.0 @@ -278,26 +241,11 @@ rb_joint_controller: u_max: 200.0 output_min: -200.0 output_max: 200.0 - suspension_td_h: 0.001 - suspension_td_r: 12.0 - suspension_eso_w0: 80.0 - suspension_k1: 6.0 - suspension_k2: 3.0 - suspension_alpha1: 0.75 - suspension_alpha2: 0.7 - suspension_delta: 0.02 - suspension_u_min: -35.0 - suspension_u_max: 35.0 - suspension_output_min: -35.0 - suspension_output_max: 35.0 - torque_feedforward_gain: 0.0 rf_joint_controller: ros__parameters: # Same joint-servo layout as lf_joint_controller measurement_angle: /chassis/right_front_joint/physical_angle - measurement_velocity: /chassis/right_front_joint/physical_velocity setpoint_angle: /chassis/right_front_joint/target_physical_angle - setpoint_velocity: /chassis/right_front_joint/target_physical_velocity control: /chassis/right_front_joint/control_torque dt: 0.001 b0: -1.0 @@ -315,16 +263,3 @@ rf_joint_controller: u_max: 200.0 output_min: -200.0 output_max: 200.0 - suspension_td_h: 0.001 - suspension_td_r: 12.0 - suspension_eso_w0: 80.0 - suspension_k1: 6.0 - suspension_k2: 3.0 - suspension_alpha1: 0.75 - suspension_alpha2: 0.7 - suspension_delta: 0.02 - suspension_u_min: -35.0 - suspension_u_max: 35.0 - suspension_output_min: -35.0 - suspension_output_max: 35.0 - torque_feedforward_gain: 0.0 diff --git a/rmcs_ws/src/rmcs_bringup/config/deformable-infantry-omni.yaml b/rmcs_ws/src/rmcs_bringup/config/deformable-infantry-omni.yaml index 6a871259..75d015ef 100644 --- a/rmcs_ws/src/rmcs_bringup/config/deformable-infantry-omni.yaml +++ b/rmcs_ws/src/rmcs_bringup/config/deformable-infantry-omni.yaml @@ -61,21 +61,7 @@ chassis_controller: active_suspension_enable: true spin_ratio: 1.0 - # Suspension geometry / support model. - active_suspension_mass: 22.5 - active_suspension_rod_length: 0.150 - active_suspension_Kz: 150.0 - active_suspension_D_leg: 10.0 - active_suspension_torque_limit: 80.0 - active_suspension_gravity_comp_gain: 1.0 - active_suspension_control_acceleration_limit: 6.0 - active_suspension_preload_angle_deg: 8.0 - active_suspension_entry_offset_deg: 1.5 - active_suspension_ride_height_offset_deg: 3.0 - active_suspension_hold_travel_deg: 5.0 - active_suspension_activation_velocity_threshold_deg: 15.0 - - # IMU attitude correction as suspension force bias. + # IMU attitude correction at min-angle stance. active_suspension_pitch_kp: 8.0 active_suspension_pitch_ki: 0.35 active_suspension_pitch_kd: 0.28 @@ -197,12 +183,9 @@ lf_joint_controller: ros__parameters: # Joint-local servo inputs produced by chassis intent generation measurement_angle: /chassis/left_front_joint/physical_angle - measurement_velocity: /chassis/left_front_joint/physical_velocity setpoint_angle: /chassis/left_front_joint/target_physical_angle - setpoint_velocity: /chassis/left_front_joint/target_physical_velocity control: /chassis/left_front_joint/control_torque - # Normal ADRC servo mode dt: 0.001 b0: -0.60 kt: 1.0 @@ -219,30 +202,11 @@ lf_joint_controller: u_max: 200.0 output_min: -200.0 output_max: 200.0 - - # Suspension ADRC servo mode - suspension_td_h: 0.001 - suspension_td_r: 12.0 - suspension_eso_w0: 80.0 - suspension_k1: 6.0 - suspension_k2: 3.0 - suspension_alpha1: 0.75 - suspension_alpha2: 0.7 - suspension_delta: 0.02 - suspension_u_min: -35.0 - suspension_u_max: 35.0 - suspension_output_min: -35.0 - suspension_output_max: 35.0 - - # Joint-local feedforward / limit shaping - torque_feedforward_gain: 0.0 lb_joint_controller: ros__parameters: # Same joint-servo layout as lf_joint_controller measurement_angle: /chassis/left_back_joint/physical_angle - measurement_velocity: /chassis/left_back_joint/physical_velocity setpoint_angle: /chassis/left_back_joint/target_physical_angle - setpoint_velocity: /chassis/left_back_joint/target_physical_velocity control: /chassis/left_back_joint/control_torque dt: 0.001 b0: -0.60 @@ -260,26 +224,11 @@ lb_joint_controller: u_max: 200.0 output_min: -200.0 output_max: 200.0 - suspension_td_h: 0.001 - suspension_td_r: 12.0 - suspension_eso_w0: 80.0 - suspension_k1: 6.0 - suspension_k2: 3.0 - suspension_alpha1: 0.75 - suspension_alpha2: 0.7 - suspension_delta: 0.02 - suspension_u_min: -35.0 - suspension_u_max: 35.0 - suspension_output_min: -35.0 - suspension_output_max: 35.0 - torque_feedforward_gain: 0.0 rb_joint_controller: ros__parameters: # Same joint-servo layout as lf_joint_controller measurement_angle: /chassis/right_back_joint/physical_angle - measurement_velocity: /chassis/right_back_joint/physical_velocity setpoint_angle: /chassis/right_back_joint/target_physical_angle - setpoint_velocity: /chassis/right_back_joint/target_physical_velocity control: /chassis/right_back_joint/control_torque dt: 0.001 b0: -0.60 @@ -297,26 +246,11 @@ rb_joint_controller: u_max: 200.0 output_min: -200.0 output_max: 200.0 - suspension_td_h: 0.001 - suspension_td_r: 12.0 - suspension_eso_w0: 80.0 - suspension_k1: 6.0 - suspension_k2: 3.0 - suspension_alpha1: 0.75 - suspension_alpha2: 0.7 - suspension_delta: 0.02 - suspension_u_min: -35.0 - suspension_u_max: 35.0 - suspension_output_min: -35.0 - suspension_output_max: 35.0 - torque_feedforward_gain: 0.0 rf_joint_controller: ros__parameters: # Same joint-servo layout as lf_joint_controller measurement_angle: /chassis/right_front_joint/physical_angle - measurement_velocity: /chassis/right_front_joint/physical_velocity setpoint_angle: /chassis/right_front_joint/target_physical_angle - setpoint_velocity: /chassis/right_front_joint/target_physical_velocity control: /chassis/right_front_joint/control_torque dt: 0.001 b0: -0.60 @@ -334,16 +268,3 @@ rf_joint_controller: u_max: 200.0 output_min: -200.0 output_max: 200.0 - suspension_td_h: 0.001 - suspension_td_r: 12.0 - suspension_eso_w0: 80.0 - suspension_k1: 6.0 - suspension_k2: 3.0 - suspension_alpha1: 0.75 - suspension_alpha2: 0.7 - suspension_delta: 0.02 - suspension_u_min: -35.0 - suspension_u_max: 35.0 - suspension_output_min: -35.0 - suspension_output_max: 35.0 - torque_feedforward_gain: 0.0 diff --git a/rmcs_ws/src/rmcs_bringup/config/deformable-infantry-steering.yaml b/rmcs_ws/src/rmcs_bringup/config/deformable-infantry-steering.yaml index 51a8fc1d..794c623b 100644 --- a/rmcs_ws/src/rmcs_bringup/config/deformable-infantry-steering.yaml +++ b/rmcs_ws/src/rmcs_bringup/config/deformable-infantry-steering.yaml @@ -178,12 +178,9 @@ lf_joint_controller: ros__parameters: # Joint-local servo inputs produced by chassis intent generation measurement_angle: /chassis/left_front_joint/physical_angle - measurement_velocity: /chassis/left_front_joint/physical_velocity setpoint_angle: /chassis/left_front_joint/target_physical_angle - setpoint_velocity: /chassis/left_front_joint/target_physical_velocity control: /chassis/left_front_joint/control_torque - # Normal ADRC servo mode dt: 0.001 b0: -1.0 kt: 1.0 @@ -200,30 +197,11 @@ lf_joint_controller: u_max: 200.0 output_min: -200.0 output_max: 200.0 - - # Suspension ADRC servo mode - suspension_td_h: 0.001 - suspension_td_r: 12.0 - suspension_eso_w0: 80.0 - suspension_k1: 6.0 - suspension_k2: 3.0 - suspension_alpha1: 0.75 - suspension_alpha2: 0.7 - suspension_delta: 0.02 - suspension_u_min: -35.0 - suspension_u_max: 35.0 - suspension_output_min: -35.0 - suspension_output_max: 35.0 - - # Joint-local feedforward / limit shaping - torque_feedforward_gain: 0.0 lb_joint_controller: ros__parameters: # Same joint-servo layout as lf_joint_controller measurement_angle: /chassis/left_back_joint/physical_angle - measurement_velocity: /chassis/left_back_joint/physical_velocity setpoint_angle: /chassis/left_back_joint/target_physical_angle - setpoint_velocity: /chassis/left_back_joint/target_physical_velocity control: /chassis/left_back_joint/control_torque dt: 0.001 b0: -1.0 @@ -241,26 +219,11 @@ lb_joint_controller: u_max: 200.0 output_min: -200.0 output_max: 200.0 - suspension_td_h: 0.001 - suspension_td_r: 12.0 - suspension_eso_w0: 80.0 - suspension_k1: 6.0 - suspension_k2: 3.0 - suspension_alpha1: 0.75 - suspension_alpha2: 0.7 - suspension_delta: 0.02 - suspension_u_min: -35.0 - suspension_u_max: 35.0 - suspension_output_min: -35.0 - suspension_output_max: 35.0 - torque_feedforward_gain: 0.0 rb_joint_controller: ros__parameters: # Same joint-servo layout as lf_joint_controller measurement_angle: /chassis/right_back_joint/physical_angle - measurement_velocity: /chassis/right_back_joint/physical_velocity setpoint_angle: /chassis/right_back_joint/target_physical_angle - setpoint_velocity: /chassis/right_back_joint/target_physical_velocity control: /chassis/right_back_joint/control_torque dt: 0.001 b0: -1.0 @@ -278,26 +241,11 @@ rb_joint_controller: u_max: 200.0 output_min: -200.0 output_max: 200.0 - suspension_td_h: 0.001 - suspension_td_r: 12.0 - suspension_eso_w0: 80.0 - suspension_k1: 6.0 - suspension_k2: 3.0 - suspension_alpha1: 0.75 - suspension_alpha2: 0.7 - suspension_delta: 0.02 - suspension_u_min: -35.0 - suspension_u_max: 35.0 - suspension_output_min: -35.0 - suspension_output_max: 35.0 - torque_feedforward_gain: 0.0 rf_joint_controller: ros__parameters: # Same joint-servo layout as lf_joint_controller measurement_angle: /chassis/right_front_joint/physical_angle - measurement_velocity: /chassis/right_front_joint/physical_velocity setpoint_angle: /chassis/right_front_joint/target_physical_angle - setpoint_velocity: /chassis/right_front_joint/target_physical_velocity control: /chassis/right_front_joint/control_torque dt: 0.001 b0: -1.0 @@ -315,16 +263,3 @@ rf_joint_controller: u_max: 200.0 output_min: -200.0 output_max: 200.0 - suspension_td_h: 0.001 - suspension_td_r: 12.0 - suspension_eso_w0: 80.0 - suspension_k1: 6.0 - suspension_k2: 3.0 - suspension_alpha1: 0.75 - suspension_alpha2: 0.7 - suspension_delta: 0.02 - suspension_u_min: -35.0 - suspension_u_max: 35.0 - suspension_output_min: -35.0 - suspension_output_max: 35.0 - torque_feedforward_gain: 0.0 diff --git a/rmcs_ws/src/rmcs_core/src/controller/chassis/deformable_chassis.cpp b/rmcs_ws/src/rmcs_core/src/controller/chassis/deformable_chassis.cpp index 10aabcfa..be49b501 100644 --- a/rmcs_ws/src/rmcs_core/src/controller/chassis/deformable_chassis.cpp +++ b/rmcs_ws/src/rmcs_core/src/controller/chassis/deformable_chassis.cpp @@ -26,7 +26,6 @@ class DeformableChassis : public rmcs_executor::Component , public rclcpp::Node { public: - enum class JointFeedbackSource : uint8_t { kLegacyEncoderAngle, kMotorAngle }; enum JointIndex : size_t { kLeftFront = 0, kLeftBack = 1, @@ -35,15 +34,6 @@ class DeformableChassis kJointCount = 4, }; - struct JointFeedbackFrame { - std::array motor_angles{}; - std::array physical_angles{}; - std::array physical_velocities{}; - std::array joint_torques{}; - std::array eso_z2{}; - std::array eso_z3{}; - }; - struct AttitudePidAxis { double kp = 20.0; double ki = 0.0; @@ -75,10 +65,6 @@ class DeformableChassis , min_angle_(get_parameter_or("min_angle", 15.0)) , max_angle_(get_parameter_or("max_angle", 55.0)) - , left_front_joint_offset_(get_parameter_or("left_front_joint_offset", 0.0)) - , left_back_joint_offset_(get_parameter_or("left_back_joint_offset", 0.0)) - , right_front_joint_offset_(get_parameter_or("right_front_joint_offset", 0.0)) - , right_back_joint_offset_(get_parameter_or("right_back_joint_offset", 0.0)) , target_physical_velocity_limit_( std::max( deg_to_rad(std::abs(get_parameter_or("target_physical_velocity_limit", 180.0))), @@ -152,11 +138,6 @@ class DeformableChassis register_input("/gimbal/yaw/angle", gimbal_yaw_angle_, false); register_input("/gimbal/yaw/control_angle_error", gimbal_yaw_angle_error_, false); - register_input("/chassis/left_front_joint/angle", left_front_joint_angle_, false); - register_input("/chassis/left_back_joint/angle", left_back_joint_angle_, false); - register_input("/chassis/right_front_joint/angle", right_front_joint_angle_, false); - register_input("/chassis/right_back_joint/angle", right_back_joint_angle_, false); - register_input( "/chassis/left_front_joint/physical_angle", left_front_joint_physical_angle_, false); register_input( @@ -165,31 +146,6 @@ class DeformableChassis "/chassis/right_front_joint/physical_angle", right_front_joint_physical_angle_, false); register_input( "/chassis/right_back_joint/physical_angle", right_back_joint_physical_angle_, false); - register_input( - "/chassis/left_front_joint/physical_velocity", left_front_joint_physical_velocity_, - false); - register_input( - "/chassis/left_back_joint/physical_velocity", left_back_joint_physical_velocity_, - false); - register_input( - "/chassis/right_back_joint/physical_velocity", right_back_joint_physical_velocity_, - false); - register_input( - "/chassis/right_front_joint/physical_velocity", right_front_joint_physical_velocity_, - false); - register_input("/chassis/left_front_joint/torque", left_front_joint_torque_, false); - register_input("/chassis/left_back_joint/torque", left_back_joint_torque_, false); - register_input("/chassis/right_back_joint/torque", right_back_joint_torque_, false); - register_input("/chassis/right_front_joint/torque", right_front_joint_torque_, false); - - register_input( - "/chassis/left_front_joint/encoder_angle", left_front_joint_encoder_angle_, false); - register_input( - "/chassis/left_back_joint/encoder_angle", left_back_joint_encoder_angle_, false); - register_input( - "/chassis/right_front_joint/encoder_angle", right_front_joint_encoder_angle_, false); - register_input( - "/chassis/right_back_joint/encoder_angle", right_back_joint_encoder_angle_, false); register_input("/chassis/imu/pitch", chassis_imu_pitch_, false); register_input("/chassis/imu/roll", chassis_imu_roll_, false); register_input("/chassis/imu/pitch_rate", chassis_imu_pitch_rate_, false); @@ -208,15 +164,6 @@ class DeformableChassis register_output("/chassis/right_front_joint/control_angle_error", rf_angle_error_, nan_); register_output("/chassis/right_back_joint/control_angle_error", rb_angle_error_, nan_); - register_output( - "/chassis/left_front_joint/target_angle", left_front_joint_target_angle_, nan_); - register_output( - "/chassis/left_back_joint/target_angle", left_back_joint_target_angle_, nan_); - register_output( - "/chassis/right_back_joint/target_angle", right_back_joint_target_angle_, nan_); - register_output( - "/chassis/right_front_joint/target_angle", right_front_joint_target_angle_, nan_); - register_output( "/chassis/left_front_joint/target_physical_angle", left_front_joint_target_physical_angle_, nan_); @@ -253,7 +200,6 @@ class DeformableChassis register_output( "/chassis/right_front_joint/target_physical_acceleration", right_front_joint_target_physical_acceleration_, nan_); - register_output("/chassis/processed_encoder/angle", processed_encoder_angle_, nan_); *mode_ = rmcs_msgs::ChassisMode::AUTO; chassis_control_velocity_->vector << nan_, nan_, nan_; @@ -263,23 +209,6 @@ class DeformableChassis lb_current_target_angle_ = max_angle_; rf_current_target_angle_ = max_angle_; rb_current_target_angle_ = max_angle_; - - const bool left_front_joint_offset = has_parameter("left_front_joint_offset"); - const bool left_back_joint_offset = has_parameter("left_back_joint_offset"); - const bool right_front_joint_offset = has_parameter("right_front_joint_offset"); - const bool right_back_joint_offset = has_parameter("right_back_joint_offset"); - - const bool has_any_joint_offset = left_front_joint_offset || left_back_joint_offset - || right_front_joint_offset || right_back_joint_offset; - const bool has_all_joint_offsets = left_front_joint_offset && left_back_joint_offset - && right_front_joint_offset && right_back_joint_offset; - if (has_any_joint_offset && !has_all_joint_offsets) - throw std::runtime_error( - "deformable chassis joint offsets must be configured for all four joints or " - "removed entirely"); - - joint_feedback_source_ = has_all_joint_offsets ? JointFeedbackSource::kLegacyEncoderAngle - : JointFeedbackSource::kMotorAngle; } void before_updating() override { @@ -292,14 +221,6 @@ class DeformableChassis RCLCPP_WARN( get_logger(), "Failed to fetch \"/gimbal/yaw/control_angle_error\". Set to 0.0."); } - if (!left_front_joint_torque_.ready()) - left_front_joint_torque_.make_and_bind_directly(0.0); - if (!left_back_joint_torque_.ready()) - left_back_joint_torque_.make_and_bind_directly(0.0); - if (!right_back_joint_torque_.ready()) - right_back_joint_torque_.make_and_bind_directly(0.0); - if (!right_front_joint_torque_.ready()) - right_front_joint_torque_.make_and_bind_directly(0.0); if (!chassis_imu_pitch_.ready()) chassis_imu_pitch_.make_and_bind_directly(0.0); if (!chassis_imu_roll_.ready()) @@ -338,48 +259,18 @@ class DeformableChassis } private: - static constexpr double inf_ = std::numeric_limits::infinity(); static constexpr double nan_ = std::numeric_limits::quiet_NaN(); static constexpr double translational_velocity_max_ = 10.0; static constexpr double angular_velocity_max_ = 30.0; static constexpr double rad_to_deg_ = 180.0 / std::numbers::pi; - static double wrap_deg(double deg) { - deg = std::fmod(deg, 360.0); - if (deg >= 180.0) - deg -= 360.0; - if (deg < -180.0) - deg += 360.0; - return deg; - } - void validate_joint_feedback_inputs() const { - const bool ready = - joint_feedback_source_ == JointFeedbackSource::kMotorAngle - ? left_front_joint_angle_.ready() && left_back_joint_angle_.ready() - && right_front_joint_angle_.ready() && right_back_joint_angle_.ready() - : left_front_joint_encoder_angle_.ready() && left_back_joint_encoder_angle_.ready() - && right_front_joint_encoder_angle_.ready() - && right_back_joint_encoder_angle_.ready(); - - if (ready) + if (left_front_joint_physical_angle_.ready() && left_back_joint_physical_angle_.ready() + && right_front_joint_physical_angle_.ready() && right_back_joint_physical_angle_.ready()) return; throw std::runtime_error( - joint_feedback_source_ == JointFeedbackSource::kMotorAngle - ? "missing V2 joint feedback interfaces: expected /chassis/*_joint/angle" - : "missing legacy joint feedback interfaces: expected /chassis/*_joint/" - "encoder_angle"); - } - - double joint_angle_deg( - const InputInterface& joint_angle, - const InputInterface& joint_encoder_angle, double joint_offset, - double legacy_fixed_compensation) const { - if (joint_feedback_source_ == JointFeedbackSource::kMotorAngle) - return wrap_deg(*joint_angle * rad_to_deg_); - - return wrap_deg(joint_offset) - wrap_deg(*joint_encoder_angle) + legacy_fixed_compensation; + "missing deformable chassis feedback interfaces: expected /chassis/*_joint/physical_angle"); } void update_mode_from_inputs_( @@ -416,13 +307,20 @@ class DeformableChassis *mode_ = mode; } - // JointFeedbackAdapter: normalize motor / physical / legacy encoder feedback into one frame. - JointFeedbackFrame read_joint_feedback_frame_() const { - JointFeedbackFrame joint_feedback; - update_current_joint_feedback( - joint_feedback.motor_angles, joint_feedback.physical_angles, - joint_feedback.physical_velocities, joint_feedback.joint_torques); - return joint_feedback; + std::array read_current_joint_physical_angles_() const { + const std::array*, kJointCount> physical_angle_inputs{ + &left_front_joint_physical_angle_, &left_back_joint_physical_angle_, + &right_back_joint_physical_angle_, &right_front_joint_physical_angle_}; + + std::array current_physical_angles{}; + current_physical_angles.fill(nan_); + for (size_t i = 0; i < kJointCount; ++i) { + if (physical_angle_inputs[i]->ready() && std::isfinite(*(*physical_angle_inputs[i]))) { + current_physical_angles[i] = *(*physical_angle_inputs[i]); + } + } + + return current_physical_angles; } bool prone_override_requested_by_keyboard() const { return keyboard_.ready() && keyboard_->ctrl; } @@ -527,57 +425,13 @@ class DeformableChassis chassis_imu_calibration_sample_count_); } - void update_current_joint_feedback( - std::array& current_motor_angles, - std::array& current_physical_angles, - std::array& current_physical_velocities, - std::array& current_joint_torques) const { - const std::array*, kJointCount> motor_angle_inputs{ - &left_front_joint_angle_, &left_back_joint_angle_, &right_back_joint_angle_, - &right_front_joint_angle_}; - const std::array*, kJointCount> physical_angle_inputs{ - &left_front_joint_physical_angle_, &left_back_joint_physical_angle_, - &right_back_joint_physical_angle_, &right_front_joint_physical_angle_}; - const std::array*, kJointCount> physical_velocity_inputs{ - &left_front_joint_physical_velocity_, &left_back_joint_physical_velocity_, - &right_back_joint_physical_velocity_, &right_front_joint_physical_velocity_}; - const std::array*, kJointCount> torque_inputs{ - &left_front_joint_torque_, &left_back_joint_torque_, &right_back_joint_torque_, - &right_front_joint_torque_}; - current_motor_angles.fill(nan_); - current_physical_angles.fill(nan_); - current_physical_velocities.fill(nan_); - current_joint_torques.fill(nan_); - - for (size_t i = 0; i < kJointCount; ++i) { - if (motor_angle_inputs[i]->ready() && std::isfinite(*(*motor_angle_inputs[i]))) { - current_motor_angles[i] = *(*motor_angle_inputs[i]); - current_physical_angles[i] = motor_to_physical_angle(current_motor_angles[i]); - } - - if (physical_angle_inputs[i]->ready() && std::isfinite(*(*physical_angle_inputs[i]))) { - current_physical_angles[i] = *(*physical_angle_inputs[i]); - } - if (physical_velocity_inputs[i]->ready() - && std::isfinite(*(*physical_velocity_inputs[i]))) { - current_physical_velocities[i] = *(*physical_velocity_inputs[i]); - } - if (torque_inputs[i]->ready() && std::isfinite(*(*torque_inputs[i]))) { - current_joint_torques[i] = *(*torque_inputs[i]); - } - } - } - bool initialize_joint_target_states_from_feedback( - const std::array& current_motor_angles, const std::array& current_physical_angles) { for (size_t i = 0; i < kJointCount; ++i) { - if (!std::isfinite(current_motor_angles[i]) - || !std::isfinite(current_physical_angles[i])) + if (!std::isfinite(current_physical_angles[i])) return false; } - joint_target_angle_state_rad_ = current_motor_angles; joint_target_physical_angle_state_rad_ = current_physical_angles; joint_target_physical_velocity_state_rad_ = {0.0, 0.0, 0.0, 0.0}; joint_target_physical_acceleration_state_rad_ = {0.0, 0.0, 0.0, 0.0}; @@ -587,7 +441,7 @@ class DeformableChassis return true; } - void update_active_suspension_(const JointFeedbackFrame&) { + void update_active_suspension_() { if (!suspension_requested_by_input_()) { reset_attitude_correction_state_(); return; @@ -662,11 +516,6 @@ class DeformableChassis *rf_angle_error_ = nan_; *rb_angle_error_ = nan_; - *left_front_joint_target_angle_ = nan_; - *left_back_joint_target_angle_ = nan_; - *right_back_joint_target_angle_ = nan_; - *right_front_joint_target_angle_ = nan_; - *left_front_joint_target_physical_angle_ = nan_; *left_back_joint_target_physical_angle_ = nan_; *right_back_joint_target_physical_angle_ = nan_; @@ -680,8 +529,6 @@ class DeformableChassis *right_back_joint_target_physical_acceleration_ = nan_; *right_front_joint_target_physical_acceleration_ = nan_; - *processed_encoder_angle_ = nan_; - } void update_velocity_control() { @@ -811,11 +658,10 @@ class DeformableChassis // Chassis owns the high-level joint intent pipeline: read feedback, generate deploy targets, // coordinate suspension overrides, then publish the resulting joint intent for the servo layer. void run_joint_intent_pipeline_() { - const auto joint_feedback = read_joint_feedback_frame_(); + const auto current_physical_angles = read_current_joint_physical_angles_(); if (!joint_target_active_ - && !initialize_joint_target_states_from_feedback( - joint_feedback.motor_angles, joint_feedback.physical_angles)) { + && !initialize_joint_target_states_from_feedback(current_physical_angles)) { publish_nan_joint_targets(); return; } @@ -823,22 +669,14 @@ class DeformableChassis update_chassis_imu_calibration_(); const bool prone_override = refresh_requested_joint_targets_from_deploy_state_(); scope_motor_control(prone_override); - update_active_suspension_(joint_feedback); + update_active_suspension_(); update_joint_target_trajectory(); - publish_joint_target_angles(joint_feedback.physical_angles); + publish_joint_target_angles(current_physical_angles); } static double deg_to_rad(double deg) { return deg * std::numbers::pi / 180.0; } - static double physical_to_motor_angle(double physical_angle_rad) { - return joint_zero_physical_angle_rad_ - physical_angle_rad; - } - - static double motor_to_physical_angle(double motor_angle_rad) { - return joint_zero_physical_angle_rad_ - motor_angle_rad; - } - void scope_motor_control(bool prone_override = false) { const bool prone_target_active = prone_override; if (prone_target_active && *mode_ != rmcs_msgs::ChassisMode::SPIN) { @@ -854,31 +692,6 @@ class DeformableChassis } } - bool publish_current_joint_target_angles() { - const std::array*, kJointCount> motor_angle_inputs{ - &left_front_joint_angle_, &left_back_joint_angle_, &right_back_joint_angle_, - &right_front_joint_angle_}; - - std::array current_motor_angles{}; - std::array current_physical_angles{}; - for (size_t i = 0; i < kJointCount; ++i) { - if (!motor_angle_inputs[i]->ready() || !std::isfinite(*(*motor_angle_inputs[i]))) { - return false; - } - current_motor_angles[i] = *(*motor_angle_inputs[i]); - current_physical_angles[i] = motor_to_physical_angle(current_motor_angles[i]); - } - - joint_target_angle_state_rad_ = current_motor_angles; - joint_target_physical_angle_state_rad_ = current_physical_angles; - joint_target_physical_velocity_state_rad_ = {0.0, 0.0, 0.0, 0.0}; - joint_target_physical_acceleration_state_rad_ = {0.0, 0.0, 0.0, 0.0}; - requested_target_physical_angles_rad_ = current_physical_angles; - current_target_physical_angles_rad_ = current_physical_angles; - joint_target_active_ = true; - return true; - } - void update_joint_target_trajectory() { const double dt = update_dt(); for (size_t i = 0; i < kJointCount; ++i) { @@ -922,8 +735,6 @@ class DeformableChassis velocity_state = 0.0; acceleration_state = 0.0; } - - joint_target_angle_state_rad_[i] = physical_to_motor_angle(angle_state); } } @@ -934,11 +745,6 @@ class DeformableChassis return; } - *left_front_joint_target_angle_ = joint_target_angle_state_rad_[kLeftFront]; - *left_back_joint_target_angle_ = joint_target_angle_state_rad_[kLeftBack]; - *right_back_joint_target_angle_ = joint_target_angle_state_rad_[kRightBack]; - *right_front_joint_target_angle_ = joint_target_angle_state_rad_[kRightFront]; - *left_front_joint_target_physical_angle_ = joint_target_physical_angle_state_rad_[kLeftFront]; *left_back_joint_target_physical_angle_ = joint_target_physical_angle_state_rad_[kLeftBack]; @@ -981,30 +787,11 @@ class DeformableChassis ? current_physical_angles[kRightFront] - joint_target_physical_angle_state_rad_[kRightFront] : nan_; - - bool all_joint_angles_finite = true; - double physical_angle_sum = 0.0; - for (double current_physical_angle : current_physical_angles) { - if (!std::isfinite(current_physical_angle)) { - all_joint_angles_finite = false; - break; - } - physical_angle_sum += current_physical_angle; - } - - *processed_encoder_angle_ = all_joint_angles_finite ? rad_to_deg_ * physical_angle_sum - / static_cast(kJointCount) - : nan_; } void publish_nan_joint_targets() { reset_attitude_correction_state_(); - *left_front_joint_target_angle_ = nan_; - *left_back_joint_target_angle_ = nan_; - *right_back_joint_target_angle_ = nan_; - *right_front_joint_target_angle_ = nan_; - *left_front_joint_target_physical_angle_ = nan_; *left_back_joint_target_physical_angle_ = nan_; *right_back_joint_target_physical_angle_ = nan_; @@ -1054,28 +841,10 @@ class DeformableChassis pid::PidCalculator following_velocity_controller_; const double spin_ratio_; - InputInterface left_front_joint_angle_; - InputInterface left_back_joint_angle_; - InputInterface right_front_joint_angle_; - InputInterface right_back_joint_angle_; - InputInterface left_front_joint_physical_angle_; InputInterface left_back_joint_physical_angle_; InputInterface right_front_joint_physical_angle_; InputInterface right_back_joint_physical_angle_; - InputInterface left_front_joint_physical_velocity_; - InputInterface left_back_joint_physical_velocity_; - InputInterface right_front_joint_physical_velocity_; - InputInterface right_back_joint_physical_velocity_; - InputInterface left_front_joint_torque_; - InputInterface left_back_joint_torque_; - InputInterface right_front_joint_torque_; - InputInterface right_back_joint_torque_; - - InputInterface left_front_joint_encoder_angle_; - InputInterface left_back_joint_encoder_angle_; - InputInterface right_front_joint_encoder_angle_; - InputInterface right_back_joint_encoder_angle_; InputInterface chassis_imu_pitch_; InputInterface chassis_imu_roll_; InputInterface chassis_imu_pitch_rate_; @@ -1088,11 +857,6 @@ class DeformableChassis OutputInterface rf_angle_error_; OutputInterface rb_angle_error_; - OutputInterface left_front_joint_target_angle_; - OutputInterface left_back_joint_target_angle_; - OutputInterface right_back_joint_target_angle_; - OutputInterface right_front_joint_target_angle_; - OutputInterface left_front_joint_target_physical_angle_; OutputInterface left_back_joint_target_physical_angle_; OutputInterface right_back_joint_target_physical_angle_; @@ -1106,15 +870,8 @@ class DeformableChassis OutputInterface right_back_joint_target_physical_acceleration_; OutputInterface right_front_joint_target_physical_acceleration_; - OutputInterface processed_encoder_angle_; - double min_angle_; double max_angle_; - double left_front_joint_offset_; - double left_back_joint_offset_; - double right_front_joint_offset_; - double right_back_joint_offset_; - JointFeedbackSource joint_feedback_source_ = JointFeedbackSource::kLegacyEncoderAngle; double current_target_angle_; double lf_current_target_angle_, lb_current_target_angle_, rb_current_target_angle_, @@ -1124,7 +881,6 @@ class DeformableChassis std::array current_target_physical_angles_rad_ = {0.0, 0.0, 0.0, 0.0}; bool joint_target_active_ = false; - std::array joint_target_angle_state_rad_ = {0.0, 0.0, 0.0, 0.0}; std::array joint_target_physical_angle_state_rad_ = {0.0, 0.0, 0.0, 0.0}; std::array joint_target_physical_velocity_state_rad_ = { 0.0, 0.0, 0.0, 0.0}; @@ -1159,9 +915,6 @@ class DeformableChassis double chassis_imu_roll_sum_ = 0.0; bool chassis_imu_calibration_completed_for_window_ = false; static constexpr double default_dt_ = 1e-3; - static constexpr double joint_zero_physical_angle_rad_ = 1.090830782496456; - - static constexpr double pi_ = std::numbers::pi; }; } // namespace rmcs_core::controller::chassis diff --git a/rmcs_ws/src/rmcs_core/src/controller/chassis/deformable_joint_controller.cpp b/rmcs_ws/src/rmcs_core/src/controller/chassis/deformable_joint_controller.cpp index a629449c..14623175 100644 --- a/rmcs_ws/src/rmcs_core/src/controller/chassis/deformable_joint_controller.cpp +++ b/rmcs_ws/src/rmcs_core/src/controller/chassis/deformable_joint_controller.cpp @@ -2,7 +2,6 @@ #include #include #include -#include #include #include @@ -33,22 +32,19 @@ class DeformableJointController : public rmcs_executor::Component , public rclcpp::Node { public: - // Joint controller owns only joint-local servo execution. Higher-level deploy/suspension - // intent is generated upstream by DeformableChassis via setpoint/mode/feedforward inputs. - struct ModeConfig { + // Joint controller owns only the local angle-servo execution. Chassis publishes the + // higher-level target angle trajectory; this controller turns that target into motor torque. + struct ControllerConfig { rmcs_core::controller::adrc::TD::Config td; rmcs_core::controller::adrc::ESO::Config eso; rmcs_core::controller::adrc::NLESF::Config nlesf; double output_min = -std::numeric_limits::infinity(); double output_max = std::numeric_limits::infinity(); - double torque_feedforward_gain = 0.0; }; struct InputSnapshot { double measurement_angle = std::numeric_limits::quiet_NaN(); double setpoint_angle = std::numeric_limits::quiet_NaN(); - double joint_torque_feedforward = 0.0; - bool suspension_mode = false; }; DeformableJointController() @@ -56,8 +52,8 @@ class DeformableJointController get_component_name(), rclcpp::NodeOptions{}.automatically_declare_parameters_from_overrides(true)) { register_interfaces_(); - load_mode_configs_(); - apply_mode_config_(normal_mode_config_); + load_config_(); + apply_config_(); } void update() override { @@ -67,208 +63,74 @@ class DeformableJointController return; } - update_mode_selection_(inputs.suspension_mode); - const auto& mode_config = active_mode_config_(inputs.suspension_mode); - const auto [output_min, output_max] = effective_output_limits_(mode_config); - if (std::isnan(output_min) || std::isnan(output_max) || output_min > output_max) { - disable_output_(); - return; - } - - if (!ensure_feedforward_ready_(mode_config, inputs)) { - disable_output_(); - return; - } - initialize_if_needed_(inputs); - rmcs_core::controller::adrc::ESO::Output eso_out; double control_torque = nan_; - if (!run_joint_servo_( - inputs, mode_config, output_min, output_max, eso_out, control_torque)) { + if (!run_joint_servo_(inputs, control_torque)) { disable_output_(); return; } - publish_control_output_(control_torque, eso_out); + publish_control_output_(control_torque); } private: void register_interfaces_() { register_input(get_parameter("measurement_angle").as_string(), measurement_angle_); - register_input(get_parameter("measurement_velocity").as_string(), measurement_velocity_); register_input(get_parameter("setpoint_angle").as_string(), setpoint_angle_); - - if (has_parameter("setpoint_velocity")) { - register_input( - get_parameter("setpoint_velocity").as_string(), setpoint_velocity_input_); - } else { - setpoint_velocity_input_.make_and_bind_directly(0.0); - } - - if (has_parameter("mode_input")) { - register_input(get_parameter("mode_input").as_string(), suspension_mode_input_); - } else { - suspension_mode_input_.make_and_bind_directly(false); - } - - if (has_parameter("suspension_torque")) { - register_input( - get_parameter("suspension_torque").as_string(), joint_torque_feedforward_input_); - } else { - joint_torque_feedforward_input_.make_and_bind_directly(0.0); - } - if (has_parameter("limit")) { - register_input(get_parameter("limit").as_string(), output_limit_); - use_dynamic_limit_ = true; - } - register_output(get_parameter("control").as_string(), control_torque_, nan_); - if (has_parameter("eso_z2_output")) { - register_output(get_parameter("eso_z2_output").as_string(), eso_z2_output_, nan_); - } - if (has_parameter("eso_z3_output")) { - register_output(get_parameter("eso_z3_output").as_string(), eso_z3_output_, nan_); - } } - void load_mode_configs_() { + void load_config_() { dt_ = load_parameter_or(*this, "dt", 0.001); b0_ = load_parameter_or(*this, "b0", 1.0); kt_ = load_parameter_or(*this, "kt", 1.0); - normal_mode_config_.td.h = load_parameter_or(*this, "td_h", dt_); - normal_mode_config_.td.r = load_parameter_or(*this, "td_r", 300.0); - normal_mode_config_.td.max_vel = + config_.td.h = load_parameter_or(*this, "td_h", dt_); + config_.td.r = load_parameter_or(*this, "td_r", 300.0); + config_.td.max_vel = load_parameter_or(*this, "td_max_vel", std::numeric_limits::infinity()); - normal_mode_config_.td.max_acc = + config_.td.max_acc = load_parameter_or(*this, "td_max_acc", std::numeric_limits::infinity()); - normal_mode_config_.eso.h = dt_; - normal_mode_config_.eso.b0 = b0_; - normal_mode_config_.eso.w0 = load_parameter_or(*this, "eso_w0", 80.0); - normal_mode_config_.eso.auto_beta = load_parameter_or(*this, "eso_auto_beta", true); - normal_mode_config_.eso.beta1 = - load_parameter_or(*this, "eso_beta1", 3.0 * normal_mode_config_.eso.w0); - normal_mode_config_.eso.beta2 = load_parameter_or( - *this, "eso_beta2", 3.0 * normal_mode_config_.eso.w0 * normal_mode_config_.eso.w0); - normal_mode_config_.eso.beta3 = load_parameter_or( - *this, "eso_beta3", - normal_mode_config_.eso.w0 * normal_mode_config_.eso.w0 * normal_mode_config_.eso.w0); - normal_mode_config_.eso.z3_limit = load_parameter_or(*this, "eso_z3_limit", 1e9); - - normal_mode_config_.nlesf.k1 = load_parameter_or(*this, "k1", 50.0); - normal_mode_config_.nlesf.k2 = load_parameter_or(*this, "k2", 5.0); - normal_mode_config_.nlesf.alpha1 = load_parameter_or(*this, "alpha1", 0.75); - normal_mode_config_.nlesf.alpha2 = load_parameter_or(*this, "alpha2", 1.25); - normal_mode_config_.nlesf.delta = load_parameter_or(*this, "delta", 0.01); - normal_mode_config_.nlesf.u_min = + config_.eso.h = dt_; + config_.eso.b0 = b0_; + config_.eso.w0 = load_parameter_or(*this, "eso_w0", 80.0); + config_.eso.auto_beta = load_parameter_or(*this, "eso_auto_beta", true); + config_.eso.beta1 = load_parameter_or(*this, "eso_beta1", 3.0 * config_.eso.w0); + config_.eso.beta2 = + load_parameter_or(*this, "eso_beta2", 3.0 * config_.eso.w0 * config_.eso.w0); + config_.eso.beta3 = load_parameter_or( + *this, "eso_beta3", config_.eso.w0 * config_.eso.w0 * config_.eso.w0); + config_.eso.z3_limit = load_parameter_or(*this, "eso_z3_limit", 1e9); + + config_.nlesf.k1 = load_parameter_or(*this, "k1", 50.0); + config_.nlesf.k2 = load_parameter_or(*this, "k2", 5.0); + config_.nlesf.alpha1 = load_parameter_or(*this, "alpha1", 0.75); + config_.nlesf.alpha2 = load_parameter_or(*this, "alpha2", 1.25); + config_.nlesf.delta = load_parameter_or(*this, "delta", 0.01); + config_.nlesf.u_min = load_parameter_or(*this, "u_min", -std::numeric_limits::infinity()); - normal_mode_config_.nlesf.u_max = + config_.nlesf.u_max = load_parameter_or(*this, "u_max", std::numeric_limits::infinity()); - normal_mode_config_.output_min = + config_.output_min = load_parameter_or(*this, "output_min", -std::numeric_limits::infinity()); - normal_mode_config_.output_max = + config_.output_max = load_parameter_or(*this, "output_max", std::numeric_limits::infinity()); - if (normal_mode_config_.output_min > normal_mode_config_.output_max) { - std::swap(normal_mode_config_.output_min, normal_mode_config_.output_max); + if (config_.output_min > config_.output_max) { + std::swap(config_.output_min, config_.output_max); } - normal_mode_config_.torque_feedforward_gain = - load_parameter_or(*this, "torque_feedforward_gain", 0.0); - - suspension_mode_config_ = normal_mode_config_; - suspension_mode_config_.td.h = - load_parameter_or(*this, "suspension_td_h", suspension_mode_config_.td.h); - suspension_mode_config_.td.r = - load_parameter_or(*this, "suspension_td_r", suspension_mode_config_.td.r); - suspension_mode_config_.td.max_vel = - load_parameter_or(*this, "suspension_td_max_vel", suspension_mode_config_.td.max_vel); - suspension_mode_config_.td.max_acc = - load_parameter_or(*this, "suspension_td_max_acc", suspension_mode_config_.td.max_acc); - suspension_mode_config_.eso.w0 = - load_parameter_or(*this, "suspension_eso_w0", suspension_mode_config_.eso.w0); - suspension_mode_config_.eso.auto_beta = load_parameter_or( - *this, "suspension_eso_auto_beta", suspension_mode_config_.eso.auto_beta); - suspension_mode_config_.eso.beta1 = - load_parameter_or(*this, "suspension_eso_beta1", suspension_mode_config_.eso.beta1); - suspension_mode_config_.eso.beta2 = - load_parameter_or(*this, "suspension_eso_beta2", suspension_mode_config_.eso.beta2); - suspension_mode_config_.eso.beta3 = - load_parameter_or(*this, "suspension_eso_beta3", suspension_mode_config_.eso.beta3); - suspension_mode_config_.eso.z3_limit = load_parameter_or( - *this, "suspension_eso_z3_limit", suspension_mode_config_.eso.z3_limit); - suspension_mode_config_.nlesf.k1 = - load_parameter_or(*this, "suspension_k1", suspension_mode_config_.nlesf.k1); - suspension_mode_config_.nlesf.k2 = - load_parameter_or(*this, "suspension_k2", suspension_mode_config_.nlesf.k2); - suspension_mode_config_.nlesf.alpha1 = - load_parameter_or(*this, "suspension_alpha1", suspension_mode_config_.nlesf.alpha1); - suspension_mode_config_.nlesf.alpha2 = - load_parameter_or(*this, "suspension_alpha2", suspension_mode_config_.nlesf.alpha2); - suspension_mode_config_.nlesf.delta = - load_parameter_or(*this, "suspension_delta", suspension_mode_config_.nlesf.delta); - suspension_mode_config_.nlesf.u_min = - load_parameter_or(*this, "suspension_u_min", suspension_mode_config_.nlesf.u_min); - suspension_mode_config_.nlesf.u_max = - load_parameter_or(*this, "suspension_u_max", suspension_mode_config_.nlesf.u_max); - suspension_mode_config_.output_min = - load_parameter_or(*this, "suspension_output_min", normal_mode_config_.output_min); - suspension_mode_config_.output_max = - load_parameter_or(*this, "suspension_output_max", normal_mode_config_.output_max); - if (suspension_mode_config_.output_min > suspension_mode_config_.output_max) { - std::swap(suspension_mode_config_.output_min, suspension_mode_config_.output_max); - } - suspension_mode_config_.torque_feedforward_gain = - load_parameter_or(*this, "suspension_torque_feedforward_gain", 1.0); } bool read_inputs_(InputSnapshot& inputs) const { inputs.measurement_angle = *measurement_angle_; inputs.setpoint_angle = *setpoint_angle_; - inputs.joint_torque_feedforward = *joint_torque_feedforward_input_; - inputs.suspension_mode = *suspension_mode_input_; return std::isfinite(inputs.measurement_angle) && std::isfinite(inputs.setpoint_angle); } - void update_mode_selection_(bool suspension_mode) { - if (suspension_mode != last_suspension_mode_) { - apply_mode_config_(active_mode_config_(suspension_mode)); - last_suspension_mode_ = suspension_mode; - } - } - - const ModeConfig& active_mode_config_(bool suspension_mode) const { - return suspension_mode ? suspension_mode_config_ : normal_mode_config_; - } - - void apply_mode_config_(const ModeConfig& mode_config) { - td_.set_config(mode_config.td); - eso_.set_config(mode_config.eso); - nlesf_.set_config(mode_config.nlesf); - } - - std::pair effective_output_limits_(const ModeConfig& mode_config) const { - double output_min = mode_config.output_min; - double output_max = mode_config.output_max; - if (use_dynamic_limit_) { - if (!output_limit_.ready()) { - return {nan_, nan_}; - } - const double limit = *output_limit_; - if (!std::isfinite(limit)) { - return {nan_, nan_}; - } - - const double effective_limit = std::max(0.0, limit); - output_min = std::max(output_min, -effective_limit); - output_max = std::min(output_max, effective_limit); - } - return {output_min, output_max}; - } - - bool ensure_feedforward_ready_( - const ModeConfig& mode_config, const InputSnapshot& inputs) const { - return mode_config.torque_feedforward_gain == 0.0 - || std::isfinite(inputs.joint_torque_feedforward); + void apply_config_() { + td_.set_config(config_.td); + eso_.set_config(config_.eso); + nlesf_.set_config(config_.nlesf); } void initialize_if_needed_(const InputSnapshot& inputs) { @@ -279,21 +141,15 @@ class DeformableJointController initialized_ = true; } - bool run_joint_servo_( - const InputSnapshot& inputs, const ModeConfig& mode_config, double output_min, - double output_max, rmcs_core::controller::adrc::ESO::Output& eso_out, - double& control_torque) { + bool run_joint_servo_(const InputSnapshot& inputs, double& control_torque) { const auto td_out = td_.update(inputs.setpoint_angle); - eso_out = eso_.update(inputs.measurement_angle, last_u_); + const auto eso_out = eso_.update(inputs.measurement_angle, last_u_); const double e1 = td_out.x1 - eso_out.z1; const double e2 = td_out.x2 - eso_out.z2; control_torque = kt_ * nlesf_.compute(e1, e2, eso_out.z3, b0_).u; - if (mode_config.torque_feedforward_gain != 0.0) { - control_torque += mode_config.torque_feedforward_gain * inputs.joint_torque_feedforward; - } - control_torque = std::clamp(control_torque, output_min, output_max); + control_torque = std::clamp(control_torque, config_.output_min, config_.output_max); return std::isfinite(control_torque); } @@ -303,63 +159,35 @@ class DeformableJointController last_u_ = 0.0; } - void publish_control_output_( - double control_torque, const rmcs_core::controller::adrc::ESO::Output& eso_out) { + void publish_control_output_(double control_torque) { *control_torque_ = control_torque; last_u_ = control_torque; - publish_eso_state_(eso_out); - } - - void publish_eso_state_(const rmcs_core::controller::adrc::ESO::Output& eso_out) { - if (eso_z2_output_.active()) { - *eso_z2_output_ = eso_out.z2; - } - if (eso_z3_output_.active()) { - *eso_z3_output_ = eso_out.z3; - } } void disable_output_() { initialized_ = false; last_u_ = 0.0; *control_torque_ = nan_; - if (eso_z2_output_.active()) { - *eso_z2_output_ = nan_; - } - if (eso_z3_output_.active()) { - *eso_z3_output_ = nan_; - } } static constexpr double nan_ = std::numeric_limits::quiet_NaN(); InputInterface measurement_angle_; - InputInterface measurement_velocity_; InputInterface setpoint_angle_; - // Kept for graph compatibility; TD derives the actual servo-rate target locally. - InputInterface setpoint_velocity_input_; - InputInterface suspension_mode_input_; - InputInterface joint_torque_feedforward_input_; - InputInterface output_limit_; OutputInterface control_torque_; - OutputInterface eso_z2_output_; - OutputInterface eso_z3_output_; rmcs_core::controller::adrc::TD td_; rmcs_core::controller::adrc::ESO eso_; rmcs_core::controller::adrc::NLESF nlesf_; - ModeConfig normal_mode_config_; - ModeConfig suspension_mode_config_; + ControllerConfig config_; double dt_ = 0.001; double b0_ = 1.0; double kt_ = 1.0; double last_u_ = 0.0; - bool use_dynamic_limit_ = false; bool initialized_ = false; - bool last_suspension_mode_ = false; }; } // namespace rmcs_core::controller::chassis From fdd0ae0ad00756dbc2c95b74d19d0cc8521c3e5c Mon Sep 17 00:00:00 2001 From: eye-on Date: Tue, 12 May 2026 02:39:21 +0800 Subject: [PATCH 10/52] feat:supercup fixed v2 --- .../src/controller/chassis/chassis_power_controller.cpp | 5 +++-- .../src/rmcs_core/src/hardware/deformable-infantry-omni.cpp | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/rmcs_ws/src/rmcs_core/src/controller/chassis/chassis_power_controller.cpp b/rmcs_ws/src/rmcs_core/src/controller/chassis/chassis_power_controller.cpp index 2d9fa58b..a014d717 100644 --- a/rmcs_ws/src/rmcs_core/src/controller/chassis/chassis_power_controller.cpp +++ b/rmcs_ws/src/rmcs_core/src/controller/chassis/chassis_power_controller.cpp @@ -109,8 +109,9 @@ class ChassisPowerController void update_control_power_limit() { double power_limit; + const bool supercap_active = *supercap_control_enabled_ && *supercap_enabled_; - if (*supercap_control_enabled_ && *supercap_enabled_) + if (supercap_active) power_limit = *mode_ == rmcs_msgs::ChassisMode::LAUNCH_RAMP ? inf_ : *chassis_power_limit_referee_ + 80.0; @@ -129,7 +130,7 @@ class ChassisPowerController 0.0, 1.0); // Maximum excess power when virtual buffer energy is full. - constexpr double excess_power_limit = 15; + const double excess_power_limit = supercap_active ? 15.0 : 0.0; power_limit += excess_power_limit; power_limit *= virtual_buffer_energy_ / virtual_buffer_energy_limit_; diff --git a/rmcs_ws/src/rmcs_core/src/hardware/deformable-infantry-omni.cpp b/rmcs_ws/src/rmcs_core/src/hardware/deformable-infantry-omni.cpp index 6a80eff4..561c22c0 100644 --- a/rmcs_ws/src/rmcs_core/src/hardware/deformable-infantry-omni.cpp +++ b/rmcs_ws/src/rmcs_core/src/hardware/deformable-infantry-omni.cpp @@ -123,7 +123,7 @@ class DeformableInfantryOmni for (auto& motor : chassis_wheel_motors_) motor.configure( device::DjiMotor::Config{device::DjiMotor::Type::kM3508} - .set_reduction_ratio(11.0) + .set_reduction_ratio(13.0) .enable_multi_turn_angle() .set_reversed()); From 2faf36ea9a83c0116cd6362b51fa54d2a95555c5 Mon Sep 17 00:00:00 2001 From: ZGZ713912 Date: Tue, 12 May 2026 12:36:11 +0800 Subject: [PATCH 11/52] feat: update build scripts and configurations for cross-compilation support --- .gitignore | 11 +++++++---- .script/build-rmcs-cross | 9 +++++++++ .script/clean-rmcs | 8 +++++++- .../config/deformable-infantry-omni-b.yaml | 2 +- rmcs_ws/src/rmcs_bringup/package.xml | 4 ++-- 5 files changed, 26 insertions(+), 8 deletions(-) diff --git a/.gitignore b/.gitignore index 05b12839..de98c474 100644 --- a/.gitignore +++ b/.gitignore @@ -1,7 +1,10 @@ -devel/ -log/ -build/ -install/ +/devel +/log +/build +/install +/rmcs_ws/log +/rmcs_ws/build +/rmcs_ws/install log-cross-*/ build-cross-*/ install-cross-*/ diff --git a/.script/build-rmcs-cross b/.script/build-rmcs-cross index 488054da..e9282b28 100755 --- a/.script/build-rmcs-cross +++ b/.script/build-rmcs-cross @@ -13,12 +13,16 @@ Examples: build-rmcs-cross --target-arch arm64 build-rmcs-cross --target-arch arm64 --link-default build-rmcs-cross --target-arch amd64 --packages-up-to rmcs_executor + +Notes: + arm64 cross builds automatically skip 'hikcamera' and 'rmcs_auto_aim_v2'. EOF } target_arch="" link_default=0 colcon_args=() +auto_skip_packages=() while (($# > 0)); do case "$1" in @@ -59,6 +63,7 @@ fi case "${target_arch}" in arm64) target_triplet="aarch64-linux-gnu" + auto_skip_packages=(hikcamera rmcs_auto_aim_v2) ;; amd64) target_triplet="x86_64-linux-gnu" @@ -152,6 +157,10 @@ export PKG_CONFIG_LIBDIR="${RMCS_SYSROOT}/usr/local/lib/${RMCS_TARGET_TRIPLET}/p cd "${workspace}" +if ((${#auto_skip_packages[@]} > 0)); then + colcon_args+=(--packages-skip "${auto_skip_packages[@]}") +fi + build_base="build-cross-${RMCS_TARGET_ARCH}" install_base="install-cross-${RMCS_TARGET_ARCH}" log_base="log-cross-${RMCS_TARGET_ARCH}" diff --git a/.script/clean-rmcs b/.script/clean-rmcs index 4c1aa6a6..f690e9e6 100755 --- a/.script/clean-rmcs +++ b/.script/clean-rmcs @@ -10,4 +10,10 @@ fi rm -rf -- \ "${RMCS_PATH}/rmcs_ws/build" \ "${RMCS_PATH}/rmcs_ws/install" \ - "${RMCS_PATH}/rmcs_ws/log" + "${RMCS_PATH}/rmcs_ws/log" \ + "${RMCS_PATH}/rmcs_ws/build-cross-arm64" \ + "${RMCS_PATH}/rmcs_ws/install-cross-arm64" \ + "${RMCS_PATH}/rmcs_ws/log-cross-arm64" \ + "${RMCS_PATH}/rmcs_ws/build-cross-amd64" \ + "${RMCS_PATH}/rmcs_ws/install-cross-amd64" \ + "${RMCS_PATH}/rmcs_ws/log-cross-amd64" diff --git a/rmcs_ws/src/rmcs_bringup/config/deformable-infantry-omni-b.yaml b/rmcs_ws/src/rmcs_bringup/config/deformable-infantry-omni-b.yaml index 489ee300..c49fdce2 100644 --- a/rmcs_ws/src/rmcs_bringup/config/deformable-infantry-omni-b.yaml +++ b/rmcs_ws/src/rmcs_bringup/config/deformable-infantry-omni-b.yaml @@ -40,7 +40,7 @@ value_broadcaster: deformable_infantry: ros__parameters: - serial_filter_rmcs_board: "AF-23FB-EE32-B892-1302-AE70-D640-7B4E-0CBF" + serial_filter_rmcs_board: "AF-C26A-0C9C-CF41-3E3C-1596-524B-7527-5744" serial_filter_top_board: "AF-ABAC-786D-1B53-99F6-00A2-42A6-AA95-9D69" left_front_zero_point: 7173 left_back_zero_point: 5167 diff --git a/rmcs_ws/src/rmcs_bringup/package.xml b/rmcs_ws/src/rmcs_bringup/package.xml index 9c8cc981..726e7b26 100644 --- a/rmcs_ws/src/rmcs_bringup/package.xml +++ b/rmcs_ws/src/rmcs_bringup/package.xml @@ -11,7 +11,7 @@ ament_cmake joint_state_broadcaster - rmcs_auto_aim_v2 + rmcs_auto_aim_v2 robot_state_publisher rviz2 xacro @@ -22,4 +22,4 @@ ament_cmake - \ No newline at end of file + From a8e6014a738fd06579faaa17eb20341638217696 Mon Sep 17 00:00:00 2001 From: ZGZ713912 Date: Thu, 14 May 2026 05:25:32 +0800 Subject: [PATCH 12/52] feat: update configuration and controller parameters for deformable infantry Co-authored-by: Copilot --- .../config/deformable-infantry-omni-b.yaml | 38 ++++--- .../config/deformable-infantry-omni.yaml | 6 +- .../src/rmcs_bringup/launch/rmcs.launch.py | 14 +-- .../chassis/chassis_power_controller.cpp | 2 +- .../hardware/deformable-infantry-omni-b.cpp | 99 +++++++++++++++---- .../referee/app/ui/deformable_infantry_ui.cpp | 14 +-- 6 files changed, 121 insertions(+), 52 deletions(-) diff --git a/rmcs_ws/src/rmcs_bringup/config/deformable-infantry-omni-b.yaml b/rmcs_ws/src/rmcs_bringup/config/deformable-infantry-omni-b.yaml index c49fdce2..0b28cf37 100644 --- a/rmcs_ws/src/rmcs_bringup/config/deformable-infantry-omni-b.yaml +++ b/rmcs_ws/src/rmcs_bringup/config/deformable-infantry-omni-b.yaml @@ -29,7 +29,8 @@ rmcs_executor: - rmcs_core::controller::chassis::DeformableJointController -> rb_joint_controller - rmcs_core::controller::chassis::DeformableJointController -> rf_joint_controller - # - rmcs_core::broadcaster::ValueBroadcaster -> value_broadcaster + - rmcs_core::broadcaster::ValueBroadcaster -> value_broadcaster + - rmcs::AutoAimComponent -> auto_aim_component value_broadcaster: ros__parameters: @@ -40,14 +41,15 @@ value_broadcaster: deformable_infantry: ros__parameters: - serial_filter_rmcs_board: "AF-C26A-0C9C-CF41-3E3C-1596-524B-7527-5744" + serial_filter_rmcs_board: "AF-23FB-EE32-B892-1302-AE70-D640-7B4E-0CBF" serial_filter_top_board: "AF-ABAC-786D-1B53-99F6-00A2-42A6-AA95-9D69" + serial_filter_imu: "AF-C26A-0C9C-CF41-3E3C-1596-524B-7527-5744" left_front_zero_point: 7173 left_back_zero_point: 5167 right_back_zero_point: 3098 right_front_zero_point: 6485 yaw_motor_zero_point: 25406 - pitch_motor_zero_point: 56556 + pitch_motor_zero_point: 56354 debug_log_supercap: false debug_log_wheel_motor: false debug_log_deformable_joint_motor: false @@ -55,8 +57,8 @@ deformable_infantry: chassis_controller: ros__parameters: # Deploy geometry / chassis-owned joint intent - min_angle: 20.0 - max_angle: 50.0 + min_angle: 7.0 + max_angle: 58.0 active_suspension_enable: true spin_ratio: 1.0 @@ -84,31 +86,35 @@ chassis_controller: gimbal_controller: ros__parameters: - inertia: 1.0 # kg·m² - friction: 1.65 # Nm/(rad/s) - - upper_limit: -0.61 # -35 deg + upper_limit: -0.80 # -35 deg lower_limit: 0.05 # 6 deg - use_encoder_pitch: true - yaw_angle_kp: 10.0 + yaw_angle_kp: 30.0 yaw_angle_ki: 0.0 yaw_angle_kd: 0.0 - yaw_velocity_kp: 8.0 + yaw_velocity_kp: 15.0 yaw_velocity_ki: 0.0 yaw_velocity_kd: 0.0 - pitch_angle_kp: 40.0 + yaw_vel_ff_gain: 0.47 + yaw_acc_ff_gain: 0.00 + + pitch_angle_kp: 25.0 pitch_angle_ki: 0.0 pitch_angle_kd: 0.0 - pitch_velocity_kp: 3.0 + pitch_velocity_kp: 2.2 pitch_velocity_ki: 0.0 pitch_velocity_kd: 0.0 + pitch_acc_ff_gain: 0.10 + pitch_torque_control: true + pitch_fusion_enabled: true + pitch_fusion_alpha: 0.98 + friction_wheel_controller: ros__parameters: friction_wheels: @@ -158,7 +164,7 @@ bullet_feeder_velocity_pid_controller: measurement: /gimbal/bullet_feeder/velocity setpoint: /gimbal/bullet_feeder/control_velocity control: /gimbal/bullet_feeder/control_torque - kp: 1.4 + kp: 1.5 ki: 0.0 kd: 0.0 @@ -169,7 +175,7 @@ deformable_chassis_controller: chassis_radius: 0.2341741 rod_length: 0.150 wheel_radius: 0.055 - friction_coefficient: 0.6 + friction_coefficient: 6.6 k1: 2.958580e+00 k2: 3.082190e-03 no_load_power: 11.37 diff --git a/rmcs_ws/src/rmcs_bringup/config/deformable-infantry-omni.yaml b/rmcs_ws/src/rmcs_bringup/config/deformable-infantry-omni.yaml index 75d015ef..84bfd1e3 100644 --- a/rmcs_ws/src/rmcs_bringup/config/deformable-infantry-omni.yaml +++ b/rmcs_ws/src/rmcs_bringup/config/deformable-infantry-omni.yaml @@ -48,7 +48,7 @@ deformable_infantry: right_back_zero_point: 7817 right_front_zero_point: 7136 yaw_motor_zero_point: 50642 - pitch_motor_zero_point: 6245 + pitch_motor_zero_point: 6853 debug_log_supercap: false debug_log_wheel_motor: false debug_log_deformable_joint_motor: false @@ -85,7 +85,7 @@ chassis_controller: gimbal_controller: ros__parameters: - upper_limit: -0.61 # -35 deg + upper_limit: -0.80 # -35 deg lower_limit: 0.05 # 6 deg yaw_angle_kp: 30.0 @@ -174,7 +174,7 @@ deformable_chassis_controller: chassis_radius: 0.2341741 rod_length: 0.150 wheel_radius: 0.055 - friction_coefficient: 66.6 + friction_coefficient: 6.6 k1: 2.958580e+00 k2: 3.082190e-03 no_load_power: 11.37 diff --git a/rmcs_ws/src/rmcs_bringup/launch/rmcs.launch.py b/rmcs_ws/src/rmcs_bringup/launch/rmcs.launch.py index 58abbbcc..d810a08d 100644 --- a/rmcs_ws/src/rmcs_bringup/launch/rmcs.launch.py +++ b/rmcs_ws/src/rmcs_bringup/launch/rmcs.launch.py @@ -54,13 +54,13 @@ def visit( if is_automatic: pass - # entities.append( - # IncludeLaunchDescription( - # PythonLaunchDescriptionSource([ - # FindPackageShare('rmcs_auto_aim_v2'), '/launch.py' - # ]) - # ) - # ) + entities.append( + IncludeLaunchDescription( + PythonLaunchDescriptionSource([ + FindPackageShare('rmcs_auto_aim_v2'), '/launch.py' + ]) + ) + ) diff --git a/rmcs_ws/src/rmcs_core/src/controller/chassis/chassis_power_controller.cpp b/rmcs_ws/src/rmcs_core/src/controller/chassis/chassis_power_controller.cpp index a014d717..1725dfa9 100644 --- a/rmcs_ws/src/rmcs_core/src/controller/chassis/chassis_power_controller.cpp +++ b/rmcs_ws/src/rmcs_core/src/controller/chassis/chassis_power_controller.cpp @@ -64,7 +64,7 @@ class ChassisPowerController update_virtual_buffer_energy(); - boost_mode_ = keyboard.shift || rotary_knob < -0.9; + boost_mode_ = keyboard.shift || keyboard.ctrl || rotary_knob < -0.9; *supercap_control_enabled_ = boost_mode_; update_control_power_limit(); } diff --git a/rmcs_ws/src/rmcs_core/src/hardware/deformable-infantry-omni-b.cpp b/rmcs_ws/src/rmcs_core/src/hardware/deformable-infantry-omni-b.cpp index f07b1de3..9be7189a 100644 --- a/rmcs_ws/src/rmcs_core/src/hardware/deformable-infantry-omni-b.cpp +++ b/rmcs_ws/src/rmcs_core/src/hardware/deformable-infantry-omni-b.cpp @@ -61,12 +61,17 @@ class DeformableInfantryOmniB status_service_callback(response); }); + std::string serial_filter_imu; + get_parameter_or("serial_filter_imu", serial_filter_imu, std::string{}); + rmcs_board_lite = std::make_unique( *this, *deformable_infantry_command_, get_parameter("serial_filter_rmcs_board").as_string()); top_board_ = std::make_unique( *this, *deformable_infantry_command_, - get_parameter("serial_filter_top_board").as_string()); + get_parameter("serial_filter_top_board").as_string(), !serial_filter_imu.empty()); + if (!serial_filter_imu.empty()) + imu_board_ = std::make_unique(*this, serial_filter_imu); } ~DeformableInfantryOmniB() override = default; @@ -79,6 +84,8 @@ class DeformableInfantryOmniB void update() override { rmcs_board_lite->update(); top_board_->update(); + if (imu_board_) + imu_board_->update(); } void command_update() { @@ -90,6 +97,7 @@ class DeformableInfantryOmniB private: class DeformableInfantryOmniBCommand; class BottomBoard; + class ImuBoard; class TopBoard; class DeformableInfantryOmniBCommand : public rmcs_executor::Component { @@ -586,6 +594,54 @@ class DeformableInfantryOmniB OutputInterface radius_; }; + class ImuBoard final : private librmcs::agent::RmcsBoardLite { + friend class DeformableInfantryOmniB; + + public: + explicit ImuBoard( + DeformableInfantryOmniB& deformableInfantry, const std::string& serial_filter = {}) + : RmcsBoardLite{ + serial_filter, + librmcs::agent::AdvancedOptions{.dangerously_skip_version_checks = true}} + , tf_{deformableInfantry.tf_} + , bmi088_{1000, 0.2, 0.0} { + + deformableInfantry.register_output( + "/gimbal/pitch/velocity_imu", gimbal_pitch_velocity_imu_); + + bmi088_.set_coordinate_mapping( + [](double x, double y, double z) { return std::make_tuple(x, z, -y); }); + } + + ~ImuBoard() override = default; + + void update() { + bmi088_.update_status(); + Eigen::Quaterniond const gimbal_imu_pose{ + bmi088_.q0(), bmi088_.q1(), bmi088_.q2(), bmi088_.q3()}; + + tf_->set_transform( + gimbal_imu_pose.conjugate()); + + *gimbal_pitch_velocity_imu_ = bmi088_.gy(); + } + + private: + void accelerometer_receive_callback( + const librmcs::data::AccelerometerDataView& data) override { + bmi088_.store_accelerometer_status(data.x, data.y, data.z); + } + + void gyroscope_receive_callback(const librmcs::data::GyroscopeDataView& data) override { + bmi088_.store_gyroscope_status(data.x, data.y, data.z); + } + + OutputInterface& tf_; + OutputInterface gimbal_pitch_velocity_imu_; + + device::Bmi088 bmi088_; + }; + class TopBoard final : private librmcs::agent::RmcsBoardLite { public: friend class DeformableInfantryOmniB; @@ -593,11 +649,12 @@ class DeformableInfantryOmniB explicit TopBoard( DeformableInfantryOmniB& deformableInfantry, DeformableInfantryOmniBCommand& deformableInfantry_command, - std::string serial_filter = {}) + std::string serial_filter = {}, bool has_external_imu_board = false) : librmcs::agent::RmcsBoardLite( serial_filter, librmcs::agent::AdvancedOptions{.dangerously_skip_version_checks = true}) , hard_sync_pending_(deformableInfantry.hard_sync_pending_) + , has_external_imu_board_(has_external_imu_board) , tf_(deformableInfantry.tf_) , bmi088_(1000, 0.2, 0.0) , gimbal_pitch_motor_(deformableInfantry, deformableInfantry_command, "/gimbal/pitch") @@ -626,8 +683,9 @@ class DeformableInfantryOmniB deformableInfantry.register_output( "/gimbal/yaw/velocity_imu", gimbal_yaw_velocity_bmi088_); - deformableInfantry.register_output( - "/gimbal/pitch/velocity_imu", gimbal_pitch_velocity_encoder_); + if (!has_external_imu_board_) + deformableInfantry.register_output( + "/gimbal/pitch/velocity_imu", gimbal_pitch_velocity_encoder_); bmi088_.set_coordinate_mapping([](double x, double y, double z) { // Top board BMI088 maps to gimbal frame as (-x, -y, z). @@ -651,22 +709,25 @@ class DeformableInfantryOmniB scope_motor_.update_status(); const double pitch_encoder_angle = gimbal_pitch_motor_.angle(); - Eigen::Quaterniond const odom_imu_to_yaw_link{ - bmi088_.q0(), bmi088_.q1(), bmi088_.q2(), bmi088_.q3()}; - Eigen::Quaterniond const yaw_link_to_odom_imu = odom_imu_to_yaw_link.conjugate(); - Eigen::Quaterniond pitch_link_to_odom_imu = - Eigen::Quaterniond{ - Eigen::AngleAxisd{-pitch_encoder_angle, Eigen::Vector3d::UnitY()}} - * yaw_link_to_odom_imu; - pitch_link_to_odom_imu.normalize(); *gimbal_yaw_velocity_bmi088_ = bmi088_.gz(); - *gimbal_pitch_velocity_encoder_ = gimbal_pitch_motor_.velocity(); - // The BMI088 is mounted on the yaw link. fast_tf stores PitchLink -> - // OdomImu, so use the encoder pitch from the TF tree to move the - // yaw-link pose back into PitchLink. - tf_->set_transform( - pitch_link_to_odom_imu); + if (!has_external_imu_board_) { + Eigen::Quaterniond const odom_imu_to_yaw_link{ + bmi088_.q0(), bmi088_.q1(), bmi088_.q2(), bmi088_.q3()}; + Eigen::Quaterniond const yaw_link_to_odom_imu = odom_imu_to_yaw_link.conjugate(); + Eigen::Quaterniond pitch_link_to_odom_imu = + Eigen::Quaterniond{ + Eigen::AngleAxisd{-pitch_encoder_angle, Eigen::Vector3d::UnitY()}} + * yaw_link_to_odom_imu; + pitch_link_to_odom_imu.normalize(); + + *gimbal_pitch_velocity_encoder_ = gimbal_pitch_motor_.velocity(); + // The BMI088 is mounted on the yaw link. fast_tf stores PitchLink -> + // OdomImu, so use the encoder pitch from the TF tree to move the + // yaw-link pose back into PitchLink. + tf_->set_transform( + pitch_link_to_odom_imu); + } tf_->set_state( pitch_encoder_angle); @@ -723,6 +784,7 @@ class DeformableInfantryOmniB } std::atomic& hard_sync_pending_; + bool has_external_imu_board_ = false; OutputInterface& tf_; OutputInterface gimbal_yaw_velocity_bmi088_; @@ -765,6 +827,7 @@ class DeformableInfantryOmniB std::shared_ptr deformable_infantry_command_; std::unique_ptr rmcs_board_lite; + std::unique_ptr imu_board_; std::unique_ptr top_board_; std::shared_ptr> status_service_; diff --git a/rmcs_ws/src/rmcs_core/src/referee/app/ui/deformable_infantry_ui.cpp b/rmcs_ws/src/rmcs_core/src/referee/app/ui/deformable_infantry_ui.cpp index 8e7a9393..5320d1df 100644 --- a/rmcs_ws/src/rmcs_core/src/referee/app/ui/deformable_infantry_ui.cpp +++ b/rmcs_ws/src/rmcs_core/src/referee/app/ui/deformable_infantry_ui.cpp @@ -97,13 +97,13 @@ class DeformableInfantry status_ring_.update_battery_power(*chassis_voltage_); status_ring_.update_auto_aim_enable(mouse_->right == 1); - pitch_hud_.update( - (gimbal_pitch_angle_.ready() && std::isfinite(*gimbal_pitch_angle_)) - ? *gimbal_pitch_angle_ - : std::numeric_limits::quiet_NaN(), - (chassis_pitch_.ready() && std::isfinite(*chassis_pitch_)) - ? *chassis_pitch_ - : std::numeric_limits::quiet_NaN()); + // pitch_hud_.update( + // (gimbal_pitch_angle_.ready() && std::isfinite(*gimbal_pitch_angle_)) + // ? *gimbal_pitch_angle_ + // : std::numeric_limits::quiet_NaN(), + // (chassis_pitch_.ready() && std::isfinite(*chassis_pitch_)) + // ? *chassis_pitch_ + // : std::numeric_limits::quiet_NaN()); } private: From 068ac3366ce8f9f3f9df4089b1b7f33f433d016a Mon Sep 17 00:00:00 2001 From: ZGZ713912 Date: Fri, 15 May 2026 07:04:59 +0800 Subject: [PATCH 13/52] feat: remove unused build and install scripts, update CMakeLists and enhance UI components for deformable infantry --- rmcs_ws/build | 1 - rmcs_ws/install | 1 - rmcs_ws/log | 1 - rmcs_ws/src/rmcs_core/CMakeLists.txt | 4 +- .../chassis/chassis_power_controller.cpp | 4 +- .../controller/chassis/deformable_chassis.cpp | 99 ++++--- .../referee/app/ui/deformable_infantry_ui.cpp | 49 +++- .../referee/app/ui/widget/animated_toggle.hpp | 74 ++++++ .../app/ui/widget/crosshair_circle.hpp | 3 + .../src/referee/app/ui/widget/pitch_hud.hpp | 243 ++++++------------ 10 files changed, 265 insertions(+), 214 deletions(-) delete mode 120000 rmcs_ws/build delete mode 120000 rmcs_ws/install delete mode 120000 rmcs_ws/log create mode 100644 rmcs_ws/src/rmcs_core/src/referee/app/ui/widget/animated_toggle.hpp diff --git a/rmcs_ws/build b/rmcs_ws/build deleted file mode 120000 index 2ec6ad5a..00000000 --- a/rmcs_ws/build +++ /dev/null @@ -1 +0,0 @@ -build-cross-arm64 \ No newline at end of file diff --git a/rmcs_ws/install b/rmcs_ws/install deleted file mode 120000 index 4ed00624..00000000 --- a/rmcs_ws/install +++ /dev/null @@ -1 +0,0 @@ -install-cross-arm64 \ No newline at end of file diff --git a/rmcs_ws/log b/rmcs_ws/log deleted file mode 120000 index 4c7d622b..00000000 --- a/rmcs_ws/log +++ /dev/null @@ -1 +0,0 @@ -log-cross-arm64 \ No newline at end of file diff --git a/rmcs_ws/src/rmcs_core/CMakeLists.txt b/rmcs_ws/src/rmcs_core/CMakeLists.txt index 36e7e570..d275c6db 100644 --- a/rmcs_ws/src/rmcs_core/CMakeLists.txt +++ b/rmcs_ws/src/rmcs_core/CMakeLists.txt @@ -17,8 +17,8 @@ include(FetchContent) set(BUILD_STATIC_LIBRMCS ON CACHE BOOL "Build static librmcs SDK" FORCE) FetchContent_Declare( librmcs - URL https://github.com/Alliance-Algorithm/librmcs/releases/download/v3.1.0/librmcs-sdk-src-3.1.0.zip - URL_HASH SHA256=07107e251745ddb23f7b3e39edec5d6910be1a197025d167ec9849c5c80dd954 + URL https://github.com/Alliance-Algorithm/librmcs/releases/download/v3.2.0rc0/librmcs-sdk-src-3.2.0-rc.0.zip + URL_HASH SHA256=accefeb3a46d7da32a215765d0907de36c25353dd7951e342aaddfe98d2971e5 DOWNLOAD_EXTRACT_TIMESTAMP TRUE ) FetchContent_MakeAvailable(librmcs) diff --git a/rmcs_ws/src/rmcs_core/src/controller/chassis/chassis_power_controller.cpp b/rmcs_ws/src/rmcs_core/src/controller/chassis/chassis_power_controller.cpp index 1725dfa9..ec120e7c 100644 --- a/rmcs_ws/src/rmcs_core/src/controller/chassis/chassis_power_controller.cpp +++ b/rmcs_ws/src/rmcs_core/src/controller/chassis/chassis_power_controller.cpp @@ -130,7 +130,7 @@ class ChassisPowerController 0.0, 1.0); // Maximum excess power when virtual buffer energy is full. - const double excess_power_limit = supercap_active ? 15.0 : 0.0; + const double excess_power_limit = supercap_active ? 15.0 : 15.0; power_limit += excess_power_limit; power_limit *= virtual_buffer_energy_ / virtual_buffer_energy_limit_; @@ -155,7 +155,7 @@ class ChassisPowerController InputInterface rotary_knob_; InputInterface chassis_power_; - static constexpr double virtual_buffer_energy_limit_ = 30.0; + static constexpr double virtual_buffer_energy_limit_ = 40.0; double virtual_buffer_energy_; InputInterface supercap_voltage_; diff --git a/rmcs_ws/src/rmcs_core/src/controller/chassis/deformable_chassis.cpp b/rmcs_ws/src/rmcs_core/src/controller/chassis/deformable_chassis.cpp index be49b501..8e4aee81 100644 --- a/rmcs_ws/src/rmcs_core/src/controller/chassis/deformable_chassis.cpp +++ b/rmcs_ws/src/rmcs_core/src/controller/chassis/deformable_chassis.cpp @@ -15,7 +15,6 @@ #include #include #include -#include #include #include "controller/pid/pid_calculator.hpp" @@ -126,11 +125,8 @@ class DeformableChassis roll_pid_.output_limit = roll_diff_limit_; register_input("/remote/joystick/right", joystick_right_); - register_input("/remote/joystick/left", joystick_left_); register_input("/remote/switch/right", switch_right_); register_input("/remote/switch/left", switch_left_); - register_input("/remote/mouse/velocity", mouse_velocity_); - register_input("/remote/mouse", mouse_); register_input("/remote/keyboard", keyboard_); register_input("/remote/rotary_knob", rotary_knob_); register_input("/predefined/update_rate", update_rate_); @@ -277,11 +273,22 @@ class DeformableChassis rmcs_msgs::Switch switch_left, rmcs_msgs::Switch switch_right, const rmcs_msgs::Keyboard& keyboard) { auto mode = *mode_; - if (switch_left == rmcs_msgs::Switch::DOWN) + if (switch_left == rmcs_msgs::Switch::DOWN) { + deactivate_complex_spin_(); return; + } - if (last_switch_right_ == rmcs_msgs::Switch::MIDDLE + if (!last_keyboard_.e && keyboard.e) { + if (complex_spin_active_) { + deactivate_complex_spin_(); + if (mode == rmcs_msgs::ChassisMode::SPIN) + mode = rmcs_msgs::ChassisMode::AUTO; + } else { + activate_complex_spin_(mode); + } + } else if (last_switch_right_ == rmcs_msgs::Switch::MIDDLE && switch_right == rmcs_msgs::Switch::DOWN) { + deactivate_complex_spin_(); if (mode == rmcs_msgs::ChassisMode::SPIN) { mode = rmcs_msgs::ChassisMode::STEP_DOWN; } else { @@ -289,6 +296,7 @@ class DeformableChassis spinning_forward_ = !spinning_forward_; } } else if (!last_keyboard_.c && keyboard.c) { + deactivate_complex_spin_(); if (mode == rmcs_msgs::ChassisMode::SPIN) { mode = rmcs_msgs::ChassisMode::AUTO; } else { @@ -296,17 +304,37 @@ class DeformableChassis spinning_forward_ = !spinning_forward_; } } else if (!last_keyboard_.x && keyboard.x) { + deactivate_complex_spin_(); mode = mode == rmcs_msgs::ChassisMode::LAUNCH_RAMP ? rmcs_msgs::ChassisMode::AUTO : rmcs_msgs::ChassisMode::LAUNCH_RAMP; } else if (!last_keyboard_.z && keyboard.z) { + deactivate_complex_spin_(); mode = mode == rmcs_msgs::ChassisMode::STEP_DOWN ? rmcs_msgs::ChassisMode::AUTO : rmcs_msgs::ChassisMode::STEP_DOWN; } + if (complex_spin_active_) + mode = rmcs_msgs::ChassisMode::SPIN; + *mode_ = mode; } + void activate_complex_spin_(rmcs_msgs::ChassisMode& mode) { + complex_spin_active_ = true; + complex_spin_elapsed_ = 0.0; + apply_symmetric_target = true; + if (mode != rmcs_msgs::ChassisMode::SPIN) { + mode = rmcs_msgs::ChassisMode::SPIN; + spinning_forward_ = !spinning_forward_; + } + } + + void deactivate_complex_spin_() { + complex_spin_active_ = false; + complex_spin_elapsed_ = 0.0; + } + std::array read_current_joint_physical_angles_() const { const std::array*, kJointCount> physical_angle_inputs{ &left_front_joint_physical_angle_, &left_back_joint_physical_angle_, @@ -349,21 +377,6 @@ class DeformableChassis && std::abs(lf_current_target_angle_ - rf_current_target_angle_) <= epsilon; } - bool refresh_requested_joint_targets_from_deploy_state_() { - requested_target_physical_angles_rad_[kLeftFront] = deg_to_rad(lf_current_target_angle_); - requested_target_physical_angles_rad_[kLeftBack] = deg_to_rad(lb_current_target_angle_); - requested_target_physical_angles_rad_[kRightBack] = deg_to_rad(rb_current_target_angle_); - requested_target_physical_angles_rad_[kRightFront] = deg_to_rad(rf_current_target_angle_); - - const bool prone_override = prone_override_requested_by_keyboard(); - if (suspension_requested_by_input_()) { - requested_target_physical_angles_rad_.fill(deg_to_rad(min_angle_)); - } - - current_target_physical_angles_rad_ = requested_target_physical_angles_rad_; - return prone_override; - } - void reset_attitude_correction_state_() { pitch_pid_.reset(); roll_pid_.reset(); @@ -435,7 +448,6 @@ class DeformableChassis joint_target_physical_angle_state_rad_ = current_physical_angles; joint_target_physical_velocity_state_rad_ = {0.0, 0.0, 0.0, 0.0}; joint_target_physical_acceleration_state_rad_ = {0.0, 0.0, 0.0, 0.0}; - requested_target_physical_angles_rad_ = current_physical_angles; current_target_physical_angles_rad_ = current_physical_angles; joint_target_active_ = true; return true; @@ -508,6 +520,7 @@ class DeformableChassis rf_current_target_angle_ = current_target_angle_; joint_target_active_ = false; suspension_on_by_switch = false; + deactivate_complex_spin_(); *scope_motor_control_torque = nan_; @@ -619,6 +632,7 @@ class DeformableChassis void update_lift_target_toggle(rmcs_msgs::Keyboard keyboard) { constexpr double rotary_knob_edge_threshold = 0.7; + constexpr double complex_spin_toggle_period = 0.5; const bool keyboard_toggle_condition = !last_keyboard_.q && keyboard.q; const bool rotary_knob_toggle_condition = @@ -626,6 +640,17 @@ class DeformableChassis && *rotary_knob_ >= rotary_knob_edge_threshold; const bool front_high_rear_low = !last_keyboard_.b && keyboard.b; const bool front_low_rear_high = !last_keyboard_.g && keyboard.g; + bool complex_spin_toggle_condition = false; + + if (complex_spin_active_) { + complex_spin_elapsed_ += update_dt(); + size_t complex_spin_toggle_count = 0; + while (complex_spin_elapsed_ >= complex_spin_toggle_period) { + complex_spin_elapsed_ -= complex_spin_toggle_period; + ++complex_spin_toggle_count; + } + complex_spin_toggle_condition = (complex_spin_toggle_count % 2) == 1; + } if (apply_symmetric_target) { lf_current_target_angle_ = current_target_angle_; @@ -634,7 +659,7 @@ class DeformableChassis rf_current_target_angle_ = current_target_angle_; } - if (rotary_knob_toggle_condition || keyboard_toggle_condition) { + if (rotary_knob_toggle_condition || keyboard_toggle_condition || complex_spin_toggle_condition) { current_target_angle_ = (std::abs(current_target_angle_ - max_angle_) < 1e-6) ? min_angle_ : max_angle_; apply_symmetric_target = true; @@ -659,6 +684,7 @@ class DeformableChassis // coordinate suspension overrides, then publish the resulting joint intent for the servo layer. void run_joint_intent_pipeline_() { const auto current_physical_angles = read_current_joint_physical_angles_(); + const bool suspension_requested = suspension_requested_by_input_(); if (!joint_target_active_ && !initialize_joint_target_states_from_feedback(current_physical_angles)) { @@ -666,9 +692,16 @@ class DeformableChassis return; } + current_target_physical_angles_rad_[kLeftFront] = deg_to_rad(lf_current_target_angle_); + current_target_physical_angles_rad_[kLeftBack] = deg_to_rad(lb_current_target_angle_); + current_target_physical_angles_rad_[kRightBack] = deg_to_rad(rb_current_target_angle_); + current_target_physical_angles_rad_[kRightFront] = deg_to_rad(rf_current_target_angle_); + if (suspension_requested) { + current_target_physical_angles_rad_.fill(deg_to_rad(min_angle_)); + } + update_chassis_imu_calibration_(); - const bool prone_override = refresh_requested_joint_targets_from_deploy_state_(); - scope_motor_control(prone_override); + scope_motor_control(suspension_requested); update_active_suspension_(); update_joint_target_trajectory(); @@ -677,18 +710,12 @@ class DeformableChassis static double deg_to_rad(double deg) { return deg * std::numbers::pi / 180.0; } - void scope_motor_control(bool prone_override = false) { - const bool prone_target_active = prone_override; + void scope_motor_control(bool suspension_requested = false) { + const bool prone_target_active = suspension_requested; if (prone_target_active && *mode_ != rmcs_msgs::ChassisMode::SPIN) { *scope_motor_control_torque = -0.3; - // if (*scope_motor_velocity <= std::abs(0.1)){ - // *scope_motor_control_torque = 0.18 * 1.0 / 36.0; - // } } else { *scope_motor_control_torque = 0.3; - // if (*scope_motor_velocity <= std::abs(0.1)){ - // *scope_motor_control_torque = -0.18 * 1.0 / 36.0; - // } } } @@ -816,11 +843,8 @@ class DeformableChassis private: InputInterface joystick_right_; - InputInterface joystick_left_; InputInterface switch_right_; InputInterface switch_left_; - InputInterface mouse_velocity_; - InputInterface mouse_; InputInterface keyboard_; InputInterface rotary_knob_; InputInterface update_rate_; @@ -838,6 +862,8 @@ class DeformableChassis bool spinning_forward_ = true; bool apply_symmetric_target = true; + bool complex_spin_active_ = false; + double complex_spin_elapsed_ = 0.0; pid::PidCalculator following_velocity_controller_; const double spin_ratio_; @@ -877,7 +903,6 @@ class DeformableChassis double lf_current_target_angle_, lb_current_target_angle_, rb_current_target_angle_, rf_current_target_angle_; - std::array requested_target_physical_angles_rad_ = {0.0, 0.0, 0.0, 0.0}; std::array current_target_physical_angles_rad_ = {0.0, 0.0, 0.0, 0.0}; bool joint_target_active_ = false; diff --git a/rmcs_ws/src/rmcs_core/src/referee/app/ui/deformable_infantry_ui.cpp b/rmcs_ws/src/rmcs_core/src/referee/app/ui/deformable_infantry_ui.cpp index 5320d1df..5bb326e2 100644 --- a/rmcs_ws/src/rmcs_core/src/referee/app/ui/deformable_infantry_ui.cpp +++ b/rmcs_ws/src/rmcs_core/src/referee/app/ui/deformable_infantry_ui.cpp @@ -1,5 +1,6 @@ #include #include +#include #include #include #include @@ -8,9 +9,11 @@ #include #include #include +#include #include #include "referee/app/ui/shape/shape.hpp" +#include "referee/app/ui/widget/animated_toggle.hpp" #include "referee/app/ui/widget/crosshair_circle.hpp" #include "referee/app/ui/widget/deformable_chassis_top_view.hpp" #include "referee/app/ui/widget/pitch_hud.hpp" @@ -25,7 +28,7 @@ class DeformableInfantry public: DeformableInfantry() : Node{get_component_name(), rclcpp::NodeOptions{}.automatically_declare_parameters_from_overrides(true)} - , crosshair_circle_(Shape::Color::WHITE, x_center - 2, y_center - 30, 8, 2) + , crosshair_circle_(Shape::Color::WHITE, x_center - 14, y_center - 42, 8, 2) , status_ring_(26.5, 26.5, 600, 300) , horizontal_center_guidelines_( {Shape::Color::WHITE, 2, x_center - 360, y_center, x_center - 110, y_center}, @@ -34,7 +37,14 @@ class DeformableInfantry {Shape::Color::WHITE, 2, x_center, 800, x_center, y_center + 110}, {Shape::Color::WHITE, 2, x_center, y_center - 110, x_center, 200}) , chassis_direction_indicator_(Shape::Color::PINK, 8, x_center, y_center, 0, 0, 84, 84) - , pitch_hud_(PitchHud::Config{1540, y_center, 180, 30.0, 5.0}) + , pitch_hud_(PitchHud::Config{ + .center_x = x_center, + .center_y = y_center, + .radius_px = 95, + .width_px = 12, + .half_span_deg = 30.0, + .warning_pitch_deg = 5.0, + }) , time_reminder_(Shape::Color::PINK, 50, 5, x_center + 150, y_center + 65, 0, false) { double deformable_leg_min_angle_deg = 8.0; @@ -48,6 +58,7 @@ class DeformableInfantry deformable_chassis_leg_arcs_.set_angle_range( deformable_leg_min_angle_deg, deformable_leg_max_angle_deg); + register_input("/predefined/timestamp", timestamp_); register_input("/chassis/control_mode", chassis_mode_); register_input("/chassis/angle", chassis_angle_); @@ -79,15 +90,20 @@ class DeformableInfantry register_input("/gimbal/right_friction/velocity", right_friction_velocity_); register_input("/remote/mouse", mouse_); + register_input("/remote/keyboard", keyboard_); register_input("/gimbal/pitch/angle", gimbal_pitch_angle_, false); register_input("/referee/game/stage", game_stage_); + + ctrl_transition_.reset(false); + pitch_hud_.set_visible(false); } void update() override { update_chassis_direction_indicator(); update_deformable_chassis_leg_arcs(); + update_ctrl_ui(); status_ring_.update_bullet_allowance(*robot_bullet_allowance_); status_ring_.update_friction_wheel_speed( @@ -97,16 +113,23 @@ class DeformableInfantry status_ring_.update_battery_power(*chassis_voltage_); status_ring_.update_auto_aim_enable(mouse_->right == 1); - // pitch_hud_.update( - // (gimbal_pitch_angle_.ready() && std::isfinite(*gimbal_pitch_angle_)) - // ? *gimbal_pitch_angle_ - // : std::numeric_limits::quiet_NaN(), - // (chassis_pitch_.ready() && std::isfinite(*chassis_pitch_)) - // ? *chassis_pitch_ - // : std::numeric_limits::quiet_NaN()); } private: + void update_ctrl_ui() { + const bool ctrl_active = keyboard_.ready() && keyboard_->ctrl; + const double reveal = ctrl_transition_.update(*timestamp_, ctrl_active); + + crosshair_circle_.set_x(static_cast(std::lround( + static_cast(crosshair_base_x_) + 20.0 * reveal))); + + // const double display_pitch = + // (chassis_pitch_.ready() && std::isfinite(*chassis_pitch_)) + // ? -*chassis_pitch_ + // : std::numeric_limits::quiet_NaN(); + // pitch_hud_.update(display_pitch, reveal); + } + void update_time_reminder() { if (!game_stage_.ready()) return; @@ -127,8 +150,8 @@ class DeformableInfantry switch (mode) { case rmcs_msgs::ChassisMode::SPIN: return Shape::Color::GREEN; case rmcs_msgs::ChassisMode::AUTO: return Shape::Color::CYAN; - case rmcs_msgs::ChassisMode::STEP_DOWN: return Shape::Color::WHITE; - default: return Shape::Color::PINK; + case rmcs_msgs::ChassisMode::STEP_DOWN: return Shape::Color::PINK; + default: return Shape::Color::WHITE; } } @@ -151,7 +174,9 @@ class DeformableInfantry static constexpr uint16_t screen_width = 1920, screen_height = 1080; static constexpr uint16_t x_center = screen_width / 2, y_center = screen_height / 2; + static constexpr uint16_t crosshair_base_x_ = x_center - 2; + InputInterface timestamp_; InputInterface chassis_mode_; InputInterface chassis_angle_; @@ -172,6 +197,7 @@ class DeformableInfantry InputInterface right_friction_velocity_; InputInterface mouse_; + InputInterface keyboard_; InputInterface gimbal_pitch_angle_; InputInterface chassis_pitch_; @@ -188,6 +214,7 @@ class DeformableInfantry DeformableChassisLegArcs deformable_chassis_leg_arcs_; PitchHud pitch_hud_; + AnimatedToggle ctrl_transition_{}; Integer time_reminder_; }; diff --git a/rmcs_ws/src/rmcs_core/src/referee/app/ui/widget/animated_toggle.hpp b/rmcs_ws/src/rmcs_core/src/referee/app/ui/widget/animated_toggle.hpp new file mode 100644 index 00000000..8409612d --- /dev/null +++ b/rmcs_ws/src/rmcs_core/src/referee/app/ui/widget/animated_toggle.hpp @@ -0,0 +1,74 @@ +#pragma once + +#include +#include +#include + +namespace rmcs_core::referee::app::ui { + +class AnimatedToggle { +public: + using Clock = std::chrono::steady_clock; + + explicit AnimatedToggle(std::chrono::duration duration = std::chrono::duration{0.5}) + : duration_(duration) {} + + void set_duration(std::chrono::duration duration) { + duration_ = std::max(duration, std::chrono::duration{0.0}); + } + + void reset(bool active) { + initialized_ = false; + value_ = active ? 1.0 : 0.0; + target_ = active; + } + + double update(Clock::time_point now, bool active) { + if (!initialized_) { + initialized_ = true; + start_time_ = now; + start_value_ = active ? 1.0 : 0.0; + end_value_ = start_value_; + value_ = start_value_; + target_ = active; + return value_; + } + + if (active != target_) { + target_ = active; + start_value_ = value_; + end_value_ = active ? 1.0 : 0.0; + start_time_ = now; + } + + if (duration_.count() <= 0.0) { + value_ = end_value_; + return value_; + } + + const double elapsed = std::chrono::duration{now - start_time_}.count(); + const double t = std::clamp(elapsed / duration_.count(), 0.0, 1.0); + value_ = std::lerp(start_value_, end_value_, ease_in_out_cubic_(t)); + return value_; + } + + double value() const { return value_; } + +private: + static double ease_in_out_cubic_(double t) { + t = std::clamp(t, 0.0, 1.0); + if (t < 0.5) + return 4.0 * t * t * t; + return 1.0 - std::pow(-2.0 * t + 2.0, 3.0) / 2.0; + } + + std::chrono::duration duration_; + Clock::time_point start_time_{}; + double start_value_ = 0.0; + double end_value_ = 0.0; + double value_ = 0.0; + bool initialized_ = false; + bool target_ = false; +}; + +} // namespace rmcs_core::referee::app::ui diff --git a/rmcs_ws/src/rmcs_core/src/referee/app/ui/widget/crosshair_circle.hpp b/rmcs_ws/src/rmcs_core/src/referee/app/ui/widget/crosshair_circle.hpp index 21526b9b..b96b7cb4 100644 --- a/rmcs_ws/src/rmcs_core/src/referee/app/ui/widget/crosshair_circle.hpp +++ b/rmcs_ws/src/rmcs_core/src/referee/app/ui/widget/crosshair_circle.hpp @@ -15,8 +15,11 @@ class CrossHairCircle { : circle_(color, width, x, y, r, r, visible) {} void set_visible(bool value) { circle_.set_visible(value); } + void set_color(Shape::Color color) { circle_.set_color(color); } void set_r(uint16_t r) { circle_.set_r(r); } void set_width(uint16_t width) { circle_.set_width(width); } + void set_x(uint16_t x) { circle_.set_x(x); } + void set_y(uint16_t y) { circle_.set_y(y); } private: Circle circle_; diff --git a/rmcs_ws/src/rmcs_core/src/referee/app/ui/widget/pitch_hud.hpp b/rmcs_ws/src/rmcs_core/src/referee/app/ui/widget/pitch_hud.hpp index ebeea1e4..37286116 100644 --- a/rmcs_ws/src/rmcs_core/src/referee/app/ui/widget/pitch_hud.hpp +++ b/rmcs_ws/src/rmcs_core/src/referee/app/ui/widget/pitch_hud.hpp @@ -1,10 +1,8 @@ #pragma once #include -#include #include #include -#include #include "referee/app/ui/shape/shape.hpp" @@ -13,11 +11,12 @@ namespace rmcs_core::referee::app::ui { class PitchHud { public: struct Config { - uint16_t center_x = 1540; + uint16_t center_x = x_center; uint16_t center_y = y_center; - uint16_t half_height_px = 180; - double half_span_deg = 30.0; - double tick_step_deg = 5.0; + uint16_t radius_px = 150; + uint16_t width_px = 12; + double half_span_deg = 22.0; + double warning_pitch_deg = 5.0; }; PitchHud() { set_config(Config{}); } @@ -25,183 +24,109 @@ class PitchHud { explicit PitchHud(Config config) { set_config(config); } void set_config(Config config) { - config.tick_step_deg = std::max(config.tick_step_deg, 1.0); - config.half_span_deg = std::max(config.half_span_deg, config.tick_step_deg); - config.half_height_px = std::max(config.half_height_px, 40); + config.radius_px = std::max(config.radius_px, 1); + config.width_px = std::max(config.width_px, 1); + config.half_span_deg = std::max(config.half_span_deg, 1.0); + config.warning_pitch_deg = std::max(config.warning_pitch_deg, 0.0); config_ = config; initialize_(); } - void update(double gimbal_pitch, double chassis_pitch) { - if (std::isfinite(gimbal_pitch)) - update_gimbal_pitch_indicator_(pitch_to_hud_y_(gimbal_pitch)); - else - set_indicator_visible_(gimbal_pitch_indicator_, false); - - if (std::isfinite(chassis_pitch)) - update_chassis_pitch_indicator_(pitch_to_hud_y_(chassis_pitch)); - else - set_indicator_visible_(chassis_pitch_indicator_, false); + void set_visible(bool visible) { + background_arc_.set_visible(visible); + positive_fill_arc_.set_visible(visible && positive_fill_visible_); + negative_fill_arc_.set_visible(visible && negative_fill_visible_); } -private: - static constexpr std::size_t tick_capacity_ = 25; - - void initialize_() { - tick_count_ = std::clamp( - static_cast( - std::floor(2.0 * config_.half_span_deg / config_.tick_step_deg + 1.0e-6)) - + 1, - std::size_t{1}, tick_capacity_); - - axis_.set_color(Shape::Color::YELLOW); - axis_.set_width(2); - axis_.set_x(config_.center_x); - axis_.set_y(axis_top_y_()); - axis_.set_x2(config_.center_x); - axis_.set_y2(axis_bottom_y_()); - axis_.set_visible(true); - - for (std::size_t i = 0; i < ticks_.size(); ++i) { - auto& tick = ticks_[i]; - if (i >= tick_count_) { - tick.set_visible(false); - continue; - } - - const double tick_deg = -config_.half_span_deg + static_cast(i) * config_.tick_step_deg; - const int rounded_tick_deg = static_cast(std::lround(tick_deg)); - if (rounded_tick_deg == 0) { - tick.set_visible(false); - continue; - } - - const bool is_major = (std::abs(rounded_tick_deg) % 10 == 0); - const uint16_t tick_length = is_major ? 18 : 10; - const uint16_t tick_y = pitch_to_hud_y_(deg_to_rad_(tick_deg)); - - tick.set_color(Shape::Color::YELLOW); - tick.set_width(2); - tick.set_x(config_.center_x); - tick.set_y(tick_y); - tick.set_x2(config_.center_x + tick_length); - tick.set_y2(tick_y); - tick.set_visible(true); + void update(double pitch_rad, double reveal = 1.0) { + reveal = std::clamp(reveal, 0.0, 1.0); + if (!std::isfinite(pitch_rad) || reveal <= 1.0e-4) { + positive_fill_visible_ = false; + negative_fill_visible_ = false; + set_visible(false); + return; } - for (auto& line : gimbal_pitch_indicator_) { - line.set_color(Shape::Color::YELLOW); - line.set_width(2); - line.set_visible(false); - } - for (auto& line : chassis_pitch_indicator_) { - line.set_color(Shape::Color::YELLOW); - line.set_width(2); - line.set_visible(false); - } - } + apply_geometry_(reveal); - uint16_t axis_top_y_() const { - return clamp_to_screen_y_( - static_cast(config_.center_y) - static_cast(config_.half_height_px)); - } + const double visible_half_span_deg = config_.half_span_deg * reveal; + const double pitch_deg = + std::clamp(rad_to_deg_(pitch_rad), -visible_half_span_deg, visible_half_span_deg); + const double fill_deg = std::abs(pitch_deg); - uint16_t axis_bottom_y_() const { - return clamp_to_screen_y_( - static_cast(config_.center_y) + static_cast(config_.half_height_px)); - } + const bool warning = pitch_deg > config_.warning_pitch_deg; + const auto fill_color = warning ? Shape::Color::PINK : Shape::Color::YELLOW; + positive_fill_arc_.set_color(fill_color); + negative_fill_arc_.set_color(fill_color); + + positive_fill_visible_ = pitch_deg >= 0.0 && fill_deg > 1.0e-4; + negative_fill_visible_ = pitch_deg < 0.0 && fill_deg > 1.0e-4; + + positive_fill_arc_.set_angle_start(to_referee_angle_(pitch_deg)); + positive_fill_arc_.set_angle_end(mid_angle_()); + + negative_fill_arc_.set_angle_start(mid_angle_()); + negative_fill_arc_.set_angle_end(to_referee_angle_(pitch_deg)); - static constexpr double deg_to_rad_(double degrees) { - return degrees * std::numbers::pi / 180.0; + set_visible(true); } - static uint16_t clamp_to_screen_y_(double y) { - return static_cast(std::clamp( - std::lround(y), 0l, static_cast(screen_height - 1))); +private: + void initialize_() { + background_arc_.set_color(Shape::Color::ORANGE); + positive_fill_arc_.set_color(Shape::Color::YELLOW); + negative_fill_arc_.set_color(Shape::Color::YELLOW); + positive_fill_visible_ = false; + negative_fill_visible_ = false; + set_visible(false); } - uint16_t pitch_to_hud_y_(double pitch_rad) const { - const double clamped_pitch = - std::clamp(pitch_rad, -deg_to_rad_(config_.half_span_deg), - deg_to_rad_(config_.half_span_deg)); - const double normalized = clamped_pitch / deg_to_rad_(config_.half_span_deg); - return clamp_to_screen_y_( - static_cast(config_.center_y) - + normalized * static_cast(config_.half_height_px)); + void apply_geometry_(double reveal) { + const uint16_t width = std::max(1, static_cast(std::lround( + static_cast(config_.width_px) * reveal))); + const double half_span_deg = config_.half_span_deg * reveal; + + background_arc_.set_x(config_.center_x); + background_arc_.set_y(config_.center_y); + background_arc_.set_r(config_.radius_px); + background_arc_.set_width(width); + background_arc_.set_angle_start(to_referee_angle_(-half_span_deg)); + background_arc_.set_angle_end(to_referee_angle_(half_span_deg)); + + positive_fill_arc_.set_x(config_.center_x); + positive_fill_arc_.set_y(config_.center_y); + positive_fill_arc_.set_r(config_.radius_px); + positive_fill_arc_.set_width(width); + + negative_fill_arc_.set_x(config_.center_x); + negative_fill_arc_.set_y(config_.center_y); + negative_fill_arc_.set_r(config_.radius_px); + negative_fill_arc_.set_width(width); } - void update_gimbal_pitch_indicator_(uint16_t y) { - const uint16_t tip_x = config_.center_x - 8; - const uint16_t back_x = config_.center_x - 20; - constexpr uint16_t half_height = 8; - const uint16_t top_y = clamp_to_screen_y_(static_cast(y) - half_height); - const uint16_t bottom_y = clamp_to_screen_y_(static_cast(y) + half_height); - - gimbal_pitch_indicator_[0].set_x(back_x); - gimbal_pitch_indicator_[0].set_y(top_y); - gimbal_pitch_indicator_[0].set_x2(tip_x); - gimbal_pitch_indicator_[0].set_y2(y); - - gimbal_pitch_indicator_[1].set_x(back_x); - gimbal_pitch_indicator_[1].set_y(bottom_y); - gimbal_pitch_indicator_[1].set_x2(tip_x); - gimbal_pitch_indicator_[1].set_y2(y); - - gimbal_pitch_indicator_[2].set_x(back_x); - gimbal_pitch_indicator_[2].set_y(top_y); - gimbal_pitch_indicator_[2].set_x2(back_x); - gimbal_pitch_indicator_[2].set_y2(bottom_y); - - set_indicator_visible_(gimbal_pitch_indicator_, true); + static constexpr double rad_to_deg_(double radians) { + return radians * 180.0 / 3.14159265358979323846; } - void update_chassis_pitch_indicator_(uint16_t y) { - const uint16_t front_x = config_.center_x - 20; - const uint16_t rear_x = config_.center_x - 34; - constexpr uint16_t front_half_height = 12; - constexpr uint16_t rear_half_height = 8; - const uint16_t front_top_y = clamp_to_screen_y_(static_cast(y) - front_half_height); - const uint16_t front_bottom_y = - clamp_to_screen_y_(static_cast(y) + front_half_height); - const uint16_t rear_top_y = clamp_to_screen_y_(static_cast(y) - rear_half_height); - const uint16_t rear_bottom_y = - clamp_to_screen_y_(static_cast(y) + rear_half_height); - - chassis_pitch_indicator_[0].set_x(rear_x); - chassis_pitch_indicator_[0].set_y(rear_top_y); - chassis_pitch_indicator_[0].set_x2(front_x); - chassis_pitch_indicator_[0].set_y2(front_top_y); - - chassis_pitch_indicator_[1].set_x(front_x); - chassis_pitch_indicator_[1].set_y(front_top_y); - chassis_pitch_indicator_[1].set_x2(front_x); - chassis_pitch_indicator_[1].set_y2(front_bottom_y); - - chassis_pitch_indicator_[2].set_x(front_x); - chassis_pitch_indicator_[2].set_y(front_bottom_y); - chassis_pitch_indicator_[2].set_x2(rear_x); - chassis_pitch_indicator_[2].set_y2(rear_bottom_y); - - chassis_pitch_indicator_[3].set_x(rear_x); - chassis_pitch_indicator_[3].set_y(rear_bottom_y); - chassis_pitch_indicator_[3].set_x2(rear_x); - chassis_pitch_indicator_[3].set_y2(rear_top_y); - - set_indicator_visible_(chassis_pitch_indicator_, true); + static uint16_t wrap_angle_(long angle) { + angle %= 360; + if (angle < 0) + angle += 360; + return static_cast(angle); } - template - static void set_indicator_visible_(std::array& indicator, bool visible) { - for (auto& line : indicator) - line.set_visible(visible); + static constexpr uint16_t mid_angle_() { return 180; } + + static uint16_t to_referee_angle_(double display_pitch_deg) { + return wrap_angle_(std::lround(static_cast(mid_angle_()) + display_pitch_deg)); } Config config_{}; - std::size_t tick_count_ = 0; - Line axis_; - std::array ticks_; - std::array gimbal_pitch_indicator_; - std::array chassis_pitch_indicator_; + Arc background_arc_{Shape::Color::WHITE, 1, 0, 0, 0, 0, 1, 1, false}; + Arc positive_fill_arc_{Shape::Color::YELLOW, 1, 0, 0, 0, 0, 1, 1, false}; + Arc negative_fill_arc_{Shape::Color::YELLOW, 1, 0, 0, 0, 0, 1, 1, false}; + bool positive_fill_visible_ = false; + bool negative_fill_visible_ = false; }; } // namespace rmcs_core::referee::app::ui From 5ac682411cef708283ce30703d5a25c3cda8f5e0 Mon Sep 17 00:00:00 2001 From: ZGZ713912 Date: Fri, 15 May 2026 07:57:39 +0800 Subject: [PATCH 14/52] feat: update supercap command transmission in deformable infantry hardware --- rmcs_ws/src/rmcs_auto_aim_v2 | 2 +- .../controller/chassis/chassis_power_controller.cpp | 2 +- .../src/hardware/deformable-infantry-omni-b.cpp | 11 +++++++++++ .../src/hardware/deformable-infantry-omni.cpp | 11 +++++++++++ 4 files changed, 24 insertions(+), 2 deletions(-) diff --git a/rmcs_ws/src/rmcs_auto_aim_v2 b/rmcs_ws/src/rmcs_auto_aim_v2 index 8f25103a..968a36c3 160000 --- a/rmcs_ws/src/rmcs_auto_aim_v2 +++ b/rmcs_ws/src/rmcs_auto_aim_v2 @@ -1 +1 @@ -Subproject commit 8f25103a42fd601bd8259a3da51ad8ef870a5b6b +Subproject commit 968a36c359c654e00fb9084ab1e5551810a19233 diff --git a/rmcs_ws/src/rmcs_core/src/controller/chassis/chassis_power_controller.cpp b/rmcs_ws/src/rmcs_core/src/controller/chassis/chassis_power_controller.cpp index ec120e7c..22be1ef3 100644 --- a/rmcs_ws/src/rmcs_core/src/controller/chassis/chassis_power_controller.cpp +++ b/rmcs_ws/src/rmcs_core/src/controller/chassis/chassis_power_controller.cpp @@ -155,7 +155,7 @@ class ChassisPowerController InputInterface rotary_knob_; InputInterface chassis_power_; - static constexpr double virtual_buffer_energy_limit_ = 40.0; + static constexpr double virtual_buffer_energy_limit_ = 30.0; double virtual_buffer_energy_; InputInterface supercap_voltage_; diff --git a/rmcs_ws/src/rmcs_core/src/hardware/deformable-infantry-omni-b.cpp b/rmcs_ws/src/rmcs_core/src/hardware/deformable-infantry-omni-b.cpp index 9be7189a..b2d55992 100644 --- a/rmcs_ws/src/rmcs_core/src/hardware/deformable-infantry-omni-b.cpp +++ b/rmcs_ws/src/rmcs_core/src/hardware/deformable-infantry-omni-b.cpp @@ -314,6 +314,17 @@ class DeformableInfantryOmniB .can_id = 0x142, .can_data = gimbal_yaw_motor_.generate_torque_command().as_bytes(), }); + builder.can1_transmit({ + .can_id = 0x1FE, + .can_data = + device::CanPacket8{ + device::CanPacket8::PaddingQuarter{}, + device::CanPacket8::PaddingQuarter{}, + device::CanPacket8::PaddingQuarter{}, + supercap_.generate_command(), + } + .as_bytes(), + }); } else { builder.can0_transmit({ .can_id = 0x141, diff --git a/rmcs_ws/src/rmcs_core/src/hardware/deformable-infantry-omni.cpp b/rmcs_ws/src/rmcs_core/src/hardware/deformable-infantry-omni.cpp index 561c22c0..5dfbcc6a 100644 --- a/rmcs_ws/src/rmcs_core/src/hardware/deformable-infantry-omni.cpp +++ b/rmcs_ws/src/rmcs_core/src/hardware/deformable-infantry-omni.cpp @@ -296,6 +296,17 @@ class DeformableInfantryOmni } .as_bytes(), }); + builder.can1_transmit({ + .can_id = 0x1FE, + .can_data = + device::CanPacket8{ + device::CanPacket8::PaddingQuarter{}, + device::CanPacket8::PaddingQuarter{}, + device::CanPacket8::PaddingQuarter{}, + supercap_.generate_command(), + } + .as_bytes(), + }); } else { builder.can0_transmit({ .can_id = 0x141, From ac7aaae9c4cf555e4dd1c8c471da9a76ffb342df Mon Sep 17 00:00:00 2001 From: ZGZ713912 Date: Sat, 16 May 2026 13:48:57 +0800 Subject: [PATCH 15/52] Refactor Deformable Infantry UI and Remove Pitch HUD - Removed unnecessary includes and the PitchHud widget from deformable_infantry_ui.cpp. - Adjusted crosshair_circle positioning in the DeformableInfantry constructor. - Updated crosshair_circle's x and y coordinates based on new calculations. - Removed unused input registration for chassis_pitch_. - Added getter methods for x and y coordinates in the CrossHairCircle class. - Introduced a new Handler class for managing USB communication in librmcs. - Implemented a transport layer for USB communication, including buffer management and event handling. - Added error handling and logging for USB transport failures. - Created a PacketBuilder class for building packets to be sent over USB. - Established a reconnect mechanism for USB transport to handle connection issues. --- .../config/deformable-infantry-omni-b.yaml | 6 +- .../config/deformable-infantry-omni.yaml | 4 +- rmcs_ws/src/rmcs_core/CMakeLists.txt | 21 + .../chassis/chassis_power_controller.cpp | 5 +- .../controller/chassis/deformable_chassis.cpp | 214 ++++++++-- .../referee/app/ui/deformable_infantry_ui.cpp | 34 +- .../app/ui/widget/crosshair_circle.hpp | 2 + .../src/referee/app/ui/widget/pitch_hud.hpp | 132 ------ .../host/include/librmcs/protocol/handler.hpp | 61 +++ .../librmcs/host/src/protocol/handler.cpp | 390 +++++++++++++++++ .../librmcs/host/src/transport/transport.hpp | 56 +++ .../librmcs/host/src/transport/usb/usb.cpp | 398 ++++++++++++++++++ 12 files changed, 1126 insertions(+), 197 deletions(-) delete mode 100644 rmcs_ws/src/rmcs_core/src/referee/app/ui/widget/pitch_hud.hpp create mode 100644 rmcs_ws/src/rmcs_core/third_party_overrides/librmcs/host/include/librmcs/protocol/handler.hpp create mode 100644 rmcs_ws/src/rmcs_core/third_party_overrides/librmcs/host/src/protocol/handler.cpp create mode 100644 rmcs_ws/src/rmcs_core/third_party_overrides/librmcs/host/src/transport/transport.hpp create mode 100644 rmcs_ws/src/rmcs_core/third_party_overrides/librmcs/host/src/transport/usb/usb.cpp diff --git a/rmcs_ws/src/rmcs_bringup/config/deformable-infantry-omni-b.yaml b/rmcs_ws/src/rmcs_bringup/config/deformable-infantry-omni-b.yaml index 0b28cf37..a235d389 100644 --- a/rmcs_ws/src/rmcs_bringup/config/deformable-infantry-omni-b.yaml +++ b/rmcs_ws/src/rmcs_bringup/config/deformable-infantry-omni-b.yaml @@ -41,14 +41,14 @@ value_broadcaster: deformable_infantry: ros__parameters: - serial_filter_rmcs_board: "AF-23FB-EE32-B892-1302-AE70-D640-7B4E-0CBF" + serial_filter_rmcs_board: "AF-73B2-E8A1-A544-79ED-5BDA-D088-7F21-A6A6" serial_filter_top_board: "AF-ABAC-786D-1B53-99F6-00A2-42A6-AA95-9D69" serial_filter_imu: "AF-C26A-0C9C-CF41-3E3C-1596-524B-7527-5744" left_front_zero_point: 7173 left_back_zero_point: 5167 right_back_zero_point: 3098 right_front_zero_point: 6485 - yaw_motor_zero_point: 25406 + yaw_motor_zero_point: 18211 pitch_motor_zero_point: 56354 debug_log_supercap: false debug_log_wheel_motor: false @@ -57,7 +57,7 @@ deformable_infantry: chassis_controller: ros__parameters: # Deploy geometry / chassis-owned joint intent - min_angle: 7.0 + min_angle: 8.0 max_angle: 58.0 active_suspension_enable: true spin_ratio: 1.0 diff --git a/rmcs_ws/src/rmcs_bringup/config/deformable-infantry-omni.yaml b/rmcs_ws/src/rmcs_bringup/config/deformable-infantry-omni.yaml index 84bfd1e3..3885fdd2 100644 --- a/rmcs_ws/src/rmcs_bringup/config/deformable-infantry-omni.yaml +++ b/rmcs_ws/src/rmcs_bringup/config/deformable-infantry-omni.yaml @@ -40,14 +40,14 @@ value_broadcaster: deformable_infantry: ros__parameters: - serial_filter_rmcs_board: "AF-73B2-E8A1-A544-79ED-5BDA-D088-7F21-A6A6" + serial_filter_rmcs_board: "AF-23FB-EE32-B892-1302-AE70-D640-7B4E-0CBF" serial_filter_top_board: "D4-0262-9E84-E715-CADB-9894-7241" serial_filter_imu: "AF-8217-B05F-811B-6F3E-04EA-448E-9D03-CA2C" left_front_zero_point: 374 left_back_zero_point: 5801 right_back_zero_point: 7817 right_front_zero_point: 7136 - yaw_motor_zero_point: 50642 + yaw_motor_zero_point: 31856 pitch_motor_zero_point: 6853 debug_log_supercap: false debug_log_wheel_motor: false diff --git a/rmcs_ws/src/rmcs_core/CMakeLists.txt b/rmcs_ws/src/rmcs_core/CMakeLists.txt index d275c6db..89fbdb4f 100644 --- a/rmcs_ws/src/rmcs_core/CMakeLists.txt +++ b/rmcs_ws/src/rmcs_core/CMakeLists.txt @@ -23,6 +23,27 @@ FetchContent_Declare( ) FetchContent_MakeAvailable(librmcs) +configure_file( + ${PROJECT_SOURCE_DIR}/third_party_overrides/librmcs/host/include/librmcs/protocol/handler.hpp + ${librmcs_SOURCE_DIR}/host/include/librmcs/protocol/handler.hpp + COPYONLY +) +configure_file( + ${PROJECT_SOURCE_DIR}/third_party_overrides/librmcs/host/src/protocol/handler.cpp + ${librmcs_SOURCE_DIR}/host/src/protocol/handler.cpp + COPYONLY +) +configure_file( + ${PROJECT_SOURCE_DIR}/third_party_overrides/librmcs/host/src/transport/transport.hpp + ${librmcs_SOURCE_DIR}/host/src/transport/transport.hpp + COPYONLY +) +configure_file( + ${PROJECT_SOURCE_DIR}/third_party_overrides/librmcs/host/src/transport/usb/usb.cpp + ${librmcs_SOURCE_DIR}/host/src/transport/usb/usb.cpp + COPYONLY +) + file(GLOB_RECURSE PROJECT_SOURCE CONFIGURE_DEPENDS ${PROJECT_SOURCE_DIR}/src/*.cpp ${PROJECT_SOURCE_DIR}/src/*.c diff --git a/rmcs_ws/src/rmcs_core/src/controller/chassis/chassis_power_controller.cpp b/rmcs_ws/src/rmcs_core/src/controller/chassis/chassis_power_controller.cpp index 22be1ef3..d4988239 100644 --- a/rmcs_ws/src/rmcs_core/src/controller/chassis/chassis_power_controller.cpp +++ b/rmcs_ws/src/rmcs_core/src/controller/chassis/chassis_power_controller.cpp @@ -109,9 +109,8 @@ class ChassisPowerController void update_control_power_limit() { double power_limit; - const bool supercap_active = *supercap_control_enabled_ && *supercap_enabled_; - if (supercap_active) + if (*supercap_control_enabled_ && *supercap_enabled_) power_limit = *mode_ == rmcs_msgs::ChassisMode::LAUNCH_RAMP ? inf_ : *chassis_power_limit_referee_ + 80.0; @@ -130,7 +129,7 @@ class ChassisPowerController 0.0, 1.0); // Maximum excess power when virtual buffer energy is full. - const double excess_power_limit = supercap_active ? 15.0 : 15.0; + constexpr double excess_power_limit = 15; power_limit += excess_power_limit; power_limit *= virtual_buffer_energy_ / virtual_buffer_energy_limit_; diff --git a/rmcs_ws/src/rmcs_core/src/controller/chassis/deformable_chassis.cpp b/rmcs_ws/src/rmcs_core/src/controller/chassis/deformable_chassis.cpp index 8e4aee81..1473f296 100644 --- a/rmcs_ws/src/rmcs_core/src/controller/chassis/deformable_chassis.cpp +++ b/rmcs_ws/src/rmcs_core/src/controller/chassis/deformable_chassis.cpp @@ -5,12 +5,14 @@ #include #include #include +#include #include #include #include #include +#include #include #include #include @@ -62,8 +64,8 @@ class DeformableChassis , following_velocity_controller_(10.0, 0.0, 0.0) , spin_ratio_(std::clamp(get_parameter_or("spin_ratio", 0.6), 0.0, 1.0)) - , min_angle_(get_parameter_or("min_angle", 15.0)) - , max_angle_(get_parameter_or("max_angle", 55.0)) + , min_angle_(get_parameter_or("min_angle", 7.0)) + , max_angle_(get_parameter_or("max_angle", 58.0)) , target_physical_velocity_limit_( std::max( deg_to_rad(std::abs(get_parameter_or("target_physical_velocity_limit", 180.0))), @@ -130,6 +132,7 @@ class DeformableChassis register_input("/remote/keyboard", keyboard_); register_input("/remote/rotary_knob", rotary_knob_); register_input("/predefined/update_rate", update_rate_); + register_input("/tf", tf_, false); register_input("/gimbal/yaw/angle", gimbal_yaw_angle_, false); register_input("/gimbal/yaw/control_angle_error", gimbal_yaw_angle_error_, false); @@ -259,6 +262,8 @@ class DeformableChassis static constexpr double translational_velocity_max_ = 10.0; static constexpr double angular_velocity_max_ = 30.0; static constexpr double rad_to_deg_ = 180.0 / std::numbers::pi; + static constexpr double imu_calibration_offset_limit_rad_ = + 1.0 * std::numbers::pi / 180.0; void validate_joint_feedback_inputs() const { if (left_front_joint_physical_angle_.ready() && left_back_joint_physical_angle_.ready() @@ -273,12 +278,36 @@ class DeformableChassis rmcs_msgs::Switch switch_left, rmcs_msgs::Switch switch_right, const rmcs_msgs::Keyboard& keyboard) { auto mode = *mode_; + const bool q_pressed = keyboard.q; + const bool e_pressed = keyboard.e; + const bool last_q_pressed = last_keyboard_.q; + const bool last_e_pressed = last_keyboard_.e; + const bool last_c_pressed = last_keyboard_.c; + const bool qe_combo_pressed = q_pressed && e_pressed; + const bool last_qe_combo_pressed = last_q_pressed && last_e_pressed; + const bool e_rising = !last_e_pressed && e_pressed; + const bool c_rising = !last_c_pressed && keyboard.c; + const bool qe_combo_rising = !last_qe_combo_pressed && qe_combo_pressed; if (switch_left == rmcs_msgs::Switch::DOWN) { deactivate_complex_spin_(); + deactivate_qe_complex_spin_(); return; } - if (!last_keyboard_.e && keyboard.e) { + if (qe_complex_spin_active_) { + if (c_rising) { + deactivate_qe_complex_spin_(); + apply_symmetric_target = true; + lf_current_target_angle_ = current_target_angle_; + lb_current_target_angle_ = current_target_angle_; + rb_current_target_angle_ = current_target_angle_; + rf_current_target_angle_ = current_target_angle_; + mode = rmcs_msgs::ChassisMode::SPIN; + } + } else if (qe_combo_rising) { + deactivate_complex_spin_(); + activate_qe_complex_spin_(mode); + } else if (e_rising && !q_pressed) { if (complex_spin_active_) { deactivate_complex_spin_(); if (mode == rmcs_msgs::ChassisMode::SPIN) @@ -289,6 +318,7 @@ class DeformableChassis } else if (last_switch_right_ == rmcs_msgs::Switch::MIDDLE && switch_right == rmcs_msgs::Switch::DOWN) { deactivate_complex_spin_(); + deactivate_qe_complex_spin_(); if (mode == rmcs_msgs::ChassisMode::SPIN) { mode = rmcs_msgs::ChassisMode::STEP_DOWN; } else { @@ -297,6 +327,7 @@ class DeformableChassis } } else if (!last_keyboard_.c && keyboard.c) { deactivate_complex_spin_(); + deactivate_qe_complex_spin_(); if (mode == rmcs_msgs::ChassisMode::SPIN) { mode = rmcs_msgs::ChassisMode::AUTO; } else { @@ -305,16 +336,18 @@ class DeformableChassis } } else if (!last_keyboard_.x && keyboard.x) { deactivate_complex_spin_(); + deactivate_qe_complex_spin_(); mode = mode == rmcs_msgs::ChassisMode::LAUNCH_RAMP ? rmcs_msgs::ChassisMode::AUTO : rmcs_msgs::ChassisMode::LAUNCH_RAMP; } else if (!last_keyboard_.z && keyboard.z) { deactivate_complex_spin_(); + deactivate_qe_complex_spin_(); mode = mode == rmcs_msgs::ChassisMode::STEP_DOWN ? rmcs_msgs::ChassisMode::AUTO : rmcs_msgs::ChassisMode::STEP_DOWN; } - if (complex_spin_active_) + if (complex_spin_active_ || qe_complex_spin_active_) mode = rmcs_msgs::ChassisMode::SPIN; *mode_ = mode; @@ -335,6 +368,109 @@ class DeformableChassis complex_spin_elapsed_ = 0.0; } + void activate_qe_complex_spin_(rmcs_msgs::ChassisMode& mode) { + qe_complex_spin_active_ = true; + qe_last_toggle_elapsed_ = 0.0; + qe_front_window_armed_ = true; + qe_rear_window_armed_ = true; + qe_front_high_rear_low_ = true; + apply_front_high_rear_low_target_(); + if (mode != rmcs_msgs::ChassisMode::SPIN) { + mode = rmcs_msgs::ChassisMode::SPIN; + spinning_forward_ = !spinning_forward_; + } + } + + void deactivate_qe_complex_spin_() { + qe_complex_spin_active_ = false; + qe_last_toggle_elapsed_ = 0.0; + qe_front_window_armed_ = true; + qe_rear_window_armed_ = true; + } + + void apply_front_high_rear_low_target_() { + lf_current_target_angle_ = max_angle_; + rf_current_target_angle_ = max_angle_; + lb_current_target_angle_ = min_angle_; + rb_current_target_angle_ = min_angle_; + apply_symmetric_target = false; + } + + void apply_front_low_rear_high_target_() { + lf_current_target_angle_ = min_angle_; + rf_current_target_angle_ = min_angle_; + lb_current_target_angle_ = max_angle_; + rb_current_target_angle_ = max_angle_; + apply_symmetric_target = false; + } + + void toggle_qe_complex_spin_target_() { + qe_front_high_rear_low_ = !qe_front_high_rear_low_; + if (qe_front_high_rear_low_) { + apply_front_high_rear_low_target_(); + } else { + apply_front_low_rear_high_target_(); + } + qe_last_toggle_elapsed_ = 0.0; + } + + static double wrap_to_pi_(double angle) { + while (angle > std::numbers::pi) + angle -= 2.0 * std::numbers::pi; + while (angle < -std::numbers::pi) + angle += 2.0 * std::numbers::pi; + return angle; + } + + static double heading_from_direction_(const Eigen::Vector3d& direction) { + return std::atan2(direction.y(), direction.x()); + } + + std::optional> read_qe_spin_headings_() const { + if (!tf_.ready()) + return std::nullopt; + + const auto chassis_direction = fast_tf::cast( + rmcs_description::PitchLink::DirectionVector{Eigen::Vector3d::UnitX()}, *tf_); + const auto gimbal_direction = fast_tf::cast( + rmcs_description::YawLink::DirectionVector{Eigen::Vector3d::UnitX()}, *tf_); + + return std::pair{ + heading_from_direction_(*chassis_direction), heading_from_direction_(*gimbal_direction)}; + } + + void update_qe_complex_spin_toggle_() { + constexpr double heading_trigger_threshold = 20.0 * std::numbers::pi / 180.0; + constexpr double heading_rearm_threshold = 40.0 * std::numbers::pi / 180.0; + constexpr double force_toggle_timeout = 0.25; + + qe_last_toggle_elapsed_ += update_dt(); + + bool should_toggle = false; + if (const auto headings = read_qe_spin_headings_()) { + const auto& [chassis_heading, gimbal_heading] = *headings; + const double relative_heading = wrap_to_pi_(chassis_heading - gimbal_heading); + const double front_error = std::abs(relative_heading); + const double rear_error = std::abs(std::abs(relative_heading) - std::numbers::pi); + + if (front_error >= heading_rearm_threshold) + qe_front_window_armed_ = true; + if (rear_error >= heading_rearm_threshold) + qe_rear_window_armed_ = true; + + if (front_error <= heading_trigger_threshold && qe_front_window_armed_) { + should_toggle = true; + qe_front_window_armed_ = false; + } else if (rear_error <= heading_trigger_threshold && qe_rear_window_armed_) { + should_toggle = true; + qe_rear_window_armed_ = false; + } + } + + if (should_toggle || qe_last_toggle_elapsed_ >= force_toggle_timeout) + toggle_qe_complex_spin_target_(); + } + std::array read_current_joint_physical_angles_() const { const std::array*, kJointCount> physical_angle_inputs{ &left_front_joint_physical_angle_, &left_back_joint_physical_angle_, @@ -392,6 +528,9 @@ class DeformableChassis } void update_chassis_imu_calibration_() { + if (chassis_imu_calibrated_once_) + return; + if (!symmetric_joint_target_requested_()) { reset_chassis_imu_calibration_window_(); return; @@ -426,10 +565,13 @@ class DeformableChassis return; } - chassis_imu_pitch_offset_ = - chassis_imu_pitch_sum_ / static_cast(chassis_imu_calibration_sample_count_); - chassis_imu_roll_offset_ = - chassis_imu_roll_sum_ / static_cast(chassis_imu_calibration_sample_count_); + chassis_imu_pitch_offset_ = std::clamp( + chassis_imu_pitch_sum_ / static_cast(chassis_imu_calibration_sample_count_), + -imu_calibration_offset_limit_rad_, imu_calibration_offset_limit_rad_); + chassis_imu_roll_offset_ = std::clamp( + chassis_imu_roll_sum_ / static_cast(chassis_imu_calibration_sample_count_), + -imu_calibration_offset_limit_rad_, imu_calibration_offset_limit_rad_); + chassis_imu_calibrated_once_ = true; RCLCPP_INFO( get_logger(), "[chassis imu calibration] pitch_offset=% .3f deg roll_offset=% .3f deg " @@ -453,6 +595,10 @@ class DeformableChassis return true; } + double active_suspension_min_angle_rad_() const { + return deg_to_rad(min_angle_ - 5.0); + } + void update_active_suspension_() { if (!suspension_requested_by_input_()) { reset_attitude_correction_state_(); @@ -460,7 +606,7 @@ class DeformableChassis } constexpr double max_attitude = 30.0 * std::numbers::pi / 180.0; - const double base_target_angle = deg_to_rad(min_angle_); + const double base_target_angle = active_suspension_min_angle_rad_(); const double max_target_angle = deg_to_rad(max_angle_); const double corrected_pitch = std::clamp(*chassis_imu_pitch_ - chassis_imu_pitch_offset_, -max_attitude, max_attitude); @@ -481,24 +627,28 @@ class DeformableChassis } // Positive pitch_angle_diff raises the rear pair. Positive roll_angle_diff raises the left - // pair. Every leg starts from min_angle and only receives additive corrections so at least - // one leg always stays at min_angle. + // pair. Every leg starts from the active-suspension minimum and only receives additive + // corrections so at least one leg always stays at that minimum. const double front_pitch_add = std::max(-pitch_angle_diff, 0.0); const double back_pitch_add = std::max(pitch_angle_diff, 0.0); const double left_roll_add = std::max(roll_angle_diff, 0.0); const double right_roll_add = std::max(-roll_angle_diff, 0.0); current_target_physical_angles_rad_[kLeftFront] = - std::clamp(base_target_angle + front_pitch_add + left_roll_add, base_target_angle, + std::clamp( + base_target_angle + front_pitch_add + left_roll_add, base_target_angle, max_target_angle); current_target_physical_angles_rad_[kLeftBack] = - std::clamp(base_target_angle + back_pitch_add + left_roll_add, base_target_angle, + std::clamp( + base_target_angle + back_pitch_add + left_roll_add, base_target_angle, max_target_angle); current_target_physical_angles_rad_[kRightBack] = - std::clamp(base_target_angle + back_pitch_add + right_roll_add, base_target_angle, + std::clamp( + base_target_angle + back_pitch_add + right_roll_add, base_target_angle, max_target_angle); current_target_physical_angles_rad_[kRightFront] = - std::clamp(base_target_angle + front_pitch_add + right_roll_add, base_target_angle, + std::clamp( + base_target_angle + front_pitch_add + right_roll_add, base_target_angle, max_target_angle); joint_suspension_active_.fill(true); @@ -521,6 +671,7 @@ class DeformableChassis joint_target_active_ = false; suspension_on_by_switch = false; deactivate_complex_spin_(); + deactivate_qe_complex_spin_(); *scope_motor_control_torque = nan_; @@ -632,14 +783,15 @@ class DeformableChassis void update_lift_target_toggle(rmcs_msgs::Keyboard keyboard) { constexpr double rotary_knob_edge_threshold = 0.7; - constexpr double complex_spin_toggle_period = 0.5; + constexpr double complex_spin_toggle_period = 0.2; - const bool keyboard_toggle_condition = !last_keyboard_.q && keyboard.q; + const bool keyboard_toggle_condition = + !qe_complex_spin_active_ && !last_keyboard_.q && keyboard.q && !keyboard.e; const bool rotary_knob_toggle_condition = last_rotary_knob_ < rotary_knob_edge_threshold && *rotary_knob_ >= rotary_knob_edge_threshold; - const bool front_high_rear_low = !last_keyboard_.b && keyboard.b; - const bool front_low_rear_high = !last_keyboard_.g && keyboard.g; + const bool front_high_rear_low = !qe_complex_spin_active_ && !last_keyboard_.b && keyboard.b; + const bool front_low_rear_high = !qe_complex_spin_active_ && !last_keyboard_.g && keyboard.g; bool complex_spin_toggle_condition = false; if (complex_spin_active_) { @@ -652,6 +804,9 @@ class DeformableChassis complex_spin_toggle_condition = (complex_spin_toggle_count % 2) == 1; } + if (qe_complex_spin_active_) + update_qe_complex_spin_toggle_(); + if (apply_symmetric_target) { lf_current_target_angle_ = current_target_angle_; lb_current_target_angle_ = current_target_angle_; @@ -664,17 +819,9 @@ class DeformableChassis (std::abs(current_target_angle_ - max_angle_) < 1e-6) ? min_angle_ : max_angle_; apply_symmetric_target = true; } else if (front_high_rear_low) { - lf_current_target_angle_ = max_angle_; - rf_current_target_angle_ = max_angle_; - lb_current_target_angle_ = min_angle_; - rb_current_target_angle_ = min_angle_; - apply_symmetric_target = false; + apply_front_high_rear_low_target_(); } else if (front_low_rear_high) { - lf_current_target_angle_ = min_angle_; - rf_current_target_angle_ = min_angle_; - lb_current_target_angle_ = max_angle_; - rb_current_target_angle_ = max_angle_; - apply_symmetric_target = false; + apply_front_low_rear_high_target_(); } last_rotary_knob_ = *rotary_knob_; @@ -697,7 +844,7 @@ class DeformableChassis current_target_physical_angles_rad_[kRightBack] = deg_to_rad(rb_current_target_angle_); current_target_physical_angles_rad_[kRightFront] = deg_to_rad(rf_current_target_angle_); if (suspension_requested) { - current_target_physical_angles_rad_.fill(deg_to_rad(min_angle_)); + current_target_physical_angles_rad_.fill(active_suspension_min_angle_rad_()); } update_chassis_imu_calibration_(); @@ -842,6 +989,7 @@ class DeformableChassis } private: + InputInterface tf_; InputInterface joystick_right_; InputInterface switch_right_; InputInterface switch_left_; @@ -864,6 +1012,11 @@ class DeformableChassis bool apply_symmetric_target = true; bool complex_spin_active_ = false; double complex_spin_elapsed_ = 0.0; + bool qe_complex_spin_active_ = false; + bool qe_front_window_armed_ = true; + bool qe_rear_window_armed_ = true; + bool qe_front_high_rear_low_ = true; + double qe_last_toggle_elapsed_ = 0.0; pid::PidCalculator following_velocity_controller_; const double spin_ratio_; @@ -939,6 +1092,7 @@ class DeformableChassis double chassis_imu_pitch_sum_ = 0.0; double chassis_imu_roll_sum_ = 0.0; bool chassis_imu_calibration_completed_for_window_ = false; + bool chassis_imu_calibrated_once_ = false; static constexpr double default_dt_ = 1e-3; }; diff --git a/rmcs_ws/src/rmcs_core/src/referee/app/ui/deformable_infantry_ui.cpp b/rmcs_ws/src/rmcs_core/src/referee/app/ui/deformable_infantry_ui.cpp index 5bb326e2..398f75d8 100644 --- a/rmcs_ws/src/rmcs_core/src/referee/app/ui/deformable_infantry_ui.cpp +++ b/rmcs_ws/src/rmcs_core/src/referee/app/ui/deformable_infantry_ui.cpp @@ -3,7 +3,6 @@ #include #include #include -#include #include #include @@ -16,7 +15,6 @@ #include "referee/app/ui/widget/animated_toggle.hpp" #include "referee/app/ui/widget/crosshair_circle.hpp" #include "referee/app/ui/widget/deformable_chassis_top_view.hpp" -#include "referee/app/ui/widget/pitch_hud.hpp" #include "referee/app/ui/widget/status_ring.hpp" namespace rmcs_core::referee::app::ui { @@ -28,7 +26,7 @@ class DeformableInfantry public: DeformableInfantry() : Node{get_component_name(), rclcpp::NodeOptions{}.automatically_declare_parameters_from_overrides(true)} - , crosshair_circle_(Shape::Color::WHITE, x_center - 14, y_center - 42, 8, 2) + , crosshair_circle_(Shape::Color::WHITE, x_center - 22, y_center - 42, 8, 2) , status_ring_(26.5, 26.5, 600, 300) , horizontal_center_guidelines_( {Shape::Color::WHITE, 2, x_center - 360, y_center, x_center - 110, y_center}, @@ -37,14 +35,6 @@ class DeformableInfantry {Shape::Color::WHITE, 2, x_center, 800, x_center, y_center + 110}, {Shape::Color::WHITE, 2, x_center, y_center - 110, x_center, 200}) , chassis_direction_indicator_(Shape::Color::PINK, 8, x_center, y_center, 0, 0, 84, 84) - , pitch_hud_(PitchHud::Config{ - .center_x = x_center, - .center_y = y_center, - .radius_px = 95, - .width_px = 12, - .half_span_deg = 30.0, - .warning_pitch_deg = 5.0, - }) , time_reminder_(Shape::Color::PINK, 50, 5, x_center + 150, y_center + 65, 0, false) { double deformable_leg_min_angle_deg = 8.0; @@ -62,8 +52,6 @@ class DeformableInfantry register_input("/chassis/control_mode", chassis_mode_); register_input("/chassis/angle", chassis_angle_); - register_input("/chassis/imu/pitch", chassis_pitch_, false); - register_input("/chassis/supercap/voltage", supercap_voltage_); register_input("/chassis/supercap/enabled", supercap_enabled_); @@ -92,12 +80,11 @@ class DeformableInfantry register_input("/remote/mouse", mouse_); register_input("/remote/keyboard", keyboard_); - register_input("/gimbal/pitch/angle", gimbal_pitch_angle_, false); - register_input("/referee/game/stage", game_stage_); + crosshair_base_x_ = crosshair_circle_.x(); + crosshair_base_y_ = crosshair_circle_.y(); ctrl_transition_.reset(false); - pitch_hud_.set_visible(false); } void update() override { @@ -122,12 +109,8 @@ class DeformableInfantry crosshair_circle_.set_x(static_cast(std::lround( static_cast(crosshair_base_x_) + 20.0 * reveal))); - - // const double display_pitch = - // (chassis_pitch_.ready() && std::isfinite(*chassis_pitch_)) - // ? -*chassis_pitch_ - // : std::numeric_limits::quiet_NaN(); - // pitch_hud_.update(display_pitch, reveal); + crosshair_circle_.set_y(static_cast(std::lround( + static_cast(crosshair_base_y_) - 20.0 * reveal))); } void update_time_reminder() { @@ -174,7 +157,6 @@ class DeformableInfantry static constexpr uint16_t screen_width = 1920, screen_height = 1080; static constexpr uint16_t x_center = screen_width / 2, y_center = screen_height / 2; - static constexpr uint16_t crosshair_base_x_ = x_center - 2; InputInterface timestamp_; InputInterface chassis_mode_; @@ -199,9 +181,6 @@ class DeformableInfantry InputInterface mouse_; InputInterface keyboard_; - InputInterface gimbal_pitch_angle_; - InputInterface chassis_pitch_; - InputInterface game_stage_; CrossHairCircle crosshair_circle_; @@ -213,8 +192,9 @@ class DeformableInfantry Arc chassis_direction_indicator_; DeformableChassisLegArcs deformable_chassis_leg_arcs_; - PitchHud pitch_hud_; AnimatedToggle ctrl_transition_{}; + uint16_t crosshair_base_x_ = 0; + uint16_t crosshair_base_y_ = 0; Integer time_reminder_; }; diff --git a/rmcs_ws/src/rmcs_core/src/referee/app/ui/widget/crosshair_circle.hpp b/rmcs_ws/src/rmcs_core/src/referee/app/ui/widget/crosshair_circle.hpp index b96b7cb4..15823c6f 100644 --- a/rmcs_ws/src/rmcs_core/src/referee/app/ui/widget/crosshair_circle.hpp +++ b/rmcs_ws/src/rmcs_core/src/referee/app/ui/widget/crosshair_circle.hpp @@ -20,6 +20,8 @@ class CrossHairCircle { void set_width(uint16_t width) { circle_.set_width(width); } void set_x(uint16_t x) { circle_.set_x(x); } void set_y(uint16_t y) { circle_.set_y(y); } + uint16_t x() const { return circle_.x(); } + uint16_t y() const { return circle_.y(); } private: Circle circle_; diff --git a/rmcs_ws/src/rmcs_core/src/referee/app/ui/widget/pitch_hud.hpp b/rmcs_ws/src/rmcs_core/src/referee/app/ui/widget/pitch_hud.hpp deleted file mode 100644 index 37286116..00000000 --- a/rmcs_ws/src/rmcs_core/src/referee/app/ui/widget/pitch_hud.hpp +++ /dev/null @@ -1,132 +0,0 @@ -#pragma once - -#include -#include -#include - -#include "referee/app/ui/shape/shape.hpp" - -namespace rmcs_core::referee::app::ui { - -class PitchHud { -public: - struct Config { - uint16_t center_x = x_center; - uint16_t center_y = y_center; - uint16_t radius_px = 150; - uint16_t width_px = 12; - double half_span_deg = 22.0; - double warning_pitch_deg = 5.0; - }; - - PitchHud() { set_config(Config{}); } - - explicit PitchHud(Config config) { set_config(config); } - - void set_config(Config config) { - config.radius_px = std::max(config.radius_px, 1); - config.width_px = std::max(config.width_px, 1); - config.half_span_deg = std::max(config.half_span_deg, 1.0); - config.warning_pitch_deg = std::max(config.warning_pitch_deg, 0.0); - config_ = config; - initialize_(); - } - - void set_visible(bool visible) { - background_arc_.set_visible(visible); - positive_fill_arc_.set_visible(visible && positive_fill_visible_); - negative_fill_arc_.set_visible(visible && negative_fill_visible_); - } - - void update(double pitch_rad, double reveal = 1.0) { - reveal = std::clamp(reveal, 0.0, 1.0); - if (!std::isfinite(pitch_rad) || reveal <= 1.0e-4) { - positive_fill_visible_ = false; - negative_fill_visible_ = false; - set_visible(false); - return; - } - - apply_geometry_(reveal); - - const double visible_half_span_deg = config_.half_span_deg * reveal; - const double pitch_deg = - std::clamp(rad_to_deg_(pitch_rad), -visible_half_span_deg, visible_half_span_deg); - const double fill_deg = std::abs(pitch_deg); - - const bool warning = pitch_deg > config_.warning_pitch_deg; - const auto fill_color = warning ? Shape::Color::PINK : Shape::Color::YELLOW; - positive_fill_arc_.set_color(fill_color); - negative_fill_arc_.set_color(fill_color); - - positive_fill_visible_ = pitch_deg >= 0.0 && fill_deg > 1.0e-4; - negative_fill_visible_ = pitch_deg < 0.0 && fill_deg > 1.0e-4; - - positive_fill_arc_.set_angle_start(to_referee_angle_(pitch_deg)); - positive_fill_arc_.set_angle_end(mid_angle_()); - - negative_fill_arc_.set_angle_start(mid_angle_()); - negative_fill_arc_.set_angle_end(to_referee_angle_(pitch_deg)); - - set_visible(true); - } - -private: - void initialize_() { - background_arc_.set_color(Shape::Color::ORANGE); - positive_fill_arc_.set_color(Shape::Color::YELLOW); - negative_fill_arc_.set_color(Shape::Color::YELLOW); - positive_fill_visible_ = false; - negative_fill_visible_ = false; - set_visible(false); - } - - void apply_geometry_(double reveal) { - const uint16_t width = std::max(1, static_cast(std::lround( - static_cast(config_.width_px) * reveal))); - const double half_span_deg = config_.half_span_deg * reveal; - - background_arc_.set_x(config_.center_x); - background_arc_.set_y(config_.center_y); - background_arc_.set_r(config_.radius_px); - background_arc_.set_width(width); - background_arc_.set_angle_start(to_referee_angle_(-half_span_deg)); - background_arc_.set_angle_end(to_referee_angle_(half_span_deg)); - - positive_fill_arc_.set_x(config_.center_x); - positive_fill_arc_.set_y(config_.center_y); - positive_fill_arc_.set_r(config_.radius_px); - positive_fill_arc_.set_width(width); - - negative_fill_arc_.set_x(config_.center_x); - negative_fill_arc_.set_y(config_.center_y); - negative_fill_arc_.set_r(config_.radius_px); - negative_fill_arc_.set_width(width); - } - - static constexpr double rad_to_deg_(double radians) { - return radians * 180.0 / 3.14159265358979323846; - } - - static uint16_t wrap_angle_(long angle) { - angle %= 360; - if (angle < 0) - angle += 360; - return static_cast(angle); - } - - static constexpr uint16_t mid_angle_() { return 180; } - - static uint16_t to_referee_angle_(double display_pitch_deg) { - return wrap_angle_(std::lround(static_cast(mid_angle_()) + display_pitch_deg)); - } - - Config config_{}; - Arc background_arc_{Shape::Color::WHITE, 1, 0, 0, 0, 0, 1, 1, false}; - Arc positive_fill_arc_{Shape::Color::YELLOW, 1, 0, 0, 0, 0, 1, 1, false}; - Arc negative_fill_arc_{Shape::Color::YELLOW, 1, 0, 0, 0, 0, 1, 1, false}; - bool positive_fill_visible_ = false; - bool negative_fill_visible_ = false; -}; - -} // namespace rmcs_core::referee::app::ui diff --git a/rmcs_ws/src/rmcs_core/third_party_overrides/librmcs/host/include/librmcs/protocol/handler.hpp b/rmcs_ws/src/rmcs_core/third_party_overrides/librmcs/host/include/librmcs/protocol/handler.hpp new file mode 100644 index 00000000..90c0a5f1 --- /dev/null +++ b/rmcs_ws/src/rmcs_core/third_party_overrides/librmcs/host/include/librmcs/protocol/handler.hpp @@ -0,0 +1,61 @@ +#pragma once + +#include +#include + +#include +#include +#include + +namespace librmcs::host::protocol { + +class LIBRMCS_API Handler { +public: + class LIBRMCS_API PacketBuilder { + public: + PacketBuilder(const PacketBuilder&) = delete; + PacketBuilder& operator=(const PacketBuilder&) = delete; + PacketBuilder(PacketBuilder&&) = delete; + PacketBuilder& operator=(PacketBuilder&&) = delete; + + ~PacketBuilder() noexcept; + + bool write_can(data::DataId field_id, const data::CanDataView& view) noexcept; + + bool write_uart(data::DataId field_id, const data::UartDataView& view) noexcept; + + bool write_gpio_digital_data( + uint8_t channel_index, const data::GpioDigitalDataView& view) noexcept; + + bool write_gpio_digital_read_config( + uint8_t channel_index, const data::GpioReadConfigView& view) noexcept; + + bool write_gpio_analog_data( + uint8_t channel_index, const data::GpioAnalogDataView& view) noexcept; + + private: + friend class Handler; + + explicit PacketBuilder(void* shared_state) noexcept; + + alignas(std::uintptr_t) std::uint8_t storage_[16 * sizeof(std::uintptr_t)]; + }; + + Handler( + uint16_t usb_vid, int32_t usb_pid, std::string_view serial_filter, + const agent::AdvancedOptions& options, data::DataCallback& callback); + + Handler(const Handler&) = delete; + Handler& operator=(const Handler&) = delete; + Handler(Handler&& other) noexcept; + Handler& operator=(Handler&& other) noexcept; + + ~Handler() noexcept; + + PacketBuilder start_transmit() noexcept; + +private: + void* shared_state_ = nullptr; +}; + +} // namespace librmcs::host::protocol diff --git a/rmcs_ws/src/rmcs_core/third_party_overrides/librmcs/host/src/protocol/handler.cpp b/rmcs_ws/src/rmcs_core/third_party_overrides/librmcs/host/src/protocol/handler.cpp new file mode 100644 index 00000000..a52bdde7 --- /dev/null +++ b/rmcs_ws/src/rmcs_core/third_party_overrides/librmcs/host/src/protocol/handler.cpp @@ -0,0 +1,390 @@ +#include "librmcs/protocol/handler.hpp" + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "core/src/protocol/deserializer.hpp" +#include "core/src/protocol/protocol.hpp" +#include "core/src/protocol/serializer.hpp" +#include "core/src/utility/assert.hpp" +#include "host/src/logging/logging.hpp" +#include "host/src/protocol/stream_buffer.hpp" +#include "host/src/transport/transport.hpp" +#include "librmcs/agent/common.hpp" +#include "librmcs/data/datas.hpp" + +namespace librmcs::host::protocol { + +namespace { + +class NullTransport final : public transport::Transport { +public: + std::unique_ptr acquire_transmit_buffer() noexcept override { + return nullptr; + } + + void transmit(std::unique_ptr buffer, size_t) override { + (void)buffer; + } + + void release_transmit_buffer(std::unique_ptr buffer) override { + (void)buffer; + } + + void receive(std::function)> callback) override { + receive_callback_ = std::move(callback); + } + + bool healthy() const noexcept override { return false; } + +private: + std::function)> receive_callback_; +}; + +} // namespace + +class HandlerImpl : public core::protocol::DeserializeCallback { +public: + explicit HandlerImpl(std::unique_ptr transport, data::DataCallback& callback) + : transport_(std::move(transport)) + , callback_(callback) + , deserializer_(*this) { + transport_->receive([this](std::span buffer) { + // Operating system automatically assembles the packet + deserializer_.feed(buffer); + deserializer_.finish_transfer(); + }); + } + + transport::Transport& transport() noexcept { return *transport_; } + + bool healthy() const noexcept { return transport_->healthy(); } + + bool is_null_transport() const noexcept { + return dynamic_cast(transport_.get()) != nullptr; + } + + void can_deserialized_callback( + core::protocol::FieldId id, const data::CanDataView& data) override { + if (!callback_.can_receive_callback(id, data)) + logging::get_logger().error("Unexpected can field id: ", static_cast(id)); + } + + void uart_deserialized_callback( + core::protocol::FieldId id, const data::UartDataView& data) override { + if (!callback_.uart_receive_callback(id, data)) + logging::get_logger().error("Unexpected uart field id: ", static_cast(id)); + } + + void gpio_digital_data_deserialized_callback( + uint8_t channel_index, const data::GpioDigitalDataView& data) override { + callback_.gpio_digital_read_result_callback(channel_index, data); + } + + void gpio_analog_data_deserialized_callback( + uint8_t channel_index, const data::GpioAnalogDataView& data) override { + callback_.gpio_analog_read_result_callback(channel_index, data); + } + + void gpio_digital_read_config_deserialized_callback( + uint8_t channel_index, const data::GpioReadConfigView& data) override { + (void)channel_index; + (void)data; + logging::get_logger().error("Unexpected gpio digital read config field in uplink"); + } + + void gpio_analog_read_config_deserialized_callback( + uint8_t channel_index, const data::GpioReadConfigView& data) override { + (void)channel_index; + (void)data; + logging::get_logger().error("Unexpected gpio analog read config field in uplink"); + } + + void accelerometer_deserialized_callback(const data::AccelerometerDataView& data) override { + callback_.accelerometer_receive_callback(data); + } + + void gyroscope_deserialized_callback(const data::GyroscopeDataView& data) override { + callback_.gyroscope_receive_callback(data); + } + + void temperature_deserialized_callback(const data::TemperatureDataView& data) override { + callback_.temperature_receive_callback(data); + } + + void error_callback() override { + logging::get_logger().error("Deserializer encountered an error while parsing input"); + } + +private: + std::unique_ptr transport_; + data::DataCallback& callback_; + core::protocol::Deserializer deserializer_; +}; + +class HandlerSharedState { +public: + HandlerSharedState( + uint16_t usb_vid, int32_t usb_pid, std::string_view serial_filter, + const agent::AdvancedOptions& options, data::DataCallback& callback) + : usb_vid_(usb_vid) + , usb_pid_(usb_pid) + , serial_filter_(serial_filter) + , options_(options) + , callback_(callback) + , impl_(std::make_shared(create_transport_(), callback_)) + , reconnect_thread_([this]() { reconnect_loop_(); }) {} + + ~HandlerSharedState() { + stop_reconnect_thread_.store(true, std::memory_order::relaxed); + if (reconnect_thread_.joinable()) + reconnect_thread_.join(); + } + + std::shared_ptr acquire_impl_for_transmit() noexcept { + maybe_restart_transport_(); + + const std::scoped_lock guard{mutex_}; + core::utility::assert_debug(static_cast(impl_)); + return impl_; + } + + void note_transmit_result(core::protocol::Serializer::SerializeResult result) noexcept { + using SerializeResult = core::protocol::Serializer::SerializeResult; + + if (result == SerializeResult::kSuccess) [[likely]] { + consecutive_acquire_failures_.store(0, std::memory_order::relaxed); + return; + } + + if (result == SerializeResult::kBadAlloc) { + logging::get_logger().error("Transmit buffer unavailable (acquire failed)"); + + const uint32_t failures = + consecutive_acquire_failures_.fetch_add(1, std::memory_order::relaxed) + 1; + if (failures >= kRestartFailureThreshold_ + && !transport_restart_requested_.exchange(true, std::memory_order::relaxed)) { + logging::get_logger().warn( + "USB transport restart scheduled after {} consecutive transmit buffer " + "acquire failures", + failures); + } + return; + } + + core::utility::assert_failed_debug(); + } + +private: + std::unique_ptr create_transport_() const { + return transport::usb::create_transport( + usb_vid_, usb_pid_, serial_filter_, transport::usb::ConnectionOptions{ + .dangerously_skip_version_checks = + options_.dangerously_skip_version_checks, + }); + } + + void maybe_restart_transport_() noexcept { + auto now = std::chrono::steady_clock::now(); + + const std::scoped_lock guard{mutex_}; + const bool reconnect_due_to_failure = + transport_restart_requested_.load(std::memory_order::relaxed); + const bool reconnect_due_to_unhealthy_transport = impl_ && !impl_->healthy(); + const bool reconnect_due_to_null_transport = impl_ && impl_->is_null_transport(); + if (!reconnect_due_to_failure && !reconnect_due_to_unhealthy_transport + && !reconnect_due_to_null_transport) + return; + if (now < next_restart_time_) + return; + + const uint32_t failures = + consecutive_acquire_failures_.load(std::memory_order::relaxed); + + std::shared_ptr old_impl; + old_impl.swap(impl_); + old_impl.reset(); + + try { + impl_ = std::make_shared(create_transport_(), callback_); + consecutive_acquire_failures_.store(0, std::memory_order::relaxed); + transport_restart_requested_.store(false, std::memory_order::relaxed); + next_restart_time_ = std::chrono::steady_clock::time_point::min(); + logging::get_logger().warn( + "USB transport restarted after {} consecutive transmit buffer acquire failures", + failures); + } catch (const std::exception& ex) { + impl_ = std::make_shared(std::make_unique(), callback_); + transport_restart_requested_.store(true, std::memory_order::relaxed); + next_restart_time_ = std::chrono::steady_clock::now() + kRestartRetryInterval_; + logging::get_logger().error("USB transport restart failed: {}", ex.what()); + } catch (...) { + impl_ = std::make_shared(std::make_unique(), callback_); + transport_restart_requested_.store(true, std::memory_order::relaxed); + next_restart_time_ = std::chrono::steady_clock::now() + kRestartRetryInterval_; + logging::get_logger().error("USB transport restart failed: unknown error"); + } + } + + void reconnect_loop_() { + while (!stop_reconnect_thread_.load(std::memory_order::relaxed)) { + maybe_restart_transport_(); + std::this_thread::sleep_for(kRestartRetryInterval_); + } + } + + static constexpr uint32_t kRestartFailureThreshold_ = 8; + static constexpr auto kRestartRetryInterval_ = std::chrono::milliseconds{500}; + + std::mutex mutex_; + uint16_t usb_vid_; + int32_t usb_pid_; + std::string serial_filter_; + agent::AdvancedOptions options_; + data::DataCallback& callback_; + std::shared_ptr impl_; + std::atomic consecutive_acquire_failures_{0}; + std::atomic transport_restart_requested_{false}; + std::atomic stop_reconnect_thread_{false}; + std::chrono::steady_clock::time_point next_restart_time_ = + std::chrono::steady_clock::time_point::min(); + std::thread reconnect_thread_; +}; + +namespace { + +struct PacketBuilderImpl { + explicit PacketBuilderImpl(HandlerSharedState& shared_state) noexcept + : impl_(shared_state.acquire_impl_for_transmit()) + , shared_state_(shared_state) + , buffer_(impl_->transport()) + , serializer_(buffer_) {} + + PacketBuilderImpl(PacketBuilderImpl&& other) noexcept + : impl_(std::move(other.impl_)) + , shared_state_(other.shared_state_) + , buffer_(std::move(other.buffer_)) + , serializer_(buffer_) {} + + PacketBuilderImpl& operator=(PacketBuilderImpl&&) = delete; + PacketBuilderImpl(const PacketBuilderImpl&) = delete; + PacketBuilderImpl& operator=(const PacketBuilderImpl&) = delete; + ~PacketBuilderImpl() = default; + + [[nodiscard]] bool write_can(data::DataId field_id, const data::CanDataView& view) noexcept { + return process_result(serializer_.write_can(field_id, view)); + } + + [[nodiscard]] bool write_uart(data::DataId field_id, const data::UartDataView& view) noexcept { + return process_result(serializer_.write_uart(field_id, view)); + } + + [[nodiscard]] bool write_gpio_digital_data( + uint8_t channel_index, const data::GpioDigitalDataView& view) noexcept { + return process_result(serializer_.write_gpio_digital_data(channel_index, view)); + } + + [[nodiscard]] bool write_gpio_digital_read_config( + uint8_t channel_index, const data::GpioReadConfigView& view) noexcept { + return process_result(serializer_.write_gpio_digital_read_config(channel_index, view)); + } + + [[nodiscard]] bool write_gpio_analog_data( + uint8_t channel_index, const data::GpioAnalogDataView& view) noexcept { + return process_result(serializer_.write_gpio_analog_data(channel_index, view)); + } + +private: + bool process_result(core::protocol::Serializer::SerializeResult result) noexcept { + using SerializeResult = core::protocol::Serializer::SerializeResult; + if (result == SerializeResult::kInvalidArgument) + return false; + + shared_state_.note_transmit_result(result); + return true; + } + + std::shared_ptr impl_; + HandlerSharedState& shared_state_; + StreamBuffer buffer_; + core::protocol::Serializer serializer_; +}; + +} // namespace + +Handler::PacketBuilder::PacketBuilder(void* shared_state_ptr) noexcept { + static_assert(sizeof(PacketBuilderImpl) <= sizeof(storage_)); + static_assert(alignof(PacketBuilderImpl) <= alignof(std::uintptr_t)); + + auto& shared_state = *static_cast(shared_state_ptr); + std::construct_at(reinterpret_cast(storage_), shared_state); +} + +Handler::PacketBuilder::~PacketBuilder() noexcept { + std::destroy_at(std::launder(reinterpret_cast(storage_))); +} + +bool Handler::PacketBuilder::write_can( + data::DataId field_id, const data::CanDataView& view) noexcept { + return std::launder(reinterpret_cast(storage_))->write_can(field_id, view); +} + +bool Handler::PacketBuilder::write_uart( + data::DataId field_id, const data::UartDataView& view) noexcept { + return std::launder(reinterpret_cast(storage_)) + ->write_uart(field_id, view); +} + +bool Handler::PacketBuilder::write_gpio_digital_data( + uint8_t channel_index, const data::GpioDigitalDataView& view) noexcept { + return std::launder(reinterpret_cast(storage_)) + ->write_gpio_digital_data(channel_index, view); +} + +bool Handler::PacketBuilder::write_gpio_digital_read_config( + uint8_t channel_index, const data::GpioReadConfigView& view) noexcept { + return std::launder(reinterpret_cast(storage_)) + ->write_gpio_digital_read_config(channel_index, view); +} + +bool Handler::PacketBuilder::write_gpio_analog_data( + uint8_t channel_index, const data::GpioAnalogDataView& view) noexcept { + return std::launder(reinterpret_cast(storage_)) + ->write_gpio_analog_data(channel_index, view); +} + +Handler::Handler( + uint16_t usb_vid, int32_t usb_pid, std::string_view serial_filter, + const agent::AdvancedOptions& options, data::DataCallback& callback) + : shared_state_(new HandlerSharedState(usb_vid, usb_pid, serial_filter, options, callback)) {} + +Handler::Handler(Handler&& other) noexcept + : shared_state_(std::exchange(other.shared_state_, nullptr)) {} + +Handler& Handler::operator=(Handler&& other) noexcept { + if (this == &other) + return *this; + delete static_cast(shared_state_); + shared_state_ = std::exchange(other.shared_state_, nullptr); + return *this; +} + +Handler::~Handler() noexcept { delete static_cast(shared_state_); } + +Handler::PacketBuilder Handler::start_transmit() noexcept { + core::utility::assert_debug(shared_state_); + return PacketBuilder{shared_state_}; +} + +} // namespace librmcs::host::protocol diff --git a/rmcs_ws/src/rmcs_core/third_party_overrides/librmcs/host/src/transport/transport.hpp b/rmcs_ws/src/rmcs_core/third_party_overrides/librmcs/host/src/transport/transport.hpp new file mode 100644 index 00000000..c298680e --- /dev/null +++ b/rmcs_ws/src/rmcs_core/third_party_overrides/librmcs/host/src/transport/transport.hpp @@ -0,0 +1,56 @@ +#pragma once + +#include +#include +#include +#include +#include +#include + +#include "core/src/protocol/constant.hpp" + +namespace librmcs::host::transport { + +class TransportBuffer { +public: + using BufferSpanType = std::span; + + TransportBuffer() = default; + TransportBuffer(const TransportBuffer&) = delete; + TransportBuffer& operator=(const TransportBuffer&) = delete; + TransportBuffer(TransportBuffer&&) = delete; + TransportBuffer& operator=(TransportBuffer&&) = delete; + virtual ~TransportBuffer() noexcept = default; + + virtual BufferSpanType data() const noexcept = 0; +}; + +class Transport { +public: + Transport() = default; + virtual ~Transport() noexcept = default; + Transport(const Transport&) = delete; + Transport& operator=(const Transport&) = delete; + Transport(Transport&&) = delete; + Transport& operator=(Transport&&) = delete; + + virtual std::unique_ptr acquire_transmit_buffer() noexcept = 0; + virtual void transmit(std::unique_ptr buffer, size_t payload_size) = 0; + virtual void release_transmit_buffer(std::unique_ptr buffer) = 0; + virtual void receive(std::function)> callback) = 0; + virtual bool healthy() const noexcept { return true; } +}; + +namespace usb { + +struct ConnectionOptions { + bool dangerously_skip_version_checks = false; +}; + +std::unique_ptr create_transport( + uint16_t usb_vid, int32_t usb_pid, std::string_view serial_filter, + const ConnectionOptions& options); + +} // namespace usb + +} // namespace librmcs::host::transport diff --git a/rmcs_ws/src/rmcs_core/third_party_overrides/librmcs/host/src/transport/usb/usb.cpp b/rmcs_ws/src/rmcs_core/third_party_overrides/librmcs/host/src/transport/usb/usb.cpp new file mode 100644 index 00000000..738ae70e --- /dev/null +++ b/rmcs_ws/src/rmcs_core/third_party_overrides/librmcs/host/src/transport/usb/usb.cpp @@ -0,0 +1,398 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +#include "core/src/protocol/constant.hpp" +#include "core/src/utility/assert.hpp" +#include "host/src/logging/logging.hpp" +#include "host/src/transport/transport.hpp" +#include "host/src/transport/usb/device_scanner.hpp" +#include "host/src/transport/usb/helper.hpp" +#include "host/src/utility/final_action.hpp" +#include "host/src/utility/ring_buffer.hpp" + +namespace librmcs::host::transport::usb { + +class Usb : public Transport { +public: + explicit Usb( + uint16_t usb_vid, int32_t usb_pid, std::string_view serial_filter, + const ConnectionOptions& options) + : logger_(logging::get_logger()) + , free_transmit_transfers_(kTransmitTransferCount) { + + usb_init(usb_vid, usb_pid, serial_filter, options); + utility::FinalAction rollback_on_failure{[this]() noexcept { + destroy_free_transmit_transfers(); + libusb_release_interface(libusb_device_handle_, kTargetInterface); + libusb_close(libusb_device_handle_); + libusb_exit(libusb_context_); + }}; + + init_transmit_transfers(); + event_thread_ = std::thread{[this]() { handle_events(); }}; + + rollback_on_failure.disable(); + } + + Usb(const Usb&) = delete; + Usb& operator=(const Usb&) = delete; + Usb(Usb&&) = delete; + Usb& operator=(Usb&&) = delete; + + ~Usb() override { + { + const std::scoped_lock guard{transmit_transfer_push_mutex_}; + stop_handling_events_.store(true, std::memory_order::relaxed); + } + destroy_free_transmit_transfers(); + + libusb_release_interface(libusb_device_handle_, kTargetInterface); + + // libusb_close() reliably cancels all pending transfers and invokes their callbacks, + // avoiding race conditions present in other cancellation methods + libusb_close(libusb_device_handle_); + + if (event_thread_.joinable()) + event_thread_.join(); + + libusb_exit(libusb_context_); + } + + std::unique_ptr acquire_transmit_buffer() noexcept override { + if (!connection_healthy_.load(std::memory_order::relaxed)) + return nullptr; + + TransferWrapper* transfer = nullptr; + { + const std::scoped_lock guard{transmit_transfer_pop_mutex_}; + free_transmit_transfers_.pop_front( + [&transfer](TransferWrapper* value) noexcept { transfer = value; }); + } + if (!transfer) + return nullptr; + + return std::unique_ptr{transfer}; + } + + void transmit(std::unique_ptr buffer, size_t size) override { + core::utility::assert_debug(static_cast(buffer)); + + if (size > core::protocol::kProtocolBufferSize) + throw std::invalid_argument("Transmit size exceeds maximum transfer length"); + + if (!connection_healthy_.load(std::memory_order::relaxed)) { + release_transmit_buffer(std::move(buffer)); + return; + } + + // NOLINTNEXTLINE(cppcoreguidelines-pro-type-static-cast-downcast) + auto& transfer = static_cast(buffer.get())->transfer_; + transfer->length = static_cast(size); + + int ret = libusb_submit_transfer(transfer); + if (ret != 0) [[unlikely]] { + mark_connection_lost_( + std::format( + "Failed to submit transmit transfer: {} ({})", ret, + helper::libusb_errname(ret))); + release_transmit_buffer(std::move(buffer)); + return; + } + + // If success: Ownership is transferred to libusb + std::ignore = buffer.release(); + } + + void release_transmit_buffer(std::unique_ptr buffer) override { + core::utility::assert_debug(static_cast(buffer)); + + const std::scoped_lock guard{transmit_transfer_push_mutex_}; + + // NOLINTNEXTLINE(cppcoreguidelines-pro-type-static-cast-downcast) + auto* wrapper = static_cast(buffer.release()); + free_transmit_transfers_.emplace_back(wrapper); + } + + void receive(std::function)> callback) override { + if (!callback) + throw std::invalid_argument{"Callback function cannot be null"}; + if (receive_callback_) + throw std::logic_error{"Receive function can only be called once"}; + + receive_callback_ = std::move(callback); + init_receive_transfers(); + } + + bool healthy() const noexcept override { + return connection_healthy_.load(std::memory_order::relaxed); + } + +private: + class TransferWrapper : public TransportBuffer { + friend class Usb; + + public: + explicit TransferWrapper(Usb& self) + : self_(self) + , transfer_(self_.create_libusb_transfer()) {} + + TransferWrapper(const TransferWrapper&) = delete; + TransferWrapper& operator=(const TransferWrapper&) = delete; + TransferWrapper(TransferWrapper&&) = delete; + TransferWrapper& operator=(TransferWrapper&&) = delete; + + ~TransferWrapper() override { + if (transfer_) { + logging::get_logger().error( + "USB TransferBuffer {} was destroyed externally - this is undefined behavior. " + "Buffers must be returned via transmit() or " + "release_transmit_buffer(). ", + static_cast(this)); + destroy(); + } + } + + BufferSpanType data() const noexcept override { + return BufferSpanType{ + reinterpret_cast(transfer_->buffer), + core::protocol::kProtocolBufferSize}; + } + + void destroy() noexcept { + self_.destroy_libusb_transfer(transfer_); + transfer_ = nullptr; + } + + private: + Usb& self_; + + libusb_transfer* transfer_; + }; + + void usb_init( + uint16_t vendor_id, int32_t product_id, std::string_view serial_filter, + const ConnectionOptions& options) { + if (const int ret = libusb_init(&libusb_context_); ret != 0) [[unlikely]] { + throw std::runtime_error( + std::format( + "Failed to initialize libusb: {} ({})", ret, helper::libusb_errname(ret))); + } + utility::FinalAction exit_libusb{[this]() noexcept { libusb_exit(libusb_context_); }}; + + libusb_device_handle_ = DeviceScanner::select_device( + libusb_context_, vendor_id, product_id, serial_filter, options); + utility::FinalAction close_device_handle{ + [this]() noexcept { libusb_close(libusb_device_handle_); }}; + + if (const int ret = libusb_claim_interface(libusb_device_handle_, kTargetInterface); + ret != 0) [[unlikely]] { + throw std::runtime_error( + std::format( + "Failed to claim interface {}: {} ({})", kTargetInterface, ret, + helper::libusb_errname(ret))); + } + + close_device_handle.disable(); + exit_libusb.disable(); + } + + void init_transmit_transfers() { + TransferWrapper* transmit_transfers[kTransmitTransferCount] = {}; + try { + for (auto& wrapper : transmit_transfers) { + wrapper = new TransferWrapper{*this}; + auto* transfer = wrapper->transfer_; + + libusb_fill_bulk_transfer( + transfer, libusb_device_handle_, kOutEndpoint, + new unsigned char[core::protocol::kProtocolBufferSize], 0, + [](libusb_transfer* transfer) { + auto* wrapper = static_cast(transfer->user_data); + wrapper->self_.usb_transmit_complete_callback(wrapper); + }, + wrapper, 0); + transfer->flags = libusb_transfer_flags::LIBUSB_TRANSFER_FREE_BUFFER; + } + } catch (...) { + for (auto& wrapper : transmit_transfers) { + if (wrapper) { + wrapper->destroy(); + delete wrapper; + } + } + throw; + } + + auto* iter = transmit_transfers; + free_transmit_transfers_.push_back_n( + [&iter]() noexcept { return *iter++; }, kTransmitTransferCount); + } + + void handle_events() { + while (active_transfers_.load(std::memory_order::relaxed)) { + libusb_handle_events(libusb_context_); + } + } + + void init_receive_transfers() { + for (size_t i = 0; i < kReceiveTransferCount; i++) { + auto* transfer = create_libusb_transfer(); + + libusb_fill_bulk_transfer( + transfer, libusb_device_handle_, kInEndpoint, + new unsigned char[core::protocol::kProtocolBufferSize], + static_cast(core::protocol::kProtocolBufferSize), + [](libusb_transfer* transfer) { + static_cast(transfer->user_data)->usb_receive_complete_callback(transfer); + }, + this, 0); + transfer->flags = libusb_transfer_flags::LIBUSB_TRANSFER_FREE_BUFFER; + + int ret = libusb_submit_transfer(transfer); + if (ret != 0) [[unlikely]] { + destroy_libusb_transfer(transfer); + throw std::runtime_error( + std::format( + "Failed to submit receive transfer: {} ({})", ret, + helper::libusb_errname(ret))); + } + } + } + + void usb_transmit_complete_callback(TransferWrapper* wrapper) { + const std::scoped_lock guard{transmit_transfer_push_mutex_}; + + if (stop_handling_events_.load(std::memory_order::relaxed)) [[unlikely]] { + wrapper->destroy(); + delete wrapper; + return; + } + + if (wrapper->transfer_->status != LIBUSB_TRANSFER_COMPLETED) [[unlikely]] { + mark_connection_lost_( + std::format( + "Transmit transfer failed with status {}", + static_cast(wrapper->transfer_->status))); + wrapper->destroy(); + delete wrapper; + return; + } + + free_transmit_transfers_.emplace_back(wrapper); + } + + void usb_receive_complete_callback(libusb_transfer* transfer) { + if (stop_handling_events_.load(std::memory_order::relaxed)) [[unlikely]] { + destroy_libusb_transfer(transfer); + return; + } + + if (transfer->status != LIBUSB_TRANSFER_COMPLETED) [[unlikely]] { + mark_connection_lost_( + std::format( + "Receive transfer failed with status {}", + static_cast(transfer->status))); + destroy_libusb_transfer(transfer); + return; + } + + const auto now = std::chrono::steady_clock::now(); + const bool should_drop = now > last_rx_callback_timepoint_ + std::chrono::seconds{1}; + last_rx_callback_timepoint_ = now; + + if (!should_drop && transfer->actual_length > 0) { + const auto* first = reinterpret_cast(transfer->buffer); + const auto size = static_cast(transfer->actual_length); + receive_callback_({first, size}); + } + + int ret = libusb_submit_transfer(transfer); + if (ret != 0) [[unlikely]] { + mark_connection_lost_( + std::format( + "Failed to re-submit receive transfer: {} ({})", ret, + helper::libusb_errname(ret))); + destroy_libusb_transfer(transfer); + } + } + + void mark_connection_lost_(const std::string& reason) noexcept { + bool expected = true; + if (!connection_healthy_.compare_exchange_strong( + expected, false, std::memory_order::relaxed, std::memory_order::relaxed)) { + return; + } + + logger_.error("USB transport marked unhealthy: {}", reason); + } + + void destroy_free_transmit_transfers() noexcept { + free_transmit_transfers_.pop_front_n([](TransferWrapper* wrapper) noexcept { + wrapper->destroy(); + delete wrapper; + }); + } + + libusb_transfer* create_libusb_transfer() { + auto* transfer = libusb_alloc_transfer(0); + if (!transfer) + throw std::bad_alloc{}; + active_transfers_.fetch_add(1, std::memory_order::relaxed); + return transfer; + } + + void destroy_libusb_transfer(libusb_transfer* transfer) noexcept { + libusb_free_transfer(transfer); + active_transfers_.fetch_sub(1, std::memory_order::relaxed); + } + + static constexpr int kTargetInterface = 0x00; + + static constexpr unsigned char kOutEndpoint = 0x01; + static constexpr unsigned char kInEndpoint = 0x81; + + static constexpr size_t kTransmitTransferCount = 64; + static constexpr size_t kReceiveTransferCount = 4; + + logging::Logger& logger_; + + libusb_context* libusb_context_ = nullptr; + libusb_device_handle* libusb_device_handle_ = nullptr; + + std::thread event_thread_; + + std::atomic active_transfers_ = 0; + std::atomic stop_handling_events_ = false; + std::atomic connection_healthy_ = true; + + utility::RingBuffer free_transmit_transfers_; + std::mutex transmit_transfer_pop_mutex_, transmit_transfer_push_mutex_; + + std::function)> receive_callback_; + std::chrono::steady_clock::time_point last_rx_callback_timepoint_ = + std::chrono::steady_clock::time_point::min(); +}; + +std::unique_ptr create_transport( + uint16_t usb_vid, int32_t usb_pid, std::string_view serial_filter, + const ConnectionOptions& options) { + return std::make_unique(usb_vid, usb_pid, serial_filter, options); +} + +} // namespace librmcs::host::transport::usb From b654d65cbb0de29c12defc6f48df5c81e772f614 Mon Sep 17 00:00:00 2001 From: ZGZ713912 Date: Sat, 16 May 2026 22:17:23 +0800 Subject: [PATCH 16/52] feat: update CAN transmission logic in deformable infantry hardware Co-authored-by: Copilot --- .../src/hardware/deformable-infantry-omni-b.cpp | 2 +- .../src/hardware/deformable-infantry-omni.cpp | 11 ----------- 2 files changed, 1 insertion(+), 12 deletions(-) diff --git a/rmcs_ws/src/rmcs_core/src/hardware/deformable-infantry-omni-b.cpp b/rmcs_ws/src/rmcs_core/src/hardware/deformable-infantry-omni-b.cpp index b2d55992..9a7c29a3 100644 --- a/rmcs_ws/src/rmcs_core/src/hardware/deformable-infantry-omni-b.cpp +++ b/rmcs_ws/src/rmcs_core/src/hardware/deformable-infantry-omni-b.cpp @@ -283,7 +283,7 @@ class DeformableInfantryOmniB device::CanPacket8{ chassis_wheel_motors_[1].generate_command(), device::CanPacket8::PaddingQuarter{}, - supercap_.generate_command(), + device::CanPacket8::PaddingQuarter{}, device::CanPacket8::PaddingQuarter{}, } .as_bytes(), diff --git a/rmcs_ws/src/rmcs_core/src/hardware/deformable-infantry-omni.cpp b/rmcs_ws/src/rmcs_core/src/hardware/deformable-infantry-omni.cpp index 5dfbcc6a..561c22c0 100644 --- a/rmcs_ws/src/rmcs_core/src/hardware/deformable-infantry-omni.cpp +++ b/rmcs_ws/src/rmcs_core/src/hardware/deformable-infantry-omni.cpp @@ -296,17 +296,6 @@ class DeformableInfantryOmni } .as_bytes(), }); - builder.can1_transmit({ - .can_id = 0x1FE, - .can_data = - device::CanPacket8{ - device::CanPacket8::PaddingQuarter{}, - device::CanPacket8::PaddingQuarter{}, - device::CanPacket8::PaddingQuarter{}, - supercap_.generate_command(), - } - .as_bytes(), - }); } else { builder.can0_transmit({ .can_id = 0x141, From d04e252d681db21b9c98a71109448604973e1597 Mon Sep 17 00:00:00 2001 From: ZGZ713912 Date: Mon, 18 May 2026 12:06:54 +0800 Subject: [PATCH 17/52] feat: add submodule configurations for hikcamera and auto_aim_v2 --- .gitmodules | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/.gitmodules b/.gitmodules index 61e5f08f..aeef93a2 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,3 +1,11 @@ [submodule "rmcs_ws/src/fast_tf"] path = rmcs_ws/src/fast_tf url = https://github.com/qzhhhi/FastTF.git + +[submodule "rmcs_ws/src/hikcamera"] + path = rmcs_ws/src/hikcamera + url = https://github.com/Alliance-Algorithm/ros2-hikcamera.git + +[submodule "rmcs_ws/src/auto_aim_v2"] + path = rmcs_ws/src/auto_aim_v2 + url = https://github.com/Alliance-Algorithm/rmcs_auto_aim_v2.git \ No newline at end of file From 234fe7302fc9df8e718472807f08be1ecdacca17 Mon Sep 17 00:00:00 2001 From: ZGZ713912 Date: Tue, 19 May 2026 00:04:10 +0800 Subject: [PATCH 18/52] feat: remove unused inputs and simplify toggle logic in deformable chassis controller --- rmcs_ws/src/rmcs_auto_aim_v2 | 2 +- .../chassis/chassis_power_controller.cpp | 3 +- .../controller/chassis/deformable_chassis.cpp | 89 +++++-------------- 3 files changed, 25 insertions(+), 69 deletions(-) diff --git a/rmcs_ws/src/rmcs_auto_aim_v2 b/rmcs_ws/src/rmcs_auto_aim_v2 index 968a36c3..d835419b 160000 --- a/rmcs_ws/src/rmcs_auto_aim_v2 +++ b/rmcs_ws/src/rmcs_auto_aim_v2 @@ -1 +1 @@ -Subproject commit 968a36c359c654e00fb9084ab1e5551810a19233 +Subproject commit d835419bcc0a0e62eb22af298f9259f2c57a8d75 diff --git a/rmcs_ws/src/rmcs_core/src/controller/chassis/chassis_power_controller.cpp b/rmcs_ws/src/rmcs_core/src/controller/chassis/chassis_power_controller.cpp index d4988239..2920b48f 100644 --- a/rmcs_ws/src/rmcs_core/src/controller/chassis/chassis_power_controller.cpp +++ b/rmcs_ws/src/rmcs_core/src/controller/chassis/chassis_power_controller.cpp @@ -54,7 +54,6 @@ class ChassisPowerController auto switch_right = *switch_right_; auto switch_left = *switch_left_; auto keyboard = *keyboard_; - auto rotary_knob = *rotary_knob_; if ((switch_left == Switch::UNKNOWN || switch_right == Switch::UNKNOWN) || (switch_left == Switch::DOWN && switch_right == Switch::DOWN)) { @@ -64,7 +63,7 @@ class ChassisPowerController update_virtual_buffer_energy(); - boost_mode_ = keyboard.shift || keyboard.ctrl || rotary_knob < -0.9; + boost_mode_ = keyboard.shift || keyboard.ctrl; *supercap_control_enabled_ = boost_mode_; update_control_power_limit(); } diff --git a/rmcs_ws/src/rmcs_core/src/controller/chassis/deformable_chassis.cpp b/rmcs_ws/src/rmcs_core/src/controller/chassis/deformable_chassis.cpp index 1473f296..ae51dcbb 100644 --- a/rmcs_ws/src/rmcs_core/src/controller/chassis/deformable_chassis.cpp +++ b/rmcs_ws/src/rmcs_core/src/controller/chassis/deformable_chassis.cpp @@ -5,14 +5,12 @@ #include #include #include -#include #include #include #include #include -#include #include #include #include @@ -132,7 +130,6 @@ class DeformableChassis register_input("/remote/keyboard", keyboard_); register_input("/remote/rotary_knob", rotary_knob_); register_input("/predefined/update_rate", update_rate_); - register_input("/tf", tf_, false); register_input("/gimbal/yaw/angle", gimbal_yaw_angle_, false); register_input("/gimbal/yaw/control_angle_error", gimbal_yaw_angle_error_, false); @@ -371,8 +368,6 @@ class DeformableChassis void activate_qe_complex_spin_(rmcs_msgs::ChassisMode& mode) { qe_complex_spin_active_ = true; qe_last_toggle_elapsed_ = 0.0; - qe_front_window_armed_ = true; - qe_rear_window_armed_ = true; qe_front_high_rear_low_ = true; apply_front_high_rear_low_target_(); if (mode != rmcs_msgs::ChassisMode::SPIN) { @@ -384,8 +379,6 @@ class DeformableChassis void deactivate_qe_complex_spin_() { qe_complex_spin_active_ = false; qe_last_toggle_elapsed_ = 0.0; - qe_front_window_armed_ = true; - qe_rear_window_armed_ = true; } void apply_front_high_rear_low_target_() { @@ -394,6 +387,7 @@ class DeformableChassis lb_current_target_angle_ = min_angle_; rb_current_target_angle_ = min_angle_; apply_symmetric_target = false; + qe_front_high_rear_low_ = true; } void apply_front_low_rear_high_target_() { @@ -402,72 +396,32 @@ class DeformableChassis lb_current_target_angle_ = max_angle_; rb_current_target_angle_ = max_angle_; apply_symmetric_target = false; + qe_front_high_rear_low_ = false; } - void toggle_qe_complex_spin_target_() { - qe_front_high_rear_low_ = !qe_front_high_rear_low_; + void toggle_bg_target_() { if (qe_front_high_rear_low_) { - apply_front_high_rear_low_target_(); - } else { apply_front_low_rear_high_target_(); + } else { + apply_front_high_rear_low_target_(); } - qe_last_toggle_elapsed_ = 0.0; - } - - static double wrap_to_pi_(double angle) { - while (angle > std::numbers::pi) - angle -= 2.0 * std::numbers::pi; - while (angle < -std::numbers::pi) - angle += 2.0 * std::numbers::pi; - return angle; - } - - static double heading_from_direction_(const Eigen::Vector3d& direction) { - return std::atan2(direction.y(), direction.x()); } - std::optional> read_qe_spin_headings_() const { - if (!tf_.ready()) - return std::nullopt; - - const auto chassis_direction = fast_tf::cast( - rmcs_description::PitchLink::DirectionVector{Eigen::Vector3d::UnitX()}, *tf_); - const auto gimbal_direction = fast_tf::cast( - rmcs_description::YawLink::DirectionVector{Eigen::Vector3d::UnitX()}, *tf_); - - return std::pair{ - heading_from_direction_(*chassis_direction), heading_from_direction_(*gimbal_direction)}; + void toggle_qe_complex_spin_target_() { + toggle_bg_target_(); } void update_qe_complex_spin_toggle_() { - constexpr double heading_trigger_threshold = 20.0 * std::numbers::pi / 180.0; - constexpr double heading_rearm_threshold = 40.0 * std::numbers::pi / 180.0; - constexpr double force_toggle_timeout = 0.25; + constexpr double qe_complex_spin_toggle_period = 1.0; qe_last_toggle_elapsed_ += update_dt(); - - bool should_toggle = false; - if (const auto headings = read_qe_spin_headings_()) { - const auto& [chassis_heading, gimbal_heading] = *headings; - const double relative_heading = wrap_to_pi_(chassis_heading - gimbal_heading); - const double front_error = std::abs(relative_heading); - const double rear_error = std::abs(std::abs(relative_heading) - std::numbers::pi); - - if (front_error >= heading_rearm_threshold) - qe_front_window_armed_ = true; - if (rear_error >= heading_rearm_threshold) - qe_rear_window_armed_ = true; - - if (front_error <= heading_trigger_threshold && qe_front_window_armed_) { - should_toggle = true; - qe_front_window_armed_ = false; - } else if (rear_error <= heading_trigger_threshold && qe_rear_window_armed_) { - should_toggle = true; - qe_rear_window_armed_ = false; - } + size_t qe_complex_spin_toggle_count = 0; + while (qe_last_toggle_elapsed_ >= qe_complex_spin_toggle_period) { + qe_last_toggle_elapsed_ -= qe_complex_spin_toggle_period; + ++qe_complex_spin_toggle_count; } - if (should_toggle || qe_last_toggle_elapsed_ >= force_toggle_timeout) + if ((qe_complex_spin_toggle_count % 2) == 1) toggle_qe_complex_spin_target_(); } @@ -782,14 +736,18 @@ class DeformableChassis } void update_lift_target_toggle(rmcs_msgs::Keyboard keyboard) { - constexpr double rotary_knob_edge_threshold = 0.7; - constexpr double complex_spin_toggle_period = 0.2; + constexpr double rotary_knob_symmetric_edge_threshold = 0.7; + constexpr double rotary_knob_bg_edge_threshold = -0.9; + constexpr double complex_spin_toggle_period = 0.5; const bool keyboard_toggle_condition = !qe_complex_spin_active_ && !last_keyboard_.q && keyboard.q && !keyboard.e; const bool rotary_knob_toggle_condition = - last_rotary_knob_ < rotary_knob_edge_threshold - && *rotary_knob_ >= rotary_knob_edge_threshold; + last_rotary_knob_ < rotary_knob_symmetric_edge_threshold + && *rotary_knob_ >= rotary_knob_symmetric_edge_threshold; + const bool rotary_knob_bg_toggle_condition = + !qe_complex_spin_active_ && last_rotary_knob_ > rotary_knob_bg_edge_threshold + && *rotary_knob_ <= rotary_knob_bg_edge_threshold; const bool front_high_rear_low = !qe_complex_spin_active_ && !last_keyboard_.b && keyboard.b; const bool front_low_rear_high = !qe_complex_spin_active_ && !last_keyboard_.g && keyboard.g; bool complex_spin_toggle_condition = false; @@ -818,6 +776,8 @@ class DeformableChassis current_target_angle_ = (std::abs(current_target_angle_ - max_angle_) < 1e-6) ? min_angle_ : max_angle_; apply_symmetric_target = true; + } else if (rotary_knob_bg_toggle_condition) { + toggle_bg_target_(); } else if (front_high_rear_low) { apply_front_high_rear_low_target_(); } else if (front_low_rear_high) { @@ -989,7 +949,6 @@ class DeformableChassis } private: - InputInterface tf_; InputInterface joystick_right_; InputInterface switch_right_; InputInterface switch_left_; @@ -1013,8 +972,6 @@ class DeformableChassis bool complex_spin_active_ = false; double complex_spin_elapsed_ = 0.0; bool qe_complex_spin_active_ = false; - bool qe_front_window_armed_ = true; - bool qe_rear_window_armed_ = true; bool qe_front_high_rear_low_ = true; double qe_last_toggle_elapsed_ = 0.0; pid::PidCalculator following_velocity_controller_; From 1827c98820d0248e8883cf7aa0bc57da90dea8dc Mon Sep 17 00:00:00 2001 From: ZGZ713912 Date: Tue, 19 May 2026 02:16:42 +0800 Subject: [PATCH 19/52] feat: enhance auto-aim logic in bullet feeder controller --- .../shooting/bullet_feeder_controller_17mm.cpp | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/rmcs_ws/src/rmcs_core/src/controller/shooting/bullet_feeder_controller_17mm.cpp b/rmcs_ws/src/rmcs_core/src/controller/shooting/bullet_feeder_controller_17mm.cpp index 556762af..49f41690 100644 --- a/rmcs_ws/src/rmcs_core/src/controller/shooting/bullet_feeder_controller_17mm.cpp +++ b/rmcs_ws/src/rmcs_core/src/controller/shooting/bullet_feeder_controller_17mm.cpp @@ -84,13 +84,15 @@ class BulletFeederController17mm if (switch_right != Switch::DOWN) { shoot_mode = keyboard.f ? ShootMode::SINGLE : ShootMode::AUTOMATIC; + const bool auto_aim_requested = mouse.right || switch_right == Switch::UP; single_shot_stop_counter_ = std::max(0, single_shot_stop_counter_ - 1); temporary_single_shot_counter_ = std::max(0, temporary_single_shot_counter_ - 1); if (!last_mouse_.left && mouse.left) single_shot_stop_counter_ = single_shot_max_stop_delay_; - else if (last_switch_left_ != Switch::DOWN && switch_left == Switch::DOWN) { + else if (!auto_aim_requested && last_switch_left_ != Switch::DOWN + && switch_left == Switch::DOWN) { single_shot_stop_counter_ = single_shot_max_stop_delay_; temporary_single_shot_counter_ = 500; } @@ -102,9 +104,12 @@ class BulletFeederController17mm if (*friction_ready_) { if (shoot_mode == ShootMode::AUTOMATIC) { - const bool auto_aim_enabled = mouse.right || switch_right == Switch::UP; - bool triggered = (mouse.left && !mouse.right) || switch_left == Switch::DOWN - || (mouse.left && auto_aim_enabled && *fire_control_); + const bool auto_aim_enabled = auto_aim_requested; + bool triggered = + (mouse.left && !mouse.right) || + (switch_left == Switch::DOWN && auto_aim_enabled && *fire_control_) || + (switch_left == Switch::DOWN && !auto_aim_enabled) || + (mouse.left && auto_aim_enabled && *fire_control_); bullet_allowance = triggered ? *control_bullet_allowance_limited_by_heat_ : 0; } else { From ce5893aaf14033b88eb763768d521204b7a6d079 Mon Sep 17 00:00:00 2001 From: ZGZ713912 Date: Wed, 20 May 2026 02:53:49 +0800 Subject: [PATCH 20/52] feat: update joint target state management and fallback radius handling in deformable infantry --- .../config/deformable-infantry-omni.yaml | 2 +- .../controller/chassis/deformable_chassis.cpp | 125 ++++++++++-------- .../hardware/deformable-infantry-omni-b.cpp | 9 +- .../src/hardware/deformable-infantry-omni.cpp | 10 +- 4 files changed, 85 insertions(+), 61 deletions(-) diff --git a/rmcs_ws/src/rmcs_bringup/config/deformable-infantry-omni.yaml b/rmcs_ws/src/rmcs_bringup/config/deformable-infantry-omni.yaml index 3885fdd2..bb968c23 100644 --- a/rmcs_ws/src/rmcs_bringup/config/deformable-infantry-omni.yaml +++ b/rmcs_ws/src/rmcs_bringup/config/deformable-infantry-omni.yaml @@ -47,7 +47,7 @@ deformable_infantry: left_back_zero_point: 5801 right_back_zero_point: 7817 right_front_zero_point: 7136 - yaw_motor_zero_point: 31856 + yaw_motor_zero_point: 31517 pitch_motor_zero_point: 6853 debug_log_supercap: false debug_log_wheel_motor: false diff --git a/rmcs_ws/src/rmcs_core/src/controller/chassis/deformable_chassis.cpp b/rmcs_ws/src/rmcs_core/src/controller/chassis/deformable_chassis.cpp index ae51dcbb..8e82b832 100644 --- a/rmcs_ws/src/rmcs_core/src/controller/chassis/deformable_chassis.cpp +++ b/rmcs_ws/src/rmcs_core/src/controller/chassis/deformable_chassis.cpp @@ -534,19 +534,29 @@ class DeformableChassis chassis_imu_calibration_sample_count_); } - bool initialize_joint_target_states_from_feedback( + bool ensure_joint_target_states_from_feedback( const std::array& current_physical_angles) { + bool any_active = false; for (size_t i = 0; i < kJointCount; ++i) { - if (!std::isfinite(current_physical_angles[i])) - return false; + if (std::isfinite(current_physical_angles[i]) && !joint_target_active_[i]) { + joint_target_physical_angle_state_rad_[i] = current_physical_angles[i]; + joint_target_physical_velocity_state_rad_[i] = 0.0; + joint_target_physical_acceleration_state_rad_[i] = 0.0; + current_target_physical_angles_rad_[i] = current_physical_angles[i]; + joint_target_active_[i] = true; + } + + any_active = any_active || joint_target_active_[i]; } - joint_target_physical_angle_state_rad_ = current_physical_angles; - joint_target_physical_velocity_state_rad_ = {0.0, 0.0, 0.0, 0.0}; - joint_target_physical_acceleration_state_rad_ = {0.0, 0.0, 0.0, 0.0}; - current_target_physical_angles_rad_ = current_physical_angles; - joint_target_active_ = true; - return true; + return any_active; + } + + bool any_joint_target_active_() const { + return std::any_of( + joint_target_active_.begin(), joint_target_active_.end(), [](bool active) { + return active; + }); } double active_suspension_min_angle_rad_() const { @@ -622,7 +632,11 @@ class DeformableChassis lb_current_target_angle_ = current_target_angle_; rb_current_target_angle_ = current_target_angle_; rf_current_target_angle_ = current_target_angle_; - joint_target_active_ = false; + joint_target_active_.fill(false); + current_target_physical_angles_rad_.fill(nan_); + joint_target_physical_angle_state_rad_.fill(nan_); + joint_target_physical_velocity_state_rad_.fill(0.0); + joint_target_physical_acceleration_state_rad_.fill(0.0); suspension_on_by_switch = false; deactivate_complex_spin_(); deactivate_qe_complex_spin_(); @@ -793,8 +807,7 @@ class DeformableChassis const auto current_physical_angles = read_current_joint_physical_angles_(); const bool suspension_requested = suspension_requested_by_input_(); - if (!joint_target_active_ - && !initialize_joint_target_states_from_feedback(current_physical_angles)) { + if (!ensure_joint_target_states_from_feedback(current_physical_angles)) { publish_nan_joint_targets(); return; } @@ -829,6 +842,9 @@ class DeformableChassis void update_joint_target_trajectory() { const double dt = update_dt(); for (size_t i = 0; i < kJointCount; ++i) { + if (!joint_target_active_[i]) + continue; + double& angle_state = joint_target_physical_angle_state_rad_[i]; double& velocity_state = joint_target_physical_velocity_state_rad_[i]; double& acceleration_state = joint_target_physical_acceleration_state_rad_[i]; @@ -874,57 +890,54 @@ class DeformableChassis void publish_joint_target_angles( const std::array& current_physical_angles) { - if (!joint_target_active_) { + if (!any_joint_target_active_()) { publish_nan_joint_targets(); return; } - *left_front_joint_target_physical_angle_ = - joint_target_physical_angle_state_rad_[kLeftFront]; - *left_back_joint_target_physical_angle_ = joint_target_physical_angle_state_rad_[kLeftBack]; - *right_back_joint_target_physical_angle_ = - joint_target_physical_angle_state_rad_[kRightBack]; - *right_front_joint_target_physical_angle_ = - joint_target_physical_angle_state_rad_[kRightFront]; - - *left_front_joint_target_physical_velocity_ = - joint_target_physical_velocity_state_rad_[kLeftFront]; - *left_back_joint_target_physical_velocity_ = - joint_target_physical_velocity_state_rad_[kLeftBack]; - *right_back_joint_target_physical_velocity_ = - joint_target_physical_velocity_state_rad_[kRightBack]; - *right_front_joint_target_physical_velocity_ = - joint_target_physical_velocity_state_rad_[kRightFront]; - - *left_front_joint_target_physical_acceleration_ = - joint_target_physical_acceleration_state_rad_[kLeftFront]; - *left_back_joint_target_physical_acceleration_ = - joint_target_physical_acceleration_state_rad_[kLeftBack]; - *right_back_joint_target_physical_acceleration_ = - joint_target_physical_acceleration_state_rad_[kRightBack]; - *right_front_joint_target_physical_acceleration_ = - joint_target_physical_acceleration_state_rad_[kRightFront]; - - *lf_angle_error_ = std::isfinite(current_physical_angles[kLeftFront]) - ? current_physical_angles[kLeftFront] - - joint_target_physical_angle_state_rad_[kLeftFront] - : nan_; - *lb_angle_error_ = std::isfinite(current_physical_angles[kLeftBack]) - ? current_physical_angles[kLeftBack] - - joint_target_physical_angle_state_rad_[kLeftBack] - : nan_; - *rb_angle_error_ = std::isfinite(current_physical_angles[kRightBack]) - ? current_physical_angles[kRightBack] - - joint_target_physical_angle_state_rad_[kRightBack] - : nan_; - *rf_angle_error_ = std::isfinite(current_physical_angles[kRightFront]) - ? current_physical_angles[kRightFront] - - joint_target_physical_angle_state_rad_[kRightFront] - : nan_; + const auto publish_joint = [this, ¤t_physical_angles]( + size_t index, OutputInterface& angle_output, + OutputInterface& velocity_output, + OutputInterface& acceleration_output, + OutputInterface& angle_error_output) { + if (!joint_target_active_[index]) { + *angle_output = nan_; + *velocity_output = nan_; + *acceleration_output = nan_; + *angle_error_output = nan_; + return; + } + + *angle_output = joint_target_physical_angle_state_rad_[index]; + *velocity_output = joint_target_physical_velocity_state_rad_[index]; + *acceleration_output = joint_target_physical_acceleration_state_rad_[index]; + *angle_error_output = std::isfinite(current_physical_angles[index]) + ? current_physical_angles[index] + - joint_target_physical_angle_state_rad_[index] + : nan_; + }; + + publish_joint( + kLeftFront, left_front_joint_target_physical_angle_, + left_front_joint_target_physical_velocity_, + left_front_joint_target_physical_acceleration_, lf_angle_error_); + publish_joint( + kLeftBack, left_back_joint_target_physical_angle_, + left_back_joint_target_physical_velocity_, + left_back_joint_target_physical_acceleration_, lb_angle_error_); + publish_joint( + kRightBack, right_back_joint_target_physical_angle_, + right_back_joint_target_physical_velocity_, + right_back_joint_target_physical_acceleration_, rb_angle_error_); + publish_joint( + kRightFront, right_front_joint_target_physical_angle_, + right_front_joint_target_physical_velocity_, + right_front_joint_target_physical_acceleration_, rf_angle_error_); } void publish_nan_joint_targets() { reset_attitude_correction_state_(); + joint_target_active_.fill(false); *left_front_joint_target_physical_angle_ = nan_; *left_back_joint_target_physical_angle_ = nan_; @@ -1015,7 +1028,7 @@ class DeformableChassis std::array current_target_physical_angles_rad_ = {0.0, 0.0, 0.0, 0.0}; - bool joint_target_active_ = false; + std::array joint_target_active_ = {false, false, false, false}; std::array joint_target_physical_angle_state_rad_ = {0.0, 0.0, 0.0, 0.0}; std::array joint_target_physical_velocity_state_rad_ = { 0.0, 0.0, 0.0, 0.0}; diff --git a/rmcs_ws/src/rmcs_core/src/hardware/deformable-infantry-omni-b.cpp b/rmcs_ws/src/rmcs_core/src/hardware/deformable-infantry-omni-b.cpp index 9a7c29a3..269083d8 100644 --- a/rmcs_ws/src/rmcs_core/src/hardware/deformable-infantry-omni-b.cpp +++ b/rmcs_ws/src/rmcs_core/src/hardware/deformable-infantry-omni-b.cpp @@ -198,7 +198,7 @@ class DeformableInfantryOmniB deformableInfantry.register_output("/chassis/encoder/alpha", encoder_alpha_, nan_); deformableInfantry.register_output( "/chassis/encoder/alpha_dot", encoder_alpha_dot_, nan_); - deformableInfantry.register_output("/chassis/radius", radius_, nan_); + deformableInfantry.register_output("/chassis/radius", radius_, default_radius_); deformableInfantry.get_parameter_or("debug_log_supercap", debug_log_supercap_, false); deformableInfantry.get_parameter_or( @@ -352,6 +352,7 @@ class DeformableInfantryOmniB static constexpr double joint_zero_physical_angle_rad_ = 62.5 * std::numbers::pi / 180.0; static constexpr double chassis_radius_base_ = 0.2341741; static constexpr double rod_length_ = 0.150; + static constexpr double default_radius_ = 0.5 * rod_length_ + chassis_radius_base_; static double to_physical_angle_(double motor_angle) { return joint_zero_physical_angle_rad_ - motor_angle; @@ -383,7 +384,11 @@ class DeformableInfantryOmniB if (!alpha_rad.array().isFinite().all() || !alpha_dot_rad.array().isFinite().all()) { *encoder_alpha_ = nan_; *encoder_alpha_dot_ = nan_; - *radius_ = nan_; + *radius_ = default_radius_; + RCLCPP_WARN_THROTTLE( + deformable_infantry_.get_logger(), *deformable_infantry_.get_clock(), 1000, + "deformable joint feedback invalid, fallback chassis radius to default %.3f m", + default_radius_); return; } diff --git a/rmcs_ws/src/rmcs_core/src/hardware/deformable-infantry-omni.cpp b/rmcs_ws/src/rmcs_core/src/hardware/deformable-infantry-omni.cpp index 561c22c0..11b162a2 100644 --- a/rmcs_ws/src/rmcs_core/src/hardware/deformable-infantry-omni.cpp +++ b/rmcs_ws/src/rmcs_core/src/hardware/deformable-infantry-omni.cpp @@ -19,6 +19,7 @@ #include #include +#include #include #include @@ -170,7 +171,7 @@ class DeformableInfantryOmni right_front_joint_physical_velocity_, kNaN); status.register_output("/chassis/encoder/alpha", encoder_alpha_, kNaN); status.register_output("/chassis/encoder/alpha_dot", encoder_alpha_dot_, kNaN); - status.register_output("/chassis/radius", radius_, kNaN); + status.register_output("/chassis/radius", radius_, default_radius_); status.get_parameter_or("debug_log_supercap", debug_log_supercap_, false); status.get_parameter_or("debug_log_wheel_motor", debug_log_wheel_motor_, false); @@ -320,6 +321,7 @@ class DeformableInfantryOmni static constexpr double joint_zero_physical_angle_rad_ = 62.5 * std::numbers::pi / 180.0; static constexpr double chassis_radius_base_ = 0.2341741; static constexpr double rod_length_ = 0.150; + static constexpr double default_radius_ = chassis_radius_base_ + rod_length_; DeformableInfantryOmni& status_; Component& command_; @@ -405,7 +407,11 @@ class DeformableInfantryOmni if (!alpha_rad.array().isFinite().all() || !alpha_dot_rad.array().isFinite().all()) { *encoder_alpha_ = kNaN; *encoder_alpha_dot_ = kNaN; - *radius_ = kNaN; + *radius_ = default_radius_; + RCLCPP_WARN_THROTTLE( + status_.get_logger(), *status_.get_clock(), 1000, + "deformable joint feedback invalid, fallback chassis radius to default %.3f m", + default_radius_); return; } From eb21d27679f01eb7ab7fa0265746755983806675 Mon Sep 17 00:00:00 2001 From: ZGZ713912 Date: Thu, 21 May 2026 02:18:37 +0800 Subject: [PATCH 21/52] feat: update yaw motor zero point in deformable infantry configuration --- rmcs_ws/src/rmcs_auto_aim_v2 | 2 +- rmcs_ws/src/rmcs_bringup/config/deformable-infantry-omni-b.yaml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/rmcs_ws/src/rmcs_auto_aim_v2 b/rmcs_ws/src/rmcs_auto_aim_v2 index d835419b..eaf4a736 160000 --- a/rmcs_ws/src/rmcs_auto_aim_v2 +++ b/rmcs_ws/src/rmcs_auto_aim_v2 @@ -1 +1 @@ -Subproject commit d835419bcc0a0e62eb22af298f9259f2c57a8d75 +Subproject commit eaf4a73625996ec3443fac333421e3f9c93e91c0 diff --git a/rmcs_ws/src/rmcs_bringup/config/deformable-infantry-omni-b.yaml b/rmcs_ws/src/rmcs_bringup/config/deformable-infantry-omni-b.yaml index a235d389..817e0a62 100644 --- a/rmcs_ws/src/rmcs_bringup/config/deformable-infantry-omni-b.yaml +++ b/rmcs_ws/src/rmcs_bringup/config/deformable-infantry-omni-b.yaml @@ -48,7 +48,7 @@ deformable_infantry: left_back_zero_point: 5167 right_back_zero_point: 3098 right_front_zero_point: 6485 - yaw_motor_zero_point: 18211 + yaw_motor_zero_point: 57900 pitch_motor_zero_point: 56354 debug_log_supercap: false debug_log_wheel_motor: false From fa7676c47553c4307b0cb760f1d67cedbb9719a3 Mon Sep 17 00:00:00 2001 From: ZGZ713912 Date: Thu, 21 May 2026 02:55:11 +0800 Subject: [PATCH 22/52] feat: update serial filter for top board and enhance top board functionality in deformable infantry --- .../config/deformable-infantry-omni.yaml | 2 +- .../src/hardware/deformable-infantry-omni.cpp | 98 ++++++++++++++++--- 2 files changed, 83 insertions(+), 17 deletions(-) diff --git a/rmcs_ws/src/rmcs_bringup/config/deformable-infantry-omni.yaml b/rmcs_ws/src/rmcs_bringup/config/deformable-infantry-omni.yaml index bb968c23..5eb1c098 100644 --- a/rmcs_ws/src/rmcs_bringup/config/deformable-infantry-omni.yaml +++ b/rmcs_ws/src/rmcs_bringup/config/deformable-infantry-omni.yaml @@ -41,7 +41,7 @@ value_broadcaster: deformable_infantry: ros__parameters: serial_filter_rmcs_board: "AF-23FB-EE32-B892-1302-AE70-D640-7B4E-0CBF" - serial_filter_top_board: "D4-0262-9E84-E715-CADB-9894-7241" + serial_filter_top_board: "AF-8AE3-4EC1-03C3-C494-88FE-2DC4-3018-0298" serial_filter_imu: "AF-8217-B05F-811B-6F3E-04EA-448E-9D03-CA2C" left_front_zero_point: 374 left_back_zero_point: 5801 diff --git a/rmcs_ws/src/rmcs_core/src/hardware/deformable-infantry-omni.cpp b/rmcs_ws/src/rmcs_core/src/hardware/deformable-infantry-omni.cpp index 11b162a2..0338686a 100644 --- a/rmcs_ws/src/rmcs_core/src/hardware/deformable-infantry-omni.cpp +++ b/rmcs_ws/src/rmcs_core/src/hardware/deformable-infantry-omni.cpp @@ -23,7 +23,6 @@ #include #include -#include #include #include #include @@ -53,7 +52,7 @@ class DeformableInfantryOmni bottom_board_ = std::make_unique( *this, *command_, get_parameter("serial_filter_rmcs_board").as_string()); top_board_ = std::make_unique( - *this, *command_, get_parameter("serial_filter_top_board").as_string()); + *this, *command_, get_parameter("serial_filter_top_board").as_string(), true); imu_board_ = std::make_unique(*this, get_parameter("serial_filter_imu").as_string()); @@ -68,6 +67,8 @@ class DeformableInfantryOmni ~DeformableInfantryOmni() override = default; + void before_updating() override { top_board_->request_hard_sync_read(); } + void update() override { bottom_board_->update(); top_board_->update(); @@ -615,15 +616,17 @@ class DeformableInfantryOmni device::Bmi088 bmi088_; }; - class TopBoard final : private librmcs::agent::CBoard { + class TopBoard final : private librmcs::agent::RmcsBoardLite { friend class DeformableInfantryOmni; public: explicit TopBoard( - DeformableInfantryOmni& status, Command& command, const std::string& serial_filter = {}) - : librmcs::agent::CBoard( + DeformableInfantryOmni& status, Command& command, std::string serial_filter = {}, + bool has_external_imu_board = false) + : librmcs::agent::RmcsBoardLite( serial_filter, librmcs::agent::AdvancedOptions{.dangerously_skip_version_checks = true}) + , has_external_imu_board_(has_external_imu_board) , tf_(status.tf_) , bmi088_(1000, 0.2, 0.0) , gimbal_pitch_motor_(status, command, "/gimbal/pitch") @@ -647,16 +650,26 @@ class DeformableInfantryOmni scope_motor_.configure( device::DjiMotor::Config{device::DjiMotor::Type::kM2006}.enable_multi_turn_angle()); - status.register_output("/gimbal/yaw/velocity_imu", gimbal_yaw_velocity_imu_); + status.register_output("/gimbal/yaw/velocity_imu", gimbal_yaw_velocity_bmi088_); + if (!has_external_imu_board_) + status.register_output("/gimbal/pitch/velocity_imu", gimbal_pitch_velocity_encoder_); - bmi088_.set_coordinate_mapping( - [](double x, double y, double z) { return std::make_tuple(y, -x, z); }); + bmi088_.set_coordinate_mapping([](double x, double y, double z) { + // Top board BMI088 maps to gimbal frame as (-x, -y, z). + return std::make_tuple(y, -x, z); + }); } ~TopBoard() override = default; + void request_hard_sync_read() { + // RMCS-lite top board variant currently has no GPIO hard-sync request + // path. + } + void update() { bmi088_.update_status(); + gimbal_pitch_motor_.update_status(); gimbal_left_friction_.update_status(); gimbal_right_friction_.update_status(); @@ -664,7 +677,24 @@ class DeformableInfantryOmni const double pitch_encoder_angle = gimbal_pitch_motor_.angle(); - *gimbal_yaw_velocity_imu_ = bmi088_.gz(); + *gimbal_yaw_velocity_bmi088_ = bmi088_.gz(); + if (!has_external_imu_board_) { + Eigen::Quaterniond const odom_imu_to_yaw_link{ + bmi088_.q0(), bmi088_.q1(), bmi088_.q2(), bmi088_.q3()}; + Eigen::Quaterniond const yaw_link_to_odom_imu = odom_imu_to_yaw_link.conjugate(); + Eigen::Quaterniond pitch_link_to_odom_imu = + Eigen::Quaterniond{ + Eigen::AngleAxisd{-pitch_encoder_angle, Eigen::Vector3d::UnitY()}} + * yaw_link_to_odom_imu; + pitch_link_to_odom_imu.normalize(); + + *gimbal_pitch_velocity_encoder_ = gimbal_pitch_motor_.velocity(); + // The BMI088 is mounted on the yaw link. fast_tf stores PitchLink -> + // OdomImu, so use the encoder pitch from the TF tree to move the + // yaw-link pose back into PitchLink. + tf_->set_transform( + pitch_link_to_odom_imu); + } tf_->set_state( pitch_encoder_angle); @@ -672,17 +702,41 @@ class DeformableInfantryOmni void command_update() { auto builder = start_transmit(); - builder.can1_transmit({ + builder.can0_transmit({ .can_id = 0x141, .can_data = gimbal_pitch_motor_.generate_command().as_bytes(), }); - builder.can2_transmit({ + builder.can1_transmit({ .can_id = 0x200, .can_data = device::CanPacket8{ gimbal_left_friction_.generate_command(), + device::CanPacket8::PaddingQuarter{}, + device::CanPacket8::PaddingQuarter{}, + device::CanPacket8::PaddingQuarter{}, + } + .as_bytes(), + }); + + builder.can2_transmit({ + .can_id = 0x200, + .can_data = + device::CanPacket8{ + device::CanPacket8::PaddingQuarter{}, gimbal_right_friction_.generate_command(), + device::CanPacket8::PaddingQuarter{}, + device::CanPacket8::PaddingQuarter{}, + } + .as_bytes(), + }); + + builder.can3_transmit({ + .can_id = 0x200, + .can_data = + device::CanPacket8{ + device::CanPacket8::PaddingQuarter{}, + device::CanPacket8::PaddingQuarter{}, scope_motor_.generate_command(), device::CanPacket8::PaddingQuarter{}, } @@ -693,21 +747,31 @@ class DeformableInfantryOmni private: void uart1_receive_callback(const librmcs::data::UartDataView&) override {} - void can1_receive_callback(const librmcs::data::CanDataView& data) override { + void can0_receive_callback(const librmcs::data::CanDataView& data) override { if (data.is_extended_can_id || data.is_remote_transmission) [[unlikely]] return; if (data.can_id == 0x141) gimbal_pitch_motor_.store_status(data.can_data); } - void can2_receive_callback(const librmcs::data::CanDataView& data) override { + void can1_receive_callback(const librmcs::data::CanDataView& data) override { if (data.is_extended_can_id || data.is_remote_transmission) [[unlikely]] return; if (data.can_id == 0x201) gimbal_left_friction_.store_status(data.can_data); - else if (data.can_id == 0x202) + } + + void can2_receive_callback(const librmcs::data::CanDataView& data) override { + if (data.is_extended_can_id || data.is_remote_transmission) [[unlikely]] + return; + if (data.can_id == 0x202) gimbal_right_friction_.store_status(data.can_data); - else if (data.can_id == 0x203) + } + + void can3_receive_callback(const librmcs::data::CanDataView& data) override { + if (data.is_extended_can_id || data.is_remote_transmission) [[unlikely]] + return; + if (data.can_id == 0x203) scope_motor_.store_status(data.can_data); } @@ -720,8 +784,10 @@ class DeformableInfantryOmni bmi088_.store_gyroscope_status(data.x, data.y, data.z); } + bool has_external_imu_board_ = false; OutputInterface& tf_; - OutputInterface gimbal_yaw_velocity_imu_; + OutputInterface gimbal_yaw_velocity_bmi088_; + OutputInterface gimbal_pitch_velocity_encoder_; device::Bmi088 bmi088_; device::LkMotor gimbal_pitch_motor_; From ed1ade5829aebde3fd8ef6dae6735d3440fde1db Mon Sep 17 00:00:00 2001 From: qzhhhi Date: Thu, 21 May 2026 02:00:51 +0800 Subject: [PATCH 23/52] vt13 test --- .../rmcs_core/src/hardware/device/dr16.hpp | 322 ++++------ .../src/hardware/device/remote_control.hpp | 94 +++ .../rmcs_core/src/hardware/device/vt13.hpp | 198 ++++++ .../rmcs_core/src/hardware/omni_infantry.cpp | 55 +- rmcs_ws/src/rmcs_core/src/hardware/sentry.cpp | 500 --------------- .../rmcs_core/src/hardware/steering-hero.cpp | 591 ------------------ .../src/hardware/steering-infantry.cpp | 537 ---------------- .../include/rmcs_utility/ring_buffer.hpp | 37 ++ 8 files changed, 478 insertions(+), 1856 deletions(-) create mode 100644 rmcs_ws/src/rmcs_core/src/hardware/device/remote_control.hpp create mode 100644 rmcs_ws/src/rmcs_core/src/hardware/device/vt13.hpp delete mode 100644 rmcs_ws/src/rmcs_core/src/hardware/sentry.cpp delete mode 100644 rmcs_ws/src/rmcs_core/src/hardware/steering-hero.cpp delete mode 100644 rmcs_ws/src/rmcs_core/src/hardware/steering-infantry.cpp diff --git a/rmcs_ws/src/rmcs_core/src/hardware/device/dr16.hpp b/rmcs_ws/src/rmcs_core/src/hardware/device/dr16.hpp index 75152b17..6bd0ac2f 100644 --- a/rmcs_ws/src/rmcs_core/src/hardware/device/dr16.hpp +++ b/rmcs_ws/src/rmcs_core/src/hardware/device/dr16.hpp @@ -1,16 +1,13 @@ #pragma once +#include +#include #include #include #include - -#include -#include +#include #include -#include -#include -#include #include #include #include @@ -19,230 +16,165 @@ namespace rmcs_core::hardware::device { class Dr16 { public: - explicit Dr16(rmcs_executor::Component& component) { - component.register_output( - "/remote/joystick/right", joystick_right_output_, Eigen::Vector2d::Zero()); - component.register_output( - "/remote/joystick/left", joystick_left_output_, Eigen::Vector2d::Zero()); - - component.register_output( - "/remote/switch/right", switch_right_output_, rmcs_msgs::Switch::UNKNOWN); - component.register_output( - "/remote/switch/left", switch_left_output_, rmcs_msgs::Switch::UNKNOWN); - - component.register_output( - "/remote/mouse/velocity", mouse_velocity_output_, Eigen::Vector2d::Zero()); - component.register_output("/remote/mouse/mouse_wheel", mouse_wheel_output_); - - component.register_output("/remote/mouse", mouse_output_); - std::memset(&*mouse_output_, 0, sizeof(*mouse_output_)); - component.register_output("/remote/keyboard", keyboard_output_); - std::memset(&*keyboard_output_, 0, sizeof(*keyboard_output_)); - - component.register_output("/remote/rotary_knob", rotary_knob_output_); - - // Simulate the rotary knob as a switch, with anti-shake algorithm. - component.register_output( - "/remote/rotary_knob_switch", rotary_knob_switch_output_, rmcs_msgs::Switch::UNKNOWN); - } + Dr16() = default; - void store_status(const std::byte* uart_data, size_t uart_data_length) { - if (uart_data_length != 6 + 8 + 4) + void store_status(std::span uart_data) { + if (uart_data.size() != kStatusSize) return; - // Avoid using reinterpret_cast here because it does not account for pointer alignment. - // Dr16DataPart structures are aligned, and using reinterpret_cast on potentially unaligned - // uart_data can cause undefined behavior on architectures that enforce strict alignment - // requirements (e.g., ARM). - // Directly accessing unaligned memory through a casted pointer can lead to crashes, - // inefficiencies, or incorrect data reads. Instead, std::memcpy safely copies the data from - // unaligned memory to properly aligned structures without violating alignment or strict - // aliasing rules. + auto* cursor = uart_data.data(); uint64_t part1{}; - std::memcpy(&part1, uart_data, 6); - uart_data += 6; + std::memcpy(&part1, cursor, kPart1Size); + cursor += kPart1Size; data_part1_.store(part1, std::memory_order::relaxed); uint64_t part2{}; - std::memcpy(&part2, uart_data, 8); - uart_data += 8; + std::memcpy(&part2, cursor, kPart2Size); + cursor += kPart2Size; data_part2_.store(part2, std::memory_order::relaxed); uint32_t part3{}; - std::memcpy(&part3, uart_data, 4); - uart_data += 4; + std::memcpy(&part3, cursor, kPart3Size); data_part3_.store(part3, std::memory_order::relaxed); } void update_status() { - auto part1 alignas(uint64_t) = - std::bit_cast(data_part1_.load(std::memory_order::relaxed)); - - auto channel_to_double = [](int32_t value) { - value -= 1024; - if (-660 <= value && value <= 660) - return value / 660.0; - return 0.0; - }; - joystick_right_.y = -channel_to_double(static_cast(part1.joystick_channel0)); - joystick_right_.x = channel_to_double(static_cast(part1.joystick_channel1)); - joystick_left_.y = -channel_to_double(static_cast(part1.joystick_channel2)); - joystick_left_.x = channel_to_double(static_cast(part1.joystick_channel3)); - - switch_right_ = static_cast(part1.switch_right); - switch_left_ = static_cast(part1.switch_left); + const auto raw_part1 = data_part1_.load(std::memory_order::relaxed); + const auto part1 = std::bit_cast(raw_part1); - auto part2 alignas(uint64_t) = - std::bit_cast(data_part2_.load(std::memory_order::relaxed)); + joystick_right_ = { + channel_to_double(static_cast(part1.joystick_channel1)), + -channel_to_double(static_cast(part1.joystick_channel0)), + }; + joystick_left_ = { + channel_to_double(static_cast(part1.joystick_channel3)), + -channel_to_double(static_cast(part1.joystick_channel2)), + }; - mouse_velocity_.x = -part2.mouse_velocity_y / 32768.0; - mouse_velocity_.y = -part2.mouse_velocity_x / 32768.0; + switch_right_ = static_cast(part1.switch_right); + switch_left_ = static_cast(part1.switch_left); - mouse_wheel_ = -part2.mouse_velocity_z / 32768.0; + const auto raw_part2 = data_part2_.load(std::memory_order::relaxed); + const auto part2 = std::bit_cast(raw_part2); - mouse_.left = part2.mouse_left; - mouse_.right = part2.mouse_right; + mouse_velocity_ = { + -static_cast(part2.mouse_velocity_y) / 32768.0, + -static_cast(part2.mouse_velocity_x) / 32768.0, + }; + mouse_wheel_ = -static_cast(part2.mouse_velocity_z) / 32768.0; + mouse_ = { + .left = part2.mouse_left, + .right = part2.mouse_right, + }; - auto part3 alignas(uint32_t) = - std::bit_cast(data_part3_.load(std::memory_order::relaxed)); + const auto raw_part3 = data_part3_.load(std::memory_order::relaxed); + const auto part3 = std::bit_cast(raw_part3); - keyboard_ = part3.keyboard; + keyboard_ = std::bit_cast(part3.keyboard); rotary_knob_ = channel_to_double(part3.rotary_knob); - - *joystick_right_output_ = joystick_right(); - *joystick_left_output_ = joystick_left(); - - *switch_right_output_ = switch_right(); - *switch_left_output_ = switch_left(); - - *mouse_velocity_output_ = mouse_velocity(); - *mouse_wheel_output_ = mouse_wheel(); - - *mouse_output_ = mouse(); - *keyboard_output_ = keyboard(); - - *rotary_knob_output_ = rotary_knob(); update_rotary_knob_switch(); } - struct Vector { - constexpr static Vector zero() { return {.x = 0, .y = 0}; } - double x, y; - }; - - enum class Switch : uint8_t { kUnknown = 0, kUp = 1, kDown = 2, kMiddle = 3 }; - - struct [[gnu::packed]] Mouse { - constexpr static Mouse zero() { - constexpr uint8_t zero = 0; - return std::bit_cast(zero); - } + [[nodiscard]] const Eigen::Vector2d& joystick_right() const noexcept { return joystick_right_; } - bool left : 1; - bool right : 1; - }; - static_assert(sizeof(Mouse) == 1); + [[nodiscard]] const Eigen::Vector2d& joystick_left() const noexcept { return joystick_left_; } - struct [[gnu::packed]] Keyboard { - constexpr static Keyboard zero() { - constexpr uint16_t zero = 0; - return std::bit_cast(zero); - } + [[nodiscard]] rmcs_msgs::Switch switch_right() const noexcept { return switch_right_; } - bool w : 1; - bool s : 1; - bool a : 1; - bool d : 1; - bool shift : 1; - bool ctrl : 1; - bool q : 1; - bool e : 1; - bool r : 1; - bool f : 1; - bool g : 1; - bool z : 1; - bool x : 1; - bool c : 1; - bool v : 1; - bool b : 1; - }; - static_assert(sizeof(Keyboard) == 2); + [[nodiscard]] rmcs_msgs::Switch switch_left() const noexcept { return switch_left_; } - Eigen::Vector2d joystick_right() const { return to_eigen_vector(joystick_right_); } - Eigen::Vector2d joystick_left() const { return to_eigen_vector(joystick_left_); } + [[nodiscard]] const Eigen::Vector2d& mouse_velocity() const noexcept { return mouse_velocity_; } - rmcs_msgs::Switch switch_right() const { - return std::bit_cast(switch_right_); - } - rmcs_msgs::Switch switch_left() const { return std::bit_cast(switch_left_); } + [[nodiscard]] rmcs_msgs::Mouse mouse() const noexcept { return mouse_; } - Eigen::Vector2d mouse_velocity() const { return to_eigen_vector(mouse_velocity_); } + [[nodiscard]] rmcs_msgs::Keyboard keyboard() const noexcept { return keyboard_; } - rmcs_msgs::Mouse mouse() const { return std::bit_cast(mouse_); } - rmcs_msgs::Keyboard keyboard() const { return std::bit_cast(keyboard_); } + [[nodiscard]] double rotary_knob() const noexcept { return rotary_knob_; } - double rotary_knob() const { return rotary_knob_; } + [[nodiscard]] double mouse_wheel() const noexcept { return mouse_wheel_; } - double mouse_wheel() const { return mouse_wheel_; } + [[nodiscard]] rmcs_msgs::Switch rotary_knob_switch() const noexcept { + return rotary_knob_switch_; + } private: - static Eigen::Vector2d to_eigen_vector(Vector vector) { return {vector.x, vector.y}; } - - void update_rotary_knob_switch() { - constexpr double divider = 0.7, anti_shake_shift = 0.05; - double upper_divider = divider, lower_divider = -divider; - - auto& switch_value = *rotary_knob_switch_output_; - if (switch_value == rmcs_msgs::Switch::UP) - upper_divider -= anti_shake_shift, lower_divider -= anti_shake_shift; - else if (switch_value == rmcs_msgs::Switch::MIDDLE) - upper_divider += anti_shake_shift, lower_divider -= anti_shake_shift; - else if (switch_value == rmcs_msgs::Switch::DOWN) - upper_divider += anti_shake_shift, lower_divider += anti_shake_shift; - - const auto knob_value = -*rotary_knob_output_; - if (knob_value > upper_divider) { - switch_value = rmcs_msgs::Switch::UP; - } else if (knob_value < lower_divider) { - switch_value = rmcs_msgs::Switch::DOWN; - } else { - switch_value = rmcs_msgs::Switch::MIDDLE; - } - } + static constexpr std::size_t kPart1Size = 6; + static constexpr std::size_t kPart2Size = 8; + static constexpr std::size_t kPart3Size = 4; + static constexpr std::size_t kStatusSize = kPart1Size + kPart2Size + kPart3Size; struct [[gnu::packed]] Dr16DataPart1 { uint64_t joystick_channel0 : 11; uint64_t joystick_channel1 : 11; uint64_t joystick_channel2 : 11; uint64_t joystick_channel3 : 11; - uint64_t switch_right : 2; - uint64_t switch_left : 2; - + uint64_t switch_left : 2; uint64_t padding : 16; }; static_assert(sizeof(Dr16DataPart1) == 8); - std::atomic data_part1_{std::bit_cast(Dr16DataPart1{ - .joystick_channel0 = 1024, - .joystick_channel1 = 1024, - .joystick_channel2 = 1024, - .joystick_channel3 = 1024, - .switch_right = static_cast(Switch::kUnknown), - .switch_left = static_cast(Switch::kUnknown), - .padding = 0, - })}; - static_assert(decltype(data_part1_)::is_always_lock_free); struct [[gnu::packed]] Dr16DataPart2 { int16_t mouse_velocity_x; int16_t mouse_velocity_y; int16_t mouse_velocity_z; - bool mouse_left; bool mouse_right; }; static_assert(sizeof(Dr16DataPart2) == 8); + + struct [[gnu::packed]] Dr16DataPart3 { + uint16_t keyboard; + uint16_t rotary_knob; + }; + static_assert(sizeof(Dr16DataPart3) == 4); + + static double channel_to_double(int32_t value) { + value -= 1024; + if (-660 <= value && value <= 660) + return value / 660.0; + return 0.0; + } + + void update_rotary_knob_switch() { + constexpr double divider = 0.7; + constexpr double anti_shake_shift = 0.05; + + double upper_divider = divider; + double lower_divider = -divider; + if (rotary_knob_switch_ == rmcs_msgs::Switch::UP) { + upper_divider -= anti_shake_shift; + lower_divider -= anti_shake_shift; + } else if (rotary_knob_switch_ == rmcs_msgs::Switch::MIDDLE) { + upper_divider += anti_shake_shift; + lower_divider -= anti_shake_shift; + } else if (rotary_knob_switch_ == rmcs_msgs::Switch::DOWN) { + upper_divider += anti_shake_shift; + lower_divider += anti_shake_shift; + } + + const auto knob_value = -rotary_knob_; + if (knob_value > upper_divider) { + rotary_knob_switch_ = rmcs_msgs::Switch::UP; + } else if (knob_value < lower_divider) { + rotary_knob_switch_ = rmcs_msgs::Switch::DOWN; + } else { + rotary_knob_switch_ = rmcs_msgs::Switch::MIDDLE; + } + } + + std::atomic data_part1_{std::bit_cast(Dr16DataPart1{ + .joystick_channel0 = 1024, + .joystick_channel1 = 1024, + .joystick_channel2 = 1024, + .joystick_channel3 = 1024, + .switch_right = static_cast(rmcs_msgs::Switch::UNKNOWN), + .switch_left = static_cast(rmcs_msgs::Switch::UNKNOWN), + .padding = 0, + })}; + static_assert(decltype(data_part1_)::is_always_lock_free); + std::atomic data_part2_{std::bit_cast(Dr16DataPart2{ .mouse_velocity_x = 0, .mouse_velocity_y = 0, @@ -252,45 +184,25 @@ class Dr16 { })}; static_assert(decltype(data_part2_)::is_always_lock_free); - struct [[gnu::packed]] Dr16DataPart3 { - Keyboard keyboard; - uint16_t rotary_knob; - }; - static_assert(sizeof(Dr16DataPart3) == 4); - std::atomic data_part3_ = {std::bit_cast(Dr16DataPart3{ - .keyboard = Keyboard::zero(), + std::atomic data_part3_{std::bit_cast(Dr16DataPart3{ + .keyboard = 0, .rotary_knob = 0, })}; static_assert(decltype(data_part3_)::is_always_lock_free); - Vector joystick_right_ = Vector::zero(); - Vector joystick_left_ = Vector::zero(); - - Switch switch_right_ = Switch::kUnknown; - Switch switch_left_ = Switch::kUnknown; + Eigen::Vector2d joystick_right_ = Eigen::Vector2d::Zero(); + Eigen::Vector2d joystick_left_ = Eigen::Vector2d::Zero(); + Eigen::Vector2d mouse_velocity_ = Eigen::Vector2d::Zero(); - Vector mouse_velocity_ = Vector::zero(); + rmcs_msgs::Switch switch_right_ = rmcs_msgs::Switch::UNKNOWN; + rmcs_msgs::Switch switch_left_ = rmcs_msgs::Switch::UNKNOWN; + rmcs_msgs::Switch rotary_knob_switch_ = rmcs_msgs::Switch::UNKNOWN; - Mouse mouse_ = Mouse::zero(); - Keyboard keyboard_ = Keyboard::zero(); + rmcs_msgs::Mouse mouse_ = rmcs_msgs::Mouse::zero(); + rmcs_msgs::Keyboard keyboard_ = rmcs_msgs::Keyboard::zero(); double rotary_knob_ = 0.0; double mouse_wheel_ = 0.0; - - rmcs_executor::Component::OutputInterface joystick_right_output_; - rmcs_executor::Component::OutputInterface joystick_left_output_; - - rmcs_executor::Component::OutputInterface switch_right_output_; - rmcs_executor::Component::OutputInterface switch_left_output_; - - rmcs_executor::Component::OutputInterface mouse_velocity_output_; - rmcs_executor::Component::OutputInterface mouse_wheel_output_; - - rmcs_executor::Component::OutputInterface mouse_output_; - rmcs_executor::Component::OutputInterface keyboard_output_; - - rmcs_executor::Component::OutputInterface rotary_knob_output_; - rmcs_executor::Component::OutputInterface rotary_knob_switch_output_; }; } // namespace rmcs_core::hardware::device diff --git a/rmcs_ws/src/rmcs_core/src/hardware/device/remote_control.hpp b/rmcs_ws/src/rmcs_core/src/hardware/device/remote_control.hpp new file mode 100644 index 00000000..99b36c3b --- /dev/null +++ b/rmcs_ws/src/rmcs_core/src/hardware/device/remote_control.hpp @@ -0,0 +1,94 @@ +#pragma once + +#include + +#include +#include +#include +#include +#include + +#include "hardware/device/dr16.hpp" +#include "hardware/device/vt13.hpp" + +namespace rmcs_core::hardware::device { + +class RemoteControl { +public: + RemoteControl(rmcs_executor::Component& component, Dr16& dr16, Vt13& vt13) + : dr16_(dr16) + , vt13_(vt13) { + component.register_output( + "/remote/joystick/right", joystick_right_output_, Eigen::Vector2d::Zero()); + component.register_output( + "/remote/joystick/left", joystick_left_output_, Eigen::Vector2d::Zero()); + + component.register_output( + "/remote/switch/right", switch_right_output_, rmcs_msgs::Switch::UNKNOWN); + component.register_output( + "/remote/switch/left", switch_left_output_, rmcs_msgs::Switch::UNKNOWN); + + component.register_output("/remote/rotary_knob", rotary_knob_output_, 0.0); + component.register_output( + "/remote/rotary_knob_switch", rotary_knob_switch_output_, rmcs_msgs::Switch::UNKNOWN); + + component.register_output( + "/remote/mouse/velocity", mouse_velocity_output_, Eigen::Vector2d::Zero()); + component.register_output("/remote/mouse/mouse_wheel", mouse_wheel_output_, 0.0); + + component.register_output("/remote/mouse", mouse_output_, rmcs_msgs::Mouse::zero()); + component.register_output( + "/remote/keyboard", keyboard_output_, rmcs_msgs::Keyboard::zero()); + } + + void update() { + *joystick_right_output_ = dr16_.joystick_right(); + *joystick_left_output_ = dr16_.joystick_left(); + + if (vt13_.mode_switch() == Vt13::ModeSwitch::kCine) { + *switch_right_output_ = rmcs_msgs::Switch::DOWN; + *switch_left_output_ = rmcs_msgs::Switch::DOWN; + } else { + *switch_right_output_ = dr16_.switch_right(); + *switch_left_output_ = dr16_.switch_left(); + } + + *rotary_knob_output_ = dr16_.rotary_knob(); + *rotary_knob_switch_output_ = dr16_.rotary_knob_switch(); + + if (vt13_.mode_switch() == Vt13::ModeSwitch::kSport) { + *mouse_velocity_output_ = vt13_.mouse_velocity(); + *mouse_wheel_output_ = vt13_.mouse_wheel(); + + *mouse_output_ = vt13_.mouse(); + *keyboard_output_ = vt13_.keyboard(); + } else { + *mouse_velocity_output_ = dr16_.mouse_velocity(); + *mouse_wheel_output_ = dr16_.mouse_wheel(); + + *mouse_output_ = dr16_.mouse(); + *keyboard_output_ = dr16_.keyboard(); + } + } + +private: + Dr16& dr16_; + Vt13& vt13_; + + rmcs_executor::Component::OutputInterface joystick_right_output_; + rmcs_executor::Component::OutputInterface joystick_left_output_; + + rmcs_executor::Component::OutputInterface switch_right_output_; + rmcs_executor::Component::OutputInterface switch_left_output_; + + rmcs_executor::Component::OutputInterface rotary_knob_output_; + rmcs_executor::Component::OutputInterface rotary_knob_switch_output_; + + rmcs_executor::Component::OutputInterface mouse_velocity_output_; + rmcs_executor::Component::OutputInterface mouse_wheel_output_; + + rmcs_executor::Component::OutputInterface mouse_output_; + rmcs_executor::Component::OutputInterface keyboard_output_; +}; + +} // namespace rmcs_core::hardware::device diff --git a/rmcs_ws/src/rmcs_core/src/hardware/device/vt13.hpp b/rmcs_ws/src/rmcs_core/src/hardware/device/vt13.hpp new file mode 100644 index 00000000..6cf34d9e --- /dev/null +++ b/rmcs_ws/src/rmcs_core/src/hardware/device/vt13.hpp @@ -0,0 +1,198 @@ +#pragma once + +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +namespace rmcs_core::hardware::device { + +class Vt13 { +public: + enum class ModeSwitch : uint8_t { + kUnknown = 0, + kCine = 1, + kNormal = 2, + kSport = 3, + }; + + Vt13() = default; + + void store_status(std::span uart_data) { + const auto written = data_buffer_.emplace_back_n( + [iter = uart_data.cbegin()](std::byte* storage) mutable noexcept { + *storage = *iter++; + }, + uart_data.size()); + if (written != uart_data.size()) { + RCLCPP_WARN( + rclcpp::get_logger("vt13"), "VT13 input buffer overflow: dropped %zu of %zu bytes", + uart_data.size() - written, uart_data.size()); + } + } + + void update_status() { + auto readable = data_buffer_.readable(); + if (!readable) + return; + + while (readable) { + ReadResult result = VerificationFailed{}; + + const std::byte front = *data_buffer_.peek_front(); + if (front == std::byte{0xa9}) + result = read_remote_control_data(readable); + else if (front == std::byte(0xa5)) + result = read_referee_style_data(readable); + + if (std::holds_alternative(result)) { + break; + } + if (std::holds_alternative(result)) { + data_buffer_.pop_front([](std::byte&&) noexcept {}); + readable--; + continue; + } + if (std::holds_alternative(result)) { + readable -= std::get(result).read; + continue; + } + } + } + + [[nodiscard]] ModeSwitch mode_switch() const noexcept { return mode_switch_; } + + [[nodiscard]] const Eigen::Vector2d& mouse_velocity() const noexcept { return mouse_velocity_; } + [[nodiscard]] double mouse_wheel() const noexcept { return mouse_wheel_; } + + [[nodiscard]] rmcs_msgs::Mouse mouse() const noexcept { return mouse_; } + [[nodiscard]] rmcs_msgs::Keyboard keyboard() const noexcept { return keyboard_; } + +private: + struct Incomplete {}; + struct VerificationFailed {}; + struct Success { + std::size_t read; + }; + using ReadResult = std::variant; + + struct [[gnu::packed]] RemoteControlData { + static constexpr uint16_t kHeaderMagic = 0x53a9; + + uint16_t header; + + uint16_t joystick_channel0 : 11; + uint16_t joystick_channel1 : 11; + uint16_t joystick_channel2 : 11; + uint16_t joystick_channel3 : 11; + + uint8_t mode_switch : 2; + uint8_t pause_button : 1; + uint8_t left_custom_button : 1; + uint8_t right_custom_button : 1; + uint16_t dial : 11; + uint8_t trigger : 1; + uint8_t padding1 : 3; + + int16_t mouse_velocity_x; + int16_t mouse_velocity_y; + int16_t mouse_velocity_z; + uint8_t mouse_left : 2; + uint8_t mouse_right : 2; + uint8_t mouse_middle : 2; + uint8_t padding2 : 2; + + uint16_t keyboard; + + uint16_t crc16; + }; + + struct [[gnu::packed]] RefereeFrameHeader { + uint8_t sof; + uint16_t data_length; + uint8_t seq; + uint8_t crc8; + }; + + ReadResult read_remote_control_data(const std::size_t readable) { + if (readable < sizeof(RemoteControlData)) + return Incomplete{}; + + RemoteControlData data; + data_buffer_.peek_front_n( + [dst = reinterpret_cast(&data)](std::byte src) mutable noexcept { + *dst++ = src; + }, + sizeof(RemoteControlData)); + + if (data.header != RemoteControlData::kHeaderMagic + || !rmcs_utility::dji_crc::verify_crc16(data)) + return VerificationFailed{}; + + data_buffer_.pop_front_n([](std::byte&&) noexcept {}, sizeof(RemoteControlData)); + + update_remote_control_data(data); + return Success{sizeof(RemoteControlData)}; + } + + void update_remote_control_data(const RemoteControlData& data) { + mode_switch_ = static_cast(data.mode_switch + 1); + + mouse_velocity_ = { + -data.mouse_velocity_y / 32768.0, + -data.mouse_velocity_x / 32768.0, + }; + mouse_wheel_ = -static_cast(data.mouse_velocity_z) / 32768.0; + + mouse_ = { + .left = static_cast(data.mouse_left), + .right = static_cast(data.mouse_right), + }; + keyboard_ = std::bit_cast(data.keyboard); + } + + ReadResult read_referee_style_data(const std::size_t readable) { + if (readable < sizeof(RefereeFrameHeader)) + return Incomplete{}; + + RefereeFrameHeader header; + data_buffer_.peek_front_n( + [dst = reinterpret_cast(&header)](std::byte src) mutable noexcept { + *dst++ = src; + }, + sizeof(RefereeFrameHeader)); + + if (!rmcs_utility::dji_crc::verify_crc8(header)) + return VerificationFailed{}; + + const std::size_t total_frame_size = + sizeof(RefereeFrameHeader) + 2 + header.data_length + 2; + if (readable < total_frame_size) + return Incomplete{}; + + data_buffer_.pop_front_n([](std::byte&&) noexcept {}, total_frame_size); + return Success{total_frame_size}; + } + + rmcs_utility::RingBuffer data_buffer_{1024}; + + ModeSwitch mode_switch_ = ModeSwitch::kUnknown; + + Eigen::Vector2d mouse_velocity_ = Eigen::Vector2d::Zero(); + double mouse_wheel_ = 0; + + rmcs_msgs::Mouse mouse_ = rmcs_msgs::Mouse::zero(); + rmcs_msgs::Keyboard keyboard_ = rmcs_msgs::Keyboard::zero(); +}; + +} // namespace rmcs_core::hardware::device diff --git a/rmcs_ws/src/rmcs_core/src/hardware/omni_infantry.cpp b/rmcs_ws/src/rmcs_core/src/hardware/omni_infantry.cpp index 234d5aa7..3c01ac5a 100644 --- a/rmcs_ws/src/rmcs_core/src/hardware/omni_infantry.cpp +++ b/rmcs_ws/src/rmcs_core/src/hardware/omni_infantry.cpp @@ -6,7 +6,7 @@ #include #include -#include +#include #include #include #include @@ -25,20 +25,22 @@ #include "hardware/device/dji_motor.hpp" #include "hardware/device/dr16.hpp" #include "hardware/device/lk_motor.hpp" +#include "hardware/device/remote_control.hpp" #include "hardware/device/supercap.hpp" +#include "hardware/device/vt13.hpp" namespace rmcs_core::hardware { class OmniInfantry : public rmcs_executor::Component , public rclcpp::Node - , private librmcs::agent::CBoard { + , private librmcs::agent::RmcsBoardLite { public: OmniInfantry() : Node{ get_component_name(), rclcpp::NodeOptions{}.automatically_declare_parameters_from_overrides(true)} - , librmcs::agent::CBoard{get_parameter("board_serial").as_string()} + , librmcs::agent::RmcsBoardLite(get_parameter("board_serial").as_string(), {true}) , logger_(get_logger()) , infantry_command_( create_partner_component(get_component_name() + "_command", *this)) @@ -53,7 +55,7 @@ class OmniInfantry , gimbal_left_friction_(*this, *infantry_command_, "/gimbal/left_friction") , gimbal_right_friction_(*this, *infantry_command_, "/gimbal/right_friction") , gimbal_bullet_feeder_(*this, *infantry_command_, "/gimbal/bullet_feeder") - , dr16_{*this} + , remote_control_(*this, dr16_, vt13_) , bmi088_(1000, 0.2, 0.0) { for (auto& motor : chassis_wheel_motors_) @@ -126,9 +128,8 @@ class OmniInfantry [&buffer](std::byte byte) noexcept { *buffer++ = byte; }, size); }; referee_serial_->write = [this](const std::byte* buffer, size_t size) { - start_transmit().uart1_transmit({ - .uart_data = std::span{buffer, size} - }); + start_transmit().uart1_transmit( + {.uart_data = std::span{buffer, size}}); return size; }; } @@ -144,6 +145,8 @@ class OmniInfantry update_motors(); update_imu(); dr16_.update_status(); + vt13_.update_status(); + remote_control_.update(); supercap_.update_status(); } @@ -154,11 +157,11 @@ class OmniInfantry .can_id = 0x1FE, .can_data = device::CanPacket8{ - device::CanPacket8::PaddingQuarter{}, - device::CanPacket8::PaddingQuarter{}, - device::CanPacket8::PaddingQuarter{}, - supercap_.generate_command(), - } + device::CanPacket8::PaddingQuarter{}, + device::CanPacket8::PaddingQuarter{}, + device::CanPacket8::PaddingQuarter{}, + supercap_.generate_command(), + } .as_bytes(), }); @@ -171,11 +174,11 @@ class OmniInfantry .can_id = 0x200, .can_data = device::CanPacket8{ - chassis_wheel_motors_[0].generate_command(), - chassis_wheel_motors_[1].generate_command(), - chassis_wheel_motors_[2].generate_command(), - chassis_wheel_motors_[3].generate_command(), - } + chassis_wheel_motors_[0].generate_command(), + chassis_wheel_motors_[1].generate_command(), + chassis_wheel_motors_[2].generate_command(), + chassis_wheel_motors_[3].generate_command(), + } .as_bytes(), }); @@ -188,11 +191,11 @@ class OmniInfantry .can_id = 0x200, .can_data = device::CanPacket8{ - device::CanPacket8::PaddingQuarter{}, - gimbal_bullet_feeder_.generate_command(), - gimbal_left_friction_.generate_command(), - gimbal_right_friction_.generate_command(), - } + device::CanPacket8::PaddingQuarter{}, + gimbal_bullet_feeder_.generate_command(), + gimbal_left_friction_.generate_command(), + gimbal_right_friction_.generate_command(), + } .as_bytes(), }); } @@ -277,6 +280,10 @@ class OmniInfantry } } + void uart0_receive_callback(const librmcs::data::UartDataView& data) override { + vt13_.store_status(data.uart_data); + } + void uart1_receive_callback(const librmcs::data::UartDataView& data) override { const auto* uart_data = data.uart_data.data(); referee_ring_buffer_receive_.emplace_back_n( @@ -285,7 +292,7 @@ class OmniInfantry } void dbus_receive_callback(const librmcs::data::UartDataView& data) override { - dr16_.store_status(data.uart_data.data(), data.uart_data.size()); + dr16_.store_status(data.uart_data); } void accelerometer_receive_callback(const librmcs::data::AccelerometerDataView& data) override { @@ -324,6 +331,8 @@ class OmniInfantry device::DjiMotor gimbal_bullet_feeder_; device::Dr16 dr16_; + device::Vt13 vt13_; + device::RemoteControl remote_control_; device::Bmi088 bmi088_; OutputInterface gimbal_yaw_velocity_imu_; diff --git a/rmcs_ws/src/rmcs_core/src/hardware/sentry.cpp b/rmcs_ws/src/rmcs_core/src/hardware/sentry.cpp deleted file mode 100644 index 9d917a2b..00000000 --- a/rmcs_ws/src/rmcs_core/src/hardware/sentry.cpp +++ /dev/null @@ -1,500 +0,0 @@ -#include "hardware/device/bmi088.hpp" -#include "hardware/device/can_packet.hpp" -#include "hardware/device/dji_motor.hpp" -#include "hardware/device/dr16.hpp" -#include "hardware/device/lk_motor.hpp" -#include "hardware/device/supercap.hpp" - -#include - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -namespace rmcs_core::hardware { - -class Sentry - : public rmcs_executor::Component - , public rclcpp::Node { -public: - Sentry() - : Node( - get_component_name(), - rclcpp::NodeOptions().automatically_declare_parameters_from_overrides(true)) { - - register_input("/predefined/timestamp", timestamp_); - register_output("/tf", tf_); - - // For command: remote-status - using Srv = std_srvs::srv::Trigger; - status_service_ = create_service( - "/rmcs/service/robot_status", - [this](const Srv::Request::SharedPtr&, const Srv::Response::SharedPtr& response) { - status_service_callback(response); - }); - - top_board_ = std::make_unique( - *this, *command_component_, get_parameter("board_serial_top_board").as_string()); - - bottom_board_ = std::make_unique( - *this, *command_component_, get_parameter("board_serial_bottom_board").as_string()); - - gimbal_board_ = - std::make_unique(get_parameter("board_serial_gimbal_board").as_string()); - - tf_->set_transform( - Eigen::Translation3d{0.08, 0.0, 0.0}); - tf_->set_transform( - Eigen::Translation3d{0.07128, 0.0, 0.0481}); - } - - auto update() -> void override { - top_board_->update(); - bottom_board_->update(); - gimbal_board_->update(); - tf_->set_transform( - gimbal_board_->imu_pose().conjugate()); - } - -private: - class GimbalBoard final : private librmcs::agent::CBoard { - public: - explicit GimbalBoard(std::string_view board_serial = {}) - : librmcs::agent::CBoard(board_serial) { - bmi088_.set_coordinate_mapping( - [](double x, double y, double z) { return std::make_tuple(y, -x, z); }); - } - - GimbalBoard(const GimbalBoard&) = delete; - GimbalBoard& operator=(const GimbalBoard&) = delete; - GimbalBoard(GimbalBoard&&) = delete; - GimbalBoard& operator=(GimbalBoard&&) = delete; - - ~GimbalBoard() override = default; - - auto update() -> void { - bmi088_.update_status(); - imu_pose_ = Eigen::Quaterniond{bmi088_.q0(), bmi088_.q1(), bmi088_.q2(), bmi088_.q3()}; - } - - auto imu_pose() const -> Eigen::Quaterniond { return imu_pose_; } - - private: - auto accelerometer_receive_callback(const librmcs::data::AccelerometerDataView& data) - -> void override { - bmi088_.store_accelerometer_status(data.x, data.y, data.z); - } - - auto gyroscope_receive_callback(const librmcs::data::GyroscopeDataView& data) - -> void override { - bmi088_.store_gyroscope_status(data.x, data.y, data.z); - } - - device::Bmi088 bmi088_{1000, 0.2, 0.0}; - Eigen::Quaterniond imu_pose_ = Eigen::Quaterniond::Identity(); - }; - - class TopBoard final : private librmcs::agent::RmcsBoardLite { - friend class Sentry; - - public: - explicit TopBoard( - Sentry& sentry, rmcs_executor::Component& sentry_command, - std::string_view board_serial = {}, librmcs::agent::AdvancedOptions options = {}) - : librmcs::agent::RmcsBoardLite(board_serial, options) - , tf_(sentry.tf_) - , bmi088_(1000, 0.2, 0.0) - , gimbal_pitch_motor_(sentry, sentry_command, "/gimbal/pitch") - , gimbal_top_yaw_motor_(sentry, sentry_command, "/gimbal/top_yaw") - , gimbal_bullet_feeder_(sentry, sentry_command, "/gimbal/bullet_feeder") - , gimbal_left_friction_(sentry, sentry_command, "/gimbal/left_friction") - , gimbal_right_friction_(sentry, sentry_command, "/gimbal/right_friction") { - gimbal_pitch_motor_.configure( - device::LkMotor::Config{device::LkMotor::Type::kMG4010Ei10}.set_encoder_zero_point( - static_cast(sentry.get_parameter("pitch_motor_zero_point").as_int()))); - - gimbal_top_yaw_motor_.configure( - device::LkMotor::Config{device::LkMotor::Type::kMG4010Ei10}.set_encoder_zero_point( - static_cast(sentry.get_parameter("top_yaw_motor_zero_point").as_int()))); - - gimbal_bullet_feeder_.configure( - device::DjiMotor::Config{device::DjiMotor::Type::kM3508} - .enable_multi_turn_angle() - .set_reversed() - .set_reduction_ratio(19 * 2)); - - gimbal_left_friction_.configure( - device::DjiMotor::Config{device::DjiMotor::Type::kM3508}.set_reduction_ratio(1.)); - gimbal_right_friction_.configure( - device::DjiMotor::Config{device::DjiMotor::Type::kM3508} - .set_reduction_ratio(1.) - .set_reversed()); - - sentry.register_output("/gimbal/yaw/velocity_imu", gimbal_yaw_velocity_bmi088_); - sentry.register_output("/gimbal/pitch/velocity_imu", gimbal_pitch_velocity_bmi088_); - - bmi088_.set_coordinate_mapping( - [](double x, double y, double z) { return std::make_tuple(-x, -y, z); }); - } - - auto update() -> void { - gimbal_top_yaw_motor_.update_status(); - gimbal_pitch_motor_.update_status(); - - const auto pitch_angle = - std::remainder(gimbal_pitch_motor_.angle(), 2.0 * std::numbers::pi_v); - - bmi088_.update_status(); - const Eigen::Quaterniond gimbal_bmi088_pose{ - bmi088_.q0(), bmi088_.q1(), bmi088_.q2(), bmi088_.q3()}; - - tf_->set_transform( - gimbal_bmi088_pose.conjugate()); - - *gimbal_yaw_velocity_bmi088_ = bmi088_.gz(); - *gimbal_pitch_velocity_bmi088_ = bmi088_.gy(); - - gimbal_bullet_feeder_.update_status(); - gimbal_left_friction_.update_status(); - gimbal_right_friction_.update_status(); - - tf_->set_state( - gimbal_top_yaw_motor_.angle()); - tf_->set_state(pitch_angle); - } - - auto command_update() -> void { - auto builder = start_transmit(); - - builder.can0_transmit({ - .can_id = 0x200, - .can_data = - device::CanPacket8{ - gimbal_right_friction_.generate_command(), - gimbal_left_friction_.generate_command(), - device::CanPacket8::PaddingQuarter{}, - gimbal_bullet_feeder_.generate_command(), - } - .as_bytes(), - }); - - builder.can3_transmit({ - .can_id = 0x141, - .can_data = gimbal_top_yaw_motor_.generate_torque_command().as_bytes(), - }); - - builder.can2_transmit({ - .can_id = 0x141, - .can_data = gimbal_pitch_motor_.generate_torque_command().as_bytes(), - }); - } - - private: - auto can0_receive_callback(const librmcs::data::CanDataView& data) -> void override { - if (data.is_extended_can_id || data.is_remote_transmission) [[unlikely]] - return; - auto can_id = data.can_id; - if (can_id == 0x202) { - gimbal_left_friction_.store_status(data.can_data); - } else if (can_id == 0x201) { - gimbal_right_friction_.store_status(data.can_data); - } else if (can_id == 0x204) { - gimbal_bullet_feeder_.store_status(data.can_data); - } - } - - auto can2_receive_callback(const librmcs::data::CanDataView& data) -> void override { - if (data.is_extended_can_id || data.is_remote_transmission) [[unlikely]] - return; - auto can_id = data.can_id; - if (can_id == 0x141) - gimbal_pitch_motor_.store_status(data.can_data); - } - - auto can3_receive_callback(const librmcs::data::CanDataView& data) -> void override { - if (data.is_extended_can_id || data.is_remote_transmission) [[unlikely]] - return; - auto can_id = data.can_id; - if (can_id == 0x141) - gimbal_top_yaw_motor_.store_status(data.can_data); - } - - auto accelerometer_receive_callback(const librmcs::data::AccelerometerDataView& data) - -> void override { - bmi088_.store_accelerometer_status(data.x, data.y, data.z); - } - - auto gyroscope_receive_callback(const librmcs::data::GyroscopeDataView& data) - -> void override { - bmi088_.store_gyroscope_status(data.x, data.y, data.z); - } - - OutputInterface& tf_; - - OutputInterface gimbal_yaw_velocity_bmi088_; - OutputInterface gimbal_pitch_velocity_bmi088_; - - device::Bmi088 bmi088_; - device::LkMotor gimbal_pitch_motor_; - device::LkMotor gimbal_top_yaw_motor_; - device::DjiMotor gimbal_bullet_feeder_; - - device::DjiMotor gimbal_left_friction_; - device::DjiMotor gimbal_right_friction_; - }; - - class BottomBoard final : private librmcs::agent::CBoard { - friend class Sentry; - - public: - explicit BottomBoard( - Sentry& sentry, rmcs_executor::Component& sentry_command, - std::string_view board_serial = {}) - : librmcs::agent::CBoard(board_serial) - , imu_(1000, 0.2, 0.0) - , tf_(sentry.tf_) - , dr16_(sentry) - , gimbal_bottom_yaw_motor_(sentry, sentry_command, "/gimbal/bottom_yaw") - , chassis_wheel_motors_( - {sentry, sentry_command, "/chassis/left_front_wheel"}, - {sentry, sentry_command, "/chassis/left_back_wheel"}, - {sentry, sentry_command, "/chassis/right_back_wheel"}, - {sentry, sentry_command, "/chassis/right_front_wheel"}) - , chassis_steer_motors_( - {sentry, sentry_command, "/chassis/left_front_steering"}, - {sentry, sentry_command, "/chassis/left_back_steering"}, - {sentry, sentry_command, "/chassis/right_back_steering"}, - {sentry, sentry_command, "/chassis/right_front_steering"}) - , supercap_(sentry, sentry_command) { - sentry.register_output("/referee/serial", referee_serial_); - - referee_serial_->read = [this](std::byte* buffer, size_t size) { - return referee_ring_buffer_receive_.pop_front_n( - [&buffer](std::byte byte) noexcept { *buffer++ = byte; }, size); - }; - referee_serial_->write = [this](const std::byte* buffer, size_t size) { - start_transmit().uart1_transmit( - {.uart_data = std::span{buffer, size}}); - return size; - }; - - const auto zero_point = sentry.get_parameter("bottom_yaw_motor_zero_point").as_int(); - gimbal_bottom_yaw_motor_.configure( - device::LkMotor::Config{device::LkMotor::Type::kMG6012Ei8} - .set_reversed() - .set_encoder_zero_point(static_cast(zero_point))); - - for (auto& motor : chassis_wheel_motors_) { - motor.configure( - device::DjiMotor::Config{device::DjiMotor::Type::kM3508} - .set_reduction_ratio(11.) - .enable_multi_turn_angle() - .set_reversed()); - } - - constexpr auto kSteerNames = std::array{ - "right_back_zero_point", - "right_front_zero_point", - "left_front_zero_point", - "left_back_zero_point", - }; - for (auto&& [motor, name] : std::views::zip(chassis_steer_motors_, kSteerNames)) { - const auto zero_point = sentry.get_parameter(name).as_int(); - motor.configure( - device::DjiMotor::Config{device::DjiMotor::Type::kGM6020} - .set_reversed() - .set_encoder_zero_point(static_cast(zero_point)) - .enable_multi_turn_angle()); - } - - sentry.register_output("/chassis/yaw/velocity_imu", chassis_yaw_velocity_imu_, 0); - } - - auto update() -> void { - imu_.update_status(); - *chassis_yaw_velocity_imu_ = imu_.gz(); - supercap_.update_status(); - - for (auto& motor : chassis_wheel_motors_) - motor.update_status(); - for (auto& motor : chassis_steer_motors_) - motor.update_status(); - - dr16_.update_status(); - gimbal_bottom_yaw_motor_.update_status(); - tf_->set_state( - gimbal_bottom_yaw_motor_.angle()); - } - - auto command_update() -> void { - using namespace device; - - auto builder = start_transmit(); - builder.can1_transmit({ - .can_id = 0x141, - .can_data = gimbal_bottom_yaw_motor_.generate_command().as_bytes(), - }); - - auto cache = CanPacket8{}; - auto generate = [&](std::uint32_t id, std::ranges::range auto& motors, auto... args) { - auto command = [&](T arg) { - if constexpr (std::same_as) { - return arg; - } else { - const auto valid = arg >= 0 && arg < 4; - return valid ? motors[arg].generate_command() - : CanPacket8::PaddingQuarter{}; - } - }; - cache = CanPacket8{command(args)...}; - return librmcs::data::CanDataView{.can_id = id, .can_data = cache.as_bytes()}; - }; - - if (can_transmission_mode_) { - builder.can1_transmit(generate(0x200, chassis_wheel_motors_, 1, 0, -1, -1)) - .can2_transmit(generate(0x200, chassis_wheel_motors_, -1, 2, -1, 3)); - } else { - builder.can1_transmit(generate(0x1FE, chassis_steer_motors_, 1, 0, -1, -1)) - .can2_transmit(generate( - 0x1FE, chassis_steer_motors_, 2, 3, -1, supercap_.generate_command())); - } - can_transmission_mode_ = !can_transmission_mode_; - } - - private: - auto dbus_receive_callback(const librmcs::data::UartDataView& data) -> void override { - dr16_.store_status(data.uart_data.data(), data.uart_data.size()); - } - - auto can1_receive_callback(const librmcs::data::CanDataView& data) -> void override { - if (data.is_extended_can_id || data.is_remote_transmission) [[unlikely]] - return; - auto can_id = data.can_id; - if (can_id == 0x201) - chassis_wheel_motors_[1].store_status(data.can_data); - else if (can_id == 0x202) - chassis_wheel_motors_[0].store_status(data.can_data); - else if (can_id == 0x205) - chassis_steer_motors_[1].store_status(data.can_data); - else if (can_id == 0x206) - chassis_steer_motors_[0].store_status(data.can_data); - else if (can_id == 0x141) - gimbal_bottom_yaw_motor_.store_status(data.can_data); - } - - auto can2_receive_callback(const librmcs::data::CanDataView& data) -> void override { - if (data.is_extended_can_id || data.is_remote_transmission) [[unlikely]] - return; - auto can_id = data.can_id; - if (can_id == 0x202) - chassis_wheel_motors_[2].store_status(data.can_data); - else if (can_id == 0x204) - chassis_wheel_motors_[3].store_status(data.can_data); - else if (can_id == 0x205) - chassis_steer_motors_[2].store_status(data.can_data); - else if (can_id == 0x206) - chassis_steer_motors_[3].store_status(data.can_data); - else if (can_id == 0x300) - supercap_.store_status(data.can_data); - } - - auto uart1_receive_callback(const librmcs::data::UartDataView& data) -> void override { - const auto* uart_data = data.uart_data.data(); - referee_ring_buffer_receive_.emplace_back_n( - [&uart_data](std::byte* storage) noexcept { *storage = *uart_data++; }, - data.uart_data.size()); - } - - auto accelerometer_receive_callback(const librmcs::data::AccelerometerDataView& data) - -> void override { - imu_.store_accelerometer_status(data.x, data.y, data.z); - } - - auto gyroscope_receive_callback(const librmcs::data::GyroscopeDataView& data) - -> void override { - imu_.store_gyroscope_status(data.x, data.y, data.z); - } - - bool can_transmission_mode_ = true; - device::Bmi088 imu_; - OutputInterface& tf_; - - device::Dr16 dr16_; - device::LkMotor gimbal_bottom_yaw_motor_; - device::DjiMotor chassis_wheel_motors_[4]; - device::DjiMotor chassis_steer_motors_[4]; - device::Supercap supercap_; - - rmcs_utility::RingBuffer referee_ring_buffer_receive_{256}; - OutputInterface referee_serial_; - OutputInterface chassis_yaw_velocity_imu_; - }; - - struct CommandTransmitter : public rmcs_executor::Component { - std::function fn; - - template - explicit CommandTransmitter(Fn&& fn) - : fn{std::forward(fn)} {} - - auto update() -> void override { fn(); } - }; - - auto status_service_callback(const std::shared_ptr& response) - -> void { - response->success = true; - - auto feedback_message = std::ostringstream{}; - auto text = [&](std::format_string format, Args&&... args) { - std::println(feedback_message, format, std::forward(args)...); - }; - - text("Gimbal Status"); - text("- Bottom Yaw: {}", bottom_board_->gimbal_bottom_yaw_motor_.last_raw_angle()); - text("- Top Yaw: {}", top_board_->gimbal_top_yaw_motor_.last_raw_angle()); - text("- Pitch Angle: {}", top_board_->gimbal_pitch_motor_.last_raw_angle()); - - text("Chassis Status"); - constexpr auto kPosition = - std::array{"right back", "right front", "left front", "left back"}; - constexpr auto kMaxLength = - std::ranges::max_element(kPosition, {}, &std::string_view::size)->size(); - - for (auto&& [index, motor] : - std::views::zip(kPosition, bottom_board_->chassis_steer_motors_)) { - text("- {:{}}: {}", index, kMaxLength, motor.last_raw_angle()); - } - - response->message = feedback_message.str(); - } - - auto command_update() -> void { - top_board_->command_update(); - bottom_board_->command_update(); - } - std::shared_ptr command_component_{ - create_partner_component( - get_component_name() + "_command", [this] { command_update(); })}; - - InputInterface timestamp_; - OutputInterface tf_; - - std::unique_ptr gimbal_board_; - std::unique_ptr top_board_; - std::unique_ptr bottom_board_; - - std::shared_ptr> status_service_; -}; - -} // namespace rmcs_core::hardware - -#include - -PLUGINLIB_EXPORT_CLASS(rmcs_core::hardware::Sentry, rmcs_executor::Component) diff --git a/rmcs_ws/src/rmcs_core/src/hardware/steering-hero.cpp b/rmcs_ws/src/rmcs_core/src/hardware/steering-hero.cpp deleted file mode 100644 index d30c1ce0..00000000 --- a/rmcs_ws/src/rmcs_core/src/hardware/steering-hero.cpp +++ /dev/null @@ -1,591 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include "hardware/device/benewake.hpp" -#include "hardware/device/bmi088.hpp" -#include "hardware/device/can_packet.hpp" -#include "hardware/device/dji_motor.hpp" -#include "hardware/device/dr16.hpp" -#include "hardware/device/lk_motor.hpp" -#include "hardware/device/supercap.hpp" - -namespace rmcs_core::hardware { - -class SteeringHero - : public rmcs_executor::Component - , public rclcpp::Node { -public: - SteeringHero() - : Node{ - get_component_name(), - rclcpp::NodeOptions{}.automatically_declare_parameters_from_overrides(true)} - , command_component_( - create_partner_component( - get_component_name() + "_command", *this)) { - - register_output("/tf", tf_); - tf_->set_transform( - Eigen::Translation3d{0.16, 0.0, 0.15}); - - gimbal_calibrate_subscription_ = create_subscription( - "/gimbal/calibrate", rclcpp::QoS{0}, [this](std_msgs::msg::Int32::UniquePtr&& msg) { - gimbal_calibrate_subscription_callback(std::move(msg)); - }); - - top_board_ = std::make_unique( - *this, *command_component_, get_parameter("board_serial_top_board").as_string()); - bottom_board_ = std::make_unique( - *this, *command_component_, get_parameter("board_serial_bottom_board").as_string()); - - temperature_logging_timer_.reset(1000); - } - - SteeringHero(const SteeringHero&) = delete; - SteeringHero& operator=(const SteeringHero&) = delete; - SteeringHero(SteeringHero&&) = delete; - SteeringHero& operator=(SteeringHero&&) = delete; - - ~SteeringHero() override = default; - - void update() override { - top_board_->update(); - bottom_board_->update(); - - if (temperature_logging_timer_.tick()) { - temperature_logging_timer_.reset(1000); - RCLCPP_INFO( - get_logger(), - "Temperature: pitch: %.1f, top_yaw: %.1f, bottom_yaw: %.1f, feeder: %.1f", - top_board_->gimbal_pitch_motor_.temperature(), - top_board_->gimbal_top_yaw_motor_.temperature(), - bottom_board_->gimbal_bottom_yaw_motor_.temperature(), - top_board_->gimbal_bullet_feeder_.temperature()); - } - } - - void command_update() { - top_board_->command_update(); - bottom_board_->command_update(); - } - -private: - void gimbal_calibrate_subscription_callback(std_msgs::msg::Int32::UniquePtr) { - RCLCPP_INFO( - get_logger(), "[gimbal calibration] New bottom yaw offset: %ld", - bottom_board_->gimbal_bottom_yaw_motor_.calibrate_zero_point()); - RCLCPP_INFO( - get_logger(), "[gimbal calibration] New pitch offset: %ld", - top_board_->gimbal_pitch_motor_.calibrate_zero_point()); - RCLCPP_INFO( - get_logger(), "[gimbal calibration] New top yaw offset: %ld", - top_board_->gimbal_top_yaw_motor_.calibrate_zero_point()); - RCLCPP_INFO( - get_logger(), "[gimbal calibration] New viewer offset: %ld", - top_board_->gimbal_player_viewer_motor_.calibrate_zero_point()); - RCLCPP_INFO( - get_logger(), "[chassis calibration] left front steering offset: %d", - bottom_board_->chassis_steering_motors_[0].calibrate_zero_point()); - RCLCPP_INFO( - get_logger(), "[chassis calibration] left back steering offset: %d", - bottom_board_->chassis_steering_motors_[1].calibrate_zero_point()); - RCLCPP_INFO( - get_logger(), "[chassis calibration] right back steering offset: %d", - bottom_board_->chassis_steering_motors_[2].calibrate_zero_point()); - RCLCPP_INFO( - get_logger(), "[chassis calibration] right front steering offset: %d", - bottom_board_->chassis_steering_motors_[3].calibrate_zero_point()); - } - - class SteeringHeroCommand : public rmcs_executor::Component { - public: - explicit SteeringHeroCommand(SteeringHero& hero) - : hero_(hero) {} - - void update() override { hero_.command_update(); } - - private: - SteeringHero& hero_; - }; - std::shared_ptr command_component_; - - class TopBoard final : private librmcs::agent::CBoard { - public: - friend class SteeringHero; - explicit TopBoard( - SteeringHero& hero, SteeringHeroCommand& hero_command, - std::string_view board_serial = {}) - : librmcs::agent::CBoard(board_serial) - , tf_(hero.tf_) - , imu_(1000, 0.2, 0.0) - , benewake_(hero, "/gimbal/auto_aim/laser_distance") - , gimbal_top_yaw_motor_( - hero, hero_command, "/gimbal/top_yaw", - device::LkMotor::Config{device::LkMotor::Type::kMG5010Ei10} - .set_encoder_zero_point( - static_cast( - hero.get_parameter("top_yaw_motor_zero_point").as_int()))) - , gimbal_pitch_motor_( - hero, hero_command, "/gimbal/pitch", - device::LkMotor::Config{device::LkMotor::Type::kMG5010Ei10} - .set_encoder_zero_point( - static_cast(hero.get_parameter("pitch_motor_zero_point").as_int()))) - , gimbal_friction_wheels_( - {hero, hero_command, "/gimbal/second_left_friction", - device::DjiMotor::Config{device::DjiMotor::Type::kM3508}.set_reduction_ratio( - 1.)}, - {hero, hero_command, "/gimbal/first_left_friction", - device::DjiMotor::Config{device::DjiMotor::Type::kM3508}.set_reduction_ratio( - 1.)}, - {hero, hero_command, "/gimbal/first_right_friction", - device::DjiMotor::Config{device::DjiMotor::Type::kM3508} - .set_reduction_ratio(1.) - .set_reversed()}, - {hero, hero_command, "/gimbal/second_right_friction", - device::DjiMotor::Config{device::DjiMotor::Type::kM3508} - .set_reduction_ratio(1.) - .set_reversed()}) - , gimbal_bullet_feeder_( - hero, hero_command, "/gimbal/bullet_feeder", - device::LkMotor::Config{device::LkMotor::Type::kMG5010Ei10} - .set_reversed() - .enable_multi_turn_angle()) - , gimbal_scope_motor_( - hero, hero_command, "/gimbal/scope", - device::DjiMotor::Config{device::DjiMotor::Type::kM2006}) - , gimbal_player_viewer_motor_( - hero, hero_command, "/gimbal/player_viewer", - device::LkMotor::Config{device::LkMotor::Type::kMG4005Ei10} - .set_encoder_zero_point( - static_cast(hero.get_parameter("viewer_motor_zero_point").as_int())) - .set_reversed()) { - - imu_.set_coordinate_mapping([](double x, double y, double z) { - // Get the mapping with the following code. - // The rotation angle must be an exact multiple of 90 degrees, otherwise use a - // matrix. - - // Eigen::AngleAxisd pitch_link_to_imu_link{ - // std::numbers::pi, Eigen::Vector3d::UnitZ()}; - // Eigen::Vector3d mapping = pitch_link_to_imu_link * Eigen::Vector3d{1, 2, 3}; - // std::cout << mapping << std::endl; - - return std::make_tuple(-x, -y, z); - }); - - hero.register_output("/gimbal/yaw/velocity_imu", gimbal_yaw_velocity_imu_); - hero.register_output("/gimbal/pitch/velocity_imu", gimbal_pitch_velocity_imu_); - } - - TopBoard(const TopBoard&) = delete; - TopBoard& operator=(const TopBoard&) = delete; - TopBoard(TopBoard&&) = delete; - TopBoard& operator=(TopBoard&&) = delete; - - ~TopBoard() final = default; - - void update() { - imu_.update_status(); - const Eigen::Quaterniond gimbal_imu_pose{imu_.q0(), imu_.q1(), imu_.q2(), imu_.q3()}; - - tf_->set_transform( - gimbal_imu_pose.conjugate()); - - benewake_.update_status(); - - *gimbal_yaw_velocity_imu_ = imu_.gz(); - *gimbal_pitch_velocity_imu_ = imu_.gy(); - - gimbal_top_yaw_motor_.update_status(); - - gimbal_pitch_motor_.update_status(); - tf_->set_state( - gimbal_pitch_motor_.angle()); - - gimbal_player_viewer_motor_.update_status(); - tf_->set_state( - gimbal_player_viewer_motor_.angle()); - - gimbal_scope_motor_.update_status(); - - for (auto& motor : gimbal_friction_wheels_) - motor.update_status(); - - gimbal_bullet_feeder_.update_status(); - } - - void command_update() { - auto builder = start_transmit(); - - builder.can1_transmit({ - .can_id = 0x200, - .can_data = - device::CanPacket8{ - gimbal_friction_wheels_[0].generate_command(), - gimbal_friction_wheels_[1].generate_command(), - gimbal_friction_wheels_[2].generate_command(), - gimbal_friction_wheels_[3].generate_command(), - } - .as_bytes(), - }); - - builder.can1_transmit({ - .can_id = 0x141, - .can_data = gimbal_bullet_feeder_ - .generate_torque_command(gimbal_bullet_feeder_.control_torque()) - .as_bytes(), - }); - - builder.can2_transmit({ - .can_id = 0x200, - .can_data = - device::CanPacket8{ - gimbal_scope_motor_.generate_command(), - device::CanPacket8::PaddingQuarter{}, - device::CanPacket8::PaddingQuarter{}, - device::CanPacket8::PaddingQuarter{}, - } - .as_bytes(), - }); - - builder.can2_transmit({ - .can_id = 0x143, - .can_data = - gimbal_player_viewer_motor_ - .generate_velocity_command(gimbal_player_viewer_motor_.control_velocity()) - .as_bytes(), - }); - - builder.can2_transmit({ - .can_id = 0x141, - .can_data = gimbal_top_yaw_motor_.generate_command().as_bytes(), - }); - - builder.can2_transmit({ - .can_id = 0x142, - .can_data = gimbal_pitch_motor_.generate_command().as_bytes(), - }); - } - - private: - void can1_receive_callback(const librmcs::data::CanDataView& data) override { - if (data.is_extended_can_id || data.is_remote_transmission) [[unlikely]] - return; - - auto can_id = data.can_id; - if (can_id == 0x201) { - gimbal_friction_wheels_[0].store_status(data.can_data); - } else if (can_id == 0x202) { - gimbal_friction_wheels_[1].store_status(data.can_data); - } else if (can_id == 0x203) { - gimbal_friction_wheels_[2].store_status(data.can_data); - } else if (can_id == 0x204) { - gimbal_friction_wheels_[3].store_status(data.can_data); - } else if (can_id == 0x141) { - gimbal_bullet_feeder_.store_status(data.can_data); - } - } - - void can2_receive_callback(const librmcs::data::CanDataView& data) override { - if (data.is_extended_can_id || data.is_remote_transmission) [[unlikely]] - return; - - auto can_id = data.can_id; - if (can_id == 0x141) { - gimbal_top_yaw_motor_.store_status(data.can_data); - } else if (can_id == 0x142) { - gimbal_pitch_motor_.store_status(data.can_data); - } else if (can_id == 0x143) { - gimbal_player_viewer_motor_.store_status(data.can_data); - } else if (can_id == 0x201) { - gimbal_scope_motor_.store_status(data.can_data); - } - } - - void uart2_receive_callback(const librmcs::data::UartDataView& data) override { - benewake_.store_status(data.uart_data.data(), data.uart_data.size()); - } - - void accelerometer_receive_callback( - const librmcs::data::AccelerometerDataView& data) override { - imu_.store_accelerometer_status(data.x, data.y, data.z); - } - - void gyroscope_receive_callback(const librmcs::data::GyroscopeDataView& data) override { - imu_.store_gyroscope_status(data.x, data.y, data.z); - } - - OutputInterface& tf_; - - device::Bmi088 imu_; - device::Benewake benewake_; - - OutputInterface gimbal_yaw_velocity_imu_; - OutputInterface gimbal_pitch_velocity_imu_; - - device::LkMotor gimbal_top_yaw_motor_; - device::LkMotor gimbal_pitch_motor_; - - device::DjiMotor gimbal_friction_wheels_[4]; - device::LkMotor gimbal_bullet_feeder_; - - device::DjiMotor gimbal_scope_motor_; - device::LkMotor gimbal_player_viewer_motor_; - }; - - class BottomBoard final : private librmcs::agent::CBoard { - public: - friend class SteeringHero; - explicit BottomBoard( - SteeringHero& hero, SteeringHeroCommand& hero_command, - std::string_view board_serial = {}) - : librmcs::agent::CBoard(board_serial) - , imu_(1000, 0.2, 0.0) - , tf_(hero.tf_) - , dr16_(hero) - , chassis_steering_motors_( - {hero, hero_command, "/chassis/left_front_steering", - device::DjiMotor::Config{device::DjiMotor::Type::kGM6020} - .set_encoder_zero_point( - static_cast(hero.get_parameter("left_front_zero_point").as_int())) - .set_reversed()}, - {hero, hero_command, "/chassis/left_back_steering", - device::DjiMotor::Config{device::DjiMotor::Type::kGM6020} - .set_encoder_zero_point( - static_cast(hero.get_parameter("left_back_zero_point").as_int())) - .set_reversed()}, - {hero, hero_command, "/chassis/right_back_steering", - device::DjiMotor::Config{device::DjiMotor::Type::kGM6020} - .set_encoder_zero_point( - static_cast(hero.get_parameter("right_back_zero_point").as_int())) - .set_reversed()}, - {hero, hero_command, "/chassis/right_front_steering", - device::DjiMotor::Config{device::DjiMotor::Type::kGM6020} - .set_encoder_zero_point( - static_cast(hero.get_parameter("right_front_zero_point").as_int())) - .set_reversed()}) - , chassis_wheel_motors_( - {hero, hero_command, "/chassis/left_front_wheel", - device::DjiMotor::Config{device::DjiMotor::Type::kM3508} - .set_reversed() - .set_reduction_ratio(2232. / 169.)}, - {hero, hero_command, "/chassis/left_back_wheel", - device::DjiMotor::Config{device::DjiMotor::Type::kM3508} - .set_reversed() - .set_reduction_ratio(2232. / 169.)}, - {hero, hero_command, "/chassis/right_back_wheel", - device::DjiMotor::Config{device::DjiMotor::Type::kM3508} - .set_reversed() - .set_reduction_ratio(2232. / 169.)}, - {hero, hero_command, "/chassis/right_front_wheel", - device::DjiMotor::Config{device::DjiMotor::Type::kM3508} - .set_reversed() - .set_reduction_ratio(2232. / 169.)}) - , supercap_(hero, hero_command) - , gimbal_bottom_yaw_motor_( - hero, hero_command, "/gimbal/bottom_yaw", - device::LkMotor::Config{device::LkMotor::Type::kMG6012Ei8} - .set_reversed() - .set_encoder_zero_point( - static_cast( - hero.get_parameter("bottom_yaw_motor_zero_point").as_int()))) { - - hero.register_output("/referee/serial", referee_serial_); - referee_serial_->read = [this](std::byte* buffer, size_t size) { - return referee_ring_buffer_receive_.pop_front_n( - [&buffer](std::byte byte) noexcept { *buffer++ = byte; }, size); - }; - referee_serial_->write = [this](const std::byte* buffer, size_t size) { - start_transmit().uart1_transmit({ - .uart_data = std::span{buffer, size} - }); - return size; - }; - - hero.register_output("/chassis/yaw/velocity_imu", chassis_yaw_velocity_imu_, 0); - - hero.register_output( - "/chassis/powermeter/control_enable", powermeter_control_enabled_, false); - hero.register_output( - "/chassis/powermeter/charge_power_limit", powermeter_charge_power_limit_, 0.); - } - - BottomBoard(const BottomBoard&) = delete; - BottomBoard& operator=(const BottomBoard&) = delete; - BottomBoard(BottomBoard&&) = delete; - BottomBoard& operator=(BottomBoard&&) = delete; - - ~BottomBoard() final = default; - - void update() { - imu_.update_status(); - dr16_.update_status(); - supercap_.update_status(); - - *chassis_yaw_velocity_imu_ = imu_.gz(); - - for (auto& motor : chassis_wheel_motors_) - motor.update_status(); - for (auto& motor : chassis_steering_motors_) - motor.update_status(); - gimbal_bottom_yaw_motor_.update_status(); - tf_->set_state( - gimbal_bottom_yaw_motor_.angle()); - } - - void command_update() { - auto builder = start_transmit(); - - builder.can1_transmit({ - .can_id = 0x200, - .can_data = - device::CanPacket8{ - chassis_wheel_motors_[0].generate_command(), - chassis_wheel_motors_[1].generate_command(), - chassis_wheel_motors_[2].generate_command(), - chassis_wheel_motors_[3].generate_command(), - } - .as_bytes(), - }); - - builder.can1_transmit({ - .can_id = 0x1FE, - .can_data = - device::CanPacket8{ - device::CanPacket8::PaddingQuarter{}, - device::CanPacket8::PaddingQuarter{}, - device::CanPacket8::PaddingQuarter{}, - supercap_.generate_command(), - } - .as_bytes(), - }); - - builder.can2_transmit({ - .can_id = 0x1FE, - .can_data = - device::CanPacket8{ - chassis_steering_motors_[0].generate_command(), - chassis_steering_motors_[1].generate_command(), - chassis_steering_motors_[2].generate_command(), - chassis_steering_motors_[3].generate_command(), - } - .as_bytes(), - }); - - builder.can2_transmit({ - .can_id = 0x141, - .can_data = gimbal_bottom_yaw_motor_.generate_command().as_bytes(), - }); - } - - private: - void can1_receive_callback(const librmcs::data::CanDataView& data) override { - if (data.is_extended_can_id || data.is_remote_transmission) [[unlikely]] - return; - - auto can_id = data.can_id; - if (can_id == 0x201) { - chassis_wheel_motors_[0].store_status(data.can_data); - } else if (can_id == 0x202) { - chassis_wheel_motors_[1].store_status(data.can_data); - } else if (can_id == 0x203) { - chassis_wheel_motors_[2].store_status(data.can_data); - } else if (can_id == 0x204) { - chassis_wheel_motors_[3].store_status(data.can_data); - } else if (can_id == 0x300) { - supercap_.store_status(data.can_data); - } - } - - void can2_receive_callback(const librmcs::data::CanDataView& data) override { - if (data.is_extended_can_id || data.is_remote_transmission) [[unlikely]] - return; - - auto can_id = data.can_id; - if (can_id == 0x205) { - chassis_steering_motors_[0].store_status(data.can_data); - } else if (can_id == 0x206) { - chassis_steering_motors_[1].store_status(data.can_data); - } else if (can_id == 0x207) { - chassis_steering_motors_[2].store_status(data.can_data); - } else if (can_id == 0x208) { - chassis_steering_motors_[3].store_status(data.can_data); - } else if (can_id == 0x141) { - gimbal_bottom_yaw_motor_.store_status(data.can_data); - } - } - - void uart1_receive_callback(const librmcs::data::UartDataView& data) override { - const auto* uart_data = data.uart_data.data(); - referee_ring_buffer_receive_.emplace_back_n( - [&uart_data](std::byte* storage) noexcept { *storage = *uart_data++; }, - data.uart_data.size()); - } - - void dbus_receive_callback(const librmcs::data::UartDataView& data) override { - dr16_.store_status(data.uart_data.data(), data.uart_data.size()); - } - - void accelerometer_receive_callback( - const librmcs::data::AccelerometerDataView& data) override { - imu_.store_accelerometer_status(data.x, data.y, data.z); - } - - void gyroscope_receive_callback(const librmcs::data::GyroscopeDataView& data) override { - imu_.store_gyroscope_status(data.x, data.y, data.z); - } - - device::Bmi088 imu_; - OutputInterface& tf_; - - OutputInterface chassis_yaw_velocity_imu_; - - OutputInterface powermeter_control_enabled_; - OutputInterface powermeter_charge_power_limit_; - - device::Dr16 dr16_; - - device::DjiMotor chassis_steering_motors_[4]; - device::DjiMotor chassis_wheel_motors_[4]; - device::Supercap supercap_; - - device::LkMotor gimbal_bottom_yaw_motor_; - - rmcs_utility::RingBuffer referee_ring_buffer_receive_{256}; - OutputInterface referee_serial_; - }; - - OutputInterface tf_; - - rclcpp::Subscription::SharedPtr gimbal_calibrate_subscription_; - - std::unique_ptr top_board_; - std::unique_ptr bottom_board_; - - rmcs_utility::TickTimer temperature_logging_timer_; -}; - -} // namespace rmcs_core::hardware - -#include - -PLUGINLIB_EXPORT_CLASS(rmcs_core::hardware::SteeringHero, rmcs_executor::Component) diff --git a/rmcs_ws/src/rmcs_core/src/hardware/steering-infantry.cpp b/rmcs_ws/src/rmcs_core/src/hardware/steering-infantry.cpp deleted file mode 100644 index 2b30039e..00000000 --- a/rmcs_ws/src/rmcs_core/src/hardware/steering-infantry.cpp +++ /dev/null @@ -1,537 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include "hardware/device/bmi088.hpp" -#include "hardware/device/can_packet.hpp" -#include "hardware/device/dji_motor.hpp" -#include "hardware/device/dr16.hpp" -#include "hardware/device/lk_motor.hpp" -#include "hardware/device/supercap.hpp" - -namespace rmcs_core::hardware { - -class SteeringInfantry - : public rmcs_executor::Component - , public rclcpp::Node { -public: - SteeringInfantry() - : Node( - get_component_name(), - rclcpp::NodeOptions().automatically_declare_parameters_from_overrides(true)) - , command_component_( - create_partner_component( - get_component_name() + "_command", *this)) { - register_output("/tf", tf_); - - gimbal_calibrate_subscription_ = create_subscription( - "/gimbal/calibrate", rclcpp::QoS{0}, [this](std_msgs::msg::Int32::UniquePtr&& msg) { - gimbal_calibrate_subscription_callback(std::move(msg)); - }); - steers_calibrate_subscription_ = create_subscription( - "/steers/calibrate", rclcpp::QoS{0}, [this](std_msgs::msg::Int32::UniquePtr&& msg) { - steers_calibrate_subscription_callback(std::move(msg)); - }); - - top_board_ = std::make_unique( - *this, *command_component_, get_parameter("board_serial_top_board").as_string()); - - bottom_board_ = std::make_unique( - *this, *command_component_, get_parameter("board_serial_bottom_board").as_string()); - - tf_->set_transform( - Eigen::Translation3d{0.06603, 0.0, 0.082}); - } - - SteeringInfantry(const SteeringInfantry&) = delete; - SteeringInfantry& operator=(const SteeringInfantry&) = delete; - SteeringInfantry(SteeringInfantry&&) = delete; - SteeringInfantry& operator=(SteeringInfantry&&) = delete; - - ~SteeringInfantry() override = default; - - void update() override { - top_board_->update(); - bottom_board_->update(); - } - - void command_update() { - - top_board_->command_update(); - bottom_board_->command_update(); - } - -private: - void gimbal_calibrate_subscription_callback(std_msgs::msg::Int32::UniquePtr) { - RCLCPP_INFO( - get_logger(), "[gimbal calibration] New yaw offset: %ld", - bottom_board_->gimbal_yaw_motor_.calibrate_zero_point()); - RCLCPP_INFO( - get_logger(), "[gimbal calibration] New pitch offset: %ld", - top_board_->gimbal_pitch_motor_.calibrate_zero_point()); - } - - void steers_calibrate_subscription_callback(std_msgs::msg::Int32::UniquePtr) { - RCLCPP_INFO( - get_logger(), "[steer calibration] New left front offset: %d", - bottom_board_->chassis_steer_motors_[0].calibrate_zero_point()); - RCLCPP_INFO( - get_logger(), "[steer calibration] New left back offset: %d", - bottom_board_->chassis_steer_motors_[1].calibrate_zero_point()); - RCLCPP_INFO( - get_logger(), "[steer calibration] New right back offset: %d", - bottom_board_->chassis_steer_motors_[2].calibrate_zero_point()); - RCLCPP_INFO( - get_logger(), "[steer calibration] New right front offset: %d", - bottom_board_->chassis_steer_motors_[3].calibrate_zero_point()); - } - class SteeringInfantryCommand : public rmcs_executor::Component { - public: - explicit SteeringInfantryCommand(SteeringInfantry& steering_infantry) - : steering_infantry(steering_infantry) {} - - void update() override { steering_infantry.command_update(); } - - SteeringInfantry& steering_infantry; - }; - - class TopBoard final : private librmcs::agent::CBoard { - public: - friend class SteeringInfantry; - explicit TopBoard( - SteeringInfantry& steering_infantry, SteeringInfantryCommand& steering_infantry_command, - std::string_view board_serial = {}) - : librmcs::agent::CBoard(board_serial) - , tf_(steering_infantry.tf_) - , bmi088_(1000, 0.2, 0.0) - , gimbal_pitch_motor_(steering_infantry, steering_infantry_command, "/gimbal/pitch") - , gimbal_left_friction_( - steering_infantry, steering_infantry_command, "/gimbal/left_friction") - , gimbal_right_friction_( - steering_infantry, steering_infantry_command, "/gimbal/right_friction") { - gimbal_pitch_motor_.configure( - device::LkMotor::Config{device::LkMotor::Type::kMG4010Ei10}.set_encoder_zero_point( - static_cast( - steering_infantry.get_parameter("pitch_motor_zero_point").as_int()))); - - gimbal_left_friction_.configure( - device::DjiMotor::Config{device::DjiMotor::Type::kM3508}.set_reduction_ratio(1.)); - gimbal_right_friction_.configure( - device::DjiMotor::Config{device::DjiMotor::Type::kM3508} - .set_reduction_ratio(1.) - .set_reversed()); - - steering_infantry.register_output( - "/gimbal/yaw/velocity_imu", gimbal_yaw_velocity_bmi088_); - steering_infantry.register_output( - "/gimbal/pitch/velocity_imu", gimbal_pitch_velocity_bmi088_); - - bmi088_.set_coordinate_mapping([](double x, double y, double z) { - // Get the mapping with the following code. - // The rotation angle must be an exact multiple of 90 degrees, otherwise - // use a matrix. - - // Eigen::AngleAxisd pitch_link_to_bmi088_link{ - // std::numbers::pi / 2, Eigen::Vector3d::UnitZ()}; - // Eigen::Vector3d mapping = pitch_link_to_bmi088_link * Eigen::Vector3d{1, 2, 3}; - // std::cout << mapping << std::endl; - - return std::make_tuple(x, y, z); - }); - } - - TopBoard(const TopBoard&) = delete; - TopBoard& operator=(const TopBoard&) = delete; - TopBoard(TopBoard&&) = delete; - TopBoard& operator=(TopBoard&&) = delete; - - ~TopBoard() override = default; - - void update() { - bmi088_.update_status(); - const Eigen::Quaterniond gimbal_bmi088_pose{ - bmi088_.q0(), bmi088_.q1(), bmi088_.q2(), bmi088_.q3()}; - - tf_->set_transform( - gimbal_bmi088_pose.conjugate()); - - *gimbal_yaw_velocity_bmi088_ = bmi088_.gz(); - *gimbal_pitch_velocity_bmi088_ = bmi088_.gy(); - - gimbal_pitch_motor_.update_status(); - gimbal_left_friction_.update_status(); - gimbal_right_friction_.update_status(); - - tf_->set_state( - gimbal_pitch_motor_.angle()); - } - - void command_update() { - auto builder = start_transmit(); - - builder.can1_transmit({ - .can_id = 0x200, - .can_data = - device::CanPacket8{ - device::CanPacket8::PaddingQuarter{}, - device::CanPacket8::PaddingQuarter{}, - gimbal_left_friction_.generate_command(), - gimbal_right_friction_.generate_command(), - } - .as_bytes(), - }); - - builder.can2_transmit({ - .can_id = 0x142, - .can_data = gimbal_pitch_motor_ - .generate_velocity_command(gimbal_pitch_motor_.control_velocity()) - .as_bytes(), - }); - } - - private: - void can1_receive_callback(const librmcs::data::CanDataView& data) override { - if (data.is_extended_can_id || data.is_remote_transmission) [[unlikely]] - return; - auto can_id = data.can_id; - if (can_id == 0x203) { - gimbal_left_friction_.store_status(data.can_data); - } else if (can_id == 0x204) { - gimbal_right_friction_.store_status(data.can_data); - } - } - void can2_receive_callback(const librmcs::data::CanDataView& data) override { - if (data.is_extended_can_id || data.is_remote_transmission) [[unlikely]] - return; - auto can_id = data.can_id; - if (can_id == 0x142) - gimbal_pitch_motor_.store_status(data.can_data); - } - void accelerometer_receive_callback( - const librmcs::data::AccelerometerDataView& data) override { - bmi088_.store_accelerometer_status(data.x, data.y, data.z); - } - void gyroscope_receive_callback(const librmcs::data::GyroscopeDataView& data) override { - bmi088_.store_gyroscope_status(data.x, data.y, data.z); - } - OutputInterface& tf_; - - OutputInterface gimbal_yaw_velocity_bmi088_; - OutputInterface gimbal_pitch_velocity_bmi088_; - - device::Bmi088 bmi088_; - device::LkMotor gimbal_pitch_motor_; - device::DjiMotor gimbal_left_friction_; - device::DjiMotor gimbal_right_friction_; - }; - - class BottomBoard final : private librmcs::agent::CBoard { - public: - friend class SteeringInfantry; - - explicit BottomBoard( - SteeringInfantry& steering_infantry, SteeringInfantryCommand& steering_infantry_command, - std::string_view board_serial = {}) - : librmcs::agent::CBoard(board_serial) - , imu_(1000, 0.2, 0.0) - , tf_(steering_infantry.tf_) - , dr16_(steering_infantry) - , gimbal_yaw_motor_(steering_infantry, steering_infantry_command, "/gimbal/yaw") - , gimbal_bullet_feeder_( - steering_infantry, steering_infantry_command, "/gimbal/bullet_feeder") - , chassis_wheel_motors_( - {steering_infantry, steering_infantry_command, "/chassis/left_front_wheel"}, - {steering_infantry, steering_infantry_command, "/chassis/left_back_wheel"}, - {steering_infantry, steering_infantry_command, "/chassis/right_back_wheel"}, - {steering_infantry, steering_infantry_command, "/chassis/right_front_wheel"}) - , chassis_steer_motors_( - {steering_infantry, steering_infantry_command, "/chassis/left_front_steering"}, - {steering_infantry, steering_infantry_command, "/chassis/left_back_steering"}, - {steering_infantry, steering_infantry_command, "/chassis/right_back_steering"}, - {steering_infantry, steering_infantry_command, "/chassis/right_front_steering"}) - , supercap_(steering_infantry, steering_infantry_command) { - - steering_infantry.register_output("/referee/serial", referee_serial_); - - referee_serial_->read = [this](std::byte* buffer, size_t size) { - return referee_ring_buffer_receive_.pop_front_n( - [&buffer](std::byte byte) noexcept { *buffer++ = byte; }, size); - }; - referee_serial_->write = [this](const std::byte* buffer, size_t size) { - start_transmit().uart1_transmit({ - .uart_data = std::span{buffer, size} - }); - return size; - }; - gimbal_yaw_motor_.configure( - device::LkMotor::Config{device::LkMotor::Type::kMG4010Ei10}.set_encoder_zero_point( - static_cast( - steering_infantry.get_parameter("yaw_motor_zero_point").as_int()))); - - gimbal_bullet_feeder_.configure( - device::DjiMotor::Config{device::DjiMotor::Type::kM2006} - .enable_multi_turn_angle() - .set_reversed() - .set_reduction_ratio(19 * 2)); - - for (auto& motor : chassis_wheel_motors_) - motor.configure( - device::DjiMotor::Config{device::DjiMotor::Type::kM3508} - - .set_reduction_ratio(11.) - .enable_multi_turn_angle() - .set_reversed()); - chassis_steer_motors_[0].configure( - device::DjiMotor::Config{device::DjiMotor::Type::kGM6020} - .set_reversed() - .set_encoder_zero_point( - static_cast( - steering_infantry.get_parameter("left_front_zero_point").as_int())) - .enable_multi_turn_angle()); - chassis_steer_motors_[1].configure( - device::DjiMotor::Config{device::DjiMotor::Type::kGM6020} - .set_reversed() - .set_encoder_zero_point( - static_cast( - steering_infantry.get_parameter("left_back_zero_point").as_int())) - .enable_multi_turn_angle()); - chassis_steer_motors_[2].configure( - device::DjiMotor::Config{device::DjiMotor::Type::kGM6020} - .set_reversed() - .set_encoder_zero_point( - static_cast( - steering_infantry.get_parameter("right_back_zero_point").as_int())) - .enable_multi_turn_angle()); - chassis_steer_motors_[3].configure( - device::DjiMotor::Config{device::DjiMotor::Type::kGM6020} - .set_reversed() - .set_encoder_zero_point( - static_cast( - steering_infantry.get_parameter("right_front_zero_point").as_int())) - .enable_multi_turn_angle()); - steering_infantry.register_output( - "/chassis/yaw/velocity_imu", chassis_yaw_velocity_imu_, 0); - } - - BottomBoard(const BottomBoard&) = delete; - BottomBoard& operator=(const BottomBoard&) = delete; - BottomBoard(BottomBoard&&) = delete; - BottomBoard& operator=(BottomBoard&&) = delete; - - ~BottomBoard() override = default; - - void update() { - imu_.update_status(); - *chassis_yaw_velocity_imu_ = imu_.gy(); - supercap_.update_status(); - - for (auto& motor : chassis_wheel_motors_) - motor.update_status(); - for (auto& motor : chassis_steer_motors_) - motor.update_status(); - - dr16_.update_status(); - gimbal_yaw_motor_.update_status(); - tf_->set_state( - gimbal_yaw_motor_.angle()); - - gimbal_bullet_feeder_.update_status(); - } - - void command_update() { - if (can_transmission_mode_) { - auto builder = start_transmit(); - - builder.can1_transmit({ - .can_id = 0x200, - .can_data = - device::CanPacket8{ - chassis_wheel_motors_[0].generate_command(), - chassis_wheel_motors_[1].generate_command(), - chassis_wheel_motors_[2].generate_command(), - chassis_wheel_motors_[3].generate_command(), - } - .as_bytes(), - }); - - builder.can1_transmit({ - .can_id = 0x1FF, - .can_data = - device::CanPacket8{ - gimbal_bullet_feeder_.generate_command(), - device::CanPacket8::PaddingQuarter{}, - device::CanPacket8::PaddingQuarter{}, - device::CanPacket8::PaddingQuarter{}, - } - .as_bytes(), - }); - - builder.can1_transmit({ - .can_id = 0x1FE, - .can_data = - device::CanPacket8{ - device::CanPacket8::PaddingQuarter{}, - device::CanPacket8::PaddingQuarter{}, - device::CanPacket8::PaddingQuarter{}, - supercap_.generate_command(), - } - .as_bytes(), - }); - - auto steer_builder = start_transmit(); - steer_builder.can2_transmit({ - .can_id = 0x1FE, - .can_data = - device::CanPacket8{ - chassis_steer_motors_[0].generate_command(), - chassis_steer_motors_[1].generate_command(), - chassis_steer_motors_[2].generate_command(), - chassis_steer_motors_[3].generate_command(), - } - .as_bytes(), - }); - } else { - auto builder = start_transmit(); - - builder.can1_transmit({ - .can_id = 0x200, - .can_data = - device::CanPacket8{ - chassis_wheel_motors_[0].generate_command(), - chassis_wheel_motors_[1].generate_command(), - chassis_wheel_motors_[2].generate_command(), - chassis_wheel_motors_[3].generate_command(), - } - .as_bytes(), - }); - - builder.can1_transmit({ - .can_id = 0x1FF, - .can_data = - device::CanPacket8{ - gimbal_bullet_feeder_.generate_command(), - device::CanPacket8::PaddingQuarter{}, - device::CanPacket8::PaddingQuarter{}, - device::CanPacket8::PaddingQuarter{}, - } - .as_bytes(), - }); - - builder.can2_transmit({ - .can_id = 0x142, - .can_data = gimbal_yaw_motor_ - .generate_velocity_command( - gimbal_yaw_motor_.control_velocity() - imu_.gy()) - .as_bytes(), - }); - } - can_transmission_mode_ = !can_transmission_mode_; - } - - private: - void dbus_receive_callback(const librmcs::data::UartDataView& data) override { - dr16_.store_status(data.uart_data.data(), data.uart_data.size()); - } - - void can1_receive_callback(const librmcs::data::CanDataView& data) override { - if (data.is_extended_can_id || data.is_remote_transmission) [[unlikely]] - return; - auto can_id = data.can_id; - if (can_id == 0x201) - chassis_wheel_motors_[0].store_status(data.can_data); - else if (can_id == 0x202) - chassis_wheel_motors_[1].store_status(data.can_data); - else if (can_id == 0x203) - chassis_wheel_motors_[2].store_status(data.can_data); - else if (can_id == 0x204) - chassis_wheel_motors_[3].store_status(data.can_data); - else if (can_id == 0x205) - gimbal_bullet_feeder_.store_status(data.can_data); - else if (can_id == 0x300) - supercap_.store_status(data.can_data); - } - - void can2_receive_callback(const librmcs::data::CanDataView& data) override { - if (data.is_extended_can_id || data.is_remote_transmission) [[unlikely]] - return; - auto can_id = data.can_id; - if (can_id == 0x142) - gimbal_yaw_motor_.store_status(data.can_data); - else if (can_id == 0x205) - chassis_steer_motors_[0].store_status(data.can_data); - else if (can_id == 0x206) - chassis_steer_motors_[1].store_status(data.can_data); - else if (can_id == 0x207) - chassis_steer_motors_[2].store_status(data.can_data); - else if (can_id == 0x208) - chassis_steer_motors_[3].store_status(data.can_data); - } - - void uart1_receive_callback(const librmcs::data::UartDataView& data) override { - const auto* uart_data = data.uart_data.data(); - referee_ring_buffer_receive_.emplace_back_n( - [&uart_data](std::byte* storage) noexcept { *storage = *uart_data++; }, - data.uart_data.size()); - } - - void accelerometer_receive_callback( - const librmcs::data::AccelerometerDataView& data) override { - imu_.store_accelerometer_status(data.x, data.y, data.z); - } - - void gyroscope_receive_callback(const librmcs::data::GyroscopeDataView& data) override { - imu_.store_gyroscope_status(data.x, data.y, data.z); - } - - bool can_transmission_mode_ = true; - device::Bmi088 imu_; - OutputInterface& tf_; - OutputInterface powermeter_control_enabled_; - OutputInterface powermeter_charge_power_limit_; - - device::Dr16 dr16_; - device::LkMotor gimbal_yaw_motor_; - device::DjiMotor gimbal_bullet_feeder_; - device::DjiMotor chassis_wheel_motors_[4]; - device::DjiMotor chassis_steer_motors_[4]; - device::Supercap supercap_; - - rmcs_utility::RingBuffer referee_ring_buffer_receive_{256}; - OutputInterface referee_serial_; - OutputInterface chassis_yaw_velocity_imu_; - }; - - OutputInterface tf_; - - std::shared_ptr command_component_; - std::shared_ptr top_board_; - std::shared_ptr bottom_board_; - - rclcpp::Subscription::SharedPtr gimbal_calibrate_subscription_; - rclcpp::Subscription::SharedPtr steers_calibrate_subscription_; -}; - -} // namespace rmcs_core::hardware - -#include - -PLUGINLIB_EXPORT_CLASS(rmcs_core::hardware::SteeringInfantry, rmcs_executor::Component) diff --git a/rmcs_ws/src/rmcs_utility/include/rmcs_utility/ring_buffer.hpp b/rmcs_ws/src/rmcs_utility/include/rmcs_utility/ring_buffer.hpp index 2c829c64..82a93fba 100644 --- a/rmcs_ws/src/rmcs_utility/include/rmcs_utility/ring_buffer.hpp +++ b/rmcs_ws/src/rmcs_utility/include/rmcs_utility/ring_buffer.hpp @@ -90,6 +90,43 @@ class RingBuffer { return std::launder(reinterpret_cast(storage_[out & mask_].data)); } + /*! + * @brief Visit elements from the head without consuming them + * @tparam F Functor with signature `void(T&)` + * @param callback_functor Invoked for each readable element in FIFO order + * @param count Maximum number of elements to inspect (defaults to all available) + * @return Number of elements actually visited + * @note Consumer-only. Does not advance `out_` or destroy elements. + * Mutations performed through the callback are applied in place to the + * buffered elements. + */ + template + requires requires(F& f, T& t) { + { f(t) } noexcept; + } size_t peek_front_n(F callback_functor, size_t count = std::numeric_limits::max()) { + const auto in = in_.load(std::memory_order::acquire); + const auto out = out_.load(std::memory_order::relaxed); + + const auto readable = in - out; + count = std::min(count, readable); + if (!count) + return 0; + + const auto offset = out & mask_; + const auto slice = std::min(count, max_size() - offset); + + auto process = [&callback_functor](std::byte* storage) { + auto& element = *std::launder(reinterpret_cast(storage)); + callback_functor(element); + }; + for (size_t i = 0; i < slice; i++) + process(storage_[offset + i].data); + for (size_t i = 0; i < count - slice; i++) + process(storage_[i].data); + + return count; + } + /*! * @brief Peek the last produced element (consumer side) * @return Pointer to the last element, or nullptr if empty From e241d44764194e34ed512f84df700311a2cf4523 Mon Sep 17 00:00:00 2001 From: qzhhhi Date: Thu, 21 May 2026 02:24:32 +0800 Subject: [PATCH 24/52] add joystick --- .../src/hardware/device/remote_control.hpp | 15 ++++++++----- .../rmcs_core/src/hardware/device/vt13.hpp | 22 +++++++++++++++++++ 2 files changed, 31 insertions(+), 6 deletions(-) diff --git a/rmcs_ws/src/rmcs_core/src/hardware/device/remote_control.hpp b/rmcs_ws/src/rmcs_core/src/hardware/device/remote_control.hpp index 99b36c3b..34eaeec2 100644 --- a/rmcs_ws/src/rmcs_core/src/hardware/device/remote_control.hpp +++ b/rmcs_ws/src/rmcs_core/src/hardware/device/remote_control.hpp @@ -42,9 +42,6 @@ class RemoteControl { } void update() { - *joystick_right_output_ = dr16_.joystick_right(); - *joystick_left_output_ = dr16_.joystick_left(); - if (vt13_.mode_switch() == Vt13::ModeSwitch::kCine) { *switch_right_output_ = rmcs_msgs::Switch::DOWN; *switch_left_output_ = rmcs_msgs::Switch::DOWN; @@ -53,22 +50,28 @@ class RemoteControl { *switch_left_output_ = dr16_.switch_left(); } - *rotary_knob_output_ = dr16_.rotary_knob(); - *rotary_knob_switch_output_ = dr16_.rotary_knob_switch(); - if (vt13_.mode_switch() == Vt13::ModeSwitch::kSport) { + *joystick_right_output_ = vt13_.joystick_right(); + *joystick_left_output_ = vt13_.joystick_left(); + *mouse_velocity_output_ = vt13_.mouse_velocity(); *mouse_wheel_output_ = vt13_.mouse_wheel(); *mouse_output_ = vt13_.mouse(); *keyboard_output_ = vt13_.keyboard(); } else { + *joystick_right_output_ = dr16_.joystick_right(); + *joystick_left_output_ = dr16_.joystick_left(); + *mouse_velocity_output_ = dr16_.mouse_velocity(); *mouse_wheel_output_ = dr16_.mouse_wheel(); *mouse_output_ = dr16_.mouse(); *keyboard_output_ = dr16_.keyboard(); } + + *rotary_knob_output_ = dr16_.rotary_knob(); + *rotary_knob_switch_output_ = dr16_.rotary_knob_switch(); } private: diff --git a/rmcs_ws/src/rmcs_core/src/hardware/device/vt13.hpp b/rmcs_ws/src/rmcs_core/src/hardware/device/vt13.hpp index 6cf34d9e..b8dae5ce 100644 --- a/rmcs_ws/src/rmcs_core/src/hardware/device/vt13.hpp +++ b/rmcs_ws/src/rmcs_core/src/hardware/device/vt13.hpp @@ -72,6 +72,9 @@ class Vt13 { [[nodiscard]] ModeSwitch mode_switch() const noexcept { return mode_switch_; } + [[nodiscard]] const Eigen::Vector2d& joystick_left() const noexcept { return joystick_left_; } + [[nodiscard]] const Eigen::Vector2d& joystick_right() const noexcept { return joystick_right_; } + [[nodiscard]] const Eigen::Vector2d& mouse_velocity() const noexcept { return mouse_velocity_; } [[nodiscard]] double mouse_wheel() const noexcept { return mouse_wheel_; } @@ -148,6 +151,15 @@ class Vt13 { void update_remote_control_data(const RemoteControlData& data) { mode_switch_ = static_cast(data.mode_switch + 1); + joystick_right_ = { + channel_to_double(static_cast(data.joystick_channel1)), + -channel_to_double(static_cast(data.joystick_channel0)), + }; + joystick_left_ = { + channel_to_double(static_cast(data.joystick_channel3)), + -channel_to_double(static_cast(data.joystick_channel2)), + }; + mouse_velocity_ = { -data.mouse_velocity_y / 32768.0, -data.mouse_velocity_x / 32768.0, @@ -184,10 +196,20 @@ class Vt13 { return Success{total_frame_size}; } + static double channel_to_double(int32_t value) { + value -= 1024; + if (-660 <= value && value <= 660) + return value / 660.0; + return 0.0; + } + rmcs_utility::RingBuffer data_buffer_{1024}; ModeSwitch mode_switch_ = ModeSwitch::kUnknown; + Eigen::Vector2d joystick_left_ = Eigen::Vector2d::Zero(); + Eigen::Vector2d joystick_right_ = Eigen::Vector2d::Zero(); + Eigen::Vector2d mouse_velocity_ = Eigen::Vector2d::Zero(); double mouse_wheel_ = 0; From 0a63de81e95ed1fb2daafe739d844ce1808e8960 Mon Sep 17 00:00:00 2001 From: ZGZ713912 Date: Thu, 21 May 2026 04:01:55 +0800 Subject: [PATCH 25/52] feat: integrate Vt13 and remote control into deformable infantry components --- .../hardware/deformable-infantry-omni-b.cpp | 22 +++++++++++++--- .../src/hardware/deformable-infantry-omni.cpp | 25 +++++++++++++++---- .../hardware/deformable-infantry-steering.cpp | 4 +-- 3 files changed, 40 insertions(+), 11 deletions(-) diff --git a/rmcs_ws/src/rmcs_core/src/hardware/deformable-infantry-omni-b.cpp b/rmcs_ws/src/rmcs_core/src/hardware/deformable-infantry-omni-b.cpp index 269083d8..865e7896 100644 --- a/rmcs_ws/src/rmcs_core/src/hardware/deformable-infantry-omni-b.cpp +++ b/rmcs_ws/src/rmcs_core/src/hardware/deformable-infantry-omni-b.cpp @@ -29,7 +29,9 @@ #include "hardware/device/dji_motor.hpp" #include "hardware/device/dr16.hpp" #include "hardware/device/lk_motor.hpp" +#include "hardware/device/remote_control.hpp" #include "hardware/device/supercap.hpp" +#include "hardware/device/vt13.hpp" namespace rmcs_core::hardware { @@ -71,7 +73,8 @@ class DeformableInfantryOmniB *this, *deformable_infantry_command_, get_parameter("serial_filter_top_board").as_string(), !serial_filter_imu.empty()); if (!serial_filter_imu.empty()) - imu_board_ = std::make_unique(*this, serial_filter_imu); + imu_board_ = std::make_unique(*this, vt13_, serial_filter_imu); + remote_control_ = std::make_unique(*this, rmcs_board_lite->dr16_, vt13_); } ~DeformableInfantryOmniB() override = default; @@ -86,6 +89,8 @@ class DeformableInfantryOmniB top_board_->update(); if (imu_board_) imu_board_->update(); + vt13_.update_status(); + remote_control_->update(); } void command_update() { @@ -469,7 +474,7 @@ class DeformableInfantryOmniB } void dbus_receive_callback(const librmcs::data::UartDataView& data) override { - dr16_.store_status(data.uart_data.data(), data.uart_data.size()); + dr16_.store_status(data.uart_data); } void can0_receive_callback(const librmcs::data::CanDataView& data) override { @@ -550,7 +555,7 @@ class DeformableInfantryOmniB device::Bmi088 imu_{1000, 0.2, 0.0}; device::LkMotor gimbal_yaw_motor_{ deformable_infantry_, command_, "/gimbal/yaw"}; - device::Dr16 dr16_{deformable_infantry_}; + device::Dr16 dr16_; device::DjiMotor chassis_wheel_motors_[4]{ device::DjiMotor{ @@ -615,11 +620,13 @@ class DeformableInfantryOmniB public: explicit ImuBoard( - DeformableInfantryOmniB& deformableInfantry, const std::string& serial_filter = {}) + DeformableInfantryOmniB& deformableInfantry, device::Vt13& vt13, + const std::string& serial_filter = {}) : RmcsBoardLite{ serial_filter, librmcs::agent::AdvancedOptions{.dangerously_skip_version_checks = true}} , tf_{deformableInfantry.tf_} + , vt13_{vt13} , bmi088_{1000, 0.2, 0.0} { deformableInfantry.register_output( @@ -643,6 +650,10 @@ class DeformableInfantryOmniB } private: + void uart0_receive_callback(const librmcs::data::UartDataView& data) override { + vt13_.store_status(data.uart_data); + } + void accelerometer_receive_callback( const librmcs::data::AccelerometerDataView& data) override { bmi088_.store_accelerometer_status(data.x, data.y, data.z); @@ -654,6 +665,7 @@ class DeformableInfantryOmniB OutputInterface& tf_; OutputInterface gimbal_pitch_velocity_imu_; + device::Vt13& vt13_; device::Bmi088 bmi088_; }; @@ -837,6 +849,7 @@ class DeformableInfantryOmniB OutputInterface tf_; InputInterface timestamp_; + device::Vt13 vt13_; std::atomic hard_sync_pending_{false}; size_t hard_sync_snapshot_count_ = 0; Clock::time_point next_hard_sync_log_time_{}; @@ -845,6 +858,7 @@ class DeformableInfantryOmniB std::unique_ptr rmcs_board_lite; std::unique_ptr imu_board_; std::unique_ptr top_board_; + std::unique_ptr remote_control_; std::shared_ptr> status_service_; uint32_t cmd_tick_ = 0; diff --git a/rmcs_ws/src/rmcs_core/src/hardware/deformable-infantry-omni.cpp b/rmcs_ws/src/rmcs_core/src/hardware/deformable-infantry-omni.cpp index 0338686a..28870de0 100644 --- a/rmcs_ws/src/rmcs_core/src/hardware/deformable-infantry-omni.cpp +++ b/rmcs_ws/src/rmcs_core/src/hardware/deformable-infantry-omni.cpp @@ -3,7 +3,9 @@ #include "hardware/device/dji_motor.hpp" #include "hardware/device/dr16.hpp" #include "hardware/device/lk_motor.hpp" +#include "hardware/device/remote_control.hpp" #include "hardware/device/supercap.hpp" +#include "hardware/device/vt13.hpp" #include #include @@ -53,8 +55,9 @@ class DeformableInfantryOmni *this, *command_, get_parameter("serial_filter_rmcs_board").as_string()); top_board_ = std::make_unique( *this, *command_, get_parameter("serial_filter_top_board").as_string(), true); - imu_board_ = - std::make_unique(*this, get_parameter("serial_filter_imu").as_string()); + imu_board_ = std::make_unique( + *this, vt13_, get_parameter("serial_filter_imu").as_string()); + remote_control_ = std::make_unique(*this, bottom_board_->dr16_, vt13_); // For command: remote-status using Srv = std_srvs::srv::Trigger; @@ -73,6 +76,8 @@ class DeformableInfantryOmni bottom_board_->update(); top_board_->update(); imu_board_->update(); + vt13_.update_status(); + remote_control_->update(); } void command_update() { @@ -329,7 +334,7 @@ class DeformableInfantryOmni device::Bmi088 imu_{1000, 0.2, 0.0}; device::LkMotor gimbal_yaw_motor_{status_, command_, "/gimbal/yaw"}; - device::Dr16 dr16_{status_}; + device::Dr16 dr16_; device::DjiMotor chassis_wheel_motors_[4]{ device::DjiMotor{status_, command_, "/chassis/left_front_wheel"}, @@ -493,7 +498,7 @@ class DeformableInfantryOmni } void dbus_receive_callback(const librmcs::data::UartDataView& data) override { - dr16_.store_status(data.uart_data.data(), data.uart_data.size()); + dr16_.store_status(data.uart_data); } void can0_receive_callback(const librmcs::data::CanDataView& data) override { @@ -574,11 +579,14 @@ class DeformableInfantryOmni friend class DeformableInfantryOmni; public: - explicit ImuBoard(DeformableInfantryOmni& status, const std::string& serial_filter = {}) + explicit ImuBoard( + DeformableInfantryOmni& status, device::Vt13& vt13, + const std::string& serial_filter = {}) : RmcsBoardLite{ serial_filter, librmcs::agent::AdvancedOptions{.dangerously_skip_version_checks = true}} , tf_{status.tf_} + , vt13_{vt13} , bmi088_{1000, 0.2, 0.0} { status.register_output("/gimbal/pitch/velocity_imu", gimbal_pitch_velocity_imu_); @@ -601,6 +609,10 @@ class DeformableInfantryOmni } private: + void uart0_receive_callback(const librmcs::data::UartDataView& data) override { + vt13_.store_status(data.uart_data); + } + void accelerometer_receive_callback( const librmcs::data::AccelerometerDataView& data) override { bmi088_.store_accelerometer_status(data.x, data.y, data.z); @@ -612,6 +624,7 @@ class DeformableInfantryOmni OutputInterface& tf_; OutputInterface gimbal_pitch_velocity_imu_; + device::Vt13& vt13_; device::Bmi088 bmi088_; }; @@ -825,10 +838,12 @@ class DeformableInfantryOmni OutputInterface tf_; InputInterface timestamp_; + device::Vt13 vt13_; std::unique_ptr imu_board_; std::unique_ptr top_board_; std::unique_ptr bottom_board_; + std::unique_ptr remote_control_; std::shared_ptr command_; uint32_t cmd_tick_ = 0; diff --git a/rmcs_ws/src/rmcs_core/src/hardware/deformable-infantry-steering.cpp b/rmcs_ws/src/rmcs_core/src/hardware/deformable-infantry-steering.cpp index 5a833272..c4cace8e 100644 --- a/rmcs_ws/src/rmcs_core/src/hardware/deformable-infantry-steering.cpp +++ b/rmcs_ws/src/rmcs_core/src/hardware/deformable-infantry-steering.cpp @@ -523,7 +523,7 @@ class DeformableInfantryV2 } void dbus_receive_callback(const librmcs::data::UartDataView& data) override { - dr16_.store_status(data.uart_data.data(), data.uart_data.size()); + dr16_.store_status(data.uart_data); } void can0_receive_callback(const librmcs::data::CanDataView& data) override { @@ -610,7 +610,7 @@ class DeformableInfantryV2 device::Bmi088 imu_{1000, 0.2, 0.0}; device::LkMotor gimbal_yaw_motor_{deformable_infantry_, command_, "/gimbal/yaw"}; - device::Dr16 dr16_{deformable_infantry_}; + device::Dr16 dr16_; device::DjiMotor chassis_wheel_motors_[4]{ device::DjiMotor{deformable_infantry_, command_, "/chassis/left_front_wheel"}, From 6317815c7d21fceb81df071a3f2309534d8be6cc Mon Sep 17 00:00:00 2001 From: ZGZ713912 Date: Thu, 21 May 2026 04:11:39 +0800 Subject: [PATCH 26/52] feat: enhance remote control functionality with timeout check in Vt13 --- .../src/rmcs_core/src/hardware/device/remote_control.hpp | 9 +++++++-- rmcs_ws/src/rmcs_core/src/hardware/device/vt13.hpp | 9 +++++++++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/rmcs_ws/src/rmcs_core/src/hardware/device/remote_control.hpp b/rmcs_ws/src/rmcs_core/src/hardware/device/remote_control.hpp index 34eaeec2..fda5afdc 100644 --- a/rmcs_ws/src/rmcs_core/src/hardware/device/remote_control.hpp +++ b/rmcs_ws/src/rmcs_core/src/hardware/device/remote_control.hpp @@ -1,5 +1,6 @@ #pragma once +#include #include #include @@ -42,7 +43,9 @@ class RemoteControl { } void update() { - if (vt13_.mode_switch() == Vt13::ModeSwitch::kCine) { + const bool vt13_fresh = vt13_.remote_control_fresh(vt13_timeout_); + + if (vt13_fresh && vt13_.mode_switch() == Vt13::ModeSwitch::kCine) { *switch_right_output_ = rmcs_msgs::Switch::DOWN; *switch_left_output_ = rmcs_msgs::Switch::DOWN; } else { @@ -50,7 +53,7 @@ class RemoteControl { *switch_left_output_ = dr16_.switch_left(); } - if (vt13_.mode_switch() == Vt13::ModeSwitch::kSport) { + if (vt13_fresh && vt13_.mode_switch() == Vt13::ModeSwitch::kSport) { *joystick_right_output_ = vt13_.joystick_right(); *joystick_left_output_ = vt13_.joystick_left(); @@ -75,6 +78,8 @@ class RemoteControl { } private: + static constexpr auto vt13_timeout_ = std::chrono::milliseconds{250}; + Dr16& dr16_; Vt13& vt13_; diff --git a/rmcs_ws/src/rmcs_core/src/hardware/device/vt13.hpp b/rmcs_ws/src/rmcs_core/src/hardware/device/vt13.hpp index b8dae5ce..8a68eccf 100644 --- a/rmcs_ws/src/rmcs_core/src/hardware/device/vt13.hpp +++ b/rmcs_ws/src/rmcs_core/src/hardware/device/vt13.hpp @@ -1,5 +1,6 @@ #pragma once +#include #include #include #include @@ -19,6 +20,8 @@ namespace rmcs_core::hardware::device { class Vt13 { public: + using Clock = std::chrono::steady_clock; + enum class ModeSwitch : uint8_t { kUnknown = 0, kCine = 1, @@ -80,6 +83,10 @@ class Vt13 { [[nodiscard]] rmcs_msgs::Mouse mouse() const noexcept { return mouse_; } [[nodiscard]] rmcs_msgs::Keyboard keyboard() const noexcept { return keyboard_; } + [[nodiscard]] bool remote_control_fresh(Clock::duration timeout) const noexcept { + return last_remote_control_frame_time_ != Clock::time_point::min() + && Clock::now() - last_remote_control_frame_time_ <= timeout; + } private: struct Incomplete {}; @@ -149,6 +156,7 @@ class Vt13 { } void update_remote_control_data(const RemoteControlData& data) { + last_remote_control_frame_time_ = Clock::now(); mode_switch_ = static_cast(data.mode_switch + 1); joystick_right_ = { @@ -204,6 +212,7 @@ class Vt13 { } rmcs_utility::RingBuffer data_buffer_{1024}; + Clock::time_point last_remote_control_frame_time_ = Clock::time_point::min(); ModeSwitch mode_switch_ = ModeSwitch::kUnknown; From 1be6b3b3d90f0692878a35d50e5f37539b53b46e Mon Sep 17 00:00:00 2001 From: ZGZ713912 Date: Thu, 21 May 2026 07:31:38 +0800 Subject: [PATCH 27/52] strange --- rmcs_ws/src/rmcs_auto_aim_v2 | 2 +- .../src/rmcs_core/src/hardware/deformable-infantry-omni-b.cpp | 2 +- rmcs_ws/src/rmcs_core/src/hardware/deformable-infantry-omni.cpp | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/rmcs_ws/src/rmcs_auto_aim_v2 b/rmcs_ws/src/rmcs_auto_aim_v2 index eaf4a736..7fbe1bd4 160000 --- a/rmcs_ws/src/rmcs_auto_aim_v2 +++ b/rmcs_ws/src/rmcs_auto_aim_v2 @@ -1 +1 @@ -Subproject commit eaf4a73625996ec3443fac333421e3f9c93e91c0 +Subproject commit 7fbe1bd41659bae94d469f43715721313227f95b diff --git a/rmcs_ws/src/rmcs_core/src/hardware/deformable-infantry-omni-b.cpp b/rmcs_ws/src/rmcs_core/src/hardware/deformable-infantry-omni-b.cpp index 865e7896..55429648 100644 --- a/rmcs_ws/src/rmcs_core/src/hardware/deformable-infantry-omni-b.cpp +++ b/rmcs_ws/src/rmcs_core/src/hardware/deformable-infantry-omni-b.cpp @@ -717,7 +717,7 @@ class DeformableInfantryOmniB bmi088_.set_coordinate_mapping([](double x, double y, double z) { // Top board BMI088 maps to gimbal frame as (-x, -y, z). - return std::make_tuple(y, -x, z); + return std::make_tuple(-x, -y, z); }); } diff --git a/rmcs_ws/src/rmcs_core/src/hardware/deformable-infantry-omni.cpp b/rmcs_ws/src/rmcs_core/src/hardware/deformable-infantry-omni.cpp index 28870de0..e9b86f23 100644 --- a/rmcs_ws/src/rmcs_core/src/hardware/deformable-infantry-omni.cpp +++ b/rmcs_ws/src/rmcs_core/src/hardware/deformable-infantry-omni.cpp @@ -669,7 +669,7 @@ class DeformableInfantryOmni bmi088_.set_coordinate_mapping([](double x, double y, double z) { // Top board BMI088 maps to gimbal frame as (-x, -y, z). - return std::make_tuple(y, -x, z); + return std::make_tuple(-x, -y, z); }); } From 95c71685bb3afc66537d21b61cb2a31ad179c830 Mon Sep 17 00:00:00 2001 From: ZGZ713912 Date: Thu, 21 May 2026 17:19:10 +0800 Subject: [PATCH 28/52] feat: add fault handling and remote control invalidation in VT13 device --- .../rmcs_core/src/hardware/device/vt13.hpp | 33 ++++++++++++++++--- 1 file changed, 29 insertions(+), 4 deletions(-) diff --git a/rmcs_ws/src/rmcs_core/src/hardware/device/vt13.hpp b/rmcs_ws/src/rmcs_core/src/hardware/device/vt13.hpp index 8a68eccf..6a38b7fe 100644 --- a/rmcs_ws/src/rmcs_core/src/hardware/device/vt13.hpp +++ b/rmcs_ws/src/rmcs_core/src/hardware/device/vt13.hpp @@ -1,5 +1,6 @@ #pragma once +#include #include #include #include @@ -41,10 +42,16 @@ class Vt13 { RCLCPP_WARN( rclcpp::get_logger("vt13"), "VT13 input buffer overflow: dropped %zu of %zu bytes", uart_data.size() - written, uart_data.size()); + faulted_.store(true, std::memory_order_relaxed); } } void update_status() { + if (faulted_.exchange(false, std::memory_order_relaxed)) { + invalidate_remote_control(); + return; + } + auto readable = data_buffer_.readable(); if (!readable) return; @@ -62,9 +69,8 @@ class Vt13 { break; } if (std::holds_alternative(result)) { - data_buffer_.pop_front([](std::byte&&) noexcept {}); - readable--; - continue; + invalidate_remote_control(); + break; } if (std::holds_alternative(result)) { readable -= std::get(result).read; @@ -84,6 +90,8 @@ class Vt13 { [[nodiscard]] rmcs_msgs::Mouse mouse() const noexcept { return mouse_; } [[nodiscard]] rmcs_msgs::Keyboard keyboard() const noexcept { return keyboard_; } [[nodiscard]] bool remote_control_fresh(Clock::duration timeout) const noexcept { + if (faulted_.load(std::memory_order_relaxed)) + return false; return last_remote_control_frame_time_ != Clock::time_point::min() && Clock::now() - last_remote_control_frame_time_ <= timeout; } @@ -146,7 +154,8 @@ class Vt13 { sizeof(RemoteControlData)); if (data.header != RemoteControlData::kHeaderMagic - || !rmcs_utility::dji_crc::verify_crc16(data)) + || !rmcs_utility::dji_crc::verify_crc16(data) + || data.mode_switch > 2) return VerificationFailed{}; data_buffer_.pop_front_n([](std::byte&&) noexcept {}, sizeof(RemoteControlData)); @@ -197,6 +206,8 @@ class Vt13 { const std::size_t total_frame_size = sizeof(RefereeFrameHeader) + 2 + header.data_length + 2; + if (total_frame_size > data_buffer_.max_size()) + return VerificationFailed{}; if (readable < total_frame_size) return Incomplete{}; @@ -204,6 +215,19 @@ class Vt13 { return Success{total_frame_size}; } + void invalidate_remote_control() { + faulted_.store(false, std::memory_order_relaxed); + data_buffer_.clear(); + last_remote_control_frame_time_ = Clock::time_point::min(); + mode_switch_ = ModeSwitch::kUnknown; + joystick_left_.setZero(); + joystick_right_.setZero(); + mouse_velocity_.setZero(); + mouse_wheel_ = 0.0; + mouse_ = rmcs_msgs::Mouse::zero(); + keyboard_ = rmcs_msgs::Keyboard::zero(); + } + static double channel_to_double(int32_t value) { value -= 1024; if (-660 <= value && value <= 660) @@ -212,6 +236,7 @@ class Vt13 { } rmcs_utility::RingBuffer data_buffer_{1024}; + std::atomic faulted_{false}; Clock::time_point last_remote_control_frame_time_ = Clock::time_point::min(); ModeSwitch mode_switch_ = ModeSwitch::kUnknown; From b46fda54d8c7dd5d4a1a56f9a494f73c09df6fff Mon Sep 17 00:00:00 2001 From: ZGZ713912 Date: Fri, 22 May 2026 09:09:47 +0800 Subject: [PATCH 29/52] feat: update .gitmodules to include rmcs_auto_aim_v2 and serial submodules --- .codex | 0 .devcontainer/state/codex/.tmp/plugins | 1 - .gitmodules | 10 +++++++--- 3 files changed, 7 insertions(+), 4 deletions(-) delete mode 100644 .codex delete mode 160000 .devcontainer/state/codex/.tmp/plugins diff --git a/.codex b/.codex deleted file mode 100644 index e69de29b..00000000 diff --git a/.devcontainer/state/codex/.tmp/plugins b/.devcontainer/state/codex/.tmp/plugins deleted file mode 160000 index 3c463363..00000000 --- a/.devcontainer/state/codex/.tmp/plugins +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 3c46336323290cda7c90d78e7a0134f47cc73713 diff --git a/.gitmodules b/.gitmodules index aeef93a2..7aa943b0 100644 --- a/.gitmodules +++ b/.gitmodules @@ -6,6 +6,10 @@ path = rmcs_ws/src/hikcamera url = https://github.com/Alliance-Algorithm/ros2-hikcamera.git -[submodule "rmcs_ws/src/auto_aim_v2"] - path = rmcs_ws/src/auto_aim_v2 - url = https://github.com/Alliance-Algorithm/rmcs_auto_aim_v2.git \ No newline at end of file +[submodule "rmcs_ws/src/rmcs_auto_aim_v2"] + path = rmcs_ws/src/rmcs_auto_aim_v2 + url = https://github.com/Alliance-Algorithm/rmcs_auto_aim_v2.git + +[submodule "rmcs_ws/src/serial"] + path = rmcs_ws/src/serial + url = git@github.com:Alliance-Algorithm/ros2-serial.git From 9bf1a98a45a4bd47523776959e84eaaf78c1e76c Mon Sep 17 00:00:00 2001 From: ZGZ713912 Date: Fri, 22 May 2026 09:30:18 +0800 Subject: [PATCH 30/52] feat: update .gitmodules to remove serial submodules --- .gitmodules | 6 +----- rmcs_ws/src/serial | 1 - 2 files changed, 1 insertion(+), 6 deletions(-) delete mode 160000 rmcs_ws/src/serial diff --git a/.gitmodules b/.gitmodules index 7aa943b0..6fd0f3b2 100644 --- a/.gitmodules +++ b/.gitmodules @@ -8,8 +8,4 @@ [submodule "rmcs_ws/src/rmcs_auto_aim_v2"] path = rmcs_ws/src/rmcs_auto_aim_v2 - url = https://github.com/Alliance-Algorithm/rmcs_auto_aim_v2.git - -[submodule "rmcs_ws/src/serial"] - path = rmcs_ws/src/serial - url = git@github.com:Alliance-Algorithm/ros2-serial.git + url = https://github.com/Alliance-Algorithm/rmcs_auto_aim_v2.git \ No newline at end of file diff --git a/rmcs_ws/src/serial b/rmcs_ws/src/serial deleted file mode 160000 index d50c4018..00000000 --- a/rmcs_ws/src/serial +++ /dev/null @@ -1 +0,0 @@ -Subproject commit d50c401809f17cc5862bdbe37cc6e15737ba7c49 From c191f106dc0351796861ad16fd0f4af98278f06a Mon Sep 17 00:00:00 2001 From: ZGZ713912 Date: Fri, 22 May 2026 12:01:30 +0800 Subject: [PATCH 31/52] feat: update transform parameters and enhance chassis output status handling --- .../chassis/chassis_power_controller.cpp | 7 ++----- .../hardware/deformable-infantry-omni-b.cpp | 2 +- .../src/hardware/deformable-infantry-omni.cpp | 2 +- .../hardware/deformable-infantry-steering.cpp | 2 +- rmcs_ws/src/rmcs_core/src/referee/status.cpp | 19 ++++++++++++++++++- 5 files changed, 23 insertions(+), 9 deletions(-) diff --git a/rmcs_ws/src/rmcs_core/src/controller/chassis/chassis_power_controller.cpp b/rmcs_ws/src/rmcs_core/src/controller/chassis/chassis_power_controller.cpp index 2920b48f..543750e6 100644 --- a/rmcs_ws/src/rmcs_core/src/controller/chassis/chassis_power_controller.cpp +++ b/rmcs_ws/src/rmcs_core/src/controller/chassis/chassis_power_controller.cpp @@ -63,7 +63,7 @@ class ChassisPowerController update_virtual_buffer_energy(); - boost_mode_ = keyboard.shift || keyboard.ctrl; + boost_mode_ = keyboard.shift; *supercap_control_enabled_ = boost_mode_; update_control_power_limit(); } @@ -110,9 +110,7 @@ class ChassisPowerController double power_limit; if (*supercap_control_enabled_ && *supercap_enabled_) - power_limit = *mode_ == rmcs_msgs::ChassisMode::LAUNCH_RAMP - ? inf_ - : *chassis_power_limit_referee_ + 80.0; + power_limit = *chassis_power_limit_referee_ + 80.0; else power_limit = *chassis_power_limit_referee_; chassis_power_limit_expected_ = power_limit; @@ -142,7 +140,6 @@ class ChassisPowerController static_cast(std::round(*chassis_control_power_limit_))); } - static constexpr double inf_ = std::numeric_limits::infinity(); static constexpr double nan_ = std::numeric_limits::quiet_NaN(); InputInterface mode_; diff --git a/rmcs_ws/src/rmcs_core/src/hardware/deformable-infantry-omni-b.cpp b/rmcs_ws/src/rmcs_core/src/hardware/deformable-infantry-omni-b.cpp index 55429648..0acb879b 100644 --- a/rmcs_ws/src/rmcs_core/src/hardware/deformable-infantry-omni-b.cpp +++ b/rmcs_ws/src/rmcs_core/src/hardware/deformable-infantry-omni-b.cpp @@ -53,7 +53,7 @@ class DeformableInfantryOmniB register_input("/predefined/timestamp", timestamp_); register_output("/tf", tf_); - tf_->set_transform(Eigen::Translation3d{0.16, 0.0, 0.15}); + tf_->set_transform(Eigen::Translation3d{0.058, -0.08, 0.0}); // For command: remote-status using Srv = std_srvs::srv::Trigger; diff --git a/rmcs_ws/src/rmcs_core/src/hardware/deformable-infantry-omni.cpp b/rmcs_ws/src/rmcs_core/src/hardware/deformable-infantry-omni.cpp index e9b86f23..cfd8b398 100644 --- a/rmcs_ws/src/rmcs_core/src/hardware/deformable-infantry-omni.cpp +++ b/rmcs_ws/src/rmcs_core/src/hardware/deformable-infantry-omni.cpp @@ -49,7 +49,7 @@ class DeformableInfantryOmni register_input("/predefined/timestamp", timestamp_); register_output("/tf", tf_); - tf_->set_transform(Eigen::Translation3d{0.16, 0.0, 0.15}); + tf_->set_transform(Eigen::Translation3d{0.058, -0.08, 0.0}); bottom_board_ = std::make_unique( *this, *command_, get_parameter("serial_filter_rmcs_board").as_string()); diff --git a/rmcs_ws/src/rmcs_core/src/hardware/deformable-infantry-steering.cpp b/rmcs_ws/src/rmcs_core/src/hardware/deformable-infantry-steering.cpp index c4cace8e..301ed10f 100644 --- a/rmcs_ws/src/rmcs_core/src/hardware/deformable-infantry-steering.cpp +++ b/rmcs_ws/src/rmcs_core/src/hardware/deformable-infantry-steering.cpp @@ -51,7 +51,7 @@ class DeformableInfantryV2 register_input("/predefined/timestamp", timestamp_); register_output("/tf", tf_); - tf_->set_transform(Eigen::Translation3d{0.16, 0.0, 0.15}); + tf_->set_transform(Eigen::Translation3d{0.058, -0.08, 0.0}); // For command: remote-status using Srv = std_srvs::srv::Trigger; diff --git a/rmcs_ws/src/rmcs_core/src/referee/status.cpp b/rmcs_ws/src/rmcs_core/src/referee/status.cpp index c080a355..0ad65b0b 100644 --- a/rmcs_ws/src/rmcs_core/src/referee/status.cpp +++ b/rmcs_ws/src/rmcs_core/src/referee/status.cpp @@ -1,3 +1,4 @@ +#include #include #include #include @@ -16,6 +17,8 @@ namespace rmcs_core::referee { using namespace status; +using Clock = std::chrono::steady_clock; + class Status : public rmcs_executor::Component , public rclcpp::Node { @@ -97,8 +100,10 @@ class Status } void update() override { - if (!serial_.active()) + if (!serial_.active()) { + update_chassis_output_status_timeout(); return; + } if (cache_size_ >= sizeof(frame_.header)) { auto frame_size = sizeof(frame_.header) + sizeof(frame_.body.command_id) @@ -142,9 +147,17 @@ class Status *robot_chassis_power_ = 0.0; *robot_buffer_energy_ = 60.0; } + + update_chassis_output_status_timeout(); } private: + void update_chassis_output_status_timeout() { + if (Clock::now() - last_robot_status_update_time_ > chassis_output_status_timeout_) { + *chassis_output_status_ = true; + } + } + void process_frame() { auto command_id = frame_.body.command_id; if (command_id == 0x0001) @@ -211,6 +224,8 @@ class Status } void update_robot_status() { + last_robot_status_update_time_ = Clock::now(); + if (*game_stage_ == rmcs_msgs::GameStage::STARTED) robot_status_watchdog_.reset(60'000); else @@ -306,6 +321,7 @@ class Status static constexpr int64_t safe_shooter_heat_limit = 50'000; // Chassis: Health priority with level 1 static constexpr double safe_chassis_power_limit = 45; + static constexpr auto chassis_output_status_timeout_ = std::chrono::milliseconds{500}; rclcpp::Logger logger_; @@ -327,6 +343,7 @@ class Status OutputInterface robot_shooter_cooling_, robot_shooter_heat_limit_; OutputInterface robot_chassis_power_limit_; OutputInterface chassis_output_status_; + Clock::time_point last_robot_status_update_time_ = Clock::now(); rmcs_utility::TickTimer power_heat_data_watchdog_; OutputInterface robot_chassis_power_; From 6f182930d192f9c3ac9d489f5a07dfabefac0f2e Mon Sep 17 00:00:00 2001 From: ZGZ713912 Date: Fri, 22 May 2026 12:03:48 +0800 Subject: [PATCH 32/52] feat:add kSport default mode in remote_control --- rmcs_ws/src/rmcs_core/src/hardware/device/remote_control.hpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/rmcs_ws/src/rmcs_core/src/hardware/device/remote_control.hpp b/rmcs_ws/src/rmcs_core/src/hardware/device/remote_control.hpp index fda5afdc..9794fb72 100644 --- a/rmcs_ws/src/rmcs_core/src/hardware/device/remote_control.hpp +++ b/rmcs_ws/src/rmcs_core/src/hardware/device/remote_control.hpp @@ -54,6 +54,8 @@ class RemoteControl { } if (vt13_fresh && vt13_.mode_switch() == Vt13::ModeSwitch::kSport) { + *switch_right_output_ = rmcs_msgs::Switch::MIDDLE; + *switch_left_output_ = rmcs_msgs::Switch::MIDDLE; *joystick_right_output_ = vt13_.joystick_right(); *joystick_left_output_ = vt13_.joystick_left(); From d74f7d9ad012c93200a9943ca9a3a70a66cb2d6b Mon Sep 17 00:00:00 2001 From: qzhhhi Date: Fri, 22 May 2026 13:21:21 +0800 Subject: [PATCH 33/52] feat: update serial filter for IMU and modify chassis direction indicator angle --- .../config/deformable-infantry-omni.yaml | 4 +- .../rmcs_core/src/hardware/device/dr16.hpp | 16 ++ .../src/hardware/device/remote_control.hpp | 32 ++- .../src/hardware/device/supercap.hpp | 2 +- .../rmcs_core/src/hardware/device/vt13.hpp | 203 +++++++++++++++++- .../referee/app/ui/deformable_infantry_ui.cpp | 2 +- 6 files changed, 247 insertions(+), 12 deletions(-) diff --git a/rmcs_ws/src/rmcs_bringup/config/deformable-infantry-omni.yaml b/rmcs_ws/src/rmcs_bringup/config/deformable-infantry-omni.yaml index 5eb1c098..e3fa2fef 100644 --- a/rmcs_ws/src/rmcs_bringup/config/deformable-infantry-omni.yaml +++ b/rmcs_ws/src/rmcs_bringup/config/deformable-infantry-omni.yaml @@ -42,7 +42,7 @@ deformable_infantry: ros__parameters: serial_filter_rmcs_board: "AF-23FB-EE32-B892-1302-AE70-D640-7B4E-0CBF" serial_filter_top_board: "AF-8AE3-4EC1-03C3-C494-88FE-2DC4-3018-0298" - serial_filter_imu: "AF-8217-B05F-811B-6F3E-04EA-448E-9D03-CA2C" + serial_filter_imu: "AF-7A42-07AA-D181-0356-7715-6D7C-4C65-5762" left_front_zero_point: 374 left_back_zero_point: 5801 right_back_zero_point: 7817 @@ -51,7 +51,7 @@ deformable_infantry: pitch_motor_zero_point: 6853 debug_log_supercap: false debug_log_wheel_motor: false - debug_log_deformable_joint_motor: false + debug_log_deformable_joint_motor: true chassis_controller: ros__parameters: diff --git a/rmcs_ws/src/rmcs_core/src/hardware/device/dr16.hpp b/rmcs_ws/src/rmcs_core/src/hardware/device/dr16.hpp index 6bd0ac2f..17711aa7 100644 --- a/rmcs_ws/src/rmcs_core/src/hardware/device/dr16.hpp +++ b/rmcs_ws/src/rmcs_core/src/hardware/device/dr16.hpp @@ -2,6 +2,7 @@ #include #include +#include #include #include #include @@ -22,6 +23,8 @@ class Dr16 { if (uart_data.size() != kStatusSize) return; + last_receive_time_.store(Clock::now(), std::memory_order_relaxed); + auto* cursor = uart_data.data(); uint64_t part1{}; @@ -78,6 +81,12 @@ class Dr16 { [[nodiscard]] const Eigen::Vector2d& joystick_right() const noexcept { return joystick_right_; } + [[nodiscard]] bool valid() const noexcept { + const auto last_receive_time = last_receive_time_.load(std::memory_order_relaxed); + return last_receive_time != TimePoint::min() + && Clock::now() - last_receive_time <= kFreshTimeout; + } + [[nodiscard]] const Eigen::Vector2d& joystick_left() const noexcept { return joystick_left_; } [[nodiscard]] rmcs_msgs::Switch switch_right() const noexcept { return switch_right_; } @@ -99,6 +108,11 @@ class Dr16 { } private: + using Clock = std::chrono::steady_clock; + using TimePoint = Clock::time_point; + + static constexpr auto kFreshTimeout = std::chrono::milliseconds(500); + static constexpr std::size_t kPart1Size = 6; static constexpr std::size_t kPart2Size = 8; static constexpr std::size_t kPart3Size = 4; @@ -190,6 +204,8 @@ class Dr16 { })}; static_assert(decltype(data_part3_)::is_always_lock_free); + std::atomic last_receive_time_{TimePoint::min()}; + Eigen::Vector2d joystick_right_ = Eigen::Vector2d::Zero(); Eigen::Vector2d joystick_left_ = Eigen::Vector2d::Zero(); Eigen::Vector2d mouse_velocity_ = Eigen::Vector2d::Zero(); diff --git a/rmcs_ws/src/rmcs_core/src/hardware/device/remote_control.hpp b/rmcs_ws/src/rmcs_core/src/hardware/device/remote_control.hpp index 9794fb72..ebaa8809 100644 --- a/rmcs_ws/src/rmcs_core/src/hardware/device/remote_control.hpp +++ b/rmcs_ws/src/rmcs_core/src/hardware/device/remote_control.hpp @@ -43,16 +43,20 @@ class RemoteControl { } void update() { +<<<<<<< HEAD const bool vt13_fresh = vt13_.remote_control_fresh(vt13_timeout_); if (vt13_fresh && vt13_.mode_switch() == Vt13::ModeSwitch::kCine) { *switch_right_output_ = rmcs_msgs::Switch::DOWN; *switch_left_output_ = rmcs_msgs::Switch::DOWN; } else { +======= + if (dr16_.valid() || !vt13_.valid() || vt13_.mode_switch() == Vt13::ModeSwitch::kNormal) { +>>>>>>> 7a9bbcf6 (vt13 fixfixfixfix) *switch_right_output_ = dr16_.switch_right(); *switch_left_output_ = dr16_.switch_left(); - } +<<<<<<< HEAD if (vt13_fresh && vt13_.mode_switch() == Vt13::ModeSwitch::kSport) { *switch_right_output_ = rmcs_msgs::Switch::MIDDLE; *switch_left_output_ = rmcs_msgs::Switch::MIDDLE; @@ -65,6 +69,8 @@ class RemoteControl { *mouse_output_ = vt13_.mouse(); *keyboard_output_ = vt13_.keyboard(); } else { +======= +>>>>>>> 7a9bbcf6 (vt13 fixfixfixfix) *joystick_right_output_ = dr16_.joystick_right(); *joystick_left_output_ = dr16_.joystick_left(); @@ -73,6 +79,30 @@ class RemoteControl { *mouse_output_ = dr16_.mouse(); *keyboard_output_ = dr16_.keyboard(); + } else if (vt13_.mode_switch() == Vt13::ModeSwitch::kCine) { + *switch_right_output_ = rmcs_msgs::Switch::DOWN; + *switch_left_output_ = rmcs_msgs::Switch::DOWN; + + *joystick_right_output_ = Eigen::Vector2d::Zero(); + *joystick_left_output_ = Eigen::Vector2d::Zero(); + + *mouse_velocity_output_ = Eigen::Vector2d::Zero(); + *mouse_wheel_output_ = 0; + + *mouse_output_ = rmcs_msgs::Mouse::zero(); + *keyboard_output_ = rmcs_msgs::Keyboard::zero(); + } else if (vt13_.mode_switch() == Vt13::ModeSwitch::kSport) { + *switch_right_output_ = rmcs_msgs::Switch::MIDDLE; + *switch_left_output_ = rmcs_msgs::Switch::MIDDLE; + + *joystick_right_output_ = vt13_.joystick_right(); + *joystick_left_output_ = vt13_.joystick_left(); + + *mouse_velocity_output_ = vt13_.mouse_velocity(); + *mouse_wheel_output_ = vt13_.mouse_wheel(); + + *mouse_output_ = vt13_.mouse(); + *keyboard_output_ = vt13_.keyboard(); } *rotary_knob_output_ = dr16_.rotary_knob(); diff --git a/rmcs_ws/src/rmcs_core/src/hardware/device/supercap.hpp b/rmcs_ws/src/rmcs_core/src/hardware/device/supercap.hpp index e88fc018..087b9f96 100644 --- a/rmcs_ws/src/rmcs_core/src/hardware/device/supercap.hpp +++ b/rmcs_ws/src/rmcs_core/src/hardware/device/supercap.hpp @@ -54,7 +54,7 @@ class Supercap { CanPacket8::Quarter generate_command() const { SupercapCommand command; - command.enabled = *chassis_output_status_ && *supercap_control_enabled_; + command.enabled = *chassis_output_status_; const double power_limit = *supercap_charge_power_limit_; if (std::isnan(power_limit)) diff --git a/rmcs_ws/src/rmcs_core/src/hardware/device/vt13.hpp b/rmcs_ws/src/rmcs_core/src/hardware/device/vt13.hpp index 6a38b7fe..0e374a19 100644 --- a/rmcs_ws/src/rmcs_core/src/hardware/device/vt13.hpp +++ b/rmcs_ws/src/rmcs_core/src/hardware/device/vt13.hpp @@ -1,8 +1,15 @@ #pragma once +<<<<<<< HEAD #include +======= +#include +#include +#include +>>>>>>> 7a9bbcf6 (vt13 fixfixfixfix) #include #include +#include #include #include #include @@ -33,53 +40,89 @@ class Vt13 { Vt13() = default; void store_status(std::span uart_data) { + store_calls_.fetch_add(1, std::memory_order_relaxed); + received_bytes_.fetch_add(uart_data.size(), std::memory_order_relaxed); + const auto written = data_buffer_.emplace_back_n( [iter = uart_data.cbegin()](std::byte* storage) mutable noexcept { *storage = *iter++; }, uart_data.size()); if (written != uart_data.size()) { +<<<<<<< HEAD RCLCPP_WARN( rclcpp::get_logger("vt13"), "VT13 input buffer overflow: dropped %zu of %zu bytes", uart_data.size() - written, uart_data.size()); faulted_.store(true, std::memory_order_relaxed); +======= + const auto dropped = uart_data.size() - written; + overflow_count_.fetch_add(1, std::memory_order_relaxed); + overflow_dropped_bytes_.fetch_add(dropped, std::memory_order_relaxed); + if (should_log_overflow()) { + RCLCPP_WARN( + logger_, "VT13 input buffer overflow: dropped %zu of %zu bytes", dropped, + uart_data.size()); + } +>>>>>>> 7a9bbcf6 (vt13 fixfixfixfix) } } void update_status() { +<<<<<<< HEAD if (faulted_.exchange(false, std::memory_order_relaxed)) { invalidate_remote_control(); return; } +======= + const auto now = Clock::now(); +>>>>>>> 7a9bbcf6 (vt13 fixfixfixfix) auto readable = data_buffer_.readable(); - if (!readable) - return; + peak_readable_ = std::max(peak_readable_, readable); while (readable) { ReadResult result = VerificationFailed{}; const std::byte front = *data_buffer_.peek_front(); if (front == std::byte{0xa9}) - result = read_remote_control_data(readable); + result = read_remote_control_data(readable, now); else if (front == std::byte(0xa5)) - result = read_referee_style_data(readable); + result = read_referee_style_data(readable, now); + else { + unknown_prefix_count_++; + if (should_log_verification_failure(now)) { + RCLCPP_WARN( + logger_, "VT13 unknown prefix: front=0x%02x readable=%zu", + std::to_integer(front), readable); + } + } if (std::holds_alternative(result)) { break; } if (std::holds_alternative(result)) { +<<<<<<< HEAD invalidate_remote_control(); break; +======= + verification_failures_++; + data_buffer_.pop_front([](std::byte&&) noexcept {}); + readable--; + continue; +>>>>>>> 7a9bbcf6 (vt13 fixfixfixfix) } if (std::holds_alternative(result)) { readable -= std::get(result).read; continue; } } + + refresh_validity(now); + maybe_log_statistics(now); } [[nodiscard]] ModeSwitch mode_switch() const noexcept { return mode_switch_; } + [[nodiscard]] bool valid() const noexcept { return valid_; } [[nodiscard]] const Eigen::Vector2d& joystick_left() const noexcept { return joystick_left_; } [[nodiscard]] const Eigen::Vector2d& joystick_right() const noexcept { return joystick_right_; } @@ -97,6 +140,15 @@ class Vt13 { } private: + using Clock = std::chrono::steady_clock; + using TimePoint = Clock::time_point; + + static constexpr auto kFreshTimeout = std::chrono::milliseconds(500); + static constexpr auto kVerificationLogInterval = std::chrono::seconds(1); + static constexpr auto kOverflowLogInterval = std::chrono::seconds(1); + static constexpr auto kStatisticsLogInterval = std::chrono::seconds(5); + static constexpr std::size_t kRefereeFrameMaxSize = 256; + struct Incomplete {}; struct VerificationFailed {}; struct Success { @@ -142,7 +194,7 @@ class Vt13 { uint8_t crc8; }; - ReadResult read_remote_control_data(const std::size_t readable) { + ReadResult read_remote_control_data(const std::size_t readable, const TimePoint now) { if (readable < sizeof(RemoteControlData)) return Incomplete{}; @@ -153,14 +205,34 @@ class Vt13 { }, sizeof(RemoteControlData)); +<<<<<<< HEAD if (data.header != RemoteControlData::kHeaderMagic || !rmcs_utility::dji_crc::verify_crc16(data) || data.mode_switch > 2) +======= + if (data.header != RemoteControlData::kHeaderMagic) { + remote_bad_header_count_++; + if (should_log_verification_failure(now)) { + RCLCPP_WARN( + logger_, "VT13 remote control header invalid: header=0x%04x readable=%zu", + data.header, readable); + } +>>>>>>> 7a9bbcf6 (vt13 fixfixfixfix) + return VerificationFailed{}; + } + if (!rmcs_utility::dji_crc::verify_crc16(data)) { + remote_bad_crc_count_++; + if (should_log_verification_failure(now)) + RCLCPP_WARN(logger_, "VT13 remote control crc16 invalid: readable=%zu", readable); return VerificationFailed{}; + } data_buffer_.pop_front_n([](std::byte&&) noexcept {}, sizeof(RemoteControlData)); update_remote_control_data(data); + valid_ = true; + last_remote_control_received_at_ = now; + remote_success_count_++; return Success{sizeof(RemoteControlData)}; } @@ -190,7 +262,7 @@ class Vt13 { keyboard_ = std::bit_cast(data.keyboard); } - ReadResult read_referee_style_data(const std::size_t readable) { + ReadResult read_referee_style_data(const std::size_t readable, const TimePoint now) { if (readable < sizeof(RefereeFrameHeader)) return Incomplete{}; @@ -201,20 +273,38 @@ class Vt13 { }, sizeof(RefereeFrameHeader)); - if (!rmcs_utility::dji_crc::verify_crc8(header)) + if (!rmcs_utility::dji_crc::verify_crc8(header)) { + referee_bad_crc8_count_++; + if (should_log_verification_failure(now)) + RCLCPP_WARN(logger_, "VT13 referee header crc8 invalid: readable=%zu", readable); return VerificationFailed{}; + } const std::size_t total_frame_size = sizeof(RefereeFrameHeader) + 2 + header.data_length + 2; +<<<<<<< HEAD if (total_frame_size > data_buffer_.max_size()) return VerificationFailed{}; +======= + if (total_frame_size > kRefereeFrameMaxSize) { + referee_oversize_count_++; + if (should_log_verification_failure(now)) { + RCLCPP_WARN( + logger_, "VT13 referee frame oversized: data_length=%u total=%zu readable=%zu", + header.data_length, total_frame_size, readable); + } + return VerificationFailed{}; + } +>>>>>>> 7a9bbcf6 (vt13 fixfixfixfix) if (readable < total_frame_size) return Incomplete{}; data_buffer_.pop_front_n([](std::byte&&) noexcept {}, total_frame_size); + referee_discarded_count_++; return Success{total_frame_size}; } +<<<<<<< HEAD void invalidate_remote_control() { faulted_.store(false, std::memory_order_relaxed); data_buffer_.clear(); @@ -224,6 +314,83 @@ class Vt13 { joystick_right_.setZero(); mouse_velocity_.setZero(); mouse_wheel_ = 0.0; +======= + bool should_log_verification_failure(const TimePoint now) { + if (last_verification_log_time_ != TimePoint::min() + && now - last_verification_log_time_ < kVerificationLogInterval) + return false; + last_verification_log_time_ = now; + return true; + } + + bool should_log_overflow() { + const auto now = Clock::now(); + if (last_overflow_log_time_ != TimePoint::min() + && now - last_overflow_log_time_ < kOverflowLogInterval) + return false; + + last_overflow_log_time_ = now; + return true; + } + + void refresh_validity(const TimePoint now) { + if (!valid_ || now - last_remote_control_received_at_ <= kFreshTimeout) + return; + + reset_remote_control_state(); + valid_ = false; + } + + void maybe_log_statistics(const TimePoint now) { + if (last_statistics_log_time_ == TimePoint::min()) { + last_statistics_log_time_ = now; + return; + } + + const auto elapsed = now - last_statistics_log_time_; + if (elapsed < kStatisticsLogInterval) + return; + + const auto readable = data_buffer_.readable(); + const auto store_calls = store_calls_.exchange(0, std::memory_order_relaxed); + const auto received_bytes = received_bytes_.exchange(0, std::memory_order_relaxed); + const auto overflow_count = overflow_count_.exchange(0, std::memory_order_relaxed); + const auto overflow_dropped_bytes = + overflow_dropped_bytes_.exchange(0, std::memory_order_relaxed); + const auto elapsed_seconds = std::chrono::duration(elapsed).count(); + + RCLCPP_INFO( + logger_, + "VT13 stats: rx=%.1f Hz %.1f B/s remote_ok=%zu verify_fail=%zu remote_bad_header=%zu " + "remote_bad_crc=%zu referee_discarded=%zu referee_bad_crc8=%zu referee_oversize=%zu " + "unknown_prefix=%zu overflow=%llu dropped=%llu readable=%zu peak=%zu valid=%s", + static_cast(store_calls) / elapsed_seconds, + static_cast(received_bytes) / elapsed_seconds, remote_success_count_, + verification_failures_, remote_bad_header_count_, remote_bad_crc_count_, + referee_discarded_count_, referee_bad_crc8_count_, referee_oversize_count_, + unknown_prefix_count_, static_cast(overflow_count), + static_cast(overflow_dropped_bytes), readable, peak_readable_, + valid_ ? "true" : "false"); + + remote_success_count_ = 0; + verification_failures_ = 0; + remote_bad_header_count_ = 0; + remote_bad_crc_count_ = 0; + referee_discarded_count_ = 0; + referee_bad_crc8_count_ = 0; + referee_oversize_count_ = 0; + unknown_prefix_count_ = 0; + peak_readable_ = readable; + last_statistics_log_time_ = now; + } + + void reset_remote_control_state() { + mode_switch_ = ModeSwitch::kUnknown; + joystick_left_ = Eigen::Vector2d::Zero(); + joystick_right_ = Eigen::Vector2d::Zero(); + mouse_velocity_ = Eigen::Vector2d::Zero(); + mouse_wheel_ = 0; +>>>>>>> 7a9bbcf6 (vt13 fixfixfixfix) mouse_ = rmcs_msgs::Mouse::zero(); keyboard_ = rmcs_msgs::Keyboard::zero(); } @@ -235,10 +402,32 @@ class Vt13 { return 0.0; } + rclcpp::Logger logger_ = rclcpp::get_logger("vt13"); rmcs_utility::RingBuffer data_buffer_{1024}; std::atomic faulted_{false}; Clock::time_point last_remote_control_frame_time_ = Clock::time_point::min(); + std::atomic store_calls_{0}; + std::atomic received_bytes_{0}; + std::atomic overflow_count_{0}; + std::atomic overflow_dropped_bytes_{0}; + + TimePoint last_remote_control_received_at_ = TimePoint::min(); + TimePoint last_verification_log_time_ = TimePoint::min(); + TimePoint last_overflow_log_time_ = TimePoint::min(); + TimePoint last_statistics_log_time_ = TimePoint::min(); + + bool valid_ = false; + std::size_t peak_readable_ = 0; + std::size_t remote_success_count_ = 0; + std::size_t verification_failures_ = 0; + std::size_t remote_bad_header_count_ = 0; + std::size_t remote_bad_crc_count_ = 0; + std::size_t referee_discarded_count_ = 0; + std::size_t referee_bad_crc8_count_ = 0; + std::size_t referee_oversize_count_ = 0; + std::size_t unknown_prefix_count_ = 0; + ModeSwitch mode_switch_ = ModeSwitch::kUnknown; Eigen::Vector2d joystick_left_ = Eigen::Vector2d::Zero(); diff --git a/rmcs_ws/src/rmcs_core/src/referee/app/ui/deformable_infantry_ui.cpp b/rmcs_ws/src/rmcs_core/src/referee/app/ui/deformable_infantry_ui.cpp index 398f75d8..c6961758 100644 --- a/rmcs_ws/src/rmcs_core/src/referee/app/ui/deformable_infantry_ui.cpp +++ b/rmcs_ws/src/rmcs_core/src/referee/app/ui/deformable_infantry_ui.cpp @@ -126,7 +126,7 @@ class DeformableInfantry std::round((2 * std::numbers::pi - angle) / std::numbers::pi * 180)); }; chassis_direction_indicator_.set_color(chassis_direction_indicator_color(chassis_mode)); - chassis_direction_indicator_.set_angle(to_referee_angle(*chassis_angle_), 30); + chassis_direction_indicator_.set_angle(to_referee_angle(0.0), 30); } static Shape::Color chassis_direction_indicator_color(rmcs_msgs::ChassisMode mode) { From 2e0034a08f525de6c5a633434dda9b1249fa9a2e Mon Sep 17 00:00:00 2001 From: qzhhhi Date: Fri, 22 May 2026 13:21:21 +0800 Subject: [PATCH 34/52] vt13 fixfixfix --- .../rmcs_core/src/hardware/device/dr16.hpp | 2 +- .../src/hardware/device/remote_control.hpp | 29 +-------- .../rmcs_core/src/hardware/device/vt13.hpp | 60 +------------------ 3 files changed, 3 insertions(+), 88 deletions(-) diff --git a/rmcs_ws/src/rmcs_core/src/hardware/device/dr16.hpp b/rmcs_ws/src/rmcs_core/src/hardware/device/dr16.hpp index 17711aa7..cd0a8fbc 100644 --- a/rmcs_ws/src/rmcs_core/src/hardware/device/dr16.hpp +++ b/rmcs_ws/src/rmcs_core/src/hardware/device/dr16.hpp @@ -221,4 +221,4 @@ class Dr16 { double mouse_wheel_ = 0.0; }; -} // namespace rmcs_core::hardware::device +} // namespace rmcs_core::hardware::device \ No newline at end of file diff --git a/rmcs_ws/src/rmcs_core/src/hardware/device/remote_control.hpp b/rmcs_ws/src/rmcs_core/src/hardware/device/remote_control.hpp index ebaa8809..236f3821 100644 --- a/rmcs_ws/src/rmcs_core/src/hardware/device/remote_control.hpp +++ b/rmcs_ws/src/rmcs_core/src/hardware/device/remote_control.hpp @@ -1,6 +1,5 @@ #pragma once -#include #include #include @@ -43,34 +42,10 @@ class RemoteControl { } void update() { -<<<<<<< HEAD - const bool vt13_fresh = vt13_.remote_control_fresh(vt13_timeout_); - - if (vt13_fresh && vt13_.mode_switch() == Vt13::ModeSwitch::kCine) { - *switch_right_output_ = rmcs_msgs::Switch::DOWN; - *switch_left_output_ = rmcs_msgs::Switch::DOWN; - } else { -======= if (dr16_.valid() || !vt13_.valid() || vt13_.mode_switch() == Vt13::ModeSwitch::kNormal) { ->>>>>>> 7a9bbcf6 (vt13 fixfixfixfix) *switch_right_output_ = dr16_.switch_right(); *switch_left_output_ = dr16_.switch_left(); -<<<<<<< HEAD - if (vt13_fresh && vt13_.mode_switch() == Vt13::ModeSwitch::kSport) { - *switch_right_output_ = rmcs_msgs::Switch::MIDDLE; - *switch_left_output_ = rmcs_msgs::Switch::MIDDLE; - *joystick_right_output_ = vt13_.joystick_right(); - *joystick_left_output_ = vt13_.joystick_left(); - - *mouse_velocity_output_ = vt13_.mouse_velocity(); - *mouse_wheel_output_ = vt13_.mouse_wheel(); - - *mouse_output_ = vt13_.mouse(); - *keyboard_output_ = vt13_.keyboard(); - } else { -======= ->>>>>>> 7a9bbcf6 (vt13 fixfixfixfix) *joystick_right_output_ = dr16_.joystick_right(); *joystick_left_output_ = dr16_.joystick_left(); @@ -110,8 +85,6 @@ class RemoteControl { } private: - static constexpr auto vt13_timeout_ = std::chrono::milliseconds{250}; - Dr16& dr16_; Vt13& vt13_; @@ -131,4 +104,4 @@ class RemoteControl { rmcs_executor::Component::OutputInterface keyboard_output_; }; -} // namespace rmcs_core::hardware::device +} // namespace rmcs_core::hardware::device \ No newline at end of file diff --git a/rmcs_ws/src/rmcs_core/src/hardware/device/vt13.hpp b/rmcs_ws/src/rmcs_core/src/hardware/device/vt13.hpp index 0e374a19..eca43528 100644 --- a/rmcs_ws/src/rmcs_core/src/hardware/device/vt13.hpp +++ b/rmcs_ws/src/rmcs_core/src/hardware/device/vt13.hpp @@ -1,12 +1,8 @@ #pragma once -<<<<<<< HEAD -#include -======= #include #include #include ->>>>>>> 7a9bbcf6 (vt13 fixfixfixfix) #include #include #include @@ -28,8 +24,6 @@ namespace rmcs_core::hardware::device { class Vt13 { public: - using Clock = std::chrono::steady_clock; - enum class ModeSwitch : uint8_t { kUnknown = 0, kCine = 1, @@ -49,12 +43,6 @@ class Vt13 { }, uart_data.size()); if (written != uart_data.size()) { -<<<<<<< HEAD - RCLCPP_WARN( - rclcpp::get_logger("vt13"), "VT13 input buffer overflow: dropped %zu of %zu bytes", - uart_data.size() - written, uart_data.size()); - faulted_.store(true, std::memory_order_relaxed); -======= const auto dropped = uart_data.size() - written; overflow_count_.fetch_add(1, std::memory_order_relaxed); overflow_dropped_bytes_.fetch_add(dropped, std::memory_order_relaxed); @@ -63,20 +51,11 @@ class Vt13 { logger_, "VT13 input buffer overflow: dropped %zu of %zu bytes", dropped, uart_data.size()); } ->>>>>>> 7a9bbcf6 (vt13 fixfixfixfix) } } void update_status() { -<<<<<<< HEAD - if (faulted_.exchange(false, std::memory_order_relaxed)) { - invalidate_remote_control(); - return; - } - -======= const auto now = Clock::now(); ->>>>>>> 7a9bbcf6 (vt13 fixfixfixfix) auto readable = data_buffer_.readable(); peak_readable_ = std::max(peak_readable_, readable); @@ -101,15 +80,10 @@ class Vt13 { break; } if (std::holds_alternative(result)) { -<<<<<<< HEAD - invalidate_remote_control(); - break; -======= verification_failures_++; data_buffer_.pop_front([](std::byte&&) noexcept {}); readable--; continue; ->>>>>>> 7a9bbcf6 (vt13 fixfixfixfix) } if (std::holds_alternative(result)) { readable -= std::get(result).read; @@ -132,12 +106,6 @@ class Vt13 { [[nodiscard]] rmcs_msgs::Mouse mouse() const noexcept { return mouse_; } [[nodiscard]] rmcs_msgs::Keyboard keyboard() const noexcept { return keyboard_; } - [[nodiscard]] bool remote_control_fresh(Clock::duration timeout) const noexcept { - if (faulted_.load(std::memory_order_relaxed)) - return false; - return last_remote_control_frame_time_ != Clock::time_point::min() - && Clock::now() - last_remote_control_frame_time_ <= timeout; - } private: using Clock = std::chrono::steady_clock; @@ -205,11 +173,6 @@ class Vt13 { }, sizeof(RemoteControlData)); -<<<<<<< HEAD - if (data.header != RemoteControlData::kHeaderMagic - || !rmcs_utility::dji_crc::verify_crc16(data) - || data.mode_switch > 2) -======= if (data.header != RemoteControlData::kHeaderMagic) { remote_bad_header_count_++; if (should_log_verification_failure(now)) { @@ -217,7 +180,6 @@ class Vt13 { logger_, "VT13 remote control header invalid: header=0x%04x readable=%zu", data.header, readable); } ->>>>>>> 7a9bbcf6 (vt13 fixfixfixfix) return VerificationFailed{}; } if (!rmcs_utility::dji_crc::verify_crc16(data)) { @@ -237,7 +199,6 @@ class Vt13 { } void update_remote_control_data(const RemoteControlData& data) { - last_remote_control_frame_time_ = Clock::now(); mode_switch_ = static_cast(data.mode_switch + 1); joystick_right_ = { @@ -282,10 +243,6 @@ class Vt13 { const std::size_t total_frame_size = sizeof(RefereeFrameHeader) + 2 + header.data_length + 2; -<<<<<<< HEAD - if (total_frame_size > data_buffer_.max_size()) - return VerificationFailed{}; -======= if (total_frame_size > kRefereeFrameMaxSize) { referee_oversize_count_++; if (should_log_verification_failure(now)) { @@ -295,7 +252,6 @@ class Vt13 { } return VerificationFailed{}; } ->>>>>>> 7a9bbcf6 (vt13 fixfixfixfix) if (readable < total_frame_size) return Incomplete{}; @@ -304,17 +260,6 @@ class Vt13 { return Success{total_frame_size}; } -<<<<<<< HEAD - void invalidate_remote_control() { - faulted_.store(false, std::memory_order_relaxed); - data_buffer_.clear(); - last_remote_control_frame_time_ = Clock::time_point::min(); - mode_switch_ = ModeSwitch::kUnknown; - joystick_left_.setZero(); - joystick_right_.setZero(); - mouse_velocity_.setZero(); - mouse_wheel_ = 0.0; -======= bool should_log_verification_failure(const TimePoint now) { if (last_verification_log_time_ != TimePoint::min() && now - last_verification_log_time_ < kVerificationLogInterval) @@ -390,7 +335,6 @@ class Vt13 { joystick_right_ = Eigen::Vector2d::Zero(); mouse_velocity_ = Eigen::Vector2d::Zero(); mouse_wheel_ = 0; ->>>>>>> 7a9bbcf6 (vt13 fixfixfixfix) mouse_ = rmcs_msgs::Mouse::zero(); keyboard_ = rmcs_msgs::Keyboard::zero(); } @@ -404,8 +348,6 @@ class Vt13 { rclcpp::Logger logger_ = rclcpp::get_logger("vt13"); rmcs_utility::RingBuffer data_buffer_{1024}; - std::atomic faulted_{false}; - Clock::time_point last_remote_control_frame_time_ = Clock::time_point::min(); std::atomic store_calls_{0}; std::atomic received_bytes_{0}; @@ -440,4 +382,4 @@ class Vt13 { rmcs_msgs::Keyboard keyboard_ = rmcs_msgs::Keyboard::zero(); }; -} // namespace rmcs_core::hardware::device +} // namespace rmcs_core::hardware::device \ No newline at end of file From 88f2e5193bca22557debfabe800882ce06e01510 Mon Sep 17 00:00:00 2001 From: ZGZ713912 Date: Fri, 22 May 2026 13:58:47 +0800 Subject: [PATCH 35/52] feat: simplify chassis direction indicator angle calculation --- .../rmcs_core/src/referee/app/ui/deformable_infantry_ui.cpp | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/rmcs_ws/src/rmcs_core/src/referee/app/ui/deformable_infantry_ui.cpp b/rmcs_ws/src/rmcs_core/src/referee/app/ui/deformable_infantry_ui.cpp index c6961758..79a59e5b 100644 --- a/rmcs_ws/src/rmcs_core/src/referee/app/ui/deformable_infantry_ui.cpp +++ b/rmcs_ws/src/rmcs_core/src/referee/app/ui/deformable_infantry_ui.cpp @@ -121,12 +121,8 @@ class DeformableInfantry void update_chassis_direction_indicator() { auto chassis_mode = *chassis_mode_; - auto to_referee_angle = [](double angle) { - return static_cast( - std::round((2 * std::numbers::pi - angle) / std::numbers::pi * 180)); - }; chassis_direction_indicator_.set_color(chassis_direction_indicator_color(chassis_mode)); - chassis_direction_indicator_.set_angle(to_referee_angle(0.0), 30); + chassis_direction_indicator_.set_angle(270, 30); } static Shape::Color chassis_direction_indicator_color(rmcs_msgs::ChassisMode mode) { From d454db35549599d696da789ae52127e2a8defdb2 Mon Sep 17 00:00:00 2001 From: ZGZ713912 Date: Fri, 22 May 2026 19:22:06 +0800 Subject: [PATCH 36/52] feat: update deformable infantry configuration and UI angle settings --- rmcs_ws/src/rmcs_bringup/config/deformable-infantry-omni.yaml | 2 +- rmcs_ws/src/rmcs_core/src/hardware/device/vt13.hpp | 4 ++-- .../rmcs_core/src/referee/app/ui/deformable_infantry_ui.cpp | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/rmcs_ws/src/rmcs_bringup/config/deformable-infantry-omni.yaml b/rmcs_ws/src/rmcs_bringup/config/deformable-infantry-omni.yaml index e3fa2fef..aed9a7d7 100644 --- a/rmcs_ws/src/rmcs_bringup/config/deformable-infantry-omni.yaml +++ b/rmcs_ws/src/rmcs_bringup/config/deformable-infantry-omni.yaml @@ -51,7 +51,7 @@ deformable_infantry: pitch_motor_zero_point: 6853 debug_log_supercap: false debug_log_wheel_motor: false - debug_log_deformable_joint_motor: true + debug_log_deformable_joint_motor: false chassis_controller: ros__parameters: diff --git a/rmcs_ws/src/rmcs_core/src/hardware/device/vt13.hpp b/rmcs_ws/src/rmcs_core/src/hardware/device/vt13.hpp index eca43528..5d9c4698 100644 --- a/rmcs_ws/src/rmcs_core/src/hardware/device/vt13.hpp +++ b/rmcs_ws/src/rmcs_core/src/hardware/device/vt13.hpp @@ -206,8 +206,8 @@ class Vt13 { -channel_to_double(static_cast(data.joystick_channel0)), }; joystick_left_ = { - channel_to_double(static_cast(data.joystick_channel3)), - -channel_to_double(static_cast(data.joystick_channel2)), + channel_to_double(static_cast(data.joystick_channel2)), + -channel_to_double(static_cast(data.joystick_channel3)), }; mouse_velocity_ = { diff --git a/rmcs_ws/src/rmcs_core/src/referee/app/ui/deformable_infantry_ui.cpp b/rmcs_ws/src/rmcs_core/src/referee/app/ui/deformable_infantry_ui.cpp index 79a59e5b..6213421b 100644 --- a/rmcs_ws/src/rmcs_core/src/referee/app/ui/deformable_infantry_ui.cpp +++ b/rmcs_ws/src/rmcs_core/src/referee/app/ui/deformable_infantry_ui.cpp @@ -122,7 +122,7 @@ class DeformableInfantry auto chassis_mode = *chassis_mode_; chassis_direction_indicator_.set_color(chassis_direction_indicator_color(chassis_mode)); - chassis_direction_indicator_.set_angle(270, 30); + chassis_direction_indicator_.set_angle(0, 30); } static Shape::Color chassis_direction_indicator_color(rmcs_msgs::ChassisMode mode) { From b49a30801ef14106dc8e66b3279e7067b7ac5b36 Mon Sep 17 00:00:00 2001 From: eye-on Date: Fri, 22 May 2026 19:39:50 +0800 Subject: [PATCH 37/52] feat:add heat controller --- .../config/deformable-infantry-omni.yaml | 7 ++- .../controller/chassis/deformable_chassis.cpp | 4 +- .../shooting/friction_wheel_controller.cpp | 53 ++++++++++++++++++- rmcs_ws/src/serial | 1 + 4 files changed, 61 insertions(+), 4 deletions(-) create mode 160000 rmcs_ws/src/serial diff --git a/rmcs_ws/src/rmcs_bringup/config/deformable-infantry-omni.yaml b/rmcs_ws/src/rmcs_bringup/config/deformable-infantry-omni.yaml index aed9a7d7..4f9a3df6 100644 --- a/rmcs_ws/src/rmcs_bringup/config/deformable-infantry-omni.yaml +++ b/rmcs_ws/src/rmcs_bringup/config/deformable-infantry-omni.yaml @@ -58,6 +58,7 @@ chassis_controller: # Deploy geometry / chassis-owned joint intent min_angle: 8.0 max_angle: 58.0 + launch_ramp_shortcut_enabled: false active_suspension_enable: true spin_ratio: 1.0 @@ -120,8 +121,10 @@ friction_wheel_controller: - /gimbal/left_friction - /gimbal/right_friction friction_velocities: - - 600.0 - - 600.0 + - 575.0 + - 575.0 + friction_velocity_min: 550.0 + friction_velocity_max: 600.0 friction_soft_start_stop_time: 1.0 heat_controller: diff --git a/rmcs_ws/src/rmcs_core/src/controller/chassis/deformable_chassis.cpp b/rmcs_ws/src/rmcs_core/src/controller/chassis/deformable_chassis.cpp index 8e82b832..cb6b93d4 100644 --- a/rmcs_ws/src/rmcs_core/src/controller/chassis/deformable_chassis.cpp +++ b/rmcs_ws/src/rmcs_core/src/controller/chassis/deformable_chassis.cpp @@ -61,6 +61,7 @@ class DeformableChassis rclcpp::NodeOptions{}.automatically_declare_parameters_from_overrides(true)) , following_velocity_controller_(10.0, 0.0, 0.0) , spin_ratio_(std::clamp(get_parameter_or("spin_ratio", 0.6), 0.0, 1.0)) + , launch_ramp_shortcut_enabled_(get_parameter_or("launch_ramp_shortcut_enabled", true)) , min_angle_(get_parameter_or("min_angle", 7.0)) , max_angle_(get_parameter_or("max_angle", 58.0)) @@ -331,7 +332,7 @@ class DeformableChassis mode = rmcs_msgs::ChassisMode::SPIN; spinning_forward_ = !spinning_forward_; } - } else if (!last_keyboard_.x && keyboard.x) { + } else if (launch_ramp_shortcut_enabled_ && !last_keyboard_.x && keyboard.x) { deactivate_complex_spin_(); deactivate_qe_complex_spin_(); mode = mode == rmcs_msgs::ChassisMode::LAUNCH_RAMP @@ -989,6 +990,7 @@ class DeformableChassis double qe_last_toggle_elapsed_ = 0.0; pid::PidCalculator following_velocity_controller_; const double spin_ratio_; + const bool launch_ramp_shortcut_enabled_; InputInterface left_front_joint_physical_angle_; InputInterface left_back_joint_physical_angle_; diff --git a/rmcs_ws/src/rmcs_core/src/controller/shooting/friction_wheel_controller.cpp b/rmcs_ws/src/rmcs_core/src/controller/shooting/friction_wheel_controller.cpp index a83dc5f4..d3470eb3 100644 --- a/rmcs_ws/src/rmcs_core/src/controller/shooting/friction_wheel_controller.cpp +++ b/rmcs_ws/src/rmcs_core/src/controller/shooting/friction_wheel_controller.cpp @@ -31,6 +31,7 @@ class FrictionWheelController register_input("/remote/switch/right", switch_right_); register_input("/remote/switch/left", switch_left_); register_input("/remote/keyboard", keyboard_); + register_input("/remote/mouse/mouse_wheel", mouse_wheel_); auto friction_wheels = get_parameter("friction_wheels").as_string_array(); auto friction_working_velocities = get_parameter("friction_velocities").as_double_array(); @@ -53,6 +54,36 @@ class FrictionWheelController friction_wheels[i] + "/control_velocity", friction_control_velocities_[i], nan_); } + const bool has_adjustable_min = has_parameter("friction_velocity_min"); + const bool has_adjustable_max = has_parameter("friction_velocity_max"); + if (has_adjustable_min != has_adjustable_max) + throw std::runtime_error( + "Parameter mismatch: 'friction_velocity_min' and 'friction_velocity_max' must be " + "provided together."); + + friction_velocity_adjustment_enabled_ = has_adjustable_min; + if (friction_velocity_adjustment_enabled_) { + friction_velocity_min_ = get_parameter("friction_velocity_min").as_double(); + friction_velocity_max_ = get_parameter("friction_velocity_max").as_double(); + if (friction_velocity_min_ > friction_velocity_max_) + throw std::runtime_error( + "Invalid friction velocity range: 'friction_velocity_min' must be less than " + "or equal to 'friction_velocity_max'."); + + friction_adjustable_velocity_ = friction_working_velocities_[0]; + for (size_t i = 1; i < friction_count_; i++) { + if (std::abs(friction_working_velocities_[i] - friction_adjustable_velocity_) > 1e-6) + throw std::runtime_error( + "Adjustable friction velocity requires all 'friction_velocities' to be " + "identical."); + } + + if (friction_adjustable_velocity_ < friction_velocity_min_ + || friction_adjustable_velocity_ > friction_velocity_max_) + throw std::runtime_error( + "Initial friction velocity is outside the configured adjustable range."); + } + friction_soft_start_stop_step_ = (1 / 1000.0) / get_parameter("friction_soft_start_stop_time").as_double(); @@ -65,6 +96,7 @@ class FrictionWheelController const auto switch_right = *switch_right_; const auto switch_left = *switch_left_; const auto keyboard = *keyboard_; + const auto mouse_wheel = *mouse_wheel_; using namespace rmcs_msgs; if ((switch_left == Switch::UNKNOWN || switch_right == Switch::UNKNOWN) @@ -74,6 +106,8 @@ class FrictionWheelController } if (switch_right != Switch::DOWN) { + update_adjustable_friction_velocity(keyboard, mouse_wheel); + if ((!last_keyboard_.v && keyboard.v) || (last_switch_left_ == Switch::MIDDLE && switch_left == Switch::UP)) { friction_enabled_ = !friction_enabled_; @@ -107,6 +141,18 @@ class FrictionWheelController *friction_ready_ = *friction_jammed_ = *bullet_fired_ = false; } + void update_adjustable_friction_velocity(const rmcs_msgs::Keyboard& keyboard, double mouse_wheel) { + if (!friction_velocity_adjustment_enabled_ || !keyboard.x) + return; + + friction_adjustable_velocity_ += mouse_wheel * 5.0 * 0.001; + friction_adjustable_velocity_ = + std::clamp(friction_adjustable_velocity_, friction_velocity_min_, friction_velocity_max_); + + for (size_t i = 0; i < friction_count_; i++) + friction_working_velocities_[i] = friction_adjustable_velocity_; + } + void update_friction_velocities() { if (std::isnan(friction_soft_start_stop_percentage_)) { friction_soft_start_stop_percentage_ = 0.0; @@ -187,6 +233,7 @@ class FrictionWheelController InputInterface switch_right_; InputInterface switch_left_; InputInterface keyboard_; + InputInterface mouse_wheel_; rmcs_msgs::Switch last_switch_right_ = rmcs_msgs::Switch::UNKNOWN; rmcs_msgs::Switch last_switch_left_ = rmcs_msgs::Switch::UNKNOWN; @@ -199,6 +246,10 @@ class FrictionWheelController std::unique_ptr[]> friction_velocities_; bool friction_enabled_ = false; + bool friction_velocity_adjustment_enabled_ = false; + double friction_adjustable_velocity_ = 0.0; + double friction_velocity_min_ = 0.0; + double friction_velocity_max_ = 0.0; double friction_soft_start_stop_step_; double friction_soft_start_stop_percentage_ = nan_; @@ -219,4 +270,4 @@ class FrictionWheelController #include PLUGINLIB_EXPORT_CLASS( - rmcs_core::controller::shooting::FrictionWheelController, rmcs_executor::Component) \ No newline at end of file + rmcs_core::controller::shooting::FrictionWheelController, rmcs_executor::Component) diff --git a/rmcs_ws/src/serial b/rmcs_ws/src/serial new file mode 160000 index 00000000..d50c4018 --- /dev/null +++ b/rmcs_ws/src/serial @@ -0,0 +1 @@ +Subproject commit d50c401809f17cc5862bdbe37cc6e15737ba7c49 From f913c3226d622991502d963632a3d07388b97c62 Mon Sep 17 00:00:00 2001 From: eye-on Date: Fri, 22 May 2026 21:15:47 +0800 Subject: [PATCH 38/52] feat:add info logger --- .codex | 0 rmcs_ws/src/rmcs_auto_aim_v2 | 2 +- .../src/controller/shooting/friction_wheel_controller.cpp | 4 ++++ rmcs_ws/src/serial | 1 - 4 files changed, 5 insertions(+), 2 deletions(-) create mode 100644 .codex delete mode 160000 rmcs_ws/src/serial diff --git a/.codex b/.codex new file mode 100644 index 00000000..e69de29b diff --git a/rmcs_ws/src/rmcs_auto_aim_v2 b/rmcs_ws/src/rmcs_auto_aim_v2 index 7fbe1bd4..50f0c914 160000 --- a/rmcs_ws/src/rmcs_auto_aim_v2 +++ b/rmcs_ws/src/rmcs_auto_aim_v2 @@ -1 +1 @@ -Subproject commit 7fbe1bd41659bae94d469f43715721313227f95b +Subproject commit 50f0c914430bb874c4207c253942991dc23d3ba5 diff --git a/rmcs_ws/src/rmcs_core/src/controller/shooting/friction_wheel_controller.cpp b/rmcs_ws/src/rmcs_core/src/controller/shooting/friction_wheel_controller.cpp index d3470eb3..1d7e5a9b 100644 --- a/rmcs_ws/src/rmcs_core/src/controller/shooting/friction_wheel_controller.cpp +++ b/rmcs_ws/src/rmcs_core/src/controller/shooting/friction_wheel_controller.cpp @@ -124,6 +124,10 @@ class FrictionWheelController last_switch_left_ = switch_left; last_keyboard_ = keyboard; } + + RCLCPP_INFO( + get_logger(), "current speed: %.2f, %.2f", + *friction_velocities_[0], *friction_velocities_[1]); } private: diff --git a/rmcs_ws/src/serial b/rmcs_ws/src/serial deleted file mode 160000 index d50c4018..00000000 --- a/rmcs_ws/src/serial +++ /dev/null @@ -1 +0,0 @@ -Subproject commit d50c401809f17cc5862bdbe37cc6e15737ba7c49 From e494a6cb4d73613c504db2ad4fb2ff263a3a2981 Mon Sep 17 00:00:00 2001 From: ZGZ713912 Date: Fri, 22 May 2026 21:25:02 +0800 Subject: [PATCH 39/52] feat: enhance friction wheel controller and UI with working velocity outputs --- rmcs_ws/src/rmcs_auto_aim_v2 | 2 +- .../shooting/friction_wheel_controller.cpp | 12 +++++++ .../controller/shooting/heat_controller.cpp | 2 +- .../referee/app/ui/deformable_infantry_ui.cpp | 32 +++++++++++++++++-- 4 files changed, 43 insertions(+), 5 deletions(-) diff --git a/rmcs_ws/src/rmcs_auto_aim_v2 b/rmcs_ws/src/rmcs_auto_aim_v2 index 50f0c914..7fbe1bd4 160000 --- a/rmcs_ws/src/rmcs_auto_aim_v2 +++ b/rmcs_ws/src/rmcs_auto_aim_v2 @@ -1 +1 @@ -Subproject commit 50f0c914430bb874c4207c253942991dc23d3ba5 +Subproject commit 7fbe1bd41659bae94d469f43715721313227f95b diff --git a/rmcs_ws/src/rmcs_core/src/controller/shooting/friction_wheel_controller.cpp b/rmcs_ws/src/rmcs_core/src/controller/shooting/friction_wheel_controller.cpp index 1d7e5a9b..050443e5 100644 --- a/rmcs_ws/src/rmcs_core/src/controller/shooting/friction_wheel_controller.cpp +++ b/rmcs_ws/src/rmcs_core/src/controller/shooting/friction_wheel_controller.cpp @@ -46,10 +46,15 @@ class FrictionWheelController friction_count_ = friction_wheels.size(); friction_working_velocities_ = std::make_unique(friction_count_); friction_velocities_ = std::make_unique[]>(friction_count_); + friction_working_velocity_outputs_ = + std::make_unique[]>(friction_count_); friction_control_velocities_ = std::make_unique[]>(friction_count_); for (size_t i = 0; i < friction_count_; i++) { friction_working_velocities_[i] = friction_working_velocities[i]; register_input(friction_wheels[i] + "/velocity", friction_velocities_[i]); + register_output( + friction_wheels[i] + "/working_velocity", friction_working_velocity_outputs_[i], + friction_working_velocities_[i]); register_output( friction_wheels[i] + "/control_velocity", friction_control_velocities_[i], nan_); } @@ -107,6 +112,7 @@ class FrictionWheelController if (switch_right != Switch::DOWN) { update_adjustable_friction_velocity(keyboard, mouse_wheel); + update_friction_working_velocity_outputs(); if ((!last_keyboard_.v && keyboard.v) || (last_switch_left_ == Switch::MIDDLE && switch_left == Switch::UP)) { @@ -157,6 +163,11 @@ class FrictionWheelController friction_working_velocities_[i] = friction_adjustable_velocity_; } + void update_friction_working_velocity_outputs() { + for (size_t i = 0; i < friction_count_; i++) + *friction_working_velocity_outputs_[i] = friction_working_velocities_[i]; + } + void update_friction_velocities() { if (std::isnan(friction_soft_start_stop_percentage_)) { friction_soft_start_stop_percentage_ = 0.0; @@ -248,6 +259,7 @@ class FrictionWheelController std::unique_ptr friction_working_velocities_; std::unique_ptr[]> friction_velocities_; + std::unique_ptr[]> friction_working_velocity_outputs_; bool friction_enabled_ = false; bool friction_velocity_adjustment_enabled_ = false; diff --git a/rmcs_ws/src/rmcs_core/src/controller/shooting/heat_controller.cpp b/rmcs_ws/src/rmcs_core/src/controller/shooting/heat_controller.cpp index 3de0960a..ce6c85f4 100644 --- a/rmcs_ws/src/rmcs_core/src/controller/shooting/heat_controller.cpp +++ b/rmcs_ws/src/rmcs_core/src/controller/shooting/heat_controller.cpp @@ -29,7 +29,7 @@ class HeatController shooter_heat_ = std::max(0, shooter_heat_ - *shooter_cooling_); if (*bullet_fired_) - shooter_heat_ += heat_per_shot + 10; + shooter_heat_ += heat_per_shot + *shooter_cooling_; *control_bullet_allowance_ = std::max( 0, (*shooter_heat_limit_ - shooter_heat_ - reserved_heat) / heat_per_shot); diff --git a/rmcs_ws/src/rmcs_core/src/referee/app/ui/deformable_infantry_ui.cpp b/rmcs_ws/src/rmcs_core/src/referee/app/ui/deformable_infantry_ui.cpp index 6213421b..d3a472ff 100644 --- a/rmcs_ws/src/rmcs_core/src/referee/app/ui/deformable_infantry_ui.cpp +++ b/rmcs_ws/src/rmcs_core/src/referee/app/ui/deformable_infantry_ui.cpp @@ -3,6 +3,7 @@ #include #include #include +#include #include #include @@ -34,6 +35,15 @@ class DeformableInfantry , vertical_center_guidelines_( {Shape::Color::WHITE, 2, x_center, 800, x_center, y_center + 110}, {Shape::Color::WHITE, 2, x_center, y_center - 110, x_center, 200}) + , friction_wheel_speed_indicator_( + Shape::Color::PINK, 20, 2, + static_cast(std::lround( + static_cast(x_center) + + friction_wheel_speed_indicator_radius_ / std::numbers::sqrt2)), + static_cast(std::lround( + static_cast(y_center) + - friction_wheel_speed_indicator_radius_ / std::numbers::sqrt2)), + 0) , chassis_direction_indicator_(Shape::Color::PINK, 8, x_center, y_center, 0, 0, 84, 84) , time_reminder_(Shape::Color::PINK, 50, 5, x_center + 150, y_center + 65, 0, false) { @@ -73,6 +83,7 @@ class DeformableInfantry register_input("/referee/shooter/bullet_allowance", robot_bullet_allowance_); + register_input("/gimbal/left_friction/working_velocity", left_friction_working_velocity_); register_input("/gimbal/left_friction/control_velocity", left_friction_control_velocity_); register_input("/gimbal/left_friction/velocity", left_friction_velocity_); register_input("/gimbal/right_friction/velocity", right_friction_velocity_); @@ -93,9 +104,21 @@ class DeformableInfantry update_ctrl_ui(); status_ring_.update_bullet_allowance(*robot_bullet_allowance_); - status_ring_.update_friction_wheel_speed( - std::min(*left_friction_velocity_, *right_friction_velocity_), - *left_friction_control_velocity_ > 0); + const double friction_wheel_speed = + std::min(*left_friction_velocity_, *right_friction_velocity_); + const bool friction_wheel_enabled = *left_friction_control_velocity_ > 0; + const auto friction_wheel_speed_value = + static_cast(std::lround(*left_friction_working_velocity_)); + status_ring_.update_friction_wheel_speed(friction_wheel_speed, friction_wheel_enabled); + if (friction_wheel_speed_indicator_.value() != friction_wheel_speed_value) { + friction_wheel_speed_indicator_.set_value(friction_wheel_speed_value); + friction_wheel_speed_indicator_.set_center_x( + static_cast(std::lround( + static_cast(x_center) + + friction_wheel_speed_indicator_radius_ / std::numbers::sqrt2))); + } + friction_wheel_speed_indicator_.set_color( + friction_wheel_enabled ? Shape::Color::GREEN : Shape::Color::PINK); status_ring_.update_supercap(*supercap_voltage_, *supercap_enabled_); status_ring_.update_battery_power(*chassis_voltage_); @@ -153,6 +176,7 @@ class DeformableInfantry static constexpr uint16_t screen_width = 1920, screen_height = 1080; static constexpr uint16_t x_center = screen_width / 2, y_center = screen_height / 2; + static constexpr double friction_wheel_speed_indicator_radius_ = 430.0; InputInterface timestamp_; InputInterface chassis_mode_; @@ -170,6 +194,7 @@ class DeformableInfantry InputInterface robot_bullet_allowance_; + InputInterface left_friction_working_velocity_; InputInterface left_friction_control_velocity_; InputInterface left_friction_velocity_; InputInterface right_friction_velocity_; @@ -184,6 +209,7 @@ class DeformableInfantry Line horizontal_center_guidelines_[2]; Line vertical_center_guidelines_[2]; + Integer friction_wheel_speed_indicator_; Arc chassis_direction_indicator_; DeformableChassisLegArcs deformable_chassis_leg_arcs_; From 49890c8cc476af52ea939cc44442fa6cb3ce3a63 Mon Sep 17 00:00:00 2001 From: eye-on Date: Fri, 22 May 2026 21:54:57 +0800 Subject: [PATCH 40/52] feat;heat parameter fixed --- rmcs_ws/src/rmcs_auto_aim_v2 | 2 +- .../src/controller/shooting/friction_wheel_controller.cpp | 5 +---- 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/rmcs_ws/src/rmcs_auto_aim_v2 b/rmcs_ws/src/rmcs_auto_aim_v2 index 7fbe1bd4..50f0c914 160000 --- a/rmcs_ws/src/rmcs_auto_aim_v2 +++ b/rmcs_ws/src/rmcs_auto_aim_v2 @@ -1 +1 @@ -Subproject commit 7fbe1bd41659bae94d469f43715721313227f95b +Subproject commit 50f0c914430bb874c4207c253942991dc23d3ba5 diff --git a/rmcs_ws/src/rmcs_core/src/controller/shooting/friction_wheel_controller.cpp b/rmcs_ws/src/rmcs_core/src/controller/shooting/friction_wheel_controller.cpp index 050443e5..a8125c0d 100644 --- a/rmcs_ws/src/rmcs_core/src/controller/shooting/friction_wheel_controller.cpp +++ b/rmcs_ws/src/rmcs_core/src/controller/shooting/friction_wheel_controller.cpp @@ -131,9 +131,6 @@ class FrictionWheelController last_keyboard_ = keyboard; } - RCLCPP_INFO( - get_logger(), "current speed: %.2f, %.2f", - *friction_velocities_[0], *friction_velocities_[1]); } private: @@ -155,7 +152,7 @@ class FrictionWheelController if (!friction_velocity_adjustment_enabled_ || !keyboard.x) return; - friction_adjustable_velocity_ += mouse_wheel * 5.0 * 0.001; + friction_adjustable_velocity_ += mouse_wheel * 5.0 * 10; friction_adjustable_velocity_ = std::clamp(friction_adjustable_velocity_, friction_velocity_min_, friction_velocity_max_); From 09fd6f2ec134a17c5210e90773e2af90330c4779 Mon Sep 17 00:00:00 2001 From: ZGZ713912 Date: Sat, 23 May 2026 00:27:49 +0800 Subject: [PATCH 41/52] feat: update heat controller parameters and adjust friction wheel speed indicator logic --- rmcs_ws/src/rmcs_auto_aim_v2 | 2 +- .../config/deformable-infantry-omni-b.yaml | 2 +- .../config/deformable-infantry-omni.yaml | 2 +- .../config/deformable-infantry-steering.yaml | 2 +- .../chassis/chassis_power_controller.cpp | 2 +- .../shooting/friction_wheel_controller.cpp | 2 +- .../controller/shooting/heat_controller.cpp | 2 +- .../referee/app/ui/deformable_infantry_ui.cpp | 31 ++++++++++++------- 8 files changed, 26 insertions(+), 19 deletions(-) diff --git a/rmcs_ws/src/rmcs_auto_aim_v2 b/rmcs_ws/src/rmcs_auto_aim_v2 index 50f0c914..7fbe1bd4 160000 --- a/rmcs_ws/src/rmcs_auto_aim_v2 +++ b/rmcs_ws/src/rmcs_auto_aim_v2 @@ -1 +1 @@ -Subproject commit 50f0c914430bb874c4207c253942991dc23d3ba5 +Subproject commit 7fbe1bd41659bae94d469f43715721313227f95b diff --git a/rmcs_ws/src/rmcs_bringup/config/deformable-infantry-omni-b.yaml b/rmcs_ws/src/rmcs_bringup/config/deformable-infantry-omni-b.yaml index 817e0a62..77aff1ce 100644 --- a/rmcs_ws/src/rmcs_bringup/config/deformable-infantry-omni-b.yaml +++ b/rmcs_ws/src/rmcs_bringup/config/deformable-infantry-omni-b.yaml @@ -128,7 +128,7 @@ friction_wheel_controller: heat_controller: ros__parameters: heat_per_shot: 10000 - reserved_heat: 0 + reserved_heat: 2000 bullet_feeder_controller: ros__parameters: diff --git a/rmcs_ws/src/rmcs_bringup/config/deformable-infantry-omni.yaml b/rmcs_ws/src/rmcs_bringup/config/deformable-infantry-omni.yaml index 4f9a3df6..aea37ad7 100644 --- a/rmcs_ws/src/rmcs_bringup/config/deformable-infantry-omni.yaml +++ b/rmcs_ws/src/rmcs_bringup/config/deformable-infantry-omni.yaml @@ -130,7 +130,7 @@ friction_wheel_controller: heat_controller: ros__parameters: heat_per_shot: 10000 - reserved_heat: 0 + reserved_heat: 2000 bullet_feeder_controller: ros__parameters: diff --git a/rmcs_ws/src/rmcs_bringup/config/deformable-infantry-steering.yaml b/rmcs_ws/src/rmcs_bringup/config/deformable-infantry-steering.yaml index 794c623b..86fc0539 100644 --- a/rmcs_ws/src/rmcs_bringup/config/deformable-infantry-steering.yaml +++ b/rmcs_ws/src/rmcs_bringup/config/deformable-infantry-steering.yaml @@ -122,7 +122,7 @@ friction_wheel_controller: heat_controller: ros__parameters: heat_per_shot: 10000 - reserved_heat: 0 + reserved_heat: 2000 bullet_feeder_controller: ros__parameters: diff --git a/rmcs_ws/src/rmcs_core/src/controller/chassis/chassis_power_controller.cpp b/rmcs_ws/src/rmcs_core/src/controller/chassis/chassis_power_controller.cpp index 543750e6..0b80b0f9 100644 --- a/rmcs_ws/src/rmcs_core/src/controller/chassis/chassis_power_controller.cpp +++ b/rmcs_ws/src/rmcs_core/src/controller/chassis/chassis_power_controller.cpp @@ -126,7 +126,7 @@ class ChassisPowerController 0.0, 1.0); // Maximum excess power when virtual buffer energy is full. - constexpr double excess_power_limit = 15; + constexpr double excess_power_limit = 5.0; power_limit += excess_power_limit; power_limit *= virtual_buffer_energy_ / virtual_buffer_energy_limit_; diff --git a/rmcs_ws/src/rmcs_core/src/controller/shooting/friction_wheel_controller.cpp b/rmcs_ws/src/rmcs_core/src/controller/shooting/friction_wheel_controller.cpp index a8125c0d..3018b3f8 100644 --- a/rmcs_ws/src/rmcs_core/src/controller/shooting/friction_wheel_controller.cpp +++ b/rmcs_ws/src/rmcs_core/src/controller/shooting/friction_wheel_controller.cpp @@ -152,7 +152,7 @@ class FrictionWheelController if (!friction_velocity_adjustment_enabled_ || !keyboard.x) return; - friction_adjustable_velocity_ += mouse_wheel * 5.0 * 10; + friction_adjustable_velocity_ -= mouse_wheel * 5.0 * 10; friction_adjustable_velocity_ = std::clamp(friction_adjustable_velocity_, friction_velocity_min_, friction_velocity_max_); diff --git a/rmcs_ws/src/rmcs_core/src/controller/shooting/heat_controller.cpp b/rmcs_ws/src/rmcs_core/src/controller/shooting/heat_controller.cpp index ce6c85f4..3de0960a 100644 --- a/rmcs_ws/src/rmcs_core/src/controller/shooting/heat_controller.cpp +++ b/rmcs_ws/src/rmcs_core/src/controller/shooting/heat_controller.cpp @@ -29,7 +29,7 @@ class HeatController shooter_heat_ = std::max(0, shooter_heat_ - *shooter_cooling_); if (*bullet_fired_) - shooter_heat_ += heat_per_shot + *shooter_cooling_; + shooter_heat_ += heat_per_shot + 10; *control_bullet_allowance_ = std::max( 0, (*shooter_heat_limit_ - shooter_heat_ - reserved_heat) / heat_per_shot); diff --git a/rmcs_ws/src/rmcs_core/src/referee/app/ui/deformable_infantry_ui.cpp b/rmcs_ws/src/rmcs_core/src/referee/app/ui/deformable_infantry_ui.cpp index d3a472ff..686ca831 100644 --- a/rmcs_ws/src/rmcs_core/src/referee/app/ui/deformable_infantry_ui.cpp +++ b/rmcs_ws/src/rmcs_core/src/referee/app/ui/deformable_infantry_ui.cpp @@ -36,14 +36,8 @@ class DeformableInfantry {Shape::Color::WHITE, 2, x_center, 800, x_center, y_center + 110}, {Shape::Color::WHITE, 2, x_center, y_center - 110, x_center, 200}) , friction_wheel_speed_indicator_( - Shape::Color::PINK, 20, 2, - static_cast(std::lround( - static_cast(x_center) - + friction_wheel_speed_indicator_radius_ / std::numbers::sqrt2)), - static_cast(std::lround( - static_cast(y_center) - - friction_wheel_speed_indicator_radius_ / std::numbers::sqrt2)), - 0) + Shape::Color::PINK, friction_wheel_speed_indicator_font_size_, 2, 0, + friction_wheel_speed_indicator_y(), 0) , chassis_direction_indicator_(Shape::Color::PINK, 8, x_center, y_center, 0, 0, 84, 84) , time_reminder_(Shape::Color::PINK, 50, 5, x_center + 150, y_center + 65, 0, false) { @@ -93,6 +87,8 @@ class DeformableInfantry register_input("/referee/game/stage", game_stage_); + friction_wheel_speed_indicator_.set_center_x(friction_wheel_speed_indicator_center_x()); + crosshair_base_x_ = crosshair_circle_.x(); crosshair_base_y_ = crosshair_circle_.y(); ctrl_transition_.reset(false); @@ -112,10 +108,7 @@ class DeformableInfantry status_ring_.update_friction_wheel_speed(friction_wheel_speed, friction_wheel_enabled); if (friction_wheel_speed_indicator_.value() != friction_wheel_speed_value) { friction_wheel_speed_indicator_.set_value(friction_wheel_speed_value); - friction_wheel_speed_indicator_.set_center_x( - static_cast(std::lround( - static_cast(x_center) - + friction_wheel_speed_indicator_radius_ / std::numbers::sqrt2))); + friction_wheel_speed_indicator_.set_center_x(friction_wheel_speed_indicator_center_x()); } friction_wheel_speed_indicator_.set_color( friction_wheel_enabled ? Shape::Color::GREEN : Shape::Color::PINK); @@ -177,6 +170,20 @@ class DeformableInfantry static constexpr uint16_t screen_width = 1920, screen_height = 1080; static constexpr uint16_t x_center = screen_width / 2, y_center = screen_height / 2; static constexpr double friction_wheel_speed_indicator_radius_ = 430.0; + static constexpr uint16_t friction_wheel_speed_indicator_font_size_ = 20; + + static uint16_t friction_wheel_speed_indicator_center_x() { + return static_cast(std::lround( + static_cast(x_center) + + friction_wheel_speed_indicator_radius_ / std::numbers::sqrt2)); + } + + static uint16_t friction_wheel_speed_indicator_y() { + return static_cast(std::lround( + static_cast(y_center) + + friction_wheel_speed_indicator_radius_ / std::numbers::sqrt2 + - static_cast(friction_wheel_speed_indicator_font_size_) / 2.0)); + } InputInterface timestamp_; InputInterface chassis_mode_; From cf29bdc8af9cea87c020946a7f275f303d9588dd Mon Sep 17 00:00:00 2001 From: ZGZ713912 Date: Sat, 23 May 2026 09:22:59 +0800 Subject: [PATCH 42/52] feat: adjust status ring parameters for improved accuracy --- .../rmcs_core/src/referee/app/ui/deformable_infantry_ui.cpp | 2 +- .../src/rmcs_core/src/referee/app/ui/widget/status_ring.hpp | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/rmcs_ws/src/rmcs_core/src/referee/app/ui/deformable_infantry_ui.cpp b/rmcs_ws/src/rmcs_core/src/referee/app/ui/deformable_infantry_ui.cpp index 686ca831..ccc4ea48 100644 --- a/rmcs_ws/src/rmcs_core/src/referee/app/ui/deformable_infantry_ui.cpp +++ b/rmcs_ws/src/rmcs_core/src/referee/app/ui/deformable_infantry_ui.cpp @@ -28,7 +28,7 @@ class DeformableInfantry DeformableInfantry() : Node{get_component_name(), rclcpp::NodeOptions{}.automatically_declare_parameters_from_overrides(true)} , crosshair_circle_(Shape::Color::WHITE, x_center - 22, y_center - 42, 8, 2) - , status_ring_(26.5, 26.5, 600, 300) + , status_ring_(23.5, 26.5, 600, 300) , horizontal_center_guidelines_( {Shape::Color::WHITE, 2, x_center - 360, y_center, x_center - 110, y_center}, {Shape::Color::WHITE, 2, x_center + 110, y_center, x_center + 360, y_center}) diff --git a/rmcs_ws/src/rmcs_core/src/referee/app/ui/widget/status_ring.hpp b/rmcs_ws/src/rmcs_core/src/referee/app/ui/widget/status_ring.hpp index c00e5f59..1f3ed26e 100644 --- a/rmcs_ws/src/rmcs_core/src/referee/app/ui/widget/status_ring.hpp +++ b/rmcs_ws/src/rmcs_core/src/referee/app/ui/widget/status_ring.hpp @@ -246,10 +246,10 @@ class StatusRing { } void update_supercap(double value, bool enable) { - auto angle = 275 + calculate_angle(value, 10.5, supercap_limit_) + 1; + auto angle = 275 + calculate_angle(value, 8.5, supercap_limit_) + 1; supercap_status_.set_angle_end(static_cast(angle)); - if (value > 22.6) { + if (value > 20.0) { supercap_status_.set_color(enable ? Shape::Color::CYAN : Shape::Color::GREEN); } else if (value > 13.5) { supercap_status_.set_color(enable ? Shape::Color::YELLOW : Shape::Color::ORANGE); From c09ea57098c4c885a9d09632ec46250578992f73 Mon Sep 17 00:00:00 2001 From: ZGZ713912 Date: Sat, 23 May 2026 10:30:30 +0800 Subject: [PATCH 43/52] feat: increase excess power limit for virtual buffer energy --- .../src/controller/chassis/chassis_power_controller.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rmcs_ws/src/rmcs_core/src/controller/chassis/chassis_power_controller.cpp b/rmcs_ws/src/rmcs_core/src/controller/chassis/chassis_power_controller.cpp index 0b80b0f9..06229d18 100644 --- a/rmcs_ws/src/rmcs_core/src/controller/chassis/chassis_power_controller.cpp +++ b/rmcs_ws/src/rmcs_core/src/controller/chassis/chassis_power_controller.cpp @@ -126,7 +126,7 @@ class ChassisPowerController 0.0, 1.0); // Maximum excess power when virtual buffer energy is full. - constexpr double excess_power_limit = 5.0; + constexpr double excess_power_limit = 15.0; power_limit += excess_power_limit; power_limit *= virtual_buffer_energy_ / virtual_buffer_energy_limit_; From 78b2cc44d9ca14cacf0c795197ef39ec9b430d1d Mon Sep 17 00:00:00 2001 From: ZGZ713912 Date: Sat, 23 May 2026 10:56:11 +0800 Subject: [PATCH 44/52] feat: increase reserved heat parameter for heat controller and adjust status ring parameters --- rmcs_ws/src/rmcs_bringup/config/deformable-infantry-omni-b.yaml | 2 +- rmcs_ws/src/rmcs_bringup/config/deformable-infantry-omni.yaml | 2 +- .../src/rmcs_bringup/config/deformable-infantry-steering.yaml | 2 +- .../src/rmcs_core/src/referee/app/ui/deformable_infantry_ui.cpp | 2 +- rmcs_ws/src/rmcs_core/src/referee/app/ui/widget/status_ring.hpp | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-) diff --git a/rmcs_ws/src/rmcs_bringup/config/deformable-infantry-omni-b.yaml b/rmcs_ws/src/rmcs_bringup/config/deformable-infantry-omni-b.yaml index 77aff1ce..87e47ffa 100644 --- a/rmcs_ws/src/rmcs_bringup/config/deformable-infantry-omni-b.yaml +++ b/rmcs_ws/src/rmcs_bringup/config/deformable-infantry-omni-b.yaml @@ -128,7 +128,7 @@ friction_wheel_controller: heat_controller: ros__parameters: heat_per_shot: 10000 - reserved_heat: 2000 + reserved_heat: 5000 bullet_feeder_controller: ros__parameters: diff --git a/rmcs_ws/src/rmcs_bringup/config/deformable-infantry-omni.yaml b/rmcs_ws/src/rmcs_bringup/config/deformable-infantry-omni.yaml index aea37ad7..47248ccd 100644 --- a/rmcs_ws/src/rmcs_bringup/config/deformable-infantry-omni.yaml +++ b/rmcs_ws/src/rmcs_bringup/config/deformable-infantry-omni.yaml @@ -130,7 +130,7 @@ friction_wheel_controller: heat_controller: ros__parameters: heat_per_shot: 10000 - reserved_heat: 2000 + reserved_heat: 5000 bullet_feeder_controller: ros__parameters: diff --git a/rmcs_ws/src/rmcs_bringup/config/deformable-infantry-steering.yaml b/rmcs_ws/src/rmcs_bringup/config/deformable-infantry-steering.yaml index 86fc0539..5c07258b 100644 --- a/rmcs_ws/src/rmcs_bringup/config/deformable-infantry-steering.yaml +++ b/rmcs_ws/src/rmcs_bringup/config/deformable-infantry-steering.yaml @@ -122,7 +122,7 @@ friction_wheel_controller: heat_controller: ros__parameters: heat_per_shot: 10000 - reserved_heat: 2000 + reserved_heat: 5000 bullet_feeder_controller: ros__parameters: diff --git a/rmcs_ws/src/rmcs_core/src/referee/app/ui/deformable_infantry_ui.cpp b/rmcs_ws/src/rmcs_core/src/referee/app/ui/deformable_infantry_ui.cpp index ccc4ea48..6f09cb3c 100644 --- a/rmcs_ws/src/rmcs_core/src/referee/app/ui/deformable_infantry_ui.cpp +++ b/rmcs_ws/src/rmcs_core/src/referee/app/ui/deformable_infantry_ui.cpp @@ -28,7 +28,7 @@ class DeformableInfantry DeformableInfantry() : Node{get_component_name(), rclcpp::NodeOptions{}.automatically_declare_parameters_from_overrides(true)} , crosshair_circle_(Shape::Color::WHITE, x_center - 22, y_center - 42, 8, 2) - , status_ring_(23.5, 26.5, 600, 300) + , status_ring_(24.0, 26.5, 600, 300) , horizontal_center_guidelines_( {Shape::Color::WHITE, 2, x_center - 360, y_center, x_center - 110, y_center}, {Shape::Color::WHITE, 2, x_center + 110, y_center, x_center + 360, y_center}) diff --git a/rmcs_ws/src/rmcs_core/src/referee/app/ui/widget/status_ring.hpp b/rmcs_ws/src/rmcs_core/src/referee/app/ui/widget/status_ring.hpp index 1f3ed26e..48e5499a 100644 --- a/rmcs_ws/src/rmcs_core/src/referee/app/ui/widget/status_ring.hpp +++ b/rmcs_ws/src/rmcs_core/src/referee/app/ui/widget/status_ring.hpp @@ -246,7 +246,7 @@ class StatusRing { } void update_supercap(double value, bool enable) { - auto angle = 275 + calculate_angle(value, 8.5, supercap_limit_) + 1; + auto angle = 275 + calculate_angle(value, 8.0, supercap_limit_) + 1; supercap_status_.set_angle_end(static_cast(angle)); if (value > 20.0) { From 2fb483bc5a225dfb35964f1d8a66b16003cc37c2 Mon Sep 17 00:00:00 2001 From: ZGZ713912 Date: Sun, 24 May 2026 00:01:23 +0800 Subject: [PATCH 45/52] feat: adjust gimbal upper limit and friction wheel velocities for improved performance --- .../src/rmcs_bringup/config/deformable-infantry-omni-b.yaml | 6 +++--- .../src/rmcs_bringup/config/deformable-infantry-omni.yaml | 6 +++--- .../rmcs_bringup/config/deformable-infantry-steering.yaml | 6 +++--- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/rmcs_ws/src/rmcs_bringup/config/deformable-infantry-omni-b.yaml b/rmcs_ws/src/rmcs_bringup/config/deformable-infantry-omni-b.yaml index 87e47ffa..a71c2a98 100644 --- a/rmcs_ws/src/rmcs_bringup/config/deformable-infantry-omni-b.yaml +++ b/rmcs_ws/src/rmcs_bringup/config/deformable-infantry-omni-b.yaml @@ -86,7 +86,7 @@ chassis_controller: gimbal_controller: ros__parameters: - upper_limit: -0.80 # -35 deg + upper_limit: -0.65 # -35 deg lower_limit: 0.05 # 6 deg yaw_angle_kp: 30.0 @@ -121,8 +121,8 @@ friction_wheel_controller: - /gimbal/left_friction - /gimbal/right_friction friction_velocities: - - 600.0 - - 600.0 + - 580.0 + - 580.0 friction_soft_start_stop_time: 1.0 heat_controller: diff --git a/rmcs_ws/src/rmcs_bringup/config/deformable-infantry-omni.yaml b/rmcs_ws/src/rmcs_bringup/config/deformable-infantry-omni.yaml index 47248ccd..883f8ed1 100644 --- a/rmcs_ws/src/rmcs_bringup/config/deformable-infantry-omni.yaml +++ b/rmcs_ws/src/rmcs_bringup/config/deformable-infantry-omni.yaml @@ -86,7 +86,7 @@ chassis_controller: gimbal_controller: ros__parameters: - upper_limit: -0.80 # -35 deg + upper_limit: -0.65 # -35 deg lower_limit: 0.05 # 6 deg yaw_angle_kp: 30.0 @@ -121,8 +121,8 @@ friction_wheel_controller: - /gimbal/left_friction - /gimbal/right_friction friction_velocities: - - 575.0 - - 575.0 + - 580.0 + - 580.0 friction_velocity_min: 550.0 friction_velocity_max: 600.0 friction_soft_start_stop_time: 1.0 diff --git a/rmcs_ws/src/rmcs_bringup/config/deformable-infantry-steering.yaml b/rmcs_ws/src/rmcs_bringup/config/deformable-infantry-steering.yaml index 5c07258b..de50ece8 100644 --- a/rmcs_ws/src/rmcs_bringup/config/deformable-infantry-steering.yaml +++ b/rmcs_ws/src/rmcs_bringup/config/deformable-infantry-steering.yaml @@ -87,7 +87,7 @@ gimbal_controller: inertia: 1.0 # kg·m² friction: 1.65 # Nm/(rad/s) - upper_limit: -0.61 # -35 deg + upper_limit: -0.65 # -35 deg lower_limit: 0.05 # 6 deg use_encoder_pitch: true @@ -115,8 +115,8 @@ friction_wheel_controller: - /gimbal/left_friction - /gimbal/right_friction friction_velocities: - - 600.0 - - 600.0 + - 580.0 + - 580.0 friction_soft_start_stop_time: 1.0 heat_controller: From cd036d4ff0e636703c45585ff3509a8d17367e15 Mon Sep 17 00:00:00 2001 From: eye-on Date: Sun, 24 May 2026 00:25:10 +0800 Subject: [PATCH 46/52] feat:fixed supercup --- rmcs_ws/src/rmcs_auto_aim_v2 | 2 +- .../referee/app/ui/deformable_infantry_ui.cpp | 7 +++++- .../src/referee/app/ui/widget/status_ring.hpp | 25 ++++++++++++++++++- 3 files changed, 31 insertions(+), 3 deletions(-) diff --git a/rmcs_ws/src/rmcs_auto_aim_v2 b/rmcs_ws/src/rmcs_auto_aim_v2 index 7fbe1bd4..50f0c914 160000 --- a/rmcs_ws/src/rmcs_auto_aim_v2 +++ b/rmcs_ws/src/rmcs_auto_aim_v2 @@ -1 +1 @@ -Subproject commit 7fbe1bd41659bae94d469f43715721313227f95b +Subproject commit 50f0c914430bb874c4207c253942991dc23d3ba5 diff --git a/rmcs_ws/src/rmcs_core/src/referee/app/ui/deformable_infantry_ui.cpp b/rmcs_ws/src/rmcs_core/src/referee/app/ui/deformable_infantry_ui.cpp index 6f09cb3c..5babefe5 100644 --- a/rmcs_ws/src/rmcs_core/src/referee/app/ui/deformable_infantry_ui.cpp +++ b/rmcs_ws/src/rmcs_core/src/referee/app/ui/deformable_infantry_ui.cpp @@ -21,6 +21,10 @@ namespace rmcs_core::referee::app::ui { using namespace std::chrono_literals; +namespace { +constexpr double supercap_cutoff_voltage = 8.0; +} + class DeformableInfantry : public rmcs_executor::Component , public rclcpp::Node { @@ -112,7 +116,8 @@ class DeformableInfantry } friction_wheel_speed_indicator_.set_color( friction_wheel_enabled ? Shape::Color::GREEN : Shape::Color::PINK); - status_ring_.update_supercap(*supercap_voltage_, *supercap_enabled_); + status_ring_.update_supercap_energy( + *supercap_voltage_, *supercap_enabled_, supercap_cutoff_voltage); status_ring_.update_battery_power(*chassis_voltage_); status_ring_.update_auto_aim_enable(mouse_->right == 1); diff --git a/rmcs_ws/src/rmcs_core/src/referee/app/ui/widget/status_ring.hpp b/rmcs_ws/src/rmcs_core/src/referee/app/ui/widget/status_ring.hpp index 48e5499a..35882689 100644 --- a/rmcs_ws/src/rmcs_core/src/referee/app/ui/widget/status_ring.hpp +++ b/rmcs_ws/src/rmcs_core/src/referee/app/ui/widget/status_ring.hpp @@ -258,6 +258,19 @@ class StatusRing { } } + void update_supercap_energy(double value, bool enable, double cutoff_voltage) { + auto angle = 275 + calculate_energy_angle(value, cutoff_voltage, supercap_limit_) + 1; + supercap_status_.set_angle_end(static_cast(angle)); + + if (value > 20.0) { + supercap_status_.set_color(enable ? Shape::Color::CYAN : Shape::Color::GREEN); + } else if (value > 13.5) { + supercap_status_.set_color(enable ? Shape::Color::YELLOW : Shape::Color::ORANGE); + } else { + supercap_status_.set_color(enable ? Shape::Color::PURPLE : Shape::Color::PINK); + } + } + void update_battery_power(double value) { auto angle = 265 - calculate_angle(value, 20, 25.7) - 1; battery_status_.set_angle_start(static_cast(angle)); @@ -305,6 +318,16 @@ class StatusRing { return visible_angle * std::clamp(value - min, 0.0, max - min) / (max - min); } + static constexpr double calculate_energy_angle( + double value, double cutoff_voltage, double full_voltage) { + const double clamped_value = std::clamp(value, cutoff_voltage, full_voltage); + const double numerator = + clamped_value * clamped_value - cutoff_voltage * cutoff_voltage; + const double denominator = + full_voltage * full_voltage - cutoff_voltage * cutoff_voltage; + return visible_angle * numerator / denominator; + } + void set_limits( double supercap_limit, double battery_limit, double friction_limit, int16_t bullet_limit) { supercap_limit_ = supercap_limit; @@ -378,4 +401,4 @@ class StatusRing { Arc bullet_scales_[4]; }; -} // namespace rmcs_core::referee::app::ui \ No newline at end of file +} // namespace rmcs_core::referee::app::ui From 3f67501f4daa2c97c6567111ad56aa711c0f3f34 Mon Sep 17 00:00:00 2001 From: eye-on Date: Sun, 24 May 2026 01:29:26 +0800 Subject: [PATCH 47/52] feat:friction applied in omni-b --- .../src/rmcs_bringup/config/deformable-infantry-omni-b.yaml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/rmcs_ws/src/rmcs_bringup/config/deformable-infantry-omni-b.yaml b/rmcs_ws/src/rmcs_bringup/config/deformable-infantry-omni-b.yaml index a71c2a98..b9e8f985 100644 --- a/rmcs_ws/src/rmcs_bringup/config/deformable-infantry-omni-b.yaml +++ b/rmcs_ws/src/rmcs_bringup/config/deformable-infantry-omni-b.yaml @@ -59,6 +59,7 @@ chassis_controller: # Deploy geometry / chassis-owned joint intent min_angle: 8.0 max_angle: 58.0 + launch_ramp_shortcut_enabled: false active_suspension_enable: true spin_ratio: 1.0 @@ -123,6 +124,8 @@ friction_wheel_controller: friction_velocities: - 580.0 - 580.0 + friction_velocity_min: 550.0 + friction_velocity_max: 600.0 friction_soft_start_stop_time: 1.0 heat_controller: From 1e08647f5c4119204f53d54b6fbd5fd0619f71cc Mon Sep 17 00:00:00 2001 From: ZGZ713912 Date: Sun, 24 May 2026 03:33:37 +0800 Subject: [PATCH 48/52] feat:update virtual_buffer_energy_limit_ value --- .../src/controller/chassis/chassis_power_controller.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rmcs_ws/src/rmcs_core/src/controller/chassis/chassis_power_controller.cpp b/rmcs_ws/src/rmcs_core/src/controller/chassis/chassis_power_controller.cpp index 06229d18..1eee280c 100644 --- a/rmcs_ws/src/rmcs_core/src/controller/chassis/chassis_power_controller.cpp +++ b/rmcs_ws/src/rmcs_core/src/controller/chassis/chassis_power_controller.cpp @@ -150,7 +150,7 @@ class ChassisPowerController InputInterface rotary_knob_; InputInterface chassis_power_; - static constexpr double virtual_buffer_energy_limit_ = 30.0; + static constexpr double virtual_buffer_energy_limit_ = 15.0; double virtual_buffer_energy_; InputInterface supercap_voltage_; From 52b8e2db3e538d0ebcb164258f8b431056feff0c Mon Sep 17 00:00:00 2001 From: ZGZ713912 Date: Sun, 24 May 2026 07:55:53 +0800 Subject: [PATCH 49/52] calibrate yaw --- rmcs_ws/src/rmcs_bringup/config/deformable-infantry-omni.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rmcs_ws/src/rmcs_bringup/config/deformable-infantry-omni.yaml b/rmcs_ws/src/rmcs_bringup/config/deformable-infantry-omni.yaml index 883f8ed1..9317566a 100644 --- a/rmcs_ws/src/rmcs_bringup/config/deformable-infantry-omni.yaml +++ b/rmcs_ws/src/rmcs_bringup/config/deformable-infantry-omni.yaml @@ -47,7 +47,7 @@ deformable_infantry: left_back_zero_point: 5801 right_back_zero_point: 7817 right_front_zero_point: 7136 - yaw_motor_zero_point: 31517 + yaw_motor_zero_point: 26873 pitch_motor_zero_point: 6853 debug_log_supercap: false debug_log_wheel_motor: false From bb7f172fa2c8814f2d431156b3f2b75904c1d968 Mon Sep 17 00:00:00 2001 From: ZGZ713912 Date: Sun, 24 May 2026 07:58:40 +0800 Subject: [PATCH 50/52] calibrate yaw --- rmcs_ws/src/rmcs_bringup/config/deformable-infantry-omni.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rmcs_ws/src/rmcs_bringup/config/deformable-infantry-omni.yaml b/rmcs_ws/src/rmcs_bringup/config/deformable-infantry-omni.yaml index 9317566a..35b5eedb 100644 --- a/rmcs_ws/src/rmcs_bringup/config/deformable-infantry-omni.yaml +++ b/rmcs_ws/src/rmcs_bringup/config/deformable-infantry-omni.yaml @@ -47,7 +47,7 @@ deformable_infantry: left_back_zero_point: 5801 right_back_zero_point: 7817 right_front_zero_point: 7136 - yaw_motor_zero_point: 26873 + yaw_motor_zero_point: 43365 pitch_motor_zero_point: 6853 debug_log_supercap: false debug_log_wheel_motor: false From 83d616330b120f99fa52fc82d1573a2d6864d96a Mon Sep 17 00:00:00 2001 From: heyeuu <2829004293@qq.com> Date: Sun, 24 May 2026 11:05:06 +0800 Subject: [PATCH 51/52] feat: add armor priority --- rmcs_ws/src/rmcs_auto_aim_v2 | 2 +- .../bullet_feeder_controller_17mm.cpp | 30 +++++++------------ 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/rmcs_ws/src/rmcs_auto_aim_v2 b/rmcs_ws/src/rmcs_auto_aim_v2 index 50f0c914..feff0ed0 160000 --- a/rmcs_ws/src/rmcs_auto_aim_v2 +++ b/rmcs_ws/src/rmcs_auto_aim_v2 @@ -1 +1 @@ -Subproject commit 50f0c914430bb874c4207c253942991dc23d3ba5 +Subproject commit feff0ed06de3cec12df3df600e3df3e2f1066d60 diff --git a/rmcs_ws/src/rmcs_core/src/controller/shooting/bullet_feeder_controller_17mm.cpp b/rmcs_ws/src/rmcs_core/src/controller/shooting/bullet_feeder_controller_17mm.cpp index 49f41690..1c8f4d50 100644 --- a/rmcs_ws/src/rmcs_core/src/controller/shooting/bullet_feeder_controller_17mm.cpp +++ b/rmcs_ws/src/rmcs_core/src/controller/shooting/bullet_feeder_controller_17mm.cpp @@ -1,7 +1,3 @@ -#include - -#include - #include #include #include @@ -53,7 +49,7 @@ class BulletFeederController17mm register_input("/remote/mouse", mouse_); register_input("/remote/keyboard", keyboard_); - register_input("/auto_aim/should_shoot", fire_control_, false); + register_input("/auto_aim/should_shoot", should_shoot_, false); register_input("/gimbal/bullet_feeder/velocity", bullet_feeder_velocity_); register_output( @@ -63,8 +59,8 @@ class BulletFeederController17mm } void before_updating() override { - if (!fire_control_.ready()) - fire_control_.bind_directly(false); + if (!should_shoot_.ready()) + should_shoot_.bind_directly(false); } void update() override { @@ -80,19 +76,17 @@ class BulletFeederController17mm || (switch_left == Switch::DOWN && switch_right == Switch::DOWN)) { reset_all_controls(); } else { - int64_t bullet_allowance = 0; + std::int64_t bullet_allowance = 0; if (switch_right != Switch::DOWN) { shoot_mode = keyboard.f ? ShootMode::SINGLE : ShootMode::AUTOMATIC; - const bool auto_aim_requested = mouse.right || switch_right == Switch::UP; single_shot_stop_counter_ = std::max(0, single_shot_stop_counter_ - 1); temporary_single_shot_counter_ = std::max(0, temporary_single_shot_counter_ - 1); if (!last_mouse_.left && mouse.left) single_shot_stop_counter_ = single_shot_max_stop_delay_; - else if (!auto_aim_requested && last_switch_left_ != Switch::DOWN - && switch_left == Switch::DOWN) { + else if (last_switch_left_ != Switch::DOWN && switch_left == Switch::DOWN) { single_shot_stop_counter_ = single_shot_max_stop_delay_; temporary_single_shot_counter_ = 500; } @@ -104,16 +98,14 @@ class BulletFeederController17mm if (*friction_ready_) { if (shoot_mode == ShootMode::AUTOMATIC) { - const bool auto_aim_enabled = auto_aim_requested; - bool triggered = - (mouse.left && !mouse.right) || - (switch_left == Switch::DOWN && auto_aim_enabled && *fire_control_) || - (switch_left == Switch::DOWN && !auto_aim_enabled) || - (mouse.left && auto_aim_enabled && *fire_control_); + auto aiming_enable = mouse_->right || (switch_right == Switch::UP); + auto attack_intent = mouse_->left || (switch_left == Switch::DOWN); + auto triggered = + aiming_enable ? (*should_shoot_ && attack_intent) : attack_intent; bullet_allowance = triggered ? *control_bullet_allowance_limited_by_heat_ : 0; } else { - bool triggered = single_shot_stop_counter_ > 0; + auto triggered = single_shot_stop_counter_ > 0; bullet_allowance = triggered && (*control_bullet_allowance_limited_by_heat_ > 0); } @@ -215,7 +207,7 @@ class BulletFeederController17mm InputInterface mouse_; InputInterface keyboard_; - InputInterface fire_control_; + InputInterface should_shoot_; rmcs_msgs::Switch last_switch_right_ = rmcs_msgs::Switch::UNKNOWN; rmcs_msgs::Switch last_switch_left_ = rmcs_msgs::Switch::UNKNOWN; From 2fdad581d6b6d7690b11cbfd3485d3297bdabaf8 Mon Sep 17 00:00:00 2001 From: ZGZ713912 Date: Sun, 24 May 2026 18:58:13 +0800 Subject: [PATCH 52/52] feat: update reserved heat value for heat controller --- rmcs_ws/src/rmcs_auto_aim_v2 | 2 +- .../src/rmcs_bringup/config/deformable-infantry-omni-b.yaml | 2 +- .../src/rmcs_bringup/config/deformable-infantry-omni.yaml | 2 +- .../rmcs_core/src/referee/app/ui/deformable_infantry_ui.cpp | 6 +++--- 4 files changed, 6 insertions(+), 6 deletions(-) diff --git a/rmcs_ws/src/rmcs_auto_aim_v2 b/rmcs_ws/src/rmcs_auto_aim_v2 index feff0ed0..3da57e44 160000 --- a/rmcs_ws/src/rmcs_auto_aim_v2 +++ b/rmcs_ws/src/rmcs_auto_aim_v2 @@ -1 +1 @@ -Subproject commit feff0ed06de3cec12df3df600e3df3e2f1066d60 +Subproject commit 3da57e441bd8b620a7a7db7bcb2ed99dab22e2e3 diff --git a/rmcs_ws/src/rmcs_bringup/config/deformable-infantry-omni-b.yaml b/rmcs_ws/src/rmcs_bringup/config/deformable-infantry-omni-b.yaml index b9e8f985..8658473f 100644 --- a/rmcs_ws/src/rmcs_bringup/config/deformable-infantry-omni-b.yaml +++ b/rmcs_ws/src/rmcs_bringup/config/deformable-infantry-omni-b.yaml @@ -131,7 +131,7 @@ friction_wheel_controller: heat_controller: ros__parameters: heat_per_shot: 10000 - reserved_heat: 5000 + reserved_heat: 15000 bullet_feeder_controller: ros__parameters: diff --git a/rmcs_ws/src/rmcs_bringup/config/deformable-infantry-omni.yaml b/rmcs_ws/src/rmcs_bringup/config/deformable-infantry-omni.yaml index 35b5eedb..2c1103c4 100644 --- a/rmcs_ws/src/rmcs_bringup/config/deformable-infantry-omni.yaml +++ b/rmcs_ws/src/rmcs_bringup/config/deformable-infantry-omni.yaml @@ -130,7 +130,7 @@ friction_wheel_controller: heat_controller: ros__parameters: heat_per_shot: 10000 - reserved_heat: 5000 + reserved_heat: 15000 bullet_feeder_controller: ros__parameters: diff --git a/rmcs_ws/src/rmcs_core/src/referee/app/ui/deformable_infantry_ui.cpp b/rmcs_ws/src/rmcs_core/src/referee/app/ui/deformable_infantry_ui.cpp index 5babefe5..69a870a4 100644 --- a/rmcs_ws/src/rmcs_core/src/referee/app/ui/deformable_infantry_ui.cpp +++ b/rmcs_ws/src/rmcs_core/src/referee/app/ui/deformable_infantry_ui.cpp @@ -31,7 +31,7 @@ class DeformableInfantry public: DeformableInfantry() : Node{get_component_name(), rclcpp::NodeOptions{}.automatically_declare_parameters_from_overrides(true)} - , crosshair_circle_(Shape::Color::WHITE, x_center - 22, y_center - 42, 8, 2) + , crosshair_circle_(Shape::Color::WHITE, x_center - 26, y_center - 34, 8, 2) , status_ring_(24.0, 26.5, 600, 300) , horizontal_center_guidelines_( {Shape::Color::WHITE, 2, x_center - 360, y_center, x_center - 110, y_center}, @@ -129,9 +129,9 @@ class DeformableInfantry const double reveal = ctrl_transition_.update(*timestamp_, ctrl_active); crosshair_circle_.set_x(static_cast(std::lround( - static_cast(crosshair_base_x_) + 20.0 * reveal))); + static_cast(crosshair_base_x_) + 45.0 * reveal))); crosshair_circle_.set_y(static_cast(std::lround( - static_cast(crosshair_base_y_) - 20.0 * reveal))); + static_cast(crosshair_base_y_) + 20.0 * reveal))); } void update_time_reminder() {