位置输出

This commit is contained in:
许晟昊 2024-12-08 11:04:18 +08:00
parent 4d406fcf97
commit fcd6b4cfbf
4 changed files with 1671 additions and 1426 deletions

File diff suppressed because it is too large Load Diff

View File

@ -1,8 +1,41 @@
#include "work.h" #include "work.h"
#include "board.h" #include "board.h"
#define OUTPUT_INFORMATION_CYCLE_BASE 100
work_t work; work_t work;
/**
* @brief
*
*
*/
static void output_information(void)
{
char data[128];
uint8_t len = 0;
int32_t rst = 0;
osel_memset((uint8_t *)data, 0, ARRAY_LEN(data));
if (work.type == WORK_MOTOR_POS)
{
rst = snprintf(data, ARRAY_LEN(data), "%f,%d,%f\r\n", work.target_pos, work.encoder_cnt, work.pwm_percent);
}
else if (work.type == WORK_MOTOR_SPEED)
{
}
else
{
return;
}
if (rst == 0 || rst > ARRAY_LEN(data))
{
return;
}
len = osel_mstrlen((uint8_t *)data);
uart_send((uint8_t *)data, len);
}
static void pwm_map(void) static void pwm_map(void)
{ {
set_motor_pwm(work.pwm_percent); set_motor_pwm(work.pwm_percent);
@ -117,7 +150,14 @@ void work_process(void)
default: default:
break; break;
} }
return; if (work.enter_cnt++ % (OUTPUT_INFORMATION_CYCLE_BASE / work.timer_cycle) == 0)
{
output_information();
}
work.pid.pid_u.fuzzy.set_kp(&work.pid.pid_u.fuzzy, work.pid_params.kp);
work.pid.pid_u.fuzzy.set_ki(&work.pid.pid_u.fuzzy, work.pid_params.ki);
work.pid.pid_u.fuzzy.set_kd(&work.pid.pid_u.fuzzy, work.pid_params.kd);
} }
void work_encoder_exti(void) void work_encoder_exti(void)
@ -157,5 +197,6 @@ void work_init(void)
} }
work.target_pos = 20; work.target_pos = 20;
work.timer_cycle = TIM_CYCLE(WORK_TIM);
return; return;
} }

View File

@ -18,6 +18,8 @@ typedef enum
typedef struct typedef struct
{ {
work_e type; work_e type;
uint8_t timer_cycle; // 定时器周期
uint32_t enter_cnt; // 进入次数
int32_t encoder_cnt; // 编码器位置 int32_t encoder_cnt; // 编码器位置
float32 pwm_percent; // PWM占空比 float32 pwm_percent; // PWM占空比
uint32_t pwm_feq; // PWM频率 uint32_t pwm_feq; // PWM频率

View File

@ -11,7 +11,7 @@
*/ */
#ifndef __TIMS_H__ #ifndef __TIMS_H__
#define __TIMS_H__ #define __TIMS_H__
#include "lib.h"
/** /**
Timer overflow time calculation formula Timer overflow time calculation formula
Tout = ((ARR + 1)*(PSC + 1)) / Tclk Tout = ((ARR + 1)*(PSC + 1)) / Tclk
@ -102,4 +102,15 @@ With these calculated values, both ARR and PSC are within the range of 0 to 6553
} \ } \
} while (__LINE__ == -1) } while (__LINE__ == -1)
/**
* @brief
*
* TIMx的周期时间
*
* @param TIMx
*
* @return
*/
#define TIM_CYCLE(TIMx) ((LL_TIM_GetAutoReload(TIMx) + 1) * 0.1)
#endif ///< __TIMS_H__ #endif ///< __TIMS_H__