|
发一个arduino UNO + MPU-6050陀螺仪 的代码,是101的修改版,process 端按原来的代码(注意修改端口 Serial.list()[0] 这个数组 )。注意:uno自带串口芯片1411无法实现与process通信,会发生获取端口时显示数组越界错误,后来接用FT232串口转tty连接UNO,才可以正常通信!
以下是UNO端代码:
[mw_shl_code=c,true]#include <MadgwickAHRS.h>
#include "I2Cdev.h"
#include "MPU6050.h"
#include "Wire.h"
MPU6050 accelgyro;
//MPU6050 accelgyro(0x69); // <-- use for AD0 high
Madgwick filter;
bool blinkState = false;
int16_t ax, ay, az;
int16_t gx, gy, gz;
float yaw;
float pitch;
float roll;
int factor = 800;
void setup() {
Wire.begin();
Serial.begin(9600);
Serial.println("Initializing I2C devices...");
accelgyro.initialize();
Serial.println("Testing device connections...");
Serial.println(accelgyro.testConnection() ? "MPU6050 connection successful" : "MPU6050 connection failed");
}
void loop() {
accelgyro.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
filter.updateIMU(gx / factor, gy / factor, gz / factor, ax, ay, az);
yaw = filter.getYaw();
roll = filter.getRoll();
pitch = filter.getPitch();
if (Serial.available() > 0) {
int val = Serial.read();
if (val == 's') { // if incoming serial is "s"
Serial.print(yaw);
Serial.print(","); // print comma so values can be parsed
Serial.print(pitch);
Serial.print(","); // print comma so values can be parsed
Serial.println(roll);
}
}
}[/mw_shl_code]
|
|