-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
203 lines (167 loc) · 7.56 KB
/
main.cpp
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
203
/*
* Copyright (C) 2020 Fondazione Istituto Italiano di Tecnologia
*
* Licensed under either the GNU Lesser General Public License v3.0 :
* https://www.gnu.org/licenses/lgpl-3.0.html
* or the GNU Lesser General Public License v2.1 :
* https://www.gnu.org/licenses/old-licenses/lgpl-2.1.html
* at your option.
*/
#include <iDynTree/ModelIO/ModelLoader.h>
#include <iDynTree/Visualizer.h>
#include <yarp/sig/Image.h>
#include <yarp/os/BufferedPort.h>
#include <yarp/os/Network.h>
#include <yarp/os/LogStream.h>
#include <yarp/os/ResourceFinder.h>
#include <yarp/dev/PolyDriver.h>
#include <yarp/dev/IEncodersTimed.h>
#include <URDFdir.h>
#include <cmath>
#include <chrono>
#include <thread>
using namespace std::chrono_literals;
int main()
{
std::vector<std::string> jointList =
{"neck_pitch", "neck_roll", "neck_yaw",
"torso_pitch", "torso_roll", "torso_yaw",
"l_shoulder_pitch", "l_shoulder_roll", "l_shoulder_yaw", "l_elbow", "l_wrist_prosup", "l_wrist_pitch", "l_wrist_yaw",
"r_shoulder_pitch", "r_shoulder_roll", "r_shoulder_yaw", "r_elbow", "r_wrist_prosup", "r_wrist_pitch", "r_wrist_yaw",
"l_hip_pitch", "l_hip_roll", "l_hip_yaw", "l_knee", "l_ankle_pitch", "l_ankle_roll",
"r_hip_pitch", "r_hip_roll", "r_hip_yaw", "r_knee", "r_ankle_pitch", "r_ankle_roll"};
std::vector<std::string> controlBoards = {"head", "torso", "left_arm", "right_arm", "left_leg", "right_leg"};
std::string robotPrefix = "icub";
bool connectToRobot = true;
iDynTree::ModelLoader modelLoader;
std::string pathToModel = yarp::os::ResourceFinder::getResourceFinderSingleton().findFileByName("model.urdf");
modelLoader.loadReducedModelFromFile(pathToModel, jointList);
iDynTree::Visualizer viz;
iDynTree::VisualizerOptions options, textureOptions;
// options.winWidth = 1920;
// options.winHeight = 1080;
viz.init(options);
iDynTree::ITexture* textureInterface = viz.textures().add("AdditionalTexture", textureOptions);
// viz.camera().setPosition(iDynTree::Position(2.0, 0.5, 0.5));
// viz.camera().setTarget(iDynTree::Position(0.4, 0.0, 0.5));
viz.camera().setPosition(iDynTree::Position(1.2, 0.0, 0.5));
viz.camera().setTarget(iDynTree::Position(-0.15, 0.0, 0.15));
viz.camera().animator()->enableMouseControl(true);
double sqrt2 = std::sqrt(2.0);
viz.enviroment().lightViz("sun").setDirection(iDynTree::Direction(-0.5/sqrt2, 0, -0.5/sqrt2));
// viz.enviroment().setFloorGridColor(iDynTree::ColorViz(0.0, 1.0, 0.0, 1.0));
textureInterface->environment().lightViz("sun").setDirection(iDynTree::Direction(-0.5/sqrt2, 0, -0.5/sqrt2));
textureInterface->environment().setElementVisibility("floor_grid", false);
textureInterface->environment().setElementVisibility("world_frame", false);
textureInterface->environment().setBackgroundColor(iDynTree::ColorViz(0.0, 0.0, 0.0, 0.0));
// textureInterface->environment().setFloorGridColor(iDynTree::ColorViz(0.0, 1.0, 0.0, 1.0));
viz.addModel(modelLoader.model(), "robot");
yarp::sig::FlexImage image;
image.setPixelCode(VOCAB_PIXEL_RGB);
image.resize(textureOptions.winWidth, textureOptions.winHeight);
// initialise yarp network
yarp::os::Network yarp;
if (!yarp.checkNetwork())
{
yError()<<"[main] Unable to find YARP network";
return EXIT_FAILURE;
}
yarp::os::BufferedPort<yarp::sig::FlexImage> imagePort;
imagePort.open("/visualizerImage");
yarp::dev::PolyDriver robotDevice;
yarp::dev::IEncodersTimed *encodersInterface{nullptr};
if (connectToRobot)
{
yarp::os::Property remapperOptions;
remapperOptions.put("device", "remotecontrolboardremapper");
yarp::os::Bottle axesNames;
yarp::os::Bottle & axesList = axesNames.addList();
for (auto& joint : jointList)
{
axesList.addString(joint);
}
remapperOptions.put("axesNames",axesNames.get(0));
yarp::os::Bottle remoteControlBoards;
yarp::os::Bottle & remoteControlBoardsList = remoteControlBoards.addList();
for (auto& cb : controlBoards)
{
remoteControlBoardsList.addString("/" + robotPrefix + "/" + cb);
}
remapperOptions.put("remoteControlBoards",remoteControlBoards.get(0));
remapperOptions.put("localPortPrefix", "/visualizer/remoteControlBoard:i");
if(!robotDevice.open(remapperOptions))
{
connectToRobot = false;
}
if (!robotDevice.view(encodersInterface) || !encodersInterface)
{
connectToRobot = false;
}
}
std::vector<iDynTree::PixelViz> pixels;
std::chrono::steady_clock::time_point now = std::chrono::steady_clock::now();
std::chrono::steady_clock::time_point lastSent = std::chrono::steady_clock::now();
std::chrono::steady_clock::time_point lastViz = std::chrono::steady_clock::now();
unsigned int desiredFPS = 30;
unsigned int maxVizFPS = 65;
bool mirrorImage = false;
long minimumMicroSec = std::round(1e6 / (double) desiredFPS);
long minimumMicroSecViz = std::round(1e6 / (double) maxVizFPS);
iDynTree::Transform wHb = iDynTree::Transform::Identity();
iDynTree::VectorDynSize joints(jointList.size());
iDynTree::VectorDynSize jointsInDeg(jointList.size());
while(viz.run())
{
now = std::chrono::steady_clock::now();
if (std::chrono::duration_cast<std::chrono::microseconds>(now - lastViz).count() < minimumMicroSecViz)
{
std::this_thread::sleep_for(1ms);
continue;
}
joints.zero();
if (connectToRobot)
{
encodersInterface->getEncoders(jointsInDeg.data());
}
for (size_t i = 0; i < jointsInDeg.size(); ++i)
{
joints(i) = iDynTree::deg2rad(jointsInDeg(i));
}
viz.modelViz("robot").setPositions(wHb, joints);
viz.draw();
lastViz = std::chrono::steady_clock::now();
if (std::chrono::duration_cast<std::chrono::microseconds>(now - lastSent).count() >= minimumMicroSec)
{
if (textureInterface->getPixels(pixels))
{
for (unsigned int i = 0; i < pixels.size(); ++i)
{
iDynTree::PixelViz& pixelImage = pixels[i];
size_t width;
if (mirrorImage)
{
width = image.width() - 1 - pixelImage.width;
}
else
{
width = pixelImage.width;
}
yarp::sig::PixelRgb& pixelYarp = *(reinterpret_cast<yarp::sig::PixelRgb*>(
image.getPixelAddress(width, pixelImage.height)));
pixelYarp.r = pixelImage.r;
pixelYarp.g = pixelImage.g;
pixelYarp.b = pixelImage.b;
}
}
yarp::sig::FlexImage& imageToBeSent = imagePort.prepare();
imageToBeSent.setPixelCode(VOCAB_PIXEL_RGB);
imageToBeSent.setExternal(image.getRawImage(), image.width(), image.height()); //Avoid to copy
imagePort.write();
lastSent = std::chrono::steady_clock::now();
}
}
image.resize(0,0);
imagePort.close();
robotDevice.close();
return 0;
}