Visual Servoing Platform  version 3.6.1 under development (2024-03-29)
vpComedi.cpp
1 /****************************************************************************
2  *
3  * ViSP, open source Visual Servoing Platform software.
4  * Copyright (C) 2005 - 2023 by Inria. All rights reserved.
5  *
6  * This software is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  * See the file LICENSE.txt at the root directory of this source
11  * distribution for additional information about the GNU GPL.
12  *
13  * For using ViSP with software that can not be combined with the GNU
14  * GPL, please contact Inria about acquiring a ViSP Professional
15  * Edition License.
16  *
17  * See https://visp.inria.fr for more information.
18  *
19  * This software was developed at:
20  * Inria Rennes - Bretagne Atlantique
21  * Campus Universitaire de Beaulieu
22  * 35042 Rennes Cedex
23  * France
24  *
25  * If you have questions regarding the use of this file, please contact
26  * Inria at visp@inria.fr
27  *
28  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
29  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
30  *
31  * Description:
32  * ATI Force torque interface.
33  *
34 *****************************************************************************/
35 
36 #include <visp3/core/vpConfig.h>
37 
38 #ifdef VISP_HAVE_COMEDI
39 
40 #include <unistd.h>
41 
42 #include <visp3/core/vpException.h>
43 #include <visp3/sensor/vpComedi.h>
44 
49  : m_device("/dev/comedi0"), m_handler(nullptr), m_subdevice(0), m_range(0), m_aref(AREF_DIFF), m_nchannel(6),
50  m_range_info(6), m_maxdata(6), m_chanlist(6)
51 { }
52 
58 
63 {
64  if (!m_handler) {
65  m_handler = comedi_open(m_device.c_str());
66 
67  if (!m_handler) {
68  throw vpException(vpException::fatalError, "Could not open device %s", m_device.c_str());
69  }
70 
71  // Print NaN for clipped inputs
72  comedi_set_global_oor_behavior(COMEDI_OOR_NAN);
73 
74  // Setup data range and max value
75  m_range_info.resize(m_nchannel);
76  m_maxdata.resize(m_nchannel);
77  m_chanlist.resize(m_nchannel);
78  for (unsigned int channel = 0; channel < m_nchannel; channel++) {
79  m_chanlist[channel] = CR_PACK(channel, m_range, m_aref);
80  m_range_info[channel] = comedi_get_range(m_handler, m_subdevice, channel, m_range);
81  m_maxdata[channel] = comedi_get_maxdata(m_handler, m_subdevice, channel);
82  }
83  }
84 }
85 
90 {
91  if (m_handler) {
92  comedi_close(m_handler);
93  m_handler = nullptr;
94  }
95 }
96 
104 std::vector<lsampl_t> vpComedi::getRawData() const
105 {
106  if (m_handler == nullptr) {
107  throw vpException(vpException::fatalError, "Comedi device not open");
108  }
109  // Get raw data
110  std::vector<lsampl_t> raw_data(m_nchannel);
111 
112  for (unsigned int channel = 0; channel < m_nchannel; channel++) {
113  // When switching the multiplexor from one channel to the next, the A/D
114  // input needs time to settle to the new input voltage. The greater the
115  // voltage difference, the more time it takes. Here we wait for 1us
116  int ret = comedi_data_read_delayed(m_handler, m_subdevice, channel, m_range, m_aref, &raw_data[channel], 1000);
117  if (ret < 0) {
119  "Cannot get %d data from device=%s subdevice=%d "
120  "channel=%d range=%d analog reference=%d",
121  m_nchannel, m_device.c_str(), m_subdevice, channel, m_aref);
122  }
123  }
124 
125  return raw_data;
126 }
127 
133 {
134  if (m_handler == nullptr) {
135  throw vpException(vpException::fatalError, "Comedi device not open");
136  }
137  // Get raw data
138  std::vector<lsampl_t> raw_data = this->getRawData();
139  vpColVector phy_data(m_nchannel);
140 
141  // Convert data to physical data
142  for (unsigned int channel = 0; channel < m_nchannel; channel++) {
143  phy_data[channel] = comedi_to_phys(raw_data[channel], m_range_info[channel], m_maxdata[channel]);
144  if (vpMath::isNaN(phy_data[channel])) {
145  throw vpException(vpException::fatalError, "Comedi DAQ get NaN value. Check the connection with your device");
146  }
147  }
148 
149  return phy_data;
150 }
151 
154 std::string vpComedi::getPhyDataUnits() const
155 {
156  if (m_handler == nullptr) {
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:
163  units = "V";
164  break;
165  case UNIT_mA:
166  units = "mA";
167  break;
168  case UNIT_none:
169  break;
170  }
171  return units;
172 }
173 
174 #elif !defined(VISP_BUILD_SHARED_LIBS)
175 // Work around to avoid warning: libvisp_sensor.a(vpComedi.cpp.o) has symbols
176 void dummy_vpComedi() { };
177 #endif
Implementation of column vector and the associated operations.
Definition: vpColVector.h:163
virtual ~vpComedi()
Definition: vpComedi.cpp:57
unsigned int m_aref
Definition: vpComedi.h:170
std::vector< lsampl_t > m_maxdata
Definition: vpComedi.h:173
std::string m_device
Definition: vpComedi.h:166
std::vector< lsampl_t > getRawData() const
Definition: vpComedi.cpp:104
unsigned int m_range
Definition: vpComedi.h:169
vpColVector getPhyData() const
Definition: vpComedi.cpp:132
void open()
Definition: vpComedi.cpp:62
std::vector< comedi_range * > m_range_info
Definition: vpComedi.h:172
vpComedi()
Definition: vpComedi.cpp:48
unsigned int m_subdevice
Definition: vpComedi.h:168
comedi_t * m_handler
Definition: vpComedi.h:167
unsigned int m_nchannel
Definition: vpComedi.h:171
std::vector< unsigned int > m_chanlist
Definition: vpComedi.h:174
std::string getPhyDataUnits() const
Definition: vpComedi.cpp:154
void close()
Definition: vpComedi.cpp:89
error that can be emitted by ViSP classes.
Definition: vpException.h:59
@ fatalError
Fatal error.
Definition: vpException.h:84
static bool isNaN(double value)
Definition: vpMath.cpp:93