ROS IMU 服务、客户端和服务器节点模板
ROS IMU services, client and server node templates
我想创建 IMU ROS 服务。我有一个服务器(在我的例子中是一个微控制器 ESC32),它可以获得 IMU 读数并应该传递给客户端(在我的例子中是 Raspberry PI 4B),当请求进一步 processing.So 时,IMU 数据我只是需要传递原始 IMU 数据。我想从一些用于 IMU 服务器和客户端节点的 ros 服务模板开始。这是我的 .srv 文件
float64 x_orient_in
float64 y_orient_in
float64 z_orient_in
float64 w_orient_in
float64 x_veloc_in
float64 y_veloc_in
float64 z_veloc_in
float64 x_accel_in
float64 y_accel_in
float64 z_accel_in
---
float64 x_orient_out
float64 y_orient_out
float64 z_orient_out
float64 w_orient_out
float64 x_veloc_out
float64 y_veloc_out
float64 z_veloc_out
float64 x_accel_out
float64 y_accel_out
float64 z_accel_out
bool success
这是我的服务器cpp
#include "ros/ros.h"
#include <sensor_msgs/Imu.h>
#include "ros_services/ImuValue.h"
bool get_val(ros_services::ImuValue::Request &req, ros_services::ImuValue::Response &res)
{
ROS_INFO("sending back response");
res.x_orient_out= req.x_orient_in;
res.y_orient_out= req.y_orient_in;
res.z_orient_out= req.z_orient_in;
res.w_orient_out= req.w_orient_in;
res.x_veloc_out= req.x_veloc_in;
res.y_veloc_out= req.y_veloc_in;
res.z_veloc_out= req.z_veloc_in;
res.x_accel_out= req.x_accel_in;
res.x_accel_out= req.x_accel_in;
res.x_accel_out= req.x_accel_in;
}
int main(int argc, char **argv)
{
ros::init(argc, argv, "imu_status_server");
ros::NodeHandle n;
ros::ServiceServer service = n.advertiseService("imu_status_server", get_val);
ROS_INFO("Starting server...");
ros::spin();
return 0;
}
而我的IMU ros题目是这个
header:
seq: 45672
stamp:
secs: 956
nsecs: 962000000
frame_id: "thrbot/imu_link"
orientation:
x: 0.0697171053094
y: 0.00825242210747
z: 0.920964387685
w: -0.383270164991
orientation_covariance: [0.0001, 0.0, 0.0, 0.0, 0.0001, 0.0, 0.0, 0.0, 0.0001]
angular_velocity:
x: 0.00156996015527
y: 0.0263644782572
z: -0.0617661883137
angular_velocity_covariance: [1.1519236000000001e-07, 0.0, 0.0, 0.0, 1.1519236000000001e-07, 0.0, 0.0, 0.0, 1.1519236000000001e-07]
linear_acceleration:
x: 1.32039048367
y: -0.376341478938
z: 9.70643773249
linear_acceleration_covariance: [1.6e-05, 0.0, 0.0, 0.0, 1.6e-05, 0.0, 0.0, 0.0, 1.6e-05]
我强烈建议查看 ros wiki tutorials。他们有很多信息,也可以为您节省一些时间。话虽如此,您只需像设置任何其他服务调用一样设置它。您可能还必须创建自己的消息,可能看起来像这样:
float64 x_accel
float64 y_accel
float64 z_accel
---
bool success
那么你只需要一个服务器节点来处理这样的传入请求:
#include "ros/ros.h"
#include "your_service/service_file.h"
bool get_val(your_service::service_file::Request &req,
your_service::service_file::Response &res)
{
res.x_accel = get_accel_x(); //Defined somewhere else by you
res.y_accel = get_accel_y(); //Defined somewhere else by you
res.z_accel = get_accel_z(); //Defined somewhere else by you
return true;
}
int main(int argc, char **argv)
{
ros::init(argc, argv, "imu_status_server");
ros::NodeHandle n;
ros::ServiceServer service = n.advertiseService("imu_status_server", add);
ROS_INFO("Starting server...");
ros::spin();
return 0;
}
终于客户端可以像这样调用服务端了
ros::ServiceClient client = n.serviceClient<beginner_tutorials::AddTwoInts>("imu_status_server");
your_service::service_file srv;
client.call(srv);
std::cout << "Got accel x: " << srv.response.x_accel << std::endl;
我想创建 IMU ROS 服务。我有一个服务器(在我的例子中是一个微控制器 ESC32),它可以获得 IMU 读数并应该传递给客户端(在我的例子中是 Raspberry PI 4B),当请求进一步 processing.So 时,IMU 数据我只是需要传递原始 IMU 数据。我想从一些用于 IMU 服务器和客户端节点的 ros 服务模板开始。这是我的 .srv 文件
float64 x_orient_in
float64 y_orient_in
float64 z_orient_in
float64 w_orient_in
float64 x_veloc_in
float64 y_veloc_in
float64 z_veloc_in
float64 x_accel_in
float64 y_accel_in
float64 z_accel_in
---
float64 x_orient_out
float64 y_orient_out
float64 z_orient_out
float64 w_orient_out
float64 x_veloc_out
float64 y_veloc_out
float64 z_veloc_out
float64 x_accel_out
float64 y_accel_out
float64 z_accel_out
bool success
这是我的服务器cpp
#include "ros/ros.h"
#include <sensor_msgs/Imu.h>
#include "ros_services/ImuValue.h"
bool get_val(ros_services::ImuValue::Request &req, ros_services::ImuValue::Response &res)
{
ROS_INFO("sending back response");
res.x_orient_out= req.x_orient_in;
res.y_orient_out= req.y_orient_in;
res.z_orient_out= req.z_orient_in;
res.w_orient_out= req.w_orient_in;
res.x_veloc_out= req.x_veloc_in;
res.y_veloc_out= req.y_veloc_in;
res.z_veloc_out= req.z_veloc_in;
res.x_accel_out= req.x_accel_in;
res.x_accel_out= req.x_accel_in;
res.x_accel_out= req.x_accel_in;
}
int main(int argc, char **argv)
{
ros::init(argc, argv, "imu_status_server");
ros::NodeHandle n;
ros::ServiceServer service = n.advertiseService("imu_status_server", get_val);
ROS_INFO("Starting server...");
ros::spin();
return 0;
}
而我的IMU ros题目是这个
header:
seq: 45672
stamp:
secs: 956
nsecs: 962000000
frame_id: "thrbot/imu_link"
orientation:
x: 0.0697171053094
y: 0.00825242210747
z: 0.920964387685
w: -0.383270164991
orientation_covariance: [0.0001, 0.0, 0.0, 0.0, 0.0001, 0.0, 0.0, 0.0, 0.0001]
angular_velocity:
x: 0.00156996015527
y: 0.0263644782572
z: -0.0617661883137
angular_velocity_covariance: [1.1519236000000001e-07, 0.0, 0.0, 0.0, 1.1519236000000001e-07, 0.0, 0.0, 0.0, 1.1519236000000001e-07]
linear_acceleration:
x: 1.32039048367
y: -0.376341478938
z: 9.70643773249
linear_acceleration_covariance: [1.6e-05, 0.0, 0.0, 0.0, 1.6e-05, 0.0, 0.0, 0.0, 1.6e-05]
我强烈建议查看 ros wiki tutorials。他们有很多信息,也可以为您节省一些时间。话虽如此,您只需像设置任何其他服务调用一样设置它。您可能还必须创建自己的消息,可能看起来像这样:
float64 x_accel
float64 y_accel
float64 z_accel
---
bool success
那么你只需要一个服务器节点来处理这样的传入请求:
#include "ros/ros.h"
#include "your_service/service_file.h"
bool get_val(your_service::service_file::Request &req,
your_service::service_file::Response &res)
{
res.x_accel = get_accel_x(); //Defined somewhere else by you
res.y_accel = get_accel_y(); //Defined somewhere else by you
res.z_accel = get_accel_z(); //Defined somewhere else by you
return true;
}
int main(int argc, char **argv)
{
ros::init(argc, argv, "imu_status_server");
ros::NodeHandle n;
ros::ServiceServer service = n.advertiseService("imu_status_server", add);
ROS_INFO("Starting server...");
ros::spin();
return 0;
}
终于客户端可以像这样调用服务端了
ros::ServiceClient client = n.serviceClient<beginner_tutorials::AddTwoInts>("imu_status_server");
your_service::service_file srv;
client.call(srv);
std::cout << "Got accel x: " << srv.response.x_accel << std::endl;