-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
74 lines (63 loc) · 2.04 KB
/
Copy pathmain.cpp
File metadata and controls
74 lines (63 loc) · 2.04 KB
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// Runs a simulation to test the PID controller and pendulum system.
#include "Systems/Pendulum.hpp"
#include "Controllers/PID.hpp"
#include <iostream>
#include <string>
#include <vector>
static const double dt{0.01};
static const double g{9.81};
int main()
{
// Define pendulum params
const double mass{1.0};
const double length{1.0};
const double damping{0.1};
const double initial_angle{2.0};
const double initial_angular_velocity{0.0};
// Initialize the pendulum
Pendulum pendulum = Pendulum(mass, length, g, damping, initial_angle, initial_angular_velocity);
// Define controller params
const double p_gain{0.0};
const double i_gain{0.0};
const double d_gain{0.0};
// Initialize the controller
PID pid = PID(p_gain, i_gain, d_gain, dt);
// Define the simulation parameters
const double simulation_time{10.0};
const int num_steps{static_cast<int>(simulation_time / dt)};
// Initialize the simulation
double cmd{0.0};
double measurement{0.0};
double output{0.0};
std::vector<double> time;
std::vector<double> angle;
std::vector<double> angular_velocity;
std::vector<double> command;
std::vector<double> output_vector;
for (int i{0}; i < num_steps; ++i)
{
// Step the controller
output = pid.step(cmd, measurement);
// Step the system
pendulum.step(output, dt);
// Save the data
std::map<std::string, double> state = pendulum.getState();
time.push_back(i * dt);
angle.push_back(state["Angle"]);
angular_velocity.push_back(state["Angular Velocity"]);
command.push_back(cmd);
output_vector.push_back(output);
}
// Plot the results
// plt::figure();
// plt::subplot(2, 1, 1);
// plt::plot(time, angle);
// plt::plot(time, command);
// plt::title("Angle");
// plt::subplot(2, 1, 2);
// plt::plot(time, angular_velocity);
// plt::plot(time, output_vector);
// plt::title("Angular Velocity");
// plt::show();
return 0;
}