GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: include/sot/core/sot-loader.hh Lines: 9 9 100.0 %
Date: 2023-03-13 12:09:37 Branches: 1 2 50.0 %

Line Branch Exec Source
1
/*
2
 * Copyright 2016,
3
 * Olivier Stasse,
4
 *
5
 * CNRS
6
 *
7
 */
8
/* -------------------------------------------------------------------------- */
9
/* --- INCLUDES ------------------------------------------------------------- */
10
/* -------------------------------------------------------------------------- */
11
12
#ifndef _SOT_LOADER_HH_
13
#define _SOT_LOADER_HH_
14
15
// STL includes
16
#include <map>
17
18
// Dynamic Graph embeded python interpreter.
19
#include <dynamic-graph/python/interpreter.hh>
20
21
// Sot Framework includes
22
#include <sot/core/abstract-sot-external-interface.hh>
23
#include <sot/core/debug.hh>
24
#include <sot/core/device.hh>
25
26
namespace dynamicgraph {
27
namespace sot {
28
29
/**
30
 * @brief This class is loading the control part of the Stack-Of-Tasks.
31
 * - 1/ It loads dynamically the graph interface.
32
 * - 2/ It loads the python interpreter.
33
 * - 3/ It loads the Device entity C++ pointer inside the python interpreter.
34
 * - 4/ It provides the user interface to the graph:
35
 *    - 4.1/ starts and stop the graph executtion.
36
 *    - 4.2/ run a python command/script inside the embeded python interpreter.
37
 *    - 4.3/ execute one iteration of the graph.
38
 *
39
 * In order to Use this class you need to provide a dynamic library containing
40
 * an implementation of the AbstractSotExternalInterface class.
41
 *
42
 * Then you can either inherite from this class an initialize and use the
43
 * sensors_in_ and control_values_ objects.
44
 * Or you can create you own outside of this class.
45
 * And then use the oneIteration to execute the graph.
46
 */
47
class SotLoader {
48
 protected:
49
  /// \brief Check if the dynamic graph is running or not.
50
  bool dynamic_graph_stopped_;
51
52
  /// \brief The interface between the device and the robot driver.
53
  AbstractSotExternalInterface *sot_external_interface_;
54
55
  /// \brief Name of the dynamic library containing the
56
  /// dgs::AbstractSotExternalInterface object.
57
  std::string sot_dynamic_library_filename_;
58
59
  /// \brief Handle on the SoT library.
60
  void *sot_dynamic_library_;
61
62
  /// \brief Embeded python interpreter.
63
  python::Interpreter embeded_python_interpreter_;
64
65
  /// \brief Map of sensor readings
66
  std::map<std::string, SensorValues> sensors_in_;
67
68
  /// \brief Map of control values
69
  std::map<std::string, ControlValues> control_values_;
70
71
  /// \brief Device entity created and loaded, so we deregister it as the Pool
72
  /// is not responsible for it's life time.
73
  std::string device_name_;
74
75
 public:
76
  /// \brief Default constructor.
77
  SotLoader();
78
  /// \brief Default destructor.
79
  ~SotLoader();
80
81
  /// \brief Read user input to extract the path of the SoT dynamic library.
82
  int parseOptions(int argc, char *argv[]);
83
84
  /// \brief Prepare the SoT framework.
85
  bool initialization();
86
87
  /// \brief Unload the library which handles the robot device.
88
  void cleanUp();
89
90
  /// \brief Get Status of dg.
91
4
  inline bool isDynamicGraphStopped() { return dynamic_graph_stopped_; }
92
93
  /// \brief Get Status of dg.
94
2
  inline void startDG() { dynamic_graph_stopped_ = false; }
95
96
  /// \brief Get Status of dg.
97
1
  inline void stopDG() { dynamic_graph_stopped_ = true; }
98
99
  /// \brief Specify the name of the dynamic library.
100
10
  inline void setDynamicLibraryName(std::string &afilename) {
101
10
    sot_dynamic_library_filename_ = afilename;
102
10
  }
103
104
  /// \brief Run a python command inside the embeded python interpreter.
105
  void runPythonCommand(const std::string &command, std::string &result,
106
                        std::string &out, std::string &err);
107
108
  /// \brief Run a python script inside the embeded python interpreter.
109
2
  inline void runPythonFile(std::string ifilename, std::string &err) {
110
2
    embeded_python_interpreter_.runPythonFile(ifilename, err);
111
2
  }
112
113
  /// \brief Run a python script inside the embeded python interpreter.
114
  inline void runPythonFile(std::string ifilename) {
115
    embeded_python_interpreter_.runPythonFile(ifilename);
116
  }
117
118
  /// \brief Compute one iteration of control.
119
  /// Basically executes fillSensors, the SoT and the readControl.
120
  void oneIteration(std::map<std::string, SensorValues> &sensors_in,
121
                    std::map<std::string, ControlValues> &control_values);
122
123
  /// \brief Load the Device entity in the python global scope.
124
  void loadDeviceInPython(const std::string &device_name);
125
};
126
127
} /* namespace sot */
128
} /* namespace dynamicgraph */
129
130
#endif /* _SOT_LOADER_HH_ */