首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Arduino:如果在构造函数中使用了->object.Attach(Pin),则带有Servo的类会破坏伺服行为

Arduino:如果在构造函数中使用了->object.Attach(Pin),则带有Servo的类会破坏伺服行为
EN

Stack Overflow用户
提问于 2015-01-05 03:23:29
回答 1查看 356关注 0票数 1

我有一个解决方案的问题,但我不明白解决方案。它可能与Arduino: initialise custom object in constructor类似,但我不认为这是同一个问题。

上下文如下。我正在用Arduino板控制一个小腿机器人。每条腿都是一个包含两个伺服的对象"MChLeg“。伺服系统是通过库来控制的。

问题是这样的:如果我在构造函数中分配伺服,程序编译得很好,但是伺服的行为很疯狂(我想我破坏了伺服调度);如果我在构造函数之外分配伺服,一切都是正常的。

我在下面只列出了C++类的一部分,因为它是唯一一个在工作版本和非工作版本之间进行更改的类。

初始程序(在构造函数中分配伺服),代码有问题:

代码语言:javascript
复制
#include <Arduino.h>
#include <Servo.h>

class MChLeg{
    int kneeIndex, hipIndex;
    int kneeAngle, hipAngle;
    Servo kneeServo; 
    Servo hipServo;
public:
    MChLeg(int, int); // (knee servo index, hip servo index); use 0 if no servo
    ~MChLeg();
    void setLeg(int, int); // (knee angle, hip angle); in degrees, 0-180, 90 is straight
    void rollLeg();
};

MChLeg::MChLeg(int x, int y){
    this->kneeIndex = x;
    this->hipIndex = y;
    if (this->kneeIndex != 0) {
        this->kneeServo.attach(this->kneeIndex);
        this->kneeServo.write(90);   // this line can be removed with no impact on issue
    }
    if (this->hipIndex != 0) {
        this->hipServo.attach(this->hipIndex);
        this->hipServo.write(90);    // this line can be removed with no impact on issue
    }
}

新代码(使用方法分配伺服),代码运行正常:

代码语言:javascript
复制
#include <Arduino.h>
#include <Servo.h>

class MChLeg{
    int kneeIndex, hipIndex;
    int kneeAngle, hipAngle;
    Servo kneeServo; 
    Servo hipServo;
public:
    MChLeg();
    ~MChLeg();
    void assignLeg(int, int); // (knee servo index, hip servo index); use 0 if no servo
    void setLeg(int, int); // (knee angle, hip angle); in degrees, 0-180, 90 is straight
    void rollLeg();
};

MChLeg::MChLeg(){
    this->kneeIndex = 0;
    this->hipIndex = 0;
}

void MChLeg::assignLeg(int x, int y){
    if (this->kneeIndex != 0) {
        this->kneeServo.detach();
    }
    if (this->hipIndex != 0) {
        this->hipServo.detach();
    }
    this->kneeIndex = x;
    this->hipIndex = y;
    if (this->kneeIndex != 0) {
        this->kneeServo.attach(this->kneeIndex);
    }
    if (this->hipIndex != 0) {
        this->hipServo.attach(this->hipIndex);
    }
}

为什么第二个代码会比第一个更好?

调用代码:

代码语言:javascript
复制
// Include application, user and local libraries
#include <Servo.h>
#include "MChButton.h"
#include "MChLeg.h"


// pin connections
const int buttonIndex = 2; //pin for button
const int greenLed = 3;//pin for green Led
const int yellowLed = 4;//pin for yellow Led
const int redLed = 5;//pin for red Led
const int servoPin1 = 9;//pin for servo
const int servoPin2 = 10;//pin for servo
const int servoPin3 = 11;//pin for servo


//define persitent objects
MChButton theButton(buttonIndex); // declare the switch pin as an input
MChLeg rightLeg; // declare right leg - was 'MChLeg rightLeg(0, servoPin1);' in first version
MChLeg leftLeg;  // declare left leg - was 'MChLeg leftLeg(servoPin2, servoPin3);' in first version


// Setup phase
void setup(){

    // declare the LED pins as outputs
    pinMode(greenLed,OUTPUT);
    pinMode(yellowLed,OUTPUT);
    pinMode(redLed,OUTPUT);

    leftLeg.assignLeg(0, servoPin1); //right knee - hip - did not exist in first version
    rightLeg.assignLeg(servoPin2, servoPin3); //right knee - hip - did not exist in first version

}

// Loop phase
void loop(){

    //state machine counter (persistant)
    static int machinestate = 0;
    const int machinestateMax = 4; //max statemachine
    const int machinestateMax1 = machinestateMax+1; //max statemachine for modulo counting

    //check if there is an event, if there is one increment state machine counter and mark event done
    //statemachine counter is limited for machinestateMac (modulus)
    if (theButton.buttonEventGet()==true) {
        machinestate = ++machinestate % machinestateMax1;
    }

    // set LEDs and Legs according to state
    switch (machinestate) {
        case 0:{
            digitalWrite(greenLed, HIGH); // turn the green LED on pin 3 on
            digitalWrite(yellowLed, LOW);  // turn the red LED on pin 4 off
            digitalWrite(redLed, LOW);  // turn the red LED on pin 5 off
            leftLeg.setLeg(10, 10); // set leg to 0°
            rightLeg.setLeg(10, 10); //set leg to 0°
            break;
        }
        case 1:{
            digitalWrite(greenLed, HIGH);
            digitalWrite(yellowLed, HIGH);
            digitalWrite(redLed, LOW);
            leftLeg.setLeg(45, 45);
            rightLeg.setLeg(45, 45);
            break;
        }
        default:{ // detect if I did it wrong :-)
            digitalWrite(greenLed, HIGH); 
            digitalWrite(yellowLed, HIGH); 
            digitalWrite(redLed, HIGH);  
        }
    }
    delay(10);
}

谢谢你的帮助。

EN

回答 1

Stack Overflow用户

发布于 2015-01-05 19:28:25

我现在没有带Arduino,但我记得我遇到了同样的问题。

就我所记得的,我在我的构造函数中使用了一个for loop来处理一个不起作用的数组。所以我结束了一个接一个的赋值。

我假设你的情况是if statement的问题。

今天晚上回家后,我会尝试不同的东西。

编辑- 2015/01/08

我做了一些测试,试图重现我所讨论的问题,但没有成功。我将保留我的答案,因为解决方案可能是而不是

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27769400

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档