ros2_control - humble
Loading...
Searching...
No Matches
inverted_pendulum_controller.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 INVERTED_PENDULUM_CONTROLLER__INVERTED_PENDULUM_CONTROLLER_HPP_
16#define INVERTED_PENDULUM_CONTROLLER__INVERTED_PENDULUM_CONTROLLER_HPP_
17
18#include <string>
19
20#include "controller_interface/controller_interface.hpp"
21#include "rclcpp/rclcpp.hpp"
22#include "rclcpp_lifecycle/state.hpp"
23
24namespace inverted_pendulum_controller
25{
26
41{
42public:
44
45 controller_interface::CallbackReturn on_init() override;
46
49
50 controller_interface::CallbackReturn on_configure(
51 const rclcpp_lifecycle::State & previous_state) override;
52 controller_interface::CallbackReturn on_activate(
53 const rclcpp_lifecycle::State & previous_state) override;
54 controller_interface::CallbackReturn on_deactivate(
55 const rclcpp_lifecycle::State & previous_state) override;
56
57 controller_interface::return_type update(
58 const rclcpp::Time & time, const rclcpp::Duration & period) override;
59
61 bool is_balancing() const { return balancing_; }
62
63private:
64 // Hardware interface names
65 std::string motor_joint_state_name_;
66 std::string motor_joint_vel_name_;
67 std::string pendulum_joint_state_name_;
68 std::string pendulum_joint_vel_name_;
69 std::string motor_joint_command_name_;
70
71 // Physical plant constants
72 double m2_{0.014};
73 double l2_{0.051};
74 double L1_{0.062};
75 double J2_{4.447e-5};
76 double g_{9.80665};
77 double E_target_{0.0};
78
79 // Full-state LQR gains: u = -K * [q1, q1_dot, q2, q2_dot]
80 double k_q1_{-5.000};
81 double k_q1_vel_{-4.352};
82 double k_q2_{418.238};
83 double k_q2_vel_{33.281};
84
85 // Energy-shaping swing-up parameters
86 double k_e_{80.0};
87 double k_arm_{31.0};
88 double k_arm_vel_{6.0};
89 double swing_accel_max_{100.0};
90 double arm_zone_{2.0};
91 double swing_start_vel_{
92 0.01};
93
94 // Mode transition & gating parameters
95 double catch_angle_{0.20};
96 double fall_angle_{
97 0.60};
98 double catch_cmd_fraction_{
99 0.60};
100 int catch_ramp_steps_{10};
101 int catch_ramp_remaining_{0};
102
103 // Actuator safety limit
104 double motor_accel_max_{150.0};
105
106 // Operational state
107 bool balancing_{false};
108};
109
110} // namespace inverted_pendulum_controller
111
112#endif // INVERTED_PENDULUM_CONTROLLER__INVERTED_PENDULUM_CONTROLLER_HPP_
Definition controller_interface.hpp:29
Hybrid energy-shaping swing-up and LQR balance controller for a Furuta rotary inverted pendulum.
Definition inverted_pendulum_controller.hpp:41
bool is_balancing() const
Returns true if currently in LQR balancing regime.
Definition inverted_pendulum_controller.hpp:61
controller_interface::return_type update(const rclcpp::Time &time, const rclcpp::Duration &period) override
Definition inverted_pendulum_controller.cpp:248
controller_interface::InterfaceConfiguration state_interface_configuration() const override
Get configuration for controller's required state interfaces.
Definition inverted_pendulum_controller.cpp:129
controller_interface::CallbackReturn on_init() override
Extending interface with initialization method which is individual for each controller.
Definition inverted_pendulum_controller.cpp:70
controller_interface::InterfaceConfiguration command_interface_configuration() const override
Get configuration for controller's required command interfaces.
Definition inverted_pendulum_controller.cpp:120
Configuring what command/state interfaces to claim.
Definition controller_interface_base.hpp:56