00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #ifndef ENCODER_POSITION_SENSOR_HPP
00029 #define ENCODER_POSITION_SENSOR_HPP
00030
00031 #include <rtt/dev/EncoderInterface.hpp>
00032 #include <rtt/dev/SensorInterface.hpp>
00033 #include <rtt/dev/CalibrationInterface.hpp>
00034
00035 #include <limits>
00036
00037 namespace OCL
00038 {
00039
00040
00045 class EncoderPositionSensor
00046 : public SensorInterface<double>
00047 {
00048 EncoderInterface* enc;
00049 double unit_to_inc;
00050 double min;
00051 double max;
00052 double posOffset;
00053 double cal_pos;
00054 double cal_dir;
00055 public:
00065 EncoderPositionSensor(EncoderInterface* _enc, double _unit_to_inc, double _minpos, double _maxpos)
00066 : enc(_enc), unit_to_inc(_unit_to_inc), min(_minpos), max(_maxpos), posOffset(0),
00067 cal_pos(0), cal_dir(-1)
00068 {}
00069
00070 virtual int readSensor( double& p ) const
00071 {
00072 p = readSensor();
00073 return 0;
00074 }
00075
00079 void limit(double _min, double _max)
00080 {
00081 min = _min;
00082 max = _max;
00083 }
00091 void calibrationPosition( double pos )
00092 {
00093 cal_pos = pos;
00094 }
00102 void calibrationDirection( double dir )
00103 {
00104 cal_dir = dir;
00105 }
00106
00107 virtual void calibrate()
00108 {
00109 this->calibrate( cal_pos, cal_dir );
00110 }
00111
00112 virtual void unCalibrate()
00113 {
00114 this->calibrated = false;
00115 }
00116
00117 virtual bool isCalibrated() const
00118 {
00119 return this->calibrated;
00120 }
00121
00122 virtual double readSensor() const
00123 {
00124 return double( enc->turnGet()*enc->resolution() + enc->positionGet() ) / unit_to_inc + posOffset;
00125 }
00126
00127 virtual double maxMeasurement() const
00128 {
00129 return calibrated ? max : std::numeric_limits<double>::max();
00130 }
00131
00132 virtual double minMeasurement() const
00133 {
00134
00135 return calibrated ? min : -std::numeric_limits<double>::max();
00136 }
00137
00138 virtual double zeroMeasurement() const
00139 {
00140 return 0.0;
00141 }
00142
00170 double calibrate( double calpos, double sign )
00171 {
00172 double currentPos;
00173 if ( sign < 0 )
00174 {
00175 if ( unit_to_inc > 0)
00176 currentPos = calpos - ( enc->resolution() - enc->positionGet() ) / unit_to_inc;
00177 else
00178 currentPos = calpos - enc->positionGet() / unit_to_inc;
00179 }
00180 else
00181 {
00182 if ( unit_to_inc > 0)
00183 currentPos = calpos + enc->positionGet() / unit_to_inc;
00184 else
00185 currentPos = calpos + (enc->resolution() - enc->positionGet() ) / unit_to_inc;
00186 }
00187 double oldposition = readSensor();
00188 posOffset = currentPos - (readSensor() - posOffset);
00189 calibrated = true;
00190 return readSensor() - oldposition;
00191 }
00192 };
00193
00194 }
00195
00196 #endif