00001 #ifndef ANALOGETHERCATOUTPUTDEVICE_HPP
00002 #define ANALOGETHERCATOUTPUTDEVICE_HPP
00003
00004 #include "rtt/dev/AnalogOutInterface.hpp"
00005
00006 namespace RTT
00007 {
00012 struct AnalogEtherCATOutputDevice :
00013 public AnalogOutInterface
00014 {
00015 unsigned char* mstartaddress;
00016 unsigned int nbofchans;
00017 double* mchannels;
00018 unsigned int mbin_range;
00019 double mlowest, mhighest;
00020
00021 AnalogEtherCATOutputDevice(unsigned char* startaddress, unsigned int channels=2, unsigned int bin_range=32676, double lowest = 0, double highest = 10)
00022 : AnalogOutInterface("AnalogOutDevice"),
00023 mstartaddress(startaddress),
00024 nbofchans(channels),
00025 mchannels( new double[channels] ),
00026 mbin_range( bin_range),
00027 mlowest( lowest),
00028 mhighest( highest)
00029 {}
00030
00031 ~AnalogEtherCATOutputDevice() {
00032 delete[] mchannels;
00033 }
00034
00035 virtual void rangeSet(unsigned int ,
00036 unsigned int ) {}
00037
00038 virtual void arefSet(unsigned int ,
00039 unsigned int ) {}
00040
00041 virtual unsigned int nbOfChannels() const {
00042 return nbofchans;
00043 }
00044
00045 virtual int rawWrite( unsigned int chan, unsigned int value ) {
00046 if (chan < nbofchans && value >= 0 && value <= mbin_range) {
00047 unsigned int tmp = value;
00048 mstartaddress[(chan * 2)] = tmp;
00049 mstartaddress[(chan * 2) + 1]= (tmp>>8);
00050 mchannels[chan] = value;
00051 return 0;
00052 }
00053 return -1;
00054 }
00055
00056 virtual int write( unsigned int chan, double value ) {
00057 unsigned int tmp = (unsigned int)((value - mlowest) * mbin_range / (mhighest - mlowest)) ;
00058 return write(chan, tmp);
00059 }
00060
00061 virtual int rawRead( unsigned int chan, unsigned int& value ) {
00062 if (chan < nbofchans) {
00063 unsigned int tmp = mstartaddress[(chan * 3) + 2];
00064 tmp = tmp<<8;
00065 tmp = tmp | mstartaddress[(chan * 3) + 1];
00066 value = tmp;
00067 return 0;
00068 }
00069 return -1;
00070 }
00071
00072 virtual int read( unsigned int chan, double& value ) {
00073 if (chan < nbofchans) {
00074 unsigned int tmp;
00075 read(chan, tmp);
00076 value = 1.0 * tmp * (mhighest - mlowest) / mbin_range + mlowest;
00077 return 0;
00078 }
00079 return -1;
00080 }
00081
00082
00083 virtual unsigned int binaryRange() const
00084 {
00085 return mbin_range;
00086 }
00087
00088 virtual unsigned int binaryLowest() const
00089 {
00090 return 0;
00091 }
00092
00093 virtual unsigned int binaryHighest() const
00094 {
00095 return mbin_range;
00096 }
00097
00098 virtual double lowest(unsigned int ) const
00099 {
00100 return mlowest;
00101 }
00102
00103 virtual double highest(unsigned int ) const
00104 {
00105 return mhighest;
00106 }
00107
00108 virtual double resolution(unsigned int ) const
00109 {
00110 return mbin_range/(mhighest-mlowest);
00111 }
00112
00113 };
00114
00115 }
00116
00117 #endif