From 71ef7cdd2097133dda9c7848626ca3f45ae95bcf Mon Sep 17 00:00:00 2001 From: lhye200 Date: Sun, 19 Oct 2025 19:08:23 +0800 Subject: [PATCH] =?UTF-8?q?esp=E5=8A=A0=E5=85=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- code/esp8266.c | 208 ++++++++++++++++++++++++ code/esp8266.h | 33 ++++ code/filter.c | 238 +++++++++++++++++++++++++--- code/filter.h | 41 ++++- code/ina226.c | 33 ++-- code/ina226.h | 10 +- code/power_ctrl.c | 223 ++++++++++++++++++++++++++ code/power_ctrl.h | 20 +++ code/power_out.c | 13 +- code/power_out.h | 17 +- code/vofa_client.c | 11 +- code/vofa_client.h | 2 +- esp8266/esp8266/esp8266.ino | 97 ++++++++++++ libraries/zf_driver/zf_driver_pwm.h | 2 +- user/cpu0_main.c | 66 ++++---- user/isr.c | 31 +++- 16 files changed, 948 insertions(+), 97 deletions(-) create mode 100644 code/esp8266.c create mode 100644 code/esp8266.h create mode 100644 code/power_ctrl.c create mode 100644 code/power_ctrl.h create mode 100644 esp8266/esp8266/esp8266.ino diff --git a/code/esp8266.c b/code/esp8266.c new file mode 100644 index 0000000..74033f0 --- /dev/null +++ b/code/esp8266.c @@ -0,0 +1,208 @@ +/* + * esp8266.c + * + * Created on: 2025年10月18日 + * Author: LHYe200 + */ + + +#include "esp8266.h" +#include "zf_driver_uart.h" +#include "zf_common_fifo.h" +#include "zf_driver_gpio.h" +#include "zf_driver_delay.h" +#include "status_led.h" + +const uint8 ESP8266_syncPkt[44] = {0x00, 0x08, 0x24, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0x07, 0x12, 0x20, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55}; +static uint8 syncPkt_index = 0; +static uint8 esp8266_quit_download_judge_buffer[2] = {0}; +static uint8 esp8266_quit_download_judge_index = 0; +static uint8 esp8266_quit_download_judge_flag = 0; +static uint8 esp8266_init_flag = 0; +uint8 esp8266_download_passthrough = 0; +static uint8 esp8266_download_rst_flag = 0; + +// static uint16 esp8266_uart_pass_tx_buff_index = 0; +// static uint16 esp8266_uart_pass_rx_buff_index = 0; + + +void isr_uart0_rx_interrupt_hook_back(uint8 rev_data); + +// // fifo_struct esp8266_rx_fifo; +// #define EU_BUFFER_SIZE 1024 +// uint8 esp8266_rx_buffer[EU_BUFFER_SIZE]; + +// // fifo_struct esp8266_tx_fifo; +// uint8 esp8266_tx_buffer[EU_BUFFER_SIZE]; + +void ESP8266_Init(void) +{ + uart_init(ESP8266_UART_CHANNEL, ESP8266_UART_BAUDRATE, ESP8266_UART_TX_PIN, ESP8266_UART_RX_PIN); + uart_rx_interrupt(ESP8266_UART_CHANNEL, 1); +// fifo_init(&esp8266_rx_fifo, FIFO_DATA_8BIT, esp8266_rx_buffer, sizeof(esp8266_rx_buffer)); + gpio_init(ESP8266_EN, GPO, 1, GPO_PUSH_PULL); + gpio_init(ESP8266_RST, GPO, 1, GPO_PUSH_PULL); + gpio_init(ESP8266_IO0, GPO, 1, GPO_PUSH_PULL); + gpio_init(ESP8266_IO2, GPO, 1, GPO_PUSH_PULL); + gpio_init(ESP8266_IO15, GPO, 0, GPO_PUSH_PULL); + + esp8266_init_flag = 1; +} + +void ESP8266_send_data(uint8 *data, uint32 len) +{ + if(esp8266_download_passthrough) + { + return; + } + uart_write_buffer(ESP8266_UART_CHANNEL, data, len); +} + +void ESP8266_Main_Loop_Change_Mode(void) +{ + if(esp8266_quit_download_judge_flag >= 4) + { + esp8266_download_passthrough = 0; + esp8266_quit_download_judge_flag = 0; + esp8266_download_rst_flag = 0; + gpio_set_level(ESP8266_IO0, 1); + gpio_set_level(ESP8266_IO2, 1); + gpio_set_level(ESP8266_IO15, 0); + gpio_set_level(ESP8266_EN, 0); + // gpio_set_level(ESP8266_RST, 0); + system_delay_ms(1); + // gpio_set_level(ESP8266_RST, 1); + gpio_set_level(ESP8266_EN, 1); + OFF_LED(STATUS_LED_9); + } + if(esp8266_download_rst_flag >=3 && !esp8266_download_passthrough) + { + system_delay_ms(1); + // gpio_set_level(ESP8266_RST, 1); + gpio_set_level(ESP8266_EN, 1); + esp8266_download_passthrough = 1; + esp8266_download_rst_flag = 0; + ON_LED(STATUS_LED_2); + ON_LED(STATUS_LED_9); + } +} + +void ESP8266_Judge_Quit_Download_Mode(uint8 rev_data) +{ + if(esp8266_download_passthrough && !esp8266_quit_download_judge_flag) + { + + if(rev_data == 0xC0 && esp8266_quit_download_judge_buffer[0] != 0xC0) + { + esp8266_quit_download_judge_index = 0; + } + esp8266_quit_download_judge_buffer[esp8266_quit_download_judge_index++] = rev_data; + if(esp8266_quit_download_judge_index >= 2 && esp8266_quit_download_judge_buffer[1] == 0x04) + { + esp8266_quit_download_judge_index = 0; + esp8266_quit_download_judge_flag = 1; + ON_LED(STATUS_LED_6); + } + } + if(esp8266_quit_download_judge_flag == 1 && rev_data == 0xC0) + { + esp8266_quit_download_judge_flag = 2; + ON_LED(STATUS_LED_7); + } +} + +void ESP8266_Auto_Download_Uart_Hook(void) +{ + uint8 rev_tmp; + while(uart_query_byte(UART_0, &rev_tmp)) + { + if((rev_tmp == ESP8266_syncPkt[syncPkt_index]) && (esp8266_download_passthrough == 0) && esp8266_init_flag) + { + Flash_LED(STATUS_LED_3); + syncPkt_index++; + if(syncPkt_index >= 44) + { + // if(!esp8266_download_rst_flag) + // { + gpio_set_level(ESP8266_IO0, 0); + gpio_set_level(ESP8266_IO2, 1); + gpio_set_level(ESP8266_IO15, 0); + // gpio_set_level(ESP8266_RST, 0); + gpio_set_level(ESP8266_EN, 0); + esp8266_download_rst_flag++; + ON_LED(STATUS_LED_1); + // } + // else + // { + // gpio_set_level(ESP8266_RST, 1); + // esp8266_download_passthrough = 1; + // ON_LED(STATUS_LED_2); + // } + syncPkt_index = 0; + } + } + else + { + syncPkt_index = 0; + } + if(esp8266_download_passthrough == 0) + { + isr_uart0_rx_interrupt_hook_back(rev_tmp); + } + else + { + // esp8266_tx_buffer[esp8266_uart_pass_tx_buff_index++] = rev_tmp; + // if(esp8266_uart_pass_tx_buff_index >= EU_BUFFER_SIZE) + // { + // uart_write_buffer(ESP8266_UART_CHANNEL, esp8266_tx_buffer, esp8266_uart_pass_tx_buff_index); + // esp8266_uart_pass_tx_buff_index = 0; + // } + // if(rev_tmp == 0xC0) + // { + // uart_write_buffer(ESP8266_UART_CHANNEL, esp8266_tx_buffer, esp8266_uart_pass_tx_buff_index); + // esp8266_uart_pass_tx_buff_index = 0; + // } + uart_write_byte(ESP8266_UART_CHANNEL, rev_tmp); + // ON_LED(STATUS_LED_3); + // ESP8266_Judge_Quit_Download_Mode(rev_tmp); + } + } +} + +void ESP8266_Uart_Callback(void) +{ + uint8 rev_tmp; + // ON_LED(STATUS_LED_4); + while(uart_query_byte(ESP8266_UART_CHANNEL, &rev_tmp)) + { + if(esp8266_download_passthrough) + { + // esp8266_rx_buffer[esp8266_uart_pass_rx_buff_index++] = rev_tmp; + // if(esp8266_uart_pass_rx_buff_index >= EU_BUFFER_SIZE) + // { + // uart_write_buffer(UART_0, esp8266_rx_buffer, esp8266_uart_pass_rx_buff_index); + // esp8266_uart_pass_rx_buff_index = 0; + // } + // if(rev_tmp == 0xC0) + // { + // uart_write_buffer(UART_0, esp8266_rx_buffer, esp8266_uart_pass_rx_buff_index); + // esp8266_uart_pass_rx_buff_index = 0; + // } + uart_write_byte(UART_0, rev_tmp); + // Flash_LED(STATUS_LED_5); + if(esp8266_quit_download_judge_flag >= 2) + { + if(rev_tmp == 0xC0) + { + ON_LED(STATUS_LED_8); + esp8266_quit_download_judge_flag ++; + } + } + } + else + { + // 处理其他数据 + + } + } +} diff --git a/code/esp8266.h b/code/esp8266.h new file mode 100644 index 0000000..7e1e53c --- /dev/null +++ b/code/esp8266.h @@ -0,0 +1,33 @@ +/* + * esp8266.h + * + * Created on: 2025年10月18日 + * Author: LHYe200 + */ + +#ifndef CODE_ESP8266_H_ +#define CODE_ESP8266_H_ + +#include "zf_common_typedef.h" + +#define ESP8266_IO0 P20_9 +#define ESP8266_IO2 P20_8 +#define ESP8266_IO15 P20_7 +#define ESP8266_RST P20_3 +#define ESP8266_EN P20_6 + +#define ESP8266_UART_CHANNEL UART_2 +#define ESP8266_UART_BAUDRATE 115200 +#define ESP8266_UART_TX_PIN UART2_TX_P33_9 +#define ESP8266_UART_RX_PIN UART2_RX_P33_8 + + +extern uint8 esp8266_download_passthrough; + +void ESP8266_Init(void); +void ESP8266_send_data(uint8 *data, uint32 len); +void ESP8266_Auto_Download_Uart_Hook(void); +void ESP8266_Uart_Callback(void); +void ESP8266_Main_Loop_Change_Mode(void); + +#endif /* CODE_ESP8266_H_ */ diff --git a/code/filter.c b/code/filter.c index bd6b0fe..13d0342 100644 --- a/code/filter.c +++ b/code/filter.c @@ -7,7 +7,9 @@ */ #include "filter.h" - +#include "math.h" +#include +#include /******************************************************************************* * @fn Init_lowPass_alpha @@ -18,11 +20,11 @@ ******************************************************************************/ void Init_lowPass_alpha(low_pass_filter_t* const filter,const float ts, const float fc) { - float b=2*M_PI*fc*ts; - filter->ts=ts; - filter->fc=fc; - filter->lastYn=0; - filter->alpha=b/(b+1); + float b=2*M_PI*fc*ts; + filter->ts=ts; + filter->fc=fc; + filter->lastYn=0; + filter->alpha=b/(b+1); } /******************************************************************************* @@ -31,11 +33,11 @@ void Init_lowPass_alpha(low_pass_filter_t* const filter,const float ts, const fl * @param data 采样数据 * @return 滤波结果 ******************************************************************************/ -float Low_pass_filter(low_pass_filter_t* const filter, const float data) +float Low_pass_filter(low_pass_filter_t* const filter, float data) { - float tem=filter->lastYn+(filter->alpha*(data-filter->lastYn)); - filter->lastYn=tem; - return tem; + float tem=filter->lastYn+(filter->alpha*(data-filter->lastYn)); + filter->lastYn=tem; + return tem; } /******************************************************************************* @@ -47,12 +49,12 @@ float Low_pass_filter(low_pass_filter_t* const filter, const float data) ******************************************************************************/ void Init_hightPass_alpha(hight_pass_filter_t* const filter,const float ts, const float fc) { - float b=2*M_PI*fc*ts; - filter->ts=ts; - filter->fc=fc; - filter->lastYn=0; - filter->lastXn=0; - filter->alpha=1/(1+b); + float b=2*M_PI*fc*ts; + filter->ts=ts; + filter->fc=fc; + filter->lastYn=0; + filter->lastXn=0; + filter->alpha=1/(1+b); } /******************************************************************************* @@ -61,11 +63,203 @@ void Init_hightPass_alpha(hight_pass_filter_t* const filter,const float ts, cons * @param data 采样数据 * @return 滤波结果 ******************************************************************************/ -float Hight_pass_filter(hight_pass_filter_t* const filter, const float data) +float Hight_pass_filter(hight_pass_filter_t* const filter, float data) { - float tem=((filter->alpha)*(filter->lastYn))+((filter->alpha)*(data-(filter->lastXn))); - filter->lastYn=tem; - filter->lastXn=data; - return tem; - + float tem=((filter->alpha)*(filter->lastYn))+((filter->alpha)*(data-(filter->lastXn))); + filter->lastYn=tem; + filter->lastXn=data; + return tem; +} + +/******************************************************************************* + * @fn Moving_Average_filter_init + * @brief 初始化滑动平均滤波器 + * @param filter 滤波器结构体指针 + * @param size 窗口大小 + * @return void + ******************************************************************************/ +void Moving_Average_filter_init(moving_average_filter_t* const filter, int size) +{ + filter->size = size; + filter->avg = 0.0f; + filter->window = (float*)malloc(size * sizeof(float)); + memset(filter->window, 0, size * sizeof(float)); +} + +/******************************************************************************* + * @fn Moving_Average_filter + * @brief 滑动平均滤波器实现 + * @param data 输入数据数组 + * @param size 数据数组的大小 + * @return 返回滑动平均值 + * @note 适用于平滑随机噪声信号。 + ******************************************************************************/ +float Moving_Average_filter(moving_average_filter_t* const filter, float newData) +{ + float sum = 0.0f; + // 移动窗口 + for(int i = 0; i < filter->size - 1; i++) + { + filter->window[i] = filter->window[i + 1]; + sum += filter->window[i]; + } + filter->window[filter->size - 1] = newData; + sum += filter->window[filter->size - 1]; + // 计算平均值 + filter->avg = sum / filter->size; + return filter->avg; +} + + +/******************************************************************************* + * @fn Kalman_filter_init + * @brief 初始化卡尔曼滤波器状态与参数 + * @param filter 卡尔曼滤波器结构体指针 + * @param q 过程噪声方差(过程噪声协方差Q) + * @param r 测量噪声方差(测量噪声协方差R) + * @param kGain 初始卡尔曼增益(可设为0或估计值) + * @return void + * @note prevData 初始为0;p 初始协方差设为1。本函数仅做初始状态配置,适用于一维单变量卡尔曼滤波结构。 + ******************************************************************************/ +void Kalman_filter_init(Kalman_filter_t* const filter, const float q, const float r) +{ + filter->prevData = 0; + filter->p = 1; + filter->q = q; + filter->r = r; + filter->kGain = 0; +} + +/******************************************************************************* + * @fn Kalman_filter + * @brief 一维卡尔曼滤波器实现(单变量增量式更新) + * @param filter 卡尔曼滤波器结构体指针,包含状态 prevData,协方差 p,噪声q/r 和当前增益 kGain + * @param inData 本次测量输入值 + * @return 返回滤波后的估计值(浮点型) + * @note 算法步骤: + * 1) 预测协方差 p <- p + q + * 2) 计算卡尔曼增益 k <- p / (p + r) + * 3) 使用增益修正估计值 x <- x_prev + k*(z - x_prev) + * 4) 更新协方差 p <- (1 - k) * p + * 5) 保存本次估计为 prevData 并返回 + * 该实现适用于一维信号的在线滤波,未包含状态转移矩阵或控制输入。 + ******************************************************************************/ +float Kalman_filter(Kalman_filter_t* const filter, float inData) +{ + filter->p = filter->p + filter->q; + filter->kGain = filter->p / ( filter->p + filter->r ); //计算卡尔曼增益 + inData = filter->prevData + ( filter->kGain * ( inData - filter->prevData ) ); //计算本次滤波估计值 + filter->p = ( 1 - filter->kGain ) * filter->p; //更新测量方差 + filter->prevData = inData; + return inData; //返回滤波值 +} + + +/******************************************************************************* + * @fn Unscented_Kalman_filter_init + * @brief 初始化无迹卡尔曼滤波器状态与参数 + * @param filter 无迹卡尔曼滤波器结构体指针 + * @param q 过程噪声协方差 + * @param r 测量噪声协方差 + * @return void + * @note prevData 初始为0;p 初始协方差设为1。 + ******************************************************************************/ +void Unscented_Kalman_filter_init(Kalman_filter_t* const filter, const float q, const float r) +{ + filter->prevData = 0; + filter->p = 1; + filter->q = q; + filter->r = r; + filter->kGain = 0; +} + +/******************************************************************************* + * @fn Unscented_Kalman_filter + * @brief 一维无迹卡尔曼滤波器实现 + * @param filter 卡尔曼滤波器结构体指针,包含状态、协方差、噪声参数等 + * @param inData 本次测量输入值 + * @return 返回滤波后的估计值(浮点型) + * @note 使用无迹变换处理非线性系统,适合复杂非线性信号。 + ******************************************************************************/ +float Unscented_Kalman_filter(Kalman_filter_t* const filter, float inData) +{ + // 无迹变换步骤 + float sigmaPoints[3]; + sigmaPoints[0] = filter->prevData; + sigmaPoints[1] = filter->prevData + sqrtf(filter->p + filter->q); + sigmaPoints[2] = filter->prevData - sqrtf(filter->p + filter->q); + + // 预测均值 + float predictedMean = (sigmaPoints[0] + sigmaPoints[1] + sigmaPoints[2]) / 3.0; + + // 预测协方差 + float predictedCov = filter->q + ((sigmaPoints[1] - predictedMean) * (sigmaPoints[1] - predictedMean) + + (sigmaPoints[2] - predictedMean) * (sigmaPoints[2] - predictedMean)) / 2.0; + + // 更新卡尔曼增益 + filter->kGain = predictedCov / (predictedCov + filter->r); + + // 更新估计值 + float updatedMean = predictedMean + filter->kGain * (inData - predictedMean); + + // 更新协方差 + filter->p = (1 - filter->kGain) * predictedCov; + + // 保存状态 + filter->prevData = updatedMean; + + return updatedMean; +} + + + +/******************************************************************************* + * @fn Adaptive_Kalman_filter_init + * @brief 初始化自适应卡尔曼滤波器状态与参数 + * @param filter 自适应卡尔曼滤波器结构体指针 + * @param q 初始过程噪声协方差 + * @param r 测量噪声协方差 + * @return void + * @note prevData 初始为0;p 初始协方差设为1。 + ******************************************************************************/ +void Adaptive_Kalman_filter_init(Kalman_filter_t* const filter, const float q, const float r) +{ + filter->prevData = 0; + filter->p = 1; + filter->q = q; + filter->r = r; + filter->kGain = 0; +} + +/******************************************************************************* + * @fn Adaptive_Kalman_filter + * @brief 一维自适应卡尔曼滤波器实现 + * @param filter 卡尔曼滤波器结构体指针,包含状态、协方差、噪声参数等 + * @param inData 本次测量输入值 + * @return 返回滤波后的估计值(浮点型) + * @note 动态调整噪声协方差,适合噪声特性变化的信号。 + ******************************************************************************/ +float Adaptive_Kalman_filter(Kalman_filter_t* const filter, float inData) +{ + // 更新过程噪声协方差 Q 和测量噪声协方差 R + float innovation = inData - filter->prevData; + float alpha = fabsf(innovation) > 1.0f ? 0.2f : 0.8f; // 根据创新调整 ALPHA + filter->q = alpha * filter->q + (1 - alpha) * (innovation * innovation); + + // 预测协方差 + filter->p = filter->p + filter->q; + + // 更新卡尔曼增益 + filter->kGain = filter->p / (filter->p + filter->r); + + // 更新估计值 + float updatedData = filter->prevData + filter->kGain * innovation; + + // 更新协方差 + filter->p = (1 - filter->kGain) * filter->p; + + // 保存状态 + filter->prevData = updatedData; + + return updatedData; } diff --git a/code/filter.h b/code/filter.h index 0566762..a1bcc38 100644 --- a/code/filter.h +++ b/code/filter.h @@ -31,17 +31,46 @@ typedef struct float alpha; //滤波系数 } hight_pass_filter_t; +typedef struct +{ + float avg; // 滑动平均值 + int size; // 窗口大小 + float *window; // 存储窗口数据 +} moving_average_filter_t; + +typedef struct +{ + float prevData; // 先前数值 + float p; // 估计协方差 + float q; // 过程噪声协方差 + float r; // 测量噪声协方差 + float kGain; // 卡尔曼增益 +} Kalman_filter_t; -//初始化滤波系数 +// 低通滤波器 void Init_lowPass_alpha(low_pass_filter_t* const filter,const float ts, const float fc); -//低通滤波 -float Low_pass_filter(low_pass_filter_t* const filter, const float data); +float Low_pass_filter(low_pass_filter_t* const filter, float data); -//初始化滤波系数 +// 高通滤波器 void Init_hightPass_alpha(hight_pass_filter_t* const filter,const float ts, const float fc); -//低通滤波 -float Hight_pass_filter(hight_pass_filter_t* const filter, const float data); +float Hight_pass_filter(hight_pass_filter_t* const filter, float data); + +// 滑动平均滤波器 +void Moving_Average_filter_init(moving_average_filter_t* const filter, int size); +float Moving_Average_filter(moving_average_filter_t* const filter, float newData); + +// 卡尔曼滤波器 +void Kalman_filter_init(Kalman_filter_t* const filter, const float q, const float r); +float Kalman_filter(Kalman_filter_t* const filter, float inData); + +// 无迹卡尔曼滤波器 +void Unscented_Kalman_filter_init(Kalman_filter_t* const filter, const float q, const float r); +float Unscented_Kalman_filter(Kalman_filter_t* const filter, float inData); + +// 自适应卡尔曼滤波器 +void Adaptive_Kalman_filter_init(Kalman_filter_t* const filter, const float q, const float r); +float Adaptive_Kalman_filter(Kalman_filter_t* const filter, float inData); #endif /* CODE_FILTER_H_ */ diff --git a/code/ina226.c b/code/ina226.c index e60eb0e..1438c5c 100644 --- a/code/ina226.c +++ b/code/ina226.c @@ -16,19 +16,19 @@ INA226_t ina226[INA226_NUM]; static soft_iic_info_struct ina226_soft_iic_config; -void INA226_Init() +void INA226_Init(void) { uint8 addr_list[INA226_NUM] = INA226_ADDR; uint8 mode[2] = INA226_DEF_MODE; - float max_current_A = 20.0f; - float rshunt_mOhm = 10.0f; - float amp_fix_k = 1.0f; - float vot_fix_k = 1.0f; + float max_current_A = 30.0f; + float rshunt_mOhm = 2.0f; + float vot_fix_k[INA226_NUM] = INA226_VOT_FIX_K; + float amp_fix_k[INA226_NUM] = INA226_AMP_FIX_K; uint8 counter; for(counter = 0; counter < INA226_NUM; counter++) { - INA226_Single_Init(&ina226[counter], addr_list[counter], mode, max_current_A, rshunt_mOhm, amp_fix_k, vot_fix_k); + INA226_Single_Init(&ina226[counter], addr_list[counter], mode, max_current_A, rshunt_mOhm, amp_fix_k[counter], vot_fix_k[counter]); } } @@ -54,13 +54,13 @@ void INA226_Single_Init(INA226_t *ina226, uint8 addr, uint8 mode[2], float max_c ina226->calibration.cal[1] = (uint8)((uint16)cal & 0xFF); memset(&ina226->result, 0, sizeof(ina226->result)); - + ina226_soft_iic_config.addr = ina226->config.addr; soft_iic_write_8bit_registers(&ina226_soft_iic_config, INA226_CONFIG_REG, ina226->config.mode, 2); soft_iic_write_8bit_registers(&ina226_soft_iic_config, INA226_CALIBRATION_REG, ina226->calibration.cal, 2); } -void INA226_Read(INA226_t *ina226) +void INA226_Single_Read(INA226_t *ina226) { uint8 counter; if(!is_iic_init) return; @@ -75,13 +75,13 @@ void INA226_Read(INA226_t *ina226) soft_iic_read_8bit_registers(&ina226_soft_iic_config, INA226_SHUNTVOT_REG, ina226->result.org_reg[0],2); - ina226->result.voltage_V = (int16)((ina226->result.org_reg[1][0] << 8) | ina226->result.org_reg[1][1]) * 0.00125f; + ina226->result.voltage_V = (int16)((ina226->result.org_reg[1][0] << 8) | ina226->result.org_reg[1][1]) * 0.00125f * ina226->config.vot_fix_k; ina226->result.current_A = (int16)((ina226->result.org_reg[3][0] << 8) | ina226->result.org_reg[3][1]) * ina226->calibration.c_lsb_mA * 0.001f; - ina226->result.power_W = (int16)((ina226->result.org_reg[2][0] << 8) | ina226->result.org_reg[2][1]) * ina226->calibration.c_lsb_mA * 0.025f; + ina226->result.power_W = (int16)((ina226->result.org_reg[2][0] << 8) | ina226->result.org_reg[2][1]) * ina226->calibration.c_lsb_mA * 0.025f * ina226->config.vot_fix_k; ina226->result.shunt_voltage_mV = (int16)((ina226->result.org_reg[0][0] << 8) | ina226->result.org_reg[0][1]) * 0.0025f; - ina226->result.voltage_V = ina226->result.voltage_V > 0 ? ina226->result.voltage_V : 0; - ina226->result.current_A = ina226->result.current_A > 0 ? ina226->result.current_A : 0; + ina226->result.voltage_V = ina226->result.voltage_V > 0.009 ? ina226->result.voltage_V : 0; + ina226->result.current_A = ina226->result.current_A > 0.009 ? ina226->result.current_A : 0; ina226->result.power_W = ina226->result.power_W > 0.016 && ina226->result.current_A > 0 && ina226->result.voltage_V > 0 ? ina226->result.power_W : 0; ina226->result.shunt_voltage_mV = ina226->result.shunt_voltage_mV > 0 ? ina226->result.shunt_voltage_mV : 0; @@ -96,3 +96,12 @@ void INA226_Read(INA226_t *ina226) ina226->result.energy_J += (ina226->result.past_voltage_V[9] * ina226->result.past_current_A[9] + ina226->result.past_voltage_V[8] * ina226->result.past_current_A[8]) * (INA226_READ_TIME_MS / 2000.0f); ina226->result.quantity_C += (ina226->result.past_current_A[9] + ina226->result.past_current_A[8]) * (INA226_READ_TIME_MS / 2000.0f); } + +void INA226_Read(void) +{ + uint8 counter; + for(counter = 0; counter < INA226_NUM; counter++) + { + INA226_Single_Read(&ina226[counter]); + } +} diff --git a/code/ina226.h b/code/ina226.h index 7c32ea9..fe79b5a 100644 --- a/code/ina226.h +++ b/code/ina226.h @@ -14,10 +14,13 @@ #define INA226_NUM 2 #define INA226_ALT_PIN {P15_2, P15_3} -#define INA226_ADDR {0x40, 0x45} +#define INA226_ADDR {0x45, 0x40} #define INA226_DEF_MODE {0x44, 0x4F} #define INA226_DEF_CAL {0x0D, 0x1B} +#define INA226_VOT_FIX_K {0.998996f, 0.998996f} +#define INA226_AMP_FIX_K {1.158f, 1.137f} + #define INA226_READ_TIME_MS 1 @@ -77,8 +80,9 @@ typedef struct extern INA226_t ina226[INA226_NUM]; +void INA226_Init(void); void INA226_Single_Init(INA226_t *ina226, uint8 addr, uint8 mode[2], float max_current_A, float rshunt_mOhm, float amp_fix_k, float vot_fix_k); -void INA226_Read(INA226_t *ina226); - +void INA226_Single_Read(INA226_t *ina226); +void INA226_Read(void); #endif /* CODE_INA226_H_ */ diff --git a/code/power_ctrl.c b/code/power_ctrl.c new file mode 100644 index 0000000..d26f40c --- /dev/null +++ b/code/power_ctrl.c @@ -0,0 +1,223 @@ +/* + * power_ctrl.c + * + * Created on: 2025年10月17日 + * Author: LHYe200 + */ + + +#include "power_ctrl.h" +#include "zf_driver_pit.h" +#include "power_out.h" +#include "pid.h" +#include "vofa_client.h" +#include "status_led.h" +#include "math.h" +#include "ina226.h" + +// PID DC_OUT1_PID; +// PID DC_AMP1_PID; + +uint32 timer_counter_1ms = 0; + +// low_pass_filter_t vot_set_filter; + +// float vot_set_filtered = 0.0f; + +// moving_average_filter_t mov_avg_filter; +// Kalman_filter_t kalman_filter; +// Kalman_filter_t unscented_kalman_filter; +// Kalman_filter_t adaptive_kalman_filter; + +// float kalman_q = 0.0001f; +// float kalman_r = 0.10f; + +void Power_Ctrl_Init(void) +{ + Power_Out_Init(); + VOFA_Client_Init(); + INA226_Init(); + // PID_Init(&DC_OUT1_PID, 3.25, 0.00909, 0.0, 500.0, 36000.0, 24000.0); + // PID_Init(&DC_OUT1_PID, 3.25, 0.00909, 0.0, 500.0, 50000.0, -50000.0); + // PID_Init(&DC_AMP1_PID, 0.0, 0.0, 0.0, 10.0, 50000.0, -50000.0); + // Init_lowPass_alpha(&vot_set_filter, 0.2f / 1000.0f, 10.0f); + + // Moving_Average_filter_init(&mov_avg_filter, 10); + // Kalman_filter_init(&kalman_filter, kalman_q, kalman_r); + // Unscented_Kalman_filter_init(&unscented_kalman_filter, kalman_q, kalman_r); + // Adaptive_Kalman_filter_init(&adaptive_kalman_filter, kalman_q, kalman_r); + + pit_us_init(CCU60_CH0, 1000); + +} + +void Power_Ctrl_Enable_Output(uint8 out_index, uint8 enable) +{ + if(out_index < POWER_OUT_NUM) + { + Power_Out_Enable(&power_outs[out_index], enable); + } +} + +void Power_Ctrl_Loop_1ms(void) +{ + + timer_counter_1ms++; + // power_outs[4].set_voltage_V = Low_pass_filter(&vot_set_filter, vot_set_filtered); + + if(timer_counter_1ms % POWER_OUT_READ_TIME_MS == 0) + { + Power_Out_Read(); + + // Moving_Average_filter(&mov_avg_filter, power_outs[4].config.v_adc); + // Kalman_filter(&kalman_filter, mov_avg_filter.avg); + // Unscented_Kalman_filter(&unscented_kalman_filter, power_outs[4].config.v_adc); + // Adaptive_Kalman_filter(&adaptive_kalman_filter, power_outs[4].config.v_adc); + } + +// if(power_outs[4].enabled) +// { +// // float err = power_outs[4].set_voltage_V - power_outs[4].status.voltage_V; +// // float set_vot = (power_outs[4].set_voltage_V - power_outs[4].config.votK[1]) / power_outs[4].config.votK[0]; +// // float err = set_vot - power_outs[4].config.vot_adc_filter.lastYn; +// // if(err < 0.0f) +// // { +// // err *= 5.0f; +// // } +// // PID_Loc_Ctrl(&DC_OUT1_PID, err); +// float err = (power_outs[4].set_voltage_V - power_outs[4].config.votK[1]) / power_outs[4].config.votK[0] - kalman_filter.prevData; +// if(fabsf(err) < 10.0f) +// { +// err = 0.0f; +// } +// else if(fabsf(err) < 282.4f) +// { +// err = err/fabsf(err)*(expf(fabsf(err)*0.02f)-1.0f); +// } +// PID_Loc_Ctrl(&DC_OUT1_PID, err); +// // PID_Inc_Ctrl(&DC_OUT1_PID,power_outs[4].set_voltage_V - power_outs[4].status.voltage_V); +// // if(power_outs[4].status.voltage_V > power_outs[4].set_voltage_V * 1.1f) +// // { +// // Power_Out_Set_PWM(&power_outs[4], 10000); +// // } +// // else if(power_outs[4].status.voltage_V < power_outs[4].set_voltage_V * 0.9f) +// // { +// // Power_Out_Set_PWM(&power_outs[4], 50000); +// // } +// // else +// // { +// // Power_Out_Set_PWM(&power_outs[4], 50000 + DC_OUT1_PID.output); +// // } +// +// Power_Out_Set_PWM(&power_outs[4], 50000 + DC_OUT1_PID.output); +// +// // Power_Out_Set_PWM(&power_outs[4], 10000); +// Flash_LED(STATUS_LED_7); +// } + + if(timer_counter_1ms % INA226_READ_TIME_MS == 0) + { + INA226_Read(); + + + } + // if(timer_counter_1ms % 3 == 0) + // { + // PID_Loc_Ctrl(&DC_OUT1_PID,power_outs[4].set_voltage_V - power_outs[4].status.voltage_V); + // } + + timer_counter_1ms %= (1 * 500); +} + + + +void Power_Status_Upload(void) +{ + VOFA_Set_JustFloat_Data(0, power_outs[0].status.voltage_V); + VOFA_Set_JustFloat_Data(1, power_outs[0].status.current_A); + VOFA_Set_JustFloat_Data(2, power_outs[1].status.voltage_V); + VOFA_Set_JustFloat_Data(3, power_outs[1].status.current_A); + VOFA_Set_JustFloat_Data(4, power_outs[2].status.voltage_V); + VOFA_Set_JustFloat_Data(5, power_outs[2].status.current_A); + VOFA_Set_JustFloat_Data(6, power_outs[3].status.voltage_V); + VOFA_Set_JustFloat_Data(7, power_outs[3].status.current_A); + VOFA_Set_JustFloat_Data(8, power_outs[4].status.voltage_V); + VOFA_Set_JustFloat_Data(9, power_outs[4].status.current_A); + VOFA_Set_JustFloat_Data(10, power_outs[5].status.voltage_V); + VOFA_Set_JustFloat_Data(11, power_outs[5].status.current_A); + VOFA_Set_JustFloat_Data(12, power_outs[6].status.voltage_V); + VOFA_Set_JustFloat_Data(13, power_outs[6].status.current_A); + VOFA_Set_JustFloat_Data(14, power_outs[7].status.voltage_V); + VOFA_Set_JustFloat_Data(15, power_outs[7].status.current_A); + VOFA_Set_JustFloat_Data(16,ina226[0].result.voltage_V); + VOFA_Set_JustFloat_Data(17,ina226[0].result.current_A); + VOFA_Set_JustFloat_Data(18,ina226[1].result.voltage_V); + VOFA_Set_JustFloat_Data(19,ina226[1].result.current_A); + + VOFA_Set_JustFloat_Data(22,vofa_last_data); + VOFA_Send_Datas(27); +} + + + +// void Power_Status_Upload(void) +// { +// VOFA_Set_JustFloat_Data(0, power_outs[0].status.voltage_V); +// VOFA_Set_JustFloat_Data(1, power_outs[0].status.current_A); +// VOFA_Set_JustFloat_Data(2, power_outs[1].status.voltage_V); +// VOFA_Set_JustFloat_Data(3, power_outs[1].status.current_A); +// VOFA_Set_JustFloat_Data(4, power_outs[2].status.voltage_V); +// VOFA_Set_JustFloat_Data(5, power_outs[2].status.current_A); +// VOFA_Set_JustFloat_Data(6, power_outs[3].status.voltage_V); +// VOFA_Set_JustFloat_Data(7, power_outs[3].status.current_A); +// VOFA_Set_JustFloat_Data(8, power_outs[4].status.voltage_V); +// VOFA_Set_JustFloat_Data(9, power_outs[4].status.current_A); +// VOFA_Set_JustFloat_Data(10, power_outs[5].status.voltage_V); +// VOFA_Set_JustFloat_Data(11, power_outs[5].status.current_A); +// VOFA_Set_JustFloat_Data(12, power_outs[6].status.voltage_V); +// VOFA_Set_JustFloat_Data(13, power_outs[6].status.current_A); +// VOFA_Set_JustFloat_Data(14, power_outs[7].status.voltage_V); +// VOFA_Set_JustFloat_Data(15, power_outs[7].status.current_A); +// VOFA_Set_JustFloat_Data(16,vofa_last_data); +// VOFA_Set_JustFloat_Data(17,DC_OUT1_PID.p_output); +// VOFA_Set_JustFloat_Data(18,DC_OUT1_PID.i_output); +// VOFA_Set_JustFloat_Data(19,DC_OUT1_PID.d_output); +// VOFA_Set_JustFloat_Data(20,power_outs[4].config.vot_adc_filter.lastYn); +// VOFA_Set_JustFloat_Data(21,DC_OUT1_PID.output); +// VOFA_Set_JustFloat_Data(22,mov_avg_filter.avg); +// VOFA_Set_JustFloat_Data(23,kalman_filter.prevData); +// VOFA_Set_JustFloat_Data(24,unscented_kalman_filter.prevData); +// VOFA_Set_JustFloat_Data(25,adaptive_kalman_filter.prevData); +// VOFA_Set_JustFloat_Data(26,power_outs[4].config.v_adc); +// VOFA_Send_Datas(27); +// } + +void Power_Vofa_Set(void) +{ + // if(vofa_receive_new_data[1]) + // { + // vofa_receive_new_data[1] = 0; + // DC_OUT1_PID.kp = vofa_data[1]; + // } + // if(vofa_receive_new_data[2]) + // { + // vofa_receive_new_data[2] = 0; + // DC_OUT1_PID.ki = vofa_data[2]; + // } + // if(vofa_receive_new_data[3]) + // { + // vofa_receive_new_data[3] = 0; + // DC_OUT1_PID.kd = vofa_data[3]; + // } + // if(vofa_receive_new_data[4]) + // { + // vofa_receive_new_data[4] = 0; + // power_outs[4].set_voltage_V = vofa_data[4]; + // // vot_set_filtered = vofa_data[4]; + // } + // if(vofa_receive_new_data[5]) + // { + // vofa_receive_new_data[5] = 0; + // Power_Out_Set_PWM(&power_outs[4], vofa_data[5]); + // } +} diff --git a/code/power_ctrl.h b/code/power_ctrl.h new file mode 100644 index 0000000..5de3617 --- /dev/null +++ b/code/power_ctrl.h @@ -0,0 +1,20 @@ +/* + * power_ctrl.h + * + * Created on: 2025年10月17日 + * Author: LHYe200 + */ + +#ifndef CODE_POWER_CTRL_H_ +#define CODE_POWER_CTRL_H_ + +#include "zf_common_typedef.h" + +void Power_Ctrl_Init(void); +void Power_Ctrl_Loop_1ms(void); +void Power_Ctrl_Enable_Output(uint8 out_index, uint8 enable); +void Power_Status_Upload(void); +void Power_Vofa_Set(void); + + +#endif /* CODE_POWER_CTRL_H_ */ diff --git a/code/power_out.c b/code/power_out.c index 0fe57fa..a6e07dc 100644 --- a/code/power_out.c +++ b/code/power_out.c @@ -15,8 +15,6 @@ Power_Out_t power_outs[POWER_OUT_NUM]; - - void Power_Out_Single_Init(Power_Out_t *power_out, int16 vot_channel, int16 amp_channel, float votK[2], float ampK[2], int16 enable_pin, uint8 can_adjust, int16 vot_pwm_channel) { if(enable_pin != POWER_OUT_FUNCTION_DISABLE) @@ -27,6 +25,7 @@ void Power_Out_Single_Init(Power_Out_t *power_out, int16 vot_channel, int16 amp_ power_out->config.current_read_channel = amp_channel; Init_lowPass_alpha(&power_out->config.vot_filter, POWER_OUT_READ_TIME_MS / 1000.0f, 10.0f); Init_lowPass_alpha(&power_out->config.amp_filter, POWER_OUT_READ_TIME_MS / 1000.0f, 10.0f); + Init_lowPass_alpha(&power_out->config.vot_adc_filter, POWER_OUT_READ_TIME_MS / 1000.0f, 100.0f); power_out->config.votK[0] = votK[0]; power_out->config.votK[1] = votK[1]; power_out->config.ampK[0] = ampK[0]; @@ -43,9 +42,9 @@ void Power_Out_Single_Init(Power_Out_t *power_out, int16 vot_channel, int16 amp_ memset(power_out->status.past_voltage_V, 0, sizeof(power_out->status.past_voltage_V)); memset(power_out->status.past_current_A, 0, sizeof(power_out->status.past_current_A)); - power_out->set_voltage_V = 0.0f; - power_out->set_current_A = 0.0f; - power_out->max_power_W = 300.0f; + power_out->set_voltage_V = VOT_ADJUST_DEF; + power_out->set_current_A = AMP_ADJUST_DEF; + power_out->max_power_W = POW_ADJUST_DEF; power_out->enabled = 0; if(vot_channel != POWER_OUT_FUNCTION_DISABLE) { @@ -58,7 +57,7 @@ void Power_Out_Single_Init(Power_Out_t *power_out, int16 vot_channel, int16 amp_ if(vot_pwm_channel != POWER_OUT_FUNCTION_DISABLE) { - pwm_init((pwm_channel_enum)vot_pwm_channel, VOT_PWM_FREQUENCY_HZ, 5000); + pwm_init((pwm_channel_enum)vot_pwm_channel, VOT_PWM_FREQUENCY_HZ, 10000); } } @@ -103,6 +102,8 @@ void Power_Out_Single_Read(Power_Out_t *power_out) { // tmp = (float)adc_convert((adc_channel_enum)power_out->config.voltage_read_channel); tmp = (float)adc_mean_filter_convert((adc_channel_enum)power_out->config.voltage_read_channel,2); + Low_pass_filter(&power_out->config.vot_adc_filter, tmp); + power_out->config.v_adc = tmp; power_out->status.voltage_V = Low_pass_filter(&power_out->config.vot_filter, tmp) * power_out->config.votK[0] + power_out->config.votK[1]; } else diff --git a/code/power_out.h b/code/power_out.h index 094bac7..5f2b409 100644 --- a/code/power_out.h +++ b/code/power_out.h @@ -25,10 +25,18 @@ #define POWER_OUT_VOT_ADC_K {{0.0015, -0.0437}, {0.0015, -0.045}, {0.0015, -0.0451}, {0.0015, -0.0451}, {0.0049, 0.0297}, {0.0049, -0.0292}, {0.0049, -0.0179}, {0.0049, 0.0397}} #define POWER_OUT_AMP_ADC_K {{0.0029, -0.1382}, {0.002868, -0.1325}, {0.0029, -0.1335}, {0.0029, -0.1294}, {0.0029, -0.1083}, {0.0029, -0.1174}, {0.0028, -0.0563}, {0.002831, -0.0786}} -#define VOT_PWM_FREQUENCY_HZ 100000 +#define VOT_PWM_FREQUENCY_HZ 50000 -#define VOT_PROTECT_K {0.9,1.1} // 电压保护系数 -#define AMP_PROTECT_K 1.1 // 电流保护系数 +#define VOT_ADJUST_MAX 20.0f +#define VOT_ADJUST_MIN 3.0f +#define AMP_ADJUST_MAX 15.0f + +#define VOT_ADJUST_DEF 5.0f +#define AMP_ADJUST_DEF 1.0f +#define POW_ADJUST_DEF 300.0f + +#define VOT_PROTECT_K {0.9f,1.1f} // 电压保护系数 +#define AMP_PROTECT_K 1.1f // 电流保护系数 typedef struct { @@ -49,6 +57,8 @@ typedef struct float ampK[2]; // 电流校准系数 low_pass_filter_t vot_filter; low_pass_filter_t amp_filter; + low_pass_filter_t vot_adc_filter; + float v_adc; int16 enable_pin; uint8 can_adjust; // 是否允许调节输出 int16 vot_pwm_channel; @@ -66,6 +76,7 @@ typedef struct extern Power_Out_t power_outs[POWER_OUT_NUM]; + void Power_Out_Single_Init(Power_Out_t *power_out, int16 vot_channel, int16 amp_channel, float votK[2], float ampK[2], int16 enable_pin, uint8 can_adjust, int16 vot_pwm_channel); void Power_Out_Init(void); void Power_Out_Enable(Power_Out_t *power_out, uint8 enable); diff --git a/code/vofa_client.c b/code/vofa_client.c index 5ed5cfa..e044a5d 100644 --- a/code/vofa_client.c +++ b/code/vofa_client.c @@ -7,7 +7,7 @@ */ #include "vofa_client.h" - +#include "esp8266.h" float vofa_data[VOFA_RECEIVE_CH] = {0}; // 记录接收数据 float vofa_last_data = 0; @@ -129,7 +129,9 @@ void VOFA_Sender(uint8 *p, uint32 len) return; } #if VOFA_CLIENT_COM_INTERFACE == 0 - uart_write_buffer(VOFA_CLIENT_UART_PORT,p,len); + + ESP8266_send_data(p,len); + // uart_write_buffer(VOFA_CLIENT_UART_PORT,p,len); #elif VOFA_CLIENT_COM_INTERFACE == 1 wifi_spi_send_buffer(p,len); #elif VOFA_CLIENT_COM_INTERFACE == 2 @@ -138,7 +140,7 @@ void VOFA_Sender(uint8 *p, uint32 len) #endif } -void VOFA_Receiver_Callback() +void VOFA_Receiver_Callback(uint8 rev_data) { if(failed_flag) // 如果初始化失败则不发送数据 { @@ -147,7 +149,8 @@ void VOFA_Receiver_Callback() uint8 rev_tmp = 0; uint8 tmp[4] = {0}; #if VOFA_CLIENT_COM_INTERFACE == 0 - while(uart_query_byte(VOFA_CLIENT_UART_PORT, &rev_tmp)) + rev_tmp = rev_data; + // while(uart_query_byte(VOFA_CLIENT_UART_PORT, &rev_tmp)) #elif VOFA_CLIENT_COM_INTERFACE == 1 while(wifi_spi_read_buffer(&rev_tmp,1)) #elif VOFA_CLIENT_COM_INTERFACE == 2 diff --git a/code/vofa_client.h b/code/vofa_client.h index 8b47f8b..9a0d5f1 100644 --- a/code/vofa_client.h +++ b/code/vofa_client.h @@ -91,7 +91,7 @@ void VOFA_Set_JustFloat_Data(int CH, float data); // void VOFA_Set_JustFloat_Datas_From_Start(int num,...); // 设置从第一个通道开始的多个通道数据 void VOFA_Send_Datas(int num); // 从第一个通道开始发送数据指定数量的数据 void VOFA_Send_JustFloat_Image(int IMG_ID, int IMG_WIDTH, int IMG_HEIGHT, int IMG_DATA_SIZE, ImgFormat IMG_FORMAT,uint8 *IMG_DATA); // 发送图像数据 -void VOFA_Receiver_Callback(void); // 接收回调函数,需要在主循环或者接受中断中调用 +void VOFA_Receiver_Callback(uint8 rev_data); // 接收回调函数,需要在主循环或者接受中断中调用 #endif /* CODE_VOFA_CLIENT_H_ */ diff --git a/esp8266/esp8266/esp8266.ino b/esp8266/esp8266/esp8266.ino new file mode 100644 index 0000000..70860f8 --- /dev/null +++ b/esp8266/esp8266/esp8266.ino @@ -0,0 +1,97 @@ +#include + + +#ifndef STASSID +#define STASSID "601_IoT_WIFI" +#define STAPSK "wuhcw64667_jsj" +#endif + +const char* ssid = STASSID; +const char* password = STAPSK; + +const char* host = "192.168.1.111"; +const uint16_t port = 1347; + + +WiFiClient client; + +void connectToServer() { + // Serial.print("connecting to "); + // Serial.print(host); + // Serial.print(':'); + // Serial.println(port); + + if (!client.connect(host, port)) { + // Serial.println("connection failed"); + return; + } + // Serial.println("connected"); +} + + +void setup() { + Serial.begin(115200); + WiFi.mode(WIFI_STA); + WiFi.begin(ssid, password); + while (WiFi.status() != WL_CONNECTED) { + delay(500); + // Serial.print("."); + } + // Serial.println(WiFi.localIP()); + connectToServer(); +} + + + +void loop() { + // static bool wait = false; + + // Serial.print("connecting to "); + // Serial.print(host); + // Serial.print(':'); + // Serial.println(port); + + // Use WiFiClient class to create TCP connections + + // This will send a string to the server + // Serial.println("sending data to server"); + if (client.connected()) { + while(Serial.available()) { + char ch = static_cast(Serial.read()); + client.print(ch); + } + // client.println("hello from ESP8266"); + } + else { + connectToServer(); + } + + unsigned long timeout = millis(); + while (client.available() == 0) { + if (millis() - timeout > 5000) { + // Serial.println(">>> Client Timeout !"); + client.stop(); + delay(1000); + return; + } + } + + // Read all the lines of the reply from server and print them to Serial + // Serial.println("receiving from remote server"); + // not testing 'client.connected()' since we do not need to send data here + while (client.available()) { + char ch = static_cast(client.read()); + Serial.print(ch); + } + + // Close the connection + // Serial.println(); + // Serial.println("closing connection"); + // client.stop(); + + // if (wait) { + // delay(300000); // execute once every 5 minutes, don't flood remote service + // } + // wait = true; + +} diff --git a/libraries/zf_driver/zf_driver_pwm.h b/libraries/zf_driver/zf_driver_pwm.h index aaddc6d..aae4e0c 100644 --- a/libraries/zf_driver/zf_driver_pwm.h +++ b/libraries/zf_driver/zf_driver_pwm.h @@ -38,7 +38,7 @@ #include "zf_common_typedef.h" -#define PWM_DUTY_MAX 10000 // PWM最大占空比 最大占空比越大占空比的步进值越小 +#define PWM_DUTY_MAX 100000 // PWM最大占空比 最大占空比越大占空比的步进值越小 // 此枚举定义不允许用户修改 typedef enum // 枚举PWM引脚 diff --git a/user/cpu0_main.c b/user/cpu0_main.c index 76fe90c..54072ad 100644 --- a/user/cpu0_main.c +++ b/user/cpu0_main.c @@ -33,9 +33,10 @@ * 2022-09-15 pudding first version ********************************************************************************************************************/ #include "zf_common_headfile.h" -#include "power_out.h" +#include "power_ctrl.h" #include "status_led.h" -#include "vofa_client.h" +#include "esp8266.h" + #pragma section all "cpu0_dsram" @@ -45,55 +46,50 @@ int core0_main(void) { clock_init(); // 获取时钟频率<务必保留> debug_init(); // 初始化默认调试串口 - Power_Out_Init(); + Power_Ctrl_Init(); + ESP8266_Init(); Status_LED_Init(); - VOFA_Client_Init(); + - ON_LED(STATUS_LED_7); - ON_LED(STATUS_LED_8); + // ON_LED(STATUS_LED_7); + // ON_LED(STATUS_LED_8); uint32 t = 0; uint8 dir = 0; - Power_Out_Enable(&power_outs[0], 1); - Power_Out_Enable(&power_outs[1], 1); - Power_Out_Enable(&power_outs[2], 1); - Power_Out_Enable(&power_outs[3], 1); - Power_Out_Enable(&power_outs[4], 1); - Power_Out_Enable(&power_outs[5], 1); - Power_Out_Enable(&power_outs[6], 1); - Power_Out_Enable(&power_outs[7], 1); + // Power_Out_Enable(&power_outs[0], 1); + // Power_Out_Enable(&power_outs[1], 1); + // Power_Out_Enable(&power_outs[2], 1); + // Power_Out_Enable(&power_outs[3], 1); + // Power_Out_Enable(&power_outs[4], 1); + // Power_Out_Enable(&power_outs[5], 1); + // Power_Out_Enable(&power_outs[6], 1); + // Power_Out_Enable(&power_outs[7], 1); // Power_Out_Set_PWM(&power_outs[4], 3000); // Power_Out_Set_PWM(&power_outs[5], 4000); // Power_Out_Set_PWM(&power_outs[6], 5000); // Power_Out_Set_PWM(&power_outs[7], 6000); - pit_ms_init(CCU60_CH0, POWER_OUT_READ_TIME_MS); // 初始化PIT0 用于读取电源模块状态 + Power_Ctrl_Enable_Output(4, 1); + Power_Ctrl_Enable_Output(5, 1); + Power_Ctrl_Enable_Output(6, 1); + Power_Ctrl_Enable_Output(7, 1); cpu_wait_event_ready(); while (TRUE) { - Flash_LED(STATUS_LED_6); + Flash_LED(STATUS_LED_10); + ESP8266_Main_Loop_Change_Mode(); + // if(!esp8266_download_passthrough) + // { + Power_Vofa_Set(); + Power_Status_Upload(); + system_delay_ms(1); + // } - VOFA_Set_JustFloat_Data(0, power_outs[0].status.voltage_V); - VOFA_Set_JustFloat_Data(1, power_outs[0].status.current_A); - VOFA_Set_JustFloat_Data(2, power_outs[1].status.voltage_V); - VOFA_Set_JustFloat_Data(3, power_outs[1].status.current_A); - VOFA_Set_JustFloat_Data(4, power_outs[2].status.voltage_V); - VOFA_Set_JustFloat_Data(5, power_outs[2].status.current_A); - VOFA_Set_JustFloat_Data(6, power_outs[3].status.voltage_V); - VOFA_Set_JustFloat_Data(7, power_outs[3].status.current_A); - VOFA_Set_JustFloat_Data(8, power_outs[4].status.voltage_V); - VOFA_Set_JustFloat_Data(9, power_outs[4].status.current_A); - VOFA_Set_JustFloat_Data(10, power_outs[5].status.voltage_V); - VOFA_Set_JustFloat_Data(11, power_outs[5].status.current_A); - VOFA_Set_JustFloat_Data(12, power_outs[6].status.voltage_V); - VOFA_Set_JustFloat_Data(13, power_outs[6].status.current_A); - VOFA_Set_JustFloat_Data(14, power_outs[7].status.voltage_V); - VOFA_Set_JustFloat_Data(15, power_outs[7].status.current_A); - VOFA_Send_Datas(16); - system_delay_ms(1); + + // system_delay_ms(1); // if(dir == 0) // { @@ -114,7 +110,7 @@ int core0_main(void) // } // } // Power_Out_Set_PWM(&power_outs[7], t); - // system_delay_ms(1); + } } diff --git a/user/isr.c b/user/isr.c index 8233bc0..8bbd955 100644 --- a/user/isr.c +++ b/user/isr.c @@ -35,7 +35,11 @@ #include "isr_config.h" #include "isr.h" -#include "power_out.h" +#include "power_ctrl.h" +#include "vofa_client.h" +#include "esp8266.h" +#include "status_led.h" + // 对于TC系列默认是不支持中断嵌套的,希望支持中断嵌套需要在中断内使用 interrupt_global_enable(0); 来开启中断嵌套 // 简单点说实际上进入中断后TC系列的硬件自动调用了 interrupt_global_disable(); 来拒绝响应任何的中断,因此需要我们自己手动调用 interrupt_global_enable(0); 来开启中断的响应。 @@ -44,7 +48,7 @@ IFX_INTERRUPT(cc60_pit_ch0_isr, 0, CCU6_0_CH0_ISR_PRIORITY) { interrupt_global_enable(0); // 开启中断嵌套 pit_clear_flag(CCU60_CH0); - Power_Out_Read(); + Power_Ctrl_Loop_1ms(); } @@ -176,12 +180,24 @@ IFX_INTERRUPT(uart0_tx_isr, 0, UART0_TX_INT_PRIO) IFX_INTERRUPT(uart0_rx_isr, 0, UART0_RX_INT_PRIO) { interrupt_global_enable(0); // 开启中断嵌套 + // VOFA_Receiver_Callback(); + ESP8266_Auto_Download_Uart_Hook(); + // Flash_LED(STATUS_LED_8); + // uint8 rev_tmp; + // while(uart_query_byte(UART_0, &rev_tmp)) + // { + // uart_write_byte(UART_2, rev_tmp); + // } #if DEBUG_UART_USE_INTERRUPT // 如果开启 debug 串口中断 debug_interrupr_handler(); // 调用 debug 串口接收处理函数 数据会被 debug 环形缓冲区读取 #endif // 如果修改了 DEBUG_UART_INDEX 那这段代码需要放到对应的串口中断去 } +void isr_uart0_rx_interrupt_hook_back(uint8 rev_data) +{ + VOFA_Receiver_Callback(rev_data); +} // 串口1默认连接到摄像头配置串口 IFX_INTERRUPT(uart1_tx_isr, 0, UART1_TX_INT_PRIO) @@ -210,8 +226,15 @@ IFX_INTERRUPT(uart2_tx_isr, 0, UART2_TX_INT_PRIO) IFX_INTERRUPT(uart2_rx_isr, 0, UART2_RX_INT_PRIO) { interrupt_global_enable(0); // 开启中断嵌套 - wireless_module_uart_handler(); // 无线模块统一回调函数 - + // wireless_module_uart_handler(); // 无线模块统一回调函数 + ESP8266_Uart_Callback(); + // Flash_LED(STATUS_LED_9); + // uint8 rev_tmp; + // while(uart_query_byte(UART_2, &rev_tmp)) + // { + // uart_write_byte(UART_0, rev_tmp); + // } + }