Refactor configuration management by introducing CustomConfig class. Updated config.hpp to streamline settings and removed legacy configuration presets. Adjusted controller and custom_robot classes to utilize the new configuration structure. Enhanced error handling and logging during initialization processes.
This commit is contained in:
@@ -3,205 +3,101 @@
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
|
||||
namespace customConfig {
|
||||
namespace custom {
|
||||
|
||||
// Configuration template selector
|
||||
enum class ConfigPreset {
|
||||
Default,
|
||||
HighPerformance,
|
||||
Development,
|
||||
Safety
|
||||
};
|
||||
|
||||
// Motion gait enum
|
||||
/**
|
||||
* @brief Robot motion gait types
|
||||
*/
|
||||
enum class Gait : int {
|
||||
IDLE = 0,
|
||||
TROT = 1,
|
||||
TROT_RUNNING = 2
|
||||
IDLE = 0, // Standing still
|
||||
TROT = 1, // Normal walking
|
||||
TROT_RUNNING = 2 // Fast running
|
||||
};
|
||||
|
||||
// Network configuration
|
||||
|
||||
// Network settings
|
||||
constexpr std::string_view NETWORK_INTERFACE = "eth0";
|
||||
|
||||
// MQTT configuration
|
||||
// MQTT settings
|
||||
constexpr std::string_view MQTT_BROKER = "localhost";
|
||||
constexpr int MQTT_PORT = 1883;
|
||||
constexpr std::string_view MQTT_CLIENT_ID = "unitree_go2_client";
|
||||
constexpr std::string_view MQTT_USERNAME = "";
|
||||
constexpr std::string_view MQTT_PASSWORD = "";
|
||||
constexpr std::string_view MQTT_CLIENT_ID = "unitree_go2_client";
|
||||
|
||||
// Topic configuration
|
||||
// Topic settings
|
||||
constexpr std::string_view TOPIC_PREFIX = "unitree/go2";
|
||||
constexpr std::string_view TOPIC_CMD = "cmd";
|
||||
constexpr std::string_view TOPIC_STATE = "state";
|
||||
constexpr std::string_view TOPIC_VIDEO = "video";
|
||||
constexpr std::string_view TOPIC_AUDIO = "audio";
|
||||
|
||||
// Robot control configuration
|
||||
constexpr double CONTROL_FREQUENCY = 200.0; // Hz
|
||||
constexpr double STATE_PUBLISH_FREQUENCY = 50.0; // Hz
|
||||
// Robot control settings
|
||||
constexpr double CONTROL_FREQUENCY = 200.0; // Hz
|
||||
constexpr double STATE_PUBLISH_FREQUENCY = 50.0; // Hz
|
||||
|
||||
// Safety configuration
|
||||
constexpr double MAX_LINEAR_VELOCITY = 1.5; // m/s
|
||||
constexpr double MAX_ANGULAR_VELOCITY = 2.0; // rad/s
|
||||
constexpr double EMERGENCY_STOP_TIMEOUT = 5.0; // seconds
|
||||
// Safety settings
|
||||
constexpr double MAX_LINEAR_VELOCITY = 1.5; // m/s
|
||||
constexpr double MAX_ANGULAR_VELOCITY = 2.0; // rad/s
|
||||
constexpr double EMERGENCY_STOP_TIMEOUT = 5.0; // seconds
|
||||
|
||||
// Motion configuration
|
||||
constexpr double STAND_HEIGHT = 0.0; // relative height
|
||||
// Motion settings
|
||||
constexpr double STAND_HEIGHT = 0.0; // relative height
|
||||
constexpr Gait DEFAULT_GAIT = Gait::IDLE;
|
||||
|
||||
// Video configuration
|
||||
// Video settings
|
||||
constexpr int VIDEO_WIDTH = 1280;
|
||||
constexpr int VIDEO_HEIGHT = 720;
|
||||
constexpr int VIDEO_FPS = 30;
|
||||
constexpr std::string_view VIDEO_FORMAT = "h264";
|
||||
constexpr bool VIDEO_ENABLED = true;
|
||||
|
||||
// Audio configuration
|
||||
// Audio settings
|
||||
constexpr int AUDIO_SAMPLE_RATE = 16000;
|
||||
constexpr int AUDIO_CHANNELS = 1;
|
||||
constexpr std::string_view AUDIO_FORMAT = "pcm";
|
||||
constexpr bool AUDIO_ENABLED = true;
|
||||
|
||||
// Configuration presets using template specialization
|
||||
template<ConfigPreset P>
|
||||
struct ConfigParams;
|
||||
class CustomConfig {
|
||||
public:
|
||||
// 网络和MQTT连接
|
||||
std::string network_interface;
|
||||
std::string mqtt_broker;
|
||||
int mqtt_port;
|
||||
std::string mqtt_username;
|
||||
std::string mqtt_password;
|
||||
std::string mqtt_client_id;
|
||||
|
||||
// MQTT话题
|
||||
std::string topic_prefix;
|
||||
std::string topic_cmd;
|
||||
std::string topic_state;
|
||||
std::string topic_video;
|
||||
std::string topic_audio;
|
||||
|
||||
// 机器人控制和安全
|
||||
double control_frequency;
|
||||
double state_publish_frequency;
|
||||
double max_linear_velocity;
|
||||
double max_angular_velocity;
|
||||
double emergency_stop_timeout;
|
||||
double stand_height;
|
||||
int default_gait;
|
||||
|
||||
// 多媒体
|
||||
bool enable_video;
|
||||
bool enable_audio;
|
||||
int video_width;
|
||||
int video_height;
|
||||
int video_fps;
|
||||
std::string video_format;
|
||||
int audio_sample_rate;
|
||||
int audio_channels;
|
||||
std::string audio_format;
|
||||
|
||||
void loadDefaults();
|
||||
|
||||
// Default preset
|
||||
template<>
|
||||
struct ConfigParams<ConfigPreset::Default> {
|
||||
static constexpr double control_frequency = CONTROL_FREQUENCY;
|
||||
static constexpr double state_publish_frequency = STATE_PUBLISH_FREQUENCY;
|
||||
static constexpr double max_linear_velocity = MAX_LINEAR_VELOCITY;
|
||||
static constexpr double max_angular_velocity = MAX_ANGULAR_VELOCITY;
|
||||
static constexpr double emergency_stop_timeout = EMERGENCY_STOP_TIMEOUT;
|
||||
static constexpr bool video_enabled = VIDEO_ENABLED;
|
||||
static constexpr bool audio_enabled = AUDIO_ENABLED;
|
||||
};
|
||||
|
||||
// High performance preset
|
||||
template<>
|
||||
struct ConfigParams<ConfigPreset::HighPerformance> {
|
||||
static constexpr double control_frequency = 500.0;
|
||||
static constexpr double state_publish_frequency = 100.0;
|
||||
static constexpr double max_linear_velocity = 2.5;
|
||||
static constexpr double max_angular_velocity = 3.0;
|
||||
static constexpr double emergency_stop_timeout = EMERGENCY_STOP_TIMEOUT;
|
||||
static constexpr bool video_enabled = false; // Disabled for performance
|
||||
static constexpr bool audio_enabled = false; // Disabled for performance
|
||||
};
|
||||
|
||||
// Development preset
|
||||
template<>
|
||||
struct ConfigParams<ConfigPreset::Development> {
|
||||
static constexpr double control_frequency = 100.0;
|
||||
static constexpr double state_publish_frequency = 20.0;
|
||||
static constexpr double max_linear_velocity = 0.8;
|
||||
static constexpr double max_angular_velocity = 1.0;
|
||||
static constexpr double emergency_stop_timeout = EMERGENCY_STOP_TIMEOUT;
|
||||
static constexpr bool video_enabled = true;
|
||||
static constexpr bool audio_enabled = true;
|
||||
};
|
||||
|
||||
// Safety preset
|
||||
template<>
|
||||
struct ConfigParams<ConfigPreset::Safety> {
|
||||
static constexpr double control_frequency = 50.0;
|
||||
static constexpr double state_publish_frequency = 10.0;
|
||||
static constexpr double max_linear_velocity = 0.5;
|
||||
static constexpr double max_angular_velocity = 0.5;
|
||||
static constexpr double emergency_stop_timeout = 2.0;
|
||||
static constexpr bool video_enabled = true;
|
||||
static constexpr bool audio_enabled = true;
|
||||
};
|
||||
|
||||
// Compile-time configuration selection (set this to choose preset)
|
||||
constexpr ConfigPreset ACTIVE_PRESET = ConfigPreset::Default;
|
||||
|
||||
// Compile-time configuration struct
|
||||
template<ConfigPreset P = ACTIVE_PRESET>
|
||||
struct CompileTimeConfig {
|
||||
// Use selected preset parameters
|
||||
using params = ConfigParams<P>;
|
||||
|
||||
// Network settings
|
||||
static constexpr std::string_view network_interface = NETWORK_INTERFACE;
|
||||
|
||||
// MQTT settings
|
||||
static constexpr std::string_view mqtt_broker = MQTT_BROKER;
|
||||
static constexpr int mqtt_port = MQTT_PORT;
|
||||
static constexpr std::string_view mqtt_username = MQTT_USERNAME;
|
||||
static constexpr std::string_view mqtt_password = MQTT_PASSWORD;
|
||||
static constexpr std::string_view mqtt_client_id = MQTT_CLIENT_ID;
|
||||
|
||||
// Topics
|
||||
static constexpr std::string_view topic_prefix = TOPIC_PREFIX;
|
||||
static constexpr std::string_view cmd_topic = TOPIC_CMD;
|
||||
static constexpr std::string_view state_topic = TOPIC_STATE;
|
||||
static constexpr std::string_view video_topic = TOPIC_VIDEO;
|
||||
static constexpr std::string_view audio_topic = TOPIC_AUDIO;
|
||||
|
||||
// Robot settings (from preset)
|
||||
static constexpr double control_frequency = params::control_frequency;
|
||||
static constexpr double state_publish_frequency = params::state_publish_frequency;
|
||||
static constexpr bool enable_video = params::video_enabled;
|
||||
static constexpr bool enable_audio = params::audio_enabled;
|
||||
|
||||
// Safety settings (from preset)
|
||||
static constexpr double max_linear_velocity = params::max_linear_velocity;
|
||||
static constexpr double max_angular_velocity = params::max_angular_velocity;
|
||||
static constexpr double emergency_stop_timeout = params::emergency_stop_timeout;
|
||||
|
||||
// Motion settings
|
||||
static constexpr double stand_height = STAND_HEIGHT;
|
||||
static constexpr Gait default_gait = DEFAULT_GAIT;
|
||||
|
||||
// Video settings
|
||||
static constexpr int video_width = VIDEO_WIDTH;
|
||||
static constexpr int video_height = VIDEO_HEIGHT;
|
||||
static constexpr int video_fps = VIDEO_FPS;
|
||||
static constexpr std::string_view video_format = VIDEO_FORMAT;
|
||||
|
||||
// Audio settings
|
||||
static constexpr int audio_sample_rate = AUDIO_SAMPLE_RATE;
|
||||
static constexpr int audio_channels = AUDIO_CHANNELS;
|
||||
static constexpr std::string_view audio_format = AUDIO_FORMAT;
|
||||
|
||||
// Utility functions
|
||||
static std::string getFullTopic(std::string_view topic) {
|
||||
return std::string(topic_prefix) + "/" + std::string(topic);
|
||||
}
|
||||
};
|
||||
|
||||
// Type alias for current configuration
|
||||
using RobotConfig = CompileTimeConfig<ACTIVE_PRESET>;
|
||||
|
||||
// Configuration validation utilities
|
||||
template<ConfigPreset P>
|
||||
constexpr bool isConfigValid() {
|
||||
using config = ConfigParams<P>;
|
||||
return config::control_frequency > 0.0 &&
|
||||
config::state_publish_frequency > 0.0 &&
|
||||
config::max_linear_velocity > 0.0 &&
|
||||
config::max_angular_velocity > 0.0 &&
|
||||
config::emergency_stop_timeout > 0.0;
|
||||
}
|
||||
|
||||
// Compile-time validation
|
||||
static_assert(isConfigValid<ACTIVE_PRESET>(), "Invalid configuration parameters");
|
||||
|
||||
// Configuration preset name utilities
|
||||
template<ConfigPreset P>
|
||||
constexpr const char* getPresetName() {
|
||||
if constexpr (P == ConfigPreset::Default) return "Default";
|
||||
else if constexpr (P == ConfigPreset::HighPerformance) return "HighPerformance";
|
||||
else if constexpr (P == ConfigPreset::Development) return "Development";
|
||||
else if constexpr (P == ConfigPreset::Safety) return "Safety";
|
||||
else return "Unknown";
|
||||
}
|
||||
|
||||
constexpr const char* getActivePresetName() {
|
||||
return getPresetName<ACTIVE_PRESET>();
|
||||
}
|
||||
|
||||
} // namespace customConfig
|
||||
} // namespace custom
|
||||
|
||||
Reference in New Issue
Block a user