00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #include "LaserDistance.hpp"
00019 #include <iostream>
00020
00021 #if defined (OROPKG_DEVICE_DRIVERS_COMEDI)
00022 #define NR_CHAN 2
00023 #define OFFSET 0
00024 #endif
00025
00026 namespace OCL
00027 {
00028 using namespace RTT;
00029 using namespace std;
00030
00031 LaserDistance::LaserDistance(string name,unsigned int nr_chan ,string propertyfile):
00032 TaskContext(name),
00033 #if defined (OROPKG_DEVICE_DRIVERS_COMEDI)
00034 _LaserInput(nr_chan),
00035 #endif
00036 _nr_chan(nr_chan),
00037 _simulation_values("sim_values","Value used for simulation",vector<double>(nr_chan,0)),
00038 _volt2m("volt2m","Convert Factor from volt to m",vector<double>(nr_chan,0)),
00039 _offsets("offsets","Offset in m",vector<double>(nr_chan,0)),
00040 _lowerLimits("low_limits","LowerLimits of the distance sensor",vector<double>(nr_chan,0)),
00041 _upperLimits("up_limits","UpperLimits of the distance sensor",vector<double>(nr_chan,0)),
00042 _distances("LaserDistance",vector<double>(nr_chan,0)),
00043 _distanceOutOfRange("distanceOutOfRange"),
00044 _measurement(nr_chan),
00045 _distances_local(nr_chan),
00046 _propertyfile(propertyfile)
00047 {
00048 log(Debug) <<this->getName()<<": adding Properties"<<endlog();
00049 properties()->addProperty(&_simulation_values);
00050 properties()->addProperty(&_volt2m);
00051 properties()->addProperty(&_offsets);
00052 properties()->addProperty(&_upperLimits);
00053 properties()->addProperty(&_lowerLimits);
00054
00055 log(Debug) <<this->getName()<<": adding Ports"<<endlog();
00056 ports()->addPort(&_distances);
00057
00058 log(Debug) <<this->getName()<<": adding Events"<<endlog();
00059 events()->addEvent(&_distanceOutOfRange, "Distance out of Range", "C", "Channel", "V", "Value");
00060
00061 #if defined (OROPKG_DEVICE_DRIVERS_COMEDI)
00062 if(_nr_chan>NR_CHAN){
00063 log(Warning) <<"Only 2 hardware sensors currently available, resetting nr of channels to 2"<<endlog();
00064 _nr_chan = NR_CHAN;
00065 }
00066 log(Debug) <<this->getName()<<": Creating ComediDevice"<<endlog();
00067 _comediDev_NI6024 = new ComediDevice( 4 );
00068 int subd;
00069 subd = 0;
00070 log(Debug) <<this->getName()<<": Creating ComediSubDevice"<<endlog();
00071 _comediSubdevAIn = new ComediSubDeviceAIn( _comediDev_NI6024, "Laser", subd );
00072 for(unsigned int i = 0; i < nr_chan;i++){
00073 log(Debug) <<this->getName()<<": Creating AnalogInput "<<i<<endlog();
00074 _LaserInput[i] = new AnalogInput(_comediSubdevAIn, i+OFFSET);
00075 }
00076 #endif
00077
00078 if(!marshalling()->readProperties(_propertyfile))
00079 log(Error)<<"Reading properties failed."<<endlog();
00080
00081 log(Debug) <<this->getName()<<": constructed."<<endlog();
00082
00083
00084 }
00085
00086 LaserDistance::~LaserDistance()
00087 {
00088 #if defined (OROPKG_DEVICE_DRIVERS_COMEDI)
00089 delete _comediDev_NI6024;
00090 delete _comediSubdevAIn;
00091 for(unsigned int i=0; i<_LaserInput.size();i++)
00092 delete _LaserInput[i];
00093 #endif
00094 }
00095
00096 bool LaserDistance::startup()
00097 {
00098 if(_simulation_values.value().size()!=_nr_chan||
00099 _volt2m.value().size()!=_nr_chan||
00100 _offsets.value().size()!=_nr_chan||
00101 _upperLimits.value().size()!=_nr_chan||
00102 _lowerLimits.value().size()!=_nr_chan)
00103 {
00104 log(Error) <<"size of Properties do not match nr of channels"<<endlog();
00105 return false;
00106 }
00107 return true;
00108 }
00109
00110 void LaserDistance::update()
00111 {
00112 #if defined (OROPKG_DEVICE_DRIVERS_COMEDI)
00113 for(unsigned int i = 0 ; i<_nr_chan;i++){
00114 _measurement[i] = _LaserInput[i]->value();
00115 _distances_local[i] = _volt2m.value()[i]*_measurement[i]+_offsets.value()[i];
00116 if(_distances_local[i]<_lowerLimits.value()[i]||
00117 _distances_local[i]>_upperLimits.value()[i])
00118 _distanceOutOfRange(i,_distances_local[i]);
00119 }
00120 #else
00121 _distances_local = _simulation_values.value();
00122 #endif
00123 _distances.Set(_distances_local);
00124 }
00125
00126 void LaserDistance::shutdown()
00127 {
00128 marshalling()->writeProperties(_propertyfile);
00129 }
00130 }
00131