This repository has been archived on 2025-01-02. You can view files and clone it, but cannot push or open issues or pull requests.
torsion/User/board/board.c

97 lines
2.1 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include "board.h"
#include "motor.h"
uart_t *uarts[UART_NUM_MAX];
motor_t *motor;
motor_ctrl_t *motor_param = NULL;
uint8_t motor_state = MOTOR_IDEL;
adc_t adc1 = {
.adc = ADC1,
.dma = DMA1,
.dma_channel = LL_DMA_CHANNEL_1,
};
// 单位N·m
float32 torsion_detect(uint16_t adc)
{
float32 val = 0;
val = ((float32)adc - 1500) / (1.934 * 3) * 10;
return val;
}
// 单位kPa
float32 pressure_detect(uint16_t adc)
{
float32 val = 0;
val = ((float32)adc - 600) / (10 * 15) / 16 * 10;
return val;
}
// 单位SLPM
float32 flow_detect(uint16_t adc)
{
float32 val = 0;
val = ((float32)adc - 600) / (10 * 15) / 16 * 300;
return val;
}
static void motor_init(void)
{
motor = motor_create(STEP_MOTOR);
gpio_t dir = {
.port = DIR_GPIO_Port,
.pin = DIR_Pin,
};
gpio_t en = {
.port = ENA_GPIO_Port,
.pin = ENA_Pin,
};
motor->handle.step_motor.interface.init(motor, dir, en, MAX_STEP_ANGLE, TIM21, LL_TIM_CHANNEL_CH2);
}
void motor_process_init(void)
{
motor_param = (motor_ctrl_t *)osel_mem_alloc(sizeof(motor_ctrl_t));
DBG_ASSERT(motor_param != NULL __DBG_LINE);
osel_memset((uint8_t *)motor_param, 0, sizeof(motor_ctrl_t));
}
void motor_process(void)
{
step_motor_t *step_motor = &motor->handle.step_motor;
switch (motor_state)
{
case MOTOR_RUN:
step_motor->interface.run(motor, (dir_e)motor_param->dir);
motor_state = MOTOR_IDEL;
break;
case MOTOR_STOP:
step_motor->interface.stop(motor);
motor_state = MOTOR_IDEL;
case MOTOR_STEP:
step_motor->interface.set_angle(motor, motor_param->angle, (dir_e)motor_param->dir);
motor_state = MOTOR_IDEL;
break;
case MOTOR_IDEL:
break;
default:
break;
}
}
void board_init(void)
{
motor = NULL;
osel_memset((uint8_t *)uarts, 0, sizeof(uarts));
my_mem_init(SRAMIN);
motor_init(); // 电机初始化
adc_init(adc1); // 初始化ADC1通道默认采集AD
ENABLE_TIM(TIM6); // 任务流程定时器使能
}