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
fadecandy_driver::FadecandyDriverROS::driver_
FadecandyDriver driver_
driver_ Fadecandy driver
Definition: fadecandy_driver_ros.h:62
diagnostic_updater::Updater::force_update
void force_update()
fadecandy_driver::FadecandyDriver::connect
std::string connect()
connect Initialize the Fadecandy device
Definition: fadecandy_driver.cpp:61
fadecandy_driver::FadecandyDriverROS::run
void run()
run Listen to LED messages and publishes diagnostic of the driver
Definition: fadecandy_driver_ros.cpp:53
ROS_WARN_ONCE
#define ROS_WARN_ONCE(...)
fadecandy_driver::FadecandyDriverROS::setLedsCallback
void setLedsCallback(const fadecandy_msgs::LEDArrayConstPtr &msg)
Definition: fadecandy_driver_ros.cpp:73
fadecandy_driver::FadecandyDriverROS::restart_patience_
double restart_patience_
restart_patience_ Restart patience time
Definition: fadecandy_driver_ros.h:101
fadecandy_driver::FadecandyDriverROS::led_subscriber_
ros::Subscriber led_subscriber_
led_subscriber_ LED messages subscriber
Definition: fadecandy_driver_ros.h:72
fadecandy_driver::FadecandyDriverROS::diagnostic_updater_
diagnostic_updater::Updater diagnostic_updater_
diagnostic_updater_ Diagnostic updater
Definition: fadecandy_driver_ros.h:96
diagnostic_updater::DiagnosticStatusWrapper::summary
void summary(const diagnostic_msgs::DiagnosticStatus &src)
fadecandy_driver::FadecandyDriverROS::connectTimerCallback
void connectTimerCallback(const ros::TimerEvent &e)
Definition: fadecandy_driver_ros.cpp:119
fadecandy_driver::FadecandyDriver::setColors
void setColors(std::vector< std::vector< Color >> led_colors)
setColors Transfer the LED color stream to the driver
Definition: fadecandy_driver.cpp:121
ros::NodeHandle::subscribe
Subscriber subscribe(const std::string &topic, uint32_t queue_size, const boost::function< void(C)> &callback, const VoidConstPtr &tracked_object=VoidConstPtr(), const TransportHints &transport_hints=TransportHints())
ros::TimerEvent
diagnostic_updater::Updater::setHardwareID
void setHardwareID(const std::string &hwid)
fadecandy_driver::FadecandyDriverROS::diagnostics_timer_
ros::Timer diagnostics_timer_
timer_ Periodic timer for updating the diagnostics
Definition: fadecandy_driver_ros.h:84
fadecandy_driver::FadecandyDriverROS::FadecandyDriverROS
FadecandyDriverROS(double restart_patience)
FadecandyDriverRos fadecandy driver ROS wrapper.
Definition: fadecandy_driver_ros.cpp:41
fadecandy_driver
Definition: fadecandy_driver.cpp:39
ROS_ERROR
#define ROS_ERROR(...)
diagnostic_updater::DiagnosticStatusWrapper
fadecandy_driver::FadecandyDriverROS::diagnosticsCallback
void diagnosticsCallback(diagnostic_updater::DiagnosticStatusWrapper &diagnostic_status)
diagnosticsCallback Diagnostics callback
Definition: fadecandy_driver_ros.cpp:102
fadecandy_driver_ros.h
ros::spin
ROSCPP_DECL void spin()
fadecandy_driver::FadecandyDriver::isConnected
bool isConnected()
isConnected
Definition: fadecandy_driver.cpp:116
ROS_INFO
#define ROS_INFO(...)
fadecandy_driver::FadecandyDriverROS::connect_timer_
ros::Timer connect_timer_
connection_check_timer_ Periodic timer for checking the connection
Definition: fadecandy_driver_ros.h:90
diagnostic_updater::DiagnosticTaskVector::add
void add(const std::string &name, TaskFunction f)
ros::NodeHandle::createTimer
Timer createTimer(Duration period, const TimerCallback &callback, bool oneshot=false, bool autostart=true) const
ros::Duration
fadecandy_driver::FadecandyDriverROS::diagnosticsTimerCallback
void diagnosticsTimerCallback(const ros::TimerEvent &e)
Definition: fadecandy_driver_ros.cpp:114
fadecandy_driver::FadecandyDriverROS::setupConnection
void setupConnection()
setupConnection Connect the driver to the device
Definition: fadecandy_driver_ros.cpp:59
ros::NodeHandle


fadecandy_driver
Author(s):
autogenerated on Wed Mar 2 2022 00:19:01