2016-09-21 57 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

謝謝你讓我感到羞恥。花了整整一天的時間,試圖弄清楚這一點,仍然沒有任何結果。雖然公平地說,我今天遇到的很多東西都解釋了你所做的事情,而且我不知道其他情況 –

相關問題