22 #include "phymarxbot.h"
24 #include "phycylinder.h"
26 #include "phyballandsocket.h"
27 #include "physphere.h"
28 #include "physuspension.h"
29 #include "phycompoundobject.h"
31 #include "mathutils.h"
36 const real PhyMarXbot::basex = 0.034f;
37 const real PhyMarXbot::basey = 0.143f;
38 const real PhyMarXbot::basez = 0.048f;
39 const real PhyMarXbot::basem = 0.4f;
40 const real PhyMarXbot::bodyr = 0.085f;
41 const real PhyMarXbot::bodyh = 0.0055f;
42 const real PhyMarXbot::bodym = 0.02f;
43 const real PhyMarXbot::axledistance = 0.104f;
44 const real PhyMarXbot::trackradius = 0.022f;
45 const real PhyMarXbot::trackheight = 0.0295f;
46 const real PhyMarXbot::trackm = 0.05f;
47 const real PhyMarXbot::treaddepth = 0.004f;
48 const real PhyMarXbot::wheelr = 0.027f;
49 const real PhyMarXbot::wheelh = 0.0215f;
50 const real PhyMarXbot::wheelm = 0.02f;
51 const real PhyMarXbot::attachringh = 0.0285f;
52 const real PhyMarXbot::attachringm = 0.08f;
53 const real PhyMarXbot::ledsh = 0.010f;
54 const real PhyMarXbot::ledsradius = 0.080f;
55 const real PhyMarXbot::ledsm = 0.03f;
58 kinematicSimulation =
false;
83 QVector<PhyObject*> objs;
87 tm.w_pos[2] = basez/2.0f + 0.015f;
93 obj->setMass( bodym );
95 tm.w_pos[2] = 0.015f + basez + bodyh/2.0f;
99 obj =
new PhyBox( trackheight, axledistance, trackradius*2.0f, w );
101 tm.w_pos[0] = (basex + trackheight)/2.0f;
102 tm.w_pos[2] = trackradius + treaddepth;
103 obj->setMass( trackm );
104 obj->setMatrix( tm );
106 obj =
new PhyCylinder( trackradius, trackheight, w );
107 obj->setMass( wheelm );
108 tm.w_pos[1] = axledistance/2.0;
109 obj->setMatrix( tm );
111 obj =
new PhyCylinder( trackradius, trackheight, w );
112 obj->setMass( wheelm );
113 tm.w_pos[1] = -axledistance/2.0;
114 obj->setMatrix( tm );
117 obj =
new PhyBox( trackheight, axledistance, trackradius*2.0f, w );
119 tm.w_pos[0] = -(basex + trackheight)/2.0f;
120 tm.w_pos[2] = trackradius + treaddepth;
121 obj->setMass( trackm );
122 obj->setMatrix( tm );
124 obj =
new PhyCylinder( trackradius, trackheight, w );
125 obj->setMass( wheelm );
126 tm.w_pos[1] = axledistance/2.0;
127 obj->setMatrix( tm );
129 obj =
new PhyCylinder( trackradius, trackheight, w );
130 obj->setMass( wheelm );
131 tm.w_pos[1] = -axledistance/2.0;
132 obj->setMatrix( tm );
136 base->setMaterial(
"marXbotMaterial");
153 double xoffs[6] = { 1, -1, 1, -1, 1, -1 };
154 double yoffs[6] = { 0, 0, 1, 1, -1, -1 };
157 for(
int i=0; i<6; i++ ) {
161 xoffs[i] * (basex + trackheight)/2.0f + (yoffs[i]==0 ? xoffs[i]*(trackheight/2.0+wheelh/2.0f+0.006f) : 0.0f),
162 yoffs[i] * (axledistance/2.0f),
166 wtm.w_pos[2] = wheelr;
167 awheel =
new PhyCylinder( wheelr, wheelh, w,
"motorWheel" );
170 wtm.w_pos[2] = treaddepth;
171 awheel =
new PhySphere( treaddepth, w,
"passiveWheel" );
173 wheelstm.append( wtm );
175 awheel->setColor( Qt::blue );
176 awheel->setMaterial(
"marXbotTire" );
179 wheels.append( awheel );
186 wheels[i]->matrix().w_pos,
189 joint->
dofs()[0]->disableLimits();
193 wheels[i]->
matrix().w_pos,
198 wheelJoints.append( joint );
203 obj =
new PhyCone( bodyr, attachringh+0.010f, w );
204 obj->setMass( attachringm );
206 tm.w_pos[0] = 0.005f;
207 obj->setMatrix( tm );
209 obj =
new PhyCone( bodyr, attachringh+0.010f, w );
210 obj->setMass( attachringm );
212 tm.w_pos[0] = -0.005f;
213 obj->setMatrix( tm );
216 obj->setMass( ledsm );
217 obj->setTexture(
"marXbot_12leds" );
218 obj->setUseColorTextureOfOwner(
false );
219 tm.w_pos[0] = attachringh/2.0f + ledsh/2.0f + 0.0015f;
220 obj->setMatrix( tm );
224 attachring->setMaterial(
"marXbotMaterial");
225 attachring->
setOwner(
this,
false );
228 attachringtm.w_pos[2] = 0.015f + basez + bodyh + attachringh/2.0f + 0.0015f;
229 attachring->
setMatrix( attachringtm * basetm );
231 forcesensor =
new PhyFixed( base, attachring, w );
232 forcesensor->
setOwner(
this,
false );
235 QVector<PhyDOF*> motors;
236 motors << wheelJoints[0]->dofs()[0] << wheelJoints[1]->dofs()[0];
238 wheelsCtrl->
setOwner(
this,
false );
240 #warning QUI DECIDERE PER LE VELOCITÀ MASSIME E MINIME DELLE RUOTE
249 QVector<SingleIR> sensors;
252 #warning QUI DECIDERE PER I RANGE/APERTURE E LE POSIZIONI (SPECIE PER I GROUND)
255 for (
unsigned int i = 0; i < 24; i++) {
256 const double curAngle = double(i) * (360.0 / 24.0) + ((360.0 / 24.0) / 2.0);
257 const double radius = bodyr;
260 mtr.w_pos.x = radius * cos(deg2rad(curAngle));
261 mtr.w_pos.y = radius * sin(deg2rad(curAngle));
262 mtr.w_pos.z = 0.015f + basez + bodyh/2.0f;
264 sensors.append(
SingleIR(base, mtr, 0.02, 0.1, 10.0, 5));
271 const double distFromBorder = 0.003;
274 mtr.w_pos =
wVector(basex / 2.0f - distFromBorder, basey / 2.0f - distFromBorder, 0.015f);
275 sensors.append(
SingleIR(base, mtr, 0.0, mtr.w_pos.z + 0.005, 0.0, 1));
276 mtr.w_pos =
wVector(basex / 2.0f - distFromBorder, -basey / 2.0f + distFromBorder, 0.015f);
277 sensors.append(
SingleIR(base, mtr, 0.0, mtr.w_pos.z + 0.005, 0.0, 1));
278 mtr.w_pos =
wVector(-basex / 2.0f + distFromBorder, basey / 2.0f - distFromBorder, 0.015f);
279 sensors.append(
SingleIR(base, mtr, 0.0, mtr.w_pos.z + 0.005, 0.0, 1));
280 mtr.w_pos =
wVector(-basex / 2.0f + distFromBorder, -basey / 2.0f + distFromBorder, 0.015f);
281 sensors.append(
SingleIR(base, mtr, 0.0, mtr.w_pos.z + 0.005, 0.0, 1));
288 for (
unsigned int i = 0; i < 8; i++) {
289 const double curAngle = double(i) * (360.0 / 8.0);
290 const double radius = bodyr - 0.003;
293 mtr.w_pos.x = radius * cos(deg2rad(curAngle));
294 mtr.w_pos.y = radius * sin(deg2rad(curAngle));
295 mtr.w_pos.z = 0.015f + basez;
297 sensors.append(
SingleIR(base, mtr, 0.0, mtr.w_pos.z + 0.005, 0.0, 1));
304 w->pushObject(
this );
308 foreach(
PhyJoint* susp, wheelJoints ) {
319 delete groundBottomIR;
320 delete groundAroundIR;
330 if (kinematicSimulation) {
341 const real leftSpeed = leftWheelVelocity * wheelr;
342 const real rightSpeed = rightWheelVelocity * wheelr;
346 if (fabs(leftSpeed - rightSpeed) < 0.00001f) {
348 mtr.w_pos += -mtr.y_ax.
scale(rightSpeed *
world()->timeStep());
395 const wVector L(0.0, -leftSpeed, 0.0);
396 const wVector A(wheelstm[0].w_pos.x - wheelstm[1].w_pos.x, -(rightSpeed - leftSpeed), 0.0);
397 const real a = -(L.y / A.y);
405 const real distLeftWheel = -C.x;
406 const real distRightWheel = -(C.x - A.x);
407 const real angularVelocity = (fabs(distLeftWheel) > fabs(distRightWheel)) ? (-leftSpeed / distLeftWheel) : (-rightSpeed / distRightWheel);
413 const real angularDisplacement = angularVelocity *
world()->
timeStep();
418 mtr = mtr.
rotateAround(axisOfRotation, centerOfRotation, angularDisplacement);
460 if (kinematicSimulation == k) {
464 kinematicSimulation = k;
465 if (kinematicSimulation) {
467 for (
int i = 0; i < wheelJoints.size(); i++) {
468 wheelJoints[i]->enable(
false);
470 forcesensor->
enable(
false);
474 for (
int i = 0; i < wheels.size(); i++) {
475 wheels[i]->setKinematic(
true);
481 for (
int i = 0; i < wheels.size(); i++) {
482 wheels[i]->setKinematic(
false);
487 for (
int i = 0; i < wheelJoints.size(); i++) {
488 wheelJoints[i]->enable(
true);
489 wheelJoints[i]->updateJointInfo();
491 forcesensor->
enable(
true);
498 leftWheelVelocity = velocity;
503 rightWheelVelocity = velocity;
509 for(
int i=0; i<wheels.size(); i++ ) {
510 wheels[i]->setMatrix( wheelstm[i] * tm );
512 foreach(
PhyJoint* ajoint, wheelJoints ) {
515 attachring->
setMatrix( attachringtm * tm );