fadecandy_driver_ros.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2021 Eurotec, Netherlands
3  * All rights reserved.
4  *
5  * Author: Jad Haj Mustafa
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above
14  * copyright notice, this list of conditions and the following
15  * disclaimer in the documentation and/or other materials provided
16  * with the distribution.
17  * 3. Neither the name of the copyright holder nor the names of its
18  * contributors may be used to endorse or promote products derived
19  * from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25  * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * POSSIBILITY OF SUCH DAMAGE.
33  */
34 
35 #include <fadecandy_msgs/LEDArray.h>
36 
37 #include "./fadecandy_driver_ros.h"
38 
39 namespace fadecandy_driver
40 {
41 FadecandyDriverROS::FadecandyDriverROS(double restart_patience) : restart_patience_(restart_patience)
42 {
43  ros::NodeHandle nh;
44 
47 
49 
50  led_subscriber_ = nh.subscribe<fadecandy_msgs::LEDArray>("set_leds", 1, &FadecandyDriverROS::setLedsCallback, this);
51 }
52 
54 {
56  ros::spin();
57 }
58 
60 {
61  try
62  {
63  auto serial_number = driver_.connect();
64  diagnostic_updater_.setHardwareID(serial_number);
65  ROS_INFO("Fadecandy device is connected.");
66  }
67  catch (const std::exception& e)
68  {
69  ROS_WARN_ONCE("Failed to connect to device: %s; will retry every %f seconds", e.what(), restart_patience_);
70  }
71 }
72 
73 void FadecandyDriverROS::setLedsCallback(const fadecandy_msgs::LEDArrayConstPtr& led_array_msg)
74 {
75  if (!driver_.isConnected())
76  {
77  return;
78  }
79 
80  std::vector<std::vector<Color>> led_array_colors;
81  for (const auto& strip : led_array_msg->strips)
82  {
83  std::vector<Color> led_strip_colors;
84  for (const auto& color : strip.colors)
85  {
86  led_strip_colors.emplace_back(static_cast<int>(color.r * 255), static_cast<int>(color.g * 255),
87  static_cast<int>(color.b * 255));
88  }
89  led_array_colors.push_back(led_strip_colors);
90  }
91 
92  try
93  {
94  driver_.setColors(led_array_colors);
95  }
96  catch (const std::exception& e)
97  {
98  ROS_ERROR("Error occured: %s ", e.what());
99  }
100 };
101 
103 {
104  if (driver_.isConnected())
105  {
106  diagnostic_status.summary(diagnostic_msgs::DiagnosticStatus::OK, "Connected");
107  }
108  else
109  {
110  diagnostic_status.summary(diagnostic_msgs::DiagnosticStatus::ERROR, "Disconnected");
111  }
112 }
113 
115 {
117 }
118 
120 {
121  if (driver_.isConnected())
122  {
123  return;
124  }
125  setupConnection();
126 }
127 } // namespace fadecandy_driver
ros::Subscriber led_subscriber_
led_subscriber_ LED messages subscriber
Timer createTimer(Rate r, Handler h, Obj o, bool oneshot=false, bool autostart=true) const
void summary(unsigned char lvl, const std::string s)
Subscriber subscribe(const std::string &topic, uint32_t queue_size, void(T::*fp)(M), T *obj, const TransportHints &transport_hints=TransportHints())
void setHardwareID(const std::string &hwid)
void setLedsCallback(const fadecandy_msgs::LEDArrayConstPtr &msg)
void connectTimerCallback(const ros::TimerEvent &e)
void setupConnection()
setupConnection Connect the driver to the device
void add(const std::string &name, TaskFunction f)
FadecandyDriver driver_
driver_ Fadecandy driver
ros::Timer diagnostics_timer_
timer_ Periodic timer for updating the diagnostics
void run()
run Listen to LED messages and publishes diagnostic of the driver
double restart_patience_
restart_patience_ Restart patience time
FadecandyDriverROS(double restart_patience)
FadecandyDriverRos fadecandy driver ROS wrapper.
void setColors(std::vector< std::vector< Color >> led_colors)
setColors Transfer the LED color stream to the driver
diagnostic_updater::Updater diagnostic_updater_
diagnostic_updater_ Diagnostic updater
#define ROS_WARN_ONCE(...)
#define ROS_INFO(...)
void diagnosticsCallback(diagnostic_updater::DiagnosticStatusWrapper &diagnostic_status)
diagnosticsCallback Diagnostics callback
void diagnosticsTimerCallback(const ros::TimerEvent &e)
ROSCPP_DECL void spin()
ros::Timer connect_timer_
connection_check_timer_ Periodic timer for checking the connection
std::string connect()
connect Initialize the Fadecandy device
#define ROS_ERROR(...)


fadecandy_driver
Author(s):
autogenerated on Mon Feb 28 2022 22:18:46