Fawkes API  Fawkes Development Version
imu_plugin.cpp
1 
2 /***************************************************************************
3  * imu_plugin.cpp - Fawkes IMU Plugin
4  *
5  * Created: Sun Jun 22 21:41:38 2014
6  * Copyright 2006-2014 Tim Niemueller [www.niemueller.de]
7  ****************************************************************************/
8 
9 /* This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU Library General Public License for more details.
18  *
19  * Read the full text in the LICENSE.GPL file in the doc directory.
20  */
21 
22 #include <core/plugin.h>
23 
24 #include "sensor_thread.h"
25 #include "acquisition_thread.h"
26 #ifdef HAVE_CRUIZCORE
27 # include "imu_cruizcore_xg1010.h"
28 #endif
29 
30 #include <set>
31 #include <memory>
32 
33 using namespace fawkes;
34 
35 /** IMU driver plugin.
36  * @author Tim Niemueller
37  */
38 class IMUPlugin : public fawkes::Plugin
39 {
40  public:
41  /** Constructor.
42  * @param config Fawkes configuration
43  */
45  : Plugin(config)
46  {
47  std::set<std::string> configs;
48  std::set<std::string> ignored_configs;
49 
50  std::string prefix = "/hardware/imu/";
51 
52 #if __cplusplus >= 201103L
53  std::unique_ptr<Configuration::ValueIterator> i(config->search(prefix.c_str()));
54 #else
55  std::auto_ptr<Configuration::ValueIterator> i(config->search(prefix.c_str()));
56 #endif
57  while (i->next()) {
58  std::string cfg_name = std::string(i->path()).substr(prefix.length());
59  cfg_name = cfg_name.substr(0, cfg_name.find("/"));
60 
61  if ( (configs.find(cfg_name) == configs.end()) &&
62  (ignored_configs.find(cfg_name) == ignored_configs.end()) ) {
63 
64  std::string cfg_prefix = prefix + cfg_name + "/";
65 
66  bool active = true;
67  try {
68  active = config->get_bool((cfg_prefix + "active").c_str());
69  } catch (Exception &e) {} // ignored, assume enabled
70 
71  try {
72  if (active) {
73  std::string type = config->get_string((cfg_prefix + "type").c_str());
74  bool continuous = false;
75  try {
76  continuous = config->get_bool((cfg_prefix + "continuous").c_str());
77  } catch (Exception &e) {} // ignored, use default
78 
79  //printf("Adding IMU acquisition thread for %s\n", cfg_name.c_str());
80  IMUAcquisitionThread *aqt = NULL;
81 #ifdef HAVE_CRUIZCORE
82  if ( type == "CruizCore-XG1010" ) {
83  aqt = new CruizCoreXG1010AcquisitionThread(cfg_name, cfg_prefix, continuous);
84  } else
85 #endif
86 
87  {
88  throw Exception("Unknown IMU type '%s' for config %s",
89  type.c_str(), cfg_name.c_str());
90  }
91 
92  thread_list.push_back(aqt);
93  if (! continuous) {
94  thread_list.push_back(new IMUSensorThread(cfg_name, cfg_prefix, aqt));
95  }
96 
97  configs.insert(cfg_name);
98  } else {
99  //printf("Ignoring IMU config %s\n", cfg_name.c_str());
100  ignored_configs.insert(cfg_name);
101  }
102  } catch(Exception &e) {
103  for (ThreadList::iterator i = thread_list.begin();
104  i != thread_list.end(); ++i) {
105  delete *i;
106  }
107  throw;
108  }
109  }
110  }
111 
112  if ( thread_list.empty() ) {
113  throw Exception("No IMU devices configured, aborting");
114  }
115  }
116 };
117 
118 PLUGIN_DESCRIPTION("Driver for inertial measurement units (IMU)")
119 EXPORT_PLUGIN(IMUPlugin)
Plugin interface class.
Definition: plugin.h:33
Fawkes library namespace.
virtual bool get_bool(const char *path)=0
Get value from configuration which is of type bool.
virtual ValueIterator * search(const char *path)=0
Iterator with search results.
IMU acqusition thread.
virtual bool next()=0
Check if there is another element and advance to this if possible.
Base class for exceptions in Fawkes.
Definition: exception.h:36
IMUPlugin(Configuration *config)
Constructor.
Definition: imu_plugin.cpp:44
virtual const char * path() const =0
Path of value.
IMU acquisition thread for CruizCore XG1010 gyros.
Interface for configuration handling.
Definition: config.h:67
IMU driver plugin.
Definition: imu_plugin.cpp:38
virtual std::string get_string(const char *path)=0
Get value from configuration which is of type string.
IMU sensor thread.
Definition: sensor_thread.h:40