功能指针指针功能的指针

时间:2014-03-12 09:28:39

标签: c++ arduino function-pointers

我目前正在使用定制气动机器人手臂,并试图为不同的任务设计队列。在设计队列的过程中,我提出了使用队列存储函数指针和一个int传递给函数的逻辑,队列将继续尝试在队列中运行该函数,直到它返回true。

在执行基本功能时,队列可以工作,但在尝试以下列方式实现时,它会失败:

  functionQueue.push(_lowerArm->waitForHoldingPosition, 100); // Lower Arm 100%

错误讯息:

Arm.h:180: error: no matching function for call to 'Queue<bool (*)(int), 50>::push(<unresolved overloaded function type>, int)'
/Queue.h:201: note: candidates are: bool Queue<T, queueMaxSize>::push(T, int) [with T = bool (*)(int), int queueMaxSize = 50]

为了解决这个问题我试图引用_lowerArm->waitForHoldingPosition,但它并没有成功。如果有人能指出我正确的方向,我将非常感激。

本守则的其余部分:

AirCylinder lowerArm(4, 13, 7, 1, false); //Green, White
AirCylinder upperArm(10, 11, 6, 2, true); //Yellow, Orange
AirCylinder wrist(8, 9, 5, 3, true); //Red, Blue
Arm arm(&lowerArm, &upperArm, &wrist, SERVO_PIN);

loop() { /* Do Core Stuff Here */

Arm.h

#include <Arduino.h>
#include "Queue.h"

#ifndef ARM_H
#define ARM_H

#define QUEUE_SIZE 50

class Arm {
  private:
    AirCylinder* _lowerArm;
    AirCylinder* _upperArm;
    AirCylinder* _wrist;
    byte _servoPin;
    byte _servoPosition;
    byte _currentRoutine;
    boolean _doingRoutine;
    Queue<bool(*)(int),QUEUE_SIZE> functionQueue;

    void _setFingers(byte);
    void _doRoutine();
    void _pickUpCone();
    void _scoreConeLow();
    void _scoreConeHigh();
    void _scoreConeeSideways();
    void _storeCone();
    void _rest();
    void _stabilize();


  public:
    Arm(AirCylinder *lowerArm, AirCylinder *upperArm, AirCylinder *wrist, byte servoPin);
    void stabilize();
    void openFingers()             {  _setFingers(Arm::SERVO_OPEN);   }
    void closeFingers()            {  _setFingers(Arm::SERVO_CLOSE);  }
    void setFingers(byte servoPos) {  _setFingers(servoPos);    }
    void doRoutine();
    void doRoutine(byte);
    void calibrateRoutine(byte);

    static const byte SERVO_OPEN = 0;
    static const byte SERVO_CLOSE = 180;

    static const byte PICKUP_CONE = 0;
    static const byte SCORE_CONE_LOW = 1;
    static const byte SCORE_CONE_HIGH = 2;
    static const byte SCORE_CONE_SIDEWAYS = 3;
    static const byte STORE_CONE = 4;
    static const byte RESTING = 5;

};

Arm::Arm(AirCylinder *lowerArm, AirCylinder *upperArm, AirCylinder *wrist, byte servoPin) {
  // Set a reference to all the arm cylinders
  _lowerArm = lowerArm;
  _upperArm = upperArm;
  _wrist = wrist;

  // Set the pin the servos are on
  _servoPin = servoPin;

  _doingRoutine = false;
}

void Arm::_scoreConeLow() {
  // Grab cone from bot
  // Lower Arm - 100%
  // Upper Arm - 50%
  // Wrist - 20%
  // Lower Arm - 80%

  // Put cone down
  // Lower Arm - 100%
  // Upper Arm - 75%
  // Wrist - 0%
  // Lower Arm - 70%
  // Upper Arm - 60%
  // Lower Arm - 40%

  functionQueue.push(_upperArm->waitForHoldingPosition, 30); // Upper Arm 50%
  functionQueue.push(_wrist->waitForHoldingPosition, 20); // Wrist 20%
  functionQueue.push(_lowerArm->waitForHoldingPosition, 80); // Lower Arm 80%

  functionQueue.push(_lowerArm->waitForHoldingPosition, 100); // Lower Arm 100%
  functionQueue.push(_upperArm->waitForHoldingPosition, 75); // Upper Arm 75%
  functionQueue.push(_wrist->waitForHoldingPosition, 0); // Wrist 0%
  functionQueue.push(_lowerArm->waitForHoldingPosition, 70); // Lower Arm 70%
  functionQueue.push(_upperArm->waitForHoldingPosition, 60); // Upper Arm 60%
  functionQueue.push(_lowerArm->waitForHoldingPosition, 40); // Lower Arm 40%
}

AirCylinder.h

class AirCylinder {
  private:
   // Bunch of methods not used in this example

  public:
    /* Constructors */
    AirCylinder();
    AirCylinder(byte retractPin, byte extendPin, byte extendPressureHolderPin, byte potPin);
    AirCylinder(byte retractPin, byte extendPin, byte extendPressureHolderPin, byte potPin, boolean isInverse);

    /* Setters */

    /* Getters */

    /* Functions */
    bool waitForHoldingPosition(int position);
};

/**
  * waitForHoldingPosition
  *
  * Attempts to stablize the Air Cylider at the current Holding Position
  * function returns true when the position is met
  *
  */
bool AirCylinder::waitForHoldingPosition(int position) {
  setHoldingPosition(position);
  if(abs(getCurrentPosition() - position) > THRESHOLD_PERCENT) {
    // Cylinder is not within the threshold
    return false;
  }
  return true; // Cylinder is within the threshold
}

1 个答案:

答案 0 :(得分:1)

您的waitForHoldingPosition类型为bool (AirCylinder::*)(int)

但您的Queue模板需要bool (*)(int)。它们不一样。

您的Queue应该是:

Queue<bool(AirCylinder::*)(int), QUEUE_SIZE> functionQueue;