修复led和rm的reserv

This commit is contained in:
Robofish 2025-09-22 10:24:05 +08:00
parent a29e097978
commit d16427f7d4
3 changed files with 49 additions and 12 deletions

View File

@ -3,17 +3,13 @@
*/ */
/*Includes -----------------------------------------*/ /*Includes -----------------------------------------*/
#include "device/led.h" #include "device/led.h"
#include "bsp/gpio.h"
#include "bsp/pwm.h"
#include "device.h" #include "device.h"
/* Private define ----------------------------------------------------------- */ /* Private define ----------------------------------------------------------- */
/* Private macro ------------------------------------------------------------ */ /* Private macro ------------------------------------------------------------ */
/* Private typedef ---------------------------------------------------------- */ /* Private typedef ---------------------------------------------------------- */
#ifdef LED_PWM
int8_t DEVICE_LED_PWM_Set(BSP_PWM_Channel_t channel, float duty_cycle) int8_t DEVICE_LED_PWM_Set(BSP_PWM_Channel_t channel, float duty_cycle)
{ {
if (duty_cycle < 0.0f || duty_cycle > 1.0f) { if (duty_cycle < 0.0f || duty_cycle > 1.0f) {
@ -24,7 +20,9 @@ int8_t DEVICE_LED_PWM_Set(BSP_PWM_Channel_t channel, float duty_cycle)
BSP_PWM_SetComp(channel, pulse); BSP_PWM_SetComp(channel, pulse);
return DEVICE_OK; return DEVICE_OK;
} }
#endif
#ifdef LED_GPIO
int8_t DEVICE_LED_GPIO_Set(BSP_GPIO_t gpio, bool value) int8_t DEVICE_LED_GPIO_Set(BSP_GPIO_t gpio, bool value)
{ {
if (value) { if (value) {
@ -34,5 +32,6 @@ int8_t DEVICE_LED_GPIO_Set(BSP_GPIO_t gpio, bool value)
} }
return DEVICE_OK; return DEVICE_OK;
} }
#endif

View File

@ -5,27 +5,58 @@ extern "C" {
#endif #endif
/* Includes ----------------------------------------------------------------- */ /* Includes ----------------------------------------------------------------- */
#include <stdint.h> /* USER DEFIN BEGIN */
#include "bsp/gpio.h"
#include "bsp/pwm.h"
#include "bsp/bsp.h"
/* USER DEFIN END */
#include <stdint.h>
#ifdef LED_GPIO
#include "bsp/gpio.h"
#endif
#ifdef LED_PWM
#include "bsp/pwm.h"
#endif
#include "bsp/bsp.h"
/* Exported constants ------------------------------------------------------- */ /* Exported constants ------------------------------------------------------- */
/* Exported macro ----------------------------------------------------------- */ /* Exported macro ----------------------------------------------------------- */
/* Exported types ----------------------------------------------------------- */ /* Exported types ----------------------------------------------------------- */
typedef struct { typedef struct {
#ifdef LED_GPIO
BSP_GPIO_t gpio; BSP_GPIO_t gpio;
#endif
#ifdef LED_PWM
BSP_PWM_Channel_t channel; BSP_PWM_Channel_t channel;
#endif
} DEVICE_LED_t; } DEVICE_LED_t;
extern DEVICE_LED_t LED_Map; extern DEVICE_LED_t LED_Map;
/* Exported functions prototypes -------------------------------------------- */ /* Exported functions prototypes -------------------------------------------- */
#ifdef LED_PWM
int8_t DEVICE_LED_PWM_Set(BSP_PWM_Channel_t channel, float duty_cycle)
{
if (duty_cycle < 0.0f || duty_cycle > 1.0f) {
return DEVICE_ERR_NULL; // 错误:占空比超出范围
}
uint16_t pulse = (uint16_t)(duty_cycle * (float)UINT16_MAX);
BSP_PWM_Start(channel);
BSP_PWM_SetComp(channel, pulse);
return DEVICE_OK;
}
#endif
#ifdef LED_GPIO
int8_t BSP_LED_Set(char sign,DEVICE_LED_t ch,bool value,float duty_cycle); int8_t DEVICE_LED_GPIO_Set(BSP_GPIO_t gpio, bool value)
{
if (value) {
BSP_GPIO_WritePin(gpio, true);
} else {
BSP_GPIO_WritePin(gpio, false);
}
return DEVICE_OK;
}
#endif
#ifdef __cplusplus #ifdef __cplusplus
} }

View File

@ -139,12 +139,16 @@ static void Motor_RM_Decode(MOTOR_RM_t *motor, BSP_CAN_Message_t *msg) {
motor->feedback.rotor_speed = rotor_speed; motor->feedback.rotor_speed = rotor_speed;
motor->feedback.torque_current = torque_current; motor->feedback.torque_current = torque_current;
} }
if (motor->motor.reverse) {
motor->feedback.rotor_abs_angle = M_2_PI - motor->feedback.rotor_abs_angle;
motor->feedback.rotor_speed = -motor->feedback.rotor_speed;
motor->feedback.torque_current = -motor->feedback.torque_current;
}
motor->feedback.temp = msg->data[6]; motor->feedback.temp = msg->data[6];
} }
/* Exported functions ------------------------------------------------------- */ /* Exported functions ------------------------------------------------------- */
int8_t MOTOR_RM_Register(MOTOR_RM_Param_t *param) { int8_t MOTOR_RM_Register(MOTOR_RM_Param_t *param) {
if (param == NULL) return DEVICE_ERR_NULL; if (param == NULL) return DEVICE_ERR_NULL;
if (MOTOR_RM_CreateCANManager(param->can) != DEVICE_OK) return DEVICE_ERR; if (MOTOR_RM_CreateCANManager(param->can) != DEVICE_OK) return DEVICE_ERR;
@ -222,6 +226,9 @@ int8_t MOTOR_RM_SetOutput(MOTOR_RM_Param_t *param, float value) {
if (manager == NULL) return DEVICE_ERR_NO_DEV; if (manager == NULL) return DEVICE_ERR_NO_DEV;
if (value > 1.0f) value = 1.0f; if (value > 1.0f) value = 1.0f;
if (value < -1.0f) value = -1.0f; if (value < -1.0f) value = -1.0f;
if (param->reverse){
value = -value;
}
MOTOR_RM_t *motor = MOTOR_RM_GetMotor(param); MOTOR_RM_t *motor = MOTOR_RM_GetMotor(param);
if (motor == NULL) return DEVICE_ERR_NO_DEV; if (motor == NULL) return DEVICE_ERR_NO_DEV;
int8_t logical_index = MOTOR_RM_GetLogicalIndex(param->id, param->module); int8_t logical_index = MOTOR_RM_GetLogicalIndex(param->id, param->module);