1 电机介绍
官方资料:https://www.waveshare.net/wiki/DDSM210
DDSM210 直驱伺服电机是基于一体化开发理念,集外转子无刷电机、编码器、伺服驱动于一体的高可靠性永磁同步电动机,其结构紧凑,安装方便,运行稳定,小体积、大扭矩特别适合应用于以下直驱领域:机器人关节、小型 AGV 驱动轮、平衡车驱动轮、开发车载平台等高级机器人项目。
本文使用这款电机作为轮腿机器人的驱动轮电机,需要单片机通过串口发送控制命令数据控制电机,串口通信协议如下,
电机接口和线序说明:
2 实验目标
使用两个串口,一个串口用来驱动电机,另一个串口用来将发送到电机的信息和从电机接收的信息发送到电脑的串口助手中显示。使用按键控制电机的转速,每一个按键代表一个转速,按下不同按键电机就会按对应转速转动。
3 硬件连接图
电机引出的四条线分别是串口收发和电源的正负极,串口收和发分别接单片机引脚的发和收,电源正负极就接在电源上。需要注意的是还需要一条线将电源负极和单片机共地。
4 程序编写
本次实验直接使用cubemax生成HAL库的基础代码,这种方法非常方便,也是未来的主流做法。然后再基础代码基础上编写实现具体功能的代码。
4.1 配置cubemax生成代码
打开cubemax,选择用MCU开始工程,
然后搜索使用的单片机芯片,本实验使用的是f103zet6
使用USART1和串口助手通信,
使用USART3和伺服电机通信,
然后设置按键的gpio引脚为输入模式,首先看一下板子的按键原理图,
为了配置时钟选择外部晶振作为时钟源,
时钟配置如下,
然后设置工程名和IDE,生成代码
4.2 具体功能实现代码
c
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file : main.c
* @brief : Main program body
******************************************************************************
* @attention
*
* Copyright (c) 2024 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 "usart.h"
#include "gpio.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
/* 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 */
// CRC-8/MAXIM多项式
#define POLYNOMIAL 0x9B // 多项式为 x^8 + x^5 + x^4 + 1
/* USER CODE END PM */
/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN PV */
uint16_t motor_speed = 60;
uint8_t high_byte, low_byte;
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
/* USER CODE BEGIN PFP */
/* USER CODE END PFP */
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
// CRC-8查询表
const uint8_t crc_table[256] = {
0x00, 0x5e, 0xbc, 0xe2, 0x61, 0x3f, 0xdd, 0x83, 0xc2, 0x9c, 0x7e, 0x20, 0xa3, 0xfd, 0x1f, 0x41,
0x9d, 0xc3, 0x21, 0x7f, 0xfc, 0xa2, 0x40, 0x1e, 0x5f, 0x01, 0xe3, 0xbd, 0x3e, 0x60, 0x82, 0xdc,
0x23, 0x7d, 0x9f, 0xc1, 0x42, 0x1c, 0xfe, 0xa0, 0xe1, 0xbf, 0x5d, 0x03, 0x80, 0xde, 0x3c, 0x62,
0xbe, 0xe0, 0x02, 0x5c, 0xdf, 0x81, 0x63, 0x3d, 0x7c, 0x22, 0xc0, 0x9e, 0x1d, 0x43, 0xa1, 0xff,
0x46, 0x18, 0xfa, 0xa4, 0x27, 0x79, 0x9b, 0xc5, 0x84, 0xda, 0x38, 0x66, 0xe5, 0xbb, 0x59, 0x07,
0xdb, 0x85, 0x67, 0x39, 0xba, 0xe4, 0x06, 0x58, 0x19, 0x47, 0xa5, 0xfb, 0x78, 0x26, 0xc4, 0x9a,
0x65, 0x3b, 0xd9, 0x87, 0x04, 0x5a, 0xb8, 0xe6, 0xa7, 0xf9, 0x1b, 0x45, 0xc6, 0x98, 0x7a, 0x24,
0xf8, 0xa6, 0x44, 0x1a, 0x99, 0xc7, 0x25, 0x7b, 0x3a, 0x64, 0x86, 0xd8, 0x5b, 0x05, 0xe7, 0xb9,
0x8c, 0xd2, 0x30, 0x6e, 0xed, 0xb3, 0x51, 0x0f, 0x4e, 0x10, 0xf2, 0xac, 0x2f, 0x71, 0x93, 0xcd,
0x11, 0x4f, 0xad, 0xf3, 0x70, 0x2e, 0xcc, 0x92, 0xd3, 0x8d, 0x6f, 0x31, 0xb2, 0xec, 0x0e, 0x50,
0xaf, 0xf1, 0x13, 0x4d, 0xce, 0x90, 0x72, 0x2c, 0x6d, 0x33, 0xd1, 0x8f, 0x0c, 0x52, 0xb0, 0xee,
0x32, 0x6c, 0x8e, 0xd0, 0x53, 0x0d, 0xef, 0xb1, 0xf0, 0xae, 0x4c, 0x12, 0x91, 0xcf, 0x2d, 0x73,
0xca, 0x94, 0x76, 0x28, 0xab, 0xf5, 0x17, 0x49, 0x08, 0x56, 0xb4, 0xea, 0x69, 0x37, 0xd5, 0x8b,
0x57, 0x09, 0xeb, 0xb5, 0x36, 0x68, 0x8a, 0xd4, 0x95, 0xcb, 0x29, 0x77, 0xf4, 0xaa, 0x48, 0x16,
0xe9, 0xb7, 0x55, 0x0b, 0x88, 0xd6, 0x34, 0x6a, 0x2b, 0x75, 0x97, 0xc9, 0x4a, 0x14, 0xf6, 0xa8,
0x74, 0x2a, 0xc8, 0x96, 0x15, 0x4b, 0xa9, 0xf7, 0xb6, 0xe8, 0x0a, 0x54, 0xd7, 0x89, 0x6b, 0x35
};
// CRC-8计算函数
uint8_t crc8_cdma2000(uint8_t *data, uint8_t len) {
uint8_t crc = 0;
uint8_t i;
for (i = 0; i < len; ++i) {
crc = crc_table[crc ^ data[i]];
}
return crc;
}
//按键检测控制电机速度
void getSpeedFromKey() {
uint8_t key0 = HAL_GPIO_ReadPin(GPIOE, GPIO_PIN_4);
uint8_t key1 = HAL_GPIO_ReadPin(GPIOE, GPIO_PIN_3);
uint8_t key_up = HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_0);
if (key0 == 0 || key1 == 0 || key_up == 1) {//有按键按下
//消抖
HAL_Delay(10);
if (key0 == 0) {
motor_speed = 0;
printf("期望电机转速: %d\n", motor_speed);
} else if ( key1 == 0) {
motor_speed = 100;
printf("期望电机转速: %d\n", motor_speed);
} else if (key_up == 1) {
motor_speed = 210;
printf("期望电机转速: %d\n", motor_speed);
}
} else {
printf("无键按下\t");
printf("期望电机转速: %d\n", motor_speed);
return;
}
}
// 函数功能:将十进制数转换为十六进制数的高八位和第八位
// 输入参数:decimal - 十进制数
// 输出参数:high_byte - 高八位
// low_byte - 第八位
void decimal_to_hex_bytes(uint16_t decimal, uint8_t *high_byte, uint8_t *low_byte) {
uint16_t hex_value = decimal; // 将十进制数转换为十六进制数
// 获取高八位
*high_byte = (uint8_t)((hex_value >> 8) & 0xFF);
// 获取第八位
*low_byte = (uint8_t)(hex_value & 0xFF);
}
/* 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_USART1_UART_Init();
MX_USART3_UART_Init();
/* USER CODE BEGIN 2 */
//
uint8_t data[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
HAL_UART_Transmit(&huart1, data, sizeof(data), HAL_MAX_DELAY);
uint8_t data1[] = {0x01, 0xA0, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xE4}; //切换速度模式
uint8_t data2[] = {0x01, 0xA0, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xD9}; //切换位置模式
uint8_t data3[] = {0x01, 0x64, 0xFF, 0xCE, 0x00, 0x00, 0x00, 0x00, 0x00, 0xDA};
uint8_t data4[] = {0x01, 0x64, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x50};
uint8_t data5[] = {0xAA, 0x55, 0x53, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
//uint8_t data11[] = {0x01, 0xA0, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
//uint8_t data1_crc = crc8_cdma2000(data11, 9);
//
// for (int i = 0; i < 5; ++i) {
// HAL_UART_Transmit(&huart1, data5, sizeof(data5), HAL_MAX_DELAY);
// }
HAL_UART_Transmit(&huart3, data1, sizeof(data1), HAL_MAX_DELAY);//切换成速度模式
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
getSpeedFromKey();
decimal_to_hex_bytes(motor_speed, &high_byte, &low_byte);
data[0] = 0x01;
data[1] = 0x64;
data[2] = high_byte;
data[3] = low_byte;
uint8_t data_crc[] = {data[0],data[1],data[2],data[3],data[4],data[5],data[6],data[7],data[8]};
data[9] = crc8_cdma2000(data_crc, 9);
//通过串口发布
HAL_UART_Transmit(&huart3, data, sizeof(data), HAL_MAX_DELAY);
printf("data: ");
for (int i = 0; i < 10; ++i) {
printf("0x%02X ", data[i]);
}
printf("\n");
//printf("CRC-8/MAXIM: 0x%02X\n", data1_crc);
//HAL_UART_Transmit(&huart1, data1, sizeof(data1), HAL_MAX_DELAY);
//HAL_UART_Transmit(&huart3, data3, sizeof(data3), HAL_MAX_DELAY);
HAL_Delay(1000);
}
/* 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 */
int fputc(int ch, FILE *f)
{
//采用轮询方式发送1字节数据,超时时间设置为无限等待
HAL_UART_Transmit(&huart1, (uint8_t *)&ch, 1, 1);
return ch;
}
/* 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 */
实验效果
见B站:https://www.bilibili.com/video/BV1WE421j78c/?vd_source=62bfb7720b0b2f9941f7f34210ba6a18