寒假学习(14)(HAL库5)

搞了搞小车

PWM驱动

cpp 复制代码
#include "PWM.h"

// Ignore PWM dead band  忽略PWM信号死区
static int16_t Motor_Ignore_Dead_Zone(int16_t pulse)
{
    if (pulse > 0)
        return pulse + MOTOR_IGNORE_PULSE;
    if (pulse < 0)
        return pulse - MOTOR_IGNORE_PULSE;
    return 0;
}

// All motors stopped  所有电机停止
void Motor_Stop(uint8_t brake)
{
    if (brake != 0)
        brake = 1;
    PWM_M1_A = brake * MOTOR_MAX_PULSE;
    PWM_M1_B = brake * MOTOR_MAX_PULSE;
    PWM_M2_A = brake * MOTOR_MAX_PULSE;
    PWM_M2_B = brake * MOTOR_MAX_PULSE;
    PWM_M3_A = brake * MOTOR_MAX_PULSE;
    PWM_M3_B = brake * MOTOR_MAX_PULSE;
    PWM_M4_A = brake * MOTOR_MAX_PULSE;
    PWM_M4_B = brake * MOTOR_MAX_PULSE;
}

// 设置电机速度,speed:±(3600-MOTOR_IGNORE_PULSE), 0为停止
// Set motor speed, speed:± (3600-MOTOR_IGNORE_PULSE), 0 indicates stop
void Motor_Set_Pwm(uint8_t id, int16_t speed)
{
    int16_t pulse = Motor_Ignore_Dead_Zone(speed);
    // Limit input  限制输入
    if (pulse >= MOTOR_MAX_PULSE)
        pulse = MOTOR_MAX_PULSE;
    if (pulse <= -MOTOR_MAX_PULSE)
        pulse = -MOTOR_MAX_PULSE;

    switch (id)
    {
    case L_F:
    {
        pulse = -pulse;
        if (pulse >= 0)
        {
            PWM_M1_A = pulse;
            PWM_M1_B = 0;
        }
        else
        {
            PWM_M1_A = 0;
            PWM_M1_B = -pulse;
        }
        break;
    }

    case L_R:
    {
        pulse = -pulse;
        if (pulse >= 0)
        {
            PWM_M2_A = pulse;
            PWM_M2_B = 0;
        }
        else
        {
            PWM_M2_A = 0;
            PWM_M2_B = -pulse;
        }
        break;
    }

    case R_F:
    {

        if (pulse >= 0)
        {
            PWM_M3_A = pulse;
            PWM_M3_B = 0;
        }
        else
        {
            PWM_M3_A = 0;
            PWM_M3_B = -pulse;
        }
        break;
    }
    case R_R:
    {

        if (pulse >= 0)
        {
            PWM_M4_A = pulse;
            PWM_M4_B = 0;
        }
        else
        {
            PWM_M4_A = 0;
            PWM_M4_B = -pulse;
        }
        break;
    }

    default:
        break;
    }
}


void Motion_Set_Pwm(int16_t Motor_1, int16_t Motor_2, int16_t Motor_3, int16_t Motor_4)
{
    if (Motor_1 >= -MOTOR_MAX_PULSE && Motor_1 <= MOTOR_MAX_PULSE)
    {
        Motor_Set_Pwm(L_F, Motor_1);
    }
    if (Motor_2 >= -MOTOR_MAX_PULSE && Motor_2 <= MOTOR_MAX_PULSE)
    {
        Motor_Set_Pwm(L_R, Motor_2);
    }
    if (Motor_3 >= -MOTOR_MAX_PULSE && Motor_3 <= MOTOR_MAX_PULSE)
    {
        Motor_Set_Pwm(R_F, Motor_3);
    }
    if (Motor_4 >= -MOTOR_MAX_PULSE && Motor_4 <= MOTOR_MAX_PULSE)
    {
        Motor_Set_Pwm(R_R, Motor_4);
    }
}
cpp 复制代码
#ifndef __PWM_H
#define __PWM_H
#include "main.h"

enum motor
{
	L_F=0,//左前
	L_R,//左后
	R_F,
	R_R,
};

#define PWM_M1_A TIM8->CCR1
#define PWM_M1_B TIM8->CCR2
#define PWM_M2_A TIM8->CCR3
#define PWM_M2_B TIM8->CCR4

#define PWM_M3_A TIM1->CCR1
#define PWM_M3_B TIM1->CCR2
#define PWM_M4_A TIM1->CCR3
#define PWM_M4_B TIM1->CCR4

#define MOTOR_MAX_PULSE 3600

#define MOTOR_IGNORE_PULSE 2000


void Motor_Set_Pwm(uint8_t id, int16_t speed);

void Motion_Set_Pwm(int16_t Motor_1, int16_t Motor_2, int16_t Motor_3, int16_t Motor_4);


#endif
cpp 复制代码
#include "RedRay.h"

void car_irtrack(void)
{

	if((IN_X1 == 0 && IN_X3 == 0) && IN_X2 == 1 && IN_X4 == 1) //直走 go straight
	{
		Motion_Set_Pwm(600,600,600,600);
	}


	if(IN_X1 == 0 && IN_X3 == 1 && IN_X4 == 1 && IN_X2 == 1)//小幅度调整 small adjustment
	{
			Motion_Set_Pwm(0,0,500,500);
	}
	else	if(IN_X1 == 1 && IN_X3 == 0 && IN_X4 == 1 && IN_X2 == 1)
	{
			Motion_Set_Pwm(500,500,0,0);
	}


	if(IN_X2 == 0 && IN_X3 == 1 ) //大幅度左右转 Turn left and right sharply
	{
			Motion_Set_Pwm(-500,-500,500,500);
	}
	else if(IN_X4 == 0 && IN_X1 == 1 )
	{
			Motion_Set_Pwm(500,500,-500,-500);
	}

	//其它情况保持不变 Other things remain unchanged
}
cpp 复制代码
#ifndef __REDRAY_H
#define __REDRAY_H


#include "main.h"
#include "PWM.h"

#define IN_X1 HAL_GPIO_ReadPin(X1_GPIO_Port,X1_Pin)//读取X1引脚的状态 Read the status of X1 pin
#define IN_X2 HAL_GPIO_ReadPin(X2_GPIO_Port,X2_Pin)//读取X2引脚的状态 Read the status of X2 pin
#define IN_X3 HAL_GPIO_ReadPin(X3_GPIO_Port,X3_Pin)//读取X3引脚的状态 Read the status of X3 pin
#define IN_X4 HAL_GPIO_ReadPin(X4_GPIO_Port,X4_Pin)//读取X4引脚的状态 Read the status of X4 pin

void car_irtrack(void);

#endif
相关推荐
lee_curry5 小时前
第四章 jvm中的垃圾回收器
java·jvm·垃圾收集器
有谁看见我的剑了?5 小时前
linux 添加硬盘后系统识别不到硬盘处理
linux·运维·服务器
酿情师6 小时前
yihan:一款面向连续网页学习的智能侧边栏插件
学习·学习方法·工具·学习工具
九转成圣6 小时前
Java 性能优化实战:如何将海量扁平数据高效转化为类目字典树?
java·开发语言·json
yc_12247 小时前
用 Visual Studio 远程调试 Linux:从零到流畅的完整指南
linux·ide·visual studio
直奔標竿7 小时前
Java开发者AI转型第二十七课!Spring AI 个人知识库实战(六)——全栈闭环收官,解锁前端流式渲染终极技巧
java·开发语言·前端·人工智能·后端·spring
瞎某某Blinder7 小时前
DFT学习记录[6]基于 HES06的能带计算+有效质量计算
python·学习·程序人生·数据挖掘·云计算·学习方法
计算机安禾7 小时前
【Linux从入门到精通】第31篇:防火墙漫谈——iptables与firewalld防护指南
linux·运维·php
金銀銅鐵7 小时前
[java] 编译之后的记录类(Record Classes)长什么样子(上)
java·jvm·后端
下一页盛夏花开7 小时前
ubuntu 20中安装QT以后出现红色空心断点
linux·运维·ubuntu