worldsim/include/motorcontrollers.h

00001 /********************************************************************************
00002  *  WorldSim -- library for robot simulations                                   *
00003  *  Copyright (C) 2008-2011 Gianluca Massera <emmegian@yahoo.it>                *
00004  *                                                                              *
00005  *  This program is free software; you can redistribute it and/or modify        *
00006  *  it under the terms of the GNU General Public License as published by        *
00007  *  the Free Software Foundation; either version 2 of the License, or           *
00008  *  (at your option) any later version.                                         *
00009  *                                                                              *
00010  *  This program is distributed in the hope that it will be useful,             *
00011  *  but WITHOUT ANY WARRANTY; without even the implied warranty of              *
00012  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               *
00013  *  GNU General Public License for more details.                                *
00014  *                                                                              *
00015  *  You should have received a copy of the GNU General Public License           *
00016  *  along with this program; if not, write to the Free Software                 *
00017  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA  *
00018  ********************************************************************************/
00019 
00020 #ifndef MOTORCONTROLLERS_H
00021 #define MOTORCONTROLLERS_H
00022 
00023 #include "worldsimconfig.h"
00024 #include "phyjoint.h"
00025 #include "ownable.h"
00026 #include <QVector>
00027 
00028 // All this stuff is to get rid of annoying warnings...
00029 #ifdef __GNUC__
00030 #if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ > 5)
00031 #pragma GCC diagnostic push
00032 #pragma GCC diagnostic ignored "-Wunused-parameter"
00033 #else
00034 #pragma GCC diagnostic ignored "-Wunused-parameter"
00035 #endif
00036 #endif
00037 #ifdef FARSA_USE_YARP_AND_ICUB
00038     #include <yarp/dev/ControlBoardInterfaces.h>
00039     #include <yarp/dev/ControlBoardPid.h>
00040 #endif
00041 #ifdef __GNUC__
00042 #if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ > 5)
00043 #pragma GCC diagnostic pop
00044 #else
00045 #pragma GCC diagnostic warning "-Wunused-parameter"
00046 #endif
00047 #endif
00048 
00049 namespace farsa {
00050 
00051 class World;
00052 
00059 class FARSA_WSIM_API wPID {
00060 public:
00062     wPID();
00066     double pidloop( double currentvalue );
00069     void reset();
00071     double p_gain;
00073     double i_gain;
00075     double d_gain;
00077     double acc_ff;
00079     double fri_ff;
00081     double vel_ff;
00083     double bias;
00085     double accel_r;
00087     double setpt;
00089     double min;
00091     double max;
00093     double slew;
00094 private:
00095     double this_target;
00096     double next_target;
00097     double integral;
00098     double last_error;
00099     double last_output;
00100 };
00101 
00111 class FARSA_WSIM_API MotorController : public Ownable {
00112 public:
00116     MotorController( World* world );
00118     virtual ~MotorController();
00120     virtual void update() = 0;
00122     bool isEnabled() {
00123         return enabled;
00124     };
00126     void setEnabled( bool b ) {
00127         enabled = b;
00128     };
00130     World* world() {
00131         return worldv;
00132     };
00133 
00134 private:
00136     World* worldv;
00138     bool enabled;
00139 
00141     MotorController(const MotorController& other);
00142 
00144     MotorController& operator=(const MotorController& other);
00145 };
00146 
00179 class FARSA_WSIM_API MultiMotorController : public MotorController
00180 #ifdef FARSA_USE_YARP_AND_ICUB
00181 , public yarp::dev::DeviceDriver, public yarp::dev::IPositionControl, public yarp::dev::IVelocityControl, public yarp::dev::IControlMode, public yarp::dev::IEncoders, public yarp::dev::IControlLimits, public yarp::dev::IControlLimitsRaw
00182 #endif
00183 {
00184 public:
00188     MultiMotorController( QVector<PhyDOF*> motors, World* world );
00190     virtual ~MultiMotorController();
00192     QVector<PhyDOF*>& motors() {
00193         return motorsv;
00194     };
00196     QVector<wPID*>& positionPIDs() {
00197         return pos_pids;
00198     };
00200     bool setEnableLimitsRaw( int axis, bool b );
00202     virtual void update();
00204     QVector<wPID*>& velocityPIDs() {
00205         return vel_pids;
00206     };
00207 
00210 #ifdef FARSA_USE_YARP_AND_ICUB
00211 
00212     virtual bool close( );
00214     virtual bool configure( yarp::os::Searchable &config );
00216     virtual bool open( yarp::os::Searchable &config );
00217 #endif
00218 
00219 
00222     virtual bool getAxes(int *ax);
00224     virtual bool getRefAcceleration(int j, double *acc);
00226     virtual bool getRefAccelerations(double *accs);
00228     virtual bool setRefAcceleration(int j, double acc);
00230     virtual bool setRefAccelerations(const double *accs);
00232     virtual bool stop(int j);
00234     virtual bool stop();
00236 
00239     virtual bool checkMotionDone(int j, bool *flag);
00241     virtual bool checkMotionDone(bool *flag);
00243     virtual bool getRefSpeed(int j, double *ref);
00245     virtual bool getRefSpeeds(double *spds);
00247     virtual bool positionMove(int j, double ref);
00249     virtual bool positionMove(const double *refs);
00251     virtual bool relativeMove(int j, double delta);
00253     virtual bool relativeMove(const double *deltas);
00255     virtual bool setPositionMode();
00257     virtual bool setRefSpeed(int j, double sp);
00259     virtual bool setRefSpeeds(const double *spds);
00261 
00264     virtual bool setVelocityMode();
00266     virtual bool velocityMove(int j, double sp);
00268     virtual bool velocityMove(const double *sp);
00270 
00273     virtual bool getControlMode( int j, int* mode );
00275     virtual bool getControlModes( int* modes );
00277     virtual bool setImpedancePositionMode(int) { return false; }
00279     virtual bool setImpedanceVelocityMode(int) { return false; }
00281     virtual bool setOpenLoopMode(int j);
00283     virtual bool setPositionMode( int j );
00285     virtual bool setTorqueMode( int j );
00287     virtual bool setVelocityMode(int j );
00289 
00292     virtual bool getEncoder( int j, double *v );
00294     virtual bool getEncoders(double *encs);
00296     virtual bool getEncoderAcceleration( int j, double *spds );
00298     virtual bool getEncoderAccelerations( double *accs );
00300     virtual bool getEncoderSpeed( int j, double *sp );
00302     virtual bool getEncoderSpeeds( double *spds );
00304     virtual bool resetEncoder( int j );
00306     virtual bool resetEncoders();
00308     virtual bool setEncoder( int j, double val );
00310     virtual bool setEncoders( const double *vals );
00312 
00315     virtual bool getLimits( int axis, double *min, double *max );
00317     virtual bool setLimits( int axis, double min, double max );
00319 
00322     virtual bool getLimitsRaw( int axis, double *min, double *max );
00324     virtual bool setLimitsRaw( int axis, double min, double max );
00326 private:
00328     QVector<PhyDOF*> motorsv;
00329     //--- Common Variable among Control Interfaces
00334     QVector<int> controlModes;
00336     QVector<real> refsacc;
00337 
00338     //--- IPositionControl Variables
00340     QVector<real> refs;
00342     QVector<real> refsvel;
00344     QVector<real> xfx0, x0, dx0, t, T;
00348     QVector<bool> stops;
00350     QVector<wPID*> pos_pids;
00352     QVector<real> lastErrors, lastIs, lastTorques;
00354     real fromRawPosition( int i );
00356     real toRawPosition( real noRawPos, int i );
00357     //--- END IPositionControl Variables
00358 
00359     //--- IVelocityControl Variables
00361     QVector<real> desiredVel;
00363     QVector<wPID*> vel_pids;
00364     //--- END IVelocityControl Variables
00365 
00366     //--- IControlLimits Variables
00368     QVector<double> loLimits;
00370     QVector<double> hiLimits;
00371     //--- END IControlLimits Variables
00372 };
00373 
00376 class FARSA_WSIM_API WheelMotorController : public MotorController {
00377 public:
00381     WheelMotorController( QVector<PhyDOF*> wheels, World* world );
00383     virtual ~WheelMotorController();
00385     QVector<PhyDOF*>& wheels() {
00386         return motorsv;
00387     };
00389     virtual void update();
00393     void setSpeeds( QVector<double> speeds );
00397     void setSpeeds( double sp1, double sp2 );
00401     void getSpeeds( QVector<double>& speeds );
00405     void getSpeeds( double& sp1, double& sp2 );
00409     void setSpeedLimits( QVector<double> minSpeeds, QVector<double> maxSpeeds );
00413     void setSpeedLimits( double minSpeed1, double minSpeed2, double maxSpeed1, double maxSpeed2 );
00417     void getSpeedLimits( QVector<double>& minSpeeds, QVector<double>& maxSpeeds );
00421     void getSpeedLimits( double& minSpeed1, double& minSpeed2, double& maxSpeed1, double& maxSpeed2 );
00422 private:
00424     QVector<PhyDOF*> motorsv;
00426     QVector<double> desiredVel;
00428     QVector<double> minVel;
00430     QVector<double> maxVel;
00431 };
00432 
00433 } // end namespace farsa
00434 
00435 #endif