blob: 08af55a7ad07037988af8acfa36b2ac582dc603a [file] [log] [blame]
Austin Schuh7400da02018-01-28 19:54:58 -08001/**********************************************************************************************************************
2This file is part of the Control Toolbox (https://adrlab.bitbucket.io/ct), copyright by ETH Zurich, Google Inc.
3Authors: Michael Neunert, Markus Giftthaler, Markus Stäuble, Diego Pardo, Farbod Farshidian
4Licensed under Apache2 license (see LICENSE file in main directory)
5**********************************************************************************************************************/
6
7#pragma once
8
9template <size_t STATE_DIM, size_t CONTROL_DIM>
10void plotResultsOscillator(const ct::core::StateVectorArray<STATE_DIM>& stateArray,
11 const ct::core::ControlVectorArray<CONTROL_DIM>& controlArray,
12 const ct::core::TimeArray& timeArray)
13{
14#ifdef PLOTTING_ENABLED
15
16 using namespace ct::core;
17
18 try
19 {
20 plot::ion();
21 plot::figure();
22
23 if (timeArray.size() != stateArray.size())
24 {
25 std::cout << timeArray.size() << std::endl;
26 std::cout << stateArray.size() << std::endl;
27 throw std::runtime_error("Cannot plot data, x and t not equal length");
28 }
29
30 std::vector<double> position;
31 std::vector<double> velocity;
32 std::vector<double> time_state;
33 for (size_t j = 0; j < stateArray.size(); j++)
34 {
35 position.push_back(stateArray[j](0));
36 velocity.push_back(stateArray[j](1));
37 time_state.push_back(timeArray[j]);
38 }
39
40 std::vector<double> control;
41 std::vector<double> time_control;
42 for (size_t j = 0; j < controlArray.size(); j++)
43 {
44 control.push_back(controlArray[j](0));
45 time_control.push_back(timeArray[j]);
46 }
47
48 plot::subplot(3, 1, 1);
49 plot::plot(time_state, position);
50 plot::title("position");
51
52 plot::subplot(3, 1, 2);
53 plot::plot(time_state, velocity);
54 plot::title("velocity");
55
56 plot::subplot(3, 1, 3);
57 plot::plot(time_control, control);
58 plot::title("control");
59
60 plot::show();
61 } catch (const std::exception& e)
62 {
63 std::cout << e.what() << std::endl;
64 }
65#else
66 std::cout << "Plotting is disabled." << std::endl;
67#endif
68}