原视频:好,升级你的桌面宠物!_哔哩哔哩_bilibili
必读:此开源文章为第一篇(开源!自制一个桌面宠物(STM32CUBEMX HAL库 PWM波 小项目)_stm32桌宠-CSDN博客)的续作,第一篇所制作的桌面宠物仅能通过手机app进行蓝牙遥控,而升级版的桌面宠物已如视频中可以语音和蓝牙同时控制。因两版本大体结构和原理相似,故本文不再重复第一篇文章的内容。重在开源更新后的stm32代码和语音模块的程序设计,以及整体结构的简单介绍。
开源协议:严禁商用;以视频或其他形式发布到互联网上的(包括成果和资料等),必须署名作者“有出息的男孩_”。
注意:语音模块我使用的是ASRPRO(核心板和开发板都可以,开发板更方便),利用自带的两个串口,实现和单片机、蓝牙模块的通信。使用这种ASRPRO核心板
的小伙伴需注意,请仔细看核心板原理图的电源设计部分是否有缺陷,5V口要额外需串接一个4.7欧姆的电阻防止浪涌和尖峰导致芯片烧毁。
这是官方公告:
软件运行框图:
STM32程序修改:
原本不需要修改,但是我无论怎么试,语音模块都发不了正确的数据格式,那就干脆给让32多加点判断类型。
例如:
原程序判断为
if(move_mode == 8){//前进
加入语音模块的指令‘8’ ,修改为
if(move_mode == 8 || move_mode == '8'){//前进
修改后的整个main文件:
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file : main.c
* @brief : Main program body
******************************************************************************
* @attention
*
* Copyright (c) 2023 STMicroelectronics.
* All rights reserved.
*
* This software is licensed under terms that can be found in the LICENSE file
* in the root directory of this software component.
* If no LICENSE file comes with this software, it is provided AS-IS.
*
******************************************************************************
*/
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "tim.h"
#include "usart.h"
#include "gpio.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include <stdio.h>
#include "oled.h"
#include "bmp.h"
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
/* USER CODE END PTD */
/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
/* USER CODE END PD */
/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
/* USER CODE END PM */
/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN PV */
uint8_t move_mode = 0;
uint8_t flag = 0;
#define move_delay 150
#define move_speed 4
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
/* USER CODE BEGIN PFP */
void Rbt_Init(void);
uint16_t angle(uint8_t angle);
void Rbt_attention(void);
void move_forward(void);
void move_behind(void);
void move_right(void);
void move_left(void);
void move_swing(void);
void move_stretch(void);
void move_sleep(void);
void move_dance(void);
/* USER CODE END PFP */
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
//void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin){//按键测试
// if(GPIO_Pin==KEY0_Pin){
// if(move_mode == 5) move_mode = 8;
// else if(move_mode == 8) move_mode = 6;
// else if(move_mode == 6) move_mode = 4;
// else if(move_mode == 4) move_mode = 2;
// else if(move_mode == 2) move_mode = 5;
// }
//}
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
if(move_mode == 8 || move_mode == '8'){
OLED_Clear();
OLED_DrawBMP(0,0,128,8,BMP2);//前进脸
}
else if(move_mode == 2 || move_mode == '2'){
OLED_Clear();
OLED_DrawBMP(0,0,128,8,BMP2);//后退脸
}
else if(move_mode == 4 || move_mode == '4'){
OLED_Clear();
OLED_DrawBMP(0,0,128,8,BMP4);//左转脸
}
else if(move_mode == 6 || move_mode == '6'){
OLED_Clear();
OLED_DrawBMP(0,0,128,8,BMP3);//右转脸
}
else if(move_mode == 'A' || move_mode == '7'){
OLED_Clear();
OLED_DrawBMP(0,0,128,8,BMP2);//前进脸
}
HAL_UART_Receive_IT(&huart1, &move_mode, 1);
}
/* USER CODE END 0 */
/**
* @brief The application entry point.
* @retval int
*/
int main(void)
{
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_TIM2_Init();
MX_USART1_UART_Init();
/* USER CODE BEGIN 2 */
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
HAL_TIM_PWM_Start(&htim2, TIM_CHANNEL_1);
HAL_TIM_PWM_Start(&htim2, TIM_CHANNEL_2);
HAL_TIM_PWM_Start(&htim2, TIM_CHANNEL_3);
HAL_TIM_PWM_Start(&htim2, TIM_CHANNEL_4);
Rbt_Init();
HAL_UART_Receive_IT(&huart1, &move_mode, 1);
HAL_Delay(500);
OLED_Init();
OLED_Clear();
OLED_DrawBMP(0,0,128,8,BMP6);
while (1)
{
/*__HAL_TIM_SetCompare(&htim2, TIM_CHANNEL_1,angle(120));
HAL_Delay(1);*/
if(move_mode == 8 || move_mode == '8'){//前进
OLED_DrawBMP(0,0,128,8,BMP2);
move_forward();
printf("forward\r\n");
HAL_GPIO_TogglePin(LED0_GPIO_Port, LED0_Pin);
flag=0;
}
else if(move_mode == 2 || move_mode == '2'){//后退
OLED_DrawBMP(0,0,128,8,BMP2);
move_behind();
printf("behind\r\n");
HAL_GPIO_TogglePin(LED0_GPIO_Port, LED0_Pin);
flag=0;
}
else if(move_mode == 4 || move_mode == '4'){//左转
move_left();
printf("left\r\n");
HAL_GPIO_TogglePin(LED0_GPIO_Port, LED0_Pin);
flag=0;
}
else if(move_mode == 6 || move_mode == '6'){//右转
OLED_DrawBMP(0,0,128,8,BMP3);
move_right();
printf("right\r\n");
HAL_GPIO_TogglePin(LED0_GPIO_Port, LED0_Pin);
flag=0;
}
else if(move_mode == 5 || move_mode == 'C' || move_mode == '1'){//立正
OLED_DrawBMP(0,0,128,8,BMP1);
Rbt_attention();
printf("stop\r\n");
HAL_GPIO_TogglePin(LED0_GPIO_Port, LED0_Pin);
flag=0;
}
else if(move_mode == 'A' || move_mode == '7'){//摇摆
OLED_DrawBMP(0,0,128,8,BMP2);
move_swing();
printf("swing\r\n");
HAL_GPIO_TogglePin(LED0_GPIO_Port, LED0_Pin);
flag=0;
}
else if(move_mode == 'B' || move_mode == '9'){//坐下招手
if(flag==0){
OLED_DrawBMP(0,0,128,8,BMP2);
move_stretch();
printf("stretch\r\n");
OLED_DrawBMP(0,0,128,8,BMP5);
HAL_GPIO_TogglePin(LED0_GPIO_Port, LED0_Pin);
flag=1;
}
}
else if(move_mode == 'D' || move_mode == '3'){//休眠
if(flag==0){
move_sleep();
printf("sleep\r\n");
OLED_DrawBMP(0,0,128,8,BMP6);
//HAL_GPIO_TogglePin(LED0_GPIO_Port, LED0_Pin);
flag=0;
}
}
else if(move_mode == '0'){//唱歌跳舞//语音模块特有指令
move_dance();
printf("dance\r\n");
HAL_Delay(200);
OLED_DrawBMP(0,0,128,8,BMP2);
HAL_GPIO_TogglePin(LED0_GPIO_Port, LED0_Pin);
}
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
}
/**
* @brief System Clock Configuration
* @retval None
*/
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
/** Initializes the RCC Oscillators according to the specified parameters
* in the RCC_OscInitTypeDef structure.
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
/** Initializes the CPU, AHB and APB buses clocks
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
{
Error_Handler();
}
}
/* USER CODE BEGIN 4 */
void Rbt_Init(void)
{
__HAL_TIM_SetCompare(&htim2, TIM_CHANNEL_1,angle(15));
__HAL_TIM_SetCompare(&htim2, TIM_CHANNEL_3,angle(160));
HAL_Delay(100);
__HAL_TIM_SetCompare(&htim2, TIM_CHANNEL_2,angle(167));
__HAL_TIM_SetCompare(&htim2, TIM_CHANNEL_4,angle(20));
HAL_Delay(100);
}
uint16_t angle(uint8_t angle)
{
return angle*2000/180+500;
}
void Rbt_attention(void)
{
__HAL_TIM_SetCompare(&htim2, TIM_CHANNEL_1,angle(90));
__HAL_TIM_SetCompare(&htim2, TIM_CHANNEL_2,angle(90));
__HAL_TIM_SetCompare(&htim2, TIM_CHANNEL_3,angle(90));
__HAL_TIM_SetCompare(&htim2, TIM_CHANNEL_4,angle(90));
HAL_Delay(100);
}
void move_forward(void)
{
__HAL_TIM_SetCompare(&htim2, TIM_CHANNEL_1,angle(135));
__HAL_TIM_SetCompare(&htim2, TIM_CHANNEL_4,angle(45));
HAL_Delay(move_delay);
__HAL_TIM_SetCompare(&htim2, TIM_CHANNEL_2,angle(45));
__HAL_TIM_SetCompare(&htim2, TIM_CHANNEL_3,angle(135));
HAL_Delay(move_delay);
__HAL_TIM_SetCompare(&htim2, TIM_CHANNEL_1,angle(90));
__HAL_TIM_SetCompare(&htim2, TIM_CHANNEL_4,angle(90));
HAL_Delay(move_delay);
__HAL_TIM_SetCompare(&htim2, TIM_CHANNEL_2,angle(90));
__HAL_TIM_SetCompare(&htim2, TIM_CHANNEL_3,angle(90));
HAL_Delay(move_delay);
__HAL_TIM_SetCompare(&htim2, TIM_CHANNEL_2,angle(135));
__HAL_TIM_SetCompare(&htim2, TIM_CHANNEL_3,angle(45));
HAL_Delay(move_delay);
__HAL_TIM_SetCompare(&htim2, TIM_CHANNEL_1,angle(45));
__HAL_TIM_SetCompare(&htim2, TIM_CHANNEL_4,angle(135));
HAL_Delay(move_delay);
__HAL_TIM_SetCompare(&htim2, TIM_CHANNEL_2,angle(90));
__HAL_TIM_SetCompare(&htim2, TIM_CHANNEL_3,angle(90));
HAL_Delay(move_delay);
__HAL_TIM_SetCompare(&htim2, TIM_CHANNEL_1,angle(90));
__HAL_TIM_SetCompare(&htim2, TIM_CHANNEL_4,angle(90));
HAL_Delay(move_delay);
}
void move_behind(void)
{
__HAL_TIM_SetCompare(&htim2, TIM_CHANNEL_1,angle(45));
__HAL_TIM_SetCompare(&htim2, TIM_CHANNEL_4,angle(135));
HAL_Delay(move_delay);
__HAL_TIM_SetCompare(&htim2, TIM_CHANNEL_2,angle(135));
__HAL_TIM_SetCompare(&htim2, TIM_CHANNEL_3,angle(45));
HAL_Delay(move_delay);
__HAL_TIM_SetCompare(&htim2, TIM_CHANNEL_1,angle(90));
__HAL_TIM_SetCompare(&htim2, TIM_CHANNEL_4,angle(90));
HAL_Delay(move_delay);
__HAL_TIM_SetCompare(&htim2, TIM_CHANNEL_2,angle(90));
__HAL_TIM_SetCompare(&htim2, TIM_CHANNEL_3,angle(90));
HAL_Delay(move_delay);
__HAL_TIM_SetCompare(&htim2, TIM_CHANNEL_2,angle(45));
__HAL_TIM_SetCompare(&htim2, TIM_CHANNEL_3,angle(135));
HAL_Delay(move_delay);
__HAL_TIM_SetCompare(&htim2, TIM_CHANNEL_1,angle(135));
__HAL_TIM_SetCompare(&htim2, TIM_CHANNEL_4,angle(45));
HAL_Delay(move_delay);
__HAL_TIM_SetCompare(&htim2, TIM_CHANNEL_2,angle(90));
__HAL_TIM_SetCompare(&htim2, TIM_CHANNEL_3,angle(90));
HAL_Delay(move_delay);
__HAL_TIM_SetCompare(&htim2, TIM_CHANNEL_1,angle(90));
__HAL_TIM_SetCompare(&htim2, TIM_CHANNEL_4,angle(90));
HAL_Delay(move_delay);
}
void move_right(void)
{
__HAL_TIM_SetCompare(&htim2, TIM_CHANNEL_1,angle(45));
__HAL_TIM_SetCompare(&htim2, TIM_CHANNEL_4,angle(45));
HAL_Delay(move_delay);
__HAL_TIM_SetCompare(&htim2, TIM_CHANNEL_2,angle(135));
__HAL_TIM_SetCompare(&htim2, TIM_CHANNEL_3,angle(135));
HAL_Delay(move_delay);
__HAL_TIM_SetCompare(&htim2, TIM_CHANNEL_1,angle(90));
__HAL_TIM_SetCompare(&htim2, TIM_CHANNEL_4,angle(90));
HAL_Delay(move_delay);
__HAL_TIM_SetCompare(&htim2, TIM_CHANNEL_2,angle(90));
__HAL_TIM_SetCompare(&htim2, TIM_CHANNEL_3,angle(90));
HAL_Delay(move_delay);
}
void move_left(void)
{
__HAL_TIM_SetCompare(&htim2, TIM_CHANNEL_2,angle(135));
__HAL_TIM_SetCompare(&htim2, TIM_CHANNEL_3,angle(135));
HAL_Delay(move_delay);
__HAL_TIM_SetCompare(&htim2, TIM_CHANNEL_1,angle(45));
__HAL_TIM_SetCompare(&htim2, TIM_CHANNEL_4,angle(45));
HAL_Delay(move_delay);
__HAL_TIM_SetCompare(&htim2, TIM_CHANNEL_2,angle(90));
__HAL_TIM_SetCompare(&htim2, TIM_CHANNEL_3,angle(90));
HAL_Delay(move_delay);
__HAL_TIM_SetCompare(&htim2, TIM_CHANNEL_1,angle(90));
__HAL_TIM_SetCompare(&htim2, TIM_CHANNEL_4,angle(90));
HAL_Delay(move_delay);
}
void move_swing(void)
{
__HAL_TIM_SetCompare(&htim2, TIM_CHANNEL_1,angle(130));
__HAL_TIM_SetCompare(&htim2, TIM_CHANNEL_2,angle(130));
__HAL_TIM_SetCompare(&htim2, TIM_CHANNEL_3,angle(50));
__HAL_TIM_SetCompare(&htim2, TIM_CHANNEL_4,angle(50));
HAL_Delay(200);
__HAL_TIM_SetCompare(&htim2, TIM_CHANNEL_1,angle(50));
__HAL_TIM_SetCompare(&htim2, TIM_CHANNEL_2,angle(50));
__HAL_TIM_SetCompare(&htim2, TIM_CHANNEL_3,angle(130));
__HAL_TIM_SetCompare(&htim2, TIM_CHANNEL_4,angle(130));
HAL_Delay(200);
}
void move_stretch(void){
__HAL_TIM_SetCompare(&htim2, TIM_CHANNEL_1,angle(90));
__HAL_TIM_SetCompare(&htim2, TIM_CHANNEL_2,angle(90));
__HAL_TIM_SetCompare(&htim2, TIM_CHANNEL_3,angle(90));
__HAL_TIM_SetCompare(&htim2, TIM_CHANNEL_4,angle(90));
if(TIM2->CCR1<angle(155) && TIM2->CCR3>angle(25)){
for(uint8_t i=0;i<70;i++){
__HAL_TIM_SetCompare(&htim2, TIM_CHANNEL_1,angle(90+i));
__HAL_TIM_SetCompare(&htim2, TIM_CHANNEL_3,angle(90-i));
HAL_Delay(move_speed);
}
HAL_Delay(1000);
for(uint8_t i=0;i<70;i++){
__HAL_TIM_SetCompare(&htim2, TIM_CHANNEL_1,angle(160-i));
__HAL_TIM_SetCompare(&htim2, TIM_CHANNEL_3,angle(20+i));
HAL_Delay(move_speed);
}
for(uint8_t i=0;i<65;i++){
__HAL_TIM_SetCompare(&htim2, TIM_CHANNEL_2,angle(90+i));
__HAL_TIM_SetCompare(&htim2, TIM_CHANNEL_4,angle(90-i));
HAL_Delay(move_speed);
}
for(uint8_t i=0;i<20;i++){
__HAL_TIM_SetCompare(&htim2, TIM_CHANNEL_1,angle(90-i));
__HAL_TIM_SetCompare(&htim2, TIM_CHANNEL_3,angle(90+i));
HAL_Delay(move_speed);
}
HAL_Delay(1000);
__HAL_TIM_SetCompare(&htim2, TIM_CHANNEL_1,angle(140));
HAL_Delay(1000);
__HAL_TIM_SetCompare(&htim2, TIM_CHANNEL_1,angle(178));
HAL_Delay(500);
__HAL_TIM_SetCompare(&htim2, TIM_CHANNEL_1,angle(140));
HAL_Delay(500);
__HAL_TIM_SetCompare(&htim2, TIM_CHANNEL_1,angle(178));
HAL_Delay(500);
__HAL_TIM_SetCompare(&htim2, TIM_CHANNEL_1,angle(140));
HAL_Delay(500);
__HAL_TIM_SetCompare(&htim2, TIM_CHANNEL_1,angle(70));
}
}
void move_sleep(void){
if(TIM2->CCR3<angle(160) && TIM2->CCR1>angle(20)){
for(uint8_t i=0;i<75;i++){
__HAL_TIM_SetCompare(&htim2, TIM_CHANNEL_1,angle(90-i));
__HAL_TIM_SetCompare(&htim2, TIM_CHANNEL_3,angle(90+i));
HAL_Delay(move_speed);
}
}
if(TIM2->CCR2<angle(160) && TIM2->CCR4>angle(20)){
for(uint8_t i=0;i<75;i++){
__HAL_TIM_SetCompare(&htim2, TIM_CHANNEL_2,angle(90+i));
__HAL_TIM_SetCompare(&htim2, TIM_CHANNEL_4,angle(90-i));
HAL_Delay(move_speed);
}
}
}
void move_dance(void)
{
__HAL_TIM_SetCompare(&htim2, TIM_CHANNEL_1,angle(90));
__HAL_TIM_SetCompare(&htim2, TIM_CHANNEL_2,angle(90));
__HAL_TIM_SetCompare(&htim2, TIM_CHANNEL_3,angle(90));
__HAL_TIM_SetCompare(&htim2, TIM_CHANNEL_4,angle(90));
if(TIM2->CCR1<angle(160) && TIM2->CCR2<angle(160)){//当在目标位置之外,则执行
for(uint8_t i=0;i<75;i++){
__HAL_TIM_SetCompare(&htim2, TIM_CHANNEL_1,angle(90+i));
__HAL_TIM_SetCompare(&htim2, TIM_CHANNEL_2,angle(90+i));
HAL_Delay(move_speed);
}
for(uint8_t i=0;i<75;i++){
__HAL_TIM_SetCompare(&htim2, TIM_CHANNEL_1,angle(165-i));
__HAL_TIM_SetCompare(&htim2, TIM_CHANNEL_2,angle(165-i));
HAL_Delay(move_speed);
}
}
if(TIM2->CCR3>angle(20) && TIM2->CCR4>angle(20)){
for(uint8_t i=0;i<75;i++){
__HAL_TIM_SetCompare(&htim2, TIM_CHANNEL_3,angle(90-i));
__HAL_TIM_SetCompare(&htim2, TIM_CHANNEL_4,angle(90-i));
HAL_Delay(move_speed);
}
for(uint8_t i=0;i<75;i++){
__HAL_TIM_SetCompare(&htim2, TIM_CHANNEL_3,angle(15+i));
__HAL_TIM_SetCompare(&htim2, TIM_CHANNEL_4,angle(15+i));
HAL_Delay(move_speed);
}
}
}
/* USER CODE END 4 */
/**
* @brief This function is executed in case of error occurrence.
* @retval None
*/
void Error_Handler(void)
{
/* USER CODE BEGIN Error_Handler_Debug */
/* User can add his own implementation to report the HAL error return state */
__disable_irq();
while (1)
{
}
/* USER CODE END Error_Handler_Debug */
}
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t *file, uint32_t line)
{
/* USER CODE BEGIN 6 */
/* User can add his own implementation to report the file name and line number,
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */
语音模块程序设计 :
使用天问block进行开发,积木编程。
1.设置一个全局变量,用于运动指令的存储
2. 配置语音
3.配置系统初始化,打开两个串口,打开定时器
4.配置语音指令对应的操作
5.配置定时器任务,用于蓝牙模块数据的接收和运动指令的发送
欢迎加入技术交流群: 948064955