文件定位
位置:
src/ExoData.h
作用:定义 ExoData 类,作为 Exo 系统全局数据的核心容器。它将设备的所有关键状态、配置、各侧关节/传感器/控制器等,组织成统一接口供主控和其他模块访问。
背景备注:头文件注释里说,把数据类和主控制类分开,是为了支持多板协同(如主控和BLE/GUI通讯分开)。
源代码
/**
* @file Exo.h
*
* @brief Declares exo class that all the other components will live in.
*
* @author P. Stegall
* @date Jan. 2022
*/
#ifndef Exo_h
#define Exo_h
//Arduino compiles everything in the src folder even if not included so it causes an error for the nano if this is not included
#if defined(ARDUINO_TEENSY36) || defined(ARDUINO_TEENSY41)
#include "Arduino.h"
#include "Side.h"
#include <stdint.h>
#include "ParseIni.h"
#include "Board.h"
#include "Utilities.h"
#include "SyncLed.h"
#include "StatusLed.h"
#include "StatusDefs.h"
#include "Config.h"
class Exo
{
public:
Exo(ExoData* exo_data); //Constructor: uses initializer list for the Side objects.
/**
* @brief Reads motor data from each motor used on that side and stores the values
*
* @return true if the code ran, ie tiiming was satisfied
* @return false
*/
bool run();
ExoData *data; /**< Pointer to ExoData that is getting updated by the coms mcu so they share format.*/
Side left_side; /**< Left side object that contains all the joints and sensors for that side */
Side right_side; /**< Right side object that contains all the joints and sensors for that side */
#ifdef USE_SPEED_CHECK
utils::SpeedCheck speed_check; /**< Used to check the speed of the loop without needing prints */
#endif
SyncLed sync_led; /**< Used to syncronize data with a motion capture system */
StatusLed status_led; /**< Used to display the system status */
private:
};
#endif
#endif
1. 头文件依赖
#include "Arduino.h"
#include "SideData.h"
#include <stdint.h>
#include "ParseIni.h"
#include "Board.h"
#include "StatusLed.h"
#include "StatusDefs.h"
SideData.h:定义了每一侧(左/右)的数据结构。
ParseIni/Board/Status…:涉及配置解析、硬件定义和状态管理。
2. 核心类声明
class ExoData
{
public:
ExoData(uint8_t* config_to_send); // 构造函数,传入配置数组
void reconfigure(uint8_t* config_to_send); // 重新配置
- 构造时传入配置数组,
reconfigure
可动态重加载参数。
3. 主要成员函数
关节遍历工具
// 遍历所有关节并执行回调
template <typename F>
void for_each_joint(F &&func)
{
func(&left_side.hip, NULL);
func(&left_side.knee, NULL);
func(&left_side.ankle, NULL);
func(&left_side.elbow, NULL);
func(&right_side.hip, NULL);
func(&right_side.knee, NULL);
func(&right_side.ankle, NULL);
func(&right_side.elbow, NULL);
}
// 支持额外参数
template <typename F>
void for_each_joint(F &&func, float* args) { ... }
- 统一遍历左右所有关节(髋、膝、踝、肘),批量操作非常方便。
关节与配置相关
uint8_t get_used_joints(uint8_t* used_joints); // 获取正在用的关节ID列表
JointData* get_joint_with(uint8_t id); // 根据关节id返回指针
- 方便上层通过ID访问各关节。
数据/状态操作
void print(); // 打印所有数据
void set_status(uint16_t status_to_set); // 设置状态
uint16_t get_status(void); // 获取状态
控制参数/校准
void set_default_parameters(); void set_default_parameters(uint8_t id); void start_pretrial_cal(); // 启动实验前校准
4. 主要成员变量
bool sync_led_state; // 同步LED状态
bool estop; // 急停状态
float battery_value; // 电池电压或电量
SideData left_side; // 左侧数据
SideData right_side; // 右侧数据
uint32_t mark; // 计时用
uint8_t* config; // 配置参数数组
uint8_t config_len; // 配置长度
int error_code; // 错误码
int error_joint_id; // 错误关节编号
bool user_paused; // 用户暂停标志
int hip_torque_flag = 0; // 使用力矩传感器标志(髋)
int knee_torque_flag = 0;
int ankle_torque_flag = 0;
int elbow_torque_flag = 0;
private: uint16_t _status; // 当前状态
- SideData 下又分为各个关节(hip、knee、ankle、elbow),每个关节下有 motor/controller/sensor 等。
总结
ExoData 是整个外骨骼数据的“总线”,聚合了所有核心变量和状态。
上层(如 Exo、通信模块、控制/校准流程)通过 ExoData 读写系统数据。
提供遍历/查找/配置/打印/校准/状态管理等接口,极大方便模块间解耦和灵活拓展。
结构清晰、面向对象,便于后续添加关节、传感器或新功能。