RobotHardware-UESTC/Hardware/银星机器人底盘/PiRobot-YH_Firmware v1.0/STM32/Lib/IMU/GY85.cpp

135 lines
3.8 KiB
C++
Raw Permalink Normal View History

2024-01-20 13:19:09 +08:00
#include "GY85.h"
#include "board.h"
#include "print.h"
#include "delay.h"
#define ADXL345_SCALE 0.0392266 // 4mg/LSB 4*9.80665
#define ITG3205_SCALE 0.001065264417860243 //rad/s 4000/65536*(PI/180)
#define HMC5883L_SCALE 0.92 // mG/LSb
#define WRITE_INTERVAL 20
2024-01-24 22:08:32 +08:00
bool GY85::Init()
2024-01-20 13:19:09 +08:00
{
delay_ms(500);
Board::get()->i2c_init();
if (!accel.testConnection()) {
#if IMU_DEBUG_ENABLE
2024-01-23 15:06:39 +08:00
log("ADXL345 NOT FOUND!");
2024-01-20 13:19:09 +08:00
#endif
return false;
}
accel.initialize();
delay_ms(WRITE_INTERVAL);
accel.setAutoSleepEnabled(false);
delay_ms(WRITE_INTERVAL);
accel.setFullResolution(1);
delay_ms(WRITE_INTERVAL);
accel.setRange(ADXL345_RANGE_4G);
delay_ms(WRITE_INTERVAL);
accel.setRate(ADXL345_RATE_50);
delay_ms(WRITE_INTERVAL);
#if IMU_DEBUG_ENABLE
uint8_t buffer=0;
Board::get()->i2c_read_byte(ADXL345_DEFAULT_ADDRESS, ADXL345_RA_POWER_CTL, &buffer);
2024-01-23 15:06:39 +08:00
log("ADXL345_RA_POWER_CTL=%d", buffer);
2024-01-20 13:19:09 +08:00
Board::get()->i2c_read_byte(ADXL345_DEFAULT_ADDRESS, ADXL345_RA_DATA_FORMAT, &buffer);
2024-01-23 15:06:39 +08:00
log("ADXL345_RA_DATA_FORMAT=%d", buffer);
2024-01-20 13:19:09 +08:00
Board::get()->i2c_read_byte(ADXL345_DEFAULT_ADDRESS, ADXL345_RA_BW_RATE, &buffer);
2024-01-23 15:06:39 +08:00
log("ADXL345_RA_BW_RATE=%d", buffer);
2024-01-20 13:19:09 +08:00
#endif
if (!gyro.testConnection()) {
#if IMU_DEBUG_ENABLE
2024-01-23 15:06:39 +08:00
log("ITG3205 NOT FOUND!");
2024-01-20 13:19:09 +08:00
#endif
return false;
}
gyro.initialize();
gyro.reset();
delay_ms(WRITE_INTERVAL);
gyro.setFullScaleRange(ITG3200_FULLSCALE_2000);
delay_ms(WRITE_INTERVAL);
gyro.setDLPFBandwidth(ITG3200_DLPF_BW_42);
delay_ms(WRITE_INTERVAL);
gyro.setRate(0x13); //1khz/(1+0x13) 50hz
delay_ms(WRITE_INTERVAL);
gyro.setClockSource(ITG3200_CLOCK_PLL_ZGYRO);
delay_ms(WRITE_INTERVAL);
#if IMU_DEBUG_ENABLE
Board::get()->i2c_read_byte(ITG3200_DEFAULT_ADDRESS, ITG3200_RA_PWR_MGM, &buffer);
2024-01-23 15:06:39 +08:00
log("ITG3200_RA_PWR_MGM=%d", buffer);
2024-01-20 13:19:09 +08:00
Board::get()->i2c_read_byte(ITG3200_DEFAULT_ADDRESS, ITG3200_RA_DLPF_FS, &buffer);
2024-01-23 15:06:39 +08:00
log("ITG3200_RA_DLPF_FS=%d", buffer);
2024-01-20 13:19:09 +08:00
Board::get()->i2c_read_byte(ITG3200_DEFAULT_ADDRESS, ITG3200_RA_SMPLRT_DIV, &buffer);
2024-01-23 15:06:39 +08:00
log("ITG3200_RA_SMPLRT_DIV=%d", buffer);
2024-01-20 13:19:09 +08:00
#endif
if (!mag.testConnection()) {
#if IMU_DEBUG_ENABLE
2024-01-23 15:06:39 +08:00
log("HMC5883L NOT FOUND!");
2024-01-20 13:19:09 +08:00
#endif
return false;
}
mag.initialize();
delay_ms(WRITE_INTERVAL);
mag.setGain(HMC5883L_GAIN_1090);
delay_ms(WRITE_INTERVAL);
mag.setDataRate(HMC5883L_RATE_75);
delay_ms(WRITE_INTERVAL);
mag.setSampleAveraging(HMC5883L_AVERAGING_1);
delay_ms(WRITE_INTERVAL);
mag.setMode(HMC5883L_MODE_SINGLE);
delay_ms(WRITE_INTERVAL);
#if IMU_DEBUG_ENABLE
Board::get()->i2c_read_byte(HMC5883L_DEFAULT_ADDRESS, HMC5883L_RA_CONFIG_B, &buffer);
2024-01-23 15:06:39 +08:00
log("HMC5883L_RA_CONFIG_B=%d", buffer);
2024-01-20 13:19:09 +08:00
Board::get()->i2c_read_byte(HMC5883L_DEFAULT_ADDRESS, HMC5883L_RA_CONFIG_A, &buffer);
2024-01-23 15:06:39 +08:00
log("HMC5883L_RA_CONFIG_A=%d", buffer);
2024-01-20 13:19:09 +08:00
Board::get()->i2c_read_byte(HMC5883L_DEFAULT_ADDRESS, HMC5883L_RA_MODE, &buffer);
2024-01-23 15:06:39 +08:00
log("HMC5883L_RA_MODE=%d", buffer);
2024-01-20 13:19:09 +08:00
#endif
2024-01-23 15:06:39 +08:00
log("GY85 INIT SUCCESS!");
2024-01-20 13:19:09 +08:00
return true;
}
#define CYCLE_COUNT 3
2024-01-24 22:08:32 +08:00
void GY85::GetData(float* imu_data)
2024-01-20 13:19:09 +08:00
{
static int i=0;
if (i%CYCLE_COUNT==0) {
accel.getAcceleration(&ax, &ay, &az);
} else if (i%CYCLE_COUNT==1)
{
gyro.getRotation(&gx, &gy, &gz);
} else //if (i%CYCLE_COUNT==2)
{
mag.getHeading(&mx, &my, &mz);
mag.setMode(HMC5883L_MODE_SINGLE);
}
i++;
#if IMU_DEBUG_ENABLE
2024-01-23 15:06:39 +08:00
log("[%d %d %d] [%d %d %d] [%d %d %d]", ax, ay, az, gx, gy, gz, mx, my, mz);
2024-01-20 13:19:09 +08:00
#endif
imu_data[0] = ax*ADXL345_SCALE;
imu_data[1] = ay*ADXL345_SCALE;
imu_data[2] = az*ADXL345_SCALE;
imu_data[3] = gx*ITG3205_SCALE;
imu_data[4] = gy*ITG3205_SCALE;
imu_data[5] = gz*ITG3205_SCALE;
imu_data[6] = mx*HMC5883L_SCALE;
imu_data[7] = my*HMC5883L_SCALE;
imu_data[8] = mz*HMC5883L_SCALE;
}