/*
* Copyright (c) 2020 Nanjing Xiaoxiongpai Intelligent Technology Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include "cmsis_os2.h"
#include "iot_errno.h"
#include "iot_gpio.h"
#include "iot_gpio_ex.h"
#include "iot_i2c.h"
#include "iot_i2c_ex.h"
#include "hi_io.h"
#include "mpu6050.h"
float acc_x;
float acc_y;
float acc_z;
float gyro_x;
float gyro_y;
float gyro_z;
short acc_data[3];
short gyr_data[3];
float yaw;
int current_t;
int previous_t;
float dt;
static void gpio_init()
{
// GPIO_0 multiplexed to I2C1_SDA
IoTGpioInit(MPU_6050_I2C_SDA);
IoTGpioSetFunc(MPU_6050_I2C_SDA, HI_IO_FUNC_GPIO_0_I2C1_SDA);
// GPIO_1 multiplexed to I2C1_SCL
IoTGpioInit(MPU_6050_I2C_SCL);
IoTGpioSetFunc(MPU_6050_I2C_SCL, HI_IO_FUNC_GPIO_1_I2C1_SCL);
// baudrate: 400kbps
IoTI2cInit(MPU_6050_I2C_NUM, WIFI_IOT_I2C_BAUDRATE);
}
Gyro_Offset GyroOffset;
/***************************************************************
* 函数功能: 得到对应的角度值
* 输入参数: Addr:I2C设备地址
* Reg:目标寄存器
* Value:值
* 返 回 值: 无
* 说 明: 无
**************************************************************/
void mpu_data_convertion()
{
static float fliter_value[2];
float a = 0.8f;
MPU6050ReadAcc(acc_data);
MPU6050ReadGyro(gyr_data);
acc_x = (float)acc_data[0]*acc_range/(float)IMU_RESOLUTION;
acc_y = (float)acc_data[1]*acc_range/(float)IMU_RESOLUTION;
acc_z = (float)acc_data[2]*acc_range/(float)IMU_RESOLUTION;
gyro_x = ((float)gyr_data[0] - GyroOffset.x)*(float)gyro_range/(float)IMU_RESOLUTION;
gyro_y = ((float)gyr_data[1] - GyroOffset.y)*(float)gyro_range/(float)IMU_RESOLUTION;
gyro_z = ((float)gyr_data[2] - GyroOffset.z)*(float)gyro_range/(float)IMU_RESOLUTION;
//一阶低通滤波器
// fliter_value[1] = (float)((1.0f - a) * (float)fliter_value[0] + a*gyro_z);
// fliter_value[0] = fliter_value[1];
// gyro_z = fliter_value[1];
}
/***************************************************************
* 函数功能: 将得到的数据转化为yaw角度
* 输入参数: Addr:I2C设备地址
* Reg:目标寄存器
* Value:值
* 返 回 值: 无
* 说 明: 无
**************************************************************/
float Att_GetYaw()
{
mpu_data_convertion();
current_t=osKernelGetTickCount(); //获取系统时间
dt=(float)(current_t-previous_t) * IMU_UNIT_TIME;//计算时间差,计数器相差的值 * 计数器的单位时间
previous_t=current_t;//更新时间
if(fabs(gyro_z)>0.5f)//略去微小分量
yaw += gyro_z*dt;
yaw = yaw>360.0f? (yaw - 360.0f) : yaw;
yaw = yaw<0.0f? (yaw + 360.0f) : yaw;
return yaw;
}
/***************************************************************
* 函数功能: 通过I2C写入一个值到指定寄存器内
* 输入参数: Addr:I2C设备地址
* Reg:目标寄存器
* Value:值
* 返 回 值: 无
* 说 明: 无
**************************************************************/
static int MPU6050WriteData(uint8_t Reg, uint8_t Value)
{
uint32_t ret;
uint8_t send_data[MPU6050_DATA_2_BYTE] = { Reg, Value };
ret = IoTI2cWrite(MPU_6050_I2C_NUM, (MPU6050_ADDRESS << 1) | 0x00, send_data, sizeof(send_data));
if (ret != 0) {
printf("===== Error: I2C write ret = 0x%x! =====\r\n", ret);
return -1;
}
return 0;
}
/***************************************************************
* 函数功能: 通过I2C写入一段数据到指定寄存器内
* 输入参数: Addr:I2C设备地址
* Reg:目标寄存器
* RegSize:寄存器尺寸(8位或者16位)
* pBuffer:缓冲区指针
* Length:缓冲区长度
* 返 回 值: HAL_StatusTypeDef:操作结果
* 说 明: 在循环调用是需加一定延时时间
**************************************************************/
static int MPU6050WriteBuffer(uint8_t Reg, uint8_t *pBuffer, uint16_t Length)
{
uint32_t ret = 0;
uint8_t send_data[MPU6050_DATA_256_BYTE] = { 0 };
send_data[0] = Reg;
for (int j = 0; j < Length; j++) {
send_data[j + 1] = pBuffer[j];
}
ret = IoTI2cWrite(MPU_6050_I2C_NUM, (MPU6050_ADDRESS << 1) | 0x00, send_data, Length + 1);
if (ret != 0) {
printf("===== Error: I2C write ret = 0x%x! =====\r\n", ret);
return -1;
}
return 0;
}
/***************************************************************
* 函数功能: 通过I2C读取一段寄存器内容存放到指定的缓冲区内
* 输入参数: Addr:I2C设备地址
* Reg:目标寄存器
* RegSize:寄存器尺寸(8位或者16位)
* pBuffer:缓冲区指针
* Length:缓冲区长度
* 返 回 值: HAL_StatusTypeDef:操作结果
* 说 明: 无
**************************************************************/
static int MPU6050ReadBuffer(uint8_t Reg, uint8_t *pBuffer, uint16_t Length)
{
uint32_t ret = 0;
IotI2cData mpu6050_i2c_data = { 0 };
uint8_t buffer[1] = { Reg };
mpu6050_i2c_data.sendBuf = buffer;
mpu6050_i2c_data.sendLen = 1;
mpu6050_i2c_data.receiveBuf = pBuffer;
mpu6050_i2c_data.receiveLen = Length;
ret = IoTI2cWriteread(MPU_6050_I2C_NUM, (MPU6050_ADDRESS << 1) | 0x00, &mpu6050_i2c_data);
if (ret != 0) {
printf("===== Error: I2C writeread ret = 0x%x! =====\r\n", ret);
return -1;
}
return 0;
}
/***************************************************************
* 函数功能: 写数据到MPU6050寄存器
* 输入参数: 无
* 返 回 值: 无
* 说 明: 无
***************************************************************/
static void MPU6050WriteReg(uint8_t reg_add, uint8_t reg_dat)
{
MPU6050WriteData(reg_add, reg_dat);
}
/***************************************************************
* 函数功能: 从MPU6050寄存器读取数据
* 输入参数: 无
* 返 回 值: 无
* 说 明: 无
***************************************************************/
static int MPU6050ReadData(uint8_t reg_add, unsigned char *read, uint8_t num)
{
return MPU6050ReadBuffer(reg_add, read, num);
}
/***************************************************************
* 函数功能: 读取MPU6050的加速度数据
* 输入参数: 无
* 返 回 值: 无
* 说 明: 无
***************************************************************/
int MPU6050ReadAcc(short *accData)
{
int ret;
uint8_t buf[ACCEL_DATA_LEN];
ret = MPU6050ReadData(MPU6050_ACC_OUT, buf, ACCEL_DATA_LEN);
if (ret != 0) {
return -1;
}
accData[ACCEL_X_AXIS] = (buf[ACCEL_X_AXIS_LSB] << SENSOR_DATA_WIDTH_8_BIT) | buf[ACCEL_X_AXIS_MSB];
accData[ACCEL_Y_AXIS] = (buf[ACCEL_Y_AXIS_LSB] << SENSOR_DATA_WIDTH_8_BIT) | buf[ACCEL_Y_AXIS_MSB];
accData[ACCEL_Z_AXIS] = (buf[ACCEL_Z_AXIS_LSB] << SENSOR_DATA_WIDTH_8_BIT) | buf[ACCEL_Z_AXIS_MSB];
return 0;
}
/***************************************************************
* 函数功能: 读取MPU6050的角速度数据
* 输入参数: 无
* 返 回 值: 无
* 说 明: 无
***************************************************************/
int MPU6050ReadGyro(short *gyroData)
{
int ret;
uint8_t buf[ACCEL_DATA_LEN];
ret = MPU6050ReadData(MPU6050_GYRO_OUT, buf, ACCEL_DATA_LEN);
if (ret != 0) {
return -1;
}
gyroData[ACCEL_X_AXIS] = (
没有合适的资源?快使用搜索试试~ 我知道了~
2024年嵌入式竞赛海思赛道参赛资料.zip

共43个文件
c:22个
h:12个
gn:8个

1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 46 浏览量
2025-03-20
00:45:11
上传
评论
收藏 55KB ZIP 举报
温馨提示
《2024年嵌入式竞赛海思赛道参赛资料》是专为全国大学生嵌入式芯片与系统设计竞赛海思赛道准备的宝贵资源。该资料由海思公司提供,旨在推动嵌入式系统及芯片设计领域的发展,激发学生的创新潜能。内容涵盖丰富的开发套件、详尽的文档、实用的视频教程及专家指导,助力参赛者深入掌握嵌入式系统设计与实现的核心技术。通过实践项目,参赛者将有机会提升专业技能,为未来职业生涯奠定坚实基础。请注意,此资源仅供学习使用,切勿用于商业目的。
资源推荐
资源详情
资源评论





























收起资源包目录





















































共 43 条
- 1
资源评论


葡萄籽儿
- 粉丝: 1040
上传资源 快速赚钱
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助


最新资源
- 人工智能计划书.pptx
- 土木工程知识点-CAD平面图导入Sketchup-的注意事项.doc
- 虚拟机课程2CPU虚拟化市公开课获奖课件省名师优质课赛课一等奖课件.ppt
- 学习]网络时代呼唤教育理论创新.ppt
- 东沙小学2016年防范电信网络新型违法犯罪宣传月活动方案(可编辑修改word版).pdf
- 书画用品网络营销策划书.doc
- 输电保护零序电流电气工程及其自动化培训资料.doc
- 系统安全管理.pptx
- 网络安全法相关解读.pptx
- 网络安全宣传周个人工作总结怎么写7篇.docx
- 2022年自动化设备应急预案与故障措施.doc
- 2022年自学考试电气传动与可编程控制器复习资料要点.doc
- 实施网络营销的策略组合.pptx
- 网络优化方案样本.doc
- (完整版)信息技术网络研修总结.doc
- 星级饭店细微服务基本标准.doc
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈



安全验证
文档复制为VIP权益,开通VIP直接复制
