You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
216 lines
5.4 KiB
216 lines
5.4 KiB
#include "dronedata.h"
|
|
#include <QRandomGenerator>
|
|
#include <QDebug>
|
|
#include <QtMath>
|
|
|
|
/**
|
|
* 无人机数据模型实现
|
|
* 管理单架无人机的状态信息和数据更新
|
|
*/
|
|
|
|
DroneData::DroneData(const QString& id, const QString& name, QObject* parent)
|
|
: QObject(parent)
|
|
, id_(id)
|
|
, name_(name)
|
|
, connected_(false)
|
|
, position_(0, 0, 0)
|
|
, expectedPosition_(0, 0, 0)
|
|
, velocity_(0, 0, 0)
|
|
, expectedVelocity_(0, 0, 0)
|
|
, attitude_(0, 0, 0)
|
|
, simulationTimer_(new QTimer(this))
|
|
, simulationStep_(0)
|
|
{
|
|
// 设置模拟数据更新定时器
|
|
simulationTimer_->setInterval(100); // 100ms更新一次
|
|
connect(simulationTimer_, &QTimer::timeout, this, &DroneData::updateSimulationData);
|
|
}
|
|
|
|
void DroneData::setConnected(bool connected) {
|
|
if (connected_ != connected) {
|
|
connected_ = connected;
|
|
emit connectionChanged(connected);
|
|
emit dataChanged();
|
|
}
|
|
}
|
|
|
|
void DroneData::setPosition(const QVector3D& pos) {
|
|
if (position_ != pos) {
|
|
position_ = pos;
|
|
emit dataChanged();
|
|
}
|
|
}
|
|
|
|
void DroneData::setExpectedPosition(const QVector3D& pos) {
|
|
if (expectedPosition_ != pos) {
|
|
expectedPosition_ = pos;
|
|
emit dataChanged();
|
|
}
|
|
}
|
|
|
|
void DroneData::setVelocity(const QVector3D& vel) {
|
|
if (velocity_ != vel) {
|
|
velocity_ = vel;
|
|
emit dataChanged();
|
|
}
|
|
}
|
|
|
|
void DroneData::setExpectedVelocity(const QVector3D& vel) {
|
|
if (expectedVelocity_ != vel) {
|
|
expectedVelocity_ = vel;
|
|
emit dataChanged();
|
|
}
|
|
}
|
|
|
|
void DroneData::setAttitude(const QVector3D& att) {
|
|
if (attitude_ != att) {
|
|
attitude_ = att;
|
|
emit dataChanged();
|
|
}
|
|
}
|
|
|
|
void DroneData::startSimulation() {
|
|
if (!simulationTimer_->isActive()) {
|
|
simulationTimer_->start();
|
|
qDebug() << "开始模拟无人机" << name_ << "的数据更新";
|
|
}
|
|
}
|
|
|
|
void DroneData::stopSimulation() {
|
|
if (simulationTimer_->isActive()) {
|
|
simulationTimer_->stop();
|
|
qDebug() << "停止模拟无人机" << name_ << "的数据更新";
|
|
}
|
|
}
|
|
|
|
void DroneData::updateSimulationData() {
|
|
if (!connected_) return;
|
|
|
|
simulationStep_++;
|
|
|
|
// 模拟位置变化(简单的正弦波运动)
|
|
float t = simulationStep_ * 0.01f;
|
|
float x = 10.0f * qSin(t) + 50.0f;
|
|
float y = 10.0f * qCos(t) + 50.0f;
|
|
float z = 5.0f * qSin(t * 0.5f) + 30.0f;
|
|
|
|
setPosition(QVector3D(x, y, z));
|
|
|
|
// 模拟速度变化
|
|
float vx = 10.0f * qCos(t);
|
|
float vy = -10.0f * qSin(t);
|
|
float vz = 5.0f * qCos(t * 0.5f) * 0.5f;
|
|
|
|
setVelocity(QVector3D(vx, vy, vz));
|
|
|
|
// 模拟姿态变化
|
|
float roll = 5.0f * qSin(t * 0.3f);
|
|
float pitch = 3.0f * qCos(t * 0.4f);
|
|
float yaw = t * 10.0f;
|
|
|
|
setAttitude(QVector3D(roll, pitch, yaw));
|
|
|
|
// 期望位置稍微滞后于实际位置
|
|
setExpectedPosition(QVector3D(x + 2.0f, y + 1.0f, z + 0.5f));
|
|
setExpectedVelocity(QVector3D(vx * 0.9f, vy * 0.9f, vz * 0.9f));
|
|
}
|
|
|
|
/**
|
|
* 无人机管理器实现
|
|
* 管理多架无人机的创建、删除和切换
|
|
*/
|
|
|
|
DroneManager::DroneManager(QObject* parent)
|
|
: QObject(parent)
|
|
, currentDroneId_("")
|
|
{
|
|
}
|
|
|
|
DroneManager::~DroneManager() {
|
|
qDeleteAll(drones_);
|
|
}
|
|
|
|
void DroneManager::addDrone(const QString& id, const QString& name) {
|
|
// 检查是否已存在
|
|
for (auto* drone : drones_) {
|
|
if (drone->getId() == id) {
|
|
qDebug() << "无人机" << id << "已存在";
|
|
return;
|
|
}
|
|
}
|
|
|
|
// 创建新无人机
|
|
auto* drone = new DroneData(id, name, this);
|
|
drones_.append(drone);
|
|
|
|
// 如果是第一架无人机,设为当前选中
|
|
if (currentDroneId_.isEmpty()) {
|
|
setCurrentDrone(id);
|
|
}
|
|
|
|
// 开始模拟数据更新
|
|
drone->setConnected(true);
|
|
drone->startSimulation();
|
|
|
|
emit droneAdded(id);
|
|
qDebug() << "添加无人机:" << id << name;
|
|
}
|
|
|
|
void DroneManager::removeDrone(const QString& id) {
|
|
for (int i = 0; i < drones_.size(); ++i) {
|
|
if (drones_[i]->getId() == id) {
|
|
auto* drone = drones_[i];
|
|
drone->stopSimulation();
|
|
drone->deleteLater();
|
|
drones_.removeAt(i);
|
|
|
|
// 如果删除的是当前选中的无人机,切换到其他无人机
|
|
if (currentDroneId_ == id) {
|
|
if (!drones_.isEmpty()) {
|
|
setCurrentDrone(drones_[0]->getId());
|
|
} else {
|
|
currentDroneId_ = "";
|
|
emit currentDroneChanged("");
|
|
}
|
|
}
|
|
|
|
emit droneRemoved(id);
|
|
qDebug() << "删除无人机:" << id;
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
|
|
DroneData* DroneManager::getDrone(const QString& id) const {
|
|
for (auto* drone : drones_) {
|
|
if (drone->getId() == id) {
|
|
return drone;
|
|
}
|
|
}
|
|
return nullptr;
|
|
}
|
|
|
|
DroneData* DroneManager::getCurrentDrone() const {
|
|
return getDrone(currentDroneId_);
|
|
}
|
|
|
|
QVector<DroneData*> DroneManager::getAllDrones() const {
|
|
return drones_;
|
|
}
|
|
|
|
void DroneManager::setCurrentDrone(const QString& id) {
|
|
if (currentDroneId_ != id && getDrone(id) != nullptr) {
|
|
currentDroneId_ = id;
|
|
emit currentDroneChanged(id);
|
|
qDebug() << "切换当前无人机到:" << id;
|
|
}
|
|
}
|
|
|
|
QStringList DroneManager::getDroneIds() const {
|
|
QStringList ids;
|
|
for (auto* drone : drones_) {
|
|
ids.append(drone->getId());
|
|
}
|
|
return ids;
|
|
}
|