极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 11761|回复: 3

还是相同的回路,不同的电机,一个好用,一个不好用!!!

[复制链接]
发表于 2014-9-2 19:36:22 | 显示全部楼层 |阅读模式
本帖最后由 liangquan 于 2014-9-2 19:38 编辑

参考帖子:
http://www.arduino.cn/thread-7203-1-1.html
http://www.geek-workshop.com/thread-10883-1-1.html

白手起家,一路走来,自己垫钱,投资500元买了Arduino及其附属设备(电阻、电源、面包板等等),打算大干一场,却卡在现在这个位置,不知该怎样解决……

初衷是打算做一台两轮的自平衡小车。先不管什么模型、PID算法,我现在的初步想法是先实现检测小车的倾斜角度,如果倾角>0,直流电机正转;如果倾角<0,直流电机反转。

原来采用的方案是自己用L293D搭建回路,失败(失败原因和本帖一致,见本文开头两个链接),有人说可能是隔离的问题,所以又买了一个L298N的直流电机驱动板,以为这回不会有问题,结果还是一样。

我现在的问题是:如果单独用MPU6050,我能成功读到倾角;如果单独控制直流电机,我也可以控制方向、转速。但当我将两者结合后,电机就只向一个方向转(其根本原因是MPU6050不知什么原因出现错误,读不出数据了,导致电机只向一个方向转)。

万般无奈,换了另一个购买的直流电机,发现接上这第二个电机,什么故障都没有了,顺利地实现了我的想法。真是百思不得其解。

第一个出现故障的电机是自带车轮的,第二个无故障电机无法安装车轮,这导致我现在无法进行下去(无法实现自平衡小车的开发),迫切希望解决现在的问题。

上电路图和程序,期待高人求解。



程序
  1. #include<Wire.h>
  2. #include "gyro_accel.h"

  3. // Defining constants
  4. #define dt 20            // time difference in milli seconds
  5. #define rad2degree 57.3  // radian to degree conversion
  6. #define Filter_gain 0.95  // e.g. angle = angle_gyro*Filter_gain + angle_accel*(1-Filter_gain)

  7. // Global Variables
  8. unsigned long t = 0;      // Time Variables
  9. float angle_x_gyro=0, angle_y_gyro=0, angle_z_gyro=0, angle_x_accel=0, angle_y_accel=0, angle_z_accel=0, angle_x=0, angle_y=0, angle_z=0;

  10. // DC motor driver with L298N
  11. const int motor1PWMPin = 5; // PWM Pin of Motor 1
  12. const int motor1Polarity1 = 4; // Polarity 1 Pin of Motor 1
  13. const int motor1Polarity2 = 7;  // Polarity 2 Pin of Motor 1
  14. const int motor2PWMPin = 6;  // PWM Pin of Motor 2
  15. const int motor2Polarity1 = 8; // Polarity 1 Pin of Motor 2
  16. const int motor2Polarity2 = 12;  // Polarity 2 Pin of Motor 2

  17. int ValM1 = 124;  // Initial Value for PWM Motor 1
  18. int ValM2 = 124;  // Initial Value for PWM Motor 2

  19. void setup()
  20. {
  21.   // MPU-6050
  22.   Serial.begin(115200);
  23.   Wire.begin();
  24.   MPU6050_ResetWake();
  25.   MPU6050_SetGains(0,1);  // Setting the lows scale
  26.   MPU6050_SetDLPF(0);    // Setting the DLPF to inf Bandwidth for calibration
  27.   MPU6050_OffsetCal();  // very important
  28.   MPU6050_SetDLPF(6);  // Setting the DLPF to lowest Bandwidth
  29.   
  30.   t = millis();
  31.   
  32.   // DC motor
  33.   pinMode(motor1PWMPin, OUTPUT);
  34.   pinMode(motor1Polarity1, OUTPUT);
  35.   pinMode(motor1Polarity2, OUTPUT);
  36.   
  37.   pinMode(motor2PWMPin, OUTPUT);
  38.   pinMode(motor2Polarity1, OUTPUT);
  39.   pinMode(motor2Polarity2, OUTPUT);
  40.   
  41.   // set enablePin of motor 1 high so that motor 1 can turn on
  42.   digitalWrite(motor1PWMPin, HIGH);
  43.   digitalWrite(motor1Polarity1, HIGH);
  44.   digitalWrite(motor1Polarity2, LOW);

  45.   // set enablePin of motor 2 high so that motor 2 can turn on  
  46.   digitalWrite(motor2PWMPin, HIGH);
  47.   digitalWrite(motor2Polarity1, HIGH);
  48.   digitalWrite(motor2Polarity2, LOW);
  49. }

  50. void loop()
  51. {
  52.   t = millis();
  53.   
  54.   MPU6050_ReadData();
  55.   
  56.   angle_x_gyro = (gyro_x_scalled*((float)dt/1000)+angle_x);
  57.   angle_y_gyro = (gyro_y_scalled*((float)dt/1000)+angle_y);
  58.   angle_z_gyro = (gyro_z_scalled*((float)dt/1000)+angle_z);

  59.   angle_z_accel = atan(accel_z_scalled/(sqrt(accel_y_scalled*accel_y_scalled+accel_x_scalled*accel_x_scalled)))*(float)rad2degree;
  60.   angle_y_accel = -atan(accel_x_scalled/(sqrt(accel_y_scalled*accel_y_scalled+accel_z_scalled*accel_z_scalled)))*(float)rad2degree;
  61.   angle_x_accel = atan(accel_y_scalled/(sqrt(accel_x_scalled*accel_x_scalled+accel_z_scalled*accel_z_scalled)))*(float)rad2degree;

  62.   angle_x = Filter_gain*angle_x_gyro+(1-Filter_gain)*angle_x_accel;
  63.   angle_y = Filter_gain*angle_y_gyro+(1-Filter_gain)*angle_y_accel;
  64.   angle_z = Filter_gain*angle_z_gyro+(1-Filter_gain)*angle_z_accel;
  65.   
  66.   Serial.print(angle_y);
  67.   Serial.print("\n");

  68.   if (angle_y > 0)
  69.   {
  70.     // set enablePin of motor 1 high so that motor 1 can turn on
  71.     digitalWrite(motor1PWMPin, HIGH);
  72.     digitalWrite(motor1Polarity1, HIGH);
  73.     digitalWrite(motor1Polarity2, LOW);

  74.     // set enablePin of motor 2 high so that motor 2 can turn on  
  75.     digitalWrite(motor2PWMPin, HIGH);
  76.     digitalWrite(motor2Polarity1, HIGH);
  77.     digitalWrite(motor2Polarity2, LOW);
  78.     analogWrite(motor1PWMPin, ValM1);
  79.     analogWrite(motor2PWMPin, ValM2);
  80.   }
  81.   else
  82.   {
  83.     // set enablePin of motor 1 high so that motor 1 can turn on
  84.     digitalWrite(motor1PWMPin, HIGH);
  85.     digitalWrite(motor1Polarity1, LOW);
  86.     digitalWrite(motor1Polarity2, HIGH);

  87.     // set enablePin of motor 2 high so that motor 2 can turn on  
  88.     digitalWrite(motor2PWMPin, HIGH);
  89.     digitalWrite(motor2Polarity1, LOW);
  90.     digitalWrite(motor2Polarity2, HIGH);
  91.    
  92.     analogWrite(motor1PWMPin, ValM1);
  93.     analogWrite(motor2PWMPin, ValM2);
  94.   }
  95.   
  96.   while((millis()-t) < dt)  // Maing sure the cycle time is equal to dt
  97.   {
  98.     // Do nothing
  99.   }
  100. }

复制代码
感谢前面的帖子网友们的热心回答,网友们的建议大致集中在电源上,认为应该将电源隔离,但是都说的不详细,我不知如何隔离电源?从电路图上看,我觉得最可能出现问题的部分是GND线的接法,图中是共地,可如果不共地,根本无法控制电机旋转,或者根本读不到MPU的角度,只能并且必须共地吧?

还有问题是:为什么一个电机好用(标注电压12V),另一个电机不好用(卖家告我9V)???

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x
回复

使用道具 举报

发表于 2014-9-2 22:04:57 | 显示全部楼层
本帖最后由 弘毅 于 2014-9-2 22:09 编辑

电机如果干扰大,可以尝试在电机两极上焊一个瓷片电容看看是否有改善,电机供电地与单片机地之间确保只有一个连接点,这个点串联0Ω电阻。如果串联0Ω电阻还是不行的话。。。那就只能隔离了,说明你电机太烂了。隔离的话一个系统都要隔离,成本不低。。。。那是最后的大招
回复 支持 反对

使用道具 举报

 楼主| 发表于 2014-9-3 09:28:31 | 显示全部楼层
弘毅 发表于 2014-9-2 22:04
电机如果干扰大,可以尝试在电机两极上焊一个瓷片电容看看是否有改善,电机供电地与单片机地之间确保只有一 ...

麻烦您看看这篇帖子:http://www.arduino.cn/forum.php?mod=viewthread&tid=7215&pid=73121&page=1&extra=#pid73121

我并联了一个10uF的电容行么?

串联0Ω的电阻,那不就是导线么?(电子0基础,见谅)
回复 支持 反对

使用道具 举报

 楼主| 发表于 2014-9-3 19:13:50 | 显示全部楼层
弘毅 发表于 2014-9-2 22:04
电机如果干扰大,可以尝试在电机两极上焊一个瓷片电容看看是否有改善,电机供电地与单片机地之间确保只有一 ...


我并联了电容还是不行,不会隔离。要想做成我的自平衡小车,是不得再卖一套电机和车轮了?
回复 支持 反对

使用道具 举报

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

本版积分规则

Archiver|联系我们|极客工坊

GMT+8, 2026-6-15 17:26 , Processed in 0.037369 second(s), 19 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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