-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmujinshowresults.cpp
113 lines (98 loc) · 4.54 KB
/
mujinshowresults.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
// -*- coding: utf-8 -*-
/** \example mujinshowresults.cpp
Shows how to get the result data data from an already generated task and optimization results.
*/
#include <mujincontrollerclient/mujincontrollerclient.h>
#include <iostream>
#include <vector>
using namespace std;
using namespace mujinclient;
int main(int argc, char ** argv)
{
if( argc < 2 ) {
std::cout << "need username:password. Example: mujinclienttest myuser:mypass [url]\n\nurl - [optional] For example https://controller.mujin.co.jp/" << std::endl;
return 1;
}
try {
ControllerClientPtr controller;
if( argc >= 5 ) {
controller = CreateControllerClient(argv[1], argv[2], argv[3], argv[4]);
}
if( argc == 4 ) {
controller = CreateControllerClient(argv[1], argv[2], argv[3]);
}
else if( argc == 3 ) {
controller = CreateControllerClient(argv[1], argv[2]);
}
else {
BOOST_ASSERT(0);
}
std::cout << "connected to controller v" << controller->GetVersion() << std::endl;
// get all supported keys
std::vector<std::string> scenekeys;
controller->GetScenePrimaryKeys(scenekeys);
cout << "user has " << scenekeys.size() << " scenes: " << endl;
for(size_t i = 0; i < scenekeys.size(); ++i) {
cout << scenekeys[i] << endl;
}
SceneResourcePtr scene;
// if YG_LAYOUT exists, open it, otherwise open the first file
if( find(scenekeys.begin(),scenekeys.end(), string("YG_LAYOUT")) != scenekeys.end() ) {
scene.reset(new SceneResource(controller, "YG_LAYOUT"));
}
else {
cout << "opening scene " << scenekeys.at(0) << endl;
scene.reset(new SceneResource(controller, scenekeys.at(0)));
}
// open the first task
std::vector<std::string> taskkeys;
scene->GetTaskPrimaryKeys(taskkeys);
if( taskkeys.size() == 0 ) {
std::cout << "no tasks for this scene" << std::endl;
return 0;
}
TaskResourcePtr task(new TaskResource(controller, taskkeys.at(0)));
cout << "got task " << task->Get<string>("name") << endl;
cout << "program is " << task->Get<string>("taskgoalxml") << endl;
PlanningResultResourcePtr result = task->GetResult();
EnvironmentState envstate;
if( !!result ) {
cout << "result for task exists and can be completed in " << result->Get<string>("task_time") << " seconds." << endl;
}
// get the first optimization
std::vector<std::string> optimizationkeys;
task->GetOptimizationPrimaryKeys(optimizationkeys);
OptimizationResourcePtr optimization(new OptimizationResource(controller, optimizationkeys.at(0)));
cout << "found optimization " << optimization->Get<string>("name") << endl;
std::vector<PlanningResultResourcePtr> results;
optimization->GetResults(results,0,10);
if( results.size() > 0 ) {
cout << "the top results have times: ";
for(size_t i = 0; i < results.size(); ++i) {
cout << results[i]->Get<string>("task_time") << ", ";
}
cout << endl;
PlanningResultResourcePtr bestresult = results.at(0);
bestresult->GetEnvironmentState(envstate);
cout << "robot position of best result is: ";
for(EnvironmentState::iterator it = envstate.begin(); it != envstate.end(); ++it) {
InstanceObjectState objstate = it->second;
// for now only output translate
cout << it->first << "=(" << objstate.transform.translate[0] << ", " << objstate.transform.translate[1] << ", " << objstate.transform.translate[2] << "), ";
}
cout << endl;
RobotControllerPrograms programs;
result->GetPrograms(programs);
std::cout << "found " << programs.programs.size() << " programs" << std::endl;
for(std::map<std::string, RobotProgramData>::iterator it = programs.programs.begin(); it != programs.programs.end(); ++it ) {
std::cout << "[" << it->first << "]" << std::endl << it->second.programdata << std::endl << std::endl;
}
std::cout << "final task_time is " << result->Get<string>("task_time") << std::endl;
}
}
catch(const MujinException& ex) {
std::cout << "exception thrown: " << ex.message() << std::endl;
}
// destroy all mujin controller resources
DestroyControllerClient();
}