範例一、最簡單校正程式 (方便安裝伺服馬達)
arduino程式:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | #include <Otto.h> Otto Otto; #define LeftLeg 2 // left leg pin, servo[0] #define RightLeg 3 // right leg pin, servo[1] #define LeftFoot 4 // left foot pin, servo[2] #define RightFoot 5 // right foot pin, servo[3] #define Buzzer 13 //buzzer pin void Otto_moveLegs(int T, int posLegL, int posLegR, int posFootL, int posFootR) { int posLegs[]={ posLegL,posLegR,posFootL,posFootR }; Otto._moveServos(T,posLegs); } void setup() { Otto.init(LeftLeg, RightLeg, LeftFoot, RightFoot, true, Buzzer); Otto.home(); Otto_moveLegs(200,90,90,90,90); Otto.home(); } void loop() { Otto_moveLegs(200,90,90,90,90); } |
以下是Gemini依照程式給的說明:
範例二程式碼是 Otto DIY 機器人的校準程式 (Calibration Sketch)。
它的主要用途是讓使用者透過電腦微調四個伺服馬達(舵機)的中心點,確保機器人的腿部和腳部完全垂直與水平。校準完成後,這些數值會儲存到機器人的記憶體(EEPROM)中,以後走路才不會歪掉。
以下是這段程式碼的詳細解說與操作步驟:
1. 硬體腳位配置
根據程式碼中的 #define,你的 Otto 機器人接線應如下:
左腿 (Left Leg): 數位腳位 2
右腿 (Right Leg): 數位腳位 3
左腳 (Left Foot): 數位腳位 4
右腳 (Right Foot): 數位腳位 5
蜂鳴器 (Buzzer): 數位腳位 13
2. 控制邏輯與指令
你可以透過 Arduino IDE 的 序列埠監控器 (Serial Monitor) 發送指令。請將鮑率 (Baud rate) 設定為 9600。
如何調整角度?
程式會接收一個數字加上一個字母。例如輸入 95a:
95是你設定的角度。a代表左腿。計算方式: 偏置值 (Trim) = 輸入值 - 90。所以
95a會產生+5度的偏差。
指令列表:
arduino程式:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 | #include <EEPROM.h> #include <Otto.h> Otto Otto; int i = 0; int v = 0; int positions[] = {90, 90, 90, 90}; int8_t trims[4] = {0,0,0,0}; #define LeftLeg 2 // left leg pin, servo[0] #define RightLeg 3 // right leg pin, servo[1] #define LeftFoot 4 // left foot pin, servo[2] #define RightFoot 5 // right foot pin, servo[3] #define Buzzer 13 //buzzer pin void readChar(char ch) { switch (ch) { case '0'...'9': v = (v * 10 + ch) - 48; break; case 'a': trims[0] = v-90; setTrims(); v = 0; break; case 'b': trims[1] = v-90; setTrims(); v = 0; break; case 'c': trims[2] = v-90; setTrims(); v = 0; break; case 'd': trims[3] = v-90; setTrims(); v = 0; break; case 'w': for (int count=0 ; count<4 ; count++) { Otto.walk(1,1000,1); // FORWARD } break; case 's': for (i = 0; i <= 3; i++) { EEPROM.write(i,trims[i]); } delay(500); Otto.sing(S_superHappy); Otto.crusaito(1, 1000, 25, -1); Otto.crusaito(1, 1000, 25, 1); Otto.sing(S_happy_short); break; } } void setTrims() { Otto.setTrims(trims[0],trims[1],trims[2],trims[3]); Otto._moveServos(10, positions);} void setup() { Otto.init(LeftLeg, RightLeg, LeftFoot, RightFoot, true, Buzzer); Otto.home(); Serial.begin(9600); // 1. Upload this code to your robot, wait until is successful // 2. Unplug the USB connection and close Otto Blockly. // 3. Download, Unzip and Run the calibration app // 4. Plug the USB again into Otto (the red cross will turn into a green tick) // 5. Now adjust the servo positions so the the legs and feet are correctly aligned in the robot on your table. // 6. When the servos on Otto are correctly aligned, click on 'Walk Test' to see how Otto moves. If does not wlak straight, iterate step 5 // 7. Once you are happy with the calibration, click 'Save'. v = 0; } void loop() { if (Serial.available()) { readChar((Serial.read())); } } |
沒有留言:
張貼留言