主要变更: - 二轴云台增稳控制 (control_task.c): 保持 Roll=0, Pitch=0 - 双 PID 控制器补偿基座倾斜 - Yaw 电机补偿 Roll 轴,Pitch 电机补偿 Pitch 轴 - 100Hz 控制循环 - Soft I2C 模块 (modules/bus/soft_i2c/): - 基于 GPIO 位时序的 I2C 实现 - 支持多实例,可适配到 i2c_if 接口 - 适配层用于 WIT 传感器 (sensor_i2c_port.c) - MF4010V2 驱动增强: - 添加 mf4010v2_set_angle() 位置控制函数 - 完善命令生成函数文档 Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
107 lines
3.1 KiB
C
107 lines
3.1 KiB
C
/**
|
|
* @file soft_i2c_example.c
|
|
* @brief Soft I2C 使用示例
|
|
* @note 演示如何使用 soft_i2c 模块
|
|
*/
|
|
|
|
#include "soft_i2c.h"
|
|
#include "soft_i2c_hal.h"
|
|
#include "i2c_if.h"
|
|
#include <stdint.h>
|
|
|
|
/* ============================================================================
|
|
* 示例 1: 直接使用 soft_i2c API
|
|
* ============================================================================ */
|
|
|
|
void soft_i2c_example_direct(void)
|
|
{
|
|
soft_i2c_t i2c;
|
|
soft_i2c_config_t config = {
|
|
.sda_gpio_ch = 0, /* 根据实际硬件修改 */
|
|
.scl_gpio_ch = 1,
|
|
.delay_us = 5, /* 100kHz */
|
|
.open_drain = true
|
|
};
|
|
uint8_t data[2];
|
|
|
|
/* 初始化 */
|
|
soft_i2c_init(&i2c, &config);
|
|
|
|
/* 示例:读取 AT24C02 EEPROM (地址 0x50) 寄存器 0x00 的 2 个字节 */
|
|
soft_i2c_mem_read(&i2c, 0x50, 0x00, 1, data, 2, 0);
|
|
|
|
/* 示例:写入 2 个字节 */
|
|
soft_i2c_mem_write(&i2c, 0x50, 0x00, 1, data, 2, 0);
|
|
}
|
|
|
|
/* ============================================================================
|
|
* 示例 2: 使用 HAL 适配器(通过 i2c_if 接口)
|
|
* ============================================================================ */
|
|
|
|
void soft_i2c_example_hal(void)
|
|
{
|
|
uint8_t data[16];
|
|
|
|
/* 初始化 soft_i2c HAL,从通道 2 开始 */
|
|
/* 调用后,通道 2 和 3 对应 soft_i2c 总线 */
|
|
soft_i2c_hal_init(2);
|
|
|
|
/* 现在可以使用标准 i2c_if 接口 */
|
|
/* 读取第一个 soft_i2c 总线上的设备 */
|
|
i2c_if_mem_read(2, 0x50, 0x00, 1, data, 16, 100);
|
|
|
|
/* 读取第二个 soft_i2c 总线上的设备 */
|
|
i2c_if_mem_read(3, 0x68, 0x00, 1, data, 16, 100);
|
|
}
|
|
|
|
/* ============================================================================
|
|
* 示例 3: MPU6050 读取示例
|
|
* ============================================================================ */
|
|
|
|
void soft_i2c_example_mpu6050(soft_i2c_t *i2c)
|
|
{
|
|
uint8_t who_am_i;
|
|
uint8_t data[14];
|
|
|
|
/* 读取 WHO_AM_I 寄存器 (0x75) */
|
|
soft_i2c_mem_read(i2c, 0x68, 0x75, 1, &who_am_i, 1, 0);
|
|
|
|
/* 读取加速度计和陀螺仪数据 (0x3B-0x48) */
|
|
soft_i2c_mem_read(i2c, 0x68, 0x3B, 1, data, 14, 0);
|
|
|
|
/* ACCEL_XOUT_H */
|
|
int16_t accel_x = (int16_t)((data[0] << 8) | data[1]);
|
|
(void)accel_x;
|
|
}
|
|
|
|
/* ============================================================================
|
|
* 示例 4: OLED 显示写入示例 (SSD1306)
|
|
* ============================================================================ */
|
|
|
|
void soft_i2c_example_oled_command(soft_i2c_t *i2c, uint8_t cmd)
|
|
{
|
|
uint8_t buf[2];
|
|
|
|
/* SSD1306 命令模式:第一字节 0x00 = 命令 */
|
|
buf[0] = 0x00;
|
|
buf[1] = cmd;
|
|
|
|
soft_i2c_write(i2c, 0x3C, buf, 2, 0);
|
|
}
|
|
|
|
void soft_i2c_example_oled_data(soft_i2c_t *i2c, const uint8_t *data, uint16_t size)
|
|
{
|
|
uint8_t static_buf[129]; /* 静态缓冲区,最大 128 字节数据 + 1 字节命令 */
|
|
uint16_t i;
|
|
|
|
if (size > 128) return; /* 限制最大长度 */
|
|
|
|
/* SSD1306 数据模式:第一字节 0x40 = 数据 */
|
|
static_buf[0] = 0x40;
|
|
for (i = 0; i < size; i++) {
|
|
static_buf[i + 1] = data[i];
|
|
}
|
|
|
|
soft_i2c_write(i2c, 0x3C, static_buf, size + 1, 0);
|
|
}
|