张大头闭环步进电机

一、接线

二、cubemx配置

打开一个usart,并且设置的波特率要和电机匹配

生成代码并添加相对应的文件bsp、datou.c/h

datou.h

#ifndef __DATOU_H
#define __DATOU_H

#include "main.h"

typedef struct 
{
    uint8_t controlBytes[6];
    uint8_t lastControlBytes[6];

} StepperMotorControl;
typedef struct 
{
    uint8_t controlBytes[9];
    uint8_t lastControlBytes[9];
	float now_angle;
} StepperMotorControl_location;
typedef  enum 
{
	forward=0x12,		//正
	reverse=0x02		//反
}Command;
extern StepperMotorControl moto1;
extern StepperMotorControl moto2;
extern StepperMotorControl moto3;
extern StepperMotorControl moto4;
void StepperMotorControl_init(StepperMotorControl *control, uint8_t address);
void set_speed(StepperMotorControl *control, uint8_t direction, uint16_t speed);
void set_acceleration(StepperMotorControl *control, uint16_t acceleration);
extern StepperMotorControl_location moto_9;//下
extern StepperMotorControl_location moto_8;//上
void StepperMotorControl_init_location(StepperMotorControl_location *control, uint8_t address) ;
void set_angle_control_location(StepperMotorControl_location *control, float target_angle_num);

datou.c

#include "datou.h"

#include <stdint.h>
#include <string.h>
#include <stdio.h>

#include <usart.h>
StepperMotorControl moto1;//一号轮
StepperMotorControl moto2;//二号轮
StepperMotorControl moto3;//三号轮
StepperMotorControl moto4;//四号轮
/******************************************************
Function:    		void StepperMotorControl_init(StepperMotorControl *control, uint8_t address) 
Description: 		指定电机初始化函数(这个函数是用来指定电机的速度的)
Calls:				 	NONE 
Input: 					StepperMotorControl *control这个变量是结构体变量,这个结构体里面有2个成员
								分别对应了当前发送的数据,上次发送的数据
								address	是电机设定的地址
******************************************************/

void StepperMotorControl_init(StepperMotorControl *control, uint8_t address) 
{
    uint8_t defaultBytes[6] = {0x00, 0xF6, 0x10, 0x00, 0xaf, 0x6B};
    memcpy(control->controlBytes, defaultBytes, 6);
    memcpy(control->lastControlBytes, defaultBytes, 6);
    control->controlBytes[0] = address;
}

static uint8_t hasChanged(StepperMotorControl *control) 
{
    for (uint8_t i = 0; i < 6; i++) {
        if (control->controlBytes[i] != control->lastControlBytes[i]) 
				{
            return 1;
        }
    }
    return 0;
}

static void updateLastControlBytes(StepperMotorControl *control) 
{
    for (uint8_t i = 0; i < 6; i++) {
        control->lastControlBytes[i] = control->controlBytes[i];
    }
}

static void sendCommand(StepperMotorControl *control) 
{
    if (hasChanged(control)) {

				HAL_UART_Transmit(&huart3, control->controlBytes, 6, 100);
        updateLastControlBytes(control);
    }
}
/******************************************************
Function:    		void set_speed(StepperMotorControl *control, uint8_t direction, uint16_t speed) 
Description: 		设置电机的转速
Calls:				 	static void sendCommand(StepperMotorControl *control) 
Input: 					StepperMotorControl *control这个变量是结构体变量,这个结构体里面有2个成员
								分别对应了当前发送的数据,上次发送的数据
								direction 是电机的方向
								speed	是电机的速度
******************************************************/
void set_speed(StepperMotorControl *control, uint8_t direction, uint16_t speed) 
{
    if (direction) {
        control->controlBytes[2] = (0x10) | ((speed >> 8) & 0x0F); // 逆时针方向
    } else {
        control->controlBytes[2] = (0x00) | ((speed >> 8) & 0x0F); // 顺时针方向
    }
    control->controlBytes[3] = speed & 0xFF;
    sendCommand(control);
}
/******************************************************
Function:    		void set_acceleration(StepperMotorControl *control, uint16_t acceleration) 
Description: 		设置电机的加转速
Calls:				 	static void sendCommand(StepperMotorControl *control) 
Input: 					StepperMotorControl *control这个变量是结构体变量,这个结构体里面有2个成员
								分别对应了当前发送的数据,上次发送的数据
								acceleration	是电机的加速度
******************************************************/
void set_acceleration(StepperMotorControl *control, uint16_t acceleration) 
{
    control->controlBytes[4] = acceleration;
    sendCommand(control);
}

StepperMotorControl_location moto_9;//下
StepperMotorControl_location moto_8;//上


/******************************************************
Function:    		void StepperMotorControl_init_location(StepperMotorControl_location *control, uint8_t address) 
Description: 		指定电机初始化函数(这个函数是用来指定电机的位置的)
Calls:				 	NONE 
Input: 					StepperMotorControl_location *control这个变量是结构体变量,这个结构体里面有3个成员
								分别对应了当前发送的数据,上次发送的数据,以及当前的位置值
								address	是电机设定的地址
******************************************************/

void StepperMotorControl_init_location(StepperMotorControl_location *control, uint8_t address) 
{
	
	
    uint8_t defaultBytes[9] = {0x00, 0xFD, 0x12, 0x00, 0xaf,0x00,0x00,0x00,0x6B};
    memcpy(control->controlBytes, defaultBytes, 9);
    memcpy(control->lastControlBytes, defaultBytes, 9);
    control->controlBytes[0] = address;
}
static uint8_t hasChanged_location(StepperMotorControl_location *control) 
{
    for (uint8_t i = 0; i < 9; i++) {
        if (control->controlBytes[i] != control->lastControlBytes[i]) 
				{
            return 1;
        }
    }
    return 0;
}
static void updateLastControlBytes_location(StepperMotorControl_location *control) 
{
    for (uint8_t i = 0; i < 9; i++) {
        control->lastControlBytes[i] = control->controlBytes[i];
    }
}

static void sendCommand_location(StepperMotorControl_location *control) 
{
    if (hasChanged_location(control)) {

				HAL_UART_Transmit(&huart3, control->controlBytes, 9, 100);
        updateLastControlBytes_location(control);
    }
}

static void set_location(StepperMotorControl_location *control, Command com, int pulse_num) 
{		
	control->controlBytes[2] =com;
	
	control->controlBytes[6] = pulse_num>>8;/* 脉冲数中字节 */
	control->controlBytes[7] = pulse_num-((pulse_num>>8)<<8);	/* 脉冲数低字节 */

    sendCommand_location(control);
}


/******************************************************
Function:    	void set_angle_control_location(StepperMotorControl_location *control, float target_angle_num)
Description: 	控制指定电机的角度
Calls:				 static void set_location(StepperMotorControl_location *control, Command com, int pulse_num) 
Input: 					StepperMotorControl_location *control这个变量是结构体变量,这个结构体里面有3个成员
								分别对应了当前发送的数据,上次发送的数据,以及当前的位置值(由于需要通过用角度的差值来计算脉冲数,所以需要记录当前的位置)
								target_angle_num	是目标角度
******************************************************/
#define a_circle_pulse 3200.0//这是一圈需要的脉冲数
void set_angle_control_location(StepperMotorControl_location *control, float target_angle_num)
{
	double error_angle=target_angle_num-(control->now_angle);
	int need_pulse=(int)(error_angle/360.0*a_circle_pulse);
	if(error_angle!=0)
	{
		if(need_pulse<0)
		{
			set_location(control,reverse, -need_pulse);//注意此处的reverse可能需要改成forward,这个要根据你的物理结构而定
		}
		else
		{
			set_location(control,forward, need_pulse);//注意此处的forward可能需要改成reverse,这个要根据你的物理结构而定
		}
		control->now_angle=target_angle_num;
		
	}
}

main.c

	/*底盘电机初始化*/		
	StepperMotorControl_init(&moto1,0x01);
	StepperMotorControl_init(&moto2,0x02);
	StepperMotorControl_init(&moto3,0x03);
	StepperMotorControl_init(&moto4,0x04);	
	
	HAL_Delay(100);	
	/*云台电机初始化*/		
	StepperMotorControl_init_location(&moto_9,0x09);//下云台是9
	StepperMotorControl_init_location(&moto_8,0x08);
	HAL_Delay(100);

	set_angle_control_location(&moto_8,-900);//电机转-900度
	HAL_Delay(2000);
	set_speed(&moto1,1,200) ;//电机正转且速度为200

两个错误还在纠正中

  • 9
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值