00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "ComediEncoder.hpp"
00022 #include <rtt/Logger.hpp>
00023
00024 #include "comedi_internal.h"
00025 #include "comedi_common.h"
00026 #include "comedi.h"
00027
00028 namespace OCL
00029 {
00030 ComediEncoder::ComediEncoder(ComediDevice * cd, unsigned int subd, const std::string& name)
00031 : EncoderInterface(name),
00032 _myCard(cd), _subDevice(subd),
00033 _turn(0), _upcounting(true)
00034 {
00035 init();
00036 }
00037
00038 ComediEncoder::ComediEncoder(ComediDevice * cd, unsigned int subd)
00039 : _myCard(cd), _subDevice(subd),
00040 _turn(0), _upcounting(true)
00041 {
00042 init();
00043 }
00044
00045 void ComediEncoder::init()
00046 {
00047 Logger::In in("ComediEncoder");
00048 if (!_myCard) {
00049 log(Error) << "Error creating ComediEncoder: null ComediDevice given." <<endlog();
00050 return;
00051 }
00052 log(Info) << "Creating ComediEncoder\n" << endlog();
00053
00054 if ( _myCard->getSubDeviceType( _subDevice ) != COMEDI_SUBD_COUNTER )
00055 {
00056 log(Error) << "Comedi Counter : subdev is not a counter, type = "
00057 << _myCard->getSubDeviceType(_subDevice) << endlog();
00058 _myCard = 0;
00059 return;
00060 }
00061
00062
00063
00064
00065
00066 int retval = reset_counter(_myCard->getDevice()->it, _subDevice);
00067
00068 unsigned int initial_value = 0;
00069 retval = comedi_data_write(_myCard->getDevice()->it, _subDevice, 0, 0, 0, initial_value);
00070
00071 int counter_mode = (NI_GPCT_COUNTING_MODE_QUADRATURE_X4_BITS |
00072 NI_GPCT_COUNTING_DIRECTION_HW_UP_DOWN_BITS);
00073
00074 int retval1 = set_counter_mode(_myCard->getDevice()->it, _subDevice, counter_mode);
00075 int retval2 = arm(_myCard->getDevice()->it, _subDevice, NI_GPCT_ARM_IMMEDIATE);
00076 if(retval1 < 0 || retval2 < 0) {
00077 log(Error) << "Comedi Counter : Instruction to configure counter -> encoder failed (ret:"<<retval1<<","<<retval2<<")" << endlog();
00078 _myCard = 0;
00079 } else
00080 log(Info) << "Comedi Counter : configured as encoder now" << endlog();
00081
00082 _resolution = comedi_get_maxdata(_myCard->getDevice()->it,_subDevice, 0);
00083 if ( _resolution == 0) {
00084 log(Error) << "Comedi Counter : Could not retrieve encoder resolution !"<<endlog();
00085 }
00086 }
00087
00088 ComediEncoder::~ComediEncoder(){}
00089
00090 void ComediEncoder::positionSet(int p)
00091 {
00092 if (!_myCard)
00093 return;
00094
00095
00096 int retval;
00097
00098 retval = comedi_data_write(_myCard->getDevice()->it, _subDevice, 0, 0, 0, (lsampl_t)p);
00099
00100 retval = comedi_data_write(_myCard->getDevice()->it, _subDevice, 1, 0, 0, (lsampl_t)p);
00101
00102 retval = comedi_data_write(_myCard->getDevice()->it, _subDevice, 2, 0, 0, (lsampl_t)p);
00103 }
00104
00105 void ComediEncoder::turnSet(int t){ _turn = t;}
00106 int ComediEncoder::turnGet() const { return _turn;}
00107
00108 int ComediEncoder::positionGet() const
00109 {
00110 if (!_myCard)
00111 return 0;
00112
00113 lsampl_t pos;
00114 int ret=comedi_data_read(_myCard->getDevice()->it,_subDevice,0,0,0,&pos);
00115
00116 if(ret<0){
00117 log(Error) << "Comedi Counter : reading encoder failed, ret = " << ret << endlog();
00118 }
00119 return pos;
00120 }
00121
00122 int ComediEncoder::resolution() const {return _resolution;}
00123
00124 bool ComediEncoder::upcounting() const {return _upcounting;}
00125
00126 }