ASCO Aerial Autonomy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
base_robot_system.h
Go to the documentation of this file.
1 #pragma once
2 
4 // Store type_map
6 // Iterable Enum
8 // Boost thread stuff
9 #include <boost/thread/mutex.hpp>
10 // Unique ptr
11 #include <memory>
12 
17 
18 protected:
24 
25 private:
29  std::map<HardwareType, AbstractControllerHardwareConnector *>
30  active_controllers_;
35  std::map<HardwareType, std::unique_ptr<boost::mutex>> thread_mutexes_;
36 
37 public:
42  // Initialize active controller map
43  for (auto hardware_type : IterableEnum<HardwareType>()) {
44  active_controllers_[hardware_type] = nullptr;
45  thread_mutexes_[hardware_type] =
46  std::unique_ptr<boost::mutex>(new boost::mutex);
47  }
48  }
49 
58  template <class ControllerHardwareConnectorT, class GoalT>
59  void setGoal(GoalT goal) {
60  ControllerHardwareConnectorT *controller_hardware_connector =
62  .getObject<ControllerHardwareConnectorT>();
63  controller_hardware_connector->setGoal(goal);
64  HardwareType hardware_type =
65  controller_hardware_connector->getHardwareType();
66  if (active_controllers_[hardware_type] != controller_hardware_connector) {
67  boost::mutex::scoped_lock lock(*thread_mutexes_[hardware_type]);
68  active_controllers_[hardware_type] = controller_hardware_connector;
69  }
70  }
79  template <class ControllerHardwareConnectorT, class GoalT>
80  GoalT getGoal() const {
81  const ControllerHardwareConnectorT *controller_hardware_connector =
83  .getObject<ControllerHardwareConnectorT>();
84  return controller_hardware_connector->getGoal();
85  }
86 
94  template <class ControllerHardwareConnectorT>
96  const ControllerHardwareConnectorT *controller_hardware_connector =
98  .getObject<ControllerHardwareConnectorT>();
99  return controller_hardware_connector->getStatus();
100  }
101 
111  auto active_controller = active_controllers_.find(hardware_type);
112  if (active_controller != active_controllers_.end() &&
113  active_controller->second != nullptr) {
114  return active_controller->second->getStatus();
115  } else {
117  }
118  }
119 
126  void abortController(HardwareType hardware_type) {
127  boost::mutex::scoped_lock lock(*thread_mutexes_[hardware_type]);
128  active_controllers_[hardware_type] = nullptr;
129  }
130 
136  void runActiveController(HardwareType hardware_type) {
137  // lock to ensure active_control fcn is not changed
138  AbstractControllerHardwareConnector *active_controller =
139  active_controllers_[hardware_type];
140  if (active_controller != nullptr) {
141  boost::mutex::scoped_lock lock(*thread_mutexes_[hardware_type]);
142  active_controller->run();
143  }
144  }
145 
151  virtual std::string getSystemStatus() const = 0;
152 };
virtual std::string getSystemStatus() const =0
Provide the current state of robot system.
TypeMap< AbstractControllerHardwareConnector > controller_hardware_connector_container_
Container to store and retrieve controller-hardware-connectors.
Definition: base_robot_system.h:23
void runActiveController(HardwareType hardware_type)
Run active controller stored for a given hardware type.
Definition: base_robot_system.h:136
GoalT getGoal() const
Get the goal from connector.
Definition: base_robot_system.h:80
void abortController(HardwareType hardware_type)
Remove active controller for given hardware type.
Definition: base_robot_system.h:126
Status of the controller.
Definition: controller_status.h:10
Iterable wrapper class for iterating over enums. Only works for enums that are contiguous.
Definition: iterable_enum.h:8
ObjectT * getObject()
return the object stored
Definition: type_map.h:37
void setGoal(GoalT goal)
sets goal to the connector and swaps the active connector with the specified connector type...
Definition: base_robot_system.h:59
Base for ControllerHardwareConnector class.
Definition: base_controller_hardware_connector.h:21
Provides functions to switch between active controllers and get goals.
Definition: base_robot_system.h:16
This status is used when no controller is engaged.
Definition: controller_status.h:19
ControllerStatus getStatus() const
Get the status of a controller.
Definition: base_robot_system.h:95
BaseRobotSystem()
Constructor to initialize active controllers and mutexes to NULL.
Definition: base_robot_system.h:41
virtual void run()=0
Abstract run function.
HardwareType
Type of hardware used by ControllerHardwareConnector. Enum ID must be contiguous. ...
Definition: base_controller_hardware_connector.h:11
ControllerStatus getActiveControllerStatus(HardwareType hardware_type) const
Get the status of the active controller.
Definition: base_robot_system.h:110