Fawkes API  Fawkes Development Version
dynamixel_plugin.cpp
1 
2 /***************************************************************************
3  * dynamixel_plugin.cpp - Robotis Servo driver plugin
4  *
5  * Created: Mon Mar 23 20:12:10 2015
6  * Copyright 2011-2015 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 "driver_thread.h"
25 #include "sensor_thread.h"
26 #include "act_thread.h"
27 
28 #include <set>
29 
30 using namespace fawkes;
31 
32 /** Driver plugin for Robotis dynamixel servos.
33  * @author Tim Niemueller
34  */
36 {
37  public:
38  /** Constructor.
39  * @param config Fawkes configuration
40  */
42  : Plugin(config)
43  {
44  DynamixelSensorThread *sensor_thread = new DynamixelSensorThread();
45  DynamixelActThread *act_thread = new DynamixelActThread();
46 
47 
48  std::set<std::string> configs;
49  std::set<std::string> ignored_configs;
50 
51  std::string prefix = "/dynamixel/";
52 
53  // Read configurations and spawn LaserFilterThreads
54 #if __cplusplus >= 201103L
55  std::unique_ptr<Configuration::ValueIterator> i(config->search(prefix.c_str()));
56 #else
57  std::auto_ptr<Configuration::ValueIterator> i(config->search(prefix.c_str()));
58 #endif
59  while (i->next()) {
60  std::string cfg_name = std::string(i->path()).substr(prefix.length());
61  cfg_name = cfg_name.substr(0, cfg_name.find("/"));
62 
63  if ( (configs.find(cfg_name) == configs.end()) &&
64  (ignored_configs.find(cfg_name) == ignored_configs.end()) ) {
65 
66  std::string cfg_prefix = prefix + cfg_name + "/";
67 
68  bool active = true;
69  try {
70  active = config->get_bool((cfg_prefix + "active").c_str());
71  } catch (Exception &e) {} // ignored, assume enabled
72 
73  try {
74  if (active) {
75  DynamixelDriverThread *drv_thread = new DynamixelDriverThread(cfg_name, cfg_prefix);
76  act_thread->add_driver_thread(drv_thread);
77  sensor_thread->add_driver_thread(drv_thread);
78  thread_list.push_back(drv_thread);
79  configs.insert(cfg_name);
80  } else {
81  //printf("Ignoring dynamixel config %s\n", cfg_name.c_str());
82  ignored_configs.insert(cfg_name);
83  }
84  } catch(Exception &e) {
85  for (ThreadList::iterator i = thread_list.begin();
86  i != thread_list.end(); ++i) {
87  delete *i;
88  }
89  delete act_thread;
90  delete sensor_thread;
91  throw;
92  }
93  }
94  }
95 
96  if ( thread_list.empty() ) {
97  delete act_thread;
98  delete sensor_thread;
99  throw Exception("No active servo configs, aborting");
100  }
101 
102  thread_list.push_back(sensor_thread);
103  thread_list.push_back(act_thread);
104  }
105 };
106 
107 PLUGIN_DESCRIPTION("Robotis Dynamixel servo driver plugin")
108 EXPORT_PLUGIN(DynamixelPlugin)
Plugin interface class.
Definition: plugin.h:33
Driver thread for Robotis dynamixel servos.
Definition: driver_thread.h:53
Fawkes library namespace.
virtual bool get_bool(const char *path)=0
Get value from configuration which is of type bool.
void add_driver_thread(DynamixelDriverThread *drv_thread)
Add a driver thread to wake in ACT hook.
Definition: act_thread.cpp:55
void add_driver_thread(DynamixelDriverThread *drv_thread)
Add a driver thread to wake in SENSOR_ACQUIRE hook.
virtual ValueIterator * search(const char *path)=0
Iterator with search results.
virtual bool next()=0
Check if there is another element and advance to this if possible.
Robotis dynamixel servo sensor thread.
Definition: sensor_thread.h:34
Base class for exceptions in Fawkes.
Definition: exception.h:36
Driver plugin for Robotis dynamixel servos.
Robotis dynamixel servo act thread.
Definition: act_thread.h:32
virtual const char * path() const =0
Path of value.
DynamixelPlugin(Configuration *config)
Constructor.
Interface for configuration handling.
Definition: config.h:67