mirs
読み取り中…
検索中…
一致する文字列を見つけられません
parameter_update.cpp
[詳解]
1// Copyright 2026 mirs260x
2//
3// Permission is hereby granted, free of charge, to any person obtaining a copy
4// of this software and associated documentation files (the "Software"), to deal
5// in the Software without restriction, including without limitation the rights
6// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7// copies of the Software, and to permit persons to whom the Software is
8// furnished to do so, subject to the following conditions:
9//
10// The above copyright notice and this permission notice shall be included in
11// all copies or substantial portions of the Software.
12//
13// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
16// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19// THE SOFTWARE.
20
21
25#include <chrono>
26#include <cstdlib>
27#include <memory>
28
29#include "rclcpp/rclcpp.hpp"
30#include "mirs_msgs/srv/parameter_update.hpp" // 新しいサービスファイルをインクルード
31
32using namespace std::chrono_literals;
33
34int main(int argc, char **argv)
35{
36 rclcpp::init(argc, argv);
37
38 auto node = rclcpp::Node::make_shared("parameter_update_client");
39
40 auto client = node->create_client<mirs_msgs::srv::ParameterUpdate>("esp_update");
41
42 // 既定値は config.yaml と一致させること(単独起動時の安全側)
43 node->declare_parameter("wheel_radius", 0.0391);
44 node->declare_parameter("wheel_base", 0.39);
45 node->declare_parameter("rkp", 40.0);
46 node->declare_parameter("rki", 150.0);
47 node->declare_parameter("rkd", 0.4);
48 node->declare_parameter("lkp", 40.0);
49 node->declare_parameter("lki", 150.0);
50 node->declare_parameter("lkd", 0.4);
51
52 // サービスが利用可能になるまで待機
53 while (!client->wait_for_service(1s)) {
54 if (!rclcpp::ok()) {
55 RCLCPP_ERROR(node->get_logger(), "サービス待機中に中断されました。終了します。");
56 return 1;
57 }
58 RCLCPP_INFO(node->get_logger(), "サービスが利用できません。再試行します...");
59 }
60
61 // リクエストの作成
62 auto request = std::make_shared<mirs_msgs::srv::ParameterUpdate::Request>();
63
64 request->wheel_radius = node->get_parameter("wheel_radius").as_double();
65 request->wheel_base = node->get_parameter("wheel_base").as_double();
66 request->rkp = node->get_parameter("rkp").as_double();
67 request->rki = node->get_parameter("rki").as_double();
68 request->rkd = node->get_parameter("rkd").as_double();
69 request->lkp = node->get_parameter("lkp").as_double();
70 request->lki = node->get_parameter("lki").as_double();
71 request->lkd = node->get_parameter("lkd").as_double();
72
73 auto result = client->async_send_request(request);
74
75 if (rclcpp::spin_until_future_complete(node, result) == rclcpp::FutureReturnCode::SUCCESS) {
76 const bool ok = result.get()->success;
77 RCLCPP_INFO(node->get_logger(), "リクエストの成功状態: %s", ok ? "true" : "false");
78 if (!ok) {
79 rclcpp::shutdown();
80 return 1;
81 }
82 } else {
83 RCLCPP_ERROR(node->get_logger(), "サービス呼び出しに失敗しました。");
84 rclcpp::shutdown();
85 return 1;
86 }
87
88 rclcpp::shutdown();
89 return 0;
90}
int main(int argc, char **argv)