phyjoint.h
1 /********************************************************************************
2  * WorldSim -- library for robot simulations *
3  * Copyright (C) 2008-2011 Gianluca Massera <emmegian@yahoo.it> *
4  * *
5  * This program is free software; you can redistribute it and/or modify *
6  * it under the terms of the GNU General Public License as published by *
7  * the Free Software Foundation; either version 2 of the License, or *
8  * (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public License *
16  * along with this program; if not, write to the Free Software *
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA *
18  ********************************************************************************/
19 
20 #ifndef PHYJOINT_H
21 #define PHYJOINT_H
22 
23 #include "worldsimconfig.h"
24 #include "world.h"
25 #include "phyobject.h"
26 #include "wvector.h"
27 #include "wmatrix.h"
28 #include "wquaternion.h"
29 #include "ownable.h"
30 #include <QObject>
31 
32 namespace farsa {
33 
34 class PhyJoint;
35 class PhyJointPrivate;
36 class PhyObjectPrivate;
37 class WorldPrivate;
38 class Motor;
39 
50 class FARSA_WSIM_API PhyDOF : public QObject {
51  Q_OBJECT
52 public:
54  enum MotionMode { force, vel, pos, off };
55 
61  PhyDOF( PhyJoint* parent, wVector axis, wVector centre, bool translate = false ) {
62  this->ax = axis;
63  this->istranslate = translate;
64  this->centrev = centre;
65  limitsOn = false;
66  lolimit = -3.14f;
67  hilimit = +3.14f;
68  parentv = parent;
69  desiredPos = 0.0;
70  desiredVel = 0.0;
71  motionMode = pos;
72  positionv = 0.0;
73  velocityv = 0.0;
74  stiffnessv = 0.99f;
75  maxvelocityv = 1.5708f; // 90 degrees/sec
76  maxforcev = -1.0; // no limit to force/torque
77  forcea = 0.0;
78  x_ax = wVector(0,0,0);
79  y_ax = wVector(0,0,0);
80  };
81 
83  void switchOff() {
84  motionMode = off;
85  }
86 
89  return parentv;
90  };
91 
93  const PhyJoint* joint() const {
94  return parentv;
95  };
96 
98  real appliedForce() const {
99  return forcea;
100  };
101 
104  return desiredPos;
105  };
106 
109  return desiredVel;
110  };
111 
113  MotionMode motion() const {
114  return motionMode;
115  };
116 
118  void setMotionMode ( enum MotionMode m) {
119  motionMode = m;
120  }
121 
123  wVector centre() const {
124  return centrev;
125  };
127  void setCentre( const wVector& cent ) {
128  this->centrev = cent;
129  };
130 
135  real position() const {
136  return positionv;
137  };
139  void setPosition( real newpos ) {
140  positionv = newpos;
141  emit changedPosition( newpos );
142  };
143 
145  real velocity() const {
146  return velocityv;
147  };
149  void setVelocity( real newvel ) {
150  velocityv = newvel;
151  emit changedVelocity( newvel );
152  };
153 
155  void limits( real& lo, real& hi ) const {
156  lo = lolimit;
157  hi = hilimit;
158  };
159 
161  real stiffness() const {
162  return stiffnessv;
163  };
164 
166  void setStiffness( real newstiff ) {
167  //--- clamp newstiff between 0 and 0.99
168  stiffnessv = ramp( 0.0f, 0.99f, newstiff );
169  emit changedStiffness( stiffnessv );
170  };
171 
173  void setMaxVelocity( real maxvel ) {
174  maxvelocityv = maxvel;
175  };
176 
178  real maxVelocity() const {
179  return maxvelocityv;
180  };
181 
183  void setMaxForce( real maxforce ) {
184  maxforcev = maxforce;
185  };
186 
188  real maxForce() const {
189  return maxforcev;
190  }
191 
193  wVector axis() const {
194  return ax;
195  };
197  void setAxis( const wVector& ax ) {
198  this->ax = ax;
199  };
200 
202  wVector xAxis() const {
203  return x_ax;
204  };
206  void setXAxis( const wVector& x_ax ) {
207  this->x_ax = x_ax;
208  };
209 
211  wVector yAxis() const {
212  return y_ax;
213  };
215  void setYAxis( const wVector& y_ax ) {
216  this->y_ax = y_ax;
217  };
218 
220  bool translate() const {
221  return istranslate;
222  };
223 
225  bool rotate() const {
226  return !istranslate;
227  };
228 
230  bool isLimited() const {
231  return limitsOn;
232  };
233 
235  void enableLimits() {
236  limitsOn = true;
237  };
238 
240  void disableLimits() {
241  limitsOn = false;
242  };
243 public slots:
252  void applyForce( real newforce ) {
253  forcea = newforce;
254  motionMode = force;
255  emit appliedForce( newforce );
256  return;
257  };
261  void setDesiredPosition( real wishpos ) {
262  //--- change the current desired position to avoid to push to the limits
263  real offset = (fabs(hilimit)-fabs(lolimit))*0.005;
264  desiredPos = ramp( lolimit+offset, hilimit-offset, wishpos );
265  //desiredPos = wishpos;
266  motionMode = pos;
267  emit changedDesiredPosition( wishpos );
268  return;
269  };
273  void setDesiredVelocity( real wishvel ) {
274  desiredVel = wishvel;
275  motionMode = vel;
276  emit changedDesiredVelocity( wishvel );
277  return;
278  };
285  void setLimits( real lowlimit, real highlimit ) {
286 #ifdef FARSA_DEBUG
287  if ( !istranslate && lowlimit <= -PI_GRECO ) {
288  qDebug() << "DOF Lower Limit must be greater than -pi";
289  }
290  if ( !istranslate && highlimit >= PI_GRECO ) {
291  qDebug() << "DOF Higher Limit must be lesser that pi";
292  }
293 #endif
294  lolimit = lowlimit;
295  hilimit = highlimit;
296  //--- change the current desired position to avoid to push to the limits
297  real offset = (fabs(hilimit)-fabs(lolimit))*0.005;
298  desiredPos = ramp( lolimit+offset, hilimit-offset, desiredPos );
299  emit changedLimits( lolimit, hilimit );
300  };
301 
302 signals:
304  void appliedForce( real newforce );
306  void changedDesiredPosition( real wishpos );
308  void changedDesiredVelocity( real wishvel );
310  void changedPosition( real newpos );
312  void changedVelocity( real newvel );
314  void changedStiffness( real newstiff );
316  void changedLimits( real lowlimit, real highlimit );
317 private:
318  PhyJoint* parentv;
319  wVector ax;
320  wVector x_ax;
321  wVector y_ax;
322  wVector centrev;
323  bool istranslate;
324  real stiffnessv;
325  real forcea;
327  real desiredPos;
329  real desiredVel;
331  MotionMode motionMode;
334  real positionv;
336  real velocityv;
338  bool limitsOn;
340  real lolimit;
342  real hilimit;
344  real maxvelocityv;
346  real maxforcev;
347 };
348 
359 class FARSA_WSIM_API PhyJoint : public Ownable {
360 public:
366  PhyJoint( PhyObject* parent, PhyObject* child );
367 
369  virtual ~PhyJoint();
371  virtual PhyObject* child() {
372  return childv;
373  };
375  virtual const PhyObject* child() const {
376  return childv;
377  };
379  virtual PhyObject* parent() {
380  return parentv;
381  };
383  virtual const PhyObject* parent() const {
384  return parentv;
385  };
387  virtual unsigned int numDofs() const {
388  return dofv;
389  };
391  virtual QVector<PhyDOF*> dofs() {
392  return dofsv;
393  };
395  void enable( bool b );
397  bool isEnabled() const {
398  return enabled;
399  };
400 
402  virtual wVector centre() const = 0;
403 
406  virtual wVector getForceOnJoint() const = 0;
407 
409  virtual void updateJointInfo() = 0;
410 
413  return worldv;
414  };
415 
417  const World* world() const {
418  return worldv;
419  };
420 
425  virtual void preUpdate() {
426  /* nothing to do */
427  };
432  virtual void postUpdate() {
433  /* nothing to do */
434  };
435 
436 protected:
438  QVector<PhyDOF*> dofsv;
440  int dofv;
442  bool enabled;
449 
451  PhyJointPrivate* priv;
452  PhyObjectPrivate* parentpriv;
453  PhyObjectPrivate* childpriv;
454  WorldPrivate* worldpriv;
455  friend class PhyJointPrivate;
456  virtual void updateJoint( real timestep ) = 0;
457 };
458 
459 } // end namespace farsa
460 
461 #endif
void limits(real &lo, real &hi) const
return the limits
Definition: phyjoint.h:155
int dofv
number of DOF
Definition: phyjoint.h:440
virtual void postUpdate()
postUpdate the PhyJoint this method is called at each step of the world just after the physic update ...
Definition: phyjoint.h:432
World * world()
Return the World.
Definition: phyjoint.h:412
void setLimits(real lowlimit, real highlimit)
Set limits of this DOF The meaning of parameters changes depending if it's a rotational or translatio...
Definition: phyjoint.h:285
wVector centre() const
return the centre of rotation/translation
Definition: phyjoint.h:123
void setStiffness(real newstiff)
set the stiffness about this DOF
Definition: phyjoint.h:166
virtual const PhyObject * parent() const
Return the parent object; (NULL means an object attached to static world; see Netwon Documentation) (...
Definition: phyjoint.h:383
wVector yAxis() const
return the Y axis of local DOF frame
Definition: phyjoint.h:211
void setMaxForce(real maxforce)
sets the maximum force/torque applied by the joint.
Definition: phyjoint.h:183
World * worldv
world
Definition: phyjoint.h:448
bool isEnabled() const
Return true if the joint is enabled.
Definition: phyjoint.h:397
FARSA_UTIL_TEMPLATE real ramp(real minv, real maxv, real value)
World class.
Definition: world.h:223
The base for all class that can have (and can be) an owner.
Definition: ownable.h:37
real velocity() const
return the actual relative velocity of bodies along the axis of rotation/translation ...
Definition: phyjoint.h:145
void switchOff()
Switches the motor off.
Definition: phyjoint.h:83
real position() const
return the actual position of bodies For rotational DOF is the angle of rotation For linear DOF is t...
Definition: phyjoint.h:135
virtual QVector< PhyDOF * > dofs()
Return descriptions of DOF.
Definition: phyjoint.h:391
MotionMode
Type that encode the current modality of motion (off means motor switched off)
Definition: phyjoint.h:54
wVector xAxis() const
return the X axis of local DOF frame, this is the zero angle position
Definition: phyjoint.h:202
void setDesiredVelocity(real wishvel)
Accelerate the DOF to the velocity passed and try to stay at that velocity.
Definition: phyjoint.h:273
PhyJoint class.
Definition: phyjoint.h:359
PhyObject * childv
child object
Definition: phyjoint.h:446
virtual unsigned int numDofs() const
Return the number of DOF constrained by this joint.
Definition: phyjoint.h:387
bool translate() const
return true if it translate along axis
Definition: phyjoint.h:220
PhyObject * parentv
parent object
Definition: phyjoint.h:444
PhyJointPrivate * priv
Engine encapsulation.
Definition: phyjoint.h:451
real appliedForce() const
Return the Force/Torque applied using applyForce.
Definition: phyjoint.h:98
real stiffness() const
return the stiffness
Definition: phyjoint.h:161
virtual PhyObject * parent()
Return the parent object; (NULL means an object attached to static world; see Netwon Documentation) ...
Definition: phyjoint.h:379
const PhyJoint * joint() const
return the PhyJoint (const version)
Definition: phyjoint.h:93
float real
void setDesiredPosition(real wishpos)
Move the DOF at the position passed and try to stay there.
Definition: phyjoint.h:261
real maxForce() const
returns the maximum force/torque.
Definition: phyjoint.h:188
real desiredVelocity() const
Return the desired position setted.
Definition: phyjoint.h:108
MotionMode motion() const
return the actual motion mode
Definition: phyjoint.h:113
const World * world() const
Return the World (const version)
Definition: phyjoint.h:417
virtual void preUpdate()
preUpdate the PhyJoint this method is called at each step of the world just before the physic update ...
Definition: phyjoint.h:425
void setMaxVelocity(real maxvel)
set the maximum velocity for the joint (rad/sec)
Definition: phyjoint.h:173
bool rotate() const
return true if it rotate around axis
Definition: phyjoint.h:225
void enableLimits()
enable limits
Definition: phyjoint.h:235
bool enabled
true if is enabled, false otherwise
Definition: phyjoint.h:442
wVector axis() const
return the axis of rotation/translation
Definition: phyjoint.h:193
virtual const PhyObject * child() const
Return the child object attached to this joint (see Newton Documentation) (const version) ...
Definition: phyjoint.h:375
void setMotionMode(enum MotionMode m)
change the modality of motion
Definition: phyjoint.h:118
real desiredPosition() const
Return the desired position setted.
Definition: phyjoint.h:103
void disableLimits()
enable limits
Definition: phyjoint.h:240
PhyDOF class.
Definition: phyjoint.h:50
PhyDOF(PhyJoint *parent, wVector axis, wVector centre, bool translate=false)
constructor (it's used by PhyJoint subclasses)
Definition: phyjoint.h:61
virtual PhyObject * child()
Return the child object attached to this joint (see Newton Documentation)
Definition: phyjoint.h:371
real maxVelocity() const
return the maximum velocity (rad/sec)
Definition: phyjoint.h:178
PhyJoint * joint()
return the PhyJoint
Definition: phyjoint.h:88
PhyObject class.
Definition: phyobject.h:46
void applyForce(real newforce)
Apply a Force to the object linked by this DOF.
Definition: phyjoint.h:252
bool isLimited() const
return true if rotation/translation are limited
Definition: phyjoint.h:230