visual-tracking-control
main.cpp
Go to the documentation of this file.
1 #include <chrono>
2 #include <future>
3 #include <iostream>
4 #include <memory>
5 
6 #include <BayesFilters/ResamplingWithPrior.h>
7 #include <yarp/os/ConstString.h>
8 #include <yarp/os/LogStream.h>
9 #include <yarp/os/Network.h>
10 #include <yarp/os/ResourceFinder.h>
11 #include <yarp/os/Value.h>
12 #include <opencv2/core/core.hpp>
13 #include <opencv2/core/cuda.hpp>
14 
15 #include <BrownianMotionPose.h>
16 #include <DrawFwdKinPoses.h>
17 #include <iCubGatePose.h>
18 #include <iCubFwdKinModel.h>
19 #include <InitiCubArm.h>
20 #include <PlayFwdKinModel.h>
21 #include <PlayGatePose.h>
22 #include <VisualProprioception.h>
23 #include <VisualSIS.h>
24 #include <VisualUpdateParticles.h>
25 
26 using namespace bfl;
27 using namespace cv;
28 using namespace Eigen;
29 using namespace yarp::dev;
30 using namespace yarp::os;
31 
32 
33 /* UTILITY FUNCTIONS FOREWORD DECLARATIONS */
34 std::string engine_count_to_string(int engine_count);
35 
36 
37 /* MAIN */
38 int main(int argc, char *argv[])
39 {
40  ConstString log_ID = "[Main]";
41  yInfo() << log_ID << "Configuring and starting module...";
42 
43  Network yarp;
44  if (!yarp.checkNetwork(3.0))
45  {
46  yError() << "YARP seems unavailable!";
47  return EXIT_FAILURE;
48  }
49 
50 
51  // Page locked dovrebbe essere piĆ¹ veloce da utilizzate con CUDA, non sembra essere il caso.
52 // Mat::setDefaultAllocator(cuda::HostMem::getAllocator(cuda::HostMem::PAGE_LOCKED));
53 
54  cuda::DeviceInfo gpu_dev;
55  yInfo() << log_ID << "[CUDA] Engine capability:" << engine_count_to_string(gpu_dev.asyncEngineCount());
56  yInfo() << log_ID << "[CUDA] Can have concurrent kernel:" << gpu_dev.concurrentKernels();
57  yInfo() << log_ID << "[CUDA] Streaming multiprocessor count:" << gpu_dev.multiProcessorCount();
58  yInfo() << log_ID << "[CUDA] Can map host memory:" << gpu_dev.canMapHostMemory();
59  yInfo() << log_ID << "[CUDA] Clock:" << gpu_dev.clockRate() << "KHz";
60 
61 
62  ResourceFinder rf;
63  rf.setVerbose();
64  rf.setDefaultContext("hand-tracking");
65  rf.setDefaultConfigFile("config.ini");
66  rf.configure(argc, argv);
67 
68  FilteringParamtersD paramsd;
69  paramsd["num_particles"] = rf.findGroup("PF").check("num_particles", Value(50)).asInt();
70  paramsd["gpu_count"] = rf.findGroup("PF").check("gpu_count", Value(1.0)).asInt();
71  paramsd["resample_prior"] = rf.findGroup("PF").check("resample_prior", Value(1.0)).asInt();
72  paramsd["gate_pose"] = rf.findGroup("PF").check("gate_pose", Value(0.0)).asInt();
73  paramsd["resolution_ratio"] = rf.findGroup("PF").check("resolution_ratio", Value(1.0)).asInt();
74  paramsd["num_images"] = paramsd["num_particles"] / paramsd["gpu_count"];
75  if (rf.check("play"))
76  paramsd["play"] = 1.0;
77  else
78  paramsd["play"] = rf.findGroup("PF").check("play", Value(1.0)).asDouble();
79 
80  paramsd["q_xy"] = rf.findGroup("BROWNIANMOTION").check("q_xy", Value(0.005)).asDouble();
81  paramsd["q_z"] = rf.findGroup("BROWNIANMOTION").check("q_z", Value(0.005)).asDouble();
82  paramsd["theta"] = rf.findGroup("BROWNIANMOTION").check("theta", Value(3.0)).asDouble();
83  paramsd["cone_angle"] = rf.findGroup("BROWNIANMOTION").check("cone_angle", Value(2.5)).asDouble();
84  paramsd["seed"] = rf.findGroup("BROWNIANMOTION").check("seed", Value(1.0)).asDouble();
85 
86  paramsd["use_thumb"] = rf.findGroup("VISUALPROPRIOCEPTION").check("use_thumb", Value(0.0)).asDouble();
87  paramsd["use_forearm"] = rf.findGroup("VISUALPROPRIOCEPTION").check("use_forearm", Value(0.0)).asDouble();
88 
89  paramsd["likelihood_gain"] = rf.findGroup("VISUALUPDATEPARTICLES").check("likelihood_gain", Value(0.001)).asDouble();
90 
91  paramsd["gate_x"] = rf.findGroup("GATEPOSE").check("gate_x", Value(0.1)).asDouble();
92  paramsd["gate_y"] = rf.findGroup("GATEPOSE").check("gate_y", Value(0.1)).asDouble();
93  paramsd["gate_z"] = rf.findGroup("GATEPOSE").check("gate_z", Value(0.1)).asDouble();
94  paramsd["gate_aperture"] = rf.findGroup("GATEPOSE").check("gate_aperture", Value(15.0)).asDouble();
95  paramsd["gate_rotation"] = rf.findGroup("GATEPOSE").check("gate_rotation", Value(30.0)).asDouble();
96 
97  paramsd["resample_ratio"] = rf.findGroup("RESAMPLING").check("resample_ratio", Value(0.3)).asDouble();
98  paramsd["prior_ratio"] = rf.findGroup("RESAMPLING").check("prior_ratio", Value(0.5)).asDouble();
99 
100  FilteringParamtersS paramss;
101  paramss["robot"] = rf.findGroup("PF").check("robot", Value("icub")).asString();
102  if (rf.check("cam"))
103  paramss["cam_sel"] = rf.find("cam").asString();
104  else
105  paramss["cam_sel"] = rf.findGroup("PF").check("cam_sel", Value("left")).asString();
106  paramss["laterality"] = rf.findGroup("PF").check("laterality", Value("right")).asString();
107 
108 
109  yInfo() << log_ID << "Running with:";
110  yInfo() << log_ID << " - robot:" << paramss["robot"];
111  yInfo() << log_ID << " - cam_sel:" << paramss["cam_sel"];
112  yInfo() << log_ID << " - laterality:" << paramss["laterality"];
113 
114  yInfo() << log_ID << " - num_particles:" << paramsd["num_particles"];
115  yInfo() << log_ID << " - gpu_count:" << paramsd["gpu_count"];
116  yInfo() << log_ID << " - num_images:" << paramsd["num_images"];
117  yInfo() << log_ID << " - resample_prior:" << paramsd["resample_prior"];
118  yInfo() << log_ID << " - gate_pose:" << paramsd["gate_pose"];
119  yInfo() << log_ID << " - play:" << (paramsd["play"] == 1.0 ? "true" : "false");
120 
121  yInfo() << log_ID << " - q_xy:" << paramsd["q_xy"];
122  yInfo() << log_ID << " - q_z:" << paramsd["q_z"];
123  yInfo() << log_ID << " - theta:" << paramsd["theta"];
124  yInfo() << log_ID << " - cone_angle:" << paramsd["cone_angle"];
125  yInfo() << log_ID << " - seed:" << paramsd["seed"];
126 
127  yInfo() << log_ID << " - use_thumb:" << paramsd["use_thumb"];
128  yInfo() << log_ID << " - use_forearm:" << paramsd["use_forearm"];
129 
130  yInfo() << log_ID << " - likelihood_gain:" << paramsd["likelihood_gain"];
131 
132  yInfo() << log_ID << " - resample_ratio:" << paramsd["resample_ratio"];
133  yInfo() << log_ID << " - prior_ratio:" << paramsd["prior_ratio"];
134 
135  yInfo() << log_ID << " - gate_x:" << paramsd["gate_x"];
136  yInfo() << log_ID << " - gate_y:" << paramsd["gate_y"];
137  yInfo() << log_ID << " - gate_z:" << paramsd["gate_z"];
138  yInfo() << log_ID << " - gate_aperture:" << paramsd["gate_aperture"];
139  yInfo() << log_ID << " - gate_rotation:" << paramsd["gate_rotation"];
140 
141 
142  /* INITIALIZATION */
143  std::unique_ptr<Initialization> init_arm(new InitiCubArm("hand-tracking/InitiCubArm", paramss["cam_sel"], paramss["laterality"]));
144 
145 
146  /* MOTION MODEL */
147  std::unique_ptr<StateModel> brown(new BrownianMotionPose(paramsd["q_xy"], paramsd["q_z"], paramsd["theta"], paramsd["cone_angle"], paramsd["seed"]));
148 
149  std::unique_ptr<ExogenousModel> icub_motion;
150  if (paramsd["play"] != 1.0)
151  {
152  std::unique_ptr<iCubFwdKinModel> icub_fwdkin(new iCubFwdKinModel(paramss["robot"], paramss["laterality"], paramss["cam_sel"]));
153  icub_motion = std::move(icub_fwdkin);
154  }
155  else
156  {
157  std::unique_ptr<PlayFwdKinModel> play_fwdkin(new PlayFwdKinModel(paramss["robot"], paramss["laterality"], paramss["cam_sel"]));
158  icub_motion = std::move(play_fwdkin);
159  }
160 
161  /* PREDICTION */
162  std::unique_ptr<DrawFwdKinPoses> pf_prediction(new DrawFwdKinPoses());
163  pf_prediction->setStateModel(std::move(brown));
164  pf_prediction->setExogenousModel(std::move(icub_motion));
165 
166 
167  /* SENSOR MODEL */
168  std::unique_ptr<VisualProprioception> proprio;
169  try
170  {
171  std::unique_ptr<VisualProprioception> vp(new VisualProprioception(paramsd["use_thumb"],
172  paramsd["use_forearm"],
173  paramsd["num_images"],
174  paramsd["resolution_ratio"],
175  paramss["cam_sel"],
176  paramss["laterality"],
177  rf.getContext()));
178 
179  proprio = std::move(vp);
180  paramsd["num_particles"] = proprio->getOGLTilesRows() * proprio->getOGLTilesCols() * paramsd["gpu_count"];
181  paramsd["cam_width"] = proprio->getCamWidth();
182  paramsd["cam_height"] = proprio->getCamHeight();
183  }
184  catch (const std::runtime_error& e)
185  {
186  yError() << e.what();
187  return EXIT_FAILURE;
188  }
189 
190  /* CORRECTION */
191  std::unique_ptr<PFVisualCorrection> vpf_correction;
192 
193  std::unique_ptr<PFVisualCorrection> vpf_update_particles(new VisualUpdateParticles(std::move(proprio), paramsd["likelihood_gain"], paramsd["gpu_count"]));
194 
195  if (paramsd["gate_pose"] == 1.0)
196  {
197  if (paramsd["play"] != 1.0)
198  {
199  std::unique_ptr<iCubGatePose> icub_gate_pose(new iCubGatePose(std::move(vpf_update_particles),
200  paramsd["gate_x"], paramsd["gate_y"], paramsd["gate_z"],
201  paramsd["gate_aperture"], paramsd["gate_rotation"],
202  paramss["robot"], paramss["laterality"], paramss["cam_sel"]));
203  vpf_correction = std::move(icub_gate_pose);
204  }
205  else
206  {
207  std::unique_ptr<PlayGatePose> icub_gate_pose(new PlayGatePose(std::move(vpf_update_particles),
208  paramsd["gate_x"], paramsd["gate_y"], paramsd["gate_z"],
209  paramsd["gate_aperture"], paramsd["gate_rotation"],
210  paramss["robot"], paramss["laterality"], paramss["cam_sel"]));
211  vpf_correction = std::move(icub_gate_pose);
212  }
213  }
214  else
215  vpf_correction = std::move(vpf_update_particles);
216 
217 
218  /* RESAMPLING */
219  std::unique_ptr<Resampling> pf_resampling;
220  if (paramsd["resample_prior"] != 1.0)
221  {
222  std::unique_ptr<Resampling> resampling(new Resampling());
223 
224  pf_resampling = std::move(resampling);
225  }
226  else
227  {
228  std::unique_ptr<InitiCubArm> resample_init_arm(new InitiCubArm("hand-tracking/ResamplingWithPrior/InitiCubArm", paramss["cam_sel"], paramss["laterality"]));
229  std::unique_ptr<Resampling> resampling_prior(new ResamplingWithPrior(std::move(resample_init_arm), paramsd["prior_ratio"]));
230 
231  pf_resampling = std::move(resampling_prior);
232  }
233 
234 
235  /* PARTICLE FILTER */
236  VisualSIS vsis_pf(paramss["cam_sel"],
237  paramsd["cam_width"], paramsd["cam_height"],
238  paramsd["num_particles"],
239  paramsd["resample_ratio"]);
240  vsis_pf.setInitialization(std::move(init_arm));
241  vsis_pf.setPrediction(std::move(pf_prediction));
242  vsis_pf.setCorrection(std::move(vpf_correction));
243  vsis_pf.setResampling(std::move(pf_resampling));
244 
245 
246  vsis_pf.boot();
247  vsis_pf.wait();
248 
249 
250  yInfo() << log_ID << "Application closed succesfully.";
251  return EXIT_SUCCESS;
252 }
253 
254 
255 /* UTILITY FUNCTIONS */
256 std::string engine_count_to_string(int engine_count)
257 {
258  if (engine_count == 0) return "concurrency is unsupported on this device";
259  if (engine_count == 1) return "the device can concurrently copy memory between host and device while executing a kernel";
260  if (engine_count == 2) return "the device can concurrently copy memory between host and device in both directions and execute a kernel at the same time";
261  return "wrong argument...!";
262 }
int main(int argc, char *argv[])
Definition: main.cpp:38
std::string engine_count_to_string(int engine_count)
Definition: main.cpp:256