Robofun 機器人論壇

 找回密碼
 申請會員
搜索
熱搜: 活動 交友 discuz
查看: 1996|回復: 1
打印 上一主題 下一主題

程式問題請教

[複製鏈接]
跳轉到指定樓層
1#
發表於 2013-12-11 09:39:21 | 只看該作者 回帖獎勵 |倒序瀏覽 |閱讀模式
您好
我使用Arduino DUE來驅動馬達,我採用5個感測器,有32種狀態,可是馬達運動卻不是我程式所設定的狀態,舉例case 0(感測器狀態皆為0),馬達程式預設皆停止,實際運行時馬達卻全速運行。
想請問我的程式是否有問題。
  1. const int IRPeople =10;   //紅外線感測器前方避障
  2. const int SLeft_1 = 5;   //左邊方向紅外線二訊號輸入
  3. const int SLeft = 6;      //左邊方向紅外線一訊號輸入
  4. const int SMiddle = 7;    //中間方向紅外線訊號輸入
  5. const int SRight = 11;     //右邊方向紅外線一訊號輸入
  6. const int SRight_1 = 12;  //右邊方向紅外線二訊號輸入

  7. const int PeopleLED = 13;  //紅外線感測前方避障狀態LED
  8. const int Motor_Left = 9;
  9. const int Motor_Right = 8;
  10. const int Motor_M1 = DAC0;     //右邊
  11. const int Motor_M2 = DAC1;     //左邊

  12. byte byteSensorStatus = 0;

  13. void setup(){
  14.   Serial.begin(9600);

  15.   pinMode(IRPeople,INPUT);  //紅外線感測器前方避障
  16.   pinMode(SLeft_1,INPUT);
  17.   pinMode(SLeft,INPUT);  
  18.   pinMode(SMiddle,INPUT);
  19.   pinMode(SRight,INPUT);
  20.   pinMode(SRight_1,INPUT);
  21.   
  22.   pinMode(Motor_M1,OUTPUT);
  23.   pinMode(Motor_M2,OUTPUT);
  24.   pinMode(Motor_Left,OUTPUT);
  25.   pinMode(Motor_Right,OUTPUT);
  26.   pinMode(PeopleLED,OUTPUT);  //紅外線感測前方避障狀態LED
  27.   
  28.   delay(5000);
  29. }

  30. void loop(){
  31.   int nIRStatus;
  32.   
  33.   byteSensorStatus = 0;
  34.   nIRStatus = digitalRead(IRPeople);
  35.   if(nIRStatus == 0){
  36.   
  37.     motorstop(0,0);
  38.     delay(5000);
  39.   }
  40.   else{
  41.   nIRStatus = digitalRead(SLeft_1);
  42.   if(nIRStatus == 1)
  43.   byteSensorStatus = (byteSensorStatus | (0x01 << 4));
  44.   
  45.   nIRStatus = digitalRead(SLeft);
  46.   if(nIRStatus == 1)
  47.   byteSensorStatus = (byteSensorStatus | (0x01 << 3));
  48.   
  49.   nIRStatus = digitalRead(SMiddle);
  50.   if(nIRStatus == 1)
  51.   byteSensorStatus = (byteSensorStatus | (0x01 << 2));
  52.   
  53.   nIRStatus = digitalRead(SRight);
  54.   if(nIRStatus == 1)
  55.   byteSensorStatus = (byteSensorStatus | (0x01 << 1));
  56.   
  57.   nIRStatus = digitalRead(SRight_1);
  58.   if(nIRStatus == 1)
  59.   byteSensorStatus = (byteSensorStatus | 0x01);
  60.   
  61.   drivemotor(byteSensorStatus);
  62.   }
  63. }

  64. void drivemotor(byte nStatus)
  65. {
  66.   switch(nStatus)
  67.   {
  68.     case 31:// SL1:1 SL:1 SM:1 SR:1 SR1:1// 黑黑黑黑黑
  69.     motorstop(0,0);
  70.     break;
  71.     case 30:// SL1:1 SL:1 SM:1 SR:1 SR1:0// 黑黑黑黑白
  72.     motorstop(0,0);
  73.     break;
  74.     case 29:// SL1:1 SL:1 SM:1 SR:0 SR1:1// 黑黑黑白黑
  75.     motorstop(0,0);
  76.     break;
  77.     case 28:// SL1:1 SL:1 SM:1 SR:0 SR1:0// 黑黑黑白白
  78.     right(0,0);
  79.     break;
  80.     case 27:// SL1:1 SL:1 SM:0 SR:1 SR1:1// 黑黑白黑黑
  81.     motorstop(0,0);
  82.     break;
  83.     case 26:// SL1:1 SL:1 SM:0 SR:1 SR1:1// 黑黑白黑白
  84.     motorstop(0,0);
  85.     break;
  86.     case 25:// SL1:1 SL:1 SM:0 SR:0 SR1:1// 黑黑白白黑
  87.     motorstop(0,0);
  88.     break;
  89.     case 24:// SL1:1 SL:1 SM:0 SR:0 SR1:0// 黑黑白白白
  90.     slowright_1(0,0);
  91.     break;
  92.     case 23:// SL1:1 SL:0 SM:1 SR:1 SR1:1// 黑白黑黑黑
  93.     motorstop(0,0);
  94.     break;
  95.     case 22:// SL1:1 SL:0 SM:1 SR:1 SR1:0// 黑白黑黑白
  96.     motorstop(0,0);
  97.     break;
  98.     case 21:// SL1:1 SL:0 SM:1 SR:0 SR1:1// 黑白黑白黑
  99.     motorstop(0,0);
  100.     break;
  101.     case 20:// SL1:1 SL:0 SM:1 SR:0 SR1:0// 黑白黑白白
  102.     motorstop(0,0);
  103.     break;
  104.     case 19:// SL1:1 SL:0 SM:0 SR:1 SR1:1// 黑白白黑黑
  105.     motorstop(0,0);
  106.     break;
  107.     case 18:// SL1:1 SL:0 SM:0 SR:1 SR1:0// 黑白白黑白
  108.     motorstop(0,0);
  109.     break;
  110.     case 17:// SL1:1 SL:0 SM:0 SR:0 SR1:1// 黑白白白黑
  111.     motorstop(0,0);
  112.     break;
  113.     case 16:// SL1:1 SL:0 SM:0 SR:0 SR1:0// 黑白白白白
  114.     slowright_1(0,0);
  115.     break;
  116.     case 15:// SL1:0 SL:1 SM:1 SR:1 SR1:1// 白黑黑黑黑
  117.     motorstop(0,0);
  118.     break;
  119.     case 14:// SL1:0 SL:1 SM:1 SR:1 SR1:0// 白黑黑黑白
  120.     forward(0,130);
  121.     break;
  122.     case 13:// SL1:0 SL:1 SM:1 SR:0 SR1:1// 白黑黑白黑
  123.     motorstop(0,0);
  124.     break;
  125.     case 12:// SL1:0 SL:1 SM:1 SR:0 SR1:0// 白黑黑白白
  126.     slowright(0,0);
  127.     break;
  128.     case 11:// SL1:0 SL:1 SM:0 SR:1 SR1:1// 白黑白黑黑
  129.     motorstop(0,0);
  130.     break;
  131.     case 10:// SL1:0 SL:1 SM:0 SR:1 SR1:0// 白黑白黑白
  132.     motorstop(0,0);
  133.     break;
  134.     case 9:// SL1:0 SL:1 SM:0 SR:0 SR1:1// 白黑白白黑
  135.     motorstop(0,0);
  136.     break;
  137.     case 8:// SL1:0 SL:1 SM:0 SR:0 SR1:0// 白黑白白白
  138.     slowright(0,0);
  139.     break;
  140.     case 7:// SL1:0 SL:0 SM:1 SR:1 SR1:1// 白白黑黑黑
  141.     left(0,0);
  142.     break;
  143.     case 6:// SL1:0 SL:0 SM:1 SR:1 SR1:0// 白白黑黑白
  144.     slowleft(0,0);
  145.     break;
  146.     case 5:// SL1:0 SL:0 SM:1 SR:0 SR1:1// 白白黑白黑
  147.     motorstop(0,0);
  148.     break;
  149.     case 4:// SL1:0 SL:0 SM:1 SR:0 SR1:0// 白白黑白白
  150.     forward(0,130);
  151.     break;
  152.     case 3:// SL1:0 SL:0 SM:0 SR:1 SR1:1// 白白白黑黑
  153.     slowleft_1(0,0);
  154.     break;
  155.     case 2:// SL1:0 SL:0 SM:0 SR:1 SR1:0// 白白白黑白
  156.     slowleft(0,0);
  157.     break;
  158.     case 1:// SL1:0 SL:0 SM:0 SR:0 SR1:1// 白白白白黑
  159.     slowleft_1(0,0);
  160.     break;
  161.     case 0:// SL1:0 SL:0 SM:0 SR:0 SR1:0// 白白白白白
  162.     motorstop(0,0);
  163.     break;
  164.   }
  165. }

  166. void motorstop(byte flag, byte motorspeed)
  167. {
  168.   
  169.   analogWrite(Motor_M1,motorspeed);
  170.   analogWrite(Motor_M2,motorspeed);
  171.   
  172. }

  173. void forward(byte flag, byte motorspeed)
  174. {
  175.   
  176.   digitalWrite(Motor_Left,LOW);
  177.   digitalWrite(Motor_Right,LOW);
  178.   analogWrite(Motor_M1,motorspeed);
  179.   analogWrite(Motor_M2,motorspeed);
  180. }

  181. void right(byte flag, byte motorspeed)
  182. {
  183.   digitalWrite(Motor_Left,LOW);
  184.   digitalWrite(Motor_Right,LOW);
  185.   analogWrite(Motor_M1,motorspeed);
  186.   analogWrite(Motor_M2,70);
  187. }

  188. void slowright(byte flag, byte motorspeed)
  189. {
  190.   digitalWrite(Motor_Left,LOW);
  191.   digitalWrite(Motor_Right,LOW);
  192.   analogWrite(Motor_M1,motorspeed);
  193.   analogWrite(Motor_M2,70);
  194. }

  195. void slowright_1(byte flag, byte motorspeed)
  196. {
  197.   digitalWrite(Motor_Left,LOW);
  198.   digitalWrite(Motor_Right,LOW);
  199.   analogWrite(Motor_M1,motorspeed);
  200.   analogWrite(Motor_M2,80);
  201. }

  202. void left(byte flag, byte motorspeed)
  203. {
  204.   digitalWrite(Motor_Left,LOW);
  205.   digitalWrite(Motor_Right,LOW);
  206.   analogWrite(Motor_M1,70);
  207.   analogWrite(Motor_M2,motorspeed);
  208. }

  209. void slowleft(byte flag, byte motorspeed)
  210. {
  211.   digitalWrite(Motor_Left,LOW);
  212.   digitalWrite(Motor_Right,LOW);
  213.   analogWrite(Motor_M1,70);
  214.   analogWrite(Motor_M2,motorspeed);
  215. }

  216. void slowleft_1(byte flag, byte motorspeed)
  217. {
  218.   digitalWrite(Motor_Left,LOW);
  219.   digitalWrite(Motor_Right,LOW);
  220.   analogWrite(Motor_M1,80);
  221.   analogWrite(Motor_M2,motorspeed);
  222. }
複製代碼
2#
發表於 2013-12-21 03:25:36 | 只看該作者
回復 1# ypyp3399

是的,你這個程式有些問題,
如,三方紅外線訊號輸入同時是一,
那你的byteSensorStatus,應該選擇哪個?得到最好的動作,
你應該先測試一下 motorstop(0,0);
另外寫個簡單的測試程式,沒接紅外線感測器的,
據我瞭解,有些馬達驅動晶片,即使enable設定為0,也一樣照轉不誤.
您需要登錄後才可以回帖 登錄 | 申請會員

本版積分規則

小黑屋|手機版|Archiver|機器人論壇 from 2005.07

GMT+8, 2024-9-30 17:34 , Processed in 0.199678 second(s), 7 queries , Apc On.

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回復 返回頂部 返回列表