繁体   English   中英

功能指针指向指针的功能

[英]Function Pointer to function of a pointer

我目前正在使用自定义的气动机器人手臂,正在尝试设计用于不同任务的队列。 在设计队列的过程中,我想出了让队列存储函数指针和一个传递给该函数的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
}

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

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

您的Queue应该是:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM