utest_message.cpp
Go to the documentation of this file.
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2012, Southwest Research Institute
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions are met:
9  *
10  * * Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  * * Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution.
15  * * Neither the name of the Southwest Research Institute, nor the names
16  * of its contributors may be used to endorse or promote products derived
17  * from this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
23  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
43 
44 #include <gtest/gtest.h>
45 
46 using namespace industrial::simple_message;
47 using namespace industrial::tcp_socket;
48 using namespace industrial::tcp_client;
49 using namespace industrial::tcp_server;
50 using namespace industrial::joint_data;
51 using namespace industrial::joint_message;
52 using namespace industrial::joint_traj_pt;
53 using namespace industrial::joint_traj_pt_message;
54 using namespace industrial::typed_message;
55 using namespace industrial::joint_traj;
56 using namespace industrial::robot_status;
57 using namespace industrial::robot_status_message;
58 
59 // Message passing routine, used to send and receive a typed message
60 // Useful for checking the packing and unpacking of message data.
62 {
63  const int tcpPort = TEST_PORT_BASE+401;
64  char ipAddr[] = "127.0.0.1";
65 
66  TcpClient tcpClient;
67  TcpServer tcpServer;
68  SimpleMessage msgSend, msgRecv;
69 
70  ASSERT_TRUE(send.toTopic(msgSend));
71 
72  // Construct server
73 
74  ASSERT_TRUE(tcpServer.init(tcpPort));
75 
76  // Construct a client
77  ASSERT_TRUE(tcpClient.init(&ipAddr[0], tcpPort));
78  ASSERT_TRUE(tcpClient.makeConnect());
79 
80  ASSERT_TRUE(tcpServer.makeConnect());
81 
82  ASSERT_TRUE(tcpClient.sendMsg(msgSend));
83  ASSERT_TRUE(tcpServer.receiveMsg(msgRecv));
84  ASSERT_TRUE(recv.init(msgRecv));
85 }
86 
88 {
89  JointData joint;
90 
91  joint.init();
92  EXPECT_TRUE(joint.setJoint(0, 1.0));
93  EXPECT_TRUE(joint.setJoint(1, 2.0));
94  EXPECT_TRUE(joint.setJoint(2, 3.0));
95  EXPECT_TRUE(joint.setJoint(3, 4.0));
96  EXPECT_TRUE(joint.setJoint(4, 5.0));
97  EXPECT_TRUE(joint.setJoint(5, 6.0));
98  EXPECT_TRUE(joint.setJoint(6, 7.0));
99  EXPECT_TRUE(joint.setJoint(7, 8.0));
100  EXPECT_TRUE(joint.setJoint(8, 9.0));
101  EXPECT_TRUE(joint.setJoint(9, 10.0));
102 
103  EXPECT_FALSE(joint.setJoint(10, 11.0));
104 
105 }
106 
108 {
109  JointData jointlhs, jointrhs;
110 
111  jointrhs.init();
112  jointlhs.init();
113  jointlhs.setJoint(0, -1.0);
114  jointlhs.setJoint(9, 1.0);
115 
116  EXPECT_FALSE(jointlhs==jointrhs);
117 
118  jointrhs.setJoint(0, -1.0);
119  jointrhs.setJoint(9, 1.0);
120 
121  EXPECT_TRUE(jointlhs==jointrhs);
122 
123 }
124 
125 TEST(JointMessage, toMessage)
126 {
127  JointData toMessage, fromMessage;
128  JointMessage msg;
129 
130  toMessage.init();
131  toMessage.setJoint(4, 44.44);
132 
133  msg.init(1, toMessage);
134 
135  fromMessage.copyFrom(msg.getJoints());
136 
137  EXPECT_TRUE(toMessage==fromMessage);
138 
139 }
140 
142 {
143 
144  JointMessage jointSend, jointRecv;
145  JointData posSend, posRecv;
146 
147  posSend.init();
148  posSend.setJoint(0, 1.0);
149  posSend.setJoint(1, 2.0);
150  posSend.setJoint(2, 3.0);
151  posSend.setJoint(3, 4.0);
152  posSend.setJoint(4, 5.0);
153  posSend.setJoint(5, 6.0);
154  posSend.setJoint(6, 7.0);
155  posSend.setJoint(7, 8.0);
156  posSend.setJoint(8, 9.0);
157  posSend.setJoint(9, 10.0);
158 
159  jointSend.init(1, posSend);
160 
161  messagePassing(jointSend, jointRecv);
162 
163  posRecv.copyFrom(jointRecv.getJoints());
164  ASSERT_TRUE(posRecv==posSend);
165 }
166 
168 {
169  JointTrajPt lhs, rhs;
170  JointData joint;
171 
172  joint.init();
173  ASSERT_TRUE(joint.setJoint(0, 1.0));
174  ASSERT_TRUE(joint.setJoint(1, 2.0));
175  ASSERT_TRUE(joint.setJoint(2, 3.0));
176  ASSERT_TRUE(joint.setJoint(3, 4.0));
177  ASSERT_TRUE(joint.setJoint(4, 5.0));
178  ASSERT_TRUE(joint.setJoint(5, 6.0));
179  ASSERT_TRUE(joint.setJoint(6, 7.0));
180  ASSERT_TRUE(joint.setJoint(7, 8.0));
181  ASSERT_TRUE(joint.setJoint(8, 9.0));
182  ASSERT_TRUE(joint.setJoint(9, 10.0));
183 
184  rhs.init(1.0, joint, 50.0, 100);
185  EXPECT_FALSE(lhs==rhs);
186 
187  lhs.init(0, joint, 0, 0);
188  EXPECT_FALSE(lhs==rhs);
189 
190  lhs.copyFrom(rhs);
191  EXPECT_TRUE(lhs==rhs);
192 
193 }
194 
195 TEST(JointTrajPt, toMessage)
196 {
197  JointTrajPt toMessage, fromMessage;
198  JointTrajPtMessage msg;
199 
200  toMessage.init();
201  toMessage.setSequence(99);
202  msg.init(toMessage);
203 
204  fromMessage.copyFrom(msg.point_);
205 
206  EXPECT_TRUE(toMessage==fromMessage);
207 
208 }
209 
211 {
212 
213  JointTrajPtMessage jointSend, jointRecv;
214  JointData data;
215  JointTrajPt posSend, posRecv;
216 
217  data.init();
218  data.setJoint(0, 1.0);
219  data.setJoint(1, 2.0);
220  data.setJoint(2, 3.0);
221  data.setJoint(3, 4.0);
222  data.setJoint(4, 5.0);
223  data.setJoint(5, 6.0);
224  data.setJoint(6, 7.0);
225  data.setJoint(7, 8.0);
226  data.setJoint(8, 9.0);
227  data.setJoint(9, 10.0);
228  posSend.init(1, data, 99, 100);
229 
230  jointSend.init(posSend);
231 
232  messagePassing(jointSend, jointRecv);
233 
234  posRecv.copyFrom(jointRecv.point_);
235  ASSERT_TRUE(posRecv==posSend);
236 }
237 
239 {
240  JointTraj lhs, rhs;
241  JointData joint;
242  JointTrajPt point;
243 
244  joint.init();
245  ASSERT_TRUE(joint.setJoint(0, 1.0));
246  ASSERT_TRUE(joint.setJoint(1, 2.0));
247  ASSERT_TRUE(joint.setJoint(2, 3.0));
248  ASSERT_TRUE(joint.setJoint(3, 4.0));
249  ASSERT_TRUE(joint.setJoint(4, 5.0));
250  ASSERT_TRUE(joint.setJoint(5, 6.0));
251  ASSERT_TRUE(joint.setJoint(6, 7.0));
252  ASSERT_TRUE(joint.setJoint(7, 8.0));
253  ASSERT_TRUE(joint.setJoint(8, 9.0));
254  ASSERT_TRUE(joint.setJoint(9, 10.0));
255 
256  point.init(1.0, joint, 50.0, 100);
257  rhs.addPoint(point);
258  EXPECT_FALSE(lhs==rhs);
259 
260  lhs.addPoint(point);
261  EXPECT_TRUE(lhs==rhs);
262 
263  lhs.addPoint(point);
264  EXPECT_FALSE(lhs==rhs);
265 
266  lhs.copyFrom(rhs);
267  EXPECT_TRUE(lhs==rhs);
268 
269 }
270 
271 TEST(RobotStatus, enumerations)
272 {
273  // Verifying the disabled state and aliases match
277 
278  // Verifying the enabled state and aliases values match
282 
283  // Verifying the unknown values match (this isn't reqd, but makes sense)
285 }
286 
288 {
289  RobotStatus status;
290  RobotStatus empty;
291  status.init();
292  // An empty (non-initted) status should be initialized in the constructor.
293  EXPECT_TRUE(status==empty);
294  EXPECT_EQ(status.getDrivesPowered(), TriStates::TS_UNKNOWN);
295  EXPECT_EQ(status.getEStopped(), TriStates::TS_UNKNOWN);
296  EXPECT_EQ(status.getErrorCode(), 0);
297  EXPECT_EQ(status.getInError(), TriStates::TS_UNKNOWN);
298  EXPECT_EQ(status.getInMotion(), TriStates::TS_UNKNOWN);
299  EXPECT_EQ(status.getMode(), RobotModes::UNKNOWN);
300  EXPECT_EQ(status.getMotionPossible(), TriStates::TS_UNKNOWN);
301 }
302 
304 {
305  RobotStatus lhs, rhs;
306 
307  EXPECT_TRUE(lhs==rhs);
309  EXPECT_FALSE(lhs==rhs);
311  EXPECT_TRUE(lhs==rhs);
312 
314  EXPECT_FALSE(lhs==rhs);
316  EXPECT_TRUE(lhs==rhs);
317 
318  lhs.setErrorCode(99);
319  EXPECT_FALSE(lhs==rhs);
320  rhs.setErrorCode(99);
321  EXPECT_TRUE(lhs==rhs);
322 
324  EXPECT_FALSE(lhs==rhs);
326  EXPECT_TRUE(lhs==rhs);
327 
329  EXPECT_FALSE(lhs==rhs);
331  EXPECT_TRUE(lhs==rhs);
332 
334  EXPECT_FALSE(lhs==rhs);
336  EXPECT_TRUE(lhs==rhs);
337 
339  EXPECT_FALSE(lhs==rhs);
341  EXPECT_TRUE(lhs==rhs);
342 
343 }
344 
345 TEST(RobotStatus, toMessage)
346 {
347  RobotStatus toMessage, fromMessage;
348  RobotStatusMessage msg;
349 
352  msg.init(toMessage);
353 
354  fromMessage.copyFrom(msg.status_);
355 
356  EXPECT_TRUE(toMessage==fromMessage);
357 
358 }
359 
361 {
362  RobotStatusMessage statusMsgSend, statusMsgRecv;
363  RobotStatus statusSend, statusRecv;
364 
367 
368  statusMsgSend.init(statusSend);
369 
370  messagePassing(statusMsgSend, statusMsgRecv);
371 
372  statusRecv.copyFrom(statusMsgRecv.status_);
373  ASSERT_TRUE(statusRecv==statusSend);
374 }
375 
virtual bool init(industrial::simple_message::SimpleMessage &msg)=0
Initializes message from a simple message.
Defines TCP server functions.
Definition: tcp_server.h:49
virtual bool sendMsg(industrial::simple_message::SimpleMessage &message)
Sends a message using the data connection.
void setInError(TriState inError)
Definition: robot_status.h:197
Class encapsulated joint message generation methods (either to or from a SimpleMessage type...
Definition: joint_message.h:85
virtual bool toTopic(industrial::simple_message::SimpleMessage &msg)
creates a simple_message topic
bool init(char *buff, int port_num)
initializes TCP client socket. Object can either be a client OR a server, NOT BOTH.
Definition: tcp_client.cpp:55
void setMotionPossible(TriState motionPossible)
Definition: robot_status.h:212
void setErrorCode(industrial::shared_types::shared_int errorCode)
Definition: robot_status.h:192
This class defines a simple messaging protocol for communicating with an industrial robot controller...
void messagePassing(TypedMessage &send, TypedMessage &recv)
Class encapsulated robot status data. The robot status data is meant to mirror the industrial_msgs/Ro...
Definition: robot_status.h:118
void setInMotion(TriState inMotion)
Definition: robot_status.h:202
TEST(JointMessage, init)
void setSequence(industrial::shared_types::shared_int sequence)
Sets joint trajectory point sequence number.
bool makeConnect()
connects to the remote host
Definition: tcp_server.cpp:118
bool init(industrial::simple_message::SimpleMessage &msg)
Initializes message from a simple message.
Class encapsulated joint trajectory point data. The point data serves as a waypoint along a trajector...
Definition: joint_traj_pt.h:92
void init()
Initializes an empty robot status.
Message interface for typed messages built from SimpleMessage.
Definition: typed_message.h:74
Class encapsulated robot status message generation methods (either to or from a industrial::simple_me...
bool addPoint(industrial::joint_traj_pt::JointTrajPt &point)
Adds a point value to the end of the buffer.
Definition: joint_traj.cpp:69
void copyFrom(JointData &src)
Copies the passed in value.
Definition: joint_data.cpp:107
bool setJoint(industrial::shared_types::shared_int index, industrial::shared_types::shared_real value)
Sets a joint value within the buffer.
Definition: joint_data.cpp:65
void copyFrom(JointTrajPt &src)
Copies the passed in value.
bool init(industrial::simple_message::SimpleMessage &msg)
Initializes message from a simple message.
virtual bool receiveMsg(industrial::simple_message::SimpleMessage &message)
Receives a message using the data connection.
void copyFrom(JointTraj &src)
Copies the passed in value.
Definition: joint_traj.cpp:105
Class encapsulated joint trajectory. A joint trajectory includes an array of JointTrajPt data types...
Definition: joint_traj.h:70
void copyFrom(RobotStatus &src)
Copies the passed in value.
Class encapsulated joint data (positions, accelerations, velocity, torque, and/or effort)...
Definition: joint_data.h:68
Class encapsulated joint trajectory point message generation methods (either to or from a industrial:...
void init()
Initializes a empty joint data.
Definition: joint_data.cpp:57
industrial::shared_types::shared_int getErrorCode() const
Definition: robot_status.h:157
void init()
Initializes a empty joint trajectory point.
bool makeConnect()
connects to the remote host
Definition: tcp_client.cpp:91
void setDrivesPowered(TriState drivesPowered)
Definition: robot_status.h:182
void setEStopped(TriState eStopped)
Definition: robot_status.h:187
bool init(industrial::simple_message::SimpleMessage &msg)
Initializes message from a simple message.
bool init(int port_num)
initializes TCP server socket. The connect method must be called following initialization in order to...
Definition: tcp_server.cpp:57
Defines TCP client functions.
Definition: tcp_client.h:52
industrial::joint_data::JointData & getJoints()
returns reference to underlying joint class


simple_message
Author(s): Shaun Edwards
autogenerated on Mon Feb 28 2022 22:34:36