Arduino Balance库错误

时间:2016-12-22 09:15:50

标签: c++ arduino

我一直在尝试从Github编译和修复这个库,这将允许某人创建自己的规模;但是当我尝试为arduino编译它时,我得到了这个错误。

  

Arduino:1.6.13(Windows 10),Board:" Arduino Nano,ATmega328"

     

C:\用户\用户\文件\ GitHub的\ Q2-平衡 - Arduino的库\例子\ hx711_two_cell \ hx711_two_cell.ino:   在函数' void loop()':

     

hx711_two_cell:40:错误:没有匹配的呼叫功能   ' Q2Balance ::皮重(INT)'

 balance.tare(100);

                 ^
     

C:\用户\用户\文件\ GitHub的\ Q2-平衡 - Arduino的库\例子\ hx711_two_cell \ hx711_two_cell.ino:40:21:   注意:候选人是:

     

包含来自的文件   C:\用户\用户\文件\ GitHub的\ Q2-平衡 - Arduino的库\例子\ hx711_two_cell \ hx711_two_cell.ino:1:0:

     

C:\用户\用户\文件\ Arduino的\库\ Q2-平衡 - Arduino的库主\ SRC / Q2Balance.h:64:10:   注意:void Q2Balance :: tare(long int,void(*)())

 void tare(long milliseconds, void (*afterTared)(void));

我很困惑为什么需要第二个无效参数(右,回调函数)

我不确定我需要对此函数进行哪些更改才能进行编译 这是.h文件:

#ifndef q2balance_h
#define q2balance_h
#include "Arduino.h"

#define Q2BALANCE_MARKER_COUNT 10
// #define Q2BALANCE_DEBUG

#define Q2BALANCE_UNIT_GRAM 0
#define Q2BALANCE_UNIT_POUND 1
#define Q2BALANCE_UNIT_OUNCE 2
#define Q2BALANCE_UNIT_GRAIN 3
#define Q2BALANCE_UNIT_TROY 4
#define Q2BALANCE_UNIT_PWT 5
#define Q2BALANCE_UNIT_CARAT 6
#define Q2BALANCE_UNIT_NEWTON 7

struct BalanceCalibrationStruct {
  long calibrationZero;
  long calibrationMV[Q2BALANCE_MARKER_COUNT];
  long calibrationMeasured[Q2BALANCE_MARKER_COUNT];
  float calibrationScaler[Q2BALANCE_MARKER_COUNT];
};

class Q2Balance
{
  private:
    BalanceCalibrationStruct _settings;
    bool _tared = false;
    bool _taring = false;
    bool _calibrating = false;
    bool _calibratingZero = false;
    bool _settling = false;
    long _smoothValue = 0;
    long _rawValue = 0;
    long _tareValue = 0;
    unsigned long _settleTimeout;
    long _settleMinVal = 0;
    long _settleMaxVal = 0;
    long _jitter = 0;
    int _calibrationIndex;
    void (*_afterCalibrated)(void);
    void sortCalibrations();
    int findCalibrationWindow(long voltage);
    float calcValue(int units, long value);
  public:
    long TARELIMIT = 110;
    long JUMPLIMIT = 200;
    long SAMPLE_COUNT = 10;
    bool LOGGING = false;
    Q2Balance();
    virtual ~Q2Balance();
    bool taring();
    bool tared();
    bool settling();
    bool calibrating();
    float adjustedValue(int units);
    float adjustedRawValue(int units);
    long smoothValue();
    long rawValue();
    long jitter();
    void calibrateZero(long milliseconds, void (*afterCalibrated)(void));
    void calibrate(int index, long measurement, long milliseconds, void (*afterCalibrated)(void));
    void measure(long measurement);
    void tare(long milliseconds, void (*afterTared)(void));
    long settle(long milliseconds);
    BalanceCalibrationStruct getCalibration();
    void setCalibration(BalanceCalibrationStruct newSettings);
    void tick();
    void printCalibration(int index);
    void printCalibrations();
};

#endif /* q2balance_h */

和cpp文件

#include "q2balance.h"

Q2Balance::Q2Balance(){
  _settings.calibrationZero = 0;
  for(int i = 0;i<Q2BALANCE_MARKER_COUNT;i++)
  {
    _settings.calibrationMV[i]= 0;
    _settings.calibrationMeasured[i] = 0;
    _settings.calibrationScaler[i] = 0;
  }
}

Q2Balance::~Q2Balance(){
}

BalanceCalibrationStruct Q2Balance::getCalibration(){
  BalanceCalibrationStruct newSettings;
  newSettings.calibrationZero = _settings.calibrationZero;
  for(int i = 0;i<Q2BALANCE_MARKER_COUNT;i++)
  {
    newSettings.calibrationMV[i] = _settings.calibrationMV[i];
    newSettings.calibrationMeasured[i] = _settings.calibrationMeasured[i];
    newSettings.calibrationScaler[i] = _settings.calibrationScaler[i];
  }
  return newSettings;
}

void Q2Balance::setCalibration(BalanceCalibrationStruct newSettings){
  _settings.calibrationZero = newSettings.calibrationZero;
  for(int i = 0;i<10;i++)
  {
    _settings.calibrationMV[i] = newSettings.calibrationMV[i];
    _settings.calibrationMeasured[i] = newSettings.calibrationMeasured[i];
    _settings.calibrationScaler[i] = newSettings.calibrationScaler[i];
  }
}

void Q2Balance::tick(){
  unsigned long now = millis();
  if (_settling){
    if (_smoothValue < _settleMinVal){
      _settleMinVal = _smoothValue;
    }
    if (_smoothValue > _settleMaxVal){
      _settleMaxVal = _smoothValue;
    }
    _jitter = _settleMaxVal - _settleMinVal;
    if (now > _settleTimeout){
      _settling = false;
    } else {
      return;
    }
  }

  if(_taring){
    _taring = false;
    _tared = true;
    _smoothValue = _rawValue;
    _tareValue = _rawValue;
    if (_afterCalibrated!=NULL){
      (*_afterCalibrated)();
      _afterCalibrated = NULL;
    }
  }

  if (_tared && abs(_smoothValue - _tareValue) > TARELIMIT){
    _tared = false;
  }

  if (_calibratingZero) {
    _settings.calibrationZero = _smoothValue;

    for(int i = 0;i<Q2BALANCE_MARKER_COUNT;i++) // Recalc calibrations if zero changed
    {
      if(_settings.calibrationScaler[i] != 0){
        float delta = (float)(_settings.calibrationMV[i] - _settings.calibrationZero);
        float scaler = (float)_settings.calibrationMeasured[i] / delta;
        _settings.calibrationScaler[i] =  scaler;
      }
    }

    _calibratingZero = false;
    _calibrating = false;

    if (_afterCalibrated!=NULL){
      (*_afterCalibrated)();
      _afterCalibrated = NULL;
    }

  }

  if (_calibrating) {
    _calibrating = false;
    _settings.calibrationMV[_calibrationIndex] = _smoothValue;
    float delta = (float)(_smoothValue - _settings.calibrationZero);
    float scaler = (float)_settings.calibrationMeasured[_calibrationIndex] / delta;

    _settings.calibrationScaler[_calibrationIndex] =  scaler;

    if (_afterCalibrated!=NULL){
      (*_afterCalibrated)();
      _afterCalibrated = NULL;
    }
  }
}

long Q2Balance::settle(long settleTime) {
  _settling = true;
  _settleMinVal = _smoothValue;
  _settleMaxVal = _smoothValue;
  _settleTimeout = millis() + settleTime;
}

bool Q2Balance::settling(){
  return _settling;
};

bool Q2Balance::calibrating(){
  return _calibrating;
};

void Q2Balance::tare(long settleTime, void (*afterTared)(void)){
  if (!_calibrating && !_settling){
    _taring = true;
    _afterCalibrated = afterTared;
    settle(settleTime);
  }
}

bool Q2Balance::taring(){
  return _taring;
};

bool Q2Balance::tared(){
  return _tared;
};

void Q2Balance::calibrateZero(long settleTime, void (*afterCalibrated)(void)){
  if (!_calibrating && !_settling){
    _afterCalibrated = afterCalibrated;
    _calibratingZero = true;
    _calibrating = true;
    settle(settleTime);
  }
}

void Q2Balance::calibrate(int index, long measurement, long settleTime, void (*afterCalibrated)(void)){
  if (!_calibrating && !_settling){
    if (index < Q2BALANCE_MARKER_COUNT){
      _afterCalibrated = afterCalibrated;
      _calibrating = true;
      _calibrationIndex = index;
      _settings.calibrationMeasured[index] = measurement;
      settle(settleTime);
    }
  }
}

void Q2Balance::measure(long measurement){
  long avg = _smoothValue;
  _rawValue = measurement;
  if (abs(measurement-_smoothValue) > JUMPLIMIT){  // If there is a large jump, abandon smoothing and set the value.
    _smoothValue = measurement;
    return;
  }
  avg -= avg / SAMPLE_COUNT;
  avg += measurement / SAMPLE_COUNT;
  _smoothValue = avg;
};

long Q2Balance::smoothValue(){
  return _smoothValue;
};

long Q2Balance::rawValue(){
  return _rawValue;
};

long Q2Balance::jitter(){
  return _jitter;
}

float Q2Balance::calcValue(int units, long value){
  if(_settings.calibrationZero == 0){
    return 0; //unzeroed
  }

  int index = findCalibrationWindow(_smoothValue);

  if (index == -1){
    return 0; //uncalibrated
  }
  long val = _smoothValue - _tareValue;
  if (_tared){
    return (0);
  }

  float scaled = round(val * _settings.calibrationScaler[index] * 100.0)/100.0;

  if (units > 0){

    switch (units) {
    case Q2BALANCE_UNIT_POUND:
      scaled = scaled * 0.002204622;
      break;
    case Q2BALANCE_UNIT_OUNCE:
      scaled = scaled * 0.0352734;
      break;
    case Q2BALANCE_UNIT_GRAIN:
      scaled = scaled * 15.43;
      break;
    case Q2BALANCE_UNIT_TROY:
      scaled = scaled * 0.032;
      break;
    case Q2BALANCE_UNIT_PWT:
      scaled = scaled * 0.643;
      break;
    case Q2BALANCE_UNIT_CARAT:
      scaled = scaled * 5;
      break;
    case Q2BALANCE_UNIT_NEWTON:
      scaled = scaled * 0.009;
      break;
    }
  }

  return scaled;

}

float Q2Balance::adjustedValue(int units){
  return calcValue(_smoothValue, units);
}

float Q2Balance::adjustedRawValue(int units){
  return calcValue(_rawValue, units);
}

void Q2Balance::printCalibrations(){
  char buffer[128];
  sprintf(buffer, "ZERO %ld ",_settings.calibrationZero);
  Serial.println(buffer);
  for(int c=0;c<Q2BALANCE_MARKER_COUNT;c++){
    printCalibration(c);
  }
}

void Q2Balance::printCalibration(int index){
  char buffer[128];
  char str_scaler[16];
  dtostrf(_settings.calibrationScaler[index], 6, 6, str_scaler);
  sprintf(buffer, "IDX %dMV %ld M %ld SC %s",
    index,
    _settings.calibrationMV[index],
    _settings.calibrationMeasured[index],
    str_scaler
  );
  Serial.println(buffer);
}

int Q2Balance::findCalibrationWindow(long voltage){
  int i,t=0;
  for (i = 0; i < Q2BALANCE_MARKER_COUNT; i++)
  {
    if (_settings.calibrationMV[i] == 0){
      break;
    }
    if (_settings.calibrationMV[i] >= voltage){
      break;
    }
    t = i;
  }
  if (_settings.calibrationMV[t] == 0){
    return -1;
  }
  return t;
}

1 个答案:

答案 0 :(得分:0)

当库函数balance.tare完成时,它将调用您自己的函数(又名回调函数),您可以在该附加输入参数中为函数balance.tare提供函数。 / p>

你可以把这个回调机制概念想象成“当你完成时给我打电话”。

通常有两种方法来实现回调机制:

静态链接:

函数balance.tare调用具有预定义(硬编码)名称的函数。

您必须在代码中实现该名称的功能。

由于函数调用和函数本身必然位于不同的源文件中,编译器必须假设这样的函数存在于代码中的某个位置,并将地址解析留给链接器(最终将其替换为跳转到你的功能地址。)

动态链接:

函数balance.tare将函数指针作为输入,并通过指针调用该函数(即,如果需要则推送输入参数并跳转到指向函数)。

根本没有符号解析 - 无论是在编译期间还是在链接期间(您可以说函数调用在运行时被“解析”)。

这允许您在每次调用函数时让函数balance.tare调用不同的函数。

唯一的缺点可能是输入参数的附加内存读取操作,它存储函数的地址,这是跳转到该函数所必需的。

这不是跳到已知的(常数)地址。

但它确实依赖于平台,即某些硬件架构可以在单个指令中执行此操作(就像任何其他函数调用一样)。

在您的情况下,图书馆所有者选择了后者。

用法示例:

void MyAfterTaredCallbackFunction(void)
{
    // Do some stuff
}

int main()
{
    ...
    balance.tare(100, MyAfterTaredCallbackFunction);
    ...
}