標籤:bsp markers tutorial 技術 大小 顏色 hand set 相關
一、建立一個包——進行marker練習
1、建立ROS工作空間和包
mkdir -p ~/catkin_ws/src #建立工作空間目錄#建立ROS資料包catkin_create_pkg using_markers roscpp visualization_msgs #開啟包根目錄,進行編譯cd ~/catkin_wscatkin_make
2、編寫cpp檔案,向rviz發送資料
vim ~/catkin_ws/src/using_marker/src/using_markers.cpp
貼入代碼,代碼中已經附加相關注釋
#include <ros/ros.h>#include <visualization_msgs/Marker.h> //可視化int main( int argc, char** argv ){ //初始化ROS,幷且建立一個ROS::Publisher 在話題visualization_marker上面 ros::init(argc, argv, "basic_shapes"); ros::NodeHandle n; ros::Rate r(1); ros::Publisher marker_pub = n.advertise<visualization_msgs::Marker>("visualization_marker", 1); // Set our initial shape type to be a cube // 初始化形狀為立方體 uint32_t shape = visualization_msgs::Marker::CUBE; while (ros::ok()) { //執行個體化一個Marker visualization_msgs::Marker marker; // Set the frame ID and timestamp. See the TF tutorials for information on these. // 設定frame ID 和 時間戳記 marker.header.frame_id = "/my_frame"; marker.header.stamp = ros::Time::now(); // Set the namespace and id for this marker. This serves to create a unique ID // Any marker sent with the same namespace and id will overwrite the old one // 為這個marker設定一個獨一無二的ID,一個marker接收到相同ns和id就會用新的資訊代替舊的 marker.ns = "basic_shapes"; marker.id = 0; // Set the marker type. Initially this is CUBE, and cycles between that and SPHERE, ARROW, and CYLINDER // 設定marker類型,初始化是立方體。將進行迴圈 marker.type = shape; // Set the marker action. Options are ADD, DELETE, and new in ROS Indigo: 3 (DELETEALL) marker.action = visualization_msgs::Marker::ADD; // Set the pose of the marker. This is a full 6DOF pose relative to the frame/time specified in the header // 設定marker的位置 marker.pose.position.x = 0; marker.pose.position.y = 0; marker.pose.position.z = 0; marker.pose.orientation.x = 0.0; marker.pose.orientation.y = 0.0; marker.pose.orientation.z = 0.0; marker.pose.orientation.w = 1.0; // Set the scale of the marker -- 1x1x1 here means 1m on a side // 設定marker的大小 marker.scale.x = 1.0; marker.scale.y = 1.0; marker.scale.z = 1.0; // Set the color -- be sure to set alpha to something non-zero! // 設定marker的顏色 marker.color.r = 0.0f; marker.color.g = 1.0f; marker.color.b = 0.0f; marker.color.a = 1.0; //取消自動刪除 marker.lifetime = ros::Duration(); // Publish the marker // 必須有訂閱者才會發布訊息 while (marker_pub.getNumSubscribers() < 1) { if (!ros::ok()) { return 0; } ROS_WARN_ONCE("Please create a subscriber to the marker"); sleep(1); } marker_pub.publish(marker); // Cycle between different shapes // 連續改變形狀 switch (shape) { case visualization_msgs::Marker::CUBE: shape = visualization_msgs::Marker::SPHERE; break; case visualization_msgs::Marker::SPHERE: shape = visualization_msgs::Marker::ARROW; break; case visualization_msgs::Marker::ARROW: shape = visualization_msgs::Marker::CYLINDER; break; case visualization_msgs::Marker::CYLINDER: shape = visualization_msgs::Marker::CUBE; break; } r.sleep(); }}
在CMakeList.txt檔案中加入
add_executable(basic_shapes src/basic_shapes.cpp)target_link_libraries(basic_shapes ${catkin_LIBRARIES})
3、進行rviz設定
(1)開啟roscore
(2)運行編寫的發布器
rosrun using_marker basic_shapes
(3)重設rviz,運行rviz
rosmake rvizrosrun rviz rviz
(4)在rviz中進行設定
4、rviz最終效果顯示:4個圖形進行連續的變換
一、建立一個包——進行marker練習
1、建立ROS工作空間和包
mkdir -p ~/catkin_ws/src #建立工作空間目錄#建立ROS資料包catkin_create_pkg using_markers roscpp visualization_msgs #開啟包根目錄,進行編譯cd ~/catkin_wscatkin_make
2、編寫cpp檔案,向rviz發送資料
vim ~/catkin_ws/src/using_marker/src/using_markers.cpp
貼入代碼,代碼中已經附加相關注釋
#include <ros/ros.h>#include <visualization_msgs/Marker.h> //可視化int main( int argc, char** argv ){ //初始化ROS,幷且建立一個ROS::Publisher 在話題visualization_marker上面 ros::init(argc, argv, "basic_shapes"); ros::NodeHandle n; ros::Rate r(1); ros::Publisher marker_pub = n.advertise<visualization_msgs::Marker>("visualization_marker", 1); // Set our initial shape type to be a cube // 初始化形狀為立方體 uint32_t shape = visualization_msgs::Marker::CUBE; while (ros::ok()) { //執行個體化一個Marker visualization_msgs::Marker marker; // Set the frame ID and timestamp. See the TF tutorials for information on these. // 設定frame ID 和 時間戳記 marker.header.frame_id = "/my_frame"; marker.header.stamp = ros::Time::now(); // Set the namespace and id for this marker. This serves to create a unique ID // Any marker sent with the same namespace and id will overwrite the old one // 為這個marker設定一個獨一無二的ID,一個marker接收到相同ns和id就會用新的資訊代替舊的 marker.ns = "basic_shapes"; marker.id = 0; // Set the marker type. Initially this is CUBE, and cycles between that and SPHERE, ARROW, and CYLINDER // 設定marker類型,初始化是立方體。將進行迴圈 marker.type = shape; // Set the marker action. Options are ADD, DELETE, and new in ROS Indigo: 3 (DELETEALL) marker.action = visualization_msgs::Marker::ADD; // Set the pose of the marker. This is a full 6DOF pose relative to the frame/time specified in the header // 設定marker的位置 marker.pose.position.x = 0; marker.pose.position.y = 0; marker.pose.position.z = 0; marker.pose.orientation.x = 0.0; marker.pose.orientation.y = 0.0; marker.pose.orientation.z = 0.0; marker.pose.orientation.w = 1.0; // Set the scale of the marker -- 1x1x1 here means 1m on a side // 設定marker的大小 marker.scale.x = 1.0; marker.scale.y = 1.0; marker.scale.z = 1.0; // Set the color -- be sure to set alpha to something non-zero! // 設定marker的顏色 marker.color.r = 0.0f; marker.color.g = 1.0f; marker.color.b = 0.0f; marker.color.a = 1.0; //取消自動刪除 marker.lifetime = ros::Duration(); // Publish the marker // 必須有訂閱者才會發布訊息 while (marker_pub.getNumSubscribers() < 1) { if (!ros::ok()) { return 0; } ROS_WARN_ONCE("Please create a subscriber to the marker"); sleep(1); } marker_pub.publish(marker); // Cycle between different shapes // 連續改變形狀 switch (shape) { case visualization_msgs::Marker::CUBE: shape = visualization_msgs::Marker::SPHERE; break; case visualization_msgs::Marker::SPHERE: shape = visualization_msgs::Marker::ARROW; break; case visualization_msgs::Marker::ARROW: shape = visualization_msgs::Marker::CYLINDER; break; case visualization_msgs::Marker::CYLINDER: shape = visualization_msgs::Marker::CUBE; break; } r.sleep(); }}
在CMakeList.txt檔案中加入
add_executable(basic_shapes src/basic_shapes.cpp)target_link_libraries(basic_shapes ${catkin_LIBRARIES})
3、進行rviz設定
(1)開啟roscore
(2)運行編寫的發布器
rosrun using_marker basic_shapes
(3)重設rviz,運行rviz
rosmake rvizrosrun rviz rviz
(4)在rviz中進行設定
4、rviz最終效果顯示:4個圖形進行連續的變換
rviz學習筆記(一)——Markers: Sending Basic Shapes (C++) 發送基礎形狀