低压伺服电机驱动器
1 视频演示
电流环视频演示:
低压伺服电机力矩控制
速度环视频演示:
低压伺服电机速度控制
位置环视频演示:
抵押伺服电机位置控制
2 部分代码
step1: 定义pwm结构体
Driver_PWM.h
#ifndef _Driver_PWM_H
#define _Driver_PWM_H
#include "stdint.h"
typedef struct
{
void (*Init)(void);
void (*Enable)(uint8_t isEnable);
void (*Set)(uint32_t *ccr);
void (*ChisEnable)(uint8_t ch, uint8_t isEnable);
} sPWM;
#define define_sPWM(x, xInit, xEnable, xSet, xChisEnable) \
sPWM x = { \
.Init = xInit, \
.Enable = xEnable, \
.Set = xSet, \
.ChisEnable = xChisEnable, \
};
#endif
step2: 实现和特定硬件相关的宏定义
#define htimPWM &htim1
#ifdef stm32f103
#define PWMMaxValue (3600 - 1) // 4250//3187
#else
#define PWMMaxValue (4500 - 1) // 4250//3187
#endif
#define Motor1PWMEnableIT() __HAL_TIM_ENABLE_IT(htimPWM, TIM_IT_UPDATE)
#define Motor1PWMDisableIT() __HAL_TIM_DISABLE_IT(htimPWM, TIM_IT_UPDATE)
#define Motor1PWMStartCh(ch) \
HAL_TIM_PWM_Start(htimPWM, TIM_CHANNEL_##ch); \
HAL_TIMEx_OCN_Start(htimPWM, TIM_CHANNEL_##ch)
#define Motor1PWMStopCh(ch) \
HAL_TIM_PWM_Stop(htimPWM, TIM_CHANNEL_##ch); \
HAL_TIMEx_OCN_Stop(htimPWM, TIM_CHANNEL_##ch)
#define Motor1PWMSetCh(ch, ccr) __HAL_TIM_SET_COMPARE(htimPWM, TIM_CHANNEL_##ch, ccr)
step3: 实例化pwm结构体:
TIM1PWM.c
#include "TIM1PWM.h"
#include "main.h"
#include "ADC1.h"
#include "stdint.h"
#include "tim.h"
#include <stdio.h>
#include "Motor1Config.h"
void Motor1PWMChisEnable(uint8_t ch, uint8_t isEnable)
{
if (isEnable == 0)
{
Motor1PWMStopCh(ch);
}
else
{
Motor1PWMStartCh(ch);
}
}
void Motor1PWMSet(uint32_t *ccr)
{
Motor1PWMSetCh(1, ccr[0]);
Motor1PWMSetCh(2, ccr[1]);
Motor1PWMSetCh(3, ccr[2]);
Motor1PWMSetCh(4, (PWMMaxValue - 5));
}
void Motor1PWMEnable(uint8_t isEnable)
{
if (isEnable == 0)
{
Motor1PWMSetCh(1, 0);
Motor1PWMSetCh(2, 0);
Motor1PWMSetCh(3, 0);
Motor1PWMSetCh(4, (PWMMaxValue - 5));
Motor1PWMDisableIT();
}
else
{
Motor1PWMEnableIT();
}
}
void Motor1PWMInit(void)
{
Motor1PWMStartCh(1);
Motor1PWMStartCh(2);
Motor1PWMStartCh(3);
Motor1PWMStartCh(4);
Motor1PWMEnable(0);
}
define(sPWM, (Motor1PWM, Motor1PWMInit, Motor1PWMEnable, Motor1PWMSet, Motor1PWMChisEnable));
step4: 使用pwm
FOCConfig.c
/*引入pwm资源*/
include(sPWM, Motor1PWM);
/*实际使用的时候这样调用*/
Motor1PWM.Init();
Motor1PWM.Enable(isEnable);
Motor1PWM.Set(pfoc->Svpwmdq.PWM_DUTY)
Motor1PWM.ChisEnable(1,0);