Skip to content

Commit e51fc21

Browse files
authored
Integration weekend tunes (#521)
2 parents f4fb7a4 + 2f2bf77 commit e51fc21

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+571
-2563
lines changed

bitbots_motion/bitbots_dynup/CMakeLists.txt

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,13 @@ find_package(Python3 REQUIRED COMPONENTS Interpreter Development)
3737
generate_parameter_library(dynup_parameters config/dynup_config.yaml)
3838

3939
rosidl_generate_interfaces(
40-
${PROJECT_NAME} "msg/DynupEngineDebug.msg" "msg/DynupPoses.msg" DEPENDENCIES
41-
std_msgs geometry_msgs)
40+
${PROJECT_NAME}
41+
"msg/DynupEngineDebug.msg"
42+
"msg/DynupPoses.msg"
43+
"msg/DynupIkOffset.msg"
44+
DEPENDENCIES
45+
std_msgs
46+
geometry_msgs)
4247

4348
include_directories(include ${PYTHON_INCLUDE_DIRS})
4449

bitbots_motion/bitbots_dynup/bitbots_dynup_py/py_dynup_wrapper.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,10 @@ def special_reset(self, time: float):
4949
def set_engine_goal(self, direction):
5050
self.py_dynup_wrapper.set_engine_goal(direction)
5151

52-
def step(self, dt: float, imu_msg, jointstate_msg=None):
52+
def step(self, dt: float, imu_msg, joint_state_msg):
5353
if dt == 0.0:
5454
dt = 0.001
55-
step = self.py_dynup_wrapper.step(dt, self._to_cpp(imu_msg))
55+
step = self.py_dynup_wrapper.step(dt, self._to_cpp(imu_msg), self._to_cpp(joint_state_msg))
5656
result = self._from_cpp(step, JointCommand)
5757
return result
5858

bitbots_motion/bitbots_dynup/config/dynup_config.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,13 @@ bitbots_dynup:
2121
hand_walkready_pitch:
2222
type: double
2323
description: "End pose hand pitch. Depends on walkready of walking."
24-
default_value: 40.0
24+
default_value: 8.0
2525
validation:
2626
bounds<>: [0.0, 90.0]
2727
hand_walkready_height:
2828
type: double
2929
description: "End pose hand height. Depends on walkready of walking."
30-
default_value: -0.17
30+
default_value: -0.29
3131
validation:
3232
bounds<>: [-1.0, 1.0]
3333
trunk_height:
@@ -231,7 +231,7 @@ bitbots_dynup:
231231
rise_time:
232232
type: double
233233
description: "Time to rise in seconds"
234-
default_value: 1.0
234+
default_value: 2.0
235235
validation:
236236
bounds<>: [0.0, 10.0]
237237

bitbots_motion/bitbots_dynup/config/dynup_sim.yaml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ dynup:
33
engine:
44
end_pose:
55
arm_extended_length: 0.3
6-
hand_walkready_pitch: 34.2
7-
hand_walkready_height: -0.18
86
trunk_height: 0.4
97
trunk_pitch: 0.13
108
trunk_x_final: 0.0

bitbots_motion/bitbots_dynup/include/bitbots_dynup/dynup_ik.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include <tf2/convert.h>
77

88
#include <bitbots_splines/abstract_ik.hpp>
9+
#include <sensor_msgs/msg/joint_state.hpp>
910
#include <tf2_geometry_msgs/tf2_geometry_msgs.hpp>
1011

1112
#include "dynup_parameters.hpp"
@@ -20,6 +21,7 @@ class DynupIK : public bitbots_splines::AbstractIK<DynupResponse> {
2021
void reset() override;
2122
void setDirection(DynupDirection direction);
2223
moveit::core::RobotStatePtr get_goal_state();
24+
void set_joint_positions(sensor_msgs::msg::JointState::ConstSharedPtr joint_state);
2325

2426
private:
2527
rclcpp::Node::SharedPtr node_;
@@ -29,6 +31,7 @@ class DynupIK : public bitbots_splines::AbstractIK<DynupResponse> {
2931
moveit::core::JointModelGroup *r_arm_joints_group_;
3032
moveit::core::JointModelGroup *r_leg_joints_group_;
3133
moveit::core::RobotStatePtr goal_state_;
34+
sensor_msgs::msg::JointState::ConstSharedPtr joint_state_;
3235
DynupDirection direction_;
3336
};
3437

bitbots_motion/bitbots_dynup/include/bitbots_dynup/dynup_node.hpp

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -67,13 +67,14 @@ class DynupNode {
6767
/**
6868
* Retrieve current positions of left foot and trunk relative to right foot
6969
*
70-
* @return The pair of (right foot, left foot) poses if transformation was successfull
70+
* @return The pair of (right foot, left foot) poses if transformation was successful
7171
*/
7272
bitbots_dynup::msg::DynupPoses getCurrentPoses();
7373

7474
bitbots_msgs::msg::JointCommand step(double dt);
7575

76-
bitbots_msgs::msg::JointCommand step(double dt, const sensor_msgs::msg::Imu::SharedPtr imu_msg);
76+
bitbots_msgs::msg::JointCommand step(double dt, const sensor_msgs::msg::Imu::SharedPtr imu_msg,
77+
const sensor_msgs::msg::JointState::SharedPtr joint_state_msg);
7778

7879
geometry_msgs::msg::PoseArray step_open_loop(double dt);
7980

@@ -93,10 +94,6 @@ class DynupNode {
9394
// Store reference to the "real" node
9495
rclcpp::Node::SharedPtr node_;
9596

96-
rclcpp::Publisher<visualization_msgs::msg::Marker>::SharedPtr debug_publisher_;
97-
rclcpp::Publisher<bitbots_msgs::msg::JointCommand>::SharedPtr joint_goal_publisher_;
98-
rclcpp::Subscription<sensor_msgs::msg::Imu>::SharedPtr imu_subscriber_;
99-
10097
rclcpp_action::Server<DynupGoal>::SharedPtr action_server_;
10198

10299
std::vector<std::string> param_names_;
@@ -106,21 +103,33 @@ class DynupNode {
106103
// Data structure to hold all parameters, which is build from the schema in the 'parameters.yaml'
107104
bitbots_dynup::Params params_;
108105

106+
// Subcomponents of the dynup
109107
DynupEngine engine_;
110108
Stabilizer stabilizer_;
111109
Visualizer visualizer_;
112110
DynupIK ik_;
111+
112+
// Variables for node
113113
int stable_duration_ = 0;
114114
int failed_tick_counter_ = 0;
115115
double last_ros_update_time_ = 0;
116116
double start_time_ = 0;
117117
bool server_free_ = true;
118118
bool debug_ = false;
119+
120+
// TF2 related things
119121
tf2_ros::Buffer tf_buffer_;
120122
tf2_ros::TransformListener tf_listener_;
123+
124+
// MoveIt related things
121125
std::shared_ptr<robot_model_loader::RobotModelLoader> robot_model_loader_;
122126
moveit::core::RobotModelPtr kinematic_model_;
123127

128+
// Publishers and subscribers
129+
rclcpp::Publisher<bitbots_msgs::msg::JointCommand>::SharedPtr joint_goal_publisher_;
130+
rclcpp::Subscription<sensor_msgs::msg::Imu>::SharedPtr imu_subscriber_;
131+
rclcpp::Subscription<sensor_msgs::msg::JointState>::SharedPtr joint_state_subscriber_;
132+
124133
void execute(const std::shared_ptr<DynupGoalHandle> goal);
125134

126135
/**

bitbots_motion/bitbots_dynup/include/bitbots_dynup/dynup_pywrapper.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ using namespace ros2_python_extension;
1818
class PyDynupWrapper {
1919
public:
2020
explicit PyDynupWrapper(std::string ns);
21-
py::bytes step(double dt, py::bytes &imu_msg);
21+
py::bytes step(double dt, py::bytes &imu_msg, py::bytes &joint_state_msg);
2222
py::bytes step_open_loop(double dt);
2323
py::bytes get_poses();
2424
void reset();

bitbots_motion/bitbots_dynup/include/bitbots_dynup/visualizer.hpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,20 @@
11
#ifndef BITBOTS_DYNUP_INCLUDE_BITBOTS_DYNUP_VISUALIZER_H_
22
#define BITBOTS_DYNUP_INCLUDE_BITBOTS_DYNUP_VISUALIZER_H_
33

4+
#include <moveit/robot_model/robot_model.h>
5+
#include <moveit/robot_state/robot_state.h>
46
#include <tf2/LinearMath/Vector3.h>
57

8+
#include <bitbots_dynup/dynup_utils.hpp>
9+
#include <bitbots_dynup/msg/dynup_ik_offset.hpp>
10+
#include <bitbots_splines/abstract_ik.hpp>
611
#include <bitbots_splines/abstract_visualizer.hpp>
712
#include <bitbots_splines/smooth_spline.hpp>
813
#include <bitbots_splines/spline_container.hpp>
914
#include <geometry_msgs/msg/pose_stamped.hpp>
1015
#include <rclcpp/rclcpp.hpp>
1116
#include <string>
17+
#include <tf2_eigen/tf2_eigen.hpp>
1218
#include <visualization_msgs/msg/marker_array.hpp>
1319

1420
#include "dynup_parameters.hpp"
@@ -22,10 +28,13 @@ class Visualizer : bitbots_splines::AbstractVisualizer {
2228
void setParams(bitbots_dynup::Params::Visualizer params);
2329

2430
void displaySplines(bitbots_splines::PoseSpline splines, const std::string &frame);
31+
void publishIKOffsets(const moveit::core::RobotModelPtr &model, const DynupResponse &response,
32+
const bitbots_splines::JointGoals &ik_joint_goals);
2533

2634
private:
2735
rclcpp::Node::SharedPtr node_;
2836
rclcpp::Publisher<visualization_msgs::msg::MarkerArray>::SharedPtr spline_publisher_;
37+
rclcpp::Publisher<bitbots_dynup::msg::DynupIkOffset>::SharedPtr ik_debug_publisher_;
2938
std::string base_topic_;
3039
const std::string marker_ns_ = "bitbots_dynup";
3140
bitbots_dynup::Params::Visualizer params_;
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
std_msgs/Header header
2+
3+
# offset between goal position and position computed by running FK on the IK results
4+
geometry_msgs/Pose left_foot_ik_offset
5+
geometry_msgs/Pose right_foot_ik_offset
6+
geometry_msgs/Pose left_hand_ik_offset
7+
geometry_msgs/Pose right_hand_ik_offset

bitbots_motion/bitbots_dynup/src/dynup_ik.cpp

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,27 @@ void DynupIK::init(moveit::core::RobotModelPtr kinematic_model) {
1919
void DynupIK::reset() {
2020
// we have to set some good initial position in the goal state, since we are using a gradient
2121
// based method. Otherwise, the first step will be not correct
22-
std::vector<std::string> names_vec = {"LHipPitch", "LKnee", "LAnklePitch", "RHipPitch", "RKnee", "RAnklePitch"};
23-
std::vector<double> pos_vec = {0.7, 1.0, -0.4, -0.7, -1.0, 0.4};
24-
for (size_t i = 0; i < names_vec.size(); i++) {
25-
// besides its name, this method only changes a single joint position...
26-
goal_state_->setJointPositions(names_vec[i], &pos_vec[i]);
22+
23+
// Use the current joint state as a starting point for the IK
24+
if (joint_state_) {
25+
goal_state_->setVariablePositions(joint_state_->name, joint_state_->position);
26+
} else {
27+
// if we don't have a joint state, we set some default values that are a reasonable starting point
28+
RCLCPP_WARN(node_->get_logger(),
29+
"No joint state received, using hardcoded initial positions for IK "
30+
"initialization");
31+
std::vector<std::string> names_vec = {"LHipPitch", "LKnee", "LAnklePitch", "RHipPitch", "RKnee", "RAnklePitch"};
32+
std::vector<double> pos_vec = {0.7, 1.0, -0.4, -0.7, -1.0, 0.4};
33+
for (size_t i = 0; i < names_vec.size(); i++) {
34+
// besides its name, this method only changes a single joint position...
35+
goal_state_->setJointPositions(names_vec[i], &pos_vec[i]);
36+
}
2737
}
2838
}
2939

3040
void DynupIK::setDirection(DynupDirection direction) { direction_ = direction; }
3141

32-
bitbots_splines::JointGoals DynupIK::calculate(const DynupResponse &ik_goals) {
42+
bitbots_splines::JointGoals DynupIK::calculate(const DynupResponse& ik_goals) {
3343
/* ik options is basically the command which we send to bio_ik and which describes what we want to do */
3444
auto ik_options = kinematics::KinematicsQueryOptions();
3545
ik_options.return_approximate_solution = true;
@@ -104,4 +114,7 @@ bitbots_splines::JointGoals DynupIK::calculate(const DynupResponse &ik_goals) {
104114

105115
moveit::core::RobotStatePtr DynupIK::get_goal_state() { return goal_state_; }
106116

117+
void DynupIK::set_joint_positions(sensor_msgs::msg::JointState::ConstSharedPtr joint_state) {
118+
joint_state_ = joint_state;
119+
}
107120
} // namespace bitbots_dynup

bitbots_motion/bitbots_dynup/src/dynup_node.cpp

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,19 @@ using namespace std::chrono_literals;
55

66
DynupNode::DynupNode(rclcpp::Node::SharedPtr node, const std::string &ns, std::vector<rclcpp::Parameter> parameters)
77
: node_(node),
8-
debug_publisher_(node_->create_publisher<visualization_msgs::msg::Marker>("debug_markers", 1)),
9-
joint_goal_publisher_(node_->create_publisher<bitbots_msgs::msg::JointCommand>("dynup_motor_goals", 1)),
10-
imu_subscriber_(node_->create_subscription<sensor_msgs::msg::Imu>("imu/data", 1,
11-
std::bind(&DynupNode::imuCallback, this, _1))),
128
param_listener_(node_),
139
params_(param_listener_.get_params()),
1410
engine_(node_, params_.engine),
1511
stabilizer_(node_, params_.stabilizer),
1612
visualizer_(node_, params_.visualizer, "debug/dynup"),
1713
ik_(node_),
1814
tf_buffer_(node_->get_clock()),
19-
tf_listener_(tf_buffer_, node_) {
15+
tf_listener_(tf_buffer_, node_),
16+
joint_goal_publisher_(node_->create_publisher<bitbots_msgs::msg::JointCommand>("dynup_motor_goals", 1)),
17+
imu_subscriber_(node_->create_subscription<sensor_msgs::msg::Imu>("imu/data", 1,
18+
std::bind(&DynupNode::imuCallback, this, _1))),
19+
joint_state_subscriber_(node_->create_subscription<sensor_msgs::msg::JointState>(
20+
"joint_states", 1, std::bind(&DynupNode::jointStateCallback, this, _1))) {
2021
// We need to create a new node for moveit, otherwise dynamic reconfigure will be broken...
2122
auto moveit_node = std::make_shared<rclcpp::Node>(ns + "dynup_moveit_node");
2223

@@ -84,16 +85,21 @@ DynupNode::DynupNode(rclcpp::Node::SharedPtr node, const std::string &ns, std::v
8485
RCLCPP_INFO(node_->get_logger(), "Initialized DynUp and waiting for actions");
8586
}
8687

87-
bitbots_msgs::msg::JointCommand DynupNode::step(double dt, const sensor_msgs::msg::Imu::SharedPtr imu_msg) {
88+
bitbots_msgs::msg::JointCommand DynupNode::step(double dt, const sensor_msgs::msg::Imu::SharedPtr imu_msg,
89+
const sensor_msgs::msg::JointState::SharedPtr joint_state) {
8890
// method for python interface. take all messages as parameters instead of using ROS
8991
imuCallback(imu_msg);
92+
jointStateCallback(joint_state);
9093
// update dynup engine response
9194
bitbots_msgs::msg::JointCommand joint_goals = step(dt);
9295
return joint_goals;
9396
}
9497

9598
bitbots_msgs::msg::JointCommand DynupNode::step(double dt) {
9699
if (dt <= 0) {
100+
RCLCPP_WARN(node_->get_logger(),
101+
"dt was 0. this can happen in simulation if your update rate is higher than the "
102+
"simulators.");
97103
dt = 0.001;
98104
}
99105

@@ -108,6 +114,8 @@ bitbots_msgs::msg::JointCommand DynupNode::step(double dt) {
108114
// Calculate the joint goals (IK)
109115
bitbots_splines::JointGoals goals = ik_.calculate(stabilized_response);
110116

117+
visualizer_.publishIKOffsets(kinematic_model_, stabilized_response, goals);
118+
111119
// Check if we found a solution
112120
if (goals.first.empty()) {
113121
failed_tick_counter_++;
@@ -135,6 +143,10 @@ geometry_msgs::msg::PoseArray DynupNode::step_open_loop(double dt) {
135143

136144
void DynupNode::imuCallback(const sensor_msgs::msg::Imu::SharedPtr msg) { stabilizer_.setImu(msg); }
137145

146+
void DynupNode::jointStateCallback(const sensor_msgs::msg::JointState::SharedPtr joint_states) {
147+
ik_.set_joint_positions(joint_states);
148+
}
149+
138150
void DynupNode::onSetParameters() {
139151
engine_.setParams(params_.engine);
140152
stabilizer_.setParams(params_.stabilizer);
@@ -276,7 +288,7 @@ void DynupNode::loopEngine(int loop_rate, std::shared_ptr<DynupGoalHandle> goal_
276288
}
277289

278290
bitbots_dynup::msg::DynupPoses DynupNode::getCurrentPoses() {
279-
rclcpp::Time time = node_->get_clock()->now();
291+
rclcpp::Time time;
280292
/* Transform the left foot into the right foot frame and all other splines into the base link frame*/
281293
bitbots_dynup::msg::DynupPoses msg;
282294
try {
@@ -288,13 +300,13 @@ bitbots_dynup::msg::DynupPoses DynupNode::getCurrentPoses() {
288300

289301
// Get the transforms of the end effectors
290302
geometry_msgs::msg::Transform l_foot_transformed =
291-
tf_buffer_.lookupTransform(tf_names.r_sole_frame, tf_names.l_sole_frame, time, timeout).transform;
303+
tf_buffer_.lookupTransform(tf_names.r_sole_frame, tf_names.l_sole_frame, time).transform;
292304
geometry_msgs::msg::Transform r_foot_transformed =
293-
tf_buffer_.lookupTransform(tf_names.base_link_frame, tf_names.r_sole_frame, time, timeout).transform;
305+
tf_buffer_.lookupTransform(tf_names.base_link_frame, tf_names.r_sole_frame, time).transform;
294306
geometry_msgs::msg::Transform l_hand_transformed =
295-
tf_buffer_.lookupTransform(tf_names.base_link_frame, tf_names.l_wrist_frame, time, timeout).transform;
307+
tf_buffer_.lookupTransform(tf_names.base_link_frame, tf_names.l_wrist_frame, time).transform;
296308
geometry_msgs::msg::Transform r_hand_transformed =
297-
tf_buffer_.lookupTransform(tf_names.base_link_frame, tf_names.r_wrist_frame, time, timeout).transform;
309+
tf_buffer_.lookupTransform(tf_names.base_link_frame, tf_names.r_wrist_frame, time).transform;
298310

299311
std::function transform2pose = [](geometry_msgs::msg::Transform transform) {
300312
geometry_msgs::msg::Pose pose;

bitbots_motion/bitbots_dynup/src/dynup_pywrapper.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,10 @@ PyDynupWrapper::PyDynupWrapper(const std::string ns) {
1212

1313
void PyDynupWrapper::spin_some() { rclcpp::spin_some(node_); }
1414

15-
py::bytes PyDynupWrapper::step(double dt, py::bytes &imu_msg) {
16-
bitbots_msgs::msg::JointCommand result =
17-
dynup_->step(dt, std::make_shared<sensor_msgs::msg::Imu>(fromPython<sensor_msgs::msg::Imu>(imu_msg)));
15+
py::bytes PyDynupWrapper::step(double dt, py::bytes &imu_msg, py::bytes &joint_state_msg) {
16+
bitbots_msgs::msg::JointCommand result = dynup_->step(
17+
dt, std::make_shared<sensor_msgs::msg::Imu>(fromPython<sensor_msgs::msg::Imu>(imu_msg)),
18+
std::make_shared<sensor_msgs::msg::JointState>(fromPython<sensor_msgs::msg::JointState>(joint_state_msg)));
1819
return toPython<bitbots_msgs::msg::JointCommand>(result);
1920
}
2021

bitbots_motion/bitbots_dynup/src/dynup_stabilizer.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ DynupResponse Stabilizer::stabilize(const DynupResponse &ik_goals, const rclcpp:
2121
tf2::Transform right_foot_goal;
2222

2323
// Check if we have all the necessary data to stabilize
24-
if (!ik_goals.is_stabilizing_needed and r_sole_to_trunk_ and imu_) {
24+
if (ik_goals.is_stabilizing_needed and r_sole_to_trunk_ and imu_) {
2525
// Convert to eigen quaternion
2626
tf2::Quaternion quat;
2727
tf2::convert(imu_->orientation, quat);

0 commit comments

Comments
 (0)