mirs
Toggle main menu visibility
読み取り中…
検索中…
一致する文字列を見つけられません
basic_command.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/basic_command.hpp"
// 新しいサービスファイルをインクルード
31
32
using namespace
std::chrono_literals;
33
34
int
main
(
int
argc,
char
**argv)
35
{
36
rclcpp::init(argc, argv);
37
38
auto
node = rclcpp::Node::make_shared(
"basic_command_client"
);
39
40
auto
client = node->create_client<mirs_msgs::srv::BasicCommand>(
"esp_cmd"
);
41
42
// 左はyamlから取る値、右はデフォルト値
43
node->declare_parameter(
"param1"
, 1.0);
44
node->declare_parameter(
"param2"
, 0.0);
45
node->declare_parameter(
"param3"
, 0.0);
46
node->declare_parameter(
"param4"
, 0.0);
47
48
// サービスが利用可能になるまで待機
49
while
(!client->wait_for_service(1s)) {
50
if
(!rclcpp::ok()) {
51
RCLCPP_ERROR(node->get_logger(),
"Interrupted while waiting for the service. Exiting."
);
52
return
1;
53
}
54
RCLCPP_INFO(node->get_logger(),
"service not available, waiting again..."
);
55
}
56
57
// リクエストの作成
58
auto
request = std::make_shared<mirs_msgs::srv::BasicCommand::Request>();
59
60
request->param1 = node->get_parameter(
"param1"
).as_double();
61
request->param2 = node->get_parameter(
"param2"
).as_double();
62
request->param3 = node->get_parameter(
"param3"
).as_double();
63
request->param4 = node->get_parameter(
"param4"
).as_double();
64
65
auto
result = client->async_send_request(request);
66
67
if
(rclcpp::spin_until_future_complete(node, result) == rclcpp::FutureReturnCode::SUCCESS) {
68
const
bool
ok = result.get()->success;
69
RCLCPP_INFO(node->get_logger(),
"リクエストの成功状態: %s"
, ok ?
"true"
:
"false"
);
70
rclcpp::shutdown();
71
return
ok ? 0 : 1;
72
}
else
{
73
RCLCPP_ERROR(node->get_logger(),
"サービス呼び出しに失敗しました。"
);
74
rclcpp::shutdown();
75
return
1;
76
}
77
}
main
int main(int argc, char **argv)
定義
basic_command.cpp:34
src
basic_command.cpp
構築:
1.18.0