stageprofi.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2017 Fraunhofer Institute for Manufacturing Engineering and Automation (IPA)
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9 
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 
18 #include <stageprofi.h>
19 #include <ros/ros.h>
20 #include <boost/cstdint.hpp>
21 #include <boost/integer.hpp>
22 #include <algorithm>
23 
24 StageProfi::StageProfi(SerialIO* serialIO, unsigned int leds, int led_offset)
25 {
26  _serialIO = serialIO;
27  _num_leds = leds;
28  _led_offset = led_offset;
29 }
30 
32 {
33 }
34 
36 {
37  bool ret = false;
38  const char init_data[] = { 'C', '?' };
39  int init_len = sizeof(init_data) / sizeof(init_data[0]);
40 
41  char init_buf[2];
42 
43  memcpy(&init_buf, init_data, init_len);
44  if (_serialIO->sendData(init_buf, 2) == 2)
45  {
46  std::string rec;
47  if(_serialIO->readData(rec, 1) == 1)
48  ret = true;
49  }
50  else
51  ROS_ERROR("Sending init command to stageprofi failed");
52  return ret;
53 }
54 
56 {
57  color::rgba color_tmp = color;
58 
59  color_tmp.r *= color.a;
60  color_tmp.g *= color.a;
61  color_tmp.b *= color.a;
62 
63  color_tmp.r = fabs(color_tmp.r * 255);
64  color_tmp.g = fabs(color_tmp.g * 255);
65  color_tmp.b = fabs(color_tmp.b * 255);
66 
67  unsigned int num_channels = _num_leds * 3;
68  char channelbuffer[num_channels];
69 
70  for (int i = 0; i < _num_leds; i++)
71  {
72  channelbuffer[i * 3] = (int) color_tmp.r;
73  channelbuffer[i * 3 + 1] = (int) color_tmp.g;
74  channelbuffer[i * 3 + 2] = (int) color_tmp.b;
75  }
76 
77  uint16_t index = 0;
78  while (index < num_channels)
79  {
80  unsigned int size = std::min((unsigned int) MAX_CHANNELS,
81  num_channels - index);
82  if (sendDMX(index, channelbuffer + index, size))
83  index += size;
84  else
85  {
86  ROS_ERROR("Sending color to stageprofi failed");
87  this->recover();
88  break;
89  }
90  }
91  m_sigColorSet(color);
92 }
93 
94 void StageProfi::setColorMulti(std::vector<color::rgba> &colors)
95 {
96  color::rgba color_tmp;
97  unsigned int num_channels = _num_leds * 3;
98  char channelbuffer[num_channels];
99 
100  std::vector<color::rgba> color_out = colors;
101  //shift lex index by offset
102  if(_led_offset != 0)
103  {
104  if(_led_offset > 0)
105  std::rotate(color_out.begin(), color_out.begin()+_led_offset, color_out.end());
106  else
107  std::rotate(color_out.begin(), color_out.begin()+color_out.size()+_led_offset, color_out.end());
108  }
109 
110  for (int i = 0; i < _num_leds || i < colors.size(); i++)
111  {
112  color_tmp.r = color_out[i].r * color_out[i].a;
113  color_tmp.g = color_out[i].g * color_out[i].a;
114  color_tmp.b = color_out[i].b * color_out[i].a;
115 
116  color_tmp.r = fabs(color_tmp.r * 255);
117  color_tmp.g = fabs(color_tmp.g * 255);
118  color_tmp.b = fabs(color_tmp.b * 255);
119 
120  channelbuffer[i * 3] = (int) color_tmp.r;
121  channelbuffer[i * 3 + 1] = (int) color_tmp.g;
122  channelbuffer[i * 3 + 2] = (int) color_tmp.b;
123  }
124 
125  uint16_t index = 0;
126  while (index < num_channels)
127  {
128  unsigned int size = std::min((unsigned int) MAX_CHANNELS,
129  num_channels - index);
130  if (sendDMX(index, channelbuffer + index, size))
131  index += size;
132  else
133  {
134  ROS_ERROR("Sending color to stageprofi failed");
135  this->recover();
136  break;
137  }
138  }
139  m_sigColorSet(colors[0]);
140 }
141 
142 bool StageProfi::sendDMX(uint16_t start, const char* buf, unsigned int length)
143 {
144  bool ret = false;
145  std::string recv;
146  char msg[MAX_CHANNELS + HEADER_SIZE];
147 
148  unsigned int len = std::min((unsigned int) MAX_CHANNELS, length);
149 
150  msg[0] = 0xFF;
151  msg[1] = start & 0xFF;
152  msg[2] = (start >> 8) & 0xFF;
153  msg[3] = len;
154 
155  memcpy(msg + HEADER_SIZE, buf, len);
156 
157  const int bytes_to_send = len + HEADER_SIZE;
158 
159  //send color command to controller
160  ret = _serialIO->sendData(msg, bytes_to_send) == bytes_to_send;
161  //receive ack
162  ret &= _serialIO->readData(recv, 1) == 1;
163  return ret;
164 }
165 
167 {
168  ROS_WARN("Trying to recover stagedriver");
169  if(_serialIO->recover() && this->init())
170  return true;
171  else
172  return false;
173 }
StageProfi(SerialIO *serialIO, unsigned int leds, int led_offset)
Definition: stageprofi.cpp:24
int _led_offset
Definition: stageprofi.h:41
static const unsigned int HEADER_SIZE
Definition: stageprofi.h:42
bool sendDMX(uint16_t start, const char *buf, unsigned int length)
Definition: stageprofi.cpp:142
bool recover()
Definition: serialIO.cpp:161
SerialIO * _serialIO
Definition: stageprofi.h:39
#define ROS_WARN(...)
void setColor(color::rgba color)
Definition: stageprofi.cpp:55
void setColorMulti(std::vector< color::rgba > &colors)
Definition: stageprofi.cpp:94
int sendData(std::string value)
Definition: serialIO.cpp:63
int _num_leds
Definition: iColorO.h:44
bool init()
Definition: stageprofi.cpp:35
boost::signals2::signal< void(color::rgba color)> m_sigColorSet
Definition: iColorO.h:45
bool recover()
Definition: stageprofi.cpp:166
int readData(std::string &value, size_t nBytes)
Definition: serialIO.cpp:83
#define ROS_ERROR(...)
virtual ~StageProfi()
Definition: stageprofi.cpp:31
static const unsigned int MAX_CHANNELS
Definition: stageprofi.h:43


cob_light
Author(s): Benjamin Maidel
autogenerated on Wed Apr 7 2021 02:11:39