GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: include/sot/torque_control/control-manager.hh Lines: 5 9 55.6 %
Date: 2023-06-05 17:45:50 Branches: 8 16 50.0 %

Line Branch Exec Source
1
/*
2
 * Copyright 2015, Andrea Del Prete, LAAS-CNRS
3
 *
4
 */
5
6
#ifndef __sot_torque_control_control_manager_H__
7
#define __sot_torque_control_control_manager_H__
8
9
/* --------------------------------------------------------------------- */
10
/* --- API ------------------------------------------------------------- */
11
/* --------------------------------------------------------------------- */
12
13
#if defined(WIN32)
14
#if defined(__sot_torque_control_control_manager_H__)
15
#define SOTCONTROLMANAGER_EXPORT __declspec(dllexport)
16
#else
17
#define SOTCONTROLMANAGER_EXPORT __declspec(dllimport)
18
#endif
19
#else
20
#define SOTCONTROLMANAGER_EXPORT
21
#endif
22
23
/* --------------------------------------------------------------------- */
24
/* --- INCLUDE --------------------------------------------------------- */
25
/* --------------------------------------------------------------------- */
26
27
#include <pinocchio/fwd.hpp>
28
29
// include pinocchio first
30
31
#include <dynamic-graph/signal-helper.h>
32
33
#include <map>
34
#include <pinocchio/multibody/model.hpp>
35
#include <pinocchio/parsers/urdf.hpp>
36
#include <sot/core/matrix-geometry.hh>
37
#include <sot/core/robot-utils.hh>
38
#include <sot/torque_control/utils/vector-conversions.hh>
39
#include <tsid/robots/robot-wrapper.hpp>
40
41
#include "boost/assign.hpp"
42
43
namespace dynamicgraph {
44
namespace sot {
45
namespace torque_control {
46
47
/* --------------------------------------------------------------------- */
48
/* --- CLASS ----------------------------------------------------------- */
49
/* --------------------------------------------------------------------- */
50
51
/// Number of time step to transition from one ctrl mode to another
52
#define CTRL_MODE_TRANSITION_TIME_STEP 1000.0
53
54
class CtrlMode {
55
 public:
56
  int id;
57
  std::string name;
58
59
58
  CtrlMode() : id(-1), name("None") {}
60
  CtrlMode(int id, const std::string& name) : id(id), name(name) {}
61
};
62
63
std::ostream& operator<<(std::ostream& os, const CtrlMode& s) {
64
  os << s.id << "_" << s.name;
65
  return os;
66
}
67
68
class SOTCONTROLMANAGER_EXPORT ControlManager : public ::dynamicgraph::Entity {
69
  typedef ControlManager EntityClassName;
70
8
  DYNAMIC_GRAPH_ENTITY_DECL();
71
72
 public:
73
  /* --- CONSTRUCTOR ---- */
74
  ControlManager(const std::string& name);
75
76
  /// Initialize
77
  /// @param dt: control interval
78
  /// @param urdfFile: path to the URDF model of the robot
79
  void init(const double& dt, const std::string& urdfFile,
80
            const std::string& robotRef);
81
82
  /* --- SIGNALS --- */
83
  std::vector<dynamicgraph::SignalPtr<dynamicgraph::Vector, int>*>
84
      m_ctrlInputsSIN;
85
  std::vector<dynamicgraph::SignalPtr<bool, int>*>
86
      m_emergencyStopSIN;  /// emergency stop inputs. If one is true, control is
87
                           /// set to zero forever
88
  std::vector<dynamicgraph::Signal<dynamicgraph::Vector, int>*>
89
      m_jointsCtrlModesSOUT;
90
91
  DECLARE_SIGNAL_IN(i_measured, dynamicgraph::Vector);  /// motor currents
92
  DECLARE_SIGNAL_IN(
93
      tau, dynamicgraph::Vector);  /// estimated joint torques (using dynamic
94
                                   /// robot model + F/T sensors)
95
  DECLARE_SIGNAL_IN(
96
      tau_predicted,
97
      dynamicgraph::Vector);  /// predicted joint torques (using motor model)
98
  DECLARE_SIGNAL_IN(
99
      i_max, dynamicgraph::Vector);  /// max current allowed before stopping the
100
                                     /// controller (in Ampers)
101
  DECLARE_SIGNAL_IN(
102
      u_max,
103
      dynamicgraph::Vector);  /// max desired current allowed before stopping
104
                              /// the controller (in Ampers)
105
  DECLARE_SIGNAL_IN(tau_max,
106
                    dynamicgraph::Vector);  /// max torque allowed before
107
                                            /// stopping the controller
108
109
  DECLARE_SIGNAL_OUT(u, dynamicgraph::Vector);
110
  DECLARE_SIGNAL_OUT(
111
      u_safe,
112
      dynamicgraph::Vector);  /// same as u when everything is fine, 0 otherwise
113
114
  /* --- COMMANDS --- */
115
116
  /// Commands related to the control mode.
117
  void addCtrlMode(const std::string& name);
118
  void ctrlModes();
119
  void getCtrlMode(const std::string& jointName);
120
  void setCtrlMode(const std::string& jointName, const std::string& ctrlMode);
121
  void setCtrlMode(const int jid, const CtrlMode& cm);
122
123
  void resetProfiler();
124
125
  /// Commands related to joint name and joint id
126
  void setNameToId(const std::string& jointName, const double& jointId);
127
  void setJointLimitsFromId(const double& jointId, const double& lq,
128
                            const double& uq);
129
130
  /// Command related to ForceUtil
131
  void setForceLimitsFromId(const double& jointId,
132
                            const dynamicgraph::Vector& lq,
133
                            const dynamicgraph::Vector& uq);
134
  void setForceNameToForceId(const std::string& forceName,
135
                             const double& forceId);
136
137
  /// Commands related to FootUtil
138
  void setRightFootSoleXYZ(const dynamicgraph::Vector&);
139
  void setRightFootForceSensorXYZ(const dynamicgraph::Vector&);
140
  void setFootFrameName(const std::string&, const std::string&);
141
  void setImuJointName(const std::string&);
142
  void displayRobotUtil();
143
144
  /// Commands related to HandUtil
145
  void setHandFrameName(const std::string&, const std::string&);
146
147
  /// Set the mapping between urdf and sot.
148
  void setJoints(const dynamicgraph::Vector&);
149
150
  void setStreamPrintPeriod(const double& s);
151
  void setSleepTime(const double& seconds);
152
  void addEmergencyStopSIN(const std::string& name);
153
154
  /* --- ENTITY INHERITANCE --- */
155
  virtual void display(std::ostream& os) const;
156
157
2
  void sendMsg(const std::string& msg, MsgType t = MSG_TYPE_INFO,
158
               const char* = "", int = 0) {
159



2
    logger_.stream(t) << ("[ControlManager-" + name + "] " + msg) << '\n';
160
2
  }
161
162
 protected:
163
  RobotUtilShrPtr m_robot_util;
164
  tsid::robots::RobotWrapper* m_robot;
165
  bool
166
      m_initSucceeded;  /// true if the entity has been successfully initialized
167
  double m_dt;          /// control loop time period
168
  bool m_emergency_stop_triggered;  /// true if an emergency condition as been
169
                                    /// triggered either by an other entity, or
170
                                    /// by control limit violation
171
  bool m_is_first_iter;  /// true at the first iteration, false otherwise
172
  int m_iter;
173
  double m_sleep_time;  /// time to sleep at every iteration (to slow down
174
                        /// simulation)
175
176
  std::vector<std::string> m_ctrlModes;  /// existing control modes
177
  std::vector<CtrlMode>
178
      m_jointCtrlModes_current;  /// control mode of the joints
179
  std::vector<CtrlMode>
180
      m_jointCtrlModes_previous;  /// previous control mode of the joints
181
  std::vector<int>
182
      m_jointCtrlModesCountDown;  /// counters used for the transition between
183
                                  /// two ctrl modes
184
185
  bool convertStringToCtrlMode(const std::string& name, CtrlMode& cm);
186
  bool convertJointNameToJointId(const std::string& name, unsigned int& id);
187
  bool isJointInRange(unsigned int id, double q);
188
  void updateJointCtrlModesOutputSignal();
189
190
};  // class ControlManager
191
192
}  // namespace torque_control
193
}  // namespace sot
194
}  // namespace dynamicgraph
195
196
#endif  // #ifndef __sot_torque_control_control_manager_H__