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 = off;
72  positionv = 0.0;
73  velocityv = 0.0;
74  stiffnessv = 0.99f;
75  maxvelocityv = 1.5708f; // 90 degrees/sec
76  forcea = 0.0;
77  x_ax = wVector(0,0,0);
78  y_ax = wVector(0,0,0);
79  };
80 
83  return parentv;
84  };
85 
87  const PhyJoint* joint() const {
88  return parentv;
89  };
90 
92  real appliedForce() const {
93  return forcea;
94  };
95 
97  real desiredPosition() const {
98  return desiredPos;
99  };
100 
102  real desiredVelocity() const {
103  return desiredVel;
104  };
105 
107  MotionMode motion() const {
108  return motionMode;
109  };
110 
112  wVector centre() const {
113  return centrev;
114  };
116  void setCentre( const wVector& cent ) {
117  this->centrev = cent;
118  };
119 
124  real position() const {
125  return positionv;
126  };
128  void setPosition( real newpos ) {
129  positionv = newpos;
130  emit changedPosition( newpos );
131  };
132 
134  real velocity() const {
135  return velocityv;
136  };
138  void setVelocity( real newvel ) {
139  velocityv = newvel;
140  emit changedVelocity( newvel );
141  };
142 
144  void limits( real& lo, real& hi ) const {
145  lo = lolimit;
146  hi = hilimit;
147  };
148 
150  real stiffness() const {
151  return stiffnessv;
152  };
153 
155  void setStiffness( real newstiff ) {
156  //--- clamp newstiff between 0 and 0.99
157  stiffnessv = ramp( 0.0f, 0.99f, newstiff );
158  emit changedStiffness( stiffnessv );
159  };
160 
162  void setMaxVelocity( real maxvel ) {
163  maxvelocityv = maxvel;
164  };
165 
167  real maxVelocity() const {
168  return maxvelocityv;
169  };
170 
172  wVector axis() const {
173  return ax;
174  };
176  void setAxis( const wVector& ax ) {
177  this->ax = ax;
178  };
179 
181  wVector xAxis() const {
182  return x_ax;
183  };
185  void setXAxis( const wVector& x_ax ) {
186  this->x_ax = x_ax;
187  };
188 
190  wVector yAxis() const {
191  return y_ax;
192  };
194  void setYAxis( const wVector& y_ax ) {
195  this->y_ax = y_ax;
196  };
197 
199  bool translate() const {
200  return istranslate;
201  };
202 
204  bool rotate() const {
205  return !istranslate;
206  };
207 
209  bool isLimited() const {
210  return limitsOn;
211  };
212 
214  void enableLimits() {
215  limitsOn = true;
216  };
217 
219  void disableLimits() {
220  limitsOn = false;
221  };
222 public slots:
231  void applyForce( real newforce ) {
232  forcea = newforce;
233  motionMode = force;
234  emit appliedForce( newforce );
235  return;
236  };
240  void setDesiredPosition( real wishpos ) {
241  //--- change the current desired position to avoid to push to the limits
242  real offset = (fabs(hilimit)-fabs(lolimit))*0.005;
243  desiredPos = ramp( lolimit+offset, hilimit-offset, wishpos );
244  //desiredPos = wishpos;
245  motionMode = pos;
246  emit changedDesiredPosition( wishpos );
247  return;
248  };
252  void setDesiredVelocity( real wishvel ) {
253  desiredVel = wishvel;
254  motionMode = vel;
255  emit changedDesiredVelocity( wishvel );
256  return;
257  };
264  void setLimits( real lowlimit, real highlimit ) {
265 #ifdef FARSA_DEBUG
266  if ( !istranslate && lowlimit <= -PI_GRECO ) {
267  qDebug() << "DOF Lower Limit must be greater than -pi";
268  }
269  if ( !istranslate && highlimit >= PI_GRECO ) {
270  qDebug() << "DOF Higher Limit must be lesser that pi";
271  }
272 #endif
273  lolimit = lowlimit;
274  hilimit = highlimit;
275  //--- change the current desired position to avoid to push to the limits
276  real offset = (fabs(hilimit)-fabs(lolimit))*0.005;
277  desiredPos = ramp( lolimit+offset, hilimit-offset, desiredPos );
278  emit changedLimits( lolimit, hilimit );
279  };
280 
281 signals:
283  void appliedForce( real newforce );
285  void changedDesiredPosition( real wishpos );
287  void changedDesiredVelocity( real wishvel );
289  void changedPosition( real newpos );
291  void changedVelocity( real newvel );
293  void changedStiffness( real newstiff );
295  void changedLimits( real lowlimit, real highlimit );
296 private:
297  PhyJoint* parentv;
298  wVector ax;
299  wVector x_ax;
300  wVector y_ax;
301  wVector centrev;
302  bool istranslate;
303  real stiffnessv;
304  real forcea;
306  real desiredPos;
308  real desiredVel;
310  MotionMode motionMode;
313  real positionv;
315  real velocityv;
317  bool limitsOn;
319  real lolimit;
321  real hilimit;
323  real maxvelocityv;
324 };
325 
336 class FARSA_WSIM_API PhyJoint : public Ownable {
337 public:
343  PhyJoint( PhyObject* parent, PhyObject* child );
344 
346  virtual ~PhyJoint();
348  virtual PhyObject* child() {
349  return childv;
350  };
352  virtual const PhyObject* child() const {
353  return childv;
354  };
356  virtual PhyObject* parent() {
357  return parentv;
358  };
360  virtual const PhyObject* parent() const {
361  return parentv;
362  };
364  virtual unsigned int numDofs() const {
365  return dofv;
366  };
368  virtual QVector<PhyDOF*> dofs() {
369  return dofsv;
370  };
372  void enable( bool b );
374  bool isEnabled() const {
375  return enabled;
376  };
377 
379  virtual wVector centre() const = 0;
380 
382  virtual void updateJointInfo() = 0;
383 
386  return worldv;
387  };
388 
390  const World* world() const {
391  return worldv;
392  };
393 
398  virtual void preUpdate() {
399  /* nothing to do */
400  };
405  virtual void postUpdate() {
406  /* nothing to do */
407  };
408 
409 protected:
411  QVector<PhyDOF*> dofsv;
413  int dofv;
415  bool enabled;
422 
424  PhyJointPrivate* priv;
425  PhyObjectPrivate* parentpriv;
426  PhyObjectPrivate* childpriv;
427  WorldPrivate* worldpriv;
428  friend class PhyJointPrivate;
429  virtual void updateJoint( real timestep ) = 0;
430 };
431 
432 } // end namespace farsa
433 
434 #endif