当前位置:网站首页>STM32控制电机简易教程
STM32控制电机简易教程
2022-07-22 04:21:00 【TT的嵌入式开发路】
STM32控制电机简易教程 包教包会
近期,电赛临近,来补习一下电机的使用方式,使用起来非常的方便
首先是在CUBEMX里面配置一些基本内容
然后是使用PWM去调速
其他的时钟和调试配置就不多说了。
然后就是初始化了
同样的,这里使用的是结构体,
typedef struct
{
float speed_set;
float speed_feedback;
uint8_t direction;
pid_type_def motor_pid;
float mileage_sum;
TIM_HandleTypeDef *encoder_tim;
uint32_t pwm_channel_1;
uint32_t pwm_channel_2;
TIM_HandleTypeDef *pwm_tim;
uint16_t motor_in1_pin;
GPIO_TypeDef *motor_in1_port;
} motor_control_t;
motor_control_t motor_control;
初始化
float speed_pid_pra[] = {80,10,0};
float enc_dis = 0;
//外设初始化
HAL_TIM_PWM_Start(&htim2, TIM_CHANNEL_1);
HAL_TIM_PWM_Start(&htim2 , TIM_CHANNEL_2);
HAL_TIM_Encoder_Start(&htim1, TIM_CHANNEL_ALL); //编码器,电机用
//配置电机1
motor_control.encoder_tim = &htim1;
motor_control.pwm_tim = &htim2;
motor_control.pwm_channel_2 = TIM_CHANNEL_2;
motor_control.pwm_channel_1 = TIM_CHANNEL_1;
PID_Init(&motor_control.motor_pid,PID_POSITION,speed_pid_pra,1000,1000);
我使用的是RZ7899作为电机驱动,所以配置了两个PWM,如果使用L298N的话,可以自行更改,难度不大,如遇问题可以私信我。
然后就是开始闭环控制电机了
tim_get_encoder(&motor_control);
PID_calc(&motor_control.motor_pid,motor_control.speed_feedback,motor_control.speed_set);
motor_set_pwm(&motor_control);
enc_dis = 100 * motor_control.mileage_sum;//要用距离累计使用
motor_control.mileage_sum = 0;
相关函数
void tim_get_encoder(motor_control_t *motor)
{
static s16 encoder = 0;
static float delta = 0;
motor->direction = __HAL_TIM_IS_TIM_COUNTING_DOWN(motor->encoder_tim);
encoder = htim1.Instance->CNT;
delta = (float)encoder * PI * WHEEL_DIAM/ (CIRCLE_CNT * 4 * GEAR_RATIO);
motor->speed_feedback = 1000 * delta / TASK_CYCLE;
motor->mileage_sum += delta;
__HAL_TIM_SET_COUNTER(motor->encoder_tim,0);
motor->angel = htim1.Instance->CNT/CIRCLE_CNT *360;;
motor->angel += motor->angel;
if(motor->angel >= 360) motor->angel = 0;
htim1.Instance->CNT = 0;
}
void motor_set_pwm(motor_control_t *motor)
{
if(motor->motor_pid.out > 0)
{
htim2.Instance->CCR1 = my_fabs(motor->motor_pid.out);
htim2.Instance->CCR2 = 0;
}
else
{
htim2.Instance->CCR2 = my_fabs(motor->motor_pid.out);
htim2.Instance->CCR1 = 0;
}
}
宏定义
#define CIRCLE_CNT 11 //编码器线数
#define GEAR_RATIO 50.0f //减速比
#define WHEEL_DIAM 2.0f //轮子直径,cm
#define TASK_CYCLE 10 //控制周期,ms
作者联系方式:
QQ:488628560
边栏推荐
- Signal FFT, STFT, wavelet transform, envelope analysis, etc
- Command line code for server and local data transmission
- Distributed scheduling framework elastic job
- 交換機與路由器技術:標准ACL、擴展ACL和命名ACL
- [SSM]SSM整合②(功能模块的开发)
- C language pthread_ Join() function
- SOC custom IP core -- breathing lamp
- The LAAS solution of elephant swap has risen rapidly and built a new defi2.0 protocol
- mysql中not like的简化写法
- 分布式调度框架Elastic-Job
猜你喜欢
JVM memory model: PC program counters
盒马两大供应链中心启用 多业态商品创新研发“有后台”
第七讲 管道、环境变量与常用命令
[initial exploration of STK] create a track to the moon
Fastjson 代码执行 CVE-2022-25845
A few minutes before work, express quick start
Data structure in redis (2): jump table
On contract testing
操作教程:大华摄像头通过GB28181协议注册EasyCVR平台的详细配置
Hybrid hybrid development and jsbridge
随机推荐
JVM: parental delegation mechanism for class loading
Purchase instructions for pull-up device and waist cushion
String和char[]互转的思考
红队怎么打
On contract testing
Thinking about the transformation between string and char[]
JVM内存模型:PC程序计数器
[unity project practice] game architecture
Vulkan-官方示例解读-子通道
The principle of embedded IDE, openocd introduction and how stlink connects STM32 board
【Leetcode数组--排序+辗转相除法最大公约数】6122.使数组可以被整除的最少删除次数
分布式调度框架Elastic-Job
Lesson 4 SSH
JVM memory model: virtual machine stack
Diversified distribution methods of NFT
SOC custom IP core -- breathing lamp
Write a function in C language to delete the spaces in the string and return the number of spaces
STM32 single channel and multi-channel sampling of non DMA polling ADC based on Hal Library
A 15-year-old ABAP veteran's suggestion: understanding these basic knowledge is beneficial to ABAP development
计算机网络传输层面试题