我有一個Arduino Mega連接到6軸機器人手臂。所有6箇中斷都連接到編碼器(中斷上一個編碼器引腳,另一個在香草數字輸入上)。中斷與此代碼處理:Arduino編碼器中斷破壞串行數據
void readEncoder1(){
//encoders is a 2d array, where the first d is the axis, and the two pin numbers
//first pin is on an interrupt (CHANGE), and second is a standard digital in
if (digitalRead(encoders[0][0]) == digitalRead(encoders[0][1])) {
positions[0]++;
} else {
positions[0]--;
}
if(servoEnable){
updatePositions(); //// compares positions[] to targets[] and adjusts motor speed accordingly
}
}
這被設計成保持在特定的位置 - 鎖定臂如果Arduino的檢測到電機的位置是關閉的一定閾值時,它更新電源去到電機保持手臂就位。如果兩個或三個(或更多)軸處於負載狀態(需要不斷更新以保持位置),或者它們正在移動,則Arduino將停止接收串行輸入上的完整命令,其中幾個字符將被丟棄。中斷顯然運行得非常快,並且由於某種原因,這導致命令被破壞。有沒有辦法解決?在架構上,我是這樣做的嗎?我的主要目的是在主運行循環中以100毫秒的間隔調用updatePositions(),這是否會顯着減少中斷開銷?我想我的問題歸結爲,即使所有6個編碼器都在脈動,我如何才能在Arduino中獲得可靠的串行命令?