1. 智能小车:让小车动起来(360.64)
硬件接线
- B-2A – PB0
- B-1A – PB1
- A-1B – PB2
- A-1A – PB10
- 其余接线参考上官一号小车项目。
cubemx配置
- 代码(28.smartCar_project1/MDK-ARM)
2. 智能小车:串口控制小车(361.65)
- 在 smartCar_project1 的基础上:
- 代码(28.smartCar_project2/MDK-ARM)(拷贝 4g_led_project 中串口接收数据相关的代码)
3. 智能小车:点动控制小车(362.66)
- 提升滴答定时器的优先级,降低串口的优先级
- 代码(28.smartCar_project3/MDK-ARM)(添加延时函数)
4. 智能小车:硬件PWM调速(363.67)
硬件接线
- B-1A – PA0
- A-1A – PA1
- B-1A – PB1
- A-1A – PB10
- 代码(28.smartCar_project4/MDK-ARM)(修改arr即可调速)
- 将控制车轮的4个 GPIO 口配置修改如下,否则小车动不起来。
- 原因:L9110每个控制口需要一高一低才可以动起来,如果PWM有效电平为高电平,则另一个GPIO口则需要输出低电平才可以驱动轮子。
5. 智能小车:左右轮各自调速(364.68)
- 代码(28.smartCar_project5/MDK-ARM)
//main.c
while (1){__HAL_TIM_SetCompare(&htim2, TIM_CHANNEL_1,80);__HAL_TIM_SetCompare(&htim2, TIM_CHANNEL_2,150);HAL_Delay(1000);__HAL_TIM_SetCompare(&htim2, TIM_CHANNEL_1,150);__HAL_TIM_SetCompare(&htim2, TIM_CHANNEL_2,80);HAL_Delay(1000);}
6. 智能小车:循迹小车基本实现(365.69)
硬件接线
- B-2A – PB0
- B-1A – PB1
- A-1B – PB2
- A-1A – PB10
- 循迹模块(左)DO – PB3
- 循迹模块(右)DO – PB4
代码实现
- 基于 smartCar_project1
- 代码(28.smartCar_project6/MDK-ARM)
//main.c
#define LeftWheel_Value HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_3)
#define RightWheel_Value HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_4)while (1)
{if (LeftWheel_Value == GPIO_PIN_RESET && RightWheel_Value == GPIO_PIN_RESET)goForward();if (LeftWheel_Value == GPIO_PIN_SET && RightWheel_Value == GPIO_PIN_RESET)goLeft();if (LeftWheel_Value == GPIO_PIN_RESET && RightWheel_Value == GPIO_PIN_SET)goRight();if (LeftWheel_Value == GPIO_PIN_SET && RightWheel_Value == GPIO_PIN_SET)stop();
}
7. 智能小车:循迹小车解决转弯不平滑问题(366.70)
硬件接线
- B-1A – PA0
- A-1A – PA1
- B-1A – PB1
- A-1A – PB10
代码实现
- 基于smartCar_project5(因为用到pwm)
- 代码(28.smartCar_project7/MDK-ARM)
//main.c
#define LeftWheel_Value HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_3)
#define RightWheel_Value HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_4)while (1){if(LeftWheel_Value == GPIO_PIN_RESET && RightWheel_Value == GPIO_PIN_RESET){__HAL_TIM_SetCompare(&htim2, TIM_CHANNEL_1,199);__HAL_TIM_SetCompare(&htim2, TIM_CHANNEL_2,199);}if(LeftWheel_Value == GPIO_PIN_SET && RightWheel_Value == GPIO_PIN_RESET){__HAL_TIM_SetCompare(&htim2, TIM_CHANNEL_1,150);__HAL_TIM_SetCompare(&htim2, TIM_CHANNEL_2,80);}if(LeftWheel_Value == GPIO_PIN_RESET && RightWheel_Value == GPIO_PIN_SET){__HAL_TIM_SetCompare(&htim2, TIM_CHANNEL_1,80);__HAL_TIM_SetCompare(&htim2, TIM_CHANNEL_2,150);}if(LeftWheel_Value == GPIO_PIN_SET && RightWheel_Value == GPIO_PIN_SET){__HAL_TIM_SetCompare(&htim2, TIM_CHANNEL_1,0);__HAL_TIM_SetCompare(&htim2, TIM_CHANNEL_2,0);}}
}
8. 智能小车:跟随小车的实现(367.71)
硬件接线
- B-2A – PB0
- B-1A – PB1
- A-1B – PB2
- A-1A – PB10
- 红外跟随模块(左)OUT – PB5
- 红外跟随模块(右)OUT – PB6
代码实现
- 基于smartCar_project6(基本一致)
- 代码(28.smartCar_project8/MDK-ARM)
//main.c
#define LeftWheel_Value HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_5)
#define RightWheel_Value HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_6)while (1){if(LeftWheel_Value == GPIO_PIN_RESET && RightWheel_Value == GPIO_PIN_RESET)goForward();if(LeftWheel_Value == GPIO_PIN_SET && RightWheel_Value == GPIO_PIN_RESET)goRight();if(LeftWheel_Value == GPIO_PIN_RESET && RightWheel_Value == GPIO_PIN_SET)goLeft();if(LeftWheel_Value == GPIO_PIN_SET && RightWheel_Value == GPIO_PIN_SET)stop();}
}
9. 智能小车:摇头避障小车舵机封装(368.72)
硬件接线
- sg90 – PB9
cubeMX配置
- 代码(28.smartCar_project9_1/MDK-ARM)
//sg90.c
#include "sg90.h"
#include "gpio.h"
#include "tim.h"
void initSG90(void)
{HAL_TIM_PWM_Start(&htim4,TIM_CHANNEL_4); //启动定时器4__HAL_TIM_SetCompare(&htim4, TIM_CHANNEL_4, 15); //将舵机置为90度
}
void sgMiddle(void)
{__HAL_TIM_SetCompare(&htim4, TIM_CHANNEL_4, 15); //将舵机置为90度
}
void sgRight(void)
{__HAL_TIM_SetCompare(&htim4, TIM_CHANNEL_4, 5); //将舵机置为0度
}
void sgLeft(void)
{__HAL_TIM_SetCompare(&htim4, TIM_CHANNEL_4, 25); //将舵机置为180度
}
//main.c
initSG90();
HAL_Delay(1000);
while (1)
{sgLeft();HAL_Delay(1000);sgMiddle();HAL_Delay(1000);sgRight();HAL_Delay(1000);sgMiddle();HAL_Delay(1000);
}
10. 智能小车:摇头避障小车超声波传感器封装(369.73)
硬件接线
- Trig – PB7
- Echo – PB8
cubeMX配置
- 基于 smartCar_project9_1
代码实现
- 代码(28.smartCar_project9_2/MDK-ARM)
//sr04.c
#include "sr04.h"
#include "gpio.h"
#include "tim.h"
//使用TIM2来做us级延时函数
void TIM2_Delay_us(uint16_t n_us)
{/* 使能定时器2计数 */__HAL_TIM_ENABLE(&htim2);__HAL_TIM_SetCounter(&htim2, 0);while(__HAL_TIM_GetCounter(&htim2) < ((1 * n_us)-1) );/* 关闭定时器2计数 */__HAL_TIM_DISABLE(&htim2);
}
double get_distance(void)
{int cnt=0;//1. Trig ,给Trig端口至少10us的高电平HAL_GPIO_WritePin(GPIOB, GPIO_PIN_7, GPIO_PIN_SET);//拉高TIM2_Delay_us(20);HAL_GPIO_WritePin(GPIOB, GPIO_PIN_7, GPIO_PIN_RESET);//拉低//2. echo由低电平跳转到高电平,表示开始发送波//波发出去的那一下,开始启动定时器while(HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_8) == GPIO_PIN_RESET);//等待输入电平拉高HAL_TIM_Base_Start(&htim2);__HAL_TIM_SetCounter(&htim2,0);//3. 由高电平跳转回低电平,表示波回来了while(HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_8) == GPIO_PIN_SET);//等待输入电平变低//波回来的那一下,我们开始停止定时器HAL_TIM_Base_Stop(&htim2);//4. 计算出中间经过多少时间cnt = __HAL_TIM_GetCounter(&htim2);//5. 距离 = 速度 (340m/s)* 时间/2(计数1次表示1us)return (cnt*340/2*0.000001*100); //单位:cm
}
//main.c
while (1)
{if(dir != MIDDLE){sgMiddle();dir = MIDDLE;HAL_Delay(300);
}disMiddle = get_distance();if(disMiddle > 35){//前进}else{//停止//测左边距离sgLeft();HAL_Delay(300);disLeft = get_distance();sgMiddle();HAL_Delay(300);sgRight();dir = RIGHT;HAL_Delay(300);disRight = get_distance();}
}
11. 智能小车:摇头避障小车封装电机驱动模块(370.74)
硬件接线
- B-2A – PB0
- B-1A – PB1
- A-1B – PB2
- A-1A – PB10
代码实现
- 基于 smartCar_project9_2
- 代码(28.smartCar_project9_3/MDK-ARM)
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */if(dir != MIDDLE){sgMiddle();dir = MIDDLE;HAL_Delay(300);
}disMiddle = get_distance();if(disMiddle > 35){//前进goForward();}else if(disMiddle < 10){goBack();}else{//停止stop();//测左边距离sgLeft();HAL_Delay(300);disLeft = get_distance();sgMiddle();HAL_Delay(300);sgRight();dir = RIGHT;HAL_Delay(300);disRight = get_distance();if(disLeft < disRight){goRight();HAL_Delay(150);stop();}if(disRight < disLeft){goLeft();HAL_Delay(150);stop();}}HAL_Delay(50);//防止卡死
}
12. 智能小车:测速数据通过串口发送到上位机(371.75)
硬件接线
- 测速模块:
- VCC – 3.3V (不能接 5V,否则遮挡一次会触发 3 次中断)
- OUT – PB14
代码实现
- 代码(28.smartCar_project10/MDK-ARM)
//main.c
unsigned int speedCnt = 0;
void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)//外部中断源回调函数
{if(GPIO_Pin == GPIO_PIN_14)if(HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_14) == GPIO_PIN_RESET)speedCnt++;
}void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)//定时器中断回调函数
{printf("speed: %d\r\n", speedCnt);speedCnt = 0;
}
//main函数中:
HAL_TIM_Base_Start_IT(&htim2);//开启定时器中断
13. 智能小车:OLED代码二次开发(372.76)
硬件接线
- SCL – PB6
- SDA – PB7
封装Oled模块
- 代码(28.smartCar_project11_1/MDK-ARM)
14. 智能小车:实现测速并使用Oled显示速度(373.77)
- 代码(28.smartCar_project11_2/MDK-ARM)
15. 智能小车:Wifi测速小车并本地Oled显示(374.78)
硬件接线
- 把 esp8266 插进串口1
代码实现
- 代码(28.smartCar_project12/MDK-ARM)