rosワークスペースと機能パック
**独自に開発したものを含むすべてのROSプログラムは、
機能パッケージに編成されています** ROS機能パッケージはワークスペースに保存されます。
**したがって、プログラムを作成する前の最初のステップは、機能パッケージに対応するワークスペースを作成することです。ROSワークスペースはlinuxの下のディレクトリです。ROSワークスペースを作成するにはlinuxディレクトリを作成します(デフォルトではcatkin_wsという名前のワークスペースを作成します)が、ROS仕様に従ってこのディレクトリにsrcサブディレクトリを追加する必要があります。次に実行します。 ROSコマンド。
詳細については、https://blog.csdn.net/wwwlyj123321/article/details/83147242を参照して
ください。
トピックコミュニケーション
ワークスペースディレクトリに汎用パッケージhelloを作成します
catkin_create_pkg hello roscpp
関数パッケージは、ワークスペースのsrcディレクトリにhelloフォルダを生成します
関数パッケージhelloのsrcディレクトリにhello.cpp
コンテンツを作成します。
#include "ros/ros.h"
#include<iostream>
using namespace std;
int main(int argc, char **argv)
{
cout<<"hello ros!"<<endl;
return 0;
}
helloパッケージディレクトリのCMakeLists.txtファイルを変更します。
ファイルを開いて次を追加します。
## Declare a cpp executable
add_executable(printf_hello src/hello.cpp)
ワークスペースをコンパイルする
catkin_make
新しいターミナルを開き、rosコアを起動します
roscore
新しいターミナルを開いた後、実行します
rosrun hello printf_hello
表示されます
hello ros!
サービス通信
新しい関数パッケージhello2を作成し、そのsrcディレクトリにサーバーノードとクライアントノードを作成して、
2つの整数を追加し、合計を出力します。
add_two_ints_server.cpp
#include "ros/ros.h"
#include "learning_communication/AddTwoInts.h"
// service回调函数,输入参数req,输出参数res
bool add(learning_communication::AddTwoInts::Request &req,
learning_communication::AddTwoInts::Response &res)
{
// 将输入参数中的请求数据相加,结果放到应答变量中
res.sum = req.a + req.b;
ROS_INFO("request: x=%ld, y=%ld", (long int)req.a, (long int)req.b);
ROS_INFO("sending back response: [%ld]", (long int)res.sum);
return true;
}
int main(int argc, char **argv)
{
// ROS节点初始化
ros::init(argc, argv, "add_two_ints_server");
// 创建节点句柄
ros::NodeHandle n;
// 创建一个名为add_two_ints的server,注册回调函数add()
ros::ServiceServer service = n.advertiseService("add_two_ints", add);
// 循环等待回调函数
ROS_INFO("Ready to add two ints.");
ros::spin();
return 0;
}
add_two_ints_client.cpp
#include <cstdlib>
#include "ros/ros.h"
#include "learning_communication/AddTwoInts.h"
int main(int argc, char **argv)
{
// ROS节点初始化
ros::init(argc, argv, "add_two_ints_client");
// 从终端命令行获取两个加数
if (argc != 3)
{
ROS_INFO("usage: add_two_ints_client X Y");
return 1;
}
// 创建节点句柄
ros::NodeHandle n;
// 创建一个client,请求add_two_int service,service消息类型是learning_communication::AddTwoInts
ros::ServiceClient client = n.serviceClient<learning_communication::AddTwoInts>("add_two_ints");
// 创建learning_communication::AddTwoInts类型的service消息
learning_communication::AddTwoInts srv;
srv.request.a = atoll(argv[1]);
srv.request.b = atoll(argv[2]);
// 发布service请求,等待加法运算的应答结果
if (client.call(srv))
{
ROS_INFO("Sum: %ld", (long int)srv.response.sum);
}
else
{
ROS_ERROR("Failed to call service add_two_ints");
return 1;
}
return 0;
}
ノードをコンパイルして、関数パッケージのCMakeLists.txtに追加します
add_executable(add_two_ints_server src/add_two_ints_server.cpp)
target_link_libraries(add_two_ints_server ${
catkin_LIBRARIES})
add_dependencies(add_two_ints_server learning_communication_gencpp)
add_executable(add_two_ints_client src/add_two_ints_client.cpp)
target_link_libraries(add_two_ints_client ${
catkin_LIBRARIES})
add_dependencies(add_two_ints_client learning_communication_gencpp)
関数パッケージ名:learning_communication
コンパイル
cd ~/catkin_ws/
catkin_make
ターミナル1を実行します
roscore
ターミナル2
rosrun learning_communication add_two_ints_server
ターミナル3
rosrun learning_communication add_two_ints_client 53 66
参照:https:
//blog.csdn.net/weixin_42237429/article/details/90301566
Rvizのインストールと使用
ubantuバージョン18.04ターミナルコマンドを
インストールしました
sudo apt-get install ros-melodic-rviz
rosdep install rviz
rosmake rviz
コンピュータのカメラを呼び出すために使用しrviz
3.0へのUSBコントローラとの互換性を設定1.
2. [スタートROSを
roscore
3.新しい端末を作成し、カメラノードを実行します
rosrun uvc_camera uvc_camera_node
4.カメラと画像の情報を表示するための新しい端末を作成します
rostopic list
5.カメラ情報を印刷してから、ctrl + cを使用して印刷を停止します
rostopic echo /camera_info
6.カメラインターフェースを開きます
rosrun image_view image_view image:=/image_raw
7.rvizを起動します
rosrun rviz rviz
[追加]をクリックして、画像を選択します
画像トピックのプロパティを設定する
次に、カメラ画面がrvizに表示されます