2016-09-21 59 views
0

嗨,所以即时通讯使机器人步行者的层次结构排序,使舵机的数量可管理,即时尝试创建一个包含多个伺服类的Limb类(是的,即时通讯使用内置的伺服库,但我也想调整偏移量,校准值的比例等)无论如何听到的是我的代码。Arduino启动一个包含一个类的数组的类

问题是肢体类引发剂(下4行)我通常不喜欢只是直线上升询问正确的代码行,喜欢弄明白,但四尝试一切我能想到的

PS我apolagise任何垃圾拼写和感谢

class ServoConfig{ 
    public : 
    float Offset; 
    float Scale; 
    bool Inversed; 

    Servo ServoV; 

    ServoConfig (float InOffset, float InScale, bool InInversed,int Pin){ 
     float Offset = InOffset; 
     float Scale = InScale; 
     bool Inversed = InInversed; 
     ServoV.attach(Pin); 
    } 

    void WriteToServo(float Angle){ 
    if (Inversed){ 
     ServoV.write((180-Angle)/Scale + Offset); 
    } 
    else{ 
     ServoV.write(Angle/Scale + Offset); 
    } 
    } 
}; 

class Limb{ 

    ServoConfig Servos[]; 

    Limb (ServoConfig InServos[]){ 
    ServoConfig Servos[] = InServos; 
    } 
}; 

回答

0

它不是那么容易在C + +和在Arduino更难。

但是首先你在代码中有大量的错误。例如阴影类属性通过局部变量:

ServoConfig (float InOffset, float InScale, bool InInversed,int Pin) { 
    float Offset = InOffset; // this will create new local variable and hides that one in class with the same name 
    float Scale = InScale;  // this too 
    bool Inversed = InInversed; // and this too 

同样在Limb构造函数中,但它不起作用。

它是如何工作的?你可以使用这样的事情:

#include <Servo.h> 

class ServoConfig { 
    public : 
    float Offset; 
    float Scale; 
    bool Inversed; 

    Servo ServoV; 

    ServoConfig (float InOffset, float InScale, bool InInversed,int Pin) 
     : Offset { InOffset } 
     , Scale { InScale } 
     , Inversed { InInversed } 
    { 
     ServoV.attach(Pin); // it might have to be attached in setup() as constructors are called before main() 
    } 

    void WriteToServo(float Angle){ 
     if (Inversed) { 
     ServoV.write((180-Angle)/Scale + Offset); 
     } else { 
     ServoV.write(Angle/Scale + Offset); 
     } 
    } 
}; 

ServoConfig servos[] = {{10,10,0,9},{0,1,0,10}}; // this makes servos of size two and calls constructors 

class Limb { 
    public: 
    ServoConfig * servos_begin; 
    ServoConfig * servos_end; 

    Limb(ServoConfig * beg, ServoConfig * end) 
     : servos_begin { beg } 
     , servos_end { end } 
    {;} 

    ServoConfig * begin() { return servos_begin;    } 
    ServoConfig * end() { return servos_end;    } 
    size_t   size() { return servos_end - servos_begin; } 
}; 

// much better than using sizeof(array)/sizeof(array[0]): 
template <class T, size_t size> size_t arraySize(T(&)[size]) { return size; } 

// create and construct Limb instance: 
Limb limb { servos, servos + arraySize(servos)}; 



void setup(){ 
    Serial.begin(9600); 
} 

void loop(){ 
    Serial.println("starting..."); 

    for (ServoConfig & servo : limb) { // this uses begin() and end() methods 
    servo.WriteToServo(90); 
    } 

    // or directly with array[] 
    for (ServoConfig & servo : servos) { 
    servo.WriteToServo(40); 
    } 
} 
+0

谢谢你让我感到羞耻。花了整整一天的时间,试图弄清楚这一点,仍然没有任何结果。虽然公平地说,我今天遇到的很多东西都解释了你所做的事情,而且我不知道其他情况 –

相关问题