Skip to content

Commit

Permalink
Create navigation_and_control_system.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
KOSASIH authored Jul 30, 2024
1 parent 3454722 commit 1cde8a0
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions core/aerospace/navigation_and_control_system.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#include <iostream>
#include <vector>
#include <cmath>

class NavigationAndControlSystem {
public:
NavigationAndControlSystem(Vehicle* vehicle, const std::vector<Waypoint>& waypoints)
: vehicle_(vehicle), waypoints_(waypoints) {}

void navigate() {
// Calculate current position and velocity
Vector3 currentPosition = vehicle_->getPosition();
Vector3 currentVelocity = vehicle_->getVelocity();

// Calculate desired position and velocity
Vector3 desiredPosition = waypoints_[currentWaypointIndex_].position;
Vector3 desiredVelocity = waypoints_[currentWaypointIndex_].velocity;

// Calculate navigation error
Vector3 navigationError = desiredPosition - currentPosition;

// Calculate control inputs
Vector3 controlInputs = calculateControlInputs(navigationError, currentVelocity, desiredVelocity);

// Apply control inputs
vehicle_->applyControlInputs(controlInputs);
}

private:
Vehicle* vehicle_;
std::vector<Waypoint> waypoints_;
int currentWaypointIndex_;

Vector3 calculateControlInputs(const Vector3& navigationError, const Vector3& currentVelocity, const Vector3& desiredVelocity) {
// Calculate PID gains
double kp = 1.0;
double ki = 0.1;
double kd = 0.5;

// Calculate control inputs
Vector3 controlInputs;
controlInputs.x = kp * navigationError.x + ki * navigationError.x * dt + kd * (desiredVelocity.x - currentVelocity.x);
controlInputs.y = kp * navigationError.y + ki * navigationError.y * dt + kd * (desiredVelocity.y - currentVelocity.y);
controlInputs.z = kp * navigationError.z + ki * navigationError.z * dt + kd * (desiredVelocity.z - currentVelocity.z);

return controlInputs;
}
};

0 comments on commit 1cde8a0

Please sign in to comment.