This repository has been archived on 2024-12-31. You can view files and clone it, but cannot push or open issues or pull requests.
mfps/App/Src/motor.c

268 lines
6.0 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include "motor.h"
#include "app.h"
#define STEP_LIN 16 //直行程步长单位数值(脉冲数)
#define STEP_ROT 20 //角行程步长单位数值(脉冲数)
#define DATA_LEN 10 //角行程步长单位数值(脉冲数)
#define MOTOR_STOP 0 //电机停止
#define MOTOR_POS 1 //电机正向运动
#define MOTOR_REV 2 //电机反向运动
#define MOTOR_RETURN 3 //电机返回
int Travle_Flag = 0; //0 直 1 角
char Motor_Run = 0; //0 停止 1 运行 2 运行到起始点 3 运行到结束点
char Run_Mode = 0; //0 点动 1 方案一 2 方案二
unsigned int Run_Step = 0; //电机运行步长
unsigned int Run_Inter = 0; //电机运行间隔时长
unsigned int Run_Stop = 0; //到“结束点”后,停止时间
unsigned int Run_mm = 5; //行进长度mm/转动角度(°)
unsigned int Run_num = 0; //角行程电机转动圈数
unsigned int ct_num = 0; //磁条长度
int mov_flag = 0; //脉冲标志
int send_flag = 0; //发送标志
int seat_flag = 0; //位置标志
int motor_dire = 1; //电机转动方向
int flag = 0;
int motor_direc = 1;
char motor_control = 0;
//步骤
unsigned char Runmotor_step = 0;
//次数
unsigned int Runmotor_Nums = 0;
//清除电机标记
void ClrRunmotorStep(void)
{
//步骤
Runmotor_step = 0;
//次数
Runmotor_Nums = 0;
//脉冲标记清0
mov_flag = 0;
//发送标记清0
send_flag = 0;
//位置标记清0
seat_flag = 0;
}
//处理马达运行
void Deal_Motor(void)
{
//判断直行程还是角行程
if(Travle_Flag == 0)//直行程——电机旋转一圈磁条水平位移5mm
{
motor_data[0] = 0x00;//00 直行程 01 角行程
//判断电机停止还是运行,运行到起始位还是结束位
if( Motor_Run == 0)//停止
{
tmc5160_operate(MOTOR_STOP,0);
}
else if(Motor_Run == 1)//运行
{
if(Motor_Run >= 1 && Runmotor_step == 0)
{
Runmotor_step = 1;
}
//判断电机运行方式是点动还是连续,点动为方案三,方案一和方案二为连续
if(Run_Mode == 0)//点动(方案三)
{
motor_data[2] = 0x00;//发送时反馈的数据
mov_step();//点动
}
// else if(Run_Mode == 1)//连续(方案一)“步长过大可能会越过限位开关”
// {
// motor_data[2] = 0x01;//发送时反馈的数据
// mov_loop1();//方案一
// }
// else if(Run_Mode == 2)//连续(方案二)“步长过大可能会越过限位开关”
// {
// motor_data[2] = 0x02;//发送时反馈的数据
// //磁条循环“起始点-结束点-起始点”,一定次数后停在起始点
// mov_loop2();//方案二
// }
else//初始化
{
tmc5160_operate(MOTOR_STOP,0);
}
}
else if(Motor_Run == 2)//运行到起始位
{
tmc5160_operate(MOTOR_RETURN,0);//回到起始位
}
else if(Motor_Run == 3)//运行到结束位
{
tmc5160_operate(MOTOR_RETURN,0);//移动至结束位
}
else//数据错误
{
//SC_Init();
}
}
else //角行程——电机旋转一圈磁条旋转4°
{
// motor_data[0] = 0x01;//00 直行程 01 角行程
// //判断电机停止还是运行,运行到起始位还是结束位
// if( Motor_Run == 0)//停止
// {
// tmc5160_operate(0,0);
// }
// else if(Motor_Run == 1)//运行
// {
// if(Motor_Run >= 1 && Runmotor_step == 0)
// {
// Runmotor_step = 1;
// }
//
// //判断电机运行方式是点动还是连续,点动为方案三,方案一和方案二为连续
// if(Run_Mode == 0)//点动(方案三)
// {
// motor_data[2] = 0x00;//发送时反馈的数据
// mov_step_ang();//电机点动运行
// }
// else if(Run_Mode == 1)//连续(方案一)
// {
// motor_data[2] = 0x01;//发送时反馈的数据
// mov_loop1_ang();//磁条旋转一圈
// }
// else if(Run_Mode == 2)//连续(方案二)
// {
// motor_data[2] = 0x02;//发送时反馈的数据
// if(seat_flag == 0)
// {
// if(GPIO_ReadPin(GPIO1,GPIO_PIN_4) == 0) //判断是否到达限位处
// {
// motor_stop(); //电机停止
// Run_mm = 0;
// seat_flag = 1;//位置标记
// }
// else
// {
// REV(); //反转
// motor_start(); //运行
// motor_mov(1); //提供脉冲信号
// }
// }
// else
// {
// mov_loop2_ang();//磁条旋转一圈
// }
// }
// else//初始化
// {
// motor_stop();
// }
// }
// else if(Motor_Run == 2 || Motor_Run == 3)//运行到起始位
// {
// mov_begin();//回到起始位
// }
// else//数据错误
// SC_Init();
}
}
//电机点动运行,方案三(直行程)
void mov_step(void)
{
if( (ocin1 == 0) && (motor_direc != MOTOR_POS) )//判断是否到达始限位
{
motor_direc = MOTOR_POS;
tmc5160_operate(MOTOR_STOP,0);
}
if( (ocin2 == 0) && (motor_direc != MOTOR_REV) )//判断是否到达终限位
{
motor_direc = MOTOR_REV;
tmc5160_operate(MOTOR_STOP,0);
}
if(Run_mm == 0) return;//步长不能为0
TMC5160_SPIReadInt(XACTUAL_ADDR,XA); //读取实际位置XACTUAL
XA_32 = Raw_32(XA);
TMC5160_SPIReadInt(VACTUAL_ADDR,VA); //读取实际速度VACTUAL
VA_32 = Raw_32(VA);
TMC5160_SPIReadInt(RAMP_STAT_ADDR,RAMP_STAT); //读取斜坡状态
RAMP_STAT_32 = Raw_32(RAMP_STAT);
TMC5160_SPIReadInt(DRV_STAT_ADDR,DRV_STAT); //读取驱动器状态
DRV_STAT_32 = Raw_32(DRV_STAT);
SG_RESULT_16 = ((uint16_t)DRV_STAT_32) & 0x03FF;//驱动器状态共32bit其中0-9bit为负载值读到的负载值越低负载越高0代表最高负载
switch(Runmotor_step)
{
case 1 : //电机运行准备
{
//motor_start(); //运行
if( ocin1 == 0 )//判断是否到达始限位
{
motor_direc = MOTOR_POS;
//tmc5160_operate(MOTOR_POS,0); //正转
}
if( ocin2 == 0 )//判断是否到达终限位
{
motor_direc = MOTOR_REV;
//tmc5160_operate(MOTOR_REV,0); //反转
}
Runmotor_Nums = 0; //运行次数
mov_flag = 0;
Runmotor_step++;
}
break;
case 2 : //电机运行过程
{
Run_Step = (Run_mm/0.05) * 512;
tmc5160_operate(motor_direc,Run_Step);
if(busy_flag == 0)
{
Runmotor_step++;
}
}
break;
case 3 :
{
// if(busy_flag == 0)
// {
// Runmotor_step++;
// }
Runmotor_step++;
}
break;
case 4 :
{
if(it_1ms_flag) //检查1ms定时标志
{
it_1ms_flag = 0;//定时标志清零
Runmotor_Nums++;
}
if(Runmotor_Nums >= 300)//时间
{
Runmotor_Nums = 0;
Runmotor_step++;
//motor_seat(); //读取电机当前位置
send_set_resp(0xF001, OBJ_DEVICE_ADDR, DATA_LEN, motor_data);//数据发送
}
}
break;
case 5 :
{
Runmotor_step = 0;
Motor_Run = 1; //运行标记清除
}
break;
default :
{
}
break;
}
}