ros2_control - humble
Loading...
Searching...
No Matches
mock_pendulum_hardware.hpp
1// Copyright 2026 Open Source Robotics Foundation, Inc.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#ifndef MOCK_PENDULUM_HARDWARE__MOCK_PENDULUM_HARDWARE_HPP_
16#define MOCK_PENDULUM_HARDWARE__MOCK_PENDULUM_HARDWARE_HPP_
17
18#include <memory>
19#include <random>
20#include <string>
21#include <vector>
22
23#include "hardware_interface/hardware_info.hpp"
24#include "hardware_interface/system_interface.hpp"
25#include "hardware_interface/types/hardware_interface_return_values.hpp"
26#include "rclcpp/clock.hpp"
27#include "rclcpp/macros.hpp"
28#include "rclcpp_lifecycle/node_interfaces/lifecycle_node_interface.hpp"
29#include "rclcpp_lifecycle/state.hpp"
30
31namespace mock_pendulum_hardware
32{
33
34using CallbackReturn = rclcpp_lifecycle::node_interfaces::LifecycleNodeInterface::CallbackReturn;
35
47{
48public:
49 RCLCPP_SHARED_PTR_DEFINITIONS(MockPendulumHardware)
50
51 CallbackReturn on_init(
52 const hardware_interface::HardwareComponentInterfaceParams & params) override;
53
54 std::vector<hardware_interface::StateInterface::ConstSharedPtr> on_export_state_interfaces()
55 override;
56 std::vector<hardware_interface::CommandInterface::SharedPtr> on_export_command_interfaces()
57 override;
58
59 CallbackReturn on_activate(const rclcpp_lifecycle::State & previous_state) override;
60 CallbackReturn on_deactivate(const rclcpp_lifecycle::State & previous_state) override;
61
62 hardware_interface::return_type read(
63 const rclcpp::Time & time, const rclcpp::Duration & period) override;
64 hardware_interface::return_type write(
65 const rclcpp::Time & time, const rclcpp::Duration & period) override;
66
67private:
68 // ---- parameter helpers -------------------------------------------------
69 double param(const std::string & name, double fallback) const;
70 bool param_bool(const std::string & name, bool fallback) const;
71
72 // ---- plant -------------------------------------------------------------
75 double pendulum_accel(double q2, double q2_dot, double q1_dot, double q1_ddot) const;
76
78 void integrate(double h);
79
82 void publish_states(double dt_sim);
83
84 static double wrap_pi(double a);
85
86 // ---- physical parameters (post randomisation) --------------------------
87 double J1_{6.10e-5};
88 double J2_pivot_{4.447e-5};
89 double J2_com_{8.06e-6};
90 double m2_{0.014};
91 double l2_{0.051};
92 double L1_{0.062};
93 double b1_{1.0e-4};
94 double b2_{8.0e-6};
95 double c2_{2.0e-6};
96 double g_{9.80665};
97
98 // nominal (pre-randomisation) copies, so re-activation re-rolls from nominal
99 double J1_nom_{0.0}, J2_com_nom_{0.0}, m2_nom_{0.0}, l2_nom_{0.0};
100 double b1_nom_{0.0}, b2_nom_{0.0}, c2_nom_{0.0};
101
102 // ---- actuator limits ---------------------------------------------------
103 double motor_limit_{2.35619449};
104 double motor_max_vel_{5.0};
105 double motor_max_accel_{200.0};
106 double motor_max_torque_{0.4};
107 bool model_step_loss_{true};
108 double stop_restitution_{0.0};
109
110 // ---- sensor model ------------------------------------------------------
111 double motor_steps_per_rev_{6400.0};
112 double encoder_counts_per_rev_{4096.0};
113 double encoder_noise_{0.0002};
114 double encoder_offset_{0.0};
115 bool pendulum_vel_from_encoder_{true};
116 double vel_filter_alpha_{0.95};
117 double motor_vel_noise_{0.001};
118 double report_offset_{0.0};
119
120 // ---- process noise -----------------------------------------------------
121 double pendulum_accel_noise_{
122 0.005};
123 double motor_accel_noise_{0.005};
124 double param_randomization_{0.005};
125 double initial_q2_{0.1};
126 double initial_q2_jitter_{0.002};
127
128 // ---- integration / timing ---------------------------------------------
129 double sim_speed_{1.0};
130 double max_substep_{5.0e-4};
131 double max_cycle_dt_{0.1};
132 int max_substeps_{4000};
133
134 // ---- true state --------------------------------------------------------
135 double q1_{0.0}, q1_dot_{0.0};
136 double q2_{0.0}, q2_dot_{0.0};
137 double q2_ddot_prev_{0.0};
138 double sim_time_{0.0};
139 bool at_limit_{false};
140 bool step_loss_{false};
141
142 // ---- measured state ----------------------------------------------------
143 double q2_meas_prev_{0.0};
144 double q2_dot_filt_{0.0};
145 bool have_prev_meas_{false};
146
147 std::vector<double> hw_states_;
148 std::vector<double> hw_commands_;
149
150 std::vector<hardware_interface::StateInterface::SharedPtr> state_interfaces_;
151 std::vector<hardware_interface::CommandInterface::SharedPtr> command_interfaces_;
152
153 rclcpp::Clock clock_{RCL_STEADY_TIME};
154
155 std::mt19937 rng_;
156 std::normal_distribution<double> normal_{0.0, 1.0};
157};
158
159} // namespace mock_pendulum_hardware
160
161#endif // MOCK_PENDULUM_HARDWARE__MOCK_PENDULUM_HARDWARE_HPP_
Virtual Class to implement when integrating a complex system into ros2_control.
Definition system_interface.hpp:85
Definition mock_pendulum_hardware.hpp:47
hardware_interface::return_type write(const rclcpp::Time &time, const rclcpp::Duration &period) override
Write the current command values to the actuator.
Definition mock_pendulum_hardware.cpp:449
hardware_interface::return_type read(const rclcpp::Time &time, const rclcpp::Duration &period) override
Read the current state values from the actuator.
Definition mock_pendulum_hardware.cpp:404