-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathControllerSample_11.cpp
398 lines (262 loc) · 10.5 KB
/
ControllerSample_11.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
#include <string>
#include "ControllerEvent.h"
#include "Controller.h"
#include "Logger.h"
#include <iostream>
#include <fstream>
#include <iomanip>
#include <boost/python.hpp>
#include <Python.h>
#include <dlfcn.h>
#include <typeinfo>
#include <ctime>
#include <unistd.h>
#include <sys/time.h>
#include "StopMotion.h"
#include "ControlPosition.h"
#include "ApplyAction.h"
#include "MeasureDisplacement.h"
#include "EntityMotionStatus.h"
#include "ResetEntity.h"
#define PI 3.141592
#define DEG2RAD(DEG) ( (PI) * (DEG) / 180.0 )
using namespace std;
ofstream myfile ("example_11.csv", ios_base::trunc);
class MyController : public Controller {
public:
void onInit(InitEvent &evt);
double onAction(ActionEvent&);
void onRecvMsg(RecvMsgEvent &evt);
void onCollision(CollisionEvent &evt);
void applyTapAction();
private:
const char* linkName;
CParts *link;
Vector3d initTargetPos;
Rotation initTargetRotation;
Vector3d initToolPos;
Rotation initToolRotation;
Rotation newToolRotation;
Vector3d currentToolPos;
Vector3d currentTargetPos;
Vector3d linkInitPos, linkPos;
Rotation linkInitRotation, linkCurrentRotation;
Vector3d toolVelAtHit;
bool positionData;
bool didToolHit; // to check if collision with target has happened
bool isTargetAtInitLoc; // to check if target is at initial required position
bool isToolAtInitLoc; // to check if tool is at initial required position
bool isTargetAtInitPose; // to check if target is at initial set orientation
bool isToolAtInitPose; // to check if tool is at its initial set orientation
bool isTargetAtRest; // to check if target is not moving
bool isToolAtRest; // to check if tool is not moving
bool isNewPoseObtained; // to set tool in a new pose
bool isToolAtDesiredPosition;
int simulationCount;
int manipulationsPerSample;
int totalSamples;
double *ptrTool;
double torque;
bool moveForward;
bool onRecvMsgFunction;
bool isXForceVarianceAllowed ;
bool isZForceVarianceAllowed ;
double f_x, f_z;
bool flag;
};
void MyController::onInit(InitEvent &evt) {
totalSamples = 240;
simulationCount = 0;
SimObj *box = getObj("box_001");
// SimObj *box = getObj("cylinder_001");
SimObj *TShapeTool = getObj("TShapeTool_001");
box->getPosition(initTargetPos);
box->getRotation(initTargetRotation);
TShapeTool->getPosition(initToolPos);
TShapeTool->getRotation(initToolRotation);
didToolHit = false;
flag = true;
isTargetAtInitLoc = true;
isToolAtInitLoc = true;
isTargetAtInitPose = true;
isToolAtInitPose = true;
isTargetAtRest = true;
isToolAtRest = true;
isNewPoseObtained = false;
isToolAtDesiredPosition = false;
f_x = 0 ;
f_z = 0 ;
ptrTool = NULL;
moveForward = false;
onRecvMsgFunction = false;
// std::vector<std::string> s;
// for(std::map<std::string, CParts *>::iterator it = tool->getPartsCollection().begin(); it != tool->getPartsCollection().end(); ++it){
// if (it->first != "body")
// s.push_back(it->first);
// }
// std::string linkName;
// Size si;
// Vector3d pos;
// for (int i = 0; i < s.size(); i++){
// const char* linkName = s[i].c_str();
// CParts *link = tool->getParts(linkName);
// link->getPosition(pos);
// link->getRotation(linkRotation);
// }
myfile.flush(); // I have uncommented the file, because I want to overwrite the file.
if (myfile.is_open())
{
myfile << "Action" << " , " << "FunctionalFeature" << " , " ;
myfile << "forceOnTool_X" << " , " << "forceOnTool_Z " << " , ";
myfile << "toolVelAtHit_X" << " , " << "toolVelAtHit_Z" << " , ";
myfile << "InitialToolPos_X" << " , " << "InitialToolPos_Z" << " , ";
myfile << "FinalToolPos_X" << " , " << "FinalToolPos_Z" << " , ";
myfile << "InitialLinkPos_X" << " , " << "InitialToolPos_Z" << " , ";
myfile << "InitialTargetPos_X" << " , " << "InitialTargetPos_Z" << " , ";
myfile << "targetFinalPos_X" << " , " << "targetFinalPos_Z" << " , ";
// myfile << "InitialTargetOri_X" << " , " << "InitialTargetOri_Z" << " , ";
// myfile << "targetFinalOri_X" << " , " << "targetFinalOri_Z" << " , ";
myfile << "targetPlacement_X" << " , " << "targetPlacement_Z";
myfile << "targetDisplacement_X" << " , " << "targetDisplacement_Z";
// myfile << "linkInitialPos_X" << ", " << "linkInitialPos_Z" << " , "
// myfile << "linkInitialOri_X" << ", " << "linkInitialOri_Z";
myfile << "\n" ;
}
}
double MyController::onAction(ActionEvent &evt) {
int actionNumber = 1;
int functionalFeature = 1;
myfile << setprecision(2) << std::fixed;
SimObj *box = getObj("box_001");
// SimObj *box = getObj("cylinder_001");
SimObj *TShapeTool = getObj("TShapeTool_001");
SimObj *toolName = TShapeTool;
SimObj *target = box;
int xForceVariance = 500;
int zForceVariance = 500;
double forceOnTool_x, forceOnTool_z;
forceOnTool_x = 0;
// forceOnTool_x = -1000 - f_x;
forceOnTool_z = 1000 + f_z;
// forceOnTool_z = 0;
Vector3d forceOnTool;
forceOnTool.set(forceOnTool_x, 0 , forceOnTool_z);
if( !didToolHit && isTargetAtInitLoc && isToolAtInitLoc && isToolAtInitPose && isTargetAtInitPose )
{
// cout << "Into the loop" << endl;
// cout << "The tapping force applied on tool is " << forceOnTool_x << ", " << 0 << " , " << forceOnTool_z << endl;
maintainOrientationOfTool(toolName, initToolRotation);
tapWithTool(toolName, initToolRotation, forceOnTool);
}
// cout << "Outermost loop" << endl;
if (didToolHit)
{
isToolAtRest = checkEntityMotionStatus(toolName); // checks whether the tool is moving by calculating its velocity
isTargetAtRest = checkEntityMotionStatus(target); // checks whether the object is moving by calculating its velocity
if(isToolAtRest)
{
toolName->getPosition(currentToolPos);
}
if (isToolAtRest)
{
// reset the tool position to its initial location
while (!isToolAtInitLoc)
{
isToolAtInitLoc = resetEntityPosition(toolName, initToolPos);
// cout << "Tool Position is reset " << isToolAtInitLoc << endl;
}
while (isToolAtInitLoc && !isToolAtInitPose)
{
isToolAtInitPose = resetEntityOrientation(toolName,initToolRotation);
// newToolRotation = resetToNewToolRotation();
if(isToolAtInitPose)
{
// cout << "Tool Pose is set " << endl;
}
}
}
if (isTargetAtRest && isToolAtRest && isToolAtInitLoc && isToolAtInitPose)
{
target->getPosition(currentTargetPos);
while(!isTargetAtInitLoc)
{
isTargetAtInitLoc = resetEntityPosition(target, initTargetPos);
// cout << "Target position is reset " << isTargetAtInitLoc << endl;
}
while(isTargetAtInitLoc && !isTargetAtInitPose)
{
isTargetAtInitPose = resetRandomTargetOrientation(target);
cout << "Target Orientation is Reset " << isTargetAtInitPose << endl;
// isTargetAtInitPose = true;
}
cout << "onAction " << &linkName << endl;
link->getPosition(linkInitPos);
}
if (isTargetAtRest && isTargetAtInitLoc && isTargetAtInitPose && isToolAtRest && isToolAtInitLoc && isToolAtInitPose)
{
didToolHit = false;
myfile << actionNumber << " , " << functionalFeature << " , " ;
myfile << forceOnTool_x << " , " << forceOnTool_z << " , " ;
myfile << toolVelAtHit.x() << " , " << toolVelAtHit.z() << " , " ;
myfile << initToolPos.x() << " , " << initToolPos.z() << " , " ;
myfile << currentToolPos.x() << " , " << currentToolPos.z() << " , " ;
myfile << linkInitPos.x() << " , " << linkInitPos.z() << " , " ;
myfile << initTargetPos.x() << " , " << initTargetPos.z() << " , " ;
myfile << currentTargetPos.x() << " , " << currentTargetPos.z() << " , " ;
myfile << linkInitPos.x() - initTargetPos.x() << " , " << linkInitPos.z() - initTargetPos.z() << " , ";
myfile << currentTargetPos.x() - initTargetPos.x() << " , " << currentTargetPos.z() - initTargetPos.z();
myfile << "\n";
simulationCount++;
cout << "The simulation count = " << simulationCount << endl;
// Reset the forces applied
double r;
r = ((double) rand() / (RAND_MAX)) ;
cout << "f_x = " << f_x << endl;
f_x = r * int(xForceVariance);
f_z = r * int(zForceVariance);
}
}
if (simulationCount == totalSamples)
{
myfile.close();
cout << "The number of times too had been manipulated = " << totalSamples << endl;
cout << "The simulation process has ended successfully " << endl;
exit(0);
}
return 0.01;
}
std::string msg = " ";
void MyController::onRecvMsg(RecvMsgEvent &evt) {
}
void MyController::onCollision(CollisionEvent &evt) {
SimObj *TShapeTool = getObj("TShapeTool_001");
SimObj *toolName = TShapeTool;
toolName->getLinearVelocity(toolVelAtHit);
const vector<string> & wname = evt.getWith(); // Get a name of the other
const vector<string> & wparts = evt.getWithParts(); // Get a parts name of the other's collision point
const vector<string> & mparts = evt.getMyParts(); // Get a parts of collision point of myself
Vector3d pos;
didToolHit = true;
for(int i = 0; i < wname.size(); i++)
{
// const char* linkName = mparts[i].c_str();
linkName = mparts[i].c_str();
link = toolName->getParts(linkName);
// link->getPosition(pos);
// cout << pos.x() << " , " << pos.z() << endl;
// link->getRotation(linkRotation);
// CParts *link = tool->getParts(linkName);
// link->getPosition(pos);
// link->getRotation(linkRotation);
}
isTargetAtRest = false;
isToolAtRest = false;
isToolAtInitLoc = false;
isTargetAtInitLoc = false;
isToolAtInitPose = false;
isTargetAtInitPose = false;
flag = 1;
}
extern "C" Controller * createController() {
return new MyController;
}