它的角度是以移動的電壓差值去累積計算,
所以你以2.6秒轉90度 輸出歸零值1.35v左右,並不奇怪,
請用以下的程式試看:
使用 At X-OUT and Y-OUT,
先忽略 At X4.5OUT and Y4.5OUT,
所以腳位接線請接正確 ( XOUT <-----> Analog Pin 0)
int gyroPin = 0; //Gyro is connected to analog pin 0
float gyroVoltage = 5; //Gyro is running at 5V
float gyroZeroVoltage = 1.35; //Gyro is zeroed at 1.35V
float gyroSensitivity = .002; //Our example gyro is 2mV/deg/sec
float rotationThreshold = 1; //Minimum deg/sec to keep track of - helps with gyro drifting
float currentAngle = 0; //Keep track of our current angle
void setup() {
Serial.begin (9600);
}
void loop() {
//This line converts the 0-1023 signal to 0-5V
float gyroRate = (analogRead(gyroPin) * gyroVoltage) / 1023;
//This line finds the voltage offset from sitting still
gyroRate -= gyroZeroVoltage;
//This line divides the voltage we found by the gyro's sensitivity
gyroRate /= gyroSensitivity;
//Ignore the gyro if our angular velocity does not meet our threshold
if (gyroRate >= rotationThreshold || gyroRate <= -rotationThreshold) {
//This line divides the value by 100 since we are running in a 10ms loop (1000ms/10ms)
gyroRate /= 100;
currentAngle += gyroRate;
}
//Keep our angle between 0-359 degrees
if (currentAngle < 0)
currentAngle += 360;
else if (currentAngle > 359)
currentAngle -= 360;
//DEBUG
Serial.println(currentAngle);
delay(10);
}作者: 幸福羔羊 時間: 2011-10-16 21:25
感謝v大的回覆
我照v大的方式跑之後,發現他再靜態時會一直加上去.....
目前接線
A0 <----> XOUT;
3.3V <----> VCC;
GND <----> GND;
因為ARDUINO的參考電壓為3.3V 因此我參考電壓改成3.3
請問要怎麼用呢??作者: vegewell 時間: 2011-10-16 22:34 本帖最後由 vegewell 於 2011-10-16 22:36 編輯
互補濾波器就是一串數學方程式:
We design a 2nd order complementary filter.
The high pass filter is:
The low pass filter is the complementary:
As for the 1st order complementary filter, the high pass filter can combine with the integration of the rate gyro. We do not take into account the dynamic of the rate gyro:
This 2nd order complementary filter compensates for the rate gyro bias with no steady state error.
-------------------------
把數學方程式轉換成自己用程式寫,
成為code, 一連串的函數計算,如下例:
#define LPWM 9 // left motor PWM
#define RPWM 10 // right motor PWM
#define LDIR 11 // left motor direction
#define RDIR 12 // right motor direction
#define A_PIN 0 // accelerometer analog input
#define G_PIN 4 // gyro analog input
#define S_PIN 6 // steering analog input
#define A_ZERO 341 // approx. 1.5[V] * 1024[LSB/V]
#define G_ZERO 253 // approx. 1.23[V] * 1024[LSB/V]
#define S_ZERO 766 // approx. 2.5[V] * 1024[LSB/V]
#define A_GAIN 0.932 // [deg/LSB]
#define G_GAIN 1.466 // [deg/s/LSB]
#define S_GAIN 0.25 // [LSB/LSB] (AAAHHHHHHH WHAT?)
#define DT 0.02 // [s/loop] loop period
#define A 0.962 // complementary filter constant
#define KP 0.5 // proportional controller gain [LSB/deg/loop]
#define KD 0.5 // derivative controller gain [LSB/deg/loop]
float angle = 0.0; // [deg]
float rate = 0.0; // [deg/s]
float output = 0.0; // [LSB] (100% voltage to motor is 255LSB)
void setup()
{
// Make sure all motor controller pins start low.
digitalWrite(LPWM, LOW);
digitalWrite(RPWM, LOW);
digitalWrite(LDIR, LOW);
digitalWrite(RDIR, LOW);
// Set all motor control pins to outputs.
pinMode(LPWM, OUTPUT);
pinMode(RPWM, OUTPUT);
pinMode(LDIR, OUTPUT);
pinMode(RDIR, OUTPUT);
pinMode(13, OUTPUT);
Serial.begin(9600);
}
void loop()
{
signed int accel_raw = 0;
signed int gyro_raw = 0;
signed int output_left = 0;
signed int output_right = 0;
signed int steer_raw = 0;
// Loop speed test.
digitalWrite(13, HIGH);
// Read in the raw accelerometer, gyro, and steering singals.
// Offset for zero angle/rate.
accel_raw = (signed int) analogRead(A_PIN) - A_ZERO;
gyro_raw = G_ZERO - (signed int) analogRead(G_PIN);
steer_raw = (signed int) analogRead(S_PIN) - S_ZERO;
// Scale the gyro to [deg/s].
rate = (float) gyro_raw * G_GAIN;