MIRA
Pilot.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) by
3  * MetraLabs GmbH (MLAB), GERMANY
4  * and
5  * Neuroinformatics and Cognitive Robotics Labs (NICR) at TU Ilmenau, GERMANY
6  * All rights reserved.
7  *
8  * Redistribution and modification of this code is strictly prohibited.
9  *
10  * IN NO EVENT SHALL "MLAB" OR "NICR" BE LIABLE TO ANY PARTY FOR DIRECT,
11  * INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF
12  * THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF "MLAB" OR
13  * "NICR" HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14  *
15  * "MLAB" AND "NICR" SPECIFICALLY DISCLAIM ANY WARRANTIES, INCLUDING,
16  * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
17  * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
18  * ON AN "AS IS" BASIS, AND "MLAB" AND "NICR" HAVE NO OBLIGATION TO
19  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS OR MODIFICATIONS.
20  */
21 
30 #ifndef _MIRA_PILOT_PILOT_H_
31 #define _MIRA_PILOT_PILOT_H_
32 
33 #include <serialization/adapters/boost/shared_ptr.hpp>
34 #include <serialization/adapters/std/vector>
36 
37 #include <fw/Unit.h>
38 #include <fw/ServiceProperty.h>
39 #include <robot/RobotModel.h>
40 #include <robot/Odometry.h>
41 #include <model/IRigidModelProvider.h>
42 
43 #include <pilot/AbstractPlanner.h>
45 #include <navigation/Task.h>
47 #include <navigation/INavigation.h>
48 
49 #include <model/Marker.h>
50 
51 namespace mira { namespace pilot {
52 
54 
55 class Pilot : public Unit, public navigation::INavigation, public model::IRigidModelConsumer
56 {
57 MIRA_OBJECT(Pilot);
58 public:
59 
60  Pilot() :
61  Unit(Duration::milliseconds(250))
62  {
63  mMute = false;
64  mModelProvider = ServiceProperty("IRobotModelProvider");
65  }
66 
67 public:
68 
69  template<typename Reflector>
70  void reflectServices(Reflector& r)
71  {
72  r.property("DriveService", mDriveService,
73  setter(&Pilot::setDriveService, this),
74  "The name of the service that is used for setting the velocity "
75  "(if empty, a suitable service is chosen automatically)",
76  ServiceProperty("IDrive"));
77  }
78 
80  {
81  try {
82  r.property("DriveService", mDriveService,
83  setter(&Pilot::setDriveService, this),
84  "The name of the service that is used for setting the velocity "
85  "(if empty, a suitable service is chosen automatically)",
86  ServiceProperty("IDrive"));
87  } catch (const XMemberNotFound_NoDefault&) {
88  // workaround for old configs where DriveService was a string property
89  MIRA_LOG(WARNING) << "Pilot failed reading property DriveService as ServiceProperty. "
90  "Fallback: trying as string.";
91  std::string service;
92  r.property("DriveService", service, "", "");
93  MIRA_LOG(WARNING) << "Expected ServiceProperty for Pilot's DriveService property, found string '"
94  << service << "'. Please update your config!";
95  ServiceProperty p("IDrive");
96  p = service;
97  setDriveService(p);
98  }
99  }
100 
101  template<typename Reflector>
102  void reflect(Reflector& r)
103  {
105  MIRA_REFLECT_BASE(r, IRigidModelConsumer);
106 
107  reflectServices(r);
108 
109  r.property("CPUUsage", mCPUUsage,
110  "The cpu usage in percent (default: 90%)",
111  90.0f, PropertyHints::limits(0.0f, 100.0f));
112  r.property("Mute", mMute,
113  "If enabled, then the robot does not send drive commands",false);
114 
115  r.property("UseRecovery", mUseRecovery,
116  "Are recovery strategies enabled", false);
117 
118  r.roproperty("Reached Tasks",
119  getter<bool>(&Pilot::reachedTasks, this),
120  "True, if all tasks have been reached");
121 
122  r.property("Planner", mPlanner,
123  "The motion planner that is used by the navigator");
124 
125  r.property("RecoveryStrategies", mRecoveryStrategies, "The recovery strategies",
126  std::map<std::string, navigation::TaskPtr>());
127 
129 
130  r.method("setMute", &Pilot::setMute, this, "(Un-)Mute the pilot.",
131  "mute", "true/false to mute/unmute", true);
132 
134  task->addSubTask(navigation::SubTaskPtr(
135  new navigation::OrientationTask(deg2rad(180.f), deg2rad(5.f), "/maps/MapFrame")));
136 
137  r.method("setRecoverTask", &Pilot::setRecoverTask, this,
138  "Manually trigger recovery. Only valid if UseRecovery=true.",
139  "task", "The task used for recovery.", task);
140  }
141 
142  boost::shared_ptr<robot::RobotModel> getRobotModel();
143 
144 public:
145 
146  virtual void setGoal(const Pose2& goal, float transT, float rotT);
147  virtual void setTask(navigation::TaskPtr task);
148  virtual void repeatTask() {
149  setTask(mLastTask);
150  }
151 
152  void setMute(bool mute) {
153  mMute = mute;
154  }
155 
156  bool reachedTasks() const {
157  return mPlanner ? mPlanner->reachedTasks() : true;
158  }
159 
161  return mCurrentOdometry;
162  }
163 
164  void fireEvent(const std::string& event) {
165  mEventChannel.post(event);
166  }
167 
168  boost::shared_ptr<AbstractPlanner> getPlanner(){
169  return mPlanner;
170  }
171 
172  bool getUseRecovery() const {
173  return mUseRecovery;
174  }
175 
176 protected:
177 
178  virtual void initialize();
179 
180  virtual void process(const Timer& timer);
181 
182 private:
183 
185  void setRecoverTask(navigation::TaskPtr task);
186 
187 private:
188 
189  Velocity2 getAcceleration(const Velocity2& velocity);
190  void setVelocity(const Velocity2& v);
191  void taskError(const std::string& msg, const std::string& errorEvent="UnknownError");
192  void taskInterruption(const std::string& msg,
193  const std::string& interruptionEvent="UnknownInterruption");
194 
195  void setDriveService(ServiceProperty service);
196 
198  virtual void setModelProviderInternal(ServiceProperty service);
199  virtual Authority* getAuthority() { return this; }
200 
201 private:
202 
203  bool mMute;
204  float mCPUUsage;
205 
206  ServiceProperty mDriveService;
207 
208  boost::shared_ptr<robot::RobotModel> mRobotModel;
209  boost::mutex mModelMutex;
210 
211  boost::shared_ptr<AbstractPlanner> mPlanner;
212 
213  navigation::TaskPtr mTask;
214  navigation::TaskPtr mRecoveryTask;
215  navigation::TaskPtr mLastTask;
216 
217  Channel<std::string> mEventChannel;
218  Channel<robot::Odometry2> mOdometryChannel;
219  Channel<Velocity2> mVelocityChannel;
220  Channel<Velocity2> mAccelerationChannel;
221  Channel<navigation::TaskPtr> mTaskChannel;
222  Stamped<robot::Odometry2> mCurrentOdometry;
223 
224  bool mUseRecovery;
225  typedef std::map<std::string, navigation::TaskPtr> RecoveryStrategies;
226  RecoveryStrategies mRecoveryStrategies;
227 };
228 
230 
231 }}
232 
233 #endif
virtual void process(const Timer &timer)
const Stamped< robot::Odometry2 > & getCurrentOdometry() const
Definition: Pilot.h:160
bool getUseRecovery() const
Definition: Pilot.h:172
INTERNAL std::enable_if< std::is_floating_point< T >::value, T >::type deg2rad(T value)
void post(const Stamped< T > &value)
void reflectServices(Reflector &r)
Definition: Pilot.h:70
virtual void initialize()
void reflectServices(XMLDeserializer &r)
Definition: Pilot.h:79
Definition: Pilot.h:55
void setMute(bool mute)
Definition: Pilot.h:152
#define MIRA_LOG(level)
#define MIRA_REFLECT_BASE(reflector, BaseClass)
void reflect(Reflector &r)
Definition: Pilot.h:102
boost::shared_ptr< Task > TaskPtr
boost::shared_ptr< robot::RobotModel > getRobotModel()
Setter< T > setter(void(*f)(const T &))
Description.
void property(const char *name, T &member, const char *comment, PropertyHint &&hint=PropertyHint(), ReflectCtrlFlags flags=REFLECT_CTRLFLAG_NONE)
TODO Add description.
Pilot()
Definition: Pilot.h:60
PropertyHint limits(const T &min, const T &max)
virtual void repeatTask()
Definition: Pilot.h:148
virtual void setGoal(const Pose2 &goal, float transT, float rotT)
void fireEvent(const std::string &event)
Definition: Pilot.h:164
virtual void setTask(navigation::TaskPtr task)
bool reachedTasks() const
Definition: Pilot.h:156
boost::shared_ptr< AbstractPlanner > getPlanner()
Definition: Pilot.h:168
boost::shared_ptr< SubTask > SubTaskPtr