- 3.0.2 optimal control module.
plotResultsSwitched.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 
9  const ct::core::ControlVectorArray<1>& controlArray,
10  const ct::core::TimeArray& timeArray)
11 {
12 #ifdef PLOTTING_ENABLED
13  using namespace ct::core;
14 
15  try
16  {
17  plot::ion();
18  plot::figure();
19 
20  if (timeArray.size() != stateArray.size())
21  {
22  std::cout << timeArray.size() << std::endl;
23  std::cout << stateArray.size() << std::endl;
24  throw std::runtime_error("Cannot plot data, x and t not equal length");
25  }
26 
27  std::vector<std::vector<double>> states;
28  std::vector<double> time_state;
29  std::vector<double> constraint;
30  for (size_t k = 0; k < 2; k++)
31  {
32  states.push_back(std::vector<double>());
33  }
34 
35  for (size_t j = 0; j < stateArray.size(); j++)
36  {
37  for (size_t k = 0; k < 2; k++)
38  {
39  states[k].push_back(stateArray[j](k));
40  }
41  time_state.push_back(timeArray[j]);
42  constraint.push_back(stateArray[j](0) + stateArray[j](1));
43  }
44 
45  std::vector<double> control;
46  std::vector<double> time_control;
47  for (size_t j = 0; j < controlArray.size(); j++)
48  {
49  control.push_back(controlArray[j](0));
50  time_control.push_back(timeArray[j]);
51  }
52 
53  for (size_t k = 0; k < 2; k++)
54  {
55  plot::subplot(2, 1, k + 1);
56  plot::plot(time_state, states[k]);
57  plot::title("x(" + std::to_string(k) + ")");
58  }
59 
60  plot::figure();
61  plot::plot(time_state, constraint);
62  plot::title("Constraint x(0) + x(1)");
63 
64 
65  plot::figure();
66  plot::plot(time_control, control);
67  plot::title("Control");
68 
69  plot::show();
70  } catch (const std::exception& e)
71  {
72  std::cout << e.what() << std::endl;
73  }
74 #else
75  std::cout << "Plotting is disabled." << std::endl;
76 #endif
77 }
void plotResults(const ct::core::StateVectorArray< 2 > &stateArray, const ct::core::ControlVectorArray< 1 > &controlArray, const ct::core::TimeArray &timeArray)
Definition: plotResultsSwitched.h:8
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="")