上传文件至 include/dispatch
This commit is contained in:
121
include/dispatch/agv_manager_standalone.h
Normal file
121
include/dispatch/agv_manager_standalone.h
Normal file
@@ -0,0 +1,121 @@
|
||||
#ifndef AGV_MANAGER_STANDALONE_H
|
||||
#define AGV_MANAGER_STANDALONE_H
|
||||
|
||||
#include "graph_map.h"
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <unordered_map>
|
||||
#include <memory>
|
||||
#include <mutex>
|
||||
#include <atomic>
|
||||
#include <queue>
|
||||
#include <chrono>
|
||||
#include <functional>
|
||||
|
||||
/**
|
||||
* @brief AGV状态枚举
|
||||
*/
|
||||
enum class AGVState {
|
||||
IDLE, // 空闲
|
||||
ASSIGNED, // 已分配任务
|
||||
PLANNING, // 路径规划中
|
||||
EXECUTING, // 执行任务中
|
||||
WAITING, // 等待资源
|
||||
ERROR // 错误状态
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief 任务结构
|
||||
*/
|
||||
struct Task {
|
||||
int id;
|
||||
int priority;
|
||||
int start_point_id;
|
||||
int end_point_id;
|
||||
std::string description;
|
||||
|
||||
Task(int task_id, int prio, int start, int end, const std::string& desc)
|
||||
: id(task_id), priority(prio), start_point_id(start), end_point_id(end), description(desc) {}
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief AGV小车结构
|
||||
*/
|
||||
struct AGV {
|
||||
int id;
|
||||
std::string name;
|
||||
AGVState state;
|
||||
Point* current_position;
|
||||
double battery_level; // 电量百分比
|
||||
double max_velocity; // 最大速度
|
||||
Task* current_task; // 当前任务
|
||||
std::vector<Path*> planned_path; // 规划的路径
|
||||
size_t path_index; // 当前路径索引
|
||||
|
||||
AGV(int agv_id, const std::string& agv_name, int start_point_id, Point* pos)
|
||||
: id(agv_id), name(agv_name), state(AGVState::IDLE), current_position(pos),
|
||||
battery_level(100.0), max_velocity(2.0), current_task(nullptr), path_index(0) {}
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief AGV管理器
|
||||
*/
|
||||
class AGVManager {
|
||||
private:
|
||||
GraphMap* graph_map_;
|
||||
std::unordered_map<int, std::unique_ptr<AGV>> agvs_;
|
||||
std::priority_queue<Task*, std::vector<Task*>, std::function<bool(Task*, Task*)>> task_queue_;
|
||||
mutable std::mutex agv_mutex_;
|
||||
mutable std::mutex task_mutex_;
|
||||
std::atomic<int> next_task_id_{1};
|
||||
|
||||
public:
|
||||
explicit AGVManager(GraphMap* map);
|
||||
~AGVManager() = default;
|
||||
|
||||
// AGV管理
|
||||
AGV* addAGV(int id, const std::string& name, int start_point_id);
|
||||
AGV* getAGV(int id);
|
||||
std::vector<AGV*> getAllAGVs();
|
||||
std::vector<AGV*> getIdleAGVs();
|
||||
|
||||
// 任务管理
|
||||
int addTask(int priority, int start_point_id, int end_point_id, const std::string& description = "");
|
||||
bool assignTask(AGV* agv, Task* task);
|
||||
int assignTasks();
|
||||
void completeTask(AGV* agv, bool success = true);
|
||||
|
||||
// 路径规划
|
||||
bool planPath(AGV* agv, Task* task);
|
||||
|
||||
// 状态查询
|
||||
size_t getAGVCount() const;
|
||||
size_t getTaskQueueSize() const;
|
||||
std::vector<Task*> getPendingTasks();
|
||||
|
||||
// 事件回调
|
||||
void setTaskCompleteCallback(std::function<void(const Task&, int)> callback) {
|
||||
task_complete_callback_ = callback;
|
||||
}
|
||||
|
||||
// 统计信息
|
||||
struct Statistics {
|
||||
int total_agvs;
|
||||
int idle_agvs;
|
||||
int busy_agvs;
|
||||
int pending_tasks;
|
||||
double average_battery_level;
|
||||
};
|
||||
|
||||
Statistics getStatistics() const;
|
||||
|
||||
private:
|
||||
std::function<void(const Task&, int)> task_complete_callback_;
|
||||
|
||||
void initializeTaskQueue();
|
||||
bool isValidTask(const Task* task) const;
|
||||
bool canAssignTask(const AGV* agv, const Task* task) const;
|
||||
double calculateDistance(AGV* agv, const Task* task) const;
|
||||
};
|
||||
|
||||
#endif // AGV_MANAGER_STANDALONE_H
|
||||
Reference in New Issue
Block a user