用c++语言编写代码,要求如下:用ros2编写一个温度服务节点,该节点提供一个名为“tem”的服务,该服务节点使用模拟数据生成一个随机的浮点数作为温度值,并返回给客户端。再编写一个温度客户端节点,连接到温度服务节点并发送请求,接收到温度值后,打印到控制台。
时间: 2023-06-27 16:02:07 浏览: 105
以下是实现该要求的代码:
**温度服务节点**
```cpp
#include "rclcpp/rclcpp.hpp"
#include "example_interfaces/srv/get_float.hpp"
#include <cstdlib>
#include <ctime>
using std::placeholders::_1;
using std::placeholders::_2;
class TemperatureServer : public rclcpp::Node
{
public:
TemperatureServer()
: Node("temperature_server")
{
// 创建服务
temperature_service_ = this->create_service<example_interfaces::srv::GetFloat>("tem",
std::bind(&TemperatureServer::handle_temperature_service_request, this, _1, _2));
}
private:
void handle_temperature_service_request(const std::shared_ptr<example_interfaces::srv::GetFloat::Request> request,
std::shared_ptr<example_interfaces::srv::GetFloat::Response> response)
{
// 生成模拟温度数据
srand((unsigned)time(NULL));
float temperature = (float)(rand() % 100) + ((float)(rand() % 100)) / 100;
// 返回温度数据
response->temperature = temperature;
RCLCPP_INFO(this->get_logger(), "Temperature service: Temperature value returned: %.2f", temperature);
}
rclcpp::Service<example_interfaces::srv::GetFloat>::SharedPtr temperature_service_;
};
int main(int argc, char **argv)
{
rclcpp::init(argc, argv);
rclcpp::spin(std::make_shared<TemperatureServer>());
rclcpp::shutdown();
return 0;
}
```
**温度客户端节点**
```cpp
#include "rclcpp/rclcpp.hpp"
#include "example_interfaces/srv/get_float.hpp"
using std::placeholders::_1;
using std::placeholders::_2;
class TemperatureClient : public rclcpp::Node
{
public:
TemperatureClient()
: Node("temperature_client")
{
// 创建客户端
temperature_client_ = this->create_client<example_interfaces::srv::GetFloat>("tem");
}
void request_temperature()
{
while (!temperature_client_->wait_for_service(std::chrono::seconds(1))) {
if (!rclcpp::ok()) {
RCLCPP_ERROR(this->get_logger(), "Interrupted while waiting for the service. Exiting.");
return;
}
RCLCPP_INFO(this->get_logger(), "Waiting for the service to become available...");
}
// 发送请求
auto request = std::make_shared<example_interfaces::srv::GetFloat::Request>();
auto future_result = temperature_client_->async_send_request(request);
// 接收响应
if (rclcpp::spin_until_future_complete(this->get_node_base_interface(), future_result) !=
rclcpp::executor::FutureReturnCode::SUCCESS)
{
RCLCPP_ERROR(this->get_logger(), "Failed to receive temperature value.");
return;
}
auto response = future_result.get();
RCLCPP_INFO(this->get_logger(), "Temperature client: Temperature value received: %.2f", response->temperature);
}
private:
rclcpp::Client<example_interfaces::srv::GetFloat>::SharedPtr temperature_client_;
};
int main(int argc, char **argv)
{
rclcpp::init(argc, argv);
auto temperature_client = std::make_shared<TemperatureClient>();
temperature_client->request_temperature();
rclcpp::shutdown();
return 0;
}
```
使用方法:
1. 编译
```bash
$ colcon build
```
2. 运行服务节点
```bash
$ ros2 run temperature_service temperature_server
```
3. 运行客户端节点
```bash
$ ros2 run temperature_client temperature_client
```
客户端节点将会向服务节点发送请求,接收到响应后打印温度值到控制台。服务节点将会在接收到请求后生成一个随机的浮点数作为温度值,并返回给客户端。
阅读全文