-
Notifications
You must be signed in to change notification settings - Fork 0
/
CraneX7ControllerRTC.py
401 lines (331 loc) · 10.7 KB
/
CraneX7ControllerRTC.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# -*- Python -*-
"""
@file CraneX7ControllerRTC.py
@brief RTC for CRANE-X7
@date $Date$
"""
import sys
# Import RTM module
import RTC
import OpenRTM_aist
import ManipulatorCommonInterface_Common_idl
import ManipulatorCommonInterface_Middle_idl
# Import Service implementation class
# <rtc-template block="service_impl">
from ManipulatorCommonInterface_Common_idl_example import *
from ManipulatorCommonInterface_Middle_idl_example import *
# </rtc-template>
# Import Service stub modules
# <rtc-template block="consumer_import">
# </rtc-template>
from CraneX7Controller import CraneX7 as robot
# This module's spesification
# <rtc-template block="module_spec">
cranex7controllerrtc_spec = ["implementation_id", "CraneX7ControllerRTC",
"type_name", "CraneX7ControllerRTC",
"description", "RTC for CRANE-X7",
"version", "1.0.0",
"vendor", "takahasi",
"category", "Manipulation",
"activity_type", "STATIC",
"max_instance", "1",
"language", "Python",
"lang_type", "SCRIPT",
"conf.default.device", "/dev/ttyUSB0",
"conf.__widget__.device", "text",
"conf.__type__.device", "string",
""]
# </rtc-template>
##
# @class CraneX7ControllerRTC
# @brief RTC for CRANE-X7
#
#
class CraneX7ControllerRTC(OpenRTM_aist.DataFlowComponentBase):
##
# @brief constructor
# @param manager Maneger Object
#
def __init__(self, manager):
OpenRTM_aist.DataFlowComponentBase.__init__(self, manager)
self._d_joints = RTC.TimedFloatSeq(RTC.Time(0, 0), [])
self._jointsIn = OpenRTM_aist.InPort("joints", self._d_joints)
self._d_grip = RTC.TimedOctet(RTC.Time(0, 0), 0)
self._gripIn = OpenRTM_aist.InPort("grip", self._d_grip)
self._d_is_moving = RTC.TimedBoolean(RTC.Time(0, 0), False)
self._is_movingOut = OpenRTM_aist.OutPort(
"is_moving", self._d_is_moving)
self._d_out_joints = RTC.TimedFloatSeq(RTC.Time(0, 0), [])
self._out_jointsOut = OpenRTM_aist.OutPort(
"out_joints", self._d_out_joints)
self._d_out_velocity = RTC.TimedFloatSeq(RTC.Time(0, 0), [])
self._out_velocityOut = OpenRTM_aist.OutPort(
"out_velocity", self._d_out_velocity)
self._d_out_current = RTC.TimedFloatSeq(RTC.Time(0, 0), [])
self._out_currentOut = OpenRTM_aist.OutPort(
"out_current", self._d_out_current)
self._d_status = RTC.TimedOctet(RTC.Time(0, 0), 0)
self._statusOut = OpenRTM_aist.OutPort("status", self._d_status)
self._commonPort = OpenRTM_aist.CorbaPort("common")
self._common = ManipulatorCommonInterface_Common_i()
self._middlePort = OpenRTM_aist.CorbaPort("middle")
self._middle = ManipulatorCommonInterface_Middle_i()
# initialize of configuration-data.
# <rtc-template block="init_conf_param">
"""
- Name: device
- DefaultValue: /dev/ttyUSB0
"""
self._device = ['/dev/ttyUSB0']
# </rtc-template>
##
#
# The initialize action (on CREATED->ALIVE transition)
# formaer rtc_init_entry()
#
# @return RTC::ReturnCode_t
#
#
def onInitialize(self):
# Bind variables and configuration variable
self.bindParameter("device", self._device, "/dev/ttyUSB0")
# Set InPort buffers
self.addInPort("joints", self._jointsIn)
self.addInPort("grip", self._gripIn)
# Set OutPort buffers
self.addOutPort("is_moving", self._is_movingOut)
self.addOutPort("out_joints", self._out_jointsOut)
self.addOutPort("out_velocity", self._out_velocityOut)
self.addOutPort("out_current", self._out_currentOut)
self.addOutPort("status", self._statusOut)
# Set service provider to Ports
self._commonPort.registerProvider(
"common", "JARA_ARM::ManipulatorCommonInterface_Common", self._common)
self._middlePort.registerProvider(
"middle", "JARA_ARM::ManipulatorCommonInterface_Middle", self._middle)
# Set service consumers to Ports
# Set CORBA Service Ports
self.addPort(self._commonPort)
self.addPort(self._middlePort)
instance = OpenRTM_aist.Manager.instance()
self._log = instance.getLogbuf("CraneX7Controller")
self._robot = None
return RTC.RTC_OK
##
#
# The finalize action (on ALIVE->END transition)
# formaer rtc_exiting_entry()
#
# @return RTC::ReturnCode_t
#
#
# def onFinalize(self):
#
# return RTC.RTC_OK
##
#
# The startup action when ExecutionContext startup
# former rtc_starting_entry()
#
# @param ec_id target ExecutionContext Id
#
# @return RTC::ReturnCode_t
#
#
# def onStartup(self, ec_id):
#
# return RTC.RTC_OK
##
#
# The shutdown action when ExecutionContext stop
# former rtc_stopping_entry()
#
# @param ec_id target ExecutionContext Id
#
# @return RTC::ReturnCode_t
#
#
# def onShutdown(self, ec_id):
#
# return RTC.RTC_OK
##
#
# The activated action (Active state entry action)
# former rtc_active_entry()
#
# @param ec_id target ExecutionContext Id
#
# @return RTC::ReturnCode_t
#
#
def onActivated(self, ec_id):
self._robot = robot(device=self._device[0])
if not self._robot.open():
self._log.RTC_ERROR("cannot open robot communication: " + self._device[0])
self._robot = None
return RTC.RTC_ERROR
self._middle.set_robot(self._robot)
self._middle.set_common(self._common)
self._common.set_robot(self._robot)
return RTC.RTC_OK
##
#
# The deactivated action (Active state exit action)
# former rtc_active_exit()
#
# @param ec_id target ExecutionContext Id
#
# @return RTC::ReturnCode_t
#
#
def onDeactivated(self, ec_id):
if not self._robot:
return RTC.RTC_OK
if self._robot.is_opened:
if not self._robot.close():
self._log.RTC_ERROR("cannot close robot communication")
return RTC.RTC_ERROR
self._middle.unset_robot()
self._middle.unset_common()
self._common.unset_robot()
del self._robot
self._robot = None
return RTC.RTC_OK
##
#
# The execution action that is invoked periodically
# former rtc_active_do()
#
# @param ec_id target ExecutionContext Id
#
# @return RTC::ReturnCode_t
#
#
def onExecute(self, ec_id):
if not self._robot:
return RTC.RTC_OK
if not self._robot.is_opened:
return RTC.RTC_OK
# move by joints
if self._jointsIn.isNew():
joints = self._jointsIn.read().data
if len(joints) == 7:
self._log.RTC_INFO("move; " + str(joints))
self._robot.movej(joints)
else:
self._log.RTC_ERROR("invalid joints parameters: " + str(joints))
# control gripper
if self._gripIn.isNew():
grip = self._gripIn.read().data
if grip == 0:
self._log.RTC_INFO("close_gripper")
self._robot.close_gripper()
elif grip == 1:
self._log.RTC_INFO("open_gripper")
self._robot.open_gripper()
else:
self._log.RTC_ERROR("invalid gripper control: " + str(grip))
# output moving information
is_moving = self._robot.moving
self._log.RTC_DEBUG("is_moving: " + str(is_moving))
self._d_is_moving.data = is_moving
self._is_movingOut.write()
# output joints information
joints = self._robot.pos
self._log.RTC_DEBUG("out_joints: " + str(joints))
self._d_out_joints.data = joints
self._out_jointsOut.write()
# output current information
current = self._robot.cur
self._log.RTC_DEBUG("out_current: " + str(current))
self._d_out_current.data = current
self._out_currentOut.write()
# output velocity information
velocity = self._robot.vel
self._log.RTC_DEBUG("out_velocity: " + str(velocity))
self._d_out_velocity.data = velocity
self._out_velocityOut.write()
return RTC.RTC_OK
##
#
# The aborting action when main logic error occurred.
# former rtc_aborting_entry()
#
# @param ec_id target ExecutionContext Id
#
# @return RTC::ReturnCode_t
#
#
# def onAborting(self, ec_id):
#
# return RTC.RTC_OK
##
#
# The error action in ERROR state
# former rtc_error_do()
#
# @param ec_id target ExecutionContext Id
#
# @return RTC::ReturnCode_t
#
#
# def onError(self, ec_id):
#
# return RTC.RTC_OK
##
#
# The reset action that is invoked resetting
# This is same but different the former rtc_init_entry()
#
# @param ec_id target ExecutionContext Id
#
# @return RTC::ReturnCode_t
#
#
# def onReset(self, ec_id):
#
# return RTC.RTC_OK
##
#
# The state update action that is invoked after onExecute() action
# no corresponding operation exists in OpenRTm-aist-0.2.0
#
# @param ec_id target ExecutionContext Id
#
# @return RTC::ReturnCode_t
#
#
# def onStateUpdate(self, ec_id):
#
# return RTC.RTC_OK
##
#
# The action that is invoked when execution context's rate is changed
# no corresponding operation exists in OpenRTm-aist-0.2.0
#
# @param ec_id target ExecutionContext Id
#
# @return RTC::ReturnCode_t
#
#
# def onRateChanged(self, ec_id):
#
# return RTC.RTC_OK
def CraneX7ControllerRTCInit(manager):
profile = OpenRTM_aist.Properties(defaults_str=cranex7controllerrtc_spec)
manager.registerFactory(profile,
CraneX7ControllerRTC,
OpenRTM_aist.Delete)
def MyModuleInit(manager):
CraneX7ControllerRTCInit(manager)
# Create a component
manager.createComponent("CraneX7ControllerRTC")
def main():
mgr = OpenRTM_aist.Manager.init(sys.argv)
mgr.setModuleInitProc(MyModuleInit)
mgr.activateManager()
mgr.runManager()
if __name__ == "__main__":
main()