-
Notifications
You must be signed in to change notification settings - Fork 486
/
state_log.cc
202 lines (163 loc) · 5.17 KB
/
state_log.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/*
* Copyright 2012 Open Source Robotics Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
#include <boost/filesystem.hpp>
#include "gazebo/msgs/msgs.hh"
#include "gazebo/transport/transport.hh"
#include "ServerFixture.hh"
using namespace gazebo;
class StateLogTest : public ServerFixture
{
};
double g_pr2LGripperXStart = -1;
double g_pr2LGripperXEnd = -1;
int g_msgCount = 0;
/////////////////////////////////////////////////
// Pose callback. We are just getting one link from the pr2 for simplicity.
void onPoseInfo(ConstPose_VPtr &_msg)
{
for (int i = 0; i < _msg->pose_size(); ++i)
{
if (_msg->pose(i).name() == "pr2::l_gripper_r_parallel_link")
{
if (g_pr2LGripperXStart < 0)
g_pr2LGripperXStart = _msg->pose(i).position().x();
g_pr2LGripperXEnd = _msg->pose(i).position().x();
}
}
++g_msgCount;
}
/////////////////////////////////////////////////
/// Record a log file
TEST(StateLogTest, PR2Record)
{
custom_exec("gzserver -r --record_path /tmp/gazebo_test "
"--iters 1000 --seed 12345 worlds/pr2.world");
boost::filesystem::path path = "/tmp/gazebo_test/state.log";
EXPECT_TRUE(boost::filesystem::exists(path) != false);
}
/////////////////////////////////////////////////
// Playback a log file
TEST(StateLogTest, PR2PlaybackZipped)
{
// Cleanup...not the best
custom_exec("killall -9 gzserver");
// Run playback
boost::thread *play = new boost::thread(boost::bind(&custom_exec,
"gzserver -u -p /tmp/gazebo_test/state.log"));
// Setup transportation
gazebo::transport::init();
gazebo::transport::run();
gazebo::transport::NodePtr node(new gazebo::transport::Node());
node->Init();
// Create a publisher to unpause gzserver
gazebo::transport::PublisherPtr pub =
node->Advertise<gazebo::msgs::WorldControl>(
"/gazebo/default/world_control");
pub->WaitForConnection();
// Subscribe to pose info
gazebo::transport::SubscriberPtr sub = node->Subscribe(
"/gazebo/default/pose/info", &onPoseInfo);
// Unpause gzserver
gazebo::msgs::WorldControl msg;
msg.set_pause(false);
pub->Publish(msg);
int i = 0;
// Wait for messages to arrive
while (g_msgCount < 120 && i < 100)
{
gzdbg << "Pose Msg Count = " << g_msgCount << std::endl;
gazebo::common::Time::MSleep(100);
++i;
}
// Kill gserver
play->interrupt();
delete play;
// Cleanup...not the best
custom_exec("killall -9 gzserver");
gazebo::transport::fini();
if (g_msgCount >= 120)
{
// Make sure the values are correct.
EXPECT_NEAR(g_pr2LGripperXStart, 0.83, 0.05);
EXPECT_NEAR(g_pr2LGripperXEnd, 0.76, 0.05);
}
}
/////////////////////////////////////////////////
// Playback a log file
TEST(StateLogTest, PR2PlaybackTxt)
{
// Cleanup...not the best
custom_exec("killall -9 gzserver");
g_pr2LGripperXStart = -1;
g_pr2LGripperXEnd = -1;
g_msgCount = 0;
// Convert the zipped state to txt and set a Hz filter.
custom_exec("gzlog echo /tmp/gazebo_test/state.log -z 30 > "
"/tmp/gazebo_test/state_txt.log");
// Run playback
boost::thread *play = new boost::thread(boost::bind(&custom_exec,
"gzserver -u -p /tmp/gazebo_test/state.log"));
// Setup transportation
gazebo::transport::init();
gazebo::transport::run();
gazebo::transport::NodePtr node(new gazebo::transport::Node());
node->Init();
// Create a publisher to unpause gzserver
gazebo::transport::PublisherPtr pub =
node->Advertise<gazebo::msgs::WorldControl>(
"/gazebo/default/world_control");
pub->WaitForConnection();
// Subscribe to pose info
gazebo::transport::SubscriberPtr sub = node->Subscribe(
"/gazebo/default/pose/info", &onPoseInfo);
// Unpause gzserver
gazebo::msgs::WorldControl msg;
msg.set_pause(false);
pub->Publish(msg);
int i = 0;
// Wait for messages to arrive
while (g_msgCount < 120 && i < 100)
{
gzdbg << "Pose Msg Count = " << g_msgCount << std::endl;
gazebo::common::Time::MSleep(100);
++i;
}
// Kill gserver
play->interrupt();
delete play;
// Cleanup...not the best
custom_exec("killall -9 gzserver");
gazebo::transport::fini();
if (g_msgCount > 100)
{
// Make sure the values are correct.
EXPECT_NEAR(g_pr2LGripperXStart, 0.83, 0.05);
EXPECT_NEAR(g_pr2LGripperXEnd, 0.76, 0.05);
}
}
/////////////////////////////////////////////////
int main(int argc, char **argv)
{
// Cleanup test directory and create a new one.
boost::filesystem::remove_all("/tmp/gazebo_test");
boost::filesystem::create_directories("/tmp/gazebo_test");
::testing::InitGoogleTest(&argc, argv);
int result = RUN_ALL_TESTS();
// Cleanup test directory.
boost::filesystem::remove_all("/tmp/gazebo_test");
return result;
}