Robofun 機器人論壇
標題:
程式問題請教
[打印本頁]
作者:
ypyp3399
時間:
2013-12-11 09:39
標題:
程式問題請教
您好
我使用Arduino DUE來驅動馬達,我採用5個感測器,有32種狀態,可是馬達運動卻不是我程式所設定的狀態,舉例case 0(感測器狀態皆為0),馬達程式預設皆停止,實際運行時馬達卻全速運行。
想請問我的程式是否有問題。
const int IRPeople =10; //紅外線感測器前方避障
const int SLeft_1 = 5; //左邊方向紅外線二訊號輸入
const int SLeft = 6; //左邊方向紅外線一訊號輸入
const int SMiddle = 7; //中間方向紅外線訊號輸入
const int SRight = 11; //右邊方向紅外線一訊號輸入
const int SRight_1 = 12; //右邊方向紅外線二訊號輸入
const int PeopleLED = 13; //紅外線感測前方避障狀態LED
const int Motor_Left = 9;
const int Motor_Right = 8;
const int Motor_M1 = DAC0; //右邊
const int Motor_M2 = DAC1; //左邊
byte byteSensorStatus = 0;
void setup(){
Serial.begin(9600);
pinMode(IRPeople,INPUT); //紅外線感測器前方避障
pinMode(SLeft_1,INPUT);
pinMode(SLeft,INPUT);
pinMode(SMiddle,INPUT);
pinMode(SRight,INPUT);
pinMode(SRight_1,INPUT);
pinMode(Motor_M1,OUTPUT);
pinMode(Motor_M2,OUTPUT);
pinMode(Motor_Left,OUTPUT);
pinMode(Motor_Right,OUTPUT);
pinMode(PeopleLED,OUTPUT); //紅外線感測前方避障狀態LED
delay(5000);
}
void loop(){
int nIRStatus;
byteSensorStatus = 0;
nIRStatus = digitalRead(IRPeople);
if(nIRStatus == 0){
motorstop(0,0);
delay(5000);
}
else{
nIRStatus = digitalRead(SLeft_1);
if(nIRStatus == 1)
byteSensorStatus = (byteSensorStatus | (0x01 << 4));
nIRStatus = digitalRead(SLeft);
if(nIRStatus == 1)
byteSensorStatus = (byteSensorStatus | (0x01 << 3));
nIRStatus = digitalRead(SMiddle);
if(nIRStatus == 1)
byteSensorStatus = (byteSensorStatus | (0x01 << 2));
nIRStatus = digitalRead(SRight);
if(nIRStatus == 1)
byteSensorStatus = (byteSensorStatus | (0x01 << 1));
nIRStatus = digitalRead(SRight_1);
if(nIRStatus == 1)
byteSensorStatus = (byteSensorStatus | 0x01);
drivemotor(byteSensorStatus);
}
}
void drivemotor(byte nStatus)
{
switch(nStatus)
{
case 31:// SL1:1 SL:1 SM:1 SR:1 SR1:1// 黑黑黑黑黑
motorstop(0,0);
break;
case 30:// SL1:1 SL:1 SM:1 SR:1 SR1:0// 黑黑黑黑白
motorstop(0,0);
break;
case 29:// SL1:1 SL:1 SM:1 SR:0 SR1:1// 黑黑黑白黑
motorstop(0,0);
break;
case 28:// SL1:1 SL:1 SM:1 SR:0 SR1:0// 黑黑黑白白
right(0,0);
break;
case 27:// SL1:1 SL:1 SM:0 SR:1 SR1:1// 黑黑白黑黑
motorstop(0,0);
break;
case 26:// SL1:1 SL:1 SM:0 SR:1 SR1:1// 黑黑白黑白
motorstop(0,0);
break;
case 25:// SL1:1 SL:1 SM:0 SR:0 SR1:1// 黑黑白白黑
motorstop(0,0);
break;
case 24:// SL1:1 SL:1 SM:0 SR:0 SR1:0// 黑黑白白白
slowright_1(0,0);
break;
case 23:// SL1:1 SL:0 SM:1 SR:1 SR1:1// 黑白黑黑黑
motorstop(0,0);
break;
case 22:// SL1:1 SL:0 SM:1 SR:1 SR1:0// 黑白黑黑白
motorstop(0,0);
break;
case 21:// SL1:1 SL:0 SM:1 SR:0 SR1:1// 黑白黑白黑
motorstop(0,0);
break;
case 20:// SL1:1 SL:0 SM:1 SR:0 SR1:0// 黑白黑白白
motorstop(0,0);
break;
case 19:// SL1:1 SL:0 SM:0 SR:1 SR1:1// 黑白白黑黑
motorstop(0,0);
break;
case 18:// SL1:1 SL:0 SM:0 SR:1 SR1:0// 黑白白黑白
motorstop(0,0);
break;
case 17:// SL1:1 SL:0 SM:0 SR:0 SR1:1// 黑白白白黑
motorstop(0,0);
break;
case 16:// SL1:1 SL:0 SM:0 SR:0 SR1:0// 黑白白白白
slowright_1(0,0);
break;
case 15:// SL1:0 SL:1 SM:1 SR:1 SR1:1// 白黑黑黑黑
motorstop(0,0);
break;
case 14:// SL1:0 SL:1 SM:1 SR:1 SR1:0// 白黑黑黑白
forward(0,130);
break;
case 13:// SL1:0 SL:1 SM:1 SR:0 SR1:1// 白黑黑白黑
motorstop(0,0);
break;
case 12:// SL1:0 SL:1 SM:1 SR:0 SR1:0// 白黑黑白白
slowright(0,0);
break;
case 11:// SL1:0 SL:1 SM:0 SR:1 SR1:1// 白黑白黑黑
motorstop(0,0);
break;
case 10:// SL1:0 SL:1 SM:0 SR:1 SR1:0// 白黑白黑白
motorstop(0,0);
break;
case 9:// SL1:0 SL:1 SM:0 SR:0 SR1:1// 白黑白白黑
motorstop(0,0);
break;
case 8:// SL1:0 SL:1 SM:0 SR:0 SR1:0// 白黑白白白
slowright(0,0);
break;
case 7:// SL1:0 SL:0 SM:1 SR:1 SR1:1// 白白黑黑黑
left(0,0);
break;
case 6:// SL1:0 SL:0 SM:1 SR:1 SR1:0// 白白黑黑白
slowleft(0,0);
break;
case 5:// SL1:0 SL:0 SM:1 SR:0 SR1:1// 白白黑白黑
motorstop(0,0);
break;
case 4:// SL1:0 SL:0 SM:1 SR:0 SR1:0// 白白黑白白
forward(0,130);
break;
case 3:// SL1:0 SL:0 SM:0 SR:1 SR1:1// 白白白黑黑
slowleft_1(0,0);
break;
case 2:// SL1:0 SL:0 SM:0 SR:1 SR1:0// 白白白黑白
slowleft(0,0);
break;
case 1:// SL1:0 SL:0 SM:0 SR:0 SR1:1// 白白白白黑
slowleft_1(0,0);
break;
case 0:// SL1:0 SL:0 SM:0 SR:0 SR1:0// 白白白白白
motorstop(0,0);
break;
}
}
void motorstop(byte flag, byte motorspeed)
{
analogWrite(Motor_M1,motorspeed);
analogWrite(Motor_M2,motorspeed);
}
void forward(byte flag, byte motorspeed)
{
digitalWrite(Motor_Left,LOW);
digitalWrite(Motor_Right,LOW);
analogWrite(Motor_M1,motorspeed);
analogWrite(Motor_M2,motorspeed);
}
void right(byte flag, byte motorspeed)
{
digitalWrite(Motor_Left,LOW);
digitalWrite(Motor_Right,LOW);
analogWrite(Motor_M1,motorspeed);
analogWrite(Motor_M2,70);
}
void slowright(byte flag, byte motorspeed)
{
digitalWrite(Motor_Left,LOW);
digitalWrite(Motor_Right,LOW);
analogWrite(Motor_M1,motorspeed);
analogWrite(Motor_M2,70);
}
void slowright_1(byte flag, byte motorspeed)
{
digitalWrite(Motor_Left,LOW);
digitalWrite(Motor_Right,LOW);
analogWrite(Motor_M1,motorspeed);
analogWrite(Motor_M2,80);
}
void left(byte flag, byte motorspeed)
{
digitalWrite(Motor_Left,LOW);
digitalWrite(Motor_Right,LOW);
analogWrite(Motor_M1,70);
analogWrite(Motor_M2,motorspeed);
}
void slowleft(byte flag, byte motorspeed)
{
digitalWrite(Motor_Left,LOW);
digitalWrite(Motor_Right,LOW);
analogWrite(Motor_M1,70);
analogWrite(Motor_M2,motorspeed);
}
void slowleft_1(byte flag, byte motorspeed)
{
digitalWrite(Motor_Left,LOW);
digitalWrite(Motor_Right,LOW);
analogWrite(Motor_M1,80);
analogWrite(Motor_M2,motorspeed);
}
複製代碼
作者:
vegewell
時間:
2013-12-21 03:25
回復
1#
ypyp3399
是的,你這個程式有些問題,
如,三方紅外線訊號輸入同時是一,
那你的byteSensorStatus,應該選擇哪個?得到最好的動作,
你應該先測試一下 motorstop(0,0);
另外寫個簡單的測試程式,沒接紅外線感測器的,
據我瞭解,有些馬達驅動晶片,即使enable設定為0,也一樣照轉不誤.
歡迎光臨 Robofun 機器人論壇 (https://robofun.net/forum/)
Powered by Discuz! X3.2