Robofun 機器人論壇

 找回密碼
 申請會員
搜索
熱搜: 活動 交友 discuz
查看: 3097|回復: 0
打印 上一主題 下一主題

關於平衡車校正程式碼

[複製鏈接]
跳轉到指定樓層
1#
發表於 2016-2-2 07:01:36 | 只看該作者 回帖獎勵 |倒序瀏覽 |閱讀模式
[table=98%]
[tr][td]小弟最近好不容易求到了可以讓平衡車子站起來的程式碼


#include "Wire.h"
#include <Servo.h>
#include <Kalman.h>

#include "I2Cdev.h"
#include "MPU6050.h"
float fRad2Deg = 57.295779513f;       //將弧度轉為角度的乘數
const int nCalibTimes = 1000;         //校準時讀數的次數
int calibData[6];                     //校準數據
MPU6050 accelgyro;

int16_t ax, ay, az;
int16_t gx, gy, gz;
bool blinkState = false;
char flag = false;                    //打印開關

unsigned long nLastTime = 0;          //上一次讀數的時間

Kalman kalmanRoll;                    //Roll角濾波器
Kalman kalmanPitch;                   //Pitch角濾波器

/*********** PID控制器參數 *********/
float ITerm, lastInput;              // 積分項、前次輸入
float Output = 0.0;                  // PID輸出值

/***********電機參數**************/
Servo ServoL;
Servo ServoR;

/***********四元數參數***********/
#define KP 0.025f//10.0f
#define KI 0.0f//0.008f
#define halfT 0.001f
float q0 = 1, q1 = 0, q2 = 0, q3 = 0;
float exInt = 0, eyInt = 0, ezInt = 0;
float Angle_roll, Angle_pitch, Angle_yaw;
byte ENA = 5;byte IN1 = 9; byte IN2 = 10;
byte ENB = 6;byte IN3 = 11;byte IN4 = 12;


void setup() {
    // join I2C bus (I2Cdev library doesn't do this automatically)
    Wire.begin();

    // initialize serial communication
    // (38400 chosen because it works as well at 8MHz as it does at 16MHz, but
    // it's really up to you depending on your project)
    Serial.begin(38400);

    // initialize device
    Serial.println("Initializing I2C devices...");
    accelgyro.initialize();

    // verify connection
    Serial.println("Testing device connections...");
    Serial.println(accelgyro.testConnection() ? "MPU6050 connection successful" : "MPU6050 connection failed");
for( byte i = 0; i <= 12 ; i++){
pinMode(i,OUTPUT);
}
    // configure Arduino LED for

}

void loop() {
    int16_t readouts[6];
  ReadAccGyr(readouts); //讀出測量值

  float realVals[6];
  Rectify(readouts, realVals); //根據校準的偏移量進行糾正

  //四元數解析出歐拉角
// AngleUpdate(realVals);

  //計算加速度向量的模長,均以g為單位
  float fRoll = GetRoll(realVals); //計算Roll角
  float fPitch = GetPitch(realVals); //計算Pitch角

  //計算兩次測量的時間間隔dt,以秒為單位
  unsigned long nCurTime = micros();
  float dt = (double)(nCurTime - nLastTime) / 1000000.0;
  //對Roll角和Pitch角進行卡
您需要登錄後才可以回帖 登錄 | 申請會員

本版積分規則

小黑屋|手機版|Archiver|機器人論壇 from 2005.07

GMT+8, 2024-4-29 05:12 , Processed in 0.262970 second(s), 8 queries , Apc On.

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回復 返回頂部 返回列表