forked from jzerez/Webots_Playground
-
Notifications
You must be signed in to change notification settings - Fork 0
/
flippy_physics.cpp
188 lines (155 loc) · 5.69 KB
/
flippy_physics.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
/*
* File: flippy_physics.cpp
* Date: Spring, 2021
* Description: Custom Physics plugin for flippy. Handles crawling over terrain
* Author: Jonathan Zerez
* Modifications:
*/
#include <ode/ode.h>
#include <plugins/physics.h>
#include <iostream>
#include <unordered_set>
static pthread_mutex_t mutex; // needed to run with multi-threaded version of ODE
typedef enum {STATIONARY_1, STATIONARY_2} states;
static dBodyID floorBody {};
std::unordered_set<dJointID> joints {};
/*
* Note: This plugin will become operational only after it was compiled and associated with the current world (.wbt).
* To associate this plugin with the world follow these steps:
* 1. In the Scene Tree, expand the "WorldInfo" node and select its "physics" field
* 2. Then hit the [Select] button at the bottom of the Scene Tree
* 3. In the list choose the name of this plugin (same as this file without the extention)
* 4. Then save the .wbt by hitting the "Save" button in the toolbar of the 3D view
* 5. Then reload the world: the plugin should now load and execute with the current simulation
*/
std::string flippyBodyName(int flippyNum, int sphereNum){
std::string fnum = std::to_string(flippyNum);
std::string snum = std::to_string(sphereNum);
if (flippyNum < 10){
return "F00" + fnum + "_S" + snum;
} else if (flippyNum < 100){
return "F0" + fnum + "_S" + snum;
} else {
return "F" + fnum + "_S" + snum;
}
}
// Removes all user-created joints from a specified body DEF
void removeJoints(const char* bodyDef) {
dBodyID body = dWebotsGetBodyFromDEF(bodyDef);
int numJoints = dBodyGetNumJoints(body);
for (int i = 0; i < numJoints; i++) {
dJointID joint = dBodyGetJoint(body, i);
// Only user-created joints will be in the joints set
if (joints.find(joint) != joints.end()) {
joints.erase(joint);
dJointDisable(joint);
}
}
}
// Creates a fixed joints between two specified bodies
void addFixedJoint(dBodyID b1, dBodyID b2) {
dWorldID joint_world = dBodyGetWorld(b1);
pthread_mutex_lock(&mutex);
dJointID joint = dJointCreateFixed(joint_world, 0);
dJointAttach(joint, b1, b2);
dJointSetFixed(joint);
joints.insert(joint);
pthread_mutex_unlock(&mutex);
}
void webots_physics_init() {
// int numRobots = 4;
pthread_mutex_init(&mutex, NULL);
dWebotsConsolePrintf("INITIALIZING PHYSICS...");
// Initialize Simulation Bodies
// TODO: Need to initialize bodies in a for loop when it comes to multi-agents
floorBody = dWebotsGetBodyFromDEF("FLOOR");
// dBodyID flippy_bodies[numRobots][2];
// for (int i = 0; i < numRobots; i++) {
// flippy_bodies[i][0] = dWebotsGetBodyFromDEF(flippyBodyName(i,1).c_str());
// flippy_bodies[i][1] = dWebotsGetBodyFromDEF(flippyBodyName(i,2).c_str());
// }
// dWorldID world = dBodyGetWorld(flippy_bodies[0][0]);
// Create initial joint
// TODO: create joint according to robot state only.
dWebotsConsolePrintf("creating joints....");
pthread_mutex_lock(&mutex);
// for(int i = 0; i < numRobots; i++) {
// dJointID joint = dJointCreateFixed(world, 0);
// dJointAttach(joint, flippy_bodies[i][1], floorBody);
// dJointSetFixed(joint);
// joints.insert(joint);
// }
pthread_mutex_unlock(&mutex);
}
void webots_physics_step() {
/*
* Do here what needs to be done at every time step, e.g. add forces to bodies
* dBodyAddForce(body1, f[0], f[1], f[2]);
* ...
*/
// Read data incomming from the controller
int dataSize;
const char* data = (const char*)dWebotsReceive(&dataSize);
if (dataSize > 0) {
// This code parses the messages in the packet
char *msg = new char[dataSize];
int count = 1, i = 0, j = 0;
for ( ; i < dataSize; ++i) {
char c = data[i];
if (c == '\0') {
msg[j] = c;
if (count == 1) {
// This is the first message in the package.
// Specifies the body (by DEF) to unfix
dWebotsConsolePrintf("REMOVING JOINTS FROM: ");
dWebotsConsolePrintf(msg);
removeJoints(msg);
} else {
// This is the second message in the package.
// Specifies the body (by DEF) to fix
dWebotsConsolePrintf("ADDING JOINT TO: ");
dWebotsConsolePrintf(msg);
dBodyID body1 = dWebotsGetBodyFromDEF(msg);
dBodyID body2 = dWebotsGetBodyFromDEF("FLOOR");
addFixedJoint(body1, body2);
}
++count;
j = 0;
} else {
msg[j] = c;
++j;
}
}
}
}
int webots_physics_collide(dGeomID g1, dGeomID g2) {
/*
* This function needs to be implemented if you want to overide Webots collision detection.
* It must return 1 if the collision was handled and 0 otherwise.
* Note that contact joints should be added to the contactJoint_group which can change over the time, e.g.
* n = dCollide(g1, g2, MAX_CONTACTS, &contact[0].geom, sizeof(dContact));
* dJointGroupID contactJoint_group = dWebotsGetContactJointGroup();
* dWorldID world = dBodyGetWorld(body1);
* ...
* pthread_mutex_lock(&mutex);
* dJointCreateContact(world, contactJoint_group, &contact[i])
* dJointAttach(contactJoint, body1, body2);
* pthread_mutex_unlock(&mutex);
* ...
*/
// dBodyID b1 = dGeomGetBody(g1);
// dBodyID b2 = dGeomGetBody(g2);
// if ((!dAreGeomsSame(g1, floor_geom) && dBodyGetNumJoints(b1) == 0) ||
// (!dAreGeomsSame(g2, floor_geom) && dBodyGetNumJoints(b2) == 0)) {
// addFixedJoint(g1, g2);
// return 1;
// }
return 0;
}
void webots_physics_cleanup() {
/*
* Here you need to free any memory you allocated in above, close files, etc.
* You do not need to free any ODE object, they will be freed by Webots.
*/
pthread_mutex_destroy(&mutex);
}