gpt4 book ai didi

c++ - 指向指针函数的函数指针

转载 作者:行者123 更新时间:2023-11-28 07:01:46 26 4
gpt4 key购买 nike

我目前正在使用定制的气动机器人 ARM ,并试图为不同的任务设计一个队列。在设计队列的过程中,我想到了让队列存储函数指针和传递给函数的 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%
}

气缸.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
}

最佳答案

您的waitForHoldingPositionbool (AirCylinder::*)(int) 类型。

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

您的队列应该是:

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

关于c++ - 指向指针函数的函数指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22347074/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com