esp加入
This commit is contained in:
208
code/esp8266.c
Normal file
208
code/esp8266.c
Normal file
@@ -0,0 +1,208 @@
|
||||
/*
|
||||
* esp8266.c
|
||||
*
|
||||
* Created on: 2025<32><35>10<31><30>18<31><38>
|
||||
* 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
|
||||
{
|
||||
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
33
code/esp8266.h
Normal file
33
code/esp8266.h
Normal file
@@ -0,0 +1,33 @@
|
||||
/*
|
||||
* esp8266.h
|
||||
*
|
||||
* Created on: 2025<32><35>10<31><30>18<31><38>
|
||||
* 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_ */
|
||||
238
code/filter.c
238
code/filter.c
@@ -7,7 +7,9 @@
|
||||
*/
|
||||
|
||||
#include "filter.h"
|
||||
|
||||
#include "math.h"
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
/*******************************************************************************
|
||||
* @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 <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
* @return <20>˲<EFBFBD><CBB2><EFBFBD><EFBFBD><EFBFBD>
|
||||
******************************************************************************/
|
||||
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 <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
* @return <20>˲<EFBFBD><CBB2><EFBFBD><EFBFBD><EFBFBD>
|
||||
******************************************************************************/
|
||||
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 <20><>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ƽ<EFBFBD><C6BD><EFBFBD>˲<EFBFBD><CBB2><EFBFBD>
|
||||
* @param filter <20>˲<EFBFBD><CBB2><EFBFBD><EFBFBD>ṹ<EFBFBD><E1B9B9>ָ<EFBFBD><D6B8>
|
||||
* @param size <20><><EFBFBD>ڴ<EFBFBD>С
|
||||
* @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 <20><><EFBFBD><EFBFBD>ƽ<EFBFBD><C6BD><EFBFBD>˲<EFBFBD><CBB2><EFBFBD>ʵ<EFBFBD><CAB5>
|
||||
* @param data <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
* @param size <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ĵ<EFBFBD>С
|
||||
* @return <20><><EFBFBD>ػ<EFBFBD><D8BB><EFBFBD>ƽ<EFBFBD><C6BD>ֵ
|
||||
* @note <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ƽ<EFBFBD><C6BD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>źš<C5BA>
|
||||
******************************************************************************/
|
||||
float Moving_Average_filter(moving_average_filter_t* const filter, float newData)
|
||||
{
|
||||
float sum = 0.0f;
|
||||
// <20>ƶ<EFBFBD><C6B6><EFBFBD><EFBFBD><EFBFBD>
|
||||
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];
|
||||
// <20><><EFBFBD><EFBFBD>ƽ<EFBFBD><C6BD>ֵ
|
||||
filter->avg = sum / filter->size;
|
||||
return filter->avg;
|
||||
}
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
* @fn Kalman_filter_init
|
||||
* @brief <20><>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>˲<EFBFBD><CBB2><EFBFBD>״̬<D7B4><CCAC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
* @param filter <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>˲<EFBFBD><CBB2><EFBFBD><EFBFBD>ṹ<EFBFBD><E1B9B9>ָ<EFBFBD><D6B8>
|
||||
* @param q <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EEA3A8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Э<EFBFBD><D0AD><EFBFBD><EFBFBD>Q<EFBFBD><51>
|
||||
* @param r <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EEA3A8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Э<EFBFBD><D0AD><EFBFBD><EFBFBD>R<EFBFBD><52>
|
||||
* @param kGain <20><>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>棨<EFBFBD><E6A3A8><EFBFBD><EFBFBD>Ϊ0<CEAA><30><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ<EFBFBD><D6B5>
|
||||
* @return void
|
||||
* @note prevData <20><>ʼΪ0<CEAA><30>p <20><>ʼЭ<CABC><D0AD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ1<CEAA><31><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʼ״̬<D7B4><CCAC><EFBFBD>ã<EFBFBD><C3A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һά<D2BB><CEAC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>˲<EFBFBD><CBB2>ṹ<EFBFBD><E1B9B9>
|
||||
******************************************************************************/
|
||||
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 һά<D2BB><CEAC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>˲<EFBFBD><CBB2><EFBFBD>ʵ<EFBFBD>֣<EFBFBD><D6A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʽ<EFBFBD><CABD><EFBFBD>£<EFBFBD>
|
||||
* @param filter <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>˲<EFBFBD><CBB2><EFBFBD><EFBFBD>ṹ<EFBFBD><E1B9B9>ָ<EFBFBD>룬<EFBFBD><EBA3AC><EFBFBD><EFBFBD>״̬ prevData<74><61>Э<EFBFBD><D0AD><EFBFBD><EFBFBD> p<><70><EFBFBD><EFBFBD><EFBFBD><EFBFBD>q/r <20>͵<EFBFBD>ǰ<EFBFBD><C7B0><EFBFBD><EFBFBD> kGain
|
||||
* @param inData <20><><EFBFBD>β<EFBFBD><CEB2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ
|
||||
* @return <20><><EFBFBD><EFBFBD><EFBFBD>˲<EFBFBD><CBB2><EFBFBD><EFBFBD>Ĺ<EFBFBD><C4B9><EFBFBD>ֵ<EFBFBD><D6B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͣ<EFBFBD>
|
||||
* @note <20>㷨<EFBFBD><E3B7A8><EFBFBD>裺
|
||||
* 1) Ԥ<><D4A4>Э<EFBFBD><D0AD><EFBFBD><EFBFBD> p <- p + q
|
||||
* 2) <20><><EFBFBD>㿨<EFBFBD><E3BFA8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> k <- p / (p + r)
|
||||
* 3) ʹ<><CAB9><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ x <- x_prev + k*(z - x_prev)
|
||||
* 4) <20><><EFBFBD><EFBFBD>Э<EFBFBD><D0AD><EFBFBD><EFBFBD> p <- (1 - k) * p
|
||||
* 5) <20><><EFBFBD>汾<EFBFBD>ι<EFBFBD><CEB9><EFBFBD>Ϊ prevData <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
* <20><>ʵ<EFBFBD><CAB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һά<D2BB>źŵ<C5BA><C5B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>˲<EFBFBD><CBB2><EFBFBD>δ<EFBFBD><CEB4><EFBFBD><EFBFBD>״̬ת<CCAC>ƾ<EFBFBD><C6BE><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>롣
|
||||
******************************************************************************/
|
||||
float Kalman_filter(Kalman_filter_t* const filter, float inData)
|
||||
{
|
||||
filter->p = filter->p + filter->q;
|
||||
filter->kGain = filter->p / ( filter->p + filter->r ); //<2F><><EFBFBD>㿨<EFBFBD><E3BFA8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
inData = filter->prevData + ( filter->kGain * ( inData - filter->prevData ) ); //<2F><><EFBFBD>㱾<EFBFBD><E3B1BE><EFBFBD>˲<EFBFBD><CBB2><EFBFBD><EFBFBD><EFBFBD>ֵ
|
||||
filter->p = ( 1 - filter->kGain ) * filter->p; //<2F><><EFBFBD>²<EFBFBD><C2B2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
filter->prevData = inData;
|
||||
return inData; //<2F><><EFBFBD><EFBFBD><EFBFBD>˲<EFBFBD>ֵ
|
||||
}
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
* @fn Unscented_Kalman_filter_init
|
||||
* @brief <20><>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD><DEBC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>˲<EFBFBD><CBB2><EFBFBD>״̬<D7B4><CCAC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
* @param filter <20><EFBFBD><DEBC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>˲<EFBFBD><CBB2><EFBFBD><EFBFBD>ṹ<EFBFBD><E1B9B9>ָ<EFBFBD><D6B8>
|
||||
* @param q <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Э<EFBFBD><D0AD><EFBFBD><EFBFBD>
|
||||
* @param r <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Э<EFBFBD><D0AD><EFBFBD><EFBFBD>
|
||||
* @return void
|
||||
* @note prevData <20><>ʼΪ0<CEAA><30>p <20><>ʼЭ<CABC><D0AD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ1<CEAA><31>
|
||||
******************************************************************************/
|
||||
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 һά<D2BB><EFBFBD><DEBC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>˲<EFBFBD><CBB2><EFBFBD>ʵ<EFBFBD><CAB5>
|
||||
* @param filter <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>˲<EFBFBD><CBB2><EFBFBD><EFBFBD>ṹ<EFBFBD><E1B9B9>ָ<EFBFBD>룬<EFBFBD><EBA3AC><EFBFBD><EFBFBD>״̬<D7B4><CCAC>Э<EFBFBD><D0AD><EFBFBD><EFBFBD><EEA1A2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
* @param inData <20><><EFBFBD>β<EFBFBD><CEB2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ
|
||||
* @return <20><><EFBFBD><EFBFBD><EFBFBD>˲<EFBFBD><CBB2><EFBFBD><EFBFBD>Ĺ<EFBFBD><C4B9><EFBFBD>ֵ<EFBFBD><D6B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͣ<EFBFBD>
|
||||
* @note ʹ<><CAB9><EFBFBD><EFBFBD><DEBC>任<EFBFBD><E4BBBB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ϵͳ<CFB5><CDB3><EFBFBD>ʺϸ<CABA><CFB8>ӷ<EFBFBD><D3B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD>źš<C5BA>
|
||||
******************************************************************************/
|
||||
float Unscented_Kalman_filter(Kalman_filter_t* const filter, float inData)
|
||||
{
|
||||
// <20><EFBFBD><DEBC>任<EFBFBD><E4BBBB><EFBFBD><EFBFBD>
|
||||
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);
|
||||
|
||||
// Ԥ<><D4A4><EFBFBD><EFBFBD>ֵ
|
||||
float predictedMean = (sigmaPoints[0] + sigmaPoints[1] + sigmaPoints[2]) / 3.0;
|
||||
|
||||
// Ԥ<><D4A4>Э<EFBFBD><D0AD><EFBFBD><EFBFBD>
|
||||
float predictedCov = filter->q + ((sigmaPoints[1] - predictedMean) * (sigmaPoints[1] - predictedMean) +
|
||||
(sigmaPoints[2] - predictedMean) * (sigmaPoints[2] - predictedMean)) / 2.0;
|
||||
|
||||
// <20><><EFBFBD>¿<EFBFBD><C2BF><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
filter->kGain = predictedCov / (predictedCov + filter->r);
|
||||
|
||||
// <20><><EFBFBD>¹<EFBFBD><C2B9><EFBFBD>ֵ
|
||||
float updatedMean = predictedMean + filter->kGain * (inData - predictedMean);
|
||||
|
||||
// <20><><EFBFBD><EFBFBD>Э<EFBFBD><D0AD><EFBFBD><EFBFBD>
|
||||
filter->p = (1 - filter->kGain) * predictedCov;
|
||||
|
||||
// <20><><EFBFBD><EFBFBD>״̬
|
||||
filter->prevData = updatedMean;
|
||||
|
||||
return updatedMean;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
* @fn Adaptive_Kalman_filter_init
|
||||
* @brief <20><>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӧ<EFBFBD><D3A6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>˲<EFBFBD><CBB2><EFBFBD>״̬<D7B4><CCAC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
* @param filter <20><><EFBFBD><EFBFBD>Ӧ<EFBFBD><D3A6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>˲<EFBFBD><CBB2><EFBFBD><EFBFBD>ṹ<EFBFBD><E1B9B9>ָ<EFBFBD><D6B8>
|
||||
* @param q <20><>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Э<EFBFBD><D0AD><EFBFBD><EFBFBD>
|
||||
* @param r <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Э<EFBFBD><D0AD><EFBFBD><EFBFBD>
|
||||
* @return void
|
||||
* @note prevData <20><>ʼΪ0<CEAA><30>p <20><>ʼЭ<CABC><D0AD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ1<CEAA><31>
|
||||
******************************************************************************/
|
||||
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 һά<D2BB><CEAC><EFBFBD><EFBFBD>Ӧ<EFBFBD><D3A6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>˲<EFBFBD><CBB2><EFBFBD>ʵ<EFBFBD><CAB5>
|
||||
* @param filter <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>˲<EFBFBD><CBB2><EFBFBD><EFBFBD>ṹ<EFBFBD><E1B9B9>ָ<EFBFBD>룬<EFBFBD><EBA3AC><EFBFBD><EFBFBD>״̬<D7B4><CCAC>Э<EFBFBD><D0AD><EFBFBD><EFBFBD><EEA1A2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
* @param inData <20><><EFBFBD>β<EFBFBD><CEB2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ
|
||||
* @return <20><><EFBFBD><EFBFBD><EFBFBD>˲<EFBFBD><CBB2><EFBFBD><EFBFBD>Ĺ<EFBFBD><C4B9><EFBFBD>ֵ<EFBFBD><D6B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͣ<EFBFBD>
|
||||
* @note <20><>̬<EFBFBD><CCAC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Э<EFBFBD><D0AD><EFBFBD><EFBFBD>ʺ<EFBFBD><CABA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ա仯<D4B1><E4BBAF><EFBFBD>źš<C5BA>
|
||||
******************************************************************************/
|
||||
float Adaptive_Kalman_filter(Kalman_filter_t* const filter, float inData)
|
||||
{
|
||||
// <20><><EFBFBD>¹<EFBFBD><C2B9><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Э<EFBFBD><D0AD><EFBFBD><EFBFBD> Q <20>Ͳ<EFBFBD><CDB2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Э<EFBFBD><D0AD><EFBFBD><EFBFBD> R
|
||||
float innovation = inData - filter->prevData;
|
||||
float alpha = fabsf(innovation) > 1.0f ? 0.2f : 0.8f; // <20><><EFBFBD>ݴ<EFBFBD><DDB4>µ<EFBFBD><C2B5><EFBFBD> ALPHA
|
||||
filter->q = alpha * filter->q + (1 - alpha) * (innovation * innovation);
|
||||
|
||||
// Ԥ<><D4A4>Э<EFBFBD><D0AD><EFBFBD><EFBFBD>
|
||||
filter->p = filter->p + filter->q;
|
||||
|
||||
// <20><><EFBFBD>¿<EFBFBD><C2BF><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
filter->kGain = filter->p / (filter->p + filter->r);
|
||||
|
||||
// <20><><EFBFBD>¹<EFBFBD><C2B9><EFBFBD>ֵ
|
||||
float updatedData = filter->prevData + filter->kGain * innovation;
|
||||
|
||||
// <20><><EFBFBD><EFBFBD>Э<EFBFBD><D0AD><EFBFBD><EFBFBD>
|
||||
filter->p = (1 - filter->kGain) * filter->p;
|
||||
|
||||
// <20><><EFBFBD><EFBFBD>״̬
|
||||
filter->prevData = updatedData;
|
||||
|
||||
return updatedData;
|
||||
}
|
||||
|
||||
@@ -31,17 +31,46 @@ typedef struct
|
||||
float alpha; //<2F>˲<EFBFBD>ϵ<EFBFBD><CFB5>
|
||||
} hight_pass_filter_t;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
float avg; // <20><><EFBFBD><EFBFBD>ƽ<EFBFBD><C6BD>ֵ
|
||||
int size; // <20><><EFBFBD>ڴ<EFBFBD>С
|
||||
float *window; // <20>洢<EFBFBD><E6B4A2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
} moving_average_filter_t;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
float prevData; // <20><>ǰ<EFBFBD><C7B0>ֵ
|
||||
float p; // <20><><EFBFBD><EFBFBD>Э<EFBFBD><D0AD><EFBFBD><EFBFBD>
|
||||
float q; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Э<EFBFBD><D0AD><EFBFBD><EFBFBD>
|
||||
float r; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Э<EFBFBD><D0AD><EFBFBD><EFBFBD>
|
||||
float kGain; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
} Kalman_filter_t;
|
||||
|
||||
|
||||
//<EFBFBD><EFBFBD>ʼ<EFBFBD><EFBFBD><EFBFBD>˲<EFBFBD>ϵ<EFBFBD><EFBFBD>
|
||||
// <EFBFBD><EFBFBD>ͨ<EFBFBD>˲<EFBFBD><EFBFBD><EFBFBD>
|
||||
void Init_lowPass_alpha(low_pass_filter_t* const filter,const float ts, const float fc);
|
||||
//<2F><>ͨ<EFBFBD>˲<EFBFBD>
|
||||
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);
|
||||
|
||||
//<EFBFBD><EFBFBD>ʼ<EFBFBD><EFBFBD><EFBFBD>˲<EFBFBD>ϵ<EFBFBD><EFBFBD>
|
||||
// <EFBFBD><EFBFBD>ͨ<EFBFBD>˲<EFBFBD><EFBFBD><EFBFBD>
|
||||
void Init_hightPass_alpha(hight_pass_filter_t* const filter,const float ts, const float fc);
|
||||
//<2F><>ͨ<EFBFBD>˲<EFBFBD>
|
||||
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);
|
||||
|
||||
// <20><><EFBFBD><EFBFBD>ƽ<EFBFBD><C6BD><EFBFBD>˲<EFBFBD><CBB2><EFBFBD>
|
||||
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);
|
||||
|
||||
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>˲<EFBFBD><CBB2><EFBFBD>
|
||||
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);
|
||||
|
||||
// <20><EFBFBD><DEBC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>˲<EFBFBD><CBB2><EFBFBD>
|
||||
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);
|
||||
|
||||
// <20><><EFBFBD><EFBFBD>Ӧ<EFBFBD><D3A6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>˲<EFBFBD><CBB2><EFBFBD>
|
||||
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_ */
|
||||
|
||||
@@ -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]);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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_ */
|
||||
|
||||
223
code/power_ctrl.c
Normal file
223
code/power_ctrl.c
Normal file
@@ -0,0 +1,223 @@
|
||||
/*
|
||||
* power_ctrl.c
|
||||
*
|
||||
* Created on: 2025<32><35>10<31><30>17<31><37>
|
||||
* 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]);
|
||||
// }
|
||||
}
|
||||
20
code/power_ctrl.h
Normal file
20
code/power_ctrl.h
Normal file
@@ -0,0 +1,20 @@
|
||||
/*
|
||||
* power_ctrl.h
|
||||
*
|
||||
* Created on: 2025<32><35>10<31><30>17<31><37>
|
||||
* 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_ */
|
||||
@@ -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
|
||||
|
||||
@@ -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} // <20><>ѹ<EFBFBD><D1B9><EFBFBD><EFBFBD>ϵ<EFBFBD><CFB5>
|
||||
#define AMP_PROTECT_K 1.1 // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ϵ<EFBFBD><CFB5>
|
||||
#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} // <20><>ѹ<EFBFBD><D1B9><EFBFBD><EFBFBD>ϵ<EFBFBD><CFB5>
|
||||
#define AMP_PROTECT_K 1.1f // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ϵ<EFBFBD><CFB5>
|
||||
|
||||
typedef struct
|
||||
{
|
||||
@@ -49,6 +57,8 @@ typedef struct
|
||||
float ampK[2]; // <20><><EFBFBD><EFBFBD>Уϵ<D7BC><CFB5>
|
||||
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; // <20>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
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);
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
*/
|
||||
|
||||
#include "vofa_client.h"
|
||||
|
||||
#include "esp8266.h"
|
||||
|
||||
float vofa_data[VOFA_RECEIVE_CH] = {0}; // <20><>¼<EFBFBD><C2BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
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) // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʼ<EFBFBD><CABC>ʧ<EFBFBD><CAA7><EFBFBD><EFBFBD><F2B2BBB7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
{
|
||||
@@ -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
|
||||
|
||||
@@ -91,7 +91,7 @@ void VOFA_Set_JustFloat_Data(int CH, float data); //
|
||||
void VOFA_Set_JustFloat_Datas_From_Start(int num,...); // <20><><EFBFBD>ôӵ<C3B4>һ<EFBFBD><D2BB>ͨ<EFBFBD><CDA8><EFBFBD><EFBFBD>ʼ<EFBFBD>Ķ<EFBFBD><C4B6><EFBFBD>ͨ<EFBFBD><CDA8><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
void VOFA_Send_Datas(int num); // <20>ӵ<EFBFBD>һ<EFBFBD><D2BB>ͨ<EFBFBD><CDA8><EFBFBD><EFBFBD>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ָ<EFBFBD><D6B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
void VOFA_Send_JustFloat_Image(int IMG_ID, int IMG_WIDTH, int IMG_HEIGHT, int IMG_DATA_SIZE, ImgFormat IMG_FORMAT,uint8 *IMG_DATA); // <20><><EFBFBD><EFBFBD>ͼ<EFBFBD><CDBC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
void VOFA_Receiver_Callback(void); // <20><><EFBFBD>ջص<D5BB><D8B5><EFBFBD><EFBFBD><EFBFBD>,<2C><>Ҫ<EFBFBD><D2AA><EFBFBD><EFBFBD>ѭ<EFBFBD><D1AD><EFBFBD><EFBFBD><EFBFBD>߽<EFBFBD><DFBD><EFBFBD><EFBFBD>ж<EFBFBD><D0B6>е<EFBFBD><D0B5><EFBFBD>
|
||||
void VOFA_Receiver_Callback(uint8 rev_data); // <20><><EFBFBD>ջص<D5BB><D8B5><EFBFBD><EFBFBD><EFBFBD>,<2C><>Ҫ<EFBFBD><D2AA><EFBFBD><EFBFBD>ѭ<EFBFBD><D1AD><EFBFBD><EFBFBD><EFBFBD>߽<EFBFBD><DFBD><EFBFBD><EFBFBD>ж<EFBFBD><D0B6>е<EFBFBD><D0B5><EFBFBD>
|
||||
|
||||
#endif /* CODE_VOFA_CLIENT_H_ */
|
||||
|
||||
|
||||
97
esp8266/esp8266/esp8266.ino
Normal file
97
esp8266/esp8266/esp8266.ino
Normal file
@@ -0,0 +1,97 @@
|
||||
#include <ESP8266WiFi.h>
|
||||
|
||||
|
||||
#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<char>(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<char>(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;
|
||||
|
||||
}
|
||||
@@ -38,7 +38,7 @@
|
||||
|
||||
#include "zf_common_typedef.h"
|
||||
|
||||
#define PWM_DUTY_MAX 10000 // PWM<57><4D><EFBFBD><EFBFBD>ռ<EFBFBD>ձ<EFBFBD> <20><><EFBFBD><EFBFBD>ռ<EFBFBD>ձ<EFBFBD>Խ<EFBFBD><D4BD>ռ<EFBFBD>ձȵIJ<C8B5><C4B2><EFBFBD>ֵԽС
|
||||
#define PWM_DUTY_MAX 100000 // PWM<57><4D><EFBFBD><EFBFBD>ռ<EFBFBD>ձ<EFBFBD> <20><><EFBFBD><EFBFBD>ռ<EFBFBD>ձ<EFBFBD>Խ<EFBFBD><D4BD>ռ<EFBFBD>ձȵIJ<C8B5><C4B2><EFBFBD>ֵԽС
|
||||
|
||||
// <20><>ö<EFBFBD>ٶ<EFBFBD><D9B6>岻<EFBFBD><E5B2BB><EFBFBD><EFBFBD><EFBFBD>û<EFBFBD><C3BB><EFBFBD>
|
||||
typedef enum // ö<><C3B6>PWM<57><4D><EFBFBD><EFBFBD>
|
||||
|
||||
@@ -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(); // <20><>ȡʱ<C8A1><CAB1>Ƶ<EFBFBD><C6B5><<3C><><EFBFBD>ر<EFBFBD><D8B1><EFBFBD>>
|
||||
debug_init(); // <20><>ʼ<EFBFBD><CABC>Ĭ<EFBFBD>ϵ<EFBFBD><CFB5>Դ<EFBFBD><D4B4><EFBFBD>
|
||||
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); // <20><>ʼ<EFBFBD><CABC>PIT0 <20><><EFBFBD>ڶ<EFBFBD>ȡ<EFBFBD><C8A1>Դģ<D4B4><C4A3>״̬
|
||||
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);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
29
user/isr.c
29
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"
|
||||
|
||||
// <20><><EFBFBD><EFBFBD>TCϵ<43><CFB5>Ĭ<EFBFBD><C4AC><EFBFBD>Dz<EFBFBD>֧<EFBFBD><D6A7><EFBFBD>ж<EFBFBD>Ƕ<EFBFBD>ģ<D7B5>ϣ<EFBFBD><CFA3>֧<EFBFBD><D6A7><EFBFBD>ж<EFBFBD>Ƕ<EFBFBD><C7B6><EFBFBD><EFBFBD>Ҫ<EFBFBD><D2AA><EFBFBD>ж<EFBFBD><D0B6><EFBFBD>ʹ<EFBFBD><CAB9> interrupt_global_enable(0); <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ж<EFBFBD>Ƕ<EFBFBD><C7B6>
|
||||
// <20><EFBFBD>˵ʵ<CBB5><CAB5><EFBFBD>Ͻ<EFBFBD><CFBD><EFBFBD><EFBFBD>жϺ<D0B6>TCϵ<43>е<EFBFBD>Ӳ<EFBFBD><D3B2><EFBFBD>Զ<EFBFBD><D4B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> interrupt_global_disable(); <20><><EFBFBD>ܾ<EFBFBD><DCBE><EFBFBD>Ӧ<EFBFBD>κε<CEBA><CEB5>жϣ<D0B6><CFA3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD><D2AA><EFBFBD><EFBFBD><EFBFBD>Լ<EFBFBD><D4BC>ֶ<EFBFBD><D6B6><EFBFBD><EFBFBD><EFBFBD> interrupt_global_enable(0); <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>жϵ<D0B6><CFB5><EFBFBD>Ӧ<EFBFBD><D3A6>
|
||||
|
||||
@@ -44,7 +48,7 @@ IFX_INTERRUPT(cc60_pit_ch0_isr, 0, CCU6_0_CH0_ISR_PRIORITY)
|
||||
{
|
||||
interrupt_global_enable(0); // <20><><EFBFBD><EFBFBD><EFBFBD>ж<EFBFBD>Ƕ<EFBFBD><C7B6>
|
||||
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); // <20><><EFBFBD><EFBFBD><EFBFBD>ж<EFBFBD>Ƕ<EFBFBD><C7B6>
|
||||
// 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 // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> debug <20><><EFBFBD><EFBFBD><EFBFBD>ж<EFBFBD>
|
||||
debug_interrupr_handler(); // <20><><EFBFBD><EFBFBD> debug <20><><EFBFBD>ڽ<EFBFBD><DABD>մ<EFBFBD><D5B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD>ݻᱻ debug <20><><EFBFBD>λ<EFBFBD><CEBB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȡ
|
||||
#endif // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><DEB8><EFBFBD> DEBUG_UART_INDEX <20><><EFBFBD><EFBFBD><EFBFBD>δ<EFBFBD><CEB4><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD>ŵ<EFBFBD><C5B5><EFBFBD>Ӧ<EFBFBD>Ĵ<EFBFBD><C4B4><EFBFBD><EFBFBD>ж<EFBFBD>ȥ
|
||||
}
|
||||
|
||||
void isr_uart0_rx_interrupt_hook_back(uint8 rev_data)
|
||||
{
|
||||
VOFA_Receiver_Callback(rev_data);
|
||||
}
|
||||
|
||||
// <20><><EFBFBD><EFBFBD>1Ĭ<31><C4AC><EFBFBD><EFBFBD><EFBFBD>ӵ<EFBFBD><D3B5><EFBFBD><EFBFBD><EFBFBD>ͷ<EFBFBD><CDB7><EFBFBD>ô<EFBFBD><C3B4><EFBFBD>
|
||||
IFX_INTERRUPT(uart1_tx_isr, 0, UART1_TX_INT_PRIO)
|
||||
@@ -210,7 +226,14 @@ IFX_INTERRUPT(uart2_tx_isr, 0, UART2_TX_INT_PRIO)
|
||||
IFX_INTERRUPT(uart2_rx_isr, 0, UART2_RX_INT_PRIO)
|
||||
{
|
||||
interrupt_global_enable(0); // <20><><EFBFBD><EFBFBD><EFBFBD>ж<EFBFBD>Ƕ<EFBFBD><C7B6>
|
||||
wireless_module_uart_handler(); // <20><><EFBFBD><EFBFBD>ģ<EFBFBD><C4A3>ͳһ<CDB3>ص<EFBFBD><D8B5><EFBFBD><EFBFBD><EFBFBD>
|
||||
// wireless_module_uart_handler(); // <20><><EFBFBD><EFBFBD>ģ<EFBFBD><C4A3>ͳһ<CDB3>ص<EFBFBD><D8B5><EFBFBD><EFBFBD><EFBFBD>
|
||||
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);
|
||||
// }
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user