项目名称:蓝牙/遥控 小车
功能描述:通过蓝牙连接之后,可以控制小车前进、后退、左转、右转、停止。
控制系统研究计划:先在开发板上试验各功能模块,实验成功后再整合到单独的板子上。
- 蓝牙模块:蓝牙模块与手机图形化显示软件的连接,信息的发送、接收。
- 电机控制模块:在开发板上实现对电机的控制,包括正转、反转。
- 蓝牙控制电机:通过蓝牙通讯控制小车前进、后退、左转、右转
蓝牙模块实现如下:
蓝牙控制开发板LED灯(蓝牙+串口+LCD显示)_搁浅小鲸鱼的博客-CSDN博客
电机控制模块实现如下:
按键调整PWM占空比,实现电机转速调整(基于STM32开发板)_stm32f103pwm占空比控制_搁浅小鲸鱼的博客-CSDN博客
结合上述两部分代码,可实现蓝牙通信控制电机,图片如下图所示:
实际运行视频放不上来,就简单描述一下:手机端输入控制命令
输入字符-f,电机1正转,电机2正转,前进;
输入字符-b,电机1反转,电机2反转,后退;
输入字符-l,电机1正转,电机2反转,左转;
输入字符-r,电机1反转,电机2正转,右转;
输入字符-s,电机1制动,电机2制动,制动;
其主函数代码如下:
#include "./SYSTEM/sys/sys.h"
#include "./SYSTEM/delay/delay.h"
#include "./BSP/USART_SELF/usart_self.h"
#include "./BSP/LED/led.h"
#include "./BSP/LCD/lcd.h"
#include "./BSP/TIMER/gtim.h"
#include "./BSP/GPIO/gpio.h"
extern TIM_HandleTypeDef g_timx_pwm1_chy_handle; /* 定时器x句柄 */
extern TIM_HandleTypeDef g_timx_pwm2_chy_handle; /* 定时器x句柄 */
int main(void)
{
HAL_Init(); /* 初始化HAL库 */
sys_stm32_clock_init(RCC_PLL_MUL9); /* 设置时钟,72M */
delay_init(72); /* 初始化延时函数 */
led_init(); /* 初始化LED */
usart_init(9600);
lcd_init();
ABin_init();
gtim_timx_pwm1_chy_init(500 - 1, 72 - 1); /* 1Mhz的计数频率,2Khz的PWM */
gtim_timx_pwm2_chy_init(500 - 1, 72 - 1); /* 1Mhz的计数频率,2Khz的PWM */
/* 小车运动状态判定参数 */
char carFront = 'f';
char carBack = 'b';
char carLeft = 'l';
char carRight = 'r';
char carStop = 's';
char bufDate; // 手机端发送的数据
uint16_t MotorPwmVal = 250; // 设置PWM的脉宽
/* 串口状态复位 */
while(1)
{
if(g_usart_rx_flag == 1)
{
//printf("The char is:");
//HAL_UART_Transmit(&usart_handle, (uint8_t *)g_rx_buffer, 1, 1000);
while(__HAL_UART_GET_FLAG(&usart_handle, UART_FLAG_TC) != 1);
g_usart_rx_flag = 0;
}
else
{
delay_ms(10);
}
/* 控制命令显示 */
lcd_show_string(10, 20, 240, 32, 32, "BlueConTest", BLACK);
lcd_show_string(10, 60, 240, 24, 24, "The data is:", BLACK);
lcd_show_string(160, 60, 240, 24, 24, (char *)g_rx_buffer, BLACK);
/* 电机脉宽控制 */
__HAL_TIM_SET_COMPARE(&g_timx_pwm1_chy_handle, GTIM_TIMX_PWM1_CHY, MotorPwmVal);
__HAL_TIM_SET_COMPARE(&g_timx_pwm2_chy_handle, GTIM_TIMX_PWM2_CHY, MotorPwmVal);
/* 小车控制 前进-后退-左转-右转-停止 */
bufDate = g_rx_buffer[0];
if( bufDate == carFront )
{
HAL_GPIO_WritePin(AIN1_GPIO_PORT,AIN1_GPIO_PIN,GPIO_PIN_SET);
HAL_GPIO_WritePin(AIN2_GPIO_PORT,AIN2_GPIO_PIN,GPIO_PIN_RESET);
HAL_GPIO_WritePin(BIN1_GPIO_PORT,BIN1_GPIO_PIN,GPIO_PIN_SET);
HAL_GPIO_WritePin(BIN2_GPIO_PORT,BIN2_GPIO_PIN,GPIO_PIN_RESET);
}
if( bufDate == carBack )
{
HAL_GPIO_WritePin(AIN1_GPIO_PORT,AIN1_GPIO_PIN,GPIO_PIN_RESET);
HAL_GPIO_WritePin(AIN2_GPIO_PORT,AIN2_GPIO_PIN,GPIO_PIN_SET);
HAL_GPIO_WritePin(BIN1_GPIO_PORT,BIN1_GPIO_PIN,GPIO_PIN_RESET);
HAL_GPIO_WritePin(BIN2_GPIO_PORT,BIN2_GPIO_PIN,GPIO_PIN_SET);
}
if( bufDate == carLeft )
{
HAL_GPIO_WritePin(AIN1_GPIO_PORT,AIN1_GPIO_PIN,GPIO_PIN_SET);
HAL_GPIO_WritePin(AIN2_GPIO_PORT,AIN2_GPIO_PIN,GPIO_PIN_RESET);
HAL_GPIO_WritePin(BIN1_GPIO_PORT,BIN1_GPIO_PIN,GPIO_PIN_RESET);
HAL_GPIO_WritePin(BIN2_GPIO_PORT,BIN2_GPIO_PIN,GPIO_PIN_SET);
}
if( bufDate == carRight )
{
HAL_GPIO_WritePin(AIN1_GPIO_PORT,AIN1_GPIO_PIN,GPIO_PIN_RESET);
HAL_GPIO_WritePin(AIN2_GPIO_PORT,AIN2_GPIO_PIN,GPIO_PIN_SET);
HAL_GPIO_WritePin(BIN1_GPIO_PORT,BIN1_GPIO_PIN,GPIO_PIN_SET);
HAL_GPIO_WritePin(BIN2_GPIO_PORT,BIN2_GPIO_PIN,GPIO_PIN_RESET);
}
if( bufDate == carStop )
{
ABin_init();
}
}
}