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 #ifndef INCREMENTAL_ENCODER_SENSOR_HPP
00028 #define INCREMENTAL_ENCODER_SENSOR_HPP
00029
00030 #include <ocl/OCL.hpp>
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 {
00043 class IncrementalEncoderSensor
00044 : public SensorInterface<double>
00045 {
00046 EncoderInterface* enc;
00047 double unit_to_inc;
00048 double min;
00049 double max;
00050 double posOffset;
00051 bool calibrated;
00052 double calPos;
00053 int resolution;
00054
00055
00056 public:
00067 IncrementalEncoderSensor(EncoderInterface* _enc, double _unit_to_inc, double _calPos, double _minpos, double _maxpos, int _resolution)
00068 : enc(_enc), unit_to_inc(_unit_to_inc), min(_minpos), max(_maxpos), posOffset(0), calibrated(false), calPos(_calPos), resolution(_resolution)
00069 {}
00070
00071 virtual int readSensor( double& p ) const
00072 {
00073 p = readSensor();
00074 return 0;
00075 }
00076
00080 void limit(double _min, double _max)
00081 {
00082 min = _min;
00083 max = _max;
00084 }
00085
00086 virtual void calibrate()
00087 {
00088 if( enc->upcounting() )
00089 {
00090 posOffset = calPos - ( enc->turnGet() * resolution + enc->positionGet() ) / unit_to_inc;
00091 }
00092 else
00093 {
00094 posOffset = calPos - ( enc->turnGet() * resolution + ( resolution - enc->positionGet() ) ) / unit_to_inc;
00095 }
00096
00097 this->calibrated = true;
00098 }
00099
00100 virtual void unCalibrate()
00101 {
00102 this->calibrated = false;
00103 }
00104
00105 virtual bool isCalibrated() const
00106 {
00107 return this->calibrated;
00108 }
00109
00110 virtual double readSensor() const
00111 {
00112 if ( enc->upcounting() )
00113 return ( enc->turnGet() * resolution + enc->positionGet() ) / unit_to_inc - posOffset;
00114 else
00115 return ( enc->turnGet() * resolution + ( resolution - enc->positionGet() ) ) / unit_to_inc - posOffset;
00116 }
00117
00118 virtual void writeSensor(double q) const
00119 {
00120 enc->positionSet((int)(unit_to_inc * q));
00121 }
00122
00123 virtual double maxMeasurement() const
00124 {
00125 return calibrated ? max : std::numeric_limits<double>::max();
00126 }
00127
00128 virtual double minMeasurement() const
00129 {
00130
00131 return calibrated ? min : -std::numeric_limits<double>::max();
00132 }
00133
00134 virtual double zeroMeasurement() const
00135 {
00136 return 0.0;
00137 }
00138 };
00139 }
00140
00141 #endif