-
Notifications
You must be signed in to change notification settings - Fork 0
/
KINOVA_GEN3_Button_is_Base-Tool-Flipper.py
132 lines (119 loc) · 6.64 KB
/
KINOVA_GEN3_Button_is_Base-Tool-Flipper.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
"""
BSD 3-Clause License
Copyright (c) 2021, Félix Chenette-Stewart
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
"""
import sys #import libraries and other tools/utils
import os
import time
import threading
import usb.core
import usb.util
import utilities
from kortex_api.TCPTransport import TCPTransport
from kortex_api.RouterClient import RouterClient
from kortex_api.SessionManager import SessionManager
from kortex_api.autogen.client_stubs.BaseClientRpc import BaseClient
from kortex_api.autogen.client_stubs.BaseCyclicClientRpc import BaseCyclicClient
from kortex_api.autogen.messages import Base_pb2, BaseCyclic_pb2, Common_pb2
TIMEOUT_DURATION = 10 #max action wait time
def main():
dev = usb.core.find(idVendor=0x256f, idProduct=0xc635) #look for 3DConnexion SpaceMouse Compact
cfg = dev.get_active_configuration() #get configuration
intf = cfg[(0,0)]
ep = usb.util.find_descriptor(intf, custom_match = lambda e: usb.util.endpoint_direction(e.bEndpointAddress) == usb.util.ENDPOINT_IN)
reattach = False
if dev.is_kernel_driver_active(0): #attaches to device
reattach = True
dev.detach_kernel_driver(0)
ep_in = dev[0][(0,0)][0] #data
ep_out = dev[0][(0,0)][1]
sys.path.insert(0, os.path.join(os.path.dirname(__file__), ".."))
args = utilities.parseConnectionArguments() # Parse arguments
with utilities.DeviceConnection.createTcpConnection(args) as router: # Create connection and get router
base = BaseClient(router) # Create required services
reference = "BASE"
while 1:
data = dev.read(ep_in.bEndpointAddress, ep_in.bLength, 0) #reads SpaceMouse data
def CreateCommand(vel, axys, mode): # command template with vel, axys, mode values
command = Base_pb2.TwistCommand()
if reference == "BASE":
command.reference_frame = Base_pb2.CARTESIAN_REFERENCE_FRAME_BASE
elif reference == "TOOL":
command.reference_frame = Base_pb2.CARTESIAN_REFERENCE_FRAME_TOOL
command.duration = 0
twist = command.twist
if mode == "linear": #linear movements
if axys == "x":twist.linear_x = vel
if axys == "y":twist.linear_y = vel
if axys == "z":twist.linear_z = vel
if mode == "angular": #angular movements
if axys == "x":twist.angular_x = vel
if axys == "y":twist.angular_y = vel
if axys == "z":twist.angular_z = vel
base.SendTwistCommand(command)
base.Stop() #end of movement
def check_for_end_or_abort(e):
def check(notification, e = e):
print("EVENT : " + \
Base_pb2.ActionEvent.Name(notification.action_event))
if notification.action_event == Base_pb2.ACTION_END \
or notification.action_event == Base_pb2.ACTION_ABORT:
e.set()
return check
if data[0] == 1: # checks the data (T) and seperates the values (XYZ)
tx = data[1] + (data[2]*256)
ty = data[3] + (data[4]*256)
tz = data[5] + (data[6]*256)
if data[2] > 127:tx -= 65536 # If data is abnormal
if data[4] > 127:ty -= 65536
if data[6] > 127:tz -= 65536
print ("T: ", tx, ty, tz) #prints values
if tx >= 100: CreateCommand(2,"y","linear") #user input moves the robotic arm
if tx <= -100: CreateCommand(-2,"y","linear")
if ty >= 100: CreateCommand(2,"x","linear")
if ty <= -100: CreateCommand(-2,"x","linear")
if tz >= 100: CreateCommand(-2,"z","linear")
if tz <= -100: CreateCommand(2,"z","linear")
if data[0] == 2: # checks the data (R) and seperates the values (XYZ)
rx = data[1] + (data[2]*256)
ry = data[3] + (data[4]*256)
rz = data[5] + (data[6]*256)
if data[2] > 127:rx -= 65536 # If data is abnormal
if data[4] > 127:ry -= 65536
if data[6] > 127:rz -= 65536
print ("R: ", rx, ry, rz)
if rx >= 100: CreateCommand(100,"y","angular") #user input moves the robotic arm
if rx <= -100: CreateCommand(-100,"y","angular")
if ry >= 100: CreateCommand(100,"x","angular")
if ry <= -100: CreateCommand(-100,"x","angular")
if rz >= 100: CreateCommand(100,"z","angular")
if rz <= -100: CreateCommand(-100,"z","angular")
if data[0] == 3 and data[1] == 0: #side button Left, changes reference mode, flipper
if reference == "BASE": #BASE by default
reference = "TOOL"
reference
elif reference == "TOOL":
reference = "BASE"
if __name__ == "__main__":
main()