【问题标题】:A class member function calling from other class member function (arduino)从其他类成员函数(arduino)调用的类成员函数
【发布时间】:2020-12-04 13:11:12
【问题描述】:

我正在开发与arduino 接口的nema stepper motor 驱动程序。我创建了一个类。我将其命名为Axis。我想用这个类为每个电机创建一个轴对象。但是,我不能从类中调用arduino.h 中的attachInterrupt 函数。它返回此错误:

In member function 'void Axis::setReverseInt()':
parsstep.cpp:12:77: error: invalid use of non-static member function 'void Axis::revDirection()'
   attachInterrupt(digitalPinToInterrupt(reverseDirPin), revDirection, RISING);
Axis::Axis()
{

}
Axis::~Axis()
{

}

void  Axis::setReverseInt () {
  attachInterrupt(digitalPinToInterrupt(reverseDirPin), revDirection, RISING);
}
void  Axis::setStopInt () {
  //attachInterrupt(digitalPinToInterrupt(stopPin), stopMotor, RISING);
}
void Axis::revDirection()
{
  dirMode = !dirMode;
}
void Axis::stopMotor()
{
  moveMotor = !moveMotor;
}
void Axis::startMotor()
{
  moveMotor = true;
}
void Axis::moveStep(int pulseRev, byte laps, boolean dir)
{
  digitalWrite(dirPin, dir);
  int totalPulse = pulseRev * laps;
  for (int i = 0; i < totalPulse; i++)
  {
    speedValue = map((analogRead(speedPin)), 0, 1023, 2000, 10);
    digitalWrite(pulsePin, HIGH);
    delayMicroseconds(speedValue);
    digitalWrite(pulsePin, LOW);
    delayMicroseconds(speedValue);
  }
}

【问题讨论】:

    标签: function class arduino static member


    【解决方案1】:

    在为我一直从事的大型项目编写自己的库时,我遇到了类似的问题。在方法内部配置中断时发生在我身上。我找到了一种解决方法,虽然它看起来有点复杂,但它确实可以解决问题。就我而言,我用它来读取旋转编码器的开关。

    我使用的是一个指向 ISR 处理程序的指针和一个调用它的方法。在您的设置函数中,您可以调用 ISR 服务的初始化。

    首先,在您的 .h 文件中声明以下方法(除了您自己的方法):

    void init();
    
    static void ISRHandler();
    
    void setupISRHandler(uint8_t pin, void (*ISR)(void), int state); // this will configure your ISR
    

    还有一个指针:

    static Axis *_ISRPointer; // can be declared as private (like in here from the '_' prefix)
    

    然后,在您的 .cpp 文件中,您可以像这样使用它:

    #include "Axis.h"
    
    Axis *Axis::_ISRPointer = nullptr;
    

    您将在设置期间使用 init 方法初始化 ISR 处理程序:

    void Axis::init(){
    
        setupISRHandler(reverseDirPin, Axis::ISRHandler, RISING);
    
    }
    

    设置方法如下:

    void Axis::setupISRHandler(uint8_t pin, void (*ISRFunction)(void), int state){
    
        attachInterrupt(digitalPinToInterrupt(pin), ISRFunction, state);
    
    }
    

    ISRHandler 负责调用 ISR 例程:

    void Axis::ISRHandler(){
    
        _ISRPointer-> revDirection();
    
    }
    
    void Axis::revDirection(void){
    
        // Do not forget to disable interrupts
        cli();
    
        // your routine goes here
        dirMode= !dirMode;
    
        sei();
    }
    

    因此,您将在主 .ino 文件中执行以下操作:

    #include "Axis.h"
    #include <whateverotherlibraryyouneed.h>
    
    Axis ax = Axis(); // your new object    
    
    void setup(){
    
        ax.init();
        ax.startMotor();
    
    }
    
    void loop(){
    
        if(day == SUNNY){
            ax.moveStep(100, 2 , CLOCKWISE);
        } else {
            ax.stopMotor();
        }
    
        ax.someOtherMethodToControlTheMotor(anyArgumentYouWant);
    
    }
    

    我希望我复杂的解决方案有所帮助

    干杯

    博士。丹尼尔

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-11-29
      • 1970-01-01
      • 2018-11-24
      • 1970-01-01
      • 1970-01-01
      • 2013-12-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多