news 2026/4/15 12:16:11

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

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
寒假学习(14)(HAL库5)

搞了搞小车

PWM驱动

#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); } }
#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
#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 }
#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
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/3/25 21:06:26

STM32_芯片介绍

文章目录 一、STM32是什么二、STM32分类三、STM32命名规范   1、STM32   2、类型   3、子系列   4、引脚数   5、存储量   6、封装   7、工作温度 四、分配原理图引脚五、参考手册和数据手册   1、参考手册   2、数据手册 六、引脚的功能定义解读 一、STM32是…

作者头像 李华
网站建设 2026/3/31 18:55:14

括号配对(信息学奥赛一本通- P1572)

【题目描述】 Hecy 又接了个新任务&#xff1a;BE 处理。BE 中有一类被称为 GBE。 以下是 GBE 的定义&#xff1a; 空表达式是 GBE 如果表达式 A 是 GBE&#xff0c;则 [A] 与 (A) 都是 GBE 如果 A 与 B 都是 GBE&#xff0c;那么 AB 是 GBE。 【输入】 输入仅一行&#xff0c;…

作者头像 李华
网站建设 2026/4/10 16:48:19

XQuery 选择和过滤

XQuery 选择和过滤 引言 XQuery 是一种用于查询XML文档的结构化查询语言。它被广泛应用于数据的检索、转换和处理。在XQuery中,选择和过滤是基本操作,用于从XML文档中提取所需的数据。本文将深入探讨XQuery的选择和过滤机制,包括其基本语法、常用函数和技巧。 选择操作 …

作者头像 李华
网站建设 2026/4/4 16:14:30

强化学习在AI Agent交互式学习中的应用

强化学习在AI Agent交互式学习中的应用 关键词:强化学习、AI Agent、交互式学习、马尔可夫决策过程、策略梯度算法 摘要:本文深入探讨了强化学习在AI Agent交互式学习中的应用。首先介绍了相关背景知识,包括目的、预期读者、文档结构和术语表。接着阐述了强化学习和AI Agent…

作者头像 李华
网站建设 2026/4/4 17:47:49

Spark在气象大数据分析中的实践

Spark在气象大数据分析中的实践 关键词&#xff1a;Spark、气象大数据、数据处理、数据分析、分布式计算 摘要&#xff1a;本文围绕Spark在气象大数据分析中的实践展开。首先介绍了气象大数据的特点和分析需求&#xff0c;以及Spark作为分布式计算框架的优势。接着详细阐述了Sp…

作者头像 李华