feat(ADC): 重构采样器初始化为构造函数
将 `sampler::init()` 方法改为构造函数 `sampler()`,并在其中完成 DMA 和 ADC 的初始化配置, 同时进行初始采样操作。 feat(CAN): 更新角度传感器类接口并修复拼写错误 将全局函数 `KHT7823_readAngle()` 替换为 `angle_sensor::readAngle()` 类成员函数调用, 统一命名风格并修正部分函数名中的拼写错误(如 KHT7823 -> KTH7823)。 refactor(Can): 移除冗余的 timeout 成员变量 在 `CAN_transmiter` 类中移除了未使用的 `timeout` 成员变量,减少资源占用。 refactor(Timer): 删除重复注释内容 清理 `Timer.cpp` 中关于定时器配置的重复注释信息,保留核心常量定义。 refactor(ADC): 修正头文件中函数声明 将 `adc.h` 中的 `init()` 函数声明更新为构造函数 `sampler()`,保持实现与声明一致。 refactor(ang_sensor): 将角度传感器相关功能封装为类 将原先分散的 C 风格函数封装进 `angle_sensor` 类中,提升代码结构清晰度和可维护性。 添加构造函数和读取角度的方法,并修正原有函数命名不一致问题。 refactor(main): 调整初始化顺序并移除多余包含 修改 `main.cpp` 中外设初始化顺序,移除对 `core_cm4.h` 的重复包含, 并将角度传感器和采样器通过类实例方式初始化。
This commit is contained in:
@@ -109,9 +109,10 @@ void adc_config(void) {
|
|||||||
/**
|
/**
|
||||||
* @brief 采样类初始化
|
* @brief 采样类初始化
|
||||||
*/
|
*/
|
||||||
void sampler::init() {
|
sampler::sampler() {
|
||||||
dma_config(); // DMA采样ADC初始化
|
dma_config(); // DMA采样ADC初始化
|
||||||
adc_config(); // ADC初始化
|
adc_config(); // ADC初始化
|
||||||
|
// 初始采样
|
||||||
for (int i = 0; i < FILTER_SIZE + 1; i++) {
|
for (int i = 0; i < FILTER_SIZE + 1; i++) {
|
||||||
mcu_temp_samples[i] =
|
mcu_temp_samples[i] =
|
||||||
(1.43f - adc_value[0] * ADC_SCALE) * TEMP_FACTOR + 35.0f;
|
(1.43f - adc_value[0] * ADC_SCALE) * TEMP_FACTOR + 35.0f;
|
||||||
|
|||||||
@@ -22,6 +22,7 @@
|
|||||||
extern CAN_transmiter CAN0_transmiter;
|
extern CAN_transmiter CAN0_transmiter;
|
||||||
extern FOC_Controlroller FOC;
|
extern FOC_Controlroller FOC;
|
||||||
extern sampler sampler;
|
extern sampler sampler;
|
||||||
|
extern angle_sensor KTH7823;
|
||||||
|
|
||||||
// 添加DMA相关变量
|
// 添加DMA相关变量
|
||||||
dma_parameter_struct dma_rx_parameter;
|
dma_parameter_struct dma_rx_parameter;
|
||||||
@@ -346,7 +347,7 @@ void canfd_loopback_test(void) {
|
|||||||
void can_loopback_test(void) {
|
void can_loopback_test(void) {
|
||||||
can_trasnmit_message_struct transmit_message;
|
can_trasnmit_message_struct transmit_message;
|
||||||
can_receive_message_struct receive_message;
|
can_receive_message_struct receive_message;
|
||||||
uint32_t timeout = 0xF;
|
uint32_t timeout = 0xFF;
|
||||||
uint8_t transmit_mailbox = 0;
|
uint8_t transmit_mailbox = 0;
|
||||||
|
|
||||||
std::mt19937 gen(rand()); // 使用随机设备生成的种子
|
std::mt19937 gen(rand()); // 使用随机设备生成的种子
|
||||||
@@ -429,7 +430,7 @@ void CAN_transmiter::update(void) {
|
|||||||
mcu_temp = sampler.get_mcu_temp() * 100;
|
mcu_temp = sampler.get_mcu_temp() * 100;
|
||||||
motor_temp = sampler.get_drv_temp() * 100;
|
motor_temp = sampler.get_drv_temp() * 100;
|
||||||
Power_Voltage = sampler.get_vbus_voltage() * 1000;
|
Power_Voltage = sampler.get_vbus_voltage() * 1000;
|
||||||
rt_angle_union.f = KHT7823_readAngle();
|
rt_angle_union.f = KTH7823.readAngle();
|
||||||
rt_velocity_union.f = 0.0f;
|
rt_velocity_union.f = 0.0f;
|
||||||
Status_code = (gpio_input_bit_get(GPIOB, GPIO_PIN_2) == SET) ? 0 : 1;
|
Status_code = (gpio_input_bit_get(GPIOB, GPIO_PIN_2) == SET) ? 0 : 1;
|
||||||
|
|
||||||
|
|||||||
@@ -89,7 +89,6 @@ class CAN_transmiter {
|
|||||||
|
|
||||||
can_trasnmit_message_struct transmit_message; // 发送消息结构体
|
can_trasnmit_message_struct transmit_message; // 发送消息结构体
|
||||||
u8 transmit_mailbox = CAN_NOMAILBOX; // 初始化为无mailbox状态
|
u8 transmit_mailbox = CAN_NOMAILBOX; // 初始化为无mailbox状态
|
||||||
u32 timeout = 0XFFFF; // 默认超时为0xFFFF
|
|
||||||
u8 transmit_data[64]; // CANFD最大64字节
|
u8 transmit_data[64]; // CANFD最大64字节
|
||||||
|
|
||||||
void update(); // 更新数据
|
void update(); // 更新数据
|
||||||
|
|||||||
@@ -12,10 +12,6 @@
|
|||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
// 配置定时器每100微秒中断一次
|
|
||||||
// 系统时钟为120MHz,要得到100微秒的中断周期
|
|
||||||
// 可以使用预分频器119,定时器时钟为120MHz/(119+1) = 1MHz
|
|
||||||
// 然后设置周期为99,即计数100次,1MHz/100 = 10kHz = 100微秒
|
|
||||||
constexpr int TIMER2_PRESCALER = 119; // 120MHz / 120 = 1MHz
|
constexpr int TIMER2_PRESCALER = 119; // 120MHz / 120 = 1MHz
|
||||||
constexpr int TIMER2_PERIOD = 99; // 1MHz / 100 = 100us
|
constexpr int TIMER2_PERIOD = 99; // 1MHz / 100 = 100us
|
||||||
constexpr int TIMER3_PRESCALER = 119; // 120MHz / 120 = 1MHz
|
constexpr int TIMER3_PRESCALER = 119; // 120MHz / 120 = 1MHz
|
||||||
|
|||||||
@@ -19,7 +19,7 @@ class sampler {
|
|||||||
float tempora_sample;
|
float tempora_sample;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
void init();
|
sampler();
|
||||||
void update_mcu_temp(void);
|
void update_mcu_temp(void);
|
||||||
void update_drv_temp(void);
|
void update_drv_temp(void);
|
||||||
void update_vbus_voltage(void);
|
void update_vbus_voltage(void);
|
||||||
|
|||||||
@@ -4,7 +4,6 @@
|
|||||||
#include "gd32c10x_spi.h"
|
#include "gd32c10x_spi.h"
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
|
||||||
|
|
||||||
#define SPI_CS_ENABLE() gpio_bit_reset(GPIOA, GPIO_PIN_15)
|
#define SPI_CS_ENABLE() gpio_bit_reset(GPIOA, GPIO_PIN_15)
|
||||||
#define SPI_CS_DISABLE() gpio_bit_set(GPIOA, GPIO_PIN_15)
|
#define SPI_CS_DISABLE() gpio_bit_set(GPIOA, GPIO_PIN_15)
|
||||||
|
|
||||||
@@ -179,7 +178,7 @@ extern "C" void DMA0_Channel1_IRQHandler(void) {
|
|||||||
* @brief 角度传感器初始化
|
* @brief 角度传感器初始化
|
||||||
* @param 无
|
* @param 无
|
||||||
*/
|
*/
|
||||||
void KHT7823_gpio_init(void) {
|
void KTH7823_gpio_init(void) {
|
||||||
rcu_periph_clock_enable(RCU_GPIOA);
|
rcu_periph_clock_enable(RCU_GPIOA);
|
||||||
rcu_periph_clock_enable(RCU_GPIOB);
|
rcu_periph_clock_enable(RCU_GPIOB);
|
||||||
rcu_periph_clock_enable(RCU_SPI0);
|
rcu_periph_clock_enable(RCU_SPI0);
|
||||||
@@ -204,7 +203,7 @@ void KHT7823_gpio_init(void) {
|
|||||||
*
|
*
|
||||||
* @details Configures SPI hardware and initializes necessary settings
|
* @details Configures SPI hardware and initializes necessary settings
|
||||||
*/
|
*/
|
||||||
void KHT7823_spi_init(void) {
|
void KTH7823_spi_init(void) {
|
||||||
spi_parameter_struct spi_init_str;
|
spi_parameter_struct spi_init_str;
|
||||||
spi_struct_para_init(&spi_init_str);
|
spi_struct_para_init(&spi_init_str);
|
||||||
|
|
||||||
@@ -224,27 +223,34 @@ void KHT7823_spi_init(void) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief KHT7823 初始化
|
* @brief KTH7823 初始化
|
||||||
*/
|
*/
|
||||||
void KHT7823_init(void) {
|
void angle_sensor::KTH7823_init(void) {
|
||||||
KHT7823_gpio_init(); // GPIO初始化
|
KTH7823_gpio_init(); // GPIO初始化
|
||||||
KHT7823_spi_init(); // SPI初始化
|
KTH7823_spi_init(); // SPI初始化
|
||||||
spi_dma_init(); // DMA初始化
|
spi_dma_init(); // DMA初始化
|
||||||
}
|
}
|
||||||
|
/**
|
||||||
|
* @brief 角度传感器构造函数
|
||||||
|
*/
|
||||||
|
angle_sensor::angle_sensor(void) { KTH7823_init(); }
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief 读取传感器数据
|
* @brief 读取传感器数据
|
||||||
* @return 传感器数据
|
* @return 传感器数据
|
||||||
*/
|
*/
|
||||||
float KHT7823_readAngle() {
|
float angle_sensor::readAngle(void) {
|
||||||
|
u16 u16data = FAULT_RECEIVE;
|
||||||
|
|
||||||
SPI_CS_ENABLE();
|
SPI_CS_ENABLE();
|
||||||
// 使用DMA发送命令并接收数据
|
// 使用DMA发送命令并接收数据
|
||||||
u16 u16data;
|
|
||||||
if (spi_rw_half_word_dma(SPI0, DEFAULT_COMMAND, &u16data) == 0) {
|
if (spi_rw_half_word_dma(SPI0, DEFAULT_COMMAND, &u16data) == 0) {
|
||||||
SPI_CS_DISABLE();
|
SPI_CS_DISABLE();
|
||||||
return u16data * ANGLE_FACTOR;
|
return u16data * ANGLE_FACTOR;
|
||||||
} else {
|
} else {
|
||||||
SPI_CS_DISABLE();
|
SPI_CS_DISABLE();
|
||||||
|
// 处理读取错误
|
||||||
|
printf("Error reading angle from sensor.\n");
|
||||||
return FAULT_RECEIVE; // 错误返回
|
return FAULT_RECEIVE; // 错误返回
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,15 +1,24 @@
|
|||||||
#ifndef _angsensor_H
|
#ifndef _angsensor_H
|
||||||
#define _angsensor_H
|
#define _angsensor_H
|
||||||
|
#include <cstdio>
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
extern "C" {
|
extern "C" {
|
||||||
#endif
|
#endif
|
||||||
#include "gd32c10x_eval.h"
|
#include "gd32c10x_eval.h"
|
||||||
#include "stdio.h"
|
#include "stdio.h"
|
||||||
|
|
||||||
void KHT7823_init(void);
|
|
||||||
u16 spi_rw_half_word(u32 spi_periph, const u16 i_HalfWord);
|
u16 spi_rw_half_word(u32 spi_periph, const u16 i_HalfWord);
|
||||||
float KHT7823_readAngle(void);
|
int spi_rw_half_word_dma(uint32_t spi_periph, uint16_t tx_data,
|
||||||
int spi_rw_half_word_dma(uint32_t spi_periph, uint16_t tx_data, uint16_t* rx_data);
|
uint16_t *rx_data);
|
||||||
|
|
||||||
|
class angle_sensor {
|
||||||
|
public:
|
||||||
|
angle_sensor();
|
||||||
|
float readAngle(void);
|
||||||
|
|
||||||
|
protected:
|
||||||
|
void KTH7823_init(void);
|
||||||
|
};
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
|
|||||||
+9
-11
@@ -3,29 +3,27 @@
|
|||||||
#include "DengFOC.h"
|
#include "DengFOC.h"
|
||||||
#include "Timer.h"
|
#include "Timer.h"
|
||||||
#include "adc.h"
|
#include "adc.h"
|
||||||
#include "core_cm4.h"
|
|
||||||
#include "ang_sensor.h"
|
#include "ang_sensor.h"
|
||||||
|
#include "core_cm4.h"
|
||||||
#include "initial.h"
|
#include "initial.h"
|
||||||
#include "systick.h"
|
#include "systick.h"
|
||||||
|
|
||||||
// 全局变量定义
|
// 全局变量定义
|
||||||
CAN_transmiter CAN0_transmiter; //CAN初始化
|
CAN_transmiter CAN0_transmiter; // CAN初始化
|
||||||
FOC_Controlroller FOC; // FOC控制初始化
|
FOC_Controlroller FOC; // FOC控制初始化
|
||||||
sampler sampler; // 采样器初始化
|
sampler sampler; // 采样器初始化
|
||||||
|
angle_sensor KTH7823; // 角度传感器初始化
|
||||||
|
|
||||||
int main(void) {
|
int main(void) {
|
||||||
systick_config(); // 系统时钟初始化
|
systick_config(); // 系统时钟初始化
|
||||||
USART0_init(); // 调试串口初始化
|
USART0_init(); // 调试串口初始化
|
||||||
User_timer_init(); // Timer初始化
|
User_timer_init(); // Timer初始化
|
||||||
sampler.init(); // ADC采样器初始化
|
driver_init(); // 电机驱动初始化
|
||||||
KHT7823_init(); // 角度传感器初始化
|
watchdog_init(5000); // 看门狗初始化
|
||||||
driver_init(); // 电机驱动初始化
|
|
||||||
|
|
||||||
watchdog_init(5000);
|
|
||||||
|
|
||||||
while (1) {
|
while (1) {
|
||||||
// delay_1ms(100);
|
// delay_1ms(100);
|
||||||
fwdgt_counter_reload(); // 看门狗复位
|
fwdgt_counter_reload(); // 看门狗复位
|
||||||
CAN0_transmiter.communication_check(); // CAN通信
|
CAN0_transmiter.communication_check(); // CAN通信
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user