Joystick.cpp
Go to the documentation of this file.
00001 // -*- C++ -*-
00010 #include "Joystick.h"
00011 #include "js.h"
00012 
00013 // Module specification
00014 // <rtc-template block="module_spec">
00015 static const char* joystick_spec[] =
00016   {
00017     "implementation_id", "Joystick",
00018     "type_name",         "Joystick",
00019     "description",       "Access a joystick control device.",
00020     "version",           HRPSYS_PACKAGE_VERSION,
00021     "vendor",            "AIST",
00022     "category",          "Human input",
00023     "activity_type",     "PERIODIC",
00024     "kind",              "DataFlowComponent",
00025     "max_instance",      "1",
00026     "language",          "C++",
00027     "lang_type",         "compile",
00028     // Configuration variables
00029     "conf.default.device", "/dev/input/js1",
00030     "conf.default.debugLevel", "0",
00031     ""
00032   };
00033 // </rtc-template>
00034 
00039 Joystick::Joystick(RTC::Manager* manager)
00040     // <rtc-template block="initializer">
00041   : RTC::DataFlowComponentBase(manager),
00042     m_axesOut("Axes", m_axes),
00043     m_buttonsOut("Buttons", m_buttons),
00044     m_debugLevel(0)
00045 
00046     // </rtc-template>
00047 {
00048 }
00049 
00053 Joystick::~Joystick()
00054 {
00055 }
00056 
00057 
00058 
00059 RTC::ReturnCode_t Joystick::onInitialize()
00060 {
00061   // <rtc-template block="bind_config">
00062   // Bind variables and configuration variable
00063   bindParameter("device", m_device, "/dev/input/js1");
00064   bindParameter("debugLevel", m_debugLevel, "0");
00065 
00066   // Registration: InPort/OutPort/Service
00067   // <rtc-template block="registration">
00068   // Set InPort buffers
00069   
00070   // Set OutPort buffer
00071   addOutPort("Axes", m_axesOut);
00072   addOutPort("Buttons", m_buttonsOut);
00073   
00074   // Set service provider to Ports
00075   
00076   // Set service consumers to Ports
00077   
00078   // Set CORBA Service Ports
00079   
00080   // </rtc-template>
00081 
00082   return RTC::RTC_OK;
00083 }
00084 
00085 
00086 /*
00087 RTC::ReturnCode_t Joystick::onFinalize()
00088 {
00089   return RTC::RTC_OK;
00090 }
00091 */
00092 
00093 /*
00094 RTC::ReturnCode_t Joystick::onStartup(RTC::UniqueId ec_id)
00095 {
00096   return RTC::RTC_OK;
00097 }
00098 */
00099 
00100 /*
00101 RTC::ReturnCode_t Joystick::onShutdown(RTC::UniqueId ec_id)
00102 {
00103   return RTC::RTC_OK;
00104 }
00105 */
00106 
00107 RTC::ReturnCode_t Joystick::onActivated(RTC::UniqueId ec_id)
00108 {
00109   std::cout << "Joystick::onActivated(" << ec_id << ")" << std::endl;
00110   m_js = new joystick(m_device.c_str());
00111   if (m_js->is_open()){
00112     m_axes.data.length(m_js->nAxes());
00113     for (unsigned int i=0; i<m_js->nAxes(); i++){
00114         m_axes.data[i] = m_js->getAxisState(i);
00115     }
00116     m_buttons.data.length(m_js->nButtons());
00117     for (unsigned int i=0; i<m_js->nButtons(); i++){
00118         m_buttons.data[i] = m_js->getButtonState(i);
00119     }
00120     return RTC::RTC_OK;
00121   }else{
00122     std::cerr << "Joystick device(" << m_device << ") is not opened" << std::endl;
00123     return RTC::RTC_ERROR;  
00124   }
00125 }
00126 
00127 
00128 RTC::ReturnCode_t Joystick::onDeactivated(RTC::UniqueId ec_id)
00129 {
00130   std::cout << "Joystick::onDeactivated(" << ec_id << ")" << std::endl;
00131   delete m_js;
00132   return RTC::RTC_OK;
00133 }
00134 
00135 
00136 
00137 RTC::ReturnCode_t Joystick::onExecute(RTC::UniqueId ec_id)
00138 {
00139   while(m_js->readEvent());
00140   if (m_debugLevel > 0) printf("axes:");
00141   for (unsigned int i=0; i<m_js->nAxes(); i++){
00142     m_axes.data[i] = m_js->getAxisState(i);
00143     if (m_debugLevel > 0) printf("%4.1f ", m_axes.data[i]);
00144   }
00145   if (m_debugLevel > 0) printf(", buttons:");
00146   for (unsigned int i=0; i<m_js->nButtons(); i++){
00147     m_buttons.data[i] = m_js->getButtonState(i);
00148     if (m_debugLevel > 0) printf("%d", m_buttons.data[i]);
00149   }
00150   if (m_debugLevel > 0) printf("\n");
00151 
00152   m_axesOut.write();
00153   m_buttonsOut.write();
00154 
00155   return RTC::RTC_OK;
00156 }
00157 
00158 
00159 /*
00160 RTC::ReturnCode_t Joystick::onAborting(RTC::UniqueId ec_id)
00161 {
00162   return RTC::RTC_OK;
00163 }
00164 */
00165 
00166 /*
00167 RTC::ReturnCode_t Joystick::onError(RTC::UniqueId ec_id)
00168 {
00169   return RTC::RTC_OK;
00170 }
00171 */
00172 
00173 /*
00174 RTC::ReturnCode_t Joystick::onReset(RTC::UniqueId ec_id)
00175 {
00176   return RTC::RTC_OK;
00177 }
00178 */
00179 
00180 /*
00181 RTC::ReturnCode_t Joystick::onStateUpdate(RTC::UniqueId ec_id)
00182 {
00183   return RTC::RTC_OK;
00184 }
00185 */
00186 
00187 /*
00188 RTC::ReturnCode_t Joystick::onRateChanged(RTC::UniqueId ec_id)
00189 {
00190   return RTC::RTC_OK;
00191 }
00192 */
00193 
00194 
00195 
00196 extern "C"
00197 {
00198  
00199   void JoystickInit(RTC::Manager* manager)
00200   {
00201     coil::Properties profile(joystick_spec);
00202     manager->registerFactory(profile,
00203                              RTC::Create<Joystick>,
00204                              RTC::Delete<Joystick>);
00205   }
00206   
00207 };
00208 
00209 


hrpsys
Author(s): AIST, Fumio Kanehiro
autogenerated on Wed May 15 2019 05:02:18