AutoControl.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3 # -*- Python -*-
4 
5 import sys
6 import time
7 sys.path.append(".")
8 
9 # Import RTM module
10 import RTC
11 import OpenRTM_aist
12 
13 # Import Service implementation class
14 # <rtc-template block="service_impl">
15 
16 # </rtc-template>
17 
18 # Import Service stub modules
19 # <rtc-template block="consumer_import">
20 # </rtc-template>
21 
22 
23 # This module's spesification
24 # <rtc-template block="module_spec">
25 AutoControl_spec = ["implementation_id", "AutoControl",
26  "type_name", "AutoControl",
27  "description", "Auto controller component for MobileRobot",
28  "version", "1.0.0",
29  "vendor", "AIST",
30  "category", "example",
31  "activity_type", "DataFlowComponent",
32  "max_instance", "1",
33  "language", "Python",
34  "lang_type", "SCRIPT",
35  "conf.default.velocity", "80.0",
36  "conf.default.turn_velocity", "80.0",
37  "conf.default.distance_to_env", "40.0",
38  ""]
39 # </rtc-template>
40 
42  def __init__(self, manager):
43  OpenRTM_aist.DataFlowComponentBase.__init__(self, manager)
44  # initialize of configuration-data.
45  # <rtc-template block="init_conf_param">
46  self._velocity = [80.0]
47  self._turn_velocity = [80.0]
48  self._distance_to_env = [40.0]
49 
50  return
51 
52 
53  def onInitialize(self):
54  # Bind variables and configuration variable
55  self.bindParameter("velocity", self._velocity, "80.0")
56  self.bindParameter("turn_velocity", self._turn_velocity, "80.0")
57  self.bindParameter("distance_to_env", self._distance_to_env, "40.0")
58 
59  self._d_sens = RTC.TimedFloatSeq(RTC.Time(0,0),[])
60  self._sensIn = OpenRTM_aist.InPort("sens", self._d_sens)
61  self._d_vel = RTC.TimedFloatSeq(RTC.Time(0,0),[])
62  self._velOut = OpenRTM_aist.OutPort("vel", self._d_vel)
63 
64  # Set InPort buffers
65  self.addInPort("sens",self._sensIn)
66 
67  # Set OutPort buffers
68  self.addOutPort("vel",self._velOut)
69 
70  return RTC.RTC_OK
71 
72 
73 
74  def onActivated(self, ec_id):
75  self._d_vel.data = [0.0, 0.0]
76  self._velOut.write()
77  return RTC.RTC_OK
78 
79  def onDeactivated(self, ec_id):
80  self._d_vel.data = [0.0, 0.0]
81  self._velOut.write()
82  return RTC.RTC_OK
83 
84  def onExecute(self, ec_id):
85  if self._sensIn.isNew():
86  self._d_sens = self._sensIn.read()
87  self._d_vel.data = self.calcVel()
88  self._velOut.write()
89  time.sleep(0.1)
90  return RTC.RTC_OK
91 
92  time.sleep(0.1)
93  return RTC.RTC_OK
94 
95 
96 
97  def calcVel(self):
98  if self._d_sens.data[3] <= self._distance_to_env[0]:
99  return [self._turn_velocity[0], -(self._turn_velocity[0])]
100 
101  elif self._d_sens.data[3] > self._distance_to_env[0]:
102  return [self._velocity[0] for i in range(2)]
103  else:
104  return [0.0 for i in range(2)]
105 
106 
107 
108 def AutoControlInit(manager):
109  profile = OpenRTM_aist.Properties(defaults_str=AutoControl_spec)
110  manager.registerFactory(profile,
111  AutoControl,
112  OpenRTM_aist.Delete)
113 
114 def MyModuleInit(manager):
115  AutoControlInit(manager)
116 
117  # Create a component
118  comp = manager.createComponent("AutoControl")
119 
120 
121 
122 def main():
123  mgr = OpenRTM_aist.Manager.init(sys.argv)
124  mgr.setModuleInitProc(MyModuleInit)
125  mgr.activateManager()
126  mgr.runManager()
127 
128 if __name__ == "__main__":
129  main()
130 
def bindParameter(self, param_name, var, def_val, trans=None)
template <typename vartype>=""> bool bindParameter(const char* param_name, VarType& var...
Definition: RTObject.py:2588
The Properties class represents a persistent set of properties.
Definition: Properties.py:83
def addOutPort(self, name, outport)
Definition: RTObject.py:2765
def addInPort(self, name, inport)
Definition: RTObject.py:2721
InPort template class.
Definition: InPort.py:58


openrtm_aist_python
Author(s): Shinji Kurihara
autogenerated on Thu Jun 6 2019 19:11:34