MRobot/assets/User_code/device/servo.c
2025-09-20 00:32:32 +08:00

47 lines
1019 B
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.

/*
pwm<77><6D><EFBFBD>ƶ<EFBFBD><C6B6>
*/
/*Includes -----------------------------------------*/
#include "bsp/pwm.h"
#include "servo.h"
/* USER INCLUDE BEGIN */
/* USER INCLUDE END */
#define SERVO_MIN_DUTY 0.025f
#define SERVO_MAX_DUTY 0.125f
/* USER DEFINE BEGIN */
/* USER DEFINE END */
/**
* @brief
* @param
* @retval BSP_OK / BSP_ERR
*/
int8_t SERVO_Init(SERVO_t *servo) {
if (servo == NULL) return BSP_ERR;
return BSP_PWM_Start(servo->pwm_ch);
}
int8_t SERVO_SetAngle(SERVO_t *servo, float angle) {
if (servo == NULL) return BSP_ERR;
/*<2A><><EFBFBD>ƽǶȷ<C7B6>Χ*/
if (angle < 0.0f) angle = 0.0f;
if (angle > 180.0f) angle = 180.0f;
/*<2A>Ƕ<EFBFBD>ӳ<EFBFBD>ռ<E4B5BD>ձ<EFBFBD>*/
float duty = servo->min_duty + (angle / 180.0f) * (servo->max_duty - servo->min_duty);
return BSP_PWM_Set(servo->pwm_ch, duty);
}
int8_t SERVO_Stop(SERVO_t *servo) {
if (servo == NULL) return BSP_ERR;
return BSP_PWM_Stop(servo->pwm_ch);
}