MCPcopy Create free account
hub / github.com/CopterExpress/clover / LedController

Class LedController

clover_simulation/src/sim_leds.cpp:34–102  ·  view source on GitHub ↗

LedController: a class that provides ROS interface for the LEDs.

Source from the content-addressed store, hash-verified

32
33/// LedController: a class that provides ROS interface for the LEDs.
34class LedController
35{
36private:
37 /// Role for the current controller
38 enum class Role
39 {
40 Client, // Client: runs on /gazebo_gui, responsible for "preview window"
41 Server // Server: runs on /gazebo, responsible for renders on Gazebo sensors
42 } role;
43
44 // Pointers to the LED plugins that we know about
45 std::vector<sim_led::LedVisualPlugin*> registeredLeds;
46 // Mutex protecting the vector
47 std::mutex registryMutex;
48 std::string robotNamespace;
49
50 std::unique_ptr<ros::NodeHandle> nh;
51
52 // LED state will be read from the topic to avoid creating more services
53 ros::Subscriber stateSubscriber;
54
55 void handleLedsMsg(const led_msgs::LEDStateArrayConstPtr& leds);
56
57public:
58 LedController(const std::string& robotNamespace) : robotNamespace(robotNamespace)
59 {
60 // We need "libgazebo_ros_api.so" to be loaded
61 if (!ros::isInitialized())
62 {
63 ROS_FATAL_NAMED("LedController", "Tried to load ROS plugin when ROS Gazebo API is not loaded. Please use gazebo_ros node to"
64 "launch Gazebo.");
65 }
66
67 role = (ros::this_node::getName() == "/gazebo") ? Role::Server : Role::Client;
68 ROS_INFO_NAMED(("LedController_" + robotNamespace).c_str(), "LedController has started (as %s) in namespace '%s'",
69 role == Role::Client ? "client" : "server", robotNamespace.c_str());
70
71 nh.reset(new ros::NodeHandle(robotNamespace));
72
73 stateSubscriber = nh->subscribe<led_msgs::LEDStateArray>("led/state", 1, &LedController::handleLedsMsg, this);
74 };
75
76 ~LedController()
77 {
78 nh->shutdown();
79 }
80
81 void registerPlugin(sim_led::LedVisualPlugin* plugin, int ledIdx = 0, int totalLeds = 0)
82 {
83 assert(ledIdx < totalLeds);
84 std::lock_guard<std::mutex> lock(registryMutex);
85 if (totalLeds > 0) {
86 registeredLeds.resize(totalLeds);
87 }
88 ROS_DEBUG_NAMED(("LedController_" + robotNamespace).c_str(), "Registering LED visual plugin to %s (LED id=%d)", (role == Role::Client) ? "client" : "server", ledIdx);
89 registeredLeds[ledIdx] = plugin;
90 }
91

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected