新电机完成hall+定时器完成水平调速

This commit is contained in:
起床就犯困 2025-06-09 09:44:39 +08:00
parent 9b021d1a0d
commit 845e5d73d5
10 changed files with 199 additions and 5 deletions

5
.vscode/settings.json vendored Normal file
View File

@ -0,0 +1,5 @@
{
"files.associations": {
"speed_to_hall.h": "c"
}
}

View File

@ -50,6 +50,8 @@
#include "systick.h"
#include "service_error_count.h"
#include "speed_to_hall.h"
void task_printf_init();
//
@ -126,6 +128,7 @@ static void task_start (void *p_arg)
// term_printf("\n udp init \r\n\r\n");
OSTimeDlyHMSM(0u, 0u, 0u, 100u);
init_hall_speed_module();
//光电开关初始化
init_photoelectric_switch_module();

View File

@ -336,7 +336,6 @@
#ifdef PTZ_SUPER_LIGHT_WORM_L6235D_AS5047D_24V
///水平电机减速比
#define PTZ_HORI_MOTOR_RATIO 3.0
///水平大齿轮减速比
@ -346,7 +345,7 @@
///水平电机调速模拟电压最大值
#define PTZ_HORI_VR_MAX 1999
///水平电机调速模拟电压最小值
#define PTZ_HORI_VR_MIN 0
#define PTZ_HORI_VR_MIN 300
///转速单位 转/每分
///水平电机最大转速

View File

@ -7,6 +7,8 @@
#include "full_bridge.h"
#include "as5047d.h"
#include "speed_to_hall.h"
#ifdef PTZ_BLDC_MOTOR
//发送云台实际转速
@ -109,6 +111,10 @@ static float ptz_hori_pid_calculate(float H_SampSpeed)
// }
//增量计算
H_IIncPid = g_ptz.hori_pid.A * H_IError + g_ptz.hori_pid.B * g_ptz.hori_pid.LastError + g_ptz.hori_pid.C * g_ptz.hori_pid.PrevError;
// H_IIncPid = PTZ_HORI_PID_HORI_KP;
//存储误差,用于下次计算
g_ptz.hori_pid.PrevError = g_ptz.hori_pid.LastError;
g_ptz.hori_pid.LastError = H_IError;
@ -298,7 +304,8 @@ static void ptz_hori_pid_task()
#endif
#ifdef PTZ_PID_HALL_SPEED //»ô¶û·´À¡²âËÙ
// #ifdef PTZ_PID_HALL_SPEED //»ô¶û·´À¡²âËÙ
#if 0
g_ptz.hori_pid.hall_h1_count = 0;
g_ptz.hori_pid.hall_h2_count = 0;
g_ptz.hori_pid.hall_h3_count = 0;
@ -610,6 +617,42 @@ static void ptz_hori_pid_task()
// }
// else
#ifdef PTZ_HALL_SPEED_SL
if (g_ptz.hori_speed_actual == 0) {
h_pwm_duty_change(PTZ_HORI_VR_MIN);
}
g_speed_to_hall.hori_speed_t.flag = calculation_enable;
while (!(g_speed_to_hall.hori_speed_t.flag == calculation_start
|| g_ptz.hori_start_stop_set == PTZ_HORI_STOP)) {
OSTimeDlyHMSM(0u, 0u, 0u, 1u);
}
OSTimeDlyHMSM(0u, 0u, 0u, 100u);
g_speed_to_hall.hori_speed_t.flag = calculation_ok;
while (!(g_speed_to_hall.hori_speed_t.flag == calculation_end
|| g_ptz.hori_start_stop_set == PTZ_HORI_STOP)) {
OSTimeDlyHMSM(0u, 0u, 0u, 1u);
}
int32_t time;
if (g_speed_to_hall.hori_speed_t.endTime_60ms > g_speed_to_hall.hori_speed_t.startTime_60ms) {
time = (g_speed_to_hall.hori_speed_t.endTime_60ms - g_speed_to_hall.hori_speed_t.startTime_60ms - 1) * SPEED_TIMER_PERIOD
+ g_speed_to_hall.hori_speed_t.endTime_us + (SPEED_TIMER_PERIOD - g_speed_to_hall.hori_speed_t.startTime_us);
}
else if (g_speed_to_hall.hori_speed_t.endTime_60ms = g_speed_to_hall.hori_speed_t.startTime_60ms) {
time = g_speed_to_hall.hori_speed_t.endTime_us - g_speed_to_hall.hori_speed_t.startTime_us;
}
else {
time = (g_speed_to_hall.hori_speed_t.endTime_60ms + (TIME_60MS_MAX - g_speed_to_hall.hori_speed_t.startTime_60ms) - 1) * SPEED_TIMER_PERIOD
+ g_speed_to_hall.hori_speed_t.endTime_us + (SPEED_TIMER_PERIOD - g_speed_to_hall.hori_speed_t.startTime_us);
}
g_ptz.hori_speed_actual = (float)g_speed_to_hall.hori_speed_t.hallNum * 5000000.0 / (float)time / PTZ_HORI_RATIO;
#endif
if(g_ptz.hori_speed_actual > g_ptz.hori_speed_set * 2)
{//速度大于设定速度的3倍
g_ptz.hori_pid.PidUT_float = g_ptz.hori_pid.PidUT_float * 0.7;////新增直线减速
@ -661,7 +704,7 @@ static void ptz_hori_pid_task()
//将PID输出的电机转速模拟电压等级输入到模拟电压输出芯片
// hori_dac0_data_out(g_ptz.hori_pid.PidUT_uint);
h_pwm_duty_change(g_ptz.hori_pid.PidUT_uint);
// h_pwm_duty_change(g_ptz.hori_pid.PidUT_uint);
//将当前PID输出值保存
g_ptz.hori_pid.LastUT_float = g_ptz.hori_pid.PidUT_float;

View File

@ -230,6 +230,8 @@
//#define PTZ_VERT_PID_JY02A_SPEED
#define PTZ_PID_HALL_SPEED 1
#define PTZ_HALL_SPEED_SL 1
#define PTZ_HORI_PID_T 30u
#define PTZ_HORI_PID_HORI_KP 20.0//比例系数
#define PTZ_HORI_PID_HORI_TI 100.0 //积分系数

View File

@ -8,6 +8,9 @@
#include "gd32f4xx_it.h"
#include "ptz_type_select.h"
#include "get_angle.h"
#include "speed_to_hall.h"
/// @brief 外部中断初始化
/// @param[in] usart_periph:EXTI_IRQ_init
/// @return none
@ -604,6 +607,24 @@ void ptz_H_HALL_IRQHandler(exti_line_enum hall_linex)
#ifdef PTZ_BLDC_MOTOR
#ifdef PTZ_HALL_FEEDBACK
if (g_speed_to_hall.hori_speed_t.flag == calculation_enable) {
g_speed_to_hall.hori_speed_t.startTime_us = TIMER_CNT(TIMER6);
g_speed_to_hall.hori_speed_t.startTime_60ms = g_speed_to_hall.time_60ms;
g_speed_to_hall.hori_speed_t.hallNum = 0;
g_speed_to_hall.hori_speed_t.flag = calculation_start;
}
if (g_speed_to_hall.hori_speed_t.flag == calculation_start) {
g_speed_to_hall.hori_speed_t.hallNum++;
}
if (g_speed_to_hall.hori_speed_t.flag == calculation_ok) {
// g_speed_to_hall.hori_speed_t.hallNum++;
g_speed_to_hall.hori_speed_t.endTime_us = TIMER_CNT(TIMER6);
g_speed_to_hall.hori_speed_t.endTime_60ms = g_speed_to_hall.time_60ms;
g_speed_to_hall.hori_speed_t.flag = calculation_end;
}
switch(hall_linex)
{
case EXTI_10://水平HALL-1

View File

@ -0,0 +1,61 @@
#include "speed_to_hall.h"
speed_hall g_speed_to_hall = {0};
/**
* @brief +
* @param
* @retval
*
*/
void init_hall_speed_module(void)
{
timer_parameter_struct timer_initpara;
rcu_periph_clock_enable(RCU_TIMER6);
timer_struct_para_init(&timer_initpara);
timer_deinit(TIMER6);
// TIMER1 configuration
timer_initpara.prescaler = 99;
timer_initpara.alignedmode = TIMER_COUNTER_EDGE;//计数模式,边缘对齐
timer_initpara.counterdirection = TIMER_COUNTER_UP;//计数方向
timer_initpara.period = SPEED_TIMER_PERIOD;//计数重装载值确定周期该值可在任意时刻进行修改以改变周期TIMER_CAR(timer_periph) = (uint32_t)initpara->period;
timer_initpara.clockdivision = TIMER_CKDIV_DIV1;//时钟分频
timer_initpara.repetitioncounter = 0;//计数重复值
timer_init(TIMER6, &timer_initpara);
timer_auto_reload_shadow_enable(TIMER6);
timer_enable(TIMER6);
timer_interrupt_enable(TIMER6, TIMER_INT_UP);//定时器更新中断使能
nvic_irq_enable(TIMER6_IRQn, 2U, 2U);
g_speed_to_hall.hori_speed_t.flag = calculation_disable;
g_speed_to_hall.vert_speed_t.flag = calculation_disable;
}
/**
* @brief 1
* @param
* @retval
*
*/
void TIMER6_IRQHandler(void)
{
if(RESET != timer_interrupt_flag_get(TIMER6, TIMER_INT_FLAG_UP)) {
g_speed_to_hall.time_60ms++;
if (g_speed_to_hall.time_60ms >= TIME_60MS_MAX) {
g_speed_to_hall.time_60ms = 0;
}
timer_interrupt_flag_clear(TIMER6, TIMER_INT_FLAG_UP);
}
}

View File

@ -0,0 +1,40 @@
#ifndef __BSP_DRIVER_SPEED_HALL_H_
#define __BSP_DRIVER_SPEED_HALL_H_
//#include "ptz_type_select.h"
#include "gd32f4xx.h"
typedef struct _speed_t {
uint32_t hallNum; //霍尔编码器次数(运行方向的)
uint32_t startTime_60ms; //开始时间
uint32_t startTime_us;
uint32_t endTime_60ms; //结束时间
uint32_t endTime_us;
uint8_t flag; //标志位
}speed_t;
typedef struct _speed_to_hall {
speed_t hori_speed_t;
speed_t vert_speed_t;
uint32_t time_60ms;
}speed_hall;
typedef enum _calculation_speed_type {
calculation_disable = 0, //未开始计算速度
calculation_enable = 1, //使能计算速度
calculation_ok = 2, //达到计算速度时间
calculation_start = 3, //开始计算速度
calculation_end = 4, //结束计算速度
}calculation_speed_type;
extern speed_hall g_speed_to_hall;
#define TIME_60MS_MAX 4000000000
#define SPEED_TIMER_PERIOD 60000
void init_hall_speed_module(void);
#endif

View File

@ -409,6 +409,7 @@
<state>$PROJ_DIR$\..\BSP\Driver\timer</state>
<state>$PROJ_DIR$\..\BSP\Driver\full_bridge</state>
<state>$PROJ_DIR$\..\APP\Service</state>
<state>$PROJ_DIR$\..\BSP\Driver\speed</state>
</option>
<option>
<name>CCStdIncCheck</name>
@ -825,7 +826,7 @@
</option>
<option>
<name>IlinkIcfFile</name>
<state>D:\psx\Pan-Tilt\1.software\HY\4.0 LW21-01B\BSP\IAR\GD32F450xE_APP.icf</state>
<state>D:\psx\Pan-Tilt\1.software\HY\4.0 LW21-01B\BSP\IAR\GD32F450xE.icf</state>
</option>
<option>
<name>IlinkIcfFileSlave</name>
@ -2622,6 +2623,7 @@
<state>$PROJ_DIR$\..\BSP\Driver\timer</state>
<state>$PROJ_DIR$\..\BSP\Driver\full_bridge</state>
<state>$PROJ_DIR$\..\BSP\Driver\timer</state>
<state>$PROJ_DIR$\..\BSP\Driver\speed</state>
</option>
<option>
<name>CCStdIncCheck</name>
@ -2912,6 +2914,15 @@
<name>$PROJ_DIR$\..\BSP\Driver\ringqueue\ring_queue.h</name>
</file>
</group>
<group>
<name>speed</name>
<file>
<name>$PROJ_DIR$\..\BSP\Driver\speed\speed_to_hall.c</name>
</file>
<file>
<name>$PROJ_DIR$\..\BSP\Driver\speed\speed_to_hall.h</name>
</file>
</group>
<group>
<name>timer</name>
<file>

View File

@ -3182,6 +3182,15 @@
<name>$PROJ_DIR$\..\BSP\Driver\ringqueue\ring_queue.h</name>
</file>
</group>
<group>
<name>speed</name>
<file>
<name>$PROJ_DIR$\..\BSP\Driver\speed\speed_to_hall.c</name>
</file>
<file>
<name>$PROJ_DIR$\..\BSP\Driver\speed\speed_to_hall.h</name>
</file>
</group>
<group>
<name>timer</name>
<file>