差分
このページの2つのバージョン間の差分を表示します。
| 次のリビジョン | 前のリビジョン | ||
| articles:rtc_handle [2021/11/09 12:43] – 作成 Takashi Suehiro | articles:rtc_handle [2021/11/15 11:31] (現在) – [rtc_handle] Takashi Suehiro | ||
|---|---|---|---|
| 行 2: | 行 2: | ||
| RTミドルウェア(OpenRTM-aist, | RTミドルウェア(OpenRTM-aist, | ||
| - | 環境を提供するツールです. | + | 環境を提供するツールです.とりあえずpython2, |
| **ghithubサイト(ときどき手を入れています):**[[https:// | **ghithubサイト(ときどき手を入れています):**[[https:// | ||
| 行 16: | 行 16: | ||
| * [[https:// | * [[https:// | ||
| + | 以下はipnbで生成したものをdokuwikiフォーマットに変換したものです. | ||
| + | ---- | ||
| + | ===== rtc_handle.py(basic) ===== | ||
| + | |||
| + | * this ipnb shows a basic usage of rtc_handle.py | ||
| + | * precondition: | ||
| + | * you can monitor the behavior of the system with openrtp | ||
| + | |||
| + | you can access and control rtcs of OpenRTM-aist (written in any languages, ie., c++, python, java) by using rtc_handle.py | ||
| + | |||
| + | <code python> | ||
| + | # | ||
| + | # -*- Python -*- | ||
| + | |||
| + | import sys | ||
| + | import time | ||
| + | import subprocess | ||
| + | </ | ||
| + | ==== setup user environmet ==== | ||
| + | |||
| + | * path for rtc_handle | ||
| + | * path for rtcs and user tools | ||
| + | * nameservers | ||
| + | |||
| + | you may provide a file (ex. set_env.py) for this. | ||
| + | |||
| + | <code python> | ||
| + | # | ||
| + | # set up user environment | ||
| + | # RtmToolsDir, | ||
| + | # | ||
| + | # from set_env import * : you may provide a setup file like this | ||
| + | # | ||
| + | RtmToolsDir=" | ||
| + | MyRtcDir=" | ||
| + | NS0=" | ||
| + | |||
| + | </ | ||
| + | ==== import user tools ==== | ||
| + | |||
| + | path is modified temporaly to import tools | ||
| + | |||
| + | stubs for rtc service ports might be imported (this will be explained another example). | ||
| + | |||
| + | <code python> | ||
| + | # | ||
| + | # import user tools | ||
| + | # | ||
| + | sys.path.append(" | ||
| + | save_path = sys.path[:] | ||
| + | sys.path.append(RtmToolsDir+'/ | ||
| + | from rtc_handle import * | ||
| + | # from rtc_handle_util import * | ||
| + | # sys.path.append(RtmToolsDir+'/ | ||
| + | # from EmbryonicRtc import * | ||
| + | sys.path = save_path | ||
| + | |||
| + | # | ||
| + | # import stub files | ||
| + | # | ||
| + | #import _GlobalIDL | ||
| + | |||
| + | </ | ||
| + | ==== RtmEnv: rtm environment holder ==== | ||
| + | |||
| + | RtmEnv class object contains an orb, name-servers, | ||
| + | |||
| + | the second arg is a list of cos nameservers. | ||
| + | |||
| + | <code python> | ||
| + | # | ||
| + | # user program | ||
| + | # | ||
| + | |||
| + | # | ||
| + | env = RtmEnv(sys.argv, | ||
| + | |||
| + | </ | ||
| + | ==== NameSpace ==== | ||
| + | |||
| + | NameSpace.env.list_obj() retrieves a list of corba objects in the nameserver and put them into the NameSpace.obj_list dictionary. | ||
| + | |||
| + | if an object is an rtc, its proxy object(RtcHandl) is created and put into the NameSpace.rtc_handle dictionary. | ||
| + | |||
| + | <code python> | ||
| + | env.name_space[NS0].list_obj() | ||
| + | </ | ||
| + | < | ||
| + | objcet cin0.rtc was listed. | ||
| + | port_name: str_out | ||
| + | handle for cin0.rtc was created. | ||
| + | objcet cout0.rtc was listed. | ||
| + | port_name: str_in | ||
| + | handle for cout0.rtc was created. | ||
| + | |||
| + | [[' | ||
| + | | ||
| + | </ | ||
| + | <code python> | ||
| + | env.name_space[NS0].obj_list | ||
| + | </ | ||
| + | < | ||
| + | {' | ||
| + | ' | ||
| + | </ | ||
| + | <code python> | ||
| + | env.name_space[NS0].rtc_handles | ||
| + | </ | ||
| + | < | ||
| + | {' | ||
| + | ' | ||
| + | </ | ||
| + | ==== RtcHandle: proxy of rtc ==== | ||
| + | |||
| + | RtcHandle is a proxy class of rtc and the center object of this module. an rtc can be accessed by its object reference and information of the rtc can be gathered throug the reference. an RtcHandle object holds those information and proxy the rtc. | ||
| + | |||
| + | === assign rtc proxies to valiables === | ||
| + | |||
| + | to ease access to rtc proxies, it may be a good idea assigning them to valiables. | ||
| + | |||
| + | <code python> | ||
| + | cin=env.name_space[NS0].rtc_handles[' | ||
| + | cout=env.name_space[NS0].rtc_handles[' | ||
| + | </ | ||
| + | === activate and deactivate rtcs === | ||
| + | |||
| + | <code python> | ||
| + | cout.activate() | ||
| + | </ | ||
| + | < | ||
| + | RTC_OK | ||
| + | </ | ||
| + | <code python> | ||
| + | cout.deactivate() | ||
| + | </ | ||
| + | < | ||
| + | RTC_OK | ||
| + | </ | ||
| + | <code python> | ||
| + | cout.activate() | ||
| + | </ | ||
| + | < | ||
| + | RTC_OK | ||
| + | </ | ||
| + | deactivation of some rtcs may fail. those rtcs may wait for resorces(ex. waiting for user input) in onExecute loop. for example, | ||
| + | |||
| + | <code python> | ||
| + | cin.activate() | ||
| + | </ | ||
| + | < | ||
| + | RTC_OK | ||
| + | </ | ||
| + | <code python> | ||
| + | cin.deactivate() | ||
| + | </ | ||
| + | < | ||
| + | RTC_ERROR | ||
| + | </ | ||
| + | but it usually recovers after the resorce is available. please input something at the cin console. then, | ||
| + | |||
| + | <code python> | ||
| + | cin.activate() | ||
| + | </ | ||
| + | < | ||
| + | RTC_OK | ||
| + | </ | ||
| + | === direct access to Inports and Outports === | ||
| + | |||
| + | if the interface_type of the ports is corba_cdr, you can put data to inports and get data from outpors by using rtc_handle.py. | ||
| + | |||
| + | == put data to inport == | ||
| + | |||
| + | <code python> | ||
| + | cout.inports | ||
| + | </ | ||
| + | < | ||
| + | {' | ||
| + | </ | ||
| + | <code python> | ||
| + | cout.inports[' | ||
| + | </ | ||
| + | < | ||
| + | RTC_OK | ||
| + | </ | ||
| + | <code python> | ||
| + | cout.inports[' | ||
| + | </ | ||
| + | < | ||
| + | PORT_OK | ||
| + | </ | ||
| + | <code python> | ||
| + | cout.inports[' | ||
| + | </ | ||
| + | < | ||
| + | RTC_OK | ||
| + | </ | ||
| + | == get data from outport == | ||
| + | |||
| + | by connecting to outport with setting ‘datapot.dataflow_type’ : ‘pull’ , you can get ‘dataport.corba_cdr.outport_ref’. | ||
| + | |||
| + | you can directly get the last data (and if not consumed other rtcs) by the ref. | ||
| + | |||
| + | <code python> | ||
| + | cin.outports | ||
| + | </ | ||
| + | < | ||
| + | {' | ||
| + | </ | ||
| + | <code python> | ||
| + | cin.outports[' | ||
| + | </ | ||
| + | < | ||
| + | RTC_OK | ||
| + | </ | ||
| + | <code python> | ||
| + | cin.outports[' | ||
| + | </ | ||
| + | < | ||
| + | {' | ||
| + | ' | ||
| + | ' | ||
| + | ' | ||
| + | ' | ||
| + | ' | ||
| + | ' | ||
| + | ' | ||
| + | ' | ||
| + | ' | ||
| + | ' | ||
| + | ' | ||
| + | ' | ||
| + | ' | ||
| + | </ | ||
| + | <code python> | ||
| + | cin.outports[' | ||
| + | </ | ||
| + | <code python> | ||
| + | cin.outports[' | ||
| + | </ | ||
| + | < | ||
| + | RTC_OK | ||
| + | </ | ||
| + | ==== IOConnector : connect and disconnect io-ports ==== | ||
| + | |||
| + | IOConnector contains information for connecting io-ports | ||
| + | |||
| + | create a connector between cin.outports[‘str_out’] and cout.inports[‘str_in’] | ||
| + | |||
| + | <code python> | ||
| + | con = IOConnector([cin.outports[' | ||
| + | </ | ||
| + | default properties of the connector is as follows | ||
| + | |||
| + | <code python> | ||
| + | con.def_prop | ||
| + | </ | ||
| + | < | ||
| + | {' | ||
| + | ' | ||
| + | ' | ||
| + | ' | ||
| + | ' | ||
| + | ' | ||
| + | ' | ||
| + | ' | ||
| + | ' | ||
| + | ' | ||
| + | </ | ||
| + | connect ports | ||
| + | |||
| + | <code python> | ||
| + | con.connect() | ||
| + | </ | ||
| + | < | ||
| + | RTC_OK | ||
| + | </ | ||
| + | <code python> | ||
| + | con.profile | ||
| + | </ | ||
| + | < | ||
| + | RTC.ConnectorProfile(name=' | ||
| + | </ | ||
| + | <code python> | ||
| + | con.prop_dict | ||
| + | </ | ||
| + | < | ||
| + | {' | ||
| + | ' | ||
| + | ' | ||
| + | ' | ||
| + | ' | ||
| + | ' | ||
| + | ' | ||
| + | ' | ||
| + | ' | ||
| + | ' | ||
| + | ' | ||
| + | ' | ||
| + | ' | ||
| + | ' | ||
| + | </ | ||
| + | disconnect ports | ||
| + | |||
| + | <code python> | ||
| + | con.disconnect() | ||
| + | </ | ||
| + | < | ||
| + | RTC_OK | ||
| + | </ | ||
| + | === change properties === | ||
| + | |||
| + | you can change properties by giving prop_dict | ||
| + | |||
| + | <code python> | ||
| + | con = IOConnector([cin.outports[' | ||
| + | prop_dict={' | ||
| + | </ | ||
| + | <code python> | ||
| + | con.prop_dict_req | ||
| + | </ | ||
| + | < | ||
| + | {' | ||
| + | ' | ||
| + | ' | ||
| + | ' | ||
| + | ' | ||
| + | ' | ||
| + | ' | ||
| + | ' | ||
| + | ' | ||
| + | ' | ||
| + | ' | ||
| + | </ | ||
| + | <code python> | ||
| + | con.connect() | ||
| + | </ | ||
| + | < | ||
| + | RTC_OK | ||
| + | </ | ||
| + | <code python> | ||
| + | con.prop_dict | ||
| + | </ | ||
| + | < | ||
| + | {' | ||
| + | ' | ||
| + | ' | ||
| + | ' | ||
| + | ' | ||
| + | ' | ||
| + | ' | ||
| + | ' | ||
| + | ' | ||
| + | ' | ||
| + | ' | ||
| + | ' | ||
| + | ' | ||
| + | ' | ||
| + | </ | ||
| + | <code python> | ||
| + | con.disconnect() | ||
| + | </ | ||
| + | < | ||
| + | RTC_OK | ||
| + | </ | ||
| + | === conflict of connections === | ||
| + | |||
| + | only one connection is permitted between the same ports. | ||
| + | |||
| + | so, if another connection exists, you can not control the connection by your connector. | ||
| + | |||
| + | for example, | ||
| + | |||
| + | <code python> | ||
| + | b = IOConnector([cin.outports[' | ||
| + | </ | ||
| + | <code python> | ||
| + | b.connect() | ||
| + | </ | ||
| + | < | ||
| + | RTC_OK | ||
| + | </ | ||
| + | <code python> | ||
| + | con.connect() | ||
| + | </ | ||
| + | < | ||
| + | there exists another connection. please try force=True. | ||
| + | |||
| + | ' | ||
| + | </ | ||
| + | <code python> | ||
| + | con.disconnect() | ||
| + | </ | ||
| + | < | ||
| + | there exists another connection. please try force=True. | ||
| + | |||
| + | ' | ||
| + | </ | ||
| + | you can handle this situation by forcing connect/ | ||
| + | |||
| + | <code python> | ||
| + | con.connect(force=True) | ||
| + | </ | ||
| + | < | ||
| + | RTC_OK | ||
| + | </ | ||
| + | <code python> | ||
| + | |||
| + | </ | ||