更新了一些文档
This commit is contained in:
parent
648de9f370
commit
b024d56bca
2
Makefile
2
Makefile
|
@ -130,7 +130,7 @@ modules/remote/remote_control.c \
|
|||
modules/super_cap/super_cap.c \
|
||||
modules/can_comm/can_comm.c \
|
||||
modules/message_center/message_center.c \
|
||||
modules/monitor/monitor.hc \
|
||||
modules/monitor/monitor.c \
|
||||
application/gimbal/gimbal.c \
|
||||
application/chassis/chassis.c \
|
||||
application/shoot/shoot.c \
|
||||
|
|
69
README.md
69
README.md
|
@ -1,10 +1,14 @@
|
|||
# 2023 EC-basic-framework(C语言版)说明
|
||||
|
||||
|
||||
|
||||
当前版本更新日期:2022.11.03
|
||||
|
||||
本说明仅针对电控组2023赛季框架,如有变动以日期靠后的版本为准。**==由于当前仍然处在测试开发阶段,请定期拉取(`git pull`)获取最新更新。==**
|
||||
|
||||
- 开发方式:
|
||||
## 基本信息和开发规范
|
||||
|
||||
- **开发方式**:
|
||||
|
||||
本框架使用stm32cubemx生成,基于makefile,使用gcc-arm-none-eabi编译(make命令)。
|
||||
|
||||
|
@ -12,11 +16,11 @@
|
|||
>
|
||||
> ***强烈推荐使用VSCode进行开发,Ozone进行调试。***
|
||||
|
||||
VSCode可通过Cortex-Debug利用OpenOCD进行调试,jlink/stlink/dap-link都支持,具体的使用方法和环境配置教程在[VSCode+Ozone使用方法](./VSCode+Ozone使用方法.md)中。
|
||||
VSCode可通过Cortex-Debug利用OpenOCD进行调试,jlink/stlink/dap-link都支持,具体的使用方法和环境配置教程在[VSCode+Ozone使用方法](./VSCode+Ozone使用方法.md)中。**请使用UTF-8编码查看\&编辑此项目**。
|
||||
|
||||
推荐使用 **SEGGER ozone** 进行调试。
|
||||
|
||||
- 分层:
|
||||
- **分层**:
|
||||
|
||||
本框架主要代码分为**BSP、Module、APP**三层。三层的代码分别存放在同名的三个文件夹中,这三个文件夹存放在根目录下。开发过程中主要编写APP层代码,Module层与BSP层不建议修改。如需添加module(如oled屏幕、其他传感器和外设等),请按照规范编写并联系组长提交commit到dev分支,完善后合并至主分支。在配置git的时候,将自己的`user.name`配置成英文缩写或易懂的nick name。
|
||||
|
||||
|
@ -24,7 +28,7 @@
|
|||
|
||||
**main.c的位置在**`HAL_N_Middlewares/Src/main.c`
|
||||
|
||||
- 代码格式:
|
||||
- **代码格式**:
|
||||
|
||||
在vscode-设置-扩展-C/C++-C_Cpp:style下修改。默认为`Visual Studio`。编写完新的代码后,使用右键-格式化文档(注:请勿对cube生成的文件使用此操作)。此操作不会改变文档的内容,但会改变缩进、空行、符号位置等,使代码更加统一、整洁。
|
||||
|
||||
|
@ -32,12 +36,59 @@
|
|||
|
||||
每个功能模块编写完之后,及时添加说明文档。内容参照已有的文档,要进行简短的**总体说明、代码结构、外部接口和类型定义、私有函数和变量,以及使用的说明和范例**。如果有特别需要注意的地方,也请说明。
|
||||
|
||||
==**在编写代码的时候,注意添加安全检查,“treat your users as idiot!”**==
|
||||
==**在编写代码的时候,注意添加安全检查,“treat your users as idiots!”**==
|
||||
|
||||
- 面向对象设计:
|
||||
- **面向对象设计**:
|
||||
|
||||
C语言不存在“成员函数”的概念。为实现类似效果,所有按照这一思想构建的函数都会有一个传入参数,将结构体(对象)传入。
|
||||
|
||||
- **代码风格:**
|
||||
|
||||
函数统一使用**动宾短语**,建议不超过4个单词。每个单词首字母大写:
|
||||
|
||||
```c
|
||||
void SetMotorControl()
|
||||
```
|
||||
|
||||
变量命名使用下划线命名法,统一小写。尽量不要使用缩写,并注意让变量名本身能够表达其含义:
|
||||
|
||||
```c
|
||||
uint8_t gimbal_recv_cmd;
|
||||
```
|
||||
|
||||
后续可能将指针类型的变量名都加上`ptr_`或`p`前缀。私有变量加上下划线`_`前缀。
|
||||
|
||||
在利用`typedef`定义新的类型时,使用单词首字母大写+下划线隔开+定义后缀的方式:
|
||||
|
||||
```c
|
||||
typedef struct
|
||||
{
|
||||
float Accel[3];
|
||||
float Gyro[3];
|
||||
} IMU_Data_t;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
can_instance_config_s can_config;
|
||||
uint8_t send_data_len;
|
||||
uint8_t recv_data_len;
|
||||
} CANComm_Init_Config_s;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
float *other_angle_feedback_ptr;
|
||||
float *other_speed_feedback_ptr;
|
||||
|
||||
PID_t current_PID;
|
||||
PID_t speed_PID;
|
||||
PID_t angle_PID;
|
||||
|
||||
float pid_ref; // 将会作为每个环的输入和输出顺次通过串级闭环
|
||||
} Motor_Controller_s;
|
||||
```
|
||||
|
||||
数据类型单一、结构不复杂的类型以`_t`后缀结尾(表明这是一种数据,type);复杂的结构体类型使用`_s`结尾,表明其功能和内涵多(structure)。
|
||||
|
||||
## BSP层(Board Sopport Package)
|
||||
|
||||
- TODO:
|
||||
|
@ -277,9 +328,13 @@ ROOT:.
|
|||
super_cap.md
|
||||
```
|
||||
|
||||
|
||||
|
||||
## BSP/Module/Application介绍
|
||||
|
||||
在对应应用、模块和板级支持包文件夹下。
|
||||
在对应应用、模块和板级支持包文件夹下。每个.c文件或完整的功能模块都有说明文档。在编写新代码时注意按照规范编写说明文档。
|
||||
|
||||
|
||||
|
||||
## 整体架构
|
||||
|
||||
|
|
|
@ -1,3 +1,14 @@
|
|||
/**
|
||||
* @file robot_def.h
|
||||
* @author NeoZeng neozng1@hnu.edu.cn
|
||||
* @author Even
|
||||
* @version 0.1
|
||||
* @date 2022-12-02
|
||||
*
|
||||
* @copyright Copyright (c) HNU YueLu EC 2022 all rights reserved
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef ROBOT_DEF_H
|
||||
#define ROBOT_DEF_H
|
||||
|
||||
|
|
|
@ -4,12 +4,14 @@
|
|||
#include "memory.h"
|
||||
|
||||
/* can instance ptrs storage, used for recv callback */
|
||||
// 在CAN产生接收中断会遍历数组,选出hcan和rxid与发生中断的实例相同的那个,调用其回调函数
|
||||
static can_instance *instance[MX_REGISTER_DEVICE_CNT] = {NULL};
|
||||
|
||||
/* ----------------two static function called by CANRegister()-------------------- */
|
||||
|
||||
/**
|
||||
* @brief add filter to receive mesg with specific ID,called by CANRegister()
|
||||
* 给CAN添加过滤器后,BxCAN会根据接收到的报文的id进行消息过滤,符合规则的id会被填入FIFO触发中断
|
||||
*
|
||||
* @note there are total 28 filter and 2 FIFO in bxCAN of STM32F4 series product.
|
||||
* here, we assign the former 14 to CAN1 and the rest for CAN2
|
||||
|
@ -40,6 +42,7 @@ static void CANAddFilter(can_instance *_instance)
|
|||
|
||||
/**
|
||||
* @brief called by CANRegister before the first module being registered
|
||||
* 在第一个CAN实例初始化的时候会自动调用此函数,启动CAN服务
|
||||
*
|
||||
* @note this func will handle all these thing automatically
|
||||
* there is no need to worry about hardware initialization, we do these for you!
|
||||
|
@ -57,40 +60,42 @@ static void CANServiceInit()
|
|||
|
||||
/* ----------------------- two extern callable function -----------------------*/
|
||||
|
||||
can_instance* CANRegister(can_instance_config_s* config)
|
||||
can_instance *CANRegister(can_instance_config_s *config)
|
||||
{
|
||||
static uint8_t idx;
|
||||
static uint8_t idx; // 全局CAN实例索引,每次有新的模块注册会自增
|
||||
if (!idx)
|
||||
{
|
||||
CANServiceInit();
|
||||
CANServiceInit(); // 第一次注册,先进行硬件初始化
|
||||
}
|
||||
instance[idx] = (can_instance*)malloc(sizeof(can_instance));
|
||||
memset(instance[idx],0,sizeof(can_instance));
|
||||
|
||||
instance[idx] = (can_instance *)malloc(sizeof(can_instance)); // 分配空间
|
||||
memset(instance[idx], 0, sizeof(can_instance));
|
||||
// 进行发送报文的配置
|
||||
instance[idx]->txconf.StdId = config->tx_id;
|
||||
instance[idx]->txconf.IDE = CAN_ID_STD;
|
||||
instance[idx]->txconf.RTR = CAN_RTR_DATA;
|
||||
instance[idx]->txconf.DLC = 0x08; // 默认发送长度为8
|
||||
|
||||
// 设置回调函数和接收发送id
|
||||
instance[idx]->can_handle = config->can_handle;
|
||||
instance[idx]->tx_id = config->tx_id;
|
||||
instance[idx]->tx_id = config->tx_id; // 好像没用,可以删掉
|
||||
instance[idx]->rx_id = config->rx_id;
|
||||
instance[idx]->can_module_callback = config->can_module_callback;
|
||||
|
||||
CANAddFilter(instance[idx]);
|
||||
return instance[idx++];
|
||||
CANAddFilter(instance[idx]); // 添加CAN过滤器规则
|
||||
return instance[idx++]; // 返回指针
|
||||
}
|
||||
|
||||
/* TODO:目前似乎封装过度,应该添加一个指向tx_buff的指针,tx_buff不应该由CAN instance保存 */
|
||||
void CANTransmit(can_instance *_instance)
|
||||
{
|
||||
while (HAL_CAN_GetTxMailboxesFreeLevel(_instance->can_handle) == 0)
|
||||
;
|
||||
// tx_mailbox会保存实际填入了这一帧消息的邮箱,但是知道是哪个邮箱发的似乎也没啥用
|
||||
HAL_CAN_AddTxMessage(_instance->can_handle, &_instance->txconf, _instance->tx_buff, &_instance->tx_mailbox);
|
||||
}
|
||||
|
||||
void CANSetDLC(can_instance *_instance, uint8_t length)
|
||||
{
|
||||
if (length > 8)
|
||||
if (length > 8) // 安全检查
|
||||
while (1)
|
||||
;
|
||||
_instance->txconf.DLC = length;
|
||||
|
@ -112,13 +117,13 @@ static void CANFIFOxCallback(CAN_HandleTypeDef *_hcan, uint32_t fifox)
|
|||
HAL_CAN_GetRxMessage(_hcan, fifox, &rxconf, can_rx_buff);
|
||||
for (size_t i = 0; i < DEVICE_CAN_CNT; i++)
|
||||
{
|
||||
if (instance[i] != NULL)
|
||||
{
|
||||
if (instance[i] != NULL) // 碰到NULL说明已经遍历完所有实例
|
||||
{ // 两者相等说明这是要找的实例
|
||||
if (_hcan == instance[i]->can_handle && rxconf.StdId == instance[i]->rx_id)
|
||||
{
|
||||
instance[i]->rx_len=rxconf.DLC;
|
||||
memcpy(instance[i]->rx_buff, can_rx_buff, rxconf.DLC);
|
||||
instance[i]->can_module_callback(instance[i]);
|
||||
instance[i]->rx_len = rxconf.DLC;
|
||||
memcpy(instance[i]->rx_buff, can_rx_buff, rxconf.DLC); // 消息拷贝到对应实例
|
||||
instance[i]->can_module_callback(instance[i]); // 触发回调进行数据解析和处理
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -9,24 +9,24 @@
|
|||
#define MX_CAN_FILTER_CNT (2 * 14) // temporarily useless
|
||||
#define DEVICE_CAN_CNT 2 // CAN1,CAN2
|
||||
|
||||
|
||||
/* can instance typedef, every module registered to CAN should have this variable */
|
||||
#pragma pack(1)
|
||||
typedef struct _
|
||||
{
|
||||
CAN_HandleTypeDef *can_handle;
|
||||
CAN_TxHeaderTypeDef txconf;
|
||||
uint32_t tx_id;
|
||||
uint32_t tx_mailbox;
|
||||
uint8_t tx_buff[8];
|
||||
uint8_t rx_buff[8];
|
||||
uint32_t rx_id;
|
||||
uint8_t rx_len;
|
||||
CAN_HandleTypeDef *can_handle; // can句柄
|
||||
CAN_TxHeaderTypeDef txconf; // CAN报文发送配置
|
||||
uint32_t tx_id; // 发送id
|
||||
uint32_t tx_mailbox; // CAN消息填入的邮箱号
|
||||
uint8_t tx_buff[8]; // 发送缓存,最大为8
|
||||
uint8_t rx_buff[8]; // 接收缓存
|
||||
uint32_t rx_id; // 接收id
|
||||
uint8_t rx_len; // 接收长度,可能为0-8
|
||||
// 接收的回调函数,用于解析接收到的数据
|
||||
void (*can_module_callback)(struct _ *); // callback needs an instance to tell among registered ones
|
||||
} can_instance;
|
||||
#pragma pack()
|
||||
|
||||
/* this structure is used as initialization*/
|
||||
/* this structure is used for initialization */
|
||||
typedef struct
|
||||
{
|
||||
CAN_HandleTypeDef *can_handle;
|
||||
|
@ -35,24 +35,6 @@ typedef struct
|
|||
void (*can_module_callback)(can_instance *);
|
||||
} can_instance_config_s;
|
||||
|
||||
/* module callback,which resolve protocol when new mesg arrives */
|
||||
typedef void (*can_callback)(can_instance*);
|
||||
|
||||
/**
|
||||
* @brief transmit mesg through CAN device
|
||||
*
|
||||
* @param _instance can instance owned by module
|
||||
*/
|
||||
void CANTransmit(can_instance *_instance);
|
||||
|
||||
/**
|
||||
* @brief Register a module to CAN service,remember to call this before using a CAN device
|
||||
*
|
||||
* @param config init config
|
||||
* @return can_instance* can instance owned by module
|
||||
*/
|
||||
can_instance* CANRegister(can_instance_config_s *config);
|
||||
|
||||
/**
|
||||
* @brief 修改CAN发送报文的数据帧长度;注意最大长度为8,在没有进行修改的时候,默认长度为8
|
||||
*
|
||||
|
@ -61,4 +43,20 @@ can_instance* CANRegister(can_instance_config_s *config);
|
|||
*/
|
||||
void CANSetDLC(can_instance *_instance, uint8_t length);
|
||||
|
||||
/**
|
||||
* @brief transmit mesg through CAN device,通过can实例发送消息
|
||||
* 发送前需要向CAN实例的tx_buff写入发送数据
|
||||
*
|
||||
* @param _instance* can instance owned by module
|
||||
*/
|
||||
void CANTransmit(can_instance *_instance);
|
||||
|
||||
/**
|
||||
* @brief Register a module to CAN service,remember to call this before using a CAN device
|
||||
* 注册(初始化)一个can实例,需要传入初始化配置的指针.
|
||||
* @param config init config
|
||||
* @return can_instance* can instance owned by module
|
||||
*/
|
||||
can_instance *CANRegister(can_instance_config_s *config);
|
||||
|
||||
#endif
|
||||
|
|
|
@ -56,7 +56,7 @@ typedef void (*can_callback)(can_instance*);
|
|||
- `can_instance_config`是用于初始化CAN实例的结构,在调用CAN实例的初始化函数时传入(下面介绍函数时详细介绍)。
|
||||
|
||||
- `can_module_callback()`是模块提供给CAN接收中断回调函数使用的协议解析函数指针。对于每个需要CAN的模块,需要定义一个这样的函数用于解包数据。
|
||||
- 每个使用CAN外设的module,都需要在其内部定义一个`can_instance`。
|
||||
- 每个使用CAN外设的module,都需要在其内部定义一个`can_instance*`。
|
||||
|
||||
|
||||
## 外部接口
|
||||
|
|
|
@ -2,7 +2,9 @@
|
|||
#define _BSP_LOG_H
|
||||
|
||||
void BSP_Log_Init();
|
||||
|
||||
int printf_log(const char *fmt, ...);
|
||||
|
||||
void Float2Str(char *str, float va);
|
||||
|
||||
#endif
|
Loading…
Reference in New Issue