小R科技-WIFI机器人网·机器人创意工作室

 找回密码
 立即注册
查看: 5633|回复: 2

【已解决】关于arduino下位机只驱动单电机的求助

[复制链接]
发表于 2013-12-8 21:07:24 | 显示全部楼层 |阅读模式
本帖最后由 沧海笑 于 2013-12-10 23:59 编辑

问题:在使用论坛安卓蓝牙控制端程序v1.01控制,十六进制控制方式。小车只能驱动一只电机,另外一只无反应。
情况描述:
1、小车部分使用arduino UNO V3,L298电机驱动板,驱动两电机。下位机程序附上,是利用论坛车友的程序,其中left()中加了两只LED,是为了测试程序运行。
2、上位机,论坛蓝牙控制端程序v1.01,带wifi视频那个版本。
3、我所做的尝试:在单字符方式下双电机控制正常,所以我认为接线及硬件正常。单字符控制程序,使用的arduino pin与HEX规约的完全一样,接线也未改动。
     我将left()函数中,插入了三次LED闪烁,从程序运行看,上位机发来的控制串,下位机已经收到,并且调用了left()函数,LED依次闪烁,明显判断,其中M1电机的
     驱动是空白的,也就是说,该电机未响应。M2电机驱动正常。
4、使用第三方“蓝牙调试助手”,使用HEX模式发送控制串,情况和刚才是一样的。
请问,应该如何入手检查,请提出宝贵意见,谢谢。



  1. #include <Servo.h>
  2. Servo servo1;
  3. //Servo servo2;
  4. int E1 = 11;
  5. int E2 = 10;
  6. int M1_1 = 8;
  7. int M1_2 = 9;
  8. int M2_1 = 6;
  9. int M2_2 = 7;
  10. //int LED = 13;
  11. int buffer[3];
  12. int Serial_flag;
  13. int date_index=0;
  14. void setup()
  15. {
  16.   //pinMode(13,OUTPUT);
  17.   servo1.attach(4);//定义舵机控制口
  18. // servo2.attach(10);
  19.   Serial.begin(9600);
  20.   pinMode(M1_1,OUTPUT);
  21.   pinMode(M2_2,OUTPUT);
  22.   pinMode(E1,OUTPUT);
  23.   pinMode(M2_1,OUTPUT);
  24.   pinMode(M2_2,OUTPUT);
  25.   pinMode(E2,OUTPUT);
  26.    pinMode(5, OUTPUT); //red
  27.   pinMode(12, OUTPUT); //green
  28. }
  29. void Stop(void)
  30. {
  31.   digitalWrite(E1,LOW);
  32.   digitalWrite(E2,LOW);
  33. }
  34. void advance(int x)
  35. {
  36.   analogWrite(E1,x);
  37.   digitalWrite(M1_1,LOW);
  38.   digitalWrite(M1_2,HIGH);
  39.   analogWrite(E2,x);
  40.   digitalWrite(M2_1,LOW);
  41.   digitalWrite(M2_2,HIGH);
  42. }
  43. void back(int x)
  44. {
  45.   analogWrite(E1,x);
  46.   digitalWrite(M1_1,HIGH);
  47.   digitalWrite(M1_2,LOW);
  48.   analogWrite(E2,x);
  49.   digitalWrite(M2_1,HIGH);
  50.   digitalWrite(M2_2,LOW);
  51. }
  52. void left(int x)
  53. {
  54.   digitalWrite(5,LOW);//red flash 200ms
  55.   delay(800);
  56.   digitalWrite(5,HIGH);   
  57.   analogWrite(E1,x);
  58.   digitalWrite(M1_1,LOW);
  59.   digitalWrite(M1_2,HIGH);
  60.   digitalWrite(12,LOW);//green flash 400ms
  61.   delay(800);
  62.   digitalWrite(12,HIGH);  
  63.   analogWrite(E2,x);
  64.   digitalWrite(M2_1,HIGH);
  65.   digitalWrite(M2_2,LOW);
  66.   digitalWrite(5,LOW);//red flash 200ms
  67.   delay(800);
  68.   digitalWrite(5,HIGH);   
  69. }
  70. void right(int x)
  71. {
  72.   analogWrite(E1,x);
  73.   digitalWrite(M1_1,HIGH);
  74.   digitalWrite(M1_2,LOW);
  75.   analogWrite(E2,x);
  76.   digitalWrite(M2_1,HIGH);
  77.   digitalWrite(M2_2,LOW);
  78. }
  79. void Communication_Decode()
  80. {
  81.   if(buffer[0]==0x00)
  82.   {
  83.     switch(buffer[1])
  84.     {
  85.       case 0x01:advance(150);return;
  86.       case 0x02:back(150);return;
  87.       case 0x03:right(150);return;
  88.       case 0x04:left(150);return;
  89.       case 0x00:Stop(); return;
  90.     }
  91.   }
  92.   else if(buffer[0]==0x01)
  93.   {
  94.     if(buffer[2]>180)return;
  95.     switch(buffer[1])
  96.     {
  97.       case 0x01:servo1.write(buffer[2]);return;
  98.     //  case 0x02:servo2.write(buffer[2]);return;
  99.       default:return;
  100.     }
  101.   }
  102. }
  103. void loop()
  104. {
  105.   int temp_char;
  106.   

  107.   
  108.   while(Serial.available() > 0)
  109.   {
  110.     temp_char = Serial.read();
  111.     if(temp_char!=-1)
  112.     {
  113.       
  114.       if(Serial_flag==0)
  115.       {
  116.         if(temp_char==0xff)
  117.         {
  118.           Serial_flag = 1;
  119.         }
  120.       }
  121.       else
  122.       {
  123.         if(temp_char==0xff)
  124.         {
  125.           Serial_flag = 0;
  126.          
  127.           if(date_index==3)
  128.           {
  129.             Communication_Decode();
  130.           }
  131.           date_index = 0;
  132.         }
  133.         else
  134.         {
  135.           buffer[date_index]=temp_char;
  136.           Serial.println(buffer[date_index],HEX);
  137.           date_index++;
  138.         }
  139.       }
  140.     }
  141.   }
  142. }
复制代码


回复

使用道具 举报

 楼主| 发表于 2013-12-10 23:58:43 | 显示全部楼层
本帖最后由 沧海笑 于 2013-12-11 00:09 编辑

已经解决了,
  1. int E1 = 11;
  2. int E2 = 10;
复制代码
问题是舵机库与PWM调速之间的冲突,在极客工坊中有网友对此进行过测试,可惜以前没有见到那篇帖子。
将两个调速Pin避开PWM,一切正常,双电机驱动正常。
查阅了舵机库的资料原文:
The Servo library supports up to 12 motors on most Arduino boards and 48 on the Arduino Mega. On boards other than the Mega, use of the library disables analogWrite() (PWM) functionality on pins 9 and 10, whether or not there is a Servo on those pins. On the Mega, up to 12 servos can be used without interfering with PWM functionality; use of 12 to 23 motors will disable PWM on pins 11 and 12.
回复 支持 反对

使用道具 举报

发表于 2014-7-11 13:16:43 | 显示全部楼层
l楼主咋解决的,我也遇到了类似的问题,我是一个电机单转(没有反转),arduino串口收到的代码正确,说明703N正常(测试时是把arduino的TX接电脑的RX,arduinoRX接703N的TX),arduino返回的代码都正确
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关闭

新品特惠推荐上一条 /2 下一条

QQ|QQ技术咨询1|QQ技术咨询2|商务合作微信1:xiaorgeek001|商务合作微信2:XiaoRGEEK|诚聘英才|Archiver|手机版|小R科技-WIFI机器人网·机器人创意工作室 ( 粤ICP备15000788号-6 )

GMT+8, 2024-4-26 13:03 , Processed in 1.081433 second(s), 18 queries .

Powered by XiaoR GEEK X3.4

© 2014-2021 XiaoR GEEK

快速回复 返回顶部 返回列表