我正在使用NI数据采集卡读取力传感器数据,我能够很好地读取数据,但当我检查采样率时,我混淆了如何设置期望的采样rate.Right,它给我的是随机值,就像某个时间是500 is和一些时间是50000hz。
这是我用来读取力传感器数据和计算采样率的代码。
main.h file
int Run_main(void *pUserData)
{
/** to visual data **/
CAdmittanceDlg* pDlg = (CAdmittanceDlg*)pUserData;
/** timer for calculating sampling rate **/
performanceTest timer;
/*** Force Sensor **/
ForceSensor sensor1("FT8682.cal","FT_Sensor1","dev3/ai0:6",2,1.0e4);
std::vector<double> force;
while(1)
{
/*** start time ***/
timer.setStartTimeNow();
/*** reading force sensor data ***/
force=sensor1.readForce();
/*** visualize data in GUI ***/
pDlg->setCustomData(0,"force_x ",force[0]);
pDlg->setCustomData(1,"force_y ",force[1]);
pDlg->setCustomData(2,"force_z ",force[2]);
pDlg->setCustomData(3,"position ",currentPosition);
timer.increaseFrequencyCount();
/*** end time ***/
pDlg->setCustomData(4,"Sampling rate ",1/timer.getElapsedTime());
}
return 1;
}
//here is ForceSensor.h file
class ForceSensor
{
public:
DAQmx board;
Calibration *cal;
float bias[7];
std::vector<double> f;
ForceSensor(std::string calibrationFile, std::string task,
std::string device, uInt64 numberOfSamples = 1000, float64 samplingRate = 1.0e4):
board(task, device, numberOfSamples, samplingRate),
f(6, 0)
{
board.initRead();
cal = createCalibration(calibrationFile.c_str(), 1);
if (!cal) throw std::runtime_error(calibrationFile + " couldn't be opened");
board.readVoltage(2); // to get reed of zeros in the first batch of samples
board.readVoltage(1000); // read big number of samples to determine the bias
std::copy (board.da.cbegin(), board.da.cend(), std::ostream_iterator<double>(std::cout, " "));
std::cout << std::endl;
Bias(cal, board.da.data());
}
~ForceSensor(void){}
const std::vector<double> & readForce(){
auto forces = std::vector<float>(6, 0);
board.readVoltage(2);
ConvertToFT(cal, board.da.data(), forces.data());
std::transform(forces.cbegin(), forces.cend(),
f.begin(),
[](float a){ return static_cast<double>(a);});
return f;
}
};
//DAQmx.h file
class DAQmx {
float64 MaxVoltage;
TaskHandle taskHandle;
TaskHandle counterHandle;
std::string taskName;
std::string device;
float64 samplingRate;
public:
std::vector <float> da;
uInt64 numberOfSamples;
DAQmx(std::string task, std::string device, uInt64 numberOfSamples = 1000, float64 samplingRate = 1.0e4):
taskName(task), device(device), samplingRate(samplingRate), numberOfSamples(numberOfSamples),
da(7, 0.0f)
{
MaxVoltage = 10.0;
}
~DAQmx()
{
if( taskHandle == 0 ) return;
DAQmxStopTask(taskHandle);
DAQmxClearTask(taskHandle);
}
void CheckErr(int32 error, std::string functionName = "")
{
char errBuff[2048]={'\0'};
if( DAQmxFailed(error) ){
DAQmxGetExtendedErrorInfo(errBuff, 2048);
if( taskHandle!=0 ) {
DAQmxStopTask(taskHandle);
DAQmxClearTask(taskHandle);
}
std::cerr << functionName << std::endl;
throw std::runtime_error(errBuff);
}
}
void initRead()
{
CheckErr(DAQmxCreateTask(taskName.c_str(), &taskHandle));
CheckErr(DAQmxCreateAIVoltageChan(taskHandle, device.c_str(),"", DAQmx_Val_Diff ,-10.0,10.0,DAQmx_Val_Volts,NULL));
// CheckErr(DAQmxCfgSampClkTiming(taskHandle, "" , samplingRate, DAQmx_Val_Rising, DAQmx_Val_FiniteSamps, numberOfSamples));
CheckErr(DAQmxCfgSampClkTiming(taskHandle, "OnboardClock" , samplingRate, DAQmx_Val_Rising, DAQmx_Val_ContSamps, numberOfSamples*10));
CheckErr(DAQmxSetReadRelativeTo(taskHandle, DAQmx_Val_MostRecentSamp ));
CheckErr(DAQmxSetReadOffset(taskHandle,-1));
CheckErr(DAQmxStartTask(taskHandle));
}
void initWrite()
{
CheckErr(DAQmxCreateTask(taskName.c_str(), &taskHandle));
CheckErr(DAQmxCreateAOVoltageChan(taskHandle, device.c_str(),"",-10.0, 10.0, DAQmx_Val_Volts,""));
CheckErr(DAQmxStartTask(taskHandle));
}
int32 readVoltage (uInt64 samples = 0, bool32 fillMode = DAQmx_Val_GroupByScanNumber) //the other option is DAQmx_Val_GroupByScanNumber
{
int32 read; // samples actually read
const float64 timeOut = 10;
if (samples == 0) samples = numberOfSamples;
std::vector<float64> voltagesRaw(7*samples);
CheckErr(DAQmxReadAnalogF64(taskHandle, samples, timeOut, fillMode,
voltagesRaw.data(), 7*samples, &read, NULL));
// DAQmxStopTask(taskHandle);
if (read < samples)
throw std::runtime_error ("DAQmx::readVoltage(): couldn't read all the samples,"
"requested: " + std::to_string(static_cast<long long>(samples)) +
", actually read: " + std::to_string(static_cast<long long>(read)));
//we change it
for(int axis=0;axis < 7; axis++)
{
double temp = 0.0;
for(int i=0;i<read;i++)
{
temp += voltagesRaw[i*7+axis];
}
da[axis] = temp / read;
}
return read;
}
void writeVoltage(float64 value)
{
if (value > MaxVoltage) value = MaxVoltage;
if (value < -MaxVoltage) value = -MaxVoltage;
const float64 timeOut = 10;
//A value of 0 indicates to try once to read the requested samples.
//If all the requested samples are read, the function is successful.
//Otherwise, the function returns a timeout error and returns the samples that were actually read.
float64 data[1] = {value};
int32 written;
CheckErr(DAQmxWriteAnalogF64(taskHandle, 1, 1, timeOut, DAQmx_Val_GroupByChannel, data, &written, NULL));
DAQmxStopTask(taskHandle);
}
};编辑:当我改变样本数量时,我的应用程序的吞吐量急剧下降,在ForceSensor.h文件中,如果我将下面函数中的样本数量从2更改为100,则应用程序的吞吐量会从10kHz.Please下降到500 to,这与此相关。
const std::vector<double> & readForce(){
auto forces = std::vector<float>(6, 0);
//changing value from 2 to 100 decrease the throughput from 10Khz to 500Hz
board.readVoltage(2);
ConvertToFT(cal, board.da.data(), forces.data());
std::transform(forces.cbegin(), forces.cend(),
f.begin(),
[](float a){ return static_cast<double>(a);});
return f;
}我还使用NI运动卡来控制电机,下面是我添加NI运动代码的main.h文件。当我在main.h文件中添加NI运动代码时,应用程序的吞吐量将降低到200 in,无论我在测力传感器中使用了多少个样本。当同时使用NI运动控制电机和DAQ读取力传感器时,是否能提高应用程序的吞吐量?
main.h
inline int Run_main(void *pUserData)
{
/** to visual data **/
CAdmittanceDlg* pDlg = (CAdmittanceDlg*)pUserData;
/** timer for calculating sampling rate **/
performanceTest timer;
/*** Force Sensor **/
ForceSensor sensor1("FT8682.cal","FT_Sensor1","dev3/ai0:6",2,4.0e4);
/*** NI Motion Card 2=board ID, 0x01=axis number**/
NiMotion Drive(2,0x01);
int count=0;
sensor1.Sampling_rate_device(&sampling_rate);
std::vector<double> force;
timer.setStartTimeNow();
while(x)
{
/*** start time ***/
/*** reading force sensor data ***/
force=sensor1.readForce();
/*** reading current position of drive ***/
currentPosition = Drive.readPosition();
/*** Actuate Drive ***/
Drive.actuate(2047);
enalble_motor=Drive.isEnabled();
/*** visualize data in GUI ***/
pDlg->setCustomData(0,"force_x ",force[0]);
pDlg->setCustomData(1,"force_y ",force[1]);
pDlg->setCustomData(2,"force_z ",force[2]);
pDlg->setCustomData(3,"position ",currentPosition);
pDlg->setCustomData(5,"sampling rate of device ",sampling_rate);
timer.increaseFrequencyCount();
count++;
if(count==1000)
{
pDlg->setCustomData(4,"time elapsed ",count/timer.getElapsedTime());
count=0;
timer.setStartTimeNow();
}
/*** end time ***/
}
return 1;
}
//here is NiMotion.file
class NiMotion {
u8 boardID; // Board identification number
u8 axis; // Axis number
u16 csr; // Communication status register
u16 axisStatus; // Axis status
u16 moveComplete;
int32 encoderCounts; //current position [counts]
int32 encoderCountsStartPosition;
double position; //Position in meters
bool read_first;
public:
NiMotion(u8 boardID = 1, u8 axis = NIMC_AXIS1): boardID(boardID), axis(axis), csr(0)
{
init();
}
~NiMotion(){enableMotor(false);}
void init() {
CheckErr(flex_initialize_controller(boardID, nullptr)); // use default settings
CheckErr(flex_read_pos_rtn(boardID, axis, &encoderCounts));
encoderCountsStartPosition = encoderCounts;
enableMotor(false);
read_first=true;
loadConfiguration();
}
double toCm(i32 counts){ return counts*countsToCm; }
i32 toCounts(double Cm){ return (Cm/countsToCm); }
double readPosition(){
// CheckErr(flex_read_pos_rtn(boardID, axis, &encoderCounts));
CheckErr(flex_read_encoder_rtn(boardID, axis, &encoderCounts));
if(read_first)
{
encoderCountsStartPosition = encoderCounts;
read_first=false;
}
encoderCounts -= encoderCountsStartPosition;
position = encoderCounts*countsToCm;
return position;
}
void enableMotor(bool state)
{
if (state)
CheckErr(flex_set_inhibit_output_momo(boardID, 1 << axis, 0));
else
CheckErr(flex_set_inhibit_output_momo(boardID, 0, 1 << axis));
}
bool isEnabled(){
u16 home = 0;
CheckErr(flex_read_home_input_status_rtn(boardID, &home));
return (home & (1 << axis));
}
// void resetPosition()
// {
//// CheckErr(flex_reset_encoder(boardID, NIMC_ENCODER1, 0, 0xFF));
// CheckErr(flex_reset_pos(boardID, NIMC_AXIS1, 0, 0, 0xFF));
// }
void actuate(double positionCommand)
{
int32 positionCommandCounts = toCounts(positionCommand);
// CheckErr(flex_load_dac(boardID, NIMC_DAC1, std::lround(positionCommand), 0xFF));
// CheckErr(flex_load_dac(boardID, NIMC_DAC2, std::lround(positionCommand), 0xFF));
// CheckErr(flex_load_dac(boardID, NIMC_DAC3, std::lround(positionCommand), 0xFF));
// CheckErr(flex_load_dac(boardID, NIMC_DAC1, (positionCommand), 0xFF));
CheckErr(flex_load_target_pos(boardID, axis, (positionCommand), 0xFF));
CheckErr(flex_start(2, axis, 0));
//CheckErr(flex_load_target_pos (2, 0x01, 2047, 0xFF));
// CheckErr(flex_load_dac(boardID, NIMC_DAC3, 10000, 0xFF));
// std::cout << PositionCotroller(desiredPositionCounts, encoderCounts) << std::endl;
// std::this_thread::sleep_for(cycle);
}
void moveToPosition(double desiredPosition, double P, double I, double D)
{
/* int32 desiredPositionCounts = toCounts(desiredPosition);
std::chrono::milliseconds cycle(100);
PIDController PositionCotroller = PIDController(P, I, D, 0.1);
while((encoderCounts - desiredPositionCounts)*(encoderCounts - desiredPositionCounts) > 100){
CheckErr(flex_load_dac(boardID, NIMC_DAC1, PositionCotroller(desiredPositionCounts, encoderCounts), 0xFF));
std::cout << PositionCotroller(desiredPositionCounts, encoderCounts) << std::endl;
std::this_thread::sleep_for(cycle);*/
// }
}
void loadConfiguration()
{
// Set the velocity for the move (in counts/sec)
CheckErr(flex_load_velocity(boardID, axis, 10000, 0xFF));
// Set the acceleration for the move (in counts/sec^2)
CheckErr(flex_load_acceleration(boardID, axis, NIMC_ACCELERATION, 100000, 0xFF));
// Set the deceleration for the move (in counts/sec^2)
CheckErr(flex_load_acceleration(boardID, axis, NIMC_DECELERATION, 100000, 0xFF));
// Set the jerk (s-curve value) for the move (in sample periods)
CheckErr(flex_load_scurve_time(boardID, axis, 100, 0xFF));
// Set the operation mode to velocity
CheckErr(flex_set_op_mode(boardID, axis, NIMC_RELATIVE_POSITION));
}
void CheckErr(int32 error){
if(error !=0 ) {
u32 sizeOfArray; //Size of error description
u16 descriptionType = NIMC_ERROR_ONLY; //The type of description to be printed
u16 commandID = 0;
u16 resourceID = 0;
sizeOfArray = 0;
flex_get_error_description(descriptionType, error, commandID, resourceID, NULL, &sizeOfArray );
// NULL means that we want to get the size of the message, not message itself
sizeOfArray++;
std::unique_ptr<i8[]> errorDescription(new i8[sizeOfArray]);
flex_get_error_description(descriptionType, error, commandID, resourceID, errorDescription.get(), &sizeOfArray);
throw std::runtime_error(errorDescription.get());
}
}
};发布于 2015-01-28 21:46:30
你的问题有两部分:
1.抽样率与申请成功率
在你的Run_main里
pDlg->setCustomData(4,"Sampling rate ",1/timer.getElapsedTime());这似乎就是你“检查抽样率”的方式。这不是报告样本率,而是报告应用程序吞吐量。
询问抽样率
如果要向驱动程序询问设备的采样率,请使用
int32 DllExport __CFUNC DAQmxGetSampClkRate(TaskHandle taskHandle, float64 *data);理解应用程序吞吐量
虽然硬件总是以恒定的速率采样数据,但仍然需要将其传输到应用程序的内存中。这个速率并不总是匹配的--有时更慢,有时更快,这取决于其他操作系统任务和应用程序接收多少CPU。
重要的是,平均应用程序吞吐量与硬件的采样率相匹配。如果应用程序跟不上,那么硬件最终将填充其车载和驱动程序缓冲区,并返回一个错误。
调整应用程序吞吐量
NI-DAQmx有几种不同的方法来控制硬件如何和何时将数据传输到应用程序。我推荐的两种方法是:
有关更多细节,请参见帮助。
2.实现有效的控制回路
一旦您有了依赖于输入的输出,您就有了一个控制循环,并且正在实现一个控制系统。输入速度越快,计算出的新设定点越多,输出的新设定点越好,系统控制越好。
基线
操作系统是任何控制系统的基础。在高性能的频谱端是无操作系统系统,或“裸金属”系统,如and、微处理器和数字状态机。另一方面是先发制人的OSes,如Mac、Windows和桌面Linux.在中间,您可以找到实时操作系统,如QNX、VxWorks和RTLinux。
每个OSes都有不同的功能,对于像最大环路速率约为1 kHz这样的先发制人的最大环路速率约为1 kHz(一个输入、计算、输出周期在1ms内)。输入、计算和输出越复杂,最大速率越低。
操作系统完成后,I/O传输(如USB和以太网)、I/O驱动程序和I/O硬件本身开始降低最大环路速率。最后,结束程序增加了自己的延迟。
你的问题是问节目的事。您已经选择了操作系统和I/O,因此您受到它们的限制。
先发制人OSes上的调优控制环
在输入端,提高硬件采样率和减少采样数是提高读取速度的唯一途径。对于给定的抽样率,收集100个样本所需的时间少于收集200个样本所需的时间。计算新设置点所需的样本越少,循环速率就越快。
DAQmx有两种快速读取方法:
在输出端,NI运动驱动程序使用数据包从电机的嵌入式控制器发送和接收数据。API已经为高性能进行了调优,但是NI确实有一个描述最佳实践的白皮书。也许其中的一些适用于你的情况。
https://stackoverflow.com/questions/28193258
复制相似问题