ros2_control - rolling
Loading...
Searching...
No Matches
observation_formatter.hpp
1// Copyright 2025 ros2_control Development Team
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// Author: Julia Jia
16
17#ifndef ONNX_POLICY_CONTROLLER__OBSERVATION_FORMATTER_HPP_
18#define ONNX_POLICY_CONTROLLER__OBSERVATION_FORMATTER_HPP_
19
20#include <string>
21#include <vector>
22
23#include "control_msgs/msg/float64_values.hpp"
24#include "geometry_msgs/msg/twist.hpp"
25
26namespace onnx_policy_controller
27{
28
29class ObservationFormatter
30{
31public:
32 explicit ObservationFormatter(const std::vector<std::string> & joint_names);
33
34 // High-level observation API
35 // Order: gyro(3), accel(3), commands(7), joint_pos(N), joint_vel(N), last_action*3(N),
36 // motor_targets(N), feet(2), phase(2). Ref: v2_rl_walk_mujoco.py
37 std::vector<float> format(
38 const control_msgs::msg::Float64Values & interface_data,
39 const geometry_msgs::msg::Twist & velocity_cmd, const std::vector<double> & previous_action);
40 size_t get_observation_dim() const { return observation_dim_; }
41
42 // Sensor data utilities
43 std::vector<double> extract_joint_positions(
44 const control_msgs::msg::Float64Values & interface_data);
45 void set_interface_names(const std::vector<std::string> & interface_names);
46 void update_feet_contacts_from_interfaces(
47 const control_msgs::msg::Float64Values & msg, const std::string & left_contact_full_name,
48 const std::string & right_contact_full_name);
49
50 // Configuration / state setters
51 void set_default_joint_positions(const std::vector<double> & default_positions);
52 void set_motor_targets(const std::vector<double> & motor_targets);
53 void set_velocity_commands(const std::vector<double> & commands);
54 void update_action_history(const std::vector<double> & action);
60 void set_num_steps_in_gait_period(double num_steps);
61 void set_imu_upside_down(bool upside_down) { imu_upside_down_ = upside_down; }
62 void set_gyro_deadband(double deadband) { gyro_deadband_ = deadband; }
63 void update_imitation_phase(double phase_frequency_factor);
64 std::vector<double> get_imitation_phase() const { return imitation_phase_; }
65
66private:
67 void extract_interface_data(
68 const control_msgs::msg::Float64Values & msg, std::vector<double> & gyro,
69 std::vector<double> & accelero, std::vector<double> & joint_positions,
70 std::vector<double> & joint_velocities);
71
72 void format_joint_data(
73 const std::vector<double> & joint_positions, const std::vector<double> & joint_velocities,
74 std::vector<float> & observation);
75 void append_doubles_as_floats(const std::vector<double> & src, std::vector<float> & dst);
76
77 std::vector<std::string> joint_names_;
78 size_t num_joints_;
79 size_t observation_dim_; // 17 + 6*N
80 std::vector<double> default_joint_positions_;
81 bool default_joint_positions_set_;
82 bool imu_upside_down_;
83 double gyro_deadband_;
84 std::vector<std::string> interface_names_;
85 std::vector<double> last_action_;
86 std::vector<double> last_last_action_;
87 std::vector<double> last_last_last_action_;
88 std::vector<double> motor_targets_;
89 double left_contact_;
90 double right_contact_;
91 double imitation_i_;
92 double num_steps_in_gait_period_;
93 std::vector<double> imitation_phase_;
94 std::vector<double> velocity_commands_;
95};
96
97} // namespace onnx_policy_controller
98
99#endif // ONNX_POLICY_CONTROLLER__OBSERVATION_FORMATTER_HPP_
void set_num_steps_in_gait_period(double num_steps)
Set the number of control steps in one gait cycle used for the imitation phase.
Definition observation_formatter.cpp:289