JoystickImpl.java
Go to the documentation of this file.
00001 // -*- Java -*-
00010 package jp.go.aist.hrp.joystick.rtc;
00011 
00012 import org.eclipse.swt.graphics.Point;
00013 
00014 import jp.go.aist.hrp.joystick.views.joystickView;
00015 import jp.go.aist.rtm.RTC.DataFlowComponentBase;
00016 import jp.go.aist.rtm.RTC.Manager;
00017 import jp.go.aist.rtm.RTC.port.OutPort;
00018 import jp.go.aist.rtm.RTC.util.DataRef;
00019 import RTC.ReturnCode_t;
00020 import RTC.TimedFloatSeq;
00021 import RTC.Time;
00022 
00023 
00029 public class JoystickImpl extends DataFlowComponentBase {
00030 
00035         public JoystickImpl(Manager manager) {  
00036         super(manager);
00037     }
00038 
00048     @Override
00049     protected ReturnCode_t onInitialize() {
00050         
00051         // <rtc-template block="initializer">
00052         m_pos_val = new TimedFloatSeq(new Time(0,0), new float[2]);
00053         m_pos = new DataRef<TimedFloatSeq>(m_pos_val);
00054         m_posOut = new OutPort<TimedFloatSeq>("pos", m_pos);
00055         m_vel_val = new TimedFloatSeq(new Time(0,0), new float[2]);
00056         m_vel = new DataRef<TimedFloatSeq>(m_vel_val);
00057         m_velOut = new OutPort<TimedFloatSeq>("vel", m_vel);
00058         // </rtc-template>
00059 
00060         // Registration: InPort/OutPort/Service
00061         // <rtc-template block="registration">
00062         // Set InPort buffers
00063         
00064         // Set OutPort buffer
00065         try {
00066                         registerOutPort(TimedFloatSeq.class, "pos", m_posOut);
00067                         registerOutPort(TimedFloatSeq.class, "vel", m_velOut);
00068         } catch (Exception e) {
00069                         e.printStackTrace();
00070                 }
00071        
00072         // Set service provider to Ports
00073         
00074         // Set service consumers to Ports
00075         
00076         // Set CORBA Service Ports
00077         
00078         // </rtc-template>
00079         
00080         return ReturnCode_t.RTC_OK;
00081     }
00082 
00083     /***
00084      *
00085      * The finalize action (on ALIVE->END transition)
00086      * formaer rtc_exiting_entry()
00087      *
00088      * @return RTC::ReturnCode_t
00089      * 
00090      * 
00091      */
00092     @Override
00093     protected ReturnCode_t onFinalize() {
00094         System.out.println("Joystick Finalized");
00095         return super.onFinalize();
00096     }
00097 
00098     /***
00099      *
00100      * The startup action when ExecutionContext startup
00101      * former rtc_starting_entry()
00102      *
00103      * @param ec_id target ExecutionContext Id
00104      *
00105      * @return RTC::ReturnCode_t
00106      * 
00107      * 
00108      */
00109     @Override
00110     protected ReturnCode_t onStartup(int ec_id) {
00111         System.out.println("Joystick Started");
00112         return super.onStartup(ec_id);
00113     }
00114 
00115     /***
00116      *
00117      * The shutdown action when ExecutionContext stop
00118      * former rtc_stopping_entry()
00119      *
00120      * @param ec_id target ExecutionContext Id
00121      *
00122      * @return RTC::ReturnCode_t
00123      * 
00124      * 
00125      */
00126     @Override
00127     protected ReturnCode_t onShutdown(int ec_id) {
00128         System.out.println("Joystick Shutting down");
00129         return super.onShutdown(ec_id);
00130     }
00131 
00132     /***
00133      *
00134      * The activated action (Active state entry action)
00135      * former rtc_active_entry()
00136      *
00137      * @param ec_id target ExecutionContext Id
00138      *
00139      * @return RTC::ReturnCode_t
00140      * 
00141      * 
00142      */
00143     @Override
00144     protected ReturnCode_t onActivated(int ec_id) {
00145         System.out.println("Joystick Activated");
00146         return super.onActivated(ec_id);
00147     }
00148 
00149     /***
00150      *
00151      * The deactivated action (Active state exit action)
00152      * former rtc_active_exit()
00153      *
00154      * @param ec_id target ExecutionContext Id
00155      *
00156      * @return RTC::ReturnCode_t
00157      * 
00158      * 
00159      */
00160     @Override
00161     protected ReturnCode_t onDeactivated(int ec_id) {
00162         System.out.println("Joystick Deactivated");
00163         return super.onDeactivated(ec_id);
00164     }
00165 
00166     /***
00167      *
00168      * The execution action that is invoked periodically
00169      * former rtc_active_do()
00170      *
00171      * @param ec_id target ExecutionContext Id
00172      *
00173      * @return RTC::ReturnCode_t
00174      * 
00175      * 
00176      */
00177     @Override
00178     protected ReturnCode_t onExecute(int ec_id) {
00179         
00180         Point p = joystickView.getJoystickPosition();
00181         float[] pos = {p.x,p.y};
00182         float[] vel = this.convert(p);
00183         m_pos_val.data = pos;
00184         m_vel_val.data = vel;
00185         m_posOut.write();
00186         m_velOut.write();
00187         
00188 //      System.out.println("("+p.x+", "+p.y+"), ("+vel[0]+", "+vel[1]+")");
00189         
00190         return ReturnCode_t.RTC_OK;
00191     }
00192 
00193     
00200     protected float[] convert(Point p) {
00201         double k=1.0;
00202         double _th = Math.atan2(p.y,p.x);
00203         double _v  = k * Math.hypot(p.x, p.y);
00204         double _vl = _v * Math.cos(_th - (Math.PI/4.0));
00205         double _vr = _v * Math.sin(_th - (Math.PI/4.0));
00206         if(_vr==-0.0) _vr*=-1;
00207         float[] v = {(float) _vl, (float) _vr};
00208         return v;
00209     }
00210 
00211     /***
00212      *
00213      * The aborting action when main logic error occurred.
00214      * former rtc_aborting_entry()
00215      *
00216      * @param ec_id target ExecutionContext Id
00217      *
00218      * @return RTC::ReturnCode_t
00219      * 
00220      * 
00221      */
00222   @Override
00223   public ReturnCode_t onAborting(int ec_id) {
00224           System.out.println("Joystick Aborted");
00225       return super.onAborting(ec_id);
00226   }
00227 
00228     /***
00229      *
00230      * The error action in ERROR state
00231      * former rtc_error_do()
00232      *
00233      * @param ec_id target ExecutionContext Id
00234      *
00235      * @return RTC::ReturnCode_t
00236      * 
00237      * 
00238      */
00239     @Override
00240     public ReturnCode_t onError(int ec_id) {
00241         System.out.println("Joystick Error : "+ec_id);
00242         return super.onError(ec_id);
00243     }
00244 
00245     /***
00246      *
00247      * The reset action that is invoked resetting
00248      * This is same but different the former rtc_init_entry()
00249      *
00250      * @param ec_id target ExecutionContext Id
00251      *
00252      * @return RTC::ReturnCode_t
00253      * 
00254      * 
00255      */
00256     @Override
00257     protected ReturnCode_t onReset(int ec_id) {
00258         System.out.println("Joystick Reset");
00259         return super.onReset(ec_id);
00260     }
00261 
00262     /***
00263      *
00264      * The state update action that is invoked after onExecute() action
00265      * no corresponding operation exists in OpenRTm-aist-0.2.0
00266      *
00267      * @param ec_id target ExecutionContext Id
00268      *
00269      * @return RTC::ReturnCode_t
00270      * 
00271      * 
00272      */
00273     @Override
00274     protected ReturnCode_t onStateUpdate(int ec_id) {
00275         return super.onStateUpdate(ec_id);
00276     }
00277 
00278     /***
00279      *
00280      * The action that is invoked when execution context's rate is changed
00281      * no corresponding operation exists in OpenRTm-aist-0.2.0
00282      *
00283      * @param ec_id target ExecutionContext Id
00284      *
00285      * @return RTC::ReturnCode_t
00286      * 
00287      * 
00288      */
00289     @Override
00290     protected ReturnCode_t onRateChanged(int ec_id) {
00291         return super.onRateChanged(ec_id);
00292     }
00293 
00294     // DataInPort declaration
00295     // <rtc-template block="inport_declare">
00296     
00297     // </rtc-template>
00298 
00299     // DataOutPort declaration
00300     // <rtc-template block="outport_declare">
00301     
00302     protected TimedFloatSeq m_pos_val;
00303     protected DataRef<TimedFloatSeq> m_pos;
00309     protected OutPort<TimedFloatSeq> m_posOut;
00310 
00311     protected TimedFloatSeq m_vel_val;
00312     protected DataRef<TimedFloatSeq> m_vel;
00318     protected OutPort<TimedFloatSeq> m_velOut;
00319     
00320     // </rtc-template>
00321 
00322     // CORBA Port declaration
00323     // <rtc-template block="corbaport_declare">
00324     
00325     // </rtc-template>
00326 
00327     // Service declaration
00328     // <rtc-template block="service_declare">
00329     
00330     // </rtc-template>
00331 
00332     // Consumer declaration
00333     // <rtc-template block="consumer_declare">
00334     
00335     // </rtc-template>
00336 
00337 
00338 }


openhrp3
Author(s): AIST, General Robotix Inc., Nakamura Lab of Dept. of Mechano Informatics at University of Tokyo
autogenerated on Sun Apr 2 2017 03:43:55