-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathSolidWorks.lua
190 lines (178 loc) · 4.83 KB
/
SolidWorks.lua
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
scriptId = 'com.thalmic.solidworks'
--Commands
--"waveIn" reset to front view
--"waveOut" reset to back view
--"fingersSpread" reset to top view
--"thumbToPinky" toggle unlock
--hold fist and move left, right, up and down to rotate
--hold fist and turn cw and ccw to zoom
--LOAD SCRIPT BEFORE DOING SETUP GESTURE TO MAKE SURE XPOS WORKS
PITCH_MOTION_THRESHOLD = 7 -- degrees
YAW_MOTION_THRESHOLD = 6 -- degrees
ROLL_MOTION_THRESHOLD = 7 -- degrees
SLOW_MOVE_PERIOD = 50
--Helper Functions
function getMyoYawDegrees()
local yawValue = math.deg(myo.getYaw())
return yawValue
end
function getMyoPitchDegrees()
local PitchValue = math.deg(myo.getPitch())
return PitchValue
end
function getMyoRollDegrees()
local RollValue = math.deg(myo.getRoll())
return RollValue
end
function degreeDiff(value, base)
local diff = value - base
if diff > 180 then
diff = diff - 360
elseif diff < -180 then
diff = diff + 360
end
return diff
end
function conditionalPitch(pitch)
if myo.getXDirection()== "towardElbow" then
pitch=-pitch;
end
return pitch
end
function conditionalRoll(roll)
if myo.getXDirection()== "towardElbow" then
roll=-roll;
end
return roll
end
function conditionallySwapWave(pose)
if myo.getArm() == "left" then
if pose == "waveIn" then
pose = "waveOut"
elseif pose == "waveOut" then
pose = "waveIn"
end
end
return pose
end
--Control Functions
function zoomIn()
myo.keyboard("z", "press","shift")
end
function zoomOut()
myo.keyboard("z", "press")
end
function moveLeft()
myo.keyboard("left_arrow", "press")
end
function moveRight()
myo.keyboard("right_arrow", "press")
end
function moveUp()
myo.keyboard("up_arrow", "press")
end
function moveDown()
myo.keyboard("down_arrow", "press")
end
function resetToTop()
myo.keyboard("5", "press","control")
end
function resetToFront()
myo.keyboard("1", "press","control")
end
function resetToBack()
myo.keyboard("2", "press","control")
end
--Not Used
function resetToIso()
myo.keyboard("7", "press","control")
end
--Toggle Unlock Functions
function lock()
enabled = false
myo.vibrate("short")
end
function unlock()
enabled = true
myo.vibrate("short")
myo.vibrate("short")
end
function onPoseEdge(pose, edge)
pose=conditionallySwapWave(pose)
local now = myo.getTimeMilliseconds()
--Hold to move activation
if pose == "fist" and enabled then
moveActive = edge == "on"
yawReference = getMyoYawDegrees()
pitchReference = getMyoPitchDegrees()
rollReference = getMyoRollDegrees()
moveSince = now
end
--Other shortcut control
if edge == "on" then
if pose == "waveIn" and enabled then
resetToFront()
elseif pose == "waveOut" and enabled then
resetToBack()
elseif pose == "fingersSpread" and enabled then
resetToTop()
elseif pose == "thumbToPinky" then
if enabled then
lock()
else
unlock()
end
end
end
end
-- onPeriodic runs every ~10ms
function onPeriodic()
local now = myo.getTimeMilliseconds()
if moveActive then
local relativeYaw = degreeDiff(getMyoYawDegrees(), yawReference)
if math.abs(relativeYaw)> YAW_MOTION_THRESHOLD then
if now - moveSince > SLOW_MOVE_PERIOD then
if relativeYaw>0 then
moveLeft()
else
moveRight()
end
moveSince = now
end
end
local relativePitch = degreeDiff(getMyoPitchDegrees(), pitchReference)
relativePitch=conditionalPitch(relativePitch)
if math.abs(relativePitch)> PITCH_MOTION_THRESHOLD then
if now - moveSince > SLOW_MOVE_PERIOD then
if myo.getXDirection()== "towardElbow" then
relativePitch=-relativePitch;
end
if relativePitch>0 then
moveDown()
else
moveUp()
end
moveSince = now
end
end
local relativeRoll = degreeDiff(getMyoRollDegrees(), rollReference)
relativeRoll=conditionalRoll(relativeRoll)
if math.abs(relativeRoll)> ROLL_MOTION_THRESHOLD then
if now - moveSince > SLOW_MOVE_PERIOD then
if relativeRoll>0 then
zoomIn()
else
zoomOut()
end
moveSince = now
end
end
end
end
-- Only activate when using SolidWorks
function onForegroundWindowChange(app, title)
enabled = true
if string.match(title, "SolidWorks") then
return true
end
end