Controller Area Network 控制器局域网络 是ISO国际标准化的串行通信协议。在汽车产业中,出于对安全性、舒适性、方便性、低公害、低成本的要求,各种各样的电子控制系统被开发了出来。由于这些系统之间通信所用的数据类型及对可靠性的要求不尽相同,由多条总线构成的情况很多,线束的数量也随之增加。为适应“减少线束的数量”、“通过多个LAN,进行大量数据的高速通信”的需要,1986 年德国电气商博世公司开发出面向汽车的CAN 通信协议。此后,CAN 通过ISO11898及ISO11519进行了标准化,在欧洲已是汽车网络的标准协议。
CAN 的高性能和可靠性已被认同,并被广泛地应用于工业自动化、船舶、医疗设备、工业设备等方面。现场总线是当今自动化领域技术发展的热点之一,被誉为自动化领域的计算机局域网。它的出现为分布式控制系统实现各节点之间实时、可靠的数据通信提供了强有力的技术支持。近年来,它具有的高可靠性和良好的错误检测能力受到重视,被广泛应用于汽车计算机控制系统和环境温度恶劣、电磁辐射强及振动大的工业环境。
该图生成于CAN Bit Time Calculation,可以看出在iRM2018的时钟树配置下,42MHz的CAN总线,最推荐的是黄色的那条,是稳定和高速的交际范围,越往上越快,但是越不稳定,越往下越稳定,但是越慢。因为C620电调和M3508电机接收的比特率是1Mbps,所以我们只能选择最上面的那个最快的那组参数。
/* Includes ------------------------------------------------------------------*/ #include"main.h"
/* Private includes ----------------------------------------------------------*/ /* USER CODE BEGIN Includes */ #include"string.h" #include"stdio.h" /* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/ /* USER CODE BEGIN PTD */ typedefstruct{ uint8_t motor_id; int16_t angle; int16_t current_get; int16_t speed_rpm; uint8_t temperature; } motor_3508_t; /* USER CODE END PTD */
/* Private define ------------------------------------------------------------*/ /* USER CODE BEGIN PD */ #define CAN1_DEVICE_NUM 4 #define FIRST_GROUP_ID 0x200 #define MOTOR_SPEED_MAX 16384 #define CAN_DATA_SIZE 8 #define CAN1_RX_ID_START 0x201 #define MOTOR_ID 2 /* USER CODE END PD */
/* Private macro -------------------------------------------------------------*/ /* USER CODE BEGIN PM */
/* USER CODE BEGIN PV */ int16_t speedFlag=0; uint8_t can1_rx_buffer[CAN1_DEVICE_NUM][CAN_DATA_SIZE]; motor_3508_t motorPrintTest={.motor_id=MOTOR_ID}; /* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/ voidSystemClock_Config(void); staticvoidMX_GPIO_Init(void); staticvoidMX_CAN1_Init(void); staticvoidMX_UART7_Init(void); /* USER CODE BEGIN PFP */ voidcan_filter_enable(CAN_HandleTypeDef* hcan); voidcan_filter_disable(CAN_HandleTypeDef* hcan); voidcan_transmit(CAN_HandleTypeDef* hcan, uint16_t id, int16_t msg1, int16_t msg2, int16_t msg3, int16_t msg4); /* USER CODE END PFP */
/* Private user code ---------------------------------------------------------*/ /* USER CODE BEGIN 0 */
/* USER CODE END 0 */
/** * @brief The application entry point. * @retval int */ intmain(void) { /* USER CODE BEGIN 1 */
/* 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_CAN1_Init(); MX_UART7_Init(); /* USER CODE BEGIN 2 */ can_filter_disable(&hcan1); HAL_CAN_ActivateNotification(&hcan1, CAN_IT_RX_FIFO0_MSG_PENDING); HAL_CAN_Start(&hcan1); /* USER CODE END 2 */
/* Infinite loop */ /* USER CODE BEGIN WHILE */ while (1) { while(speedFlag <= MOTOR_SPEED_MAX/12){ can_transmit(&hcan1,FIRST_GROUP_ID,speedFlag,speedFlag,speedFlag,speedFlag); speedFlag++; HAL_Delay(1); } while(speedFlag >= -MOTOR_SPEED_MAX/12){ can_transmit(&hcan1,FIRST_GROUP_ID,speedFlag,speedFlag,speedFlag,speedFlag); speedFlag--; HAL_Delay(1); } /* USER CODE END WHILE */
/** * @brief This function is executed in case of error occurrence. * @retval None */ voidError_Handler(void) { /* USER CODE BEGIN Error_Handler_Debug */ /* User can add his own implementation to report the HAL error return state */
/* 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 */ voidassert_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, tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */ /* USER CODE END 6 */ } #endif/* USE_FULL_ASSERT */
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/