h4r_x52_joyext
LED and MFD Control for Saitek X52 Pro
 All Classes Files Functions Variables Enumerator Macros Pages
value2buttonColor.cpp
Go to the documentation of this file.
1 /*
2  * value2buttonColor Node
3  * by Christian Holl (www.rad-lab.net)
4  * License: BSD
5  *
6  * Have Fun! :-)
7  */
8 
116 #include <ros/ros.h>
117 #include <string>
118 #include <vector>
119 #include <set>
120 #include <limits>
121 #include <iostream>
122 #include <sstream>
123 #include <std_msgs/Float32.h>
124 #include <std_msgs/Float64.h>
125 #include <std_msgs/Int8.h>
126 #include <std_msgs/Int16.h>
127 #include <std_msgs/Int32.h>
128 #include <std_msgs/Int64.h>
129 #include <std_msgs/UInt8.h>
130 #include <std_msgs/UInt16.h>
131 #include <std_msgs/UInt32.h>
132 #include <std_msgs/UInt64.h>
133 #include <std_msgs/Bool.h>
134 #include <boost/algorithm/string.hpp>
135 #include <sensor_msgs/Joy.h>
136 #include <h4r_x52_joyext/x52_led_color.h>
137 
138 enum
139 {
152 };
153 
154 template <typename T>
156 {
157  enum
158  {
159  OFF='O',
160  GREEN='G',
161  YELLOW='Y',
162  RED='R',
163  };
164 
165 private:
166  class PairSortMod : public std::pair<unsigned int,T>
167  {
168  public:
169  bool operator<(const PairSortMod& other) const
170  {
171  return this->second < other.second;
172  }
173 
174  };
175  std::set< PairSortMod > ranges;
176  ros::NodeHandle *n;
177  ros::Publisher pub;
178  int led;
181  ros::Subscriber sub;
182 
183 
184 public:
185  PublishLED(ros::NodeHandle *n, std::string setup, int led, int axis, bool axis_or_button)
186  :n(n),
187  axis_or_button(axis_or_button),
188  axis_button(axis)
189  {
190  std::vector<std::string> separatedSetup;
191  boost::split(separatedSetup, setup, boost::is_any_of("|"));
192  PairSortMod pair;
193  unsigned int i;
194  for (i = 0; i < separatedSetup.size(); ++i)
195  {
196  if((i+1)%2 )
197  {
198  if(separatedSetup[i].size()!=1)
199  {
200  ROS_ERROR("Faulty setup string part, expected 'O','G','Y' or 'R' got %s",separatedSetup[i].c_str());
201  exit(1);
202  }
203  else
204  {
205  switch(separatedSetup[i][0])
206  {
207  case 'O':
208  pair.first=1;
209  break;
210 
211  case 'G':
212  pair.first=2;
213  break;
214 
215  case 'Y':
216  pair.first=3;
217  break;
218 
219  case 'R':
220  pair.first=4;
221  break;
222 
223  default:
224  ROS_ERROR("Faulty setup string part, expected 'O','G','Y' or 'R' got %s",separatedSetup[i].c_str());
225  exit(1);
226  }
227  }
228  }
229  else
230  {
231  std::istringstream ss(separatedSetup[i]);
232  T value;
233  ss>>value;
234  //std::cout<<"::"<<value<<"\n";
235  if(ss.bad() || ss.fail() || !ss.eof())
236  {
237  ROS_ERROR("Error in setup string: %s",separatedSetup[i].c_str());
238  exit(1);
239  }
240  pair.second=value;
241  this->ranges.insert(pair);
242  }
243  }
244 
245  if(!i%2 || i==0)
246  ROS_ERROR("Error in setup string, missing led setting for upper end!");
247  //Get last one
248  pair.second=std::numeric_limits<T>::max();
249  this->ranges.insert(pair);
250 
251  h4r_x52_joyext::x52_led_color ledmsg;
252  if(led<0 || led>(int)ledmsg.color_leds.size())
253  {
254  ROS_ERROR("LED number out of range!");
255  exit(1);
256  }
257  else
258  {
259  this->led=led;
260  }
261  pub=n->advertise< h4r_x52_joyext::x52_led_color >("led",1);
262  }
264  {}
265 
266  void progressValue(T value)
267  {
268  for (typename std::set<PairSortMod>::iterator it = ranges.begin(); it != ranges.end(); ++it)
269  {
270  if(it->second>value)
271  {
272  h4r_x52_joyext::x52_led_color msg;
273  msg.color_leds[this->led]=it->first;
274  pub.publish(msg);
275  break;
276  }
277  }
278  }
279 
280  void JoyCallback(const sensor_msgs::JoyConstPtr &msg)
281  {
282  if(axis_or_button==false)//Axis
283  {
284  if(axis_button<(int)msg->axes.size() && axis_button>=0)
285  {
286  progressValue((T)msg->axes[axis_button]);
287  }
288  else
289  {
290  ROS_ERROR("Axis %i not available for that Joystick!", axis_button);
291  }
292  }
293  else
294  {
295  if(axis_button<(int)msg->buttons.size() && axis_button>=0)
296  {
297  progressValue((T)msg->buttons[axis_button]);
298  }
299  else
300  {
301  ROS_ERROR("Button %i not available for that Joystick!", axis_button);
302  }
303  }
304  }
305 
306  template <class MSGPTR>
307  void Callback(const MSGPTR& msg)
308  {
309  progressValue(msg->data);
310  }
311 
312 
313 
314  template <class MSG, class MSGPTR>
315  void start()
316  {
317  sub=n->subscribe<MSG>("in",1000, &PublishLED<T>::Callback< MSGPTR >, this);
318  ros::spin();
319  }
320 
321  void startJoy()
322  {
323  sub=n->subscribe<sensor_msgs::Joy>("in",1000, &PublishLED<T>::JoyCallback, this);
324  ros::spin();
325  }
326 
327 };
328 
329 
330 
331 //Off, Green, Yellow, Red
332 
333 int main(int argc, char **argv)
334 {
335  ros::init(argc, argv, "value2buttonColor");
336  ros::NodeHandle n("~");
337 
338  int type;
339  int led;
340  bool axis_or_button;
341  int axis_button;
342  std::string setup;
343 
344 
345  n.param<int>("input_type", type, INPUT_FLOAT64);
346  n.param<int>("joy_axis_button", axis_button, 0);
347  n.param<bool>("joy_axis_or_button", axis_or_button, 0);
348  n.param<int>("color_led",led, 0);
349  ROS_DEBUG("LED -> %i",led);
350 
351 
352  //means OFF till -0.5, GREEN till 0, YELLOW till 0.5, and everything greater is RED
353  n.param<std::string>("setup_string", setup, "O|-0.5|G|0|Y|0.5|R");
354 
355 
356 
357 #define casem(CASE,TYPE,TYPEROS)\
358  case CASE:\
359  {\
360  PublishLED< TYPE > obj(&n,setup,led,axis_button,axis_or_button);\
361  obj.start<std_msgs::TYPEROS, std_msgs:: TYPEROS## ConstPtr>();\
362  break;\
363  }\
364 
365 
366 
367  switch(type)
368  {
369  casem(INPUT_FLOAT64, double_t, Float64)
370  casem(INPUT_FLOAT32, float_t, Float32)
371  casem(INPUT_INT64, int64_t, Int64)
372  casem(INPUT_INT32, int32_t, Int32)
373  casem(INPUT_INT16, int16_t, Int16)
374  casem(INPUT_INT8, int8_t, Int8)
375  casem(INPUT_UINT64, uint64_t, UInt64)
376  casem(INPUT_UINT32, uint32_t, UInt32)
377  casem(INPUT_UINT16, uint16_t, UInt16)
378  casem(INPUT_UINT8, uint8_t, UInt8)
379  casem(INPUT_BOOL, bool, Bool)
380  case INPUT_JOY:
381  {
382  PublishLED< double > obj(&n,setup,led,axis_button, axis_or_button);
383  obj.startJoy();
384  break;
385  }
386  default:
387  ROS_ERROR("Unkown Input Type!");
388  break;
389  }
390 }
void Callback(const MSGPTR &msg)
ros::NodeHandle * n
#define casem(CASE, TYPE, TYPEROS)
void progressValue(T value)
void JoyCallback(const sensor_msgs::JoyConstPtr &msg)
PublishLED(ros::NodeHandle *n, std::string setup, int led, int axis, bool axis_or_button)
int main(int argc, char **argv)
ros::Publisher pub
bool operator<(const PairSortMod &other) const
std::set< PairSortMod > ranges
ros::Subscriber sub