GamePad.cpp
Go to the documentation of this file.
00001 /************************************************************************************
00002 GamePad RT-Component
00003 Copyright (c) 2010, Segway-Japan, Ltd.
00004 All rights reserved.
00005 
00006 Contact us if you use this software for sell.
00007 If you use this software not for sell, you can use this software under BSD lisence.
00008 See the files LICENSE.TXT and LICENSE-BSD.TXT for more details.                     
00009 ************************************************************************************/
00010 #include "GamePad.h"
00011 #include "pad_linux.h"
00012 
00013 
00014 // Module specification
00015 // <rtc-template block="module_spec">
00016 static const char* gamepad_spec[] =
00017   {
00018     "implementation_id", "GamePad",
00019     "type_name",         "GamePad",
00020     "description",       "GamePad Component",
00021     "version",           "0.1.0",
00022     "vendor",            "SegwayJapan",
00023     "category",          "Generic",
00024     "activity_type",     "SPORADIC",
00025     "kind",              "DataFlowComponent",
00026     "max_instance",      "10",
00027     "language",          "C++",
00028     "lang_type",         "compile",
00029     //"exec_cxt.periodic.rate", "30.0",
00030     // Configuration variables
00031     "conf.default.Klx", "1.0",
00032     "conf.default.Kly", "1.0",
00033     "conf.default.Krx", "1.0",
00034     "conf.default.Kry", "1.0",
00035     "conf.default.str_port", "/dev/input/js0",
00036 
00037     ""
00038   };
00039 // </rtc-template>
00040 
00041 GamePad::GamePad(RTC::Manager* manager)
00042     // <rtc-template block="initializer">
00043   : RTC::DataFlowComponentBase(manager),
00044     m_ButtonOut("Button", m_Button),
00045     m_StickLXOut("StickLX", m_StickLX),
00046     m_StickLYOut("StickLY", m_StickLY),
00047     m_StickRXOut("StickRX", m_StickRX),
00048     m_StickRYOut("StickRY", m_StickRY),
00049     m_StickLXdOut("StickLXd", m_StickLXd),
00050     m_StickLYdOut("StickLYd", m_StickLYd),
00051     m_StickRXdOut("StickRXd", m_StickRXd),
00052     m_StickRYdOut("StickRYd", m_StickRYd),
00053     m_VelocityOut("Velocity", m_Velocity),
00054     m_VelocityIISOut("VelocityIIS", m_VelocityIIS),
00055     m_Velocity2DIISOut("Velocity2DIIS", m_Velocity2DIIS),
00056 
00057     // </rtc-template>
00058         dummy(0)
00059 {
00060   // Registration: InPort/OutPort/Service
00061   // <rtc-template block="registration">
00062   // Set InPort buffers
00063   
00064   // Set OutPort buffer
00065   registerOutPort("Button", m_ButtonOut);
00066   registerOutPort("StickLX", m_StickLXOut);
00067   registerOutPort("StickLY", m_StickLYOut);
00068   registerOutPort("StickRX", m_StickRXOut);
00069   registerOutPort("StickRY", m_StickRYOut);
00070   registerOutPort("StickLXd", m_StickLXdOut);
00071   registerOutPort("StickLYd", m_StickLYdOut);
00072   registerOutPort("StickRXd", m_StickRXdOut);
00073   registerOutPort("StickRYd", m_StickRYdOut);
00074   registerOutPort("Velocity", m_VelocityOut);
00075   registerOutPort("VelocityIIS", m_VelocityIISOut);
00076   registerOutPort("Velocity2DIIS", m_Velocity2DIISOut);
00077   
00078   // Set service provider to Ports
00079   
00080   // Set service consumers to Ports
00081   
00082   // Set CORBA Service Ports
00083   
00084   // </rtc-template>
00085 
00086 
00087 
00088 
00089 
00090 }
00091 
00092 GamePad::~GamePad()
00093 {
00094 }
00095 
00096 
00097 RTC::ReturnCode_t GamePad::onInitialize()
00098 {
00099   // <rtc-template block="bind_config">
00100   // Bind variables and configuration variable
00101   bindParameter("Klx", m_Klx, "1.0");
00102   bindParameter("Kly", m_Kly, "1.0");
00103   bindParameter("Krx", m_Krx, "1.0");
00104   bindParameter("Kry", m_Kry, "1.0");
00105   bindParameter("str_port", m_str_port, "/dev/input/js0");
00106 
00107   
00108   // </rtc-template>
00109   return RTC::RTC_OK;
00110 }
00111 
00112 
00113 
00114 /*
00115 RTC::ReturnCode_t GamePad::onFinalize()
00116 {
00117   return RTC::RTC_OK;
00118 }
00119 */
00120 
00121 /*
00122 RTC::ReturnCode_t GamePad::onStartup(RTC::UniqueId ec_id)
00123 {
00124   return RTC::RTC_OK;
00125 }
00126 */
00127 
00128 /*
00129 RTC::ReturnCode_t GamePad::onShutdown(RTC::UniqueId ec_id)
00130 {
00131   return RTC::RTC_OK;
00132 }
00133 */
00134 
00135 
00136 RTC::ReturnCode_t GamePad::onActivated(RTC::UniqueId ec_id)
00137 {
00138 
00139   pad_sdl_js_open(m_str_port.c_str());
00140 
00141   return RTC::RTC_OK;
00142 }
00143 
00144 
00145 
00146 RTC::ReturnCode_t GamePad::onDeactivated(RTC::UniqueId ec_id)
00147 {
00148   pad_sdl_js_close();
00149 
00150   return RTC::RTC_OK;
00151 }
00152 
00153 
00159 RTC::ReturnCode_t GamePad::onExecute(RTC::UniqueId ec_id)
00160 {
00161 
00162   pad_sdl_js_update();
00163  
00164 
00165   m_Button.data = 0;
00166   for (int i = 0; i < 12; i++) {
00167     if (pad_sdl_js_button(i)) {
00168       m_Button.data |= (1<<i);
00169     }
00170   }
00171 
00172   //printf("button %x \n", (unsigned int)m_Button.data);
00173 
00174   float lx, ly, rx, ry;
00175   lx = (float)pad_sdl_js_axis(0) / (float)PAD_SDL_JS_AXIS_MAX;
00176   ly = (float)pad_sdl_js_axis(1) / (float)PAD_SDL_JS_AXIS_MAX;
00177   rx = (float)pad_sdl_js_axis(2) / (float)PAD_SDL_JS_AXIS_MAX;
00178   ry = (float)pad_sdl_js_axis(3) / (float)PAD_SDL_JS_AXIS_MAX;
00179 
00180   m_StickLX.data = m_Klx * lx;
00181   m_StickLY.data = m_Kly * ly;
00182   m_StickRX.data = m_Krx * rx;
00183   m_StickRY.data = m_Kry * ry;
00184 
00185   m_StickLXd.data = (double)m_StickLX.data;
00186   m_StickLYd.data = (double)m_StickLY.data;
00187   m_StickRXd.data = (double)m_StickRX.data;
00188   m_StickRYd.data = (double)m_StickRY.data;
00189 
00190   m_Velocity.v = -m_Kly * (double)ly;
00191   m_Velocity.w = -m_Klx * (double)rx;
00192 
00193   m_VelocityIIS.vx = -m_Kly * (double)ly;
00194   m_VelocityIIS.vy = -m_Klx * (double)lx;
00195   m_VelocityIIS.w  = -m_Krx * (double)rx;
00196 
00197   m_Velocity2DIIS.data.vx = -m_Kly * (double)ly;
00198   m_Velocity2DIIS.data.vy = -m_Klx * (double)lx;
00199   m_Velocity2DIIS.data.va = -m_Krx * (double)rx;
00200 
00201   m_ButtonOut.write();
00202   m_StickLXOut.write();
00203   m_StickLYOut.write();
00204   m_StickRXOut.write();
00205   m_StickRYOut.write();
00206 
00207   m_StickLXdOut.write();
00208   m_StickLYdOut.write();
00209   m_StickRXdOut.write();
00210   m_StickRYdOut.write();
00211 
00212   m_VelocityOut.write();
00213   m_VelocityIISOut.write();
00214   
00215   //ysuzuki replace-
00216   //m_Velocity2DIISOut.write();
00217   if(pad_sdl_js_isok()==true){
00218     m_Velocity2DIISOut.write();
00219   }
00220   //-replace
00221 
00222   coil::usleep(15000);
00223 
00224   return RTC::RTC_OK;
00225 }
00226 
00227 
00228 /*
00229 RTC::ReturnCode_t GamePad::onAborting(RTC::UniqueId ec_id)
00230 {
00231   return RTC::RTC_OK;
00232 }
00233 */
00234 
00235 /*
00236 RTC::ReturnCode_t GamePad::onError(RTC::UniqueId ec_id)
00237 {
00238   return RTC::RTC_OK;
00239 }
00240 */
00241 
00242 /*
00243 RTC::ReturnCode_t GamePad::onReset(RTC::UniqueId ec_id)
00244 {
00245   return RTC::RTC_OK;
00246 }
00247 */
00248 
00249 /*
00250 RTC::ReturnCode_t GamePad::onStateUpdate(RTC::UniqueId ec_id)
00251 {
00252   return RTC::RTC_OK;
00253 }
00254 */
00255 
00256 /*
00257 RTC::ReturnCode_t GamePad::onRateChanged(RTC::UniqueId ec_id)
00258 {
00259   return RTC::RTC_OK;
00260 }
00261 */
00262 
00263 
00264 
00265 extern "C"
00266 {
00267 
00268   void GamePadInit(RTC::Manager* manager)
00269   {
00270     RTC::Properties profile(gamepad_spec);
00271     manager->registerFactory(profile,
00272                              RTC::Create<GamePad>,
00273                              RTC::Delete<GamePad>);
00274   }
00275 
00276 };
00277 
00278 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines


RS003
Author(s):
autogenerated on Thu Jun 27 2013 14:58:49