小鱼 ROS 2 新书上线!点击链接查看, 新书配套视频点击链接查看。
提问前必看的发帖注意事项—— 提问前必看!不符合要求的问题拒绝回答!!
社区使用指南—如何添加标签修改密码
建图前的准备2 发布odom的TF
-
ros2 run fishbot_bringup fishbot_bringup
运行该指令之后没有输出(全部按照教程进行)fishbot_bringup.cpp 代码
#include <rclcpp/rclcpp.hpp>
#include <nav_msgs/msg/odometry.hpp>
#include <tf2/utils.h>
#include <tf2_ros/transform_broadcaster.h>class TopicSubscribe01 : public rclcpp::Node
{
public:
TopicSubscribe01(std::string name) : Node(name)
{
// 创建一个订阅者,订阅"odom"话题的nav_msgs::msg::Odometry类型消息
odom_subscribe_ = this->create_subscription<nav_msgs::msg::Odometry>(
"odom", rclcpp::SensorDataQoS(),
std::bind(&TopicSubscribe01::odom_callback, this, std::placeholders::1));
// 创建一个tf2_ros::TransformBroadcaster用于广播坐标变换
tf_broadcaster = std::make_unique<tf2_ros::TransformBroadcaster>(this);
}private:
rclcpp::Subscription<nav_msgs::msg::Odometry>::SharedPtr odom_subscribe_;
std::unique_ptr<tf2_ros::TransformBroadcaster> tf_broadcaster_;
nav_msgs::msg::Odometry odom_msg_;// 回调函数,处理接收到的odom消息
void odom_callback(const nav_msgs::msg::Odometry::SharedPtr msg)
{
(void)msg;
RCLCPP_INFO(this->get_logger(), "接收到里程计信息->底盘坐标系 tf :(%f,%f)",
msg->pose.pose.position.x, msg->pose.pose.position.y)
// 更新odom_msg_的姿态信息
odom_msg_.pose.pose.position.x = msg->pose.pose.position.x;
odom_msg_.pose.pose.position.y = msg->pose.pose.position.y;
odom_msg_.pose.pose.position.z = msg->pose.pose.position.z;
odom_msg_.pose.pose.orientation.x = msg->pose.pose.orientation.x;
odom_msg_.pose.pose.orientation.y = msg->pose.pose.orientation.y;
odom_msg_.pose.pose.orientation.z = msg->pose.pose.orientation.z;
odom_msg_.pose.pose.orientation.w = msg->pose.pose.orientation.w;
};public:
// 发布坐标变换信息
void publish_tf()
{
geometry_msgs::msg::TransformStamped transform;
double seconds = this->now().seconds();
transform.header.stamp = rclcpp::Time(static_cast<uint64_t>(seconds * 1e9));
transform.header.frame_id = "odom";
transform.child_frame_id = "base_footprint";
transform.transform.translation.x = odom_msg_.pose.pose.position.x;
transform.transform.translation.y = odom_msg_.pose.pose.position.y;
transform.transform.translation.z = odom_msg_.pose.pose.position.z;
transform.transform.rotation.x = odom_msg_.pose.pose.orientation.x;
transform.transform.rotation.y = odom_msg_.pose.pose.orientation.y;
transform.transform.rotation.z = odom_msg_.pose.pose.orientation.z;
transform.transform.rotation.w = odom_msg_.pose.pose.orientation.w;
// 广播坐标变换信息
tf_broadcaster_->sendTransform(transform);
}
};int main(int argc, char **argv)
{
// 初始化ROS节点
rclcpp::init(argc, argv);// 创建一个TopicSubscribe01节点
auto node = std::make_shared<TopicSubscribe01>("fishbot_bringup");// 设置循环频率
rclcpp::WallRate loop_rate(20.0);
while (rclcpp::ok())
{
// 处理回调函数
rclcpp::spin_some(node);
// 发布坐标变换信息
node->publish_tf();
// 控制循环频率
loop_rate.sleep();
}
// 关闭ROS节点
rclcpp::shutdown();
return 0;
}CMakeLists.txt 代码
cmake_minimum_required(VERSION 3.8)
project(fishbot_bringup)if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
add_compile_options(-Wall -Wextra -Wpedantic)
endif()
#find dependencies
find_package(ament_cmake REQUIRED)
find_package(rclcpp REQUIRED)
find_package(geometry_msgs REQUIRED)
find_package(nav_msgs REQUIRED)
find_package(tf2 REQUIRED)
find_package(tf2_ros REQUIRED)set(dependencies
rclcpp
geometry_msgs
nav_msgs
tf2
tf2_ros
)#uncomment the following section in order to fill in
#further dependencies manually.
#find_package(<dependency> REQUIRED)add_executable(fishbot_bringup src/fishbot_bringup.cpp)
target_include_directories(fishbot_bringup PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:include>)
target_compile_features(fishbot_bringup PUBLIC c_std_99 cxx_std_17) # Require C99 and C++17ament_target_dependencies(fishbot_bringup
${dependencies}
)
install(TARGETS fishbot_bringup
DESTINATION lib/${PROJECT_NAME})if(BUILD_TESTING)
find_package(ament_lint_auto REQUIRED)
#the following line skips the linter which checks for copyrights
#comment the line when a copyright and license is added to all source files
set(ament_cmake_copyright_FOUND TRUE)
#the following line skips cpplint (only works in a git repo)
#comment the line when this package is in a git repo and when
#a copyright and license is added to all source files
set(ament_cmake_cpplint_FOUND TRUE)
ament_lint_auto_find_test_dependencies()
endif()ament_package()
-
@2693226252 要开agent,你要理解这段代码的意思,订阅odom发布tf
-
-
@twj0011001010 无所谓,你要用那个开哪个
-
@小鱼 我根据教程里的步骤来,发现我的odom没有发布话题,我也有线运行agent了,是不是因为需要雷达转接板才能实现,必须用无线运行吗
-
@小鱼 开了agent还是不行
-
@2693226252 https://fishros.org.cn/forum/topic/151/提问前必看-不符合要求的问题拒绝回答
提问模板:
## 标题:[清晰而简洁地总结您的问题] ### 问题描述: [在这里详细描述您遇到的问题。请确保您提供了足够的背景信息,以便其他人能够理解您的问题。] ### 具体细节和研究: - [列出任何相关细节或研究,以帮助回答者更好地理解您的问题。] - [如果可能,请提供与您问题相关的链接或参考资料。] ### 尝试过的解决方法: - [描述您已经尝试过的任何解决方法或方法。这可以帮助其他人了解您遇到问题的具体情况,并避免给出与您已经尝试过的解决方案相同的答案。] 在发帖之前,请确保您已经在网站上搜索过您的问题,以确保它没有被回答过。感谢您的提问,社区会竭尽全力提供帮助!
-
@twj0011001010 请按照模板提问
-
@2693226252 解决了吗?