- 3.0.2 optimal control module.
plotResultsOscillator.h
Go to the documentation of this file.
1 /**********************************************************************************************************************
2 This file is part of the Control Toolbox (https://github.com/ethz-adrl/control-toolbox), copyright by ETH Zurich.
3 Licensed under the BSD-2 license (see LICENSE file in main directory)
4 **********************************************************************************************************************/
5 
6 #pragma once
7 
8 template <size_t STATE_DIM, size_t CONTROL_DIM>
11  const ct::core::TimeArray& timeArray)
12 {
13 #ifdef PLOTTING_ENABLED
14 
15  using namespace ct::core;
16 
17  try
18  {
19  plot::ion();
20  plot::figure();
21 
22  if (timeArray.size() != stateArray.size())
23  {
24  std::cout << timeArray.size() << std::endl;
25  std::cout << stateArray.size() << std::endl;
26  throw std::runtime_error("Cannot plot data, x and t not equal length");
27  }
28 
29  std::vector<double> position;
30  std::vector<double> velocity;
31  std::vector<double> time_state;
32  for (size_t j = 0; j < stateArray.size(); j++)
33  {
34  position.push_back(stateArray[j](0));
35  velocity.push_back(stateArray[j](1));
36  time_state.push_back(timeArray[j]);
37  }
38 
39  std::vector<double> control;
40  std::vector<double> time_control;
41  for (size_t j = 0; j < controlArray.size(); j++)
42  {
43  control.push_back(controlArray[j](0));
44  time_control.push_back(timeArray[j]);
45  }
46 
47  plot::subplot(3, 1, 1);
48  plot::plot(time_state, position);
49  plot::title("position");
50 
51  plot::subplot(3, 1, 2);
52  plot::plot(time_state, velocity);
53  plot::title("velocity");
54 
55  plot::subplot(3, 1, 3);
56  plot::plot(time_control, control);
57  plot::title("control");
58 
59  plot::show();
60  } catch (const std::exception& e)
61  {
62  std::cout << e.what() << std::endl;
63  }
64 #else
65  std::cout << "Plotting is disabled." << std::endl;
66 #endif
67 }
void plotResultsOscillator(const ct::core::StateVectorArray< STATE_DIM > &stateArray, const ct::core::ControlVectorArray< CONTROL_DIM > &controlArray, const ct::core::TimeArray &timeArray)
Definition: plotResultsOscillator.h:9
bool plot(const Eigen::Ref< const Eigen::MatrixXd > &x, const Eigen::Ref< const Eigen::MatrixXd > &y, const std::map< std::string, std::string > &keywords)
void show(bool block=true)
void title(const std::string &titlestr)
bool subplot(const size_t nrows, const size_t ncols, const size_t plot_number)
bool figure(std::string i="")