JoystickConfig.cpp
Go to the documentation of this file.
00001 /*
00002  * JoystickConfig.cpp
00003  *
00004  *  Created on: Oct 24, 2011
00005  *      Author: mriedel
00006  */
00007 
00008 #include <telekyb_hid/JoystickConfig.hpp>
00009 
00010 #include <ros/console.h>
00011 
00012 namespace TELEKYB_NAMESPACE
00013 {
00014 
00015 JoystickConfig::JoystickConfig()
00016         : axisCutOff(0.0)
00017 {
00018 
00019 }
00020 
00021 JoystickConfig::~JoystickConfig()
00022 {
00023 
00024 }
00025 
00026 void JoystickConfig::setVectors(const std::vector<unsigned char>& byteArray_,
00027                 const std::vector<JoyAxis>& axes_, const std::vector<JoyButton>& buttons_)
00028 {
00029         byteArray = byteArray_;
00030         axes = axes_;
00031         buttons = buttons_;
00032 }
00033 
00034 void JoystickConfig::setModifiers(const std::vector<int>& buttonRemapping_,
00035                 const std::vector<int>& axisRemapping_, const std::vector<double>& axisMultiplier_)
00036 {
00037         buttonRemapping = buttonRemapping_;
00038 
00039         unsigned int old_button_size = buttonRemapping.size();
00040         if (old_button_size > buttons.size()) {
00041                 ROS_ERROR("Buttonsremapping Array cannot be bigger then the number of buttons!");
00042         }
00043 
00044         buttonRemapping.resize(buttons.size());
00045         for (unsigned int i = 0; i < buttons.size(); i++) {
00046                 // Extend
00047                 if (i >= old_button_size) {
00048                         buttonRemapping[i] = i;
00049                 }
00050 
00051 
00052                 if (buttonRemapping[i] < 0 || buttonRemapping[i] > (signed)buttons.size()-1) {
00053                         ROS_ERROR("Wrong button mapping for button %d, value must be between 0 and %d "
00054                                         ,i, (signed)buttons.size()-1);
00055                         ROS_ERROR("Setting identity for button %d", i);
00056                         buttonRemapping[i] = i;
00057                 }
00058         }
00059 
00060 
00061         axisRemapping = axisRemapping_;
00062         unsigned int old_axesRe_size = axisRemapping.size();
00063         if (old_axesRe_size > axes.size()) {
00064                 ROS_ERROR("Axesremapping Array cannot be bigger then the number of Axes!");
00065         }
00066 
00067         axisMultiplier = axisMultiplier_;
00068         unsigned int old_axesMu_size = axisMultiplier.size();
00069         if (old_axesMu_size > axes.size()) {
00070                 ROS_WARN("Axesmultiplier Array cannot be bigger then the number of Axes!");
00071         }
00072 
00073         axisRemapping.resize(axes.size());
00074         axisMultiplier.resize(axes.size());
00075         for (unsigned int i = 0; i < axes.size(); ++i) {
00076                 // Extend
00077                 if (i >= old_axesRe_size) {
00078                         axisRemapping[i] = i;
00079                 }
00080 
00081                 if (i >= old_axesMu_size) {
00082                         axisMultiplier[i] = 1.0;
00083                 }
00084 
00085                 if (axisRemapping[i] < 0 || axisRemapping[i] > (signed)axes.size()-1 ) {
00086                         ROS_ERROR("Wrong axes mapping for axis %d, value must be between 0 and %d "
00087                                         ,i, (signed)axes.size()-1);
00088                         ROS_ERROR("Setting identity for axis %d", i);
00089                         axisRemapping[i] = i;
00090                 }
00091 
00092         }
00093 
00094 
00095 }
00096 
00097 void JoystickConfig::setAxisCutOff(float axisCutOff_)
00098 {
00099         axisCutOff = axisCutOff_;
00100 }
00101 
00102 void JoystickConfig::parseInput(unsigned char* input, sensor_msgs::Joy& joyMsg) const
00103 {
00104         joyMsg.axes.resize(axes.size());
00105         for (unsigned int i = 0; i < axes.size(); ++i) {
00106                 float value = (float)input[axes.at(i).bytePos] / (axes.at(i).max - axes.at(i).min);
00107                 value = (value * 2.0) - copysign(1.0, axes.at(i).max - axes.at(i).min);
00108 
00109                 if (fabsf(value) < axisCutOff) {
00110                         value = 0.0;
00111                 }
00112                 joyMsg.axes[axisRemapping[i]] = value * axisMultiplier[axisRemapping[i]];
00113 
00114         }
00115 
00116         joyMsg.buttons.resize(buttons.size());
00117         for (unsigned int i = 0; i < buttons.size(); ++i) {
00118                 bool pressed = ((input[buttons.at(i).bytePos] ^ byteArray[buttons.at(i).bytePos]) & buttons.at(i).mask)
00119                                                 == buttons.at(i).value;
00120                 joyMsg.buttons[buttonRemapping[i]] = (pressed ? 1 : 0);
00121 //              if (pressed) {
00122 //                      ROS_INFO("Button %d pressed!", i);
00123 //              }
00124         }
00125 }
00126 
00127 } /* namespace telekyb */
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Friends Defines


telekyb_hid
Author(s): mriedel
autogenerated on Mon Nov 11 2013 11:14:51