最近新发现手里有三个9G 的舵机和之前想做无人机时买的三轴陀螺仪模块,闲着没事在家制作一个手机稳定器。
制作手机稳定器的原理无非是:
三轴陀螺仪检测到X轴方向沿负方向运动,那么相机相应X轴的舵机就进行相反的方向移动,以抵消晃动。
我的三轴陀螺仪的芯片是L3G4200D;
在网上有些玩家用的是6轴陀螺仪,MPU6050.相应的库也很健全,但是目前我没有,只能充分利用手里的东西。
电路连线如图所示:
相应的三维结构图如下:
采用标准的笛卡尔三维结构,编程也方便,数学模型好建立。
关于L3G4200D陀螺仪,有如下案例:
L3G4200D是意法(ST)半导体公司推出的一款MEMS运动传感器:三轴数字输出陀螺仪。可选-250~250、-500~500、-2000-2000dps
开发环境:
系统:XP
单板:Arduino Leonardo
平台:arduino-1.8.12
目标:读三轴陀螺仪的原始数据,并通过串口显示
一、硬件介绍
三轴陀螺仪L3G4200D模块的原理图如下:
这里只用到SCL、SDA、VCC_3.3V、GND分别连接到Arduino的对应接口上。Arduino Leonardo上直接标有SDA、SCL连上即可,其它Arduino根据自己的板子连接。
二、编写测试代码
现在的arduino版本高,在网上找的例程都编译通不过,换了个低版本才编译通过。可以参考这个上面的代码 https://github.com/pololu/L3G4200D/tree/66f1448d7f6767e12d0fe0c5c50d4e037aedc27c/L3G4200D 找到这两个文件L3G4200D.cpp L3G4200D.h,但文件好像不能直接下,代码都贴在网页上,直接copy下来。然后还要在arduino-1.0.1-windows\arduino-1.0.1\libraries下新建L3G4200D目录,将L3G4200D.cpp L3G4200D.h拷到刚建的L3G4200D,就可以在Android中使用L3G4200D类。
文件L3G4200D.cpp
#include <L3G4200D.h>
#include <Wire.h>
#include <math.h>
// Defines
// The Arduino two-wire interface uses a 7-bit number for the address,
// and sets the last bit correctly based on reads and writes
#define GYR_ADDRESS (0xD2 >> 1)
// Public Methods //
// Turns on the L3G4200D's gyro and places it in normal mode.
void L3G4200D::enableDefault(void)
{
// 0x0F = 0b00001111
// Normal power mode, all axes enabled
writeReg(L3G4200D_CTRL_REG1, 0x0F);
}
// Writes a gyro register
void L3G4200D::writeReg(byte reg, byte value)
{
Wire.beginTransmission(GYR_ADDRESS);
Wire.write(reg);
Wire.write(value);
Wire.endTransmission();
}
// Reads a gyro register
byte L3G4200D::readReg(byte reg)
{
byte value;
Wire.beginTransmission(GYR_ADDRESS);
Wire.write(reg);
Wire.endTransmission();
Wire.requestFrom(GYR_ADDRESS, 1);
value = Wire.read();
Wire.endTransmission();
return value;
}
// Reads the 3 gyro channels and stores them in vector g
void L3G4200D::read()
{
Wire.beginTransmission(GYR_ADDRESS);
// assert the MSB of the address to get the gyro
// to do slave-transmit subaddress updating.
Wire.write(L3G4200D_OUT_X_L | (1 << 7));
Wire.endTransmission();
Wire.requestFrom(GYR_ADDRESS, 6);
while (Wire.available() < 6);
uint8_t xla = Wire.read();
uint8_t xha = Wire.read();
uint8_t yla = Wire.read();
uint8_t yha = Wire.read();
uint8_t zla = Wire.read