You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
279 lines
7.6 KiB
279 lines
7.6 KiB
/**
|
|
* @file dj.c
|
|
* @author 1722451300@qq.com
|
|
* @brief
|
|
* @version 0.1
|
|
* @date 2021-12-15
|
|
*
|
|
* @copyright Copyright (c) 2021
|
|
*
|
|
*/
|
|
#include "motor.h"
|
|
volatile int32_t recv_motor_data_state=0;//1就判断是不是接收完成,0不判断
|
|
volatile int32_t motor_jerk_state = 0;
|
|
extern volatile int32_t lcd_issue_motor_speed;
|
|
volatile int16_t motor_off_state=0;
|
|
extern int32_t recv_motor_data_time;
|
|
extern int32_t inquire_error_time=0;
|
|
void motor_uart_config(void)
|
|
{
|
|
/**
|
|
* @brief 用的 UART0 作为与单机的通信接口 CH340电机端
|
|
*
|
|
*/
|
|
uart0_config();
|
|
rs485_re_pin = 0;
|
|
rs485_re_port &= ~(0x01 << 3); //初始化为低电平默认接收模式
|
|
}
|
|
|
|
void send_cmd_to_motor(volatile char buff[20], unsigned int num)
|
|
{
|
|
/**
|
|
* @brief 向电机发送命令
|
|
* @param buff 发送的命令
|
|
*/
|
|
rs485_re_port |= (0x01 << 3);
|
|
while (num--) {
|
|
while (!TRMT0)
|
|
;
|
|
TX0B = *buff;
|
|
buff++;
|
|
}
|
|
while (!TRMT0)
|
|
; //等待向电机发送数据完成
|
|
rs485_re_port &= ~(0x01 << 3);
|
|
}
|
|
|
|
|
|
|
|
void inquire_motor_error_state(void)
|
|
{
|
|
/**
|
|
* @brief 每3秒查询一次电机的状态
|
|
* 3E 9C 01 00 DB
|
|
*/
|
|
unsigned char inquire_motor_error_cmd[20] = {0};
|
|
unsigned char str[2]={0};
|
|
if(inquire_error_time>=2000)//2s查询一次
|
|
{
|
|
inquire_error_time=0;
|
|
//读取错误信息
|
|
inquire_motor_error_cmd[0] = 0x3E;
|
|
inquire_motor_error_cmd[1] = 0x9A;
|
|
inquire_motor_error_cmd[2] = 0X01;
|
|
inquire_motor_error_cmd[3] = 0x00;
|
|
inquire_motor_error_cmd[4] = 0xD9;
|
|
//send_cmd_to_motor(inquire_motor_error_cmd, 5);
|
|
sleep_ms(10);
|
|
//读取速度的值
|
|
inquire_motor_error_cmd[0] = 0x3E;
|
|
inquire_motor_error_cmd[1] = 0x9C;
|
|
inquire_motor_error_cmd[2] = 0X01;
|
|
inquire_motor_error_cmd[3] = 0x00;
|
|
inquire_motor_error_cmd[4] = 0xDB;
|
|
//send_cmd_to_motor(inquire_motor_error_cmd, 5);
|
|
sleep_ms(10);
|
|
}
|
|
}
|
|
//----------------------------------------------------
|
|
//----------------------------------------------------
|
|
//----------------------------------------------------
|
|
uint8_t g_motor_rxbuf_isready = 0;
|
|
volatile char motor_rxbuff[50] = {0};
|
|
volatile uint32_t motor_count = 0;
|
|
uint32_t g_process_recv_motor_data_time = 0;
|
|
void recv_from_motor_data(unsigned char data)
|
|
{
|
|
/**
|
|
* @brief 接受来自电机的数据
|
|
*
|
|
*/
|
|
g_motor_rxbuf_isready=true;//进入中断就让他为真
|
|
motor_rxbuff[motor_count++] = data;
|
|
g_process_recv_motor_data_time= hal_get_irqticket();//记录最后的时间
|
|
}
|
|
|
|
|
|
uint32_t judge_recv_motor_data_finish(uint32_t time)
|
|
{
|
|
/**
|
|
* @brief 接收motor的数据完成
|
|
*/
|
|
static uint32_t recv_motor_data_time = 0;
|
|
recv_motor_data_time = time;
|
|
if(recv_motor_data_time=0)
|
|
{
|
|
recv_motor_data_time = hal_get_ticket();
|
|
return recv_motor_data_time;
|
|
}
|
|
else if (hal_has_passedms(recv_motor_data_time) > 10) {
|
|
g_motor_rxbuf_isready=false;
|
|
recv_motor_data_time = hal_get_ticket();
|
|
send_cmd_to_motor(motor_rxbuff,motor_count);//接收完成将数据发给电脑
|
|
motor_count=0;
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
|
|
void motor_data_analyse(volatile char buff[20], unsigned int num)
|
|
{
|
|
/**
|
|
* @brief 对电机回复的数据进行分析
|
|
* @param 电机回复的数据
|
|
* @param 数据长度
|
|
* buff[8]电机速度低字节,buff[9]电机速度高字节
|
|
*/
|
|
if (buff[0] == 0x3E && buff[1] == 0x9A)//查询电机错误状态命令下发后,电机的回复
|
|
{
|
|
if((buff[11]>>0&0x01)||(buff[11]>>3&0x01))
|
|
{
|
|
lcd_switch_anomaly_page();
|
|
}
|
|
}
|
|
if (buff[0] == 0x3E && buff[1] == 0x9C)//读取点击速度数值
|
|
{
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
volatile int32_t speed_dsp_now = 0;//记录现在的速度
|
|
volatile int32_t first_set_motor_speed_dsp = 0;
|
|
|
|
int32_t motor_jerk_status(void)
|
|
{
|
|
/**
|
|
* @brief 判断急停按键是否被按下
|
|
*
|
|
*/
|
|
if(!((PA>>0)&1)&&motor_jerk_state==0)
|
|
{
|
|
sleep_ms(10);
|
|
if(!((PA>>0)&1))
|
|
{
|
|
motor_jerk_state = 1;
|
|
speed_dsp_now=0;
|
|
first_set_motor_speed_dsp=0;
|
|
lcd_switch_anomaly_page();//急停状态的时候显示异常界面
|
|
clear_zero();
|
|
sleep_ms(3000);
|
|
lcd_switch_anomaly_page_key();
|
|
}
|
|
}
|
|
else if((PA >> 0) & 1) //不是急停状态
|
|
{
|
|
if((PA >> 0) & 1)
|
|
{
|
|
motor_jerk_state = 0;
|
|
beep_off;
|
|
}
|
|
}
|
|
}
|
|
|
|
int32_t judge_foreward_or_reversal(int32_t speed,int32_t turn_state) {
|
|
/**
|
|
* @brief 用户点击正转反转时,判断现在的状态是正转还是反转
|
|
*
|
|
*/
|
|
if(turn_state==0) //正转
|
|
{
|
|
if(first_set_motor_speed_dsp<0)
|
|
{
|
|
speed =~speed+1;
|
|
}
|
|
return speed;
|
|
}
|
|
else if (turn_state == 1) //反转
|
|
{
|
|
if(first_set_motor_speed_dsp>0)
|
|
{
|
|
speed = ~(speed - 1);
|
|
}
|
|
return speed;
|
|
}
|
|
|
|
}
|
|
|
|
|
|
void set_motor_off(void)
|
|
{
|
|
/**
|
|
* @brief 电机速度为0时也不能彻底停止需要再发一个停止指令
|
|
* 3E 80 01 00 BF
|
|
*/
|
|
motor_off_state=1;
|
|
unsigned char motor_off_cmd[20] = {0};
|
|
motor_off_cmd[0] = 0x3E;
|
|
motor_off_cmd[1] = 0x80;
|
|
motor_off_cmd[2] = 0X01;
|
|
motor_off_cmd[3] = 0x00;
|
|
motor_off_cmd[4] = 0xBF;
|
|
send_cmd_to_motor(motor_off_cmd, 5);
|
|
}
|
|
|
|
//===========================================================================
|
|
//===========================================================================
|
|
//===========================================================================
|
|
//===========================================================================
|
|
int32_t set_moter_speed_internal(int32_t speed_dsp)
|
|
{
|
|
/**
|
|
* @brief 速度闭环控制命令用来控制电机的速度
|
|
*/
|
|
unsigned char motor_speed_cmd[20] = {0};
|
|
speed_dsp_now = speed_dsp;
|
|
first_set_motor_speed_dsp = speed_dsp;
|
|
speed_dsp = speed_dsp * 100; //电机下发速度要乘以100
|
|
motor_speed_cmd[0] = 0x3E;
|
|
motor_speed_cmd[1] = 0xA2;
|
|
motor_speed_cmd[2] = 0X01;
|
|
motor_speed_cmd[3] = 0x04;
|
|
motor_speed_cmd[4] = motor_speed_cmd[0] + motor_speed_cmd[1] + motor_speed_cmd[2] + motor_speed_cmd[3];
|
|
motor_speed_cmd[5] = speed_dsp & 0xFF;
|
|
motor_speed_cmd[6] = (speed_dsp >> 8) & 0xFF;
|
|
motor_speed_cmd[7] = (speed_dsp >> 16) & 0xFF;
|
|
motor_speed_cmd[8] = (speed_dsp >> 24) & 0xFF;
|
|
motor_speed_cmd[9] = motor_speed_cmd[5] + motor_speed_cmd[6] + motor_speed_cmd[7] + motor_speed_cmd[8];
|
|
send_cmd_to_motor(motor_speed_cmd, 10);
|
|
if(speed_dsp_now==0)
|
|
{
|
|
sleep_ms(10);
|
|
set_motor_off();
|
|
}
|
|
}
|
|
/*
|
|
void set_motor_speed(int32_t speed_dsp_target)
|
|
{
|
|
/**
|
|
* @brief 设置电机的速度
|
|
* @param 要设置的速度(经过单位换算过的)
|
|
*/
|
|
/*
|
|
while(1)
|
|
{
|
|
if(speed_dsp_target==speed_dsp_now)//目标速度与现在的速度相等
|
|
{
|
|
break;
|
|
}
|
|
else if(speed_dsp_target < speed_dsp_now)//说明用户向让速度减小
|
|
{
|
|
int32_t setspeedthistime = speed_dsp_now - 10;
|
|
if (setspeedthistime < speed_dsp_target) {
|
|
setspeedthistime = speed_dsp_target;
|
|
}
|
|
set_moter_speed_internal(setspeedthistime);
|
|
}
|
|
else if (speed_dsp_target > speed_dsp_now) //说明用户想让速度进行增加 要设置的speed_dsp_target是150,现在的speed_dsp_now 0
|
|
{
|
|
int32_t nowexpectspeed = speed_dsp_now + 10;
|
|
if (nowexpectspeed > speed_dsp_target) {
|
|
nowexpectspeed = speed_dsp_target;
|
|
}
|
|
set_moter_speed_internal(nowexpectspeed);// 去设置的是10
|
|
}
|
|
sleep_ms(10);
|
|
}
|
|
}
|
|
*/
|