ocra-recipes
Doxygen documentation for the ocra-recipes repository
ControlThread.h
Go to the documentation of this file.
1 
9 /*
10  * This file is part of ocra-recipes.
11  * Copyright (C) 2016 Institut des Systèmes Intelligents et de Robotique (ISIR)
12  *
13  * This program is free software: you can redistribute it and/or modify
14  * it under the terms of the GNU General Public License as published by
15  * the Free Software Foundation, either version 3 of the License, or
16  * (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program. If not, see <http://www.gnu.org/licenses/>.
25 */
26 
27 #ifndef CONTROL_THREAD_H
28 #define CONTROL_THREAD_H
29 
30 #include <yarp/os/Network.h>
31 #include <yarp/os/RateThread.h>
32 #include <yarp/os/Port.h>
33 #include <yarp/os/RpcClient.h>
34 #include <yarp/os/Time.h>
35 
36 #include <Eigen/Dense>
37 #include <Eigen/Lgsm>
38 
39 #include <ocra/util/Macros.h>
40 
41 
42 
43 namespace ocra_recipes
44 {
46 {
48 
49 public:
50  double kp;
51  double kd;
52  int dimension;
53  Eigen::VectorXd weight;
54  Eigen::VectorXd desired;
55  Eigen::VectorXd currentState;
56  std::string type;
57  std::string name;
58  bool isActive;
59 
60  friend std::ostream& operator<<(std::ostream &out, const TaskParameters& params)
61  {
62  out << "kp = " << params.kp << std::endl;
63  out << "kd = " << params.kd << std::endl;
64  out << "dimension = " << params.dimension << std::endl;
65  out << "weight = " << params.weight.transpose() << std::endl;
66  out << "desired = " << params.desired.transpose() << std::endl;
67  out << "currentState = " << params.currentState.transpose() << std::endl;
68  out << "type = " << params.type << std::endl;
69  out << "name = " << params.name << std::endl;
70  out << "isActive = " << params.isActive << std::endl;
71  return out;
72  }
73 };
74 
75 
76 
77 class ControlThread: public yarp::os::RateThread
78 {
80 
81 public:
82  // Constructor
83  ControlThread(int period, const std::string& taskRpcPortName);
84  ~ControlThread();
85 
86  int threadId;
88 
89  // RateThread virtual functions
90  virtual bool threadInit();
91  virtual void threadRelease();
92  virtual void run();
93 
94  // ControlThread pure virtual functions
95  virtual bool ct_threadInit()=0;
96  virtual void ct_threadRelease()=0;
97  virtual void ct_run()=0;
98 
99  // ControlThread functions
100  std::string getThreadType(){return controlThreadType;}
101  bool deactivateTask();
102  bool activateTask();
103 
104  std::string getOutputPortName(){return outputPortName;}
105  std::string getInputPortName(){return inputPortName;}
106 
107 
108  /************** controlInputCallback *************/
109  class inputCallback : public yarp::os::PortReader {
110  private:
111  ControlThread& ctBase;
112 
113  public:
114  inputCallback(ControlThread& ctBaseRef);
115 
116  virtual bool read(yarp::os::ConnectionReader& connection);
117  };
118  /************** controlInputCallback *************/
119 
120 protected:
121 
122  void setThreadType(const std::string& _threadType = "ControlThread"){controlThreadType = _threadType;}
123 
124  std::string controlThreadType;
125 
126  // Yarp control ports
127  std::string inputPortName, outputPortName;
128  yarp::os::Port inputPort, outputPort;
129 
130  //Yarp RPC client
131  std::string taskRpcServerName, threadRpcClientName;
132  yarp::os::RpcClient threadRpcClient;
133 
134  //Yarp network
135  yarp::os::Network yarp;
136 
137  bool openControlPorts();
138  bool connectControlPorts();
139 
142  Eigen::VectorXd currentStateVector;
143  bool parseInput(yarp::os::Bottle* input);
144 
145  Eigen::VectorXd getCurrentState();
147  void sendGetStateMessage();
148 
150 
153 
156 
157  bool getTaskDimensions();
158  bool getTaskParameters(TaskParameters& TP);
159 
160 
162 };
163 } // namespace ocra_recipes
164 #endif //CONTROL_THREAD_H
Eigen::VectorXd currentStateVector
yarp::os::RpcClient threadRpcClient
#define DEFINE_CLASS_POINTER_TYPEDEFS(Class)
Definition: Macros.h:8
Eigen::VectorXd currentState
Definition: ControlThread.h:55
TaskParameters currentTaskParams
void setThreadType(const std::string &_threadType="ControlThread")
TaskParameters originalTaskParams
friend std::ostream & operator<<(std::ostream &out, const TaskParameters &params)
Definition: ControlThread.h:60