当前位置:   article > 正文

无人机以MID-360作为定位信息,_mid360,如何进行室内定位

mid360,如何进行室内定位

目的:从TF(Transform Frame)树中获取机器人的当前位置和姿态信息,并将其发布到MAVROS以供无人机使用。

  1. <launch>
  2. <!--向飞控发送位置信息-->
  3. <include file = "$(find fast_lio)/launch/mapping_avia.launch">
  4. < / include>
  5. <include file = "$(find livox_ros_driver2)/launch_ROS1/msg_MID360.launch">
  6. < / include>
  7. <include file = "$(find mavros)/launch/px4.launch">
  8. <arg name = "fcu_url" default = "/dev/ttyACM0:921600" / >
  9. < / include>
  10. <include file = "$(find t265_to_mavros)/launch/mid360_tf_to_mavros.launch">
  11. < / include>
  12. < / launch>
  1. #include <ros/ros.h>
  2. #include <tf2_ros/transform_listener.h>
  3. #include <geometry_msgs/TransformStamped.h>
  4. #include <geometry_msgs/Twist.h>
  5. #include <geometry_msgs/PoseStamped.h>
  6. #include <nav_msgs/Path.h>
  7. #include <nav_msgs/Odometry.h>
  8. ros::Publisher position_pub;
  9. using namespace std;
  10. int main(int argc, char** argv) {
  11. ros::init(argc, argv, "position_to_mavros");
  12. ros::NodeHandle node("~");
  13. geometry_msgs::PoseStamped cur_position;//创建一个名为 cur_position 的变量,用于存储当前位置信息。
  14. position_pub = node.advertise<geometry_msgs::PoseStamped>("/mavros/vision_pose/pose", 10);
  15. tf2_ros::Buffer tfBuffer;//创建一个坐标变换的缓存对象。
  16. tf2_ros::TransformListener tfListener(tfBuffer);//创建一个监听坐标变换的对象,并将缓存对象传递给它。
  17. //view path in rviz
  18. nav_msgs::Path body_path;
  19. std::string target_frame_id;
  20. std::string source_frame_id;
  21. //std::string target_frame_id = "carto_odom";
  22. //std::string source_frame_id = "base_link";
  23. double output_rate = 50, roll_obj = 0, pitch_obj = 0, yaw_obj = 0;
  24. node.getParam("target_frame_id", target_frame_id);
  25. node.getParam("source_frame_id", source_frame_id);
  26. node.getParam("output_rate", output_rate);
  27. node.getParam("roll_obj", roll_obj);
  28. node.getParam("pitch_obj", pitch_obj);
  29. node.getParam("yaw_obj", yaw_obj);
  30. cout << "target_frame_id:" << target_frame_id << endl;
  31. cout << "source_frame_id:" << source_frame_id << endl;
  32. cout << "output_rate:" << output_rate << endl;
  33. cout << "roll_obj:" << roll_obj << endl;
  34. cout << "pitch_obj:" << pitch_obj << endl;
  35. cout << "yaw_obj:" << yaw_obj << endl;
  36. ros::Rate rate(output_rate);
  37. while (node.ok()) {
  38. geometry_msgs::TransformStamped transformStamped;
  39. try {
  40. transformStamped = tfBuffer.lookupTransform(target_frame_id, source_frame_id,
  41. ros::Time(0), ros::Duration(3.0));//创建一个存储坐标变换信息的对象。
  42. static tf2::Quaternion quat_obj, quat_body;//创建两个静态变量,用于存储对象的四元数。
  43. quat_obj = tf2::Quaternion(transformStamped.transform.rotation.x, transformStamped.transform.rotation.y, transformStamped.transform.rotation.z, transformStamped.transform.rotation.w);//根据获取的坐标变换信息,将旋转部分的四元数存储到 quat_obj 中。
  44. quat_body.setRPY(roll_obj, pitch_obj, yaw_obj);//设置 quat_body 的欧拉角(滚转、俯仰和偏航)。
  45. //ROS_INFO_STREAM(quat_body);
  46. quat_body = quat_obj * quat_body;//将 quat_body 和 quat_obj 进行四元数乘法。
  47. quat_body.normalize();//将四元数归一化,确保其长度为1。
  48. //
  49. cur_position.pose.position.x = transformStamped.transform.translation.x;
  50. cur_position.pose.position.y = transformStamped.transform.translation.y;
  51. cur_position.pose.position.z = transformStamped.transform.translation.z;
  52. cout << "cur_position.pose.position.x:" << cur_position.pose.position.x << endl;
  53. cout << "cur_position.pose.position.y:" << cur_position.pose.position.y << endl;
  54. cout << "cur_position.pose.position.z:" << cur_position.pose.position.z << endl;
  55. cur_position.pose.orientation.x = quat_body.y();
  56. cur_position.pose.orientation.y = quat_body.x();
  57. cur_position.pose.orientation.z = quat_body.z();
  58. cur_position.pose.orientation.w = quat_body.w();
  59. cout << "cur_position.pose.orientation.x:" << cur_position.pose.orientation.x << endl;
  60. cout << "cur_position.pose.orientation.y:" << cur_position.pose.orientation.y << endl;
  61. cout << "cur_position.pose.orientation.z:" << cur_position.pose.orientation.z << endl;
  62. cout << "cur_position.pose.orientation.w:" << cur_position.pose.orientation.w << endl;
  63. cur_position.header.stamp = ros::Time::now();
  64. cur_position.header.frame_id = transformStamped.header.frame_id;
  65. position_pub.publish(cur_position);
  66. }
  67. catch (tf2::TransformException& ex) {
  68. ROS_WARN("%s", ex.what());
  69. ros::Duration(1.0).sleep();
  70. continue;
  71. }
  72. rate.sleep();
  73. }
  74. return 0;
  75. };

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/2023面试高手/article/detail/687914
推荐阅读
相关标签
  

闽ICP备14008679号