24 #include <yarp/math/Math.h> 26 #include "superqModule.h" 28 #include "src/superquadricModel_IDL.h" 37 bool SuperqModule::attach(RpcServer &source)
39 return this->yarp().attachAsServer(source);
48 outputFileName=homeContextPath+
"/"+tag_file+
".txt";
49 yDebug()<<
" [SuperqModule]: File output "<<outputFileName;
51 superqCom->setPar(
"tag_file", tag_file);
74 if ((e==
"on") || (e==
"off"))
78 if ((visualization_on==
false) && (e==
"on"))
82 visualization_on=
true;
85 else if ((visualization_on==
true) && (e==
"off"))
88 visualization_on=
false;
105 superqCom->setPar(
"object_class", object_class);
107 superqCom->setPar(
"one_shot",
"on");
111 for (
size_t i=0; i<p.size(); i++)
112 p_aux.push_back(p[i]);
114 superqCom->sendPoints(p_aux);
119 sol=superqCom->getSolution(0);
121 superqCom->setPar(
"one_shot",
"off");
124 superqCom->sendPoints(p_aux);
126 superq=fillProperty(sol);
138 superqCom->setPar(
"object_class", object_class);
140 yDebug()<<
"Time operations after set par 1"<<Time::now() - t;
142 superqCom->setPar(
"one_shot",
"on");
144 yDebug()<<
"Time operations after set par 2"<<Time::now() - t;
148 for (
size_t i=0; i<p.size(); i++)
149 p_aux.push_back(p[i]);
151 superqCom->sendPoints(p_aux);
152 yDebug()<<
"Time operations send points "<<Time::now() - t;
154 t_fin= Time::now() - t;
156 yDebug()<<
"Time operations final"<<t_fin;
164 superqCom->resetMedianFilter();
174 sol=superqCom->getSolution(1);
176 superqCom->setPar(
"one_shot",
"off");
179 superqCom->sendPoints(p_aux);
181 superq=fillProperty(sol);
192 Bottle &b1=bottle.addList();
193 b1.addDouble(sol[0]); b1.addDouble(sol[1]); b1.addDouble(sol[2]);
194 superq.put(
"dimensions", bottle.get(0));
196 Bottle &b2=bottle.addList();
197 b2.addDouble(sol[3]); b2.addDouble(sol[4]);
198 superq.put(
"exponents", bottle.get(1));
200 Bottle &b3=bottle.addList();
201 b3.addDouble(sol[5]); b3.addDouble(sol[6]); b3.addDouble(sol[7]);
202 superq.put(
"center", bottle.get(2));
204 Bottle &b4=bottle.addList();
205 Vector orient=dcm2axis(euler2dcm(sol.subVector(8,10)));
206 b4.addDouble(orient[0]); b4.addDouble(orient[1]); b4.addDouble(orient[2]); b4.addDouble(orient[3]);
207 superq.put(
"orientation", bottle.get(3));
214 if ((entry==
"on") || (entry==
"off"))
217 filter_points=(entry==
"on");
221 options.put(
"filter_radius", radius);
222 options.put(
"filter_nnThreshold", nnThreshold);
223 superqCom->setPointsFilterPar(options,
false);
224 superqCom->setPar(
"filter_points",
"on");
227 superqCom->setPar(
"filter_points",
"off");
229 yInfo()<<
"[SuperqModule]: filter_points "<<filter_points;
230 yInfo()<<
"[SuperqModule]: radius "<<radius;
231 yInfo()<<
"[SuperqModule]: nn-thrshold "<<nnThreshold;
257 if ((entry==
"on") || (entry==
"off"))
260 filter_superq= (entry==
"on");
264 options.put(
"median_order", median_order);
266 options.put(
"fixed_window",
"on");
268 options.put(
"fixed_window",
"off");
270 superqCom->setSuperqFilterPar(options,
false);
271 superqCom->setPar(
"filter_superq",
"on");
274 superqCom->setPar(
"filter_superq",
"off");
276 yInfo()<<
"[SuperqModule]: filter_superq "<<filter_superq;
277 yInfo()<<
"[SuperqModule]: fixed_window "<<fixed_window;
278 yInfo()<<
"[SuperqModule]: median_order "<<median_order;
279 yInfo()<<
"[SuperqModule]: min_median_order "<<min_median_order;
280 yInfo()<<
"[SuperqModule]: max_median_order "<<max_median_order;
319 if ((entry==
"on") || (entry==
"off"))
321 save_points=(entry==
"on");
323 superqCom->setPar(
"save_points",
"on");
325 superqCom->setPar(
"save_points",
"off");
333 if (field==
"points_filter")
334 advOptions=superqCom->getPointsFilterPar();
335 else if (field==
"superq_filter")
336 advOptions=superqCom->getSuperqFilterPar();
337 else if (field==
"optimization")
338 advOptions=superqCom->getIpoptPar();
339 else if (field==
"visualization")
340 advOptions=superqVis->getPar();
341 else if (field==
"statistics")
343 advOptions.put(
"average_computation_time", t_superq);
344 advOptions.put(
"average_visualization_time", t_vis);
353 if (field==
"points_filter")
354 superqCom->setPointsFilterPar(newOptions,
false);
355 else if (field==
"superq_filter")
356 superqCom->setSuperqFilterPar(newOptions,
false);
357 else if (field==
"optimization")
358 superqCom->setIpoptPar(newOptions,
false);
359 else if (field==
"visualization")
360 superqVis->setPar(newOptions,
false);
370 object_class=objclass;
387 yDebug()<<
"[SuperqModule]: start updatemodule";
391 superqCom->setPar(
"object_class", object_class);
392 yDebug()<<
"[SuperqModule]: set object class";
394 Property &x_to_send=portSuperq.prepare();
395 yDebug()<<
"[SuperqModule]: prepare port";
397 if (times_superq.size()<10)
398 times_superq.push_back(superqCom->getTime());
399 else if (times_superq.size()==10)
401 for (
size_t i=0; i<times_superq.size(); i++)
403 t_superq+=times_superq[i];
405 t_superq=t_superq/times_superq.size();
406 times_superq.push_back(0.0);
409 times_superq.clear();
411 yDebug()<<
"[SuperqModule]: fill solution";
414 x_to_send=fillProperty(x);
416 x_to_send=fillProperty(x_filtered);
418 yDebug()<<
"[SuperqModule]: send superq";
422 yDebug()<<
"[SuperqModule]: superq sent";
424 if (visualization_on)
426 if (times_vis.size()<10)
427 times_vis.push_back(superqVis->getTime());
428 else if (times_vis.size()==10)
430 for (
size_t i=0; i<times_vis.size(); i++)
434 t_vis=t_vis/times_vis.size();
435 times_vis.push_back(0.0);
441 yDebug()<<
"[Module ] restarting ...";
446 superqCom->threadInit();
447 superqCom->sendPoints(points_aux);
449 superqCom->setPar(
"object_class", object_class);
451 if ((filter_points==
true) && (points.size()>0))
458 go_on=superqCom->computeSuperq();
461 if ((go_on==
false) && (points.size()>0))
463 yError(
"[SuperqModule]: Not found a suitable superquadric! ");
465 else if ((go_on==
true) && (norm(x)>0.0))
468 superqCom->filterSuperq();
471 x=superqCom->getSolution(
false);
472 x_filtered=superqCom->getSolution(
true);
487 yInfo()<<
"[SuperqModule]: Configuring ... ";
491 if (filter_points==
true)
493 if (filter_superq==
true)
494 configFilterSuperq(rf);
499 config_ok=configViewer(rf);
500 if (config_ok==
false)
503 superqCom=
new SuperqComputation(mutex_shared,rate, filter_points, filter_superq, fixed_window, points, tag_file,
504 threshold_median,filter_points_par, x, x_filtered, filter_superq_par, ipopt_par,
505 homeContextPath, save_points, this->rf);
509 bool thread_started=superqCom->start();
512 yInfo()<<
"[SuperqModule]: Computetion thread started!";
514 yError()<<
"[SuperqModule]: Problems in starting the computation thread!";
517 superqVis=
new SuperqVisualization(rate_vis,eye, what_to_plot,x, x_filtered, Color, igaze, K, points, vis_points, vis_step);
519 if (visualization_on)
521 bool thread_started=superqVis->start();
524 yInfo()<<
"[SuperqModule]: Visualization thread started!";
526 yError()<<
"[SuperqModule]: Problems in starting the visualization thread!";
530 superqVis->threadInit();
539 yInfo()<<
"[SuperqModule]: Interruping ... ";
541 superqVis->interruptPorts();
542 portSuperq.interrupt();
550 yInfo()<<
"[SuperqModule]: Closing ... ";
559 if (portRpc.asPort().isOpen())
562 if (!portSuperq.isClosed())
565 if (mode_online && visualization_on)
574 homeContextPath=rf.getHomeContextPath().c_str();
575 pointCloudFileName=rf.findFile(
"pointCloudFile");
576 save_points=(rf.check(
"save_points", Value(
"no")).asString()==
"yes");
578 visualization_on=(rf.check(
"visualization_on", Value(
"yes")).asString()==
"yes");
580 rate=rf.check(
"rate", Value(100)).asInt();
581 rate_vis=rf.check(
"rate_vis", Value(100)).asInt();
583 threshold_median=rf.check(
"threshold_median", Value(0.1)).asDouble();
585 object_class=rf.check(
"object_class", Value(
"default")).asString();
587 if (rf.find(
"pointCloudFile").isNull())
594 outputFileName=rf.findFile(
"outputFile");
596 if (rf.find(
"outputFile").isNull())
597 outputFileName=homeContextPath+
"/output.txt";
599 yDebug()<<
"file output "<<outputFileName;
602 filter_points=(rf.check(
"filter_points", Value(
"off")).asString()==
"on");
603 filter_superq=(rf.check(
"filter_superq", Value(
"off")).asString()==
"on");
605 yInfo()<<
"[SuperqModule]: rate "<<rate;
606 yInfo()<<
"[SuperqModule]: filter_points "<<filter_points;
607 yInfo()<<
"[SuperqModule]: filter_superq "<<filter_superq;
615 radius=rf.check(
"radius", Value(0.005)).asDouble();
616 nnThreshold=rf.check(
"nn-threshold", Value(100)).asInt();
618 filter_points_par.put(
"filter_radius",radius);
619 filter_points_par.put(
"filter_nnThreshold",nnThreshold);
621 yInfo()<<
"[SuperqModule]: radius "<<radius;
622 yInfo()<<
"[SuperqModule]: nn-threshold "<<nnThreshold;
630 fixed_window=(rf.check(
"fixed_window", Value(
"off")).asString()==
"on");
631 reset=(rf.check(
"reset", Value(
"off")).asString()==
"on");
632 median_order=rf.check(
"median_order", Value(1)).asInt();
633 min_median_order=rf.check(
"min_median_order", Value(1)).asInt();
634 max_median_order=rf.check(
"max_median_order", Value(30)).asInt();
635 min_norm_vel=rf.check(
"min_norm_vel", Value(0.01)).asDouble();
638 filter_superq_par.put(
"fixed_window",fixed_window);
639 filter_superq_par.put(
"median_order",median_order);
640 filter_superq_par.put(
"min_median_order",min_median_order);
641 filter_superq_par.put(
"max_median_order",max_median_order);
642 filter_superq_par.put(
"threshold_median",threshold_median);
643 filter_superq_par.put(
"min_norm_vel",min_norm_vel);
645 yInfo()<<
"[SuperqModule]: fixed_window "<<fixed_window;
646 yInfo()<<
"[SuperqModule]: median_order "<<median_order;
647 yInfo()<<
"[SuperqModule]: min_median_order "<<min_median_order;
648 yInfo()<<
"[SuperqModule]: max_median_order "<<max_median_order;
649 yInfo()<<
"[SuperqModule]: threshold_median "<<threshold_median;
650 yInfo()<<
"[SuperqModule]: min_norm_vel "<<min_norm_vel;
658 portRpc.open(
"/superquadric-model/rpc");
659 portSuperq.open(
"/superquadric-model/superq:o");
671 optimizer_points=rf.check(
"optimizer_points", Value(300)).asInt();
672 max_cpu_time=rf.check(
"max_cpu_time", Value(5.0)).asDouble();
674 tol=rf.check(
"tol",Value(1e-5)).asDouble();
675 acceptable_iter=rf.check(
"acceptable_iter",Value(0)).asInt();
676 max_iter=rf.check(
"max_iter",Value(numeric_limits<int>::max())).asInt();
678 mu_strategy=rf.find(
"mu_strategy").asString().c_str();
679 if (rf.find(
"mu_strategy").isNull())
680 mu_strategy=
"adaptive";
682 nlp_scaling_method=rf.find(
"nlp_scaling_method").asString().c_str();
683 if (rf.find(
"nlp_scaling_method").isNull())
684 nlp_scaling_method=
"none";
686 ipopt_par.put(
"optimizer_points",optimizer_points);
687 ipopt_par.put(
"max_cpu_time", max_cpu_time);
688 ipopt_par.put(
"tol",tol);
689 ipopt_par.put(
"acceptable_iter",acceptable_iter);
690 ipopt_par.put(
"max_iter",max_iter);
691 ipopt_par.put(
"mu_strategy",mu_strategy);
692 ipopt_par.put(
"nlp_scaling_method",nlp_scaling_method);
694 yInfo()<<
"[SuperqModule]: optimizer_points "<<optimizer_points;
695 yInfo()<<
"[SuperqModule]: max_cpu_time "<<max_cpu_time;
696 yInfo()<<
"[SuperqModule]: tol "<<tol;
697 yInfo()<<
"[SuperqModule]: acceptable_iter "<<acceptable_iter;
698 yInfo()<<
"[SuperqModule]: max_iter "<<max_iter;
699 yInfo()<<
"[SuperqModule]: mu_strategy "<<mu_strategy;
700 yInfo()<<
"[SuperqModule]: nlp_scaling_method "<<nlp_scaling_method;
708 eye=rf.check(
"eye", Value(
"left")).asString();
709 what_to_plot=rf.find(
"plot").asString().c_str();
710 if (rf.find(
"plot").isNull())
711 what_to_plot=
"superq";
713 if (Bottle *B=rf.find(
"color").asList())
717 for (
int i=0; i<B->size();i++)
718 Color.push_back(B->get(i).asInt());
721 r=Color[0]; g=Color[1]; b=Color[2];
728 if (mode_online && visualization_on)
731 optionG.put(
"device",
"gazecontrollerclient");
732 optionG.put(
"remote",
"/iKinGazeCtrl");
733 optionG.put(
"local",
"/superquadric-model/gaze");
735 GazeCtrl.open(optionG);
739 if (GazeCtrl.isValid())
740 GazeCtrl.view(igaze);
745 igaze->getInfo(info);
753 intr_par=info.find(
"camera_intrinsics_left").asList();
755 intr_par=info.find(
"camera_intrinsics_right").asList();
757 K(0,0)=intr_par->get(0).asDouble();
758 K(0,1)=intr_par->get(1).asDouble();
759 K(0,2)=intr_par->get(2).asDouble();
760 K(1,1)=intr_par->get(5).asDouble();
761 K(1,2)=intr_par->get(6).asDouble();
766 point2D.resize(2,0.0);
768 point1.resize(3,0.0);
781 fout.open(outputFileName.c_str());
784 fout<<
"*****Result*****"<<endl;
785 fout<<
"Computed superquadric: "<<endl;
786 fout<<
" "<<x.toString(3,3);
788 fout<<
"Filtered superquadric: "<<endl;
789 fout<<
" "<<x_filtered.toString(3,3);
791 fout<<
"Execution time: "<<endl;
792 fout<<
" "<<t_superq <<endl;
793 fout<<
"Update module time"<<endl;
794 fout<<
" "<<t<<endl<<endl;
795 fout<<
"*****Optimizer parameters*****"<<endl;
796 fout<<
"Optimizer points: "<<optimizer_points<<endl;
797 fout<<
"Tolerance :"<<tol<<endl;
798 fout<<
"Nlp scaling method: "<<nlp_scaling_method<<endl;
799 fout<<
"Mu strategy: "<<mu_strategy<<endl;
807 ifstream pointsFile(pointCloudFileName.c_str());
813 if (!pointsFile.is_open())
815 yError()<<
"problem opening point cloud file!";
819 while (!pointsFile.eof())
821 pointsFile.getline(line,
sizeof(line),
'\n');
823 Value firstItem=b.get(0);
824 bool isNumber=firstItem.isInt() || firstItem.isDouble();
828 string tmp=firstItem.asString().c_str();
829 std::transform(tmp.begin(),tmp.end(),tmp.begin(),::toupper);
830 if (tmp==
"OFF" || tmp==
"COFF")
837 nPoints=firstItem.asInt();
843 if (isNumber && (b.size()>=3))
846 point[0]=b.get(0).asDouble();
847 point[1]=b.get(1).asDouble();
848 point[2]=b.get(2).asDouble();
849 points_aux.push_back(point);
bool send_point_clouds(const std::vector< yarp::sig::Vector > &p)
Get the point cloud for computing the superquadric.
bool close()
close function of RF module
bool set_tag_file(const std::string &tag_file)
Set a tag name for saving the superquadric.
bool set_points_filtering(const std::string &entry)
Set if to filter or not the point cloud.
std::string get_superq_filtering()
Get if the superquadric is filtered or not.
bool updateModule()
updateModule function of RF module
bool interruptModule()
interrupt module function of RF module
bool configServices(yarp::os::ResourceFinder &rf)
Open ports for communication.
This class shows the point cloud used for modeling or the estimated superquadric overlapped on the ca...
bool configOnOff(yarp::os::ResourceFinder &rf)
Configure all on/off options.
std::string get_visualization()
Return if visualization is on or off.
std::string get_points_filtering()
Get if the point cloud is filtered or not.
void saveSuperq()
Save computed superquadric.
bool configFilter(yarp::os::ResourceFinder &rf)
Configure point cloud filter options.
bool configViewer(yarp::os::ResourceFinder &rf)
Configure visualization options.
yarp::os::Property get_options(const std::string &field)
Get options of a given field: visualization, optimization, filtering.
bool set_superq_filtering(const std::string &entry)
Set if to filter or not the superquadric.
bool configFilterSuperq(yarp::os::ResourceFinder &rf)
Configure superquadric filter options.
bool readPointCloud()
In offline mode, read the point cloud from a txt file.
std::string get_save_points()
Get if the used point cloud is saved or not.
yarp::os::Property get_superq_filtered()
Return the filtered superquadric.
This class provides a thread for computing in real time the superquadric, filtering the point cloud a...
double getPeriod()
Get period function of RF module.
bool configure(yarp::os::ResourceFinder &rf)
configure function of RF module
yarp::os::Property get_superq(const std::vector< yarp::sig::Vector > &blob)
Return the computed superquadric, given the 2D blob of the object.
bool set_options(const yarp::os::Property &newOptions, const std::string &field)
Set options of specified field: visualization, optimization, filtering.
bool reset_filter()
Reset median filter for improving superquadric estimation.
bool configSuperq(yarp::os::ResourceFinder &rf)
Configure superquadric computation otpions.
std::string get_tag_file()
Get the tag name used for saving the superquadric.
bool set_visualization(const std::string &e)
Set if visualization is on or off.
bool set_object_class(const std::string &objclass)
Set object class for improving superquadric estimation.
bool set_save_points(const std::string &entry)
Set if to save or not the used point cloud.
yarp::os::Property fillProperty(const yarp::sig::Vector &sol)
Property fill the property with the superquadric solution.