Visual Servoing Platform  version 3.0.1
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
vpComedi.cpp
1 /****************************************************************************
2  *
3  * This file is part of the ViSP software.
4  * Copyright (C) 2005 - 2017 by Inria. All rights reserved.
5  *
6  * This software is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * ("GPL") version 2 as published by the Free Software Foundation.
9  * See the file LICENSE.txt at the root directory of this source
10  * distribution for additional information about the GNU GPL.
11  *
12  * For using ViSP with software that can not be combined with the GNU
13  * GPL, please contact Inria about acquiring a ViSP Professional
14  * Edition License.
15  *
16  * See http://visp.inria.fr for more information.
17  *
18  * This software was developed at:
19  * Inria Rennes - Bretagne Atlantique
20  * Campus Universitaire de Beaulieu
21  * 35042 Rennes Cedex
22  * France
23  *
24  * If you have questions regarding the use of this file, please contact
25  * Inria at visp@inria.fr
26  *
27  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
28  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
29  *
30  * Description:
31  * ATI Force torque interface.
32  *
33  * Authors:
34  * Fabien Spindler
35  *
36  *****************************************************************************/
37 
38 #include <visp3/core/vpConfig.h>
39 
40 #ifdef VISP_HAVE_COMEDI
41 
42 #include <unistd.h>
43 
44 #include <visp3/core/vpException.h>
45 #include <visp3/sensor/vpComedi.h>
46 
51  : m_device("/dev/comedi0"), m_handler(NULL), m_subdevice(0), m_range(0), m_aref(AREF_DIFF),
52  m_nchannel(6), m_range_info(6), m_maxdata(6), m_chanlist(6)
53 {
54 }
55 
61 {
62  close();
63 }
64 
69 {
70  if (! m_handler) {
71  m_handler = comedi_open(m_device.c_str());
72 
73  if (!m_handler) {
74  throw vpException(vpException::fatalError, "Could not open device %s", m_device.c_str());
75  }
76 
77  // Print NaN for clipped inputs
78  comedi_set_global_oor_behavior(COMEDI_OOR_NAN);
79 
80  // Setup data range and max value
81  m_range_info.resize(m_nchannel);
82  m_maxdata.resize(m_nchannel);
83  m_chanlist.resize(m_nchannel);
84  for(unsigned int channel=0; channel < m_nchannel; channel++) {
85  m_chanlist[channel] = CR_PACK(channel, m_range, m_aref);
86  m_range_info[channel] = comedi_get_range(m_handler, m_subdevice, channel, m_range);
87  m_maxdata[channel] = comedi_get_maxdata(m_handler, m_subdevice, channel);
88  }
89  }
90 }
91 
96 {
97  if (m_handler) {
98  comedi_close(m_handler);
99  m_handler = NULL;
100  }
101 }
102 
109 std::vector<lsampl_t> vpComedi::getRawData() const
110 {
111  if (m_handler == NULL) {
112  throw vpException(vpException::fatalError, "Comedi device not open");
113  }
114  // Get raw data
115  std::vector<lsampl_t> raw_data(m_nchannel);
116 
117  for(unsigned int channel=0; channel < m_nchannel; channel++) {
118  // When switching the multiplexor from one channel to the next, the A/D input needs time to settle to the
119  // new input voltage. The greater the voltage difference, the more time it takes. Here we wait for 1us
120  int ret = comedi_data_read_delayed(m_handler, m_subdevice, channel, m_range, m_aref, &raw_data[channel], 1000);
121  if(ret < 0){
122  throw vpException(vpException::fatalError, "Cannot get %d data from device=%s subdevice=%d channel=%d range=%d analog reference=%d",
123  m_nchannel, m_device.c_str(), m_subdevice, channel, m_aref);
124  }
125  }
126 
127  return raw_data;
128 }
129 
134 {
135  if (m_handler == NULL) {
136  throw vpException(vpException::fatalError, "Comedi device not open");
137  }
138  // Get raw data
139  std::vector<lsampl_t> raw_data = this->getRawData();
140  vpColVector phy_data(m_nchannel);
141 
142  // Convert data to physical data
143  for(unsigned int channel=0; channel < m_nchannel; channel++) {
144  phy_data[channel] = comedi_to_phys(raw_data[channel], m_range_info[channel], m_maxdata[channel]);
145  if (vpMath::isNaN(phy_data[channel])) {
146  throw vpException(vpException::fatalError, "Comedi DAQ get NaN value. Check the connection with your device");
147  }
148  }
149 
150  return phy_data;
151 }
152 
154 std::string vpComedi::getPhyDataUnits() const
155 {
156  if (m_handler == NULL) {
157  throw vpException(vpException::fatalError, "Comedi device not open");
158  }
159  std::string units;
160  unsigned int channel = 0;
161  switch(m_range_info[channel]->unit) {
162  case UNIT_volt: units = "V"; break;
163  case UNIT_mA: units = "mA"; break;
164  case UNIT_none: break;
165  }
166  return units;
167 }
168 
169 #elif !defined(VISP_BUILD_SHARED_LIBS)
170 // Work arround to avoid warning: libvisp_sensor.a(vpComedi.cpp.o) has no symbols
171 void dummy_vpComedi() {};
172 #endif
void open()
Definition: vpComedi.cpp:68
vpComedi()
Definition: vpComedi.cpp:50
static bool isNaN(const double value)
Definition: vpMath.cpp:85
error that can be emited by ViSP classes.
Definition: vpException.h:73
std::vector< lsampl_t > getRawData() const
Definition: vpComedi.cpp:109
unsigned int m_aref
Definition: vpComedi.h:188
unsigned int m_range
Definition: vpComedi.h:187
unsigned int m_subdevice
Definition: vpComedi.h:186
virtual ~vpComedi()
Definition: vpComedi.cpp:60
std::vector< unsigned int > m_chanlist
Definition: vpComedi.h:192
Implementation of column vector and the associated operations.
Definition: vpColVector.h:72
comedi_t * m_handler
Definition: vpComedi.h:185
std::vector< lsampl_t > m_maxdata
Definition: vpComedi.h:191
vpColVector getPhyData() const
Definition: vpComedi.cpp:133
std::string getPhyDataUnits() const
Get units (V or mA) of the physical data acquired by getPhyData() or getPhyDataAsync().
Definition: vpComedi.cpp:154
void close()
Definition: vpComedi.cpp:95
std::string m_device
Definition: vpComedi.h:184
unsigned int m_nchannel
Definition: vpComedi.h:189
std::vector< comedi_range * > m_range_info
Definition: vpComedi.h:190