service_client_ned_one.cpp
Go to the documentation of this file.
1 /*
2  ttl_driver_service_client.cpp
3  Copyright (C) 2020 Niryo
4  All rights reserved.
5 
6  This program is free software: you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  This program is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with this program. If not, see <http:// www.gnu.org/licenses/>.
18 */
19 
20 #include <ros/service_client.h>
21 
22 #include <gtest/gtest.h>
23 #include <ros/ros.h>
24 
25 #include "ros/duration.h"
27 
28 #include <memory>
29 #include <string>
30 
31 static std::unique_ptr<ros::NodeHandle> nh;
32 static bool simulation_mode;
33 
34 TEST(TESTSuite, SetLeds)
35 {
36  auto client = nh->serviceClient<niryo_robot_msgs::SetInt>("/niryo_robot/ttl_driver/set_dxl_leds");
37 
38  bool exists(client.waitForExistence(ros::Duration(1)));
39  EXPECT_TRUE(exists);
40 
41  niryo_robot_msgs::SetInt srv;
42  srv.request.value = 2;
43  client.call(srv);
44 
45  EXPECT_EQ(srv.response.status, niryo_robot_msgs::CommandStatus::SUCCESS);
46 }
47 
48 TEST(TESTSuite, WriteCustomValue)
49 {
50  auto client = nh->serviceClient<ttl_driver::WriteCustomValue>("/niryo_robot/ttl_driver/send_custom_value");
51 
52  bool exists(client.waitForExistence(ros::Duration(1)));
53  EXPECT_TRUE(exists);
54 
55  ttl_driver::WriteCustomValue srv;
56 
57  srv.request.id = 2;
58  srv.request.reg_address = 64; // Torque enable for xl430
59  srv.request.value = 1;
60  srv.request.byte_number = 1;
61 
62  client.call(srv);
63 
64  EXPECT_EQ(srv.response.status, niryo_robot_msgs::CommandStatus::SUCCESS);
65 }
66 
67 TEST(TESTSuite, WriteCustomValueWrongParam)
68 {
69  auto client = nh->serviceClient<ttl_driver::WriteCustomValue>("/niryo_robot/ttl_driver/send_custom_value");
70 
71  bool exists(client.waitForExistence(ros::Duration(1)));
72  EXPECT_TRUE(exists);
73 
74  ttl_driver::WriteCustomValue srv;
75 
76  srv.request.id = 50;
77  srv.request.reg_address = 64; // Torque enable for xl430
78  srv.request.value = 1;
79  srv.request.byte_number = 1;
80 
81  client.call(srv);
82 
83  EXPECT_EQ(srv.response.status, niryo_robot_msgs::CommandStatus::WRONG_MOTOR_TYPE);
84 }
85 
86 TEST(TESTSuite, ReadCustomValue)
87 {
88  auto client = nh->serviceClient<ttl_driver::ReadCustomValue>("/niryo_robot/ttl_driver/read_custom_value");
89 
90  bool exists(client.waitForExistence(ros::Duration(1)));
91  EXPECT_TRUE(exists);
92 
93  ttl_driver::ReadCustomValue srv;
94  srv.request.id = 2;
95  srv.request.reg_address = 64;
96  srv.request.byte_number = 1;
97 
98  client.call(srv);
99 
100  EXPECT_EQ(srv.response.status, niryo_robot_msgs::CommandStatus::SUCCESS);
101 
102  if (!simulation_mode)
103  {
104  EXPECT_EQ(srv.response.value, 1);
105  }
106 }
107 
108 TEST(TESTSuite, WritePIDValue)
109 {
110  auto client = nh->serviceClient<ttl_driver::WritePIDValue>("/niryo_robot/ttl_driver/write_pid_value");
111 
112  bool exists(client.waitForExistence(ros::Duration(1)));
113  EXPECT_TRUE(exists);
114 
115  ttl_driver::WritePIDValue srv;
116 
117  srv.request.id = 2;
118  srv.request.pos_p_gain = 3264;
119  srv.request.pos_i_gain = 0;
120  srv.request.pos_d_gain = 9520;
121  srv.request.vel_p_gain = 0;
122  srv.request.vel_i_gain = 0;
123  srv.request.ff1_gain = 0;
124  srv.request.ff2_gain = 400;
125  srv.request.vel_profile = 100;
126  srv.request.acc_profile = 200;
127 
128  client.call(srv);
129 
130  EXPECT_EQ(srv.response.status, niryo_robot_msgs::CommandStatus::SUCCESS);
131  ros::Duration(1.0).sleep();
132 }
133 
134 TEST(TESTSuite, ReadPIDValue)
135 {
136  auto client = nh->serviceClient<ttl_driver::ReadPIDValue>("/niryo_robot/ttl_driver/read_pid_value");
137 
138  bool exists(client.waitForExistence(ros::Duration(1)));
139  EXPECT_TRUE(exists);
140 
141  ttl_driver::ReadPIDValue srv;
142  srv.request.id = 2;
143 
144  client.call(srv);
145 
146  EXPECT_EQ(srv.response.status, niryo_robot_msgs::CommandStatus::SUCCESS);
147  EXPECT_EQ(srv.response.pos_p_gain, 3264);
148  EXPECT_EQ(srv.response.pos_i_gain, 0);
149  EXPECT_EQ(srv.response.pos_d_gain, 9520);
150  EXPECT_EQ(srv.response.vel_p_gain, 0);
151  EXPECT_EQ(srv.response.vel_i_gain, 0);
152  EXPECT_EQ(srv.response.ff1_gain, 0);
153  EXPECT_EQ(srv.response.ff2_gain, 400);
154 }
155 
156 TEST(TESTSuite, ReadPIDValueWrongParam)
157 {
158  auto client = nh->serviceClient<ttl_driver::ReadPIDValue>("/niryo_robot/ttl_driver/read_pid_value");
159 
160  bool exists(client.waitForExistence(ros::Duration(1)));
161  EXPECT_TRUE(exists);
162 
163  ttl_driver::ReadPIDValue srv;
164 
165  srv.request.id = 20;
166 
167  client.call(srv);
168 
169  EXPECT_EQ(srv.response.status, niryo_robot_msgs::CommandStatus::FAILURE);
170 }
171 
172 int main(int argc, char **argv)
173 {
174  ros::init(argc, argv, "ttl_driver_service_client");
175 
176  testing::InitGoogleTest(&argc, argv);
177 
178  nh = std::make_unique<ros::NodeHandle>("~");
179  nh->getParam("simulation_mode", simulation_mode);
180 
181  return RUN_ALL_TESTS();
182 }
ttl_interface_core.hpp
nh
static std::unique_ptr< ros::NodeHandle > nh
Definition: service_client_ned_one.cpp:31
simulation_mode
static bool simulation_mode
Definition: service_client_ned_one.cpp:32
main
int main(int argc, char **argv)
Definition: service_client_ned_one.cpp:172
TEST
TEST(TESTSuite, SetLeds)
Definition: service_client_ned_one.cpp:34


ttl_driver
Author(s): Clement Cocquempot
autogenerated on Wed May 21 2025 08:30:14