1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
#include <iostream> #include <cmath> // Structure to represent the drone's state struct DroneState { double x; // Position in the x-axis double y; // Position in the y-axis double vx; // Velocity in the x-axis double vy; // Velocity in the y-axis double ax; // Acceleration in the x-axis double ay; // Acceleration in the y-axis }; // Function to update the drone's state based on acceleration and time step void updateDroneState(DroneState& state, double dt) { // Update velocity based on acceleration state.vx += state.ax * dt; state.vy += state.ay * dt; // Update position based on velocity state.x += state.vx * dt; state.y += state.vy * dt; } int main() { // Initial conditions DroneState drone = {0.0, 0.0, 0.0, 0.0, 0.0, 0.0}; double dt = 0.1; // Time step (seconds) int steps = 100; // Number of simulation steps // Simulate the drone flight for (int i = 0; i < steps; ++i) { // Example acceleration input: hovering with slight forward acceleration if (i < 20) { drone.ax = 0.0; drone.ay = 0.1; // Small upward acceleration } else if (i < 50) { drone.ax = 0.1; // Forward acceleration drone.ay = 0.0; } else { drone.ax = 0.0; drone.ay = 0.0; // No acceleration (coasting) } // Update drone state updateDroneState(drone, dt); // Print the current state of the drone std::cout << "Time: " << i * dt << " s, " << "Position: (" << drone.x << ", " << drone.y << "), " << "Velocity: (" << drone.vx << ", " << drone.vy << ")\n"; } return 0; } |
Explanation
- DroneState Structure:
- This structure holds the state of the drone, including its position (
x
,y
), velocity (vx
,vy
), and acceleration (ax
,ay
) in a 2D space.
- This structure holds the state of the drone, including its position (
- updateDroneState Function:
- This function updates the drone’s velocity and position based on the current acceleration and the time step (
dt
). - The velocity is updated using the equation v=v0+a×dtv = v_0 + a \times dt, and the position is updated using s=s0+v×dts = s_0 + v \times dt.
- This function updates the drone’s velocity and position based on the current acceleration and the time step (
- Main Function:
- The drone’s initial state is set with all values (position, velocity, acceleration) at zero.
- The program simulates the drone flight for 100 time steps (
steps = 100
), with each step representing 0.1 seconds (dt = 0.1
). - The program applies different accelerations to simulate hovering, forward motion, and coasting, then prints the drone’s position and velocity at each time step.
Usage
- Initial Conditions: The drone starts at the origin (0,0) with no initial velocity or acceleration.
- Simulation Steps: The simulation runs for 100 steps, with the drone receiving different acceleration inputs to simulate different flight maneuvers.
- Output: The program outputs the drone’s position and velocity at each time step, allowing you to observe the drone’s motion over time.