| 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 |
#ifndef _NI_M_SERIES_CALIBRATOR_HPP |
| 18 |
#define _NI_M_SERIES_CALIBRATOR_HPP |
| 19 |
|
| 20 |
#include "calibrator.hpp" |
| 21 |
#include "calibrator_misc.hpp" |
| 22 |
#include <map> |
| 23 |
#include <string> |
| 24 |
#include <vector> |
| 25 |
|
| 26 |
namespace NIMSeries |
| 27 |
{ |
| 28 |
class References |
| 29 |
{ |
| 30 |
public: |
| 31 |
static const int positive_cal_shift = 7; |
| 32 |
static const int negative_cal_shift = 10; |
| 33 |
enum PositiveCalSource |
| 34 |
{ |
| 35 |
POS_CAL_REF = 2 << positive_cal_shift, |
| 36 |
POS_CAL_PWM_500mV = 3 << positive_cal_shift, |
| 37 |
POS_CAL_PWM_2V = 4 << positive_cal_shift, |
| 38 |
POS_CAL_PWM_10V = 5 << positive_cal_shift, |
| 39 |
POS_CAL_GROUND = 6 << positive_cal_shift, |
| 40 |
POS_CAL_AO = 7 << positive_cal_shift |
| 41 |
}; |
| 42 |
enum NegativeCalSource |
| 43 |
{ |
| 44 |
NEG_CAL_1V = 2 << negative_cal_shift, |
| 45 |
NEG_CAL_1mV = 3 << negative_cal_shift, |
| 46 |
NEG_CAL_GROUND = 5 << negative_cal_shift, |
| 47 |
NEG_CAL_GROUND2 = 6 << negative_cal_shift, |
| 48 |
NEG_CAL_PWM_10V = 7 << negative_cal_shift, |
| 49 |
}; |
| 50 |
References(const comedi::device &dev); |
| 51 |
void setPWM(unsigned high_ns, unsigned low_ns, unsigned *actual_high_ns = 0, unsigned *actual_low_ns = 0); |
| 52 |
void setReference(enum PositiveCalSource posSource, enum NegativeCalSource NegSource); |
| 53 |
void setReference(unsigned AOChannel); |
| 54 |
std::vector<lsampl_t> readReference(unsigned numSamples, unsigned samplePeriodNanosec, unsigned inputRange, unsigned settleNanosec) const; |
| 55 |
std::vector<double> readReferenceDouble(unsigned numSamples, unsigned samplePeriodNanosec, unsigned inputRange, unsigned settleNanosec) const; |
| 56 |
unsigned getMinSamplePeriodNanosec() const; |
| 57 |
private: |
| 58 |
void setReferenceBits(unsigned bits); |
| 59 |
|
| 60 |
comedi::device _dev; |
| 61 |
}; |
| 62 |
|
| 63 |
/* Calibrator for National Instruments M-Series boards. */ |
| 64 |
class Calibrator: public ::Calibrator |
| 65 |
{ |
| 66 |
public: |
| 67 |
Calibrator(); |
| 68 |
virtual std::string supportedDriverName() const {return "ni_pcimio";} |
| 69 |
virtual std::vector<std::string> supportedDeviceNames() const; |
| 70 |
virtual CalibrationSet calibrate(const comedi::device &dev); |
| 71 |
private: |
| 72 |
static const unsigned numSamples = 15000; |
| 73 |
static const unsigned settleNanosec = 1000000; |
| 74 |
static const unsigned baseRange = 0; |
| 75 |
static const unsigned masterClockPeriodNanosec = 50; |
| 76 |
static const unsigned minimumPWMPulseTicks = 0x20; |
| 77 |
static const unsigned TargetPWMPeriodTicks = 20 * minimumPWMPulseTicks; |
| 78 |
|
| 79 |
const SubdeviceCalibration calibrateAISubdevice(); |
| 80 |
Polynomial calibrateAINonlinearity(const std::map<unsigned, double> &PWMCharacterization); |
| 81 |
// calibrate the one range that can actually read the onboard voltage reference directly |
| 82 |
Polynomial calibrateAIBaseRange(const Polynomial &nonlinearityCorrection); |
| 83 |
Polynomial calibrateAIRange(const Polynomial &PWMCalibration, const Polynomial &nonlinearityCorrection, |
| 84 |
enum NIMSeries::References::PositiveCalSource posSource, unsigned range); |
| 85 |
Polynomial calibratePWM(const std::map<unsigned, double> &PWMCharacterization, |
| 86 |
const Polynomial &baseRangeCalibration); |
| 87 |
Polynomial calibrateAIGainAndOffset(const Polynomial &nonlinearityCorrection, |
| 88 |
enum NIMSeries::References::PositiveCalSource posReferenceSource, double referenceVoltage, unsigned range); |
| 89 |
void setPWMUpTicks(unsigned upTicks); |
| 90 |
std::map<unsigned, double> characterizePWM(enum NIMSeries::References::PositiveCalSource posReferenceSource, unsigned ADRange); |
| 91 |
unsigned smallestCalibratedAIRangeContaining(const std::vector<bool> &calibrated, double rangeThreshold); |
| 92 |
void calibrateAIRangesAboveThreshold(const Polynomial &PWMCalibration, const Polynomial &nonlinearityCorrection, |
| 93 |
enum NIMSeries::References::PositiveCalSource posReferenceSource, |
| 94 |
SubdeviceCalibration *AICalibration, std::vector<bool> *calibrated, double maxRangeThreshold); |
| 95 |
// round numSamples so we sample over an integer number of PWM periods |
| 96 |
unsigned PWMRoundedNumSamples(unsigned numSamples, unsigned samplePeriodNS) const; |
| 97 |
void checkAIBufferSize(); |
| 98 |
unsigned PWMPeriodTicks() const; |
| 99 |
const SubdeviceCalibration calibrateAOSubdevice(const SubdeviceCalibration &AICalibration); |
| 100 |
Polynomial calibrateAOChannelAndRange(const Polynomial &AICalibration, |
| 101 |
unsigned AIRange, unsigned AOChannel, unsigned AORange); |
| 102 |
unsigned findAIRangeForAO(unsigned AORange) const; |
| 103 |
// returns an AO code that will produce a high voltage inside the specified ai input range |
| 104 |
lsampl_t highCode(unsigned AIRange, unsigned AORange) const; |
| 105 |
void dumpAICalibrationSources(); |
| 106 |
|
| 107 |
comedi::device _dev; |
| 108 |
boost::shared_ptr<References> _references; |
| 109 |
}; |
| 110 |
|
| 111 |
class EEPROM |
| 112 |
{ |
| 113 |
public: |
| 114 |
EEPROM(const comedi::device &dev); |
| 115 |
float referenceVoltage() const; |
| 116 |
private: |
| 117 |
enum CalibrationAreaOffsets |
| 118 |
{ |
| 119 |
voltageReferenceOffset = 12 |
| 120 |
}; |
| 121 |
unsigned calibrationAreaBaseAddress() const; |
| 122 |
unsigned readByte(unsigned address) const; |
| 123 |
unsigned readUInt16(unsigned startAddress) const; |
| 124 |
float readFloat(unsigned startAddress) const; |
| 125 |
comedi::device _dev; |
| 126 |
}; |
| 127 |
}; |
| 128 |
|
| 129 |
#endif // _NI_M_SERIES_CALIBRATOR_HPP |