File size: 4,558 Bytes
ef17c16
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# Start dVRK system

import os, sys, ctypes

# Set RTLD_GLOBAL flag for dynamic loading (on Linux)
try:
   flags = sys.getdlopenflags()
   sys.setdlopenflags(flags | ctypes.RTLD_GLOBAL)
except AttributeError as e:
    print('Skipping dlopen flags, ' + str(e))

import cisstCommonPython as cisstCommon

# Set up cisst logging system to print errors, warnings, and verbose (but not debug)
cisstCommon.cmnLogger.SetMask(cisstCommon.CMN_LOG_ALLOW_VERBOSE)
cisstCommon.cmnLogger.SetMaskFunction(cisstCommon.CMN_LOG_ALLOW_VERBOSE)
cisstCommon.cmnLogger.SetMaskDefaultLog(cisstCommon.CMN_LOG_ALLOW_VERBOSE)
cisstCommon.cmnLogger.AddChannelToStdOut(cisstCommon.CMN_LOG_ALLOW_ERRORS_AND_WARNINGS)

def log():
   os.system('tail cisstLog.txt')

import cisstMultiTaskPython as cisstMultiTask
import cisstParameterTypesPython as cisstParameterTypes
import numpy

# Create a dictionary for dVRK commands, organized by interfaces
# (i.e., this is a dictionary of dictionaries)
dvrk = dict()

def dvrk_list(req_key = ''):
    def dvrk_list_inner(cur_dict, indent, req_key=''):
        for key, value in cur_dict.items():
            if not req_key or (key == req_key):
                if isinstance(value, dict):
                    print(indent+key+'/')
                    dvrk_list_inner(value, indent+'    ', '')
                elif isinstance(value, cisstMultiTask.mtsInterfaceRequiredPython):
                    print(indent+key+'/')
                    dvrk_list_inner(value.__dict__, indent+'    ', '')
                else:
                    print(indent+key)
    dvrk_list_inner(dvrk, '', req_key)

# Set up the system interface and add commands to the dvrk dictionary
def SetupSystem(serverName):
    system = cisstMultiTask.mtsCreateClientInterface('dvrkClient', serverName, 'Main')
    print('Connecting internal required interfaces')
    system.connect()
    system_dict = dict()
    for command in dir(system):
       # Ignore commands that start with '_' or 'this'
       if not command.startswith('_') and not command.startswith('this'):
          system_dict[command] = getattr(system, command)
    dvrk['system'] = system_dict
    return system

# Find arm components, add interfaces, and add commands to dvrk dictionary
def SetupArms(components):
    for comp in components:
        if comp.startswith('MTM') or comp.startswith('PSM') or comp.startswith('ECM'):
            obj = LCM.GetComponent(comp)
            provInterfaces = obj.GetNamesOfInterfacesProvided()
            for prov in provInterfaces:
                if (prov == 'Controller') or (prov == 'Arm'):
                    print('Found ' + comp)
                    comp_no_dash = comp.replace('-', '_')
                    interface = cisstMultiTask.mtsCreateClientInterface(comp_no_dash+'Client', comp, prov)
                    setattr(sys.modules[__name__], comp_no_dash, interface)
                    print('Type dir(' + comp_no_dash + ') to see available commands.')
                    arm_dict = dict()
                    for command in dir(interface):
                        # Ignore commands that start with '_' or 'this'
                        if not command.startswith('_') and not command.startswith('this'):
                            arm_dict[command] = getattr(interface, command)
                    dvrk[comp] = arm_dict
                    break

LCM = cisstMultiTask.mtsManagerLocal.GetInstance()

isEmbedded = False
if LCM.GetComponent('IRE'):
   print('Embedded Python configuration')
   isEmbedded = True

if isEmbedded:
   dvrkServer = LCM.GetComponent('system')
else:
    LCM.CreateAll()
    LCM.StartAll()

    dvrkServer = cisstMultiTask.mtsLoadAndCreateServer('sawIntuitiveResearchKit',
                                                       'dvrk_system',
                                                       'dvrkServer', '')

    if dvrkServer:
        print('Configuring dVRK server.')
        # Python2 uses raw_input and Python3 uses input
        try:
            configFile = raw_input('Enter config filename (JSON): ')
        except NameError:
            configFile = input('Enter config filename (JSON): ')
        dvrkServer.Configure(configFile)

if dvrkServer:
    # Setup up system component, which will create other components,
    # including the arms (MTM, PSM, ECM)
    system = SetupSystem(dvrkServer.GetName())
    # Now, look for the arm components (MTM, PSM, ECM)
    SetupArms(LCM.GetNamesOfComponents())

if not isEmbedded:
    LCM.CreateAllAndWait(2.0)
    LCM.StartAllAndWait(2.0)

print('System ready. See dvrk dict.')