|
前輩們好
小弟有個問題想請教
當我使用 arduino + MPU6050 如果透過電腦序列埠monitor 值很正常有跑出來
但有問題的地方是 當如果我將arduino 改為靠著行動電源供電5V 發現感測器的值就出不來了
想請教一下前輩們,問題出在哪邊??
感激不盡!!
附上程式碼
#include<Servo.h>
#include "I2Cdev.h"
#include "MPU6050.h"
#include "Wire.h"
Servo m1;
Servo m2;
Servo m3;
Servo m4;
int throttlePin = 0;
long LastTime, NowTime, TimeSpan;
float Ax,Ay,Az;
float X,Y,Z; // angle from acclerometer
float Gyro_x, Gyro_y,Gyro_z;// angle accleration from gyro sensor
float ix,iy,iz;// angle from gryo sensor
float Roll,Pitch,Yaw;//Position
const float Kp = 0.5, Ki = 0.00, Kd = 0.00;
float Turn;
MPU6050 accelgyro;
int16_t ax, ay, az;
int16_t gx, gy, gz;
void setup()
{
Wire.begin();
Serial.begin(9600);
accelgyro.initialize();
m1.attach(8);
m2.attach(9);
m3.attach(10);
m4.attach(11);
}
void loop()
{
int throttle = analogRead(throttlePin);
throttle = map(throttle, 0, 1023, 0, 179);
angle();
Turn = Kp * Roll;
m1.write(throttle + Turn);
m2.write(0);
m3.write(0);
m4.write(throttle - Turn);
}
void angle(){
accelgyro.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
Ax = ax/16384.00;
Ay = ay/16384.00;
Az = az/16384.00;
X = atan(Ax/sqrt(Az*Az+Ay*Ay))*180/PI; //angle from accelermeter
Y = atan(Ay/sqrt(Az*Az+Ax*Ax))*180/PI;
Z = atan(Az/sqrt(Ax*Ax+Ay*Ay))*180/PI;
Gyro_x = gx/131.00+6; // get angle acceleration
Gyro_y = gy/131.00;
Gyro_z = gz/131.00;
NowTime = millis(); //star to count time
TimeSpan = NowTime - LastTime; //count time pass
ix = 0.95*(ix + Gyro_x*TimeSpan / 1000) + 0.05*Y; // position count by Complementary Filter
iy = 0.94*(iy + Gyro_y*TimeSpan / 1000) + 0.06*X;
LastTime = NowTime;
Roll = ix - 1.44500;
Pitch = iy - 3.4;
if(Roll >= 1 || Roll <=-1){Roll = Roll;}
else{Roll = 0;}
Serial.print("Roll :");
Serial.print(Roll);
Serial.print("Pitch :");
Serial.println(Pitch);
} |
|