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

Class LedControllerPlugin

clover_simulation/src/sim_leds_controller.cpp:10–69  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

8#include <gazebo/common/common.hh>
9
10class LedControllerPlugin : public gazebo::ModelPlugin {
11private:
12 std::unique_ptr<ros::NodeHandle> nh;
13 std::string ns;
14 ros::ServiceServer setLedsSrv;
15 led_msgs::LEDStateArray ledState;
16 ros::Publisher statePublisher;
17 std::mutex handleMutex;
18
19public:
20 bool setLeds(led_msgs::SetLEDs::Request &req, led_msgs::SetLEDs::Response &resp)
21 {
22 std::lock_guard<std::mutex> lock(handleMutex);
23 for(const auto& led : req.leds)
24 {
25 if (led.index < ledState.leds.size()) {
26 ledState.leds[led.index].r = led.r;
27 ledState.leds[led.index].g = led.g;
28 ledState.leds[led.index].b = led.b;
29 }
30 }
31 statePublisher.publish(ledState);
32 resp.success = true;
33 return true;
34 }
35
36 virtual void Load(gazebo::physics::ModelPtr model, sdf::ElementPtr sdf) override
37 {
38 ROS_INFO("Initialize LED Controller");
39
40 // We need "libgazebo_ros_api.so" to be loaded
41 if (!ros::isInitialized())
42 {
43 ROS_FATAL_NAMED("LedController", "Tried to load ROS plugin when ROS Gazebo API is not loaded. Please use gazebo_ros node to"
44 "launch Gazebo.");
45 }
46
47 ns = "";
48
49 if (sdf->HasElement("robotNamespace")) {
50 ns = sdf->Get<std::string>("robotNamespace");
51 }
52 if (!sdf->HasElement("ledCount")) {
53 gzerr << "ledCount is not set, but is required for the plugin to function correctly\n";
54 return;
55 }
56 int totalLeds = sdf->Get<int>("ledCount");
57 ledState.leds.resize(totalLeds);
58 for (int i = 0; i < totalLeds; i++) {
59 ledState.leds[i].index = i;
60 }
61
62 nh.reset(new ros::NodeHandle(ns));
63
64 setLedsSrv = nh->advertiseService("led/set_leds", &LedControllerPlugin::setLeds, this);
65 statePublisher = nh->advertise<led_msgs::LEDStateArray>("led/state", 1, true);
66
67 statePublisher.publish(ledState);

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected