低压伺服电机控制器项目

低压伺服电机驱动器

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);
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小男孩和胖子

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值