|
|
本帖最后由 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不知什么原因出现错误,读不出数据了,导致电机只向一个方向转)。
万般无奈,换了另一个购买的直流电机,发现接上这第二个电机,什么故障都没有了,顺利地实现了我的想法。真是百思不得其解。
第一个出现故障的电机是自带车轮的,第二个无故障电机无法安装车轮,这导致我现在无法进行下去(无法实现自平衡小车的开发),迫切希望解决现在的问题。
上电路图和程序,期待高人求解。
程序- #include<Wire.h>
- #include "gyro_accel.h"
- // Defining constants
- #define dt 20 // time difference in milli seconds
- #define rad2degree 57.3 // radian to degree conversion
- #define Filter_gain 0.95 // e.g. angle = angle_gyro*Filter_gain + angle_accel*(1-Filter_gain)
- // Global Variables
- unsigned long t = 0; // Time Variables
- 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;
- // DC motor driver with L298N
- const int motor1PWMPin = 5; // PWM Pin of Motor 1
- const int motor1Polarity1 = 4; // Polarity 1 Pin of Motor 1
- const int motor1Polarity2 = 7; // Polarity 2 Pin of Motor 1
- const int motor2PWMPin = 6; // PWM Pin of Motor 2
- const int motor2Polarity1 = 8; // Polarity 1 Pin of Motor 2
- const int motor2Polarity2 = 12; // Polarity 2 Pin of Motor 2
- int ValM1 = 124; // Initial Value for PWM Motor 1
- int ValM2 = 124; // Initial Value for PWM Motor 2
- void setup()
- {
- // MPU-6050
- Serial.begin(115200);
- Wire.begin();
- MPU6050_ResetWake();
- MPU6050_SetGains(0,1); // Setting the lows scale
- MPU6050_SetDLPF(0); // Setting the DLPF to inf Bandwidth for calibration
- MPU6050_OffsetCal(); // very important
- MPU6050_SetDLPF(6); // Setting the DLPF to lowest Bandwidth
-
- t = millis();
-
- // DC motor
- pinMode(motor1PWMPin, OUTPUT);
- pinMode(motor1Polarity1, OUTPUT);
- pinMode(motor1Polarity2, OUTPUT);
-
- pinMode(motor2PWMPin, OUTPUT);
- pinMode(motor2Polarity1, OUTPUT);
- pinMode(motor2Polarity2, OUTPUT);
-
- // set enablePin of motor 1 high so that motor 1 can turn on
- digitalWrite(motor1PWMPin, HIGH);
- digitalWrite(motor1Polarity1, HIGH);
- digitalWrite(motor1Polarity2, LOW);
- // set enablePin of motor 2 high so that motor 2 can turn on
- digitalWrite(motor2PWMPin, HIGH);
- digitalWrite(motor2Polarity1, HIGH);
- digitalWrite(motor2Polarity2, LOW);
- }
- void loop()
- {
- t = millis();
-
- MPU6050_ReadData();
-
- angle_x_gyro = (gyro_x_scalled*((float)dt/1000)+angle_x);
- angle_y_gyro = (gyro_y_scalled*((float)dt/1000)+angle_y);
- angle_z_gyro = (gyro_z_scalled*((float)dt/1000)+angle_z);
- angle_z_accel = atan(accel_z_scalled/(sqrt(accel_y_scalled*accel_y_scalled+accel_x_scalled*accel_x_scalled)))*(float)rad2degree;
- angle_y_accel = -atan(accel_x_scalled/(sqrt(accel_y_scalled*accel_y_scalled+accel_z_scalled*accel_z_scalled)))*(float)rad2degree;
- angle_x_accel = atan(accel_y_scalled/(sqrt(accel_x_scalled*accel_x_scalled+accel_z_scalled*accel_z_scalled)))*(float)rad2degree;
- angle_x = Filter_gain*angle_x_gyro+(1-Filter_gain)*angle_x_accel;
- angle_y = Filter_gain*angle_y_gyro+(1-Filter_gain)*angle_y_accel;
- angle_z = Filter_gain*angle_z_gyro+(1-Filter_gain)*angle_z_accel;
-
- Serial.print(angle_y);
- Serial.print("\n");
-
- if (angle_y > 0)
- {
- // set enablePin of motor 1 high so that motor 1 can turn on
- digitalWrite(motor1PWMPin, HIGH);
- digitalWrite(motor1Polarity1, HIGH);
- digitalWrite(motor1Polarity2, LOW);
- // set enablePin of motor 2 high so that motor 2 can turn on
- digitalWrite(motor2PWMPin, HIGH);
- digitalWrite(motor2Polarity1, HIGH);
- digitalWrite(motor2Polarity2, LOW);
- analogWrite(motor1PWMPin, ValM1);
- analogWrite(motor2PWMPin, ValM2);
- }
- else
- {
- // set enablePin of motor 1 high so that motor 1 can turn on
- digitalWrite(motor1PWMPin, HIGH);
- digitalWrite(motor1Polarity1, LOW);
- digitalWrite(motor1Polarity2, HIGH);
- // set enablePin of motor 2 high so that motor 2 can turn on
- digitalWrite(motor2PWMPin, HIGH);
- digitalWrite(motor2Polarity1, LOW);
- digitalWrite(motor2Polarity2, HIGH);
-
- analogWrite(motor1PWMPin, ValM1);
- analogWrite(motor2PWMPin, ValM2);
- }
-
- while((millis()-t) < dt) // Maing sure the cycle time is equal to dt
- {
- // Do nothing
- }
- }
复制代码 感谢前面的帖子网友们的热心回答,网友们的建议大致集中在电源上,认为应该将电源隔离,但是都说的不详细,我不知如何隔离电源?从电路图上看,我觉得最可能出现问题的部分是GND线的接法,图中是共地,可如果不共地,根本无法控制电机旋转,或者根本读不到MPU的角度,只能并且必须共地吧?
还有问题是:为什么一个电机好用(标注电压12V),另一个电机不好用(卖家告我9V)??? |
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有帐号?注册
x
|