| 1 |
/* |
| 2 |
This program is free software; you can redistribute it and/or modify |
| 3 |
it under the terms of the GNU General Public License as published by |
| 4 |
the Free Software Foundation; either version 2 of the License, or |
| 5 |
(at your option) any later version. |
| 6 |
|
| 7 |
This program is distributed in the hope that it will be useful, |
| 8 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 9 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 10 |
GNU General Public License for more details. |
| 11 |
|
| 12 |
You should have received a copy of the GNU General Public License |
| 13 |
along with this program; if not, write to the Free Software |
| 14 |
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 15 |
*/ |
| 16 |
|
| 17 |
#include <boost/program_options/options_description.hpp> |
| 18 |
#include <boost/program_options/parsers.hpp> |
| 19 |
#include <boost/program_options/variables_map.hpp> |
| 20 |
#include "calibrator.hpp" |
| 21 |
#include "../libcomedi_calibrate/comedi_calibrate_shared.h" |
| 22 |
#include <comedilib.hpp> |
| 23 |
#include <iostream> |
| 24 |
#include "ni_m_series_calibrator.hpp" |
| 25 |
#include <sstream> |
| 26 |
#include <stdexcept> |
| 27 |
#include <string> |
| 28 |
#include <vector> |
| 29 |
|
| 30 |
void writeCalibrationSet(const CalibrationSet &calibration, const std::string &driverName, |
| 31 |
const std::string &boardName, const std::string &filePath) |
| 32 |
{ |
| 33 |
comedi_calibration_t *c_cal = static_cast<comedi_calibration_t *>(malloc(sizeof(comedi_calibration_t))); |
| 34 |
if(c_cal == 0) throw std::runtime_error("writeCalibrationSet: malloc failed\n"); |
| 35 |
memset(c_cal, 0, sizeof(comedi_calibration_t)); |
| 36 |
c_cal->driver_name = static_cast<char*>(malloc(driverName.size() + 1)); |
| 37 |
strcpy(c_cal->driver_name, driverName.c_str()); |
| 38 |
c_cal->board_name = static_cast<char*>(malloc(boardName.size() + 1)); |
| 39 |
strcpy(c_cal->board_name, boardName.c_str()); |
| 40 |
CalibrationSet::const_iterator it; |
| 41 |
for(it = calibration.begin(); it != calibration.end(); ++it) |
| 42 |
{ |
| 43 |
const SubdeviceCalibration &subdeviceCalibration = it->second; |
| 44 |
std::map<std::pair<unsigned, unsigned>, Polynomial>::const_iterator jt; |
| 45 |
for(jt = it->second.polynomials().begin(); jt != it->second.polynomials().end(); ++jt) |
| 46 |
{ |
| 47 |
comedi_calibration_setting_t *setting = sc_alloc_calibration_setting(c_cal); |
| 48 |
setting->subdevice = it->first; |
| 49 |
unsigned channel = jt->first.first; |
| 50 |
if(channel != SubdeviceCalibration::allChannels) |
| 51 |
{ |
| 52 |
sc_push_channel(setting, channel); |
| 53 |
} |
| 54 |
unsigned range = jt->first.second; |
| 55 |
if(range != SubdeviceCalibration::allRanges) |
| 56 |
{ |
| 57 |
sc_push_range(setting, range); |
| 58 |
} |
| 59 |
const Polynomial &polynomial = jt->second; |
| 60 |
comedi_polynomial_t *comediPolynomial = static_cast<comedi_polynomial_t*>(malloc(sizeof(comedi_polynomial_t))); |
| 61 |
assert(comediPolynomial); |
| 62 |
comediPolynomial->expansion_origin = polynomial.expansionOrigin; |
| 63 |
comediPolynomial->order = polynomial.order(); |
| 64 |
unsigned i; |
| 65 |
for(i = 0; i < polynomial.coefficients.size(); ++i) |
| 66 |
{ |
| 67 |
assert(i < COMEDI_MAX_NUM_POLYNOMIAL_COEFFICIENTS); |
| 68 |
comediPolynomial->coefficients[i] = polynomial.coefficients.at(i); |
| 69 |
} |
| 70 |
if(subdeviceCalibration.toPhys()) |
| 71 |
{ |
| 72 |
setting->soft_calibration.to_phys = comediPolynomial; |
| 73 |
}else |
| 74 |
{ |
| 75 |
setting->soft_calibration.from_phys = comediPolynomial; |
| 76 |
} |
| 77 |
} |
| 78 |
} |
| 79 |
int retval = write_calibration_file(filePath.c_str(), c_cal); |
| 80 |
comedi_cleanup_calibration(c_cal); |
| 81 |
if(retval) |
| 82 |
{ |
| 83 |
std::ostringstream message; |
| 84 |
message << __FUNCTION__ << ": write_calibration_file() failed."; |
| 85 |
throw std::runtime_error(message.str()); |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
class ComediSoftCalibrateApp |
| 90 |
{ |
| 91 |
public: |
| 92 |
ComediSoftCalibrateApp(int argc, char **argv); |
| 93 |
virtual ~ComediSoftCalibrateApp(); |
| 94 |
void exec(); |
| 95 |
private: |
| 96 |
boost::program_options::options_description desc; |
| 97 |
boost::program_options::variables_map vm; |
| 98 |
std::string _deviceFile; |
| 99 |
std::string _saveFile; |
| 100 |
comedi::device _comediDev; |
| 101 |
std::vector<boost::shared_ptr<Calibrator> > _calibrators; |
| 102 |
}; |
| 103 |
|
| 104 |
ComediSoftCalibrateApp::ComediSoftCalibrateApp(int argc, char **argv): |
| 105 |
desc("Allowed options") |
| 106 |
{ |
| 107 |
desc.add_options() |
| 108 |
("help", "produce this help message and exit") |
| 109 |
("file,f", boost::program_options::value<typeof(_deviceFile)>(&_deviceFile)->default_value("/dev/comedi0"), "device file") |
| 110 |
("save-file,S", boost::program_options::value<typeof(_saveFile)>(&_saveFile)->default_value(""), "calibration save file") |
| 111 |
; |
| 112 |
try |
| 113 |
{ |
| 114 |
boost::program_options::store(boost::program_options::parse_command_line(argc, argv, desc), vm); |
| 115 |
} |
| 116 |
catch(const std::exception &err) |
| 117 |
{ |
| 118 |
std::cerr << "Caught exception: " << err.what() << std::endl; |
| 119 |
std::cout << desc << std::endl; |
| 120 |
throw; |
| 121 |
} |
| 122 |
boost::program_options::notify(vm); |
| 123 |
_calibrators.push_back(boost::shared_ptr<Calibrator>(new NIMSeries::Calibrator())); |
| 124 |
} |
| 125 |
|
| 126 |
ComediSoftCalibrateApp::~ComediSoftCalibrateApp() |
| 127 |
{} |
| 128 |
|
| 129 |
void ComediSoftCalibrateApp::exec() |
| 130 |
{ |
| 131 |
if(vm.count("help")) |
| 132 |
{ |
| 133 |
std::cout << desc << std::endl; |
| 134 |
return; |
| 135 |
} |
| 136 |
_comediDev.open(_deviceFile); |
| 137 |
std::vector<boost::shared_ptr<Calibrator> >::iterator it; |
| 138 |
for(it = _calibrators.begin(); it != _calibrators.end(); ++it) |
| 139 |
{ |
| 140 |
if((*it)->supportedDriverName() != _comediDev.driver_name()) continue; |
| 141 |
std::vector<std::string> devices = (*it)->supportedDeviceNames(); |
| 142 |
std::vector<std::string>::iterator dit; |
| 143 |
for(dit = devices.begin(); dit != devices.end(); ++dit) |
| 144 |
{ |
| 145 |
if(*dit == _comediDev.board_name()) break; |
| 146 |
} |
| 147 |
if(dit == devices.end()) continue; |
| 148 |
break; |
| 149 |
} |
| 150 |
if(it == _calibrators.end()) |
| 151 |
{ |
| 152 |
std::ostringstream message; |
| 153 |
message << "Failed to find calibrator for " << _comediDev.driver_name() << " driver."; |
| 154 |
std::cerr << message.str() << std::endl; |
| 155 |
throw std::invalid_argument(message.str().c_str()); |
| 156 |
} |
| 157 |
CalibrationSet calibration = (*it)->calibrate(_comediDev); |
| 158 |
if(_saveFile == "") |
| 159 |
{ |
| 160 |
_saveFile = _comediDev.default_calibration_path(); |
| 161 |
} |
| 162 |
writeCalibrationSet(calibration, _comediDev.driver_name(), |
| 163 |
_comediDev.board_name(), _saveFile); |
| 164 |
} |
| 165 |
|
| 166 |
int main(int argc, char **argv) |
| 167 |
{ |
| 168 |
try |
| 169 |
{ |
| 170 |
ComediSoftCalibrateApp app(argc, argv); |
| 171 |
app.exec(); |
| 172 |
} |
| 173 |
catch(const std::exception &err) |
| 174 |
{ |
| 175 |
std::cerr << "Caught exception: " << err.what() << std::endl; |
| 176 |
return 1; |
| 177 |
} |
| 178 |
return 0; |
| 179 |
} |