-
Notifications
You must be signed in to change notification settings - Fork 0
/
jinputGrogu.groovy
286 lines (261 loc) · 8.16 KB
/
jinputGrogu.groovy
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
import com.neuronrobotics.bowlerstudio.BowlerStudio
import com.neuronrobotics.bowlerstudio.assets.ConfigurationDatabase
import com.neuronrobotics.bowlerstudio.creature.MobileBaseLoader
import com.neuronrobotics.bowlerstudio.scripting.ScriptingEngine
import com.neuronrobotics.sdk.addons.gamepad.BowlerJInputDevice
import com.neuronrobotics.sdk.addons.gamepad.IGameControlEvent
import com.neuronrobotics.sdk.addons.kinematics.AbstractLink
import com.neuronrobotics.sdk.addons.kinematics.DHParameterKinematics
import com.neuronrobotics.sdk.addons.kinematics.MobileBase
import com.neuronrobotics.sdk.addons.kinematics.math.RotationNR
import com.neuronrobotics.sdk.addons.kinematics.math.TransformNR
import com.neuronrobotics.sdk.common.DeviceManager
import com.neuronrobotics.sdk.util.ThreadUtil
import javax.sound.sampled.AudioInputStream
import javax.sound.sampled.AudioSystem
import javax.sound.sampled.Clip
import javax.sound.sampled.FloatControl
ScriptingEngine.pull("https://github.com/Halloween2020TheChild/GroguMechanicsCad.git")
ScriptingEngine.pull("https://github.com/madhephaestus/6dofServoArm.git")
MobileBase base=DeviceManager.getSpecificDevice( "Standard6dof",{
//If the device does not exist, prompt for the connection
MobileBase m = MobileBaseLoader.fromGit(
"https://github.com/Halloween2020TheChild/GroguMechanicsCad.git",
"hephaestus.xml"
)
if(m==null)
throw new RuntimeException("Arm failed to assemble itself")
println "Connecting new device robot arm "+m
return m
})
println base
List<String> gameControllerNames = ConfigurationDatabase.getObject("katapult", "gameControllerNames", [
"Dragon",
"X-Box",
"Game",
"Switch"
])
//Check if the device already exists in the device Manager
BowlerJInputDevice g=DeviceManager.getSpecificDevice("gamepad",{
def t = new BowlerJInputDevice(gameControllerNames); //
t.connect(); // Connect to it.
return t
})
float x =0;
float straif=0;
float rz=0;
float ljud =0;
float trigButton=0;
float trigAnalog=0;
float tilt=0;
float trig=0
long timeOfLastCommand = System.currentTimeMillis()
IGameControlEvent listener = new IGameControlEvent() {
@Override public void onEvent(String name,float value) {
timeOfLastCommand = System.currentTimeMillis()
if(name.contentEquals("l-joy-left-right")){
straif=value;
}
else if(name.contentEquals("r-joy-up-down")){
x=-value;
}
else if(name.contentEquals("l-joy-up-down")){
ljud=value;
}
else if(name.contentEquals("r-joy-left-right")){
rz=value;
}else if(name.contentEquals("analog-trig")){
trigAnalog=(value+1)/2-0.5;
trig=(trigAnalog*40)
println trig
}else if(name.contentEquals("z")){
trigButton=value/2.0+0.5;
}
else if(name.contentEquals("x-mode")){
if(value>0) {
}
}else if(name.contentEquals("r-trig-button")){
if(value>0) {
tilt=1;
}else
tilt=0;
}
else if(name.contentEquals("l-trig-button")){
if(value>0) {
tilt=-1;
}else
tilt=0;
}
else if(name.contentEquals("y-mode")){
if(value>0) {
}
}
//System.out.println(name+" is value= "+value);
}
}
g.clearListeners()
Log.enableSystemPrint(true)
g.addListeners(listener);
long msAttempted = 30
long msActual=msAttempted
def fixVector(double[] jointSpaceVect,DHParameterKinematics arm ) {
for (int i = 0; i < 6; i++) {
AbstractLink link = arm.factory.getLink(arm.getLinkConfiguration(i));
double val = jointSpaceVect[i];
Double double1 = new Double(val);
if(double1.isNaN() ||double1.isInfinite() ) {
jointSpaceVect[i]=0;
}
if (val > link.getMaxEngineeringUnits()) {
jointSpaceVect[i]=link.getMaxEngineeringUnits()-Double.MIN_VALUE;
//println "Link "+i+" u-limit "+jointSpaceVect[i]
}
if (val < link.getMinEngineeringUnits()) {
jointSpaceVect[i]=link.getMinEngineeringUnits()+Double.MIN_VALUE;
//println "Link "+i+" l-limit "+jointSpaceVect[i]
}
}
}
Thread meowThread = null
audioStream = AudioSystem.getAudioInputStream( ScriptingEngine
.fileFromGit(
"https://github.com/Halloween2020TheChild/RazerHydraHIDJava.git",//git repo URL
"master",//branch
"meow.wav"// File from within the Git repo
))
clip = AudioSystem.getClip();
clip.open(audioStream);
//gainControl = (FloatControl) clip.getControl(FloatControl.Type.MASTER_GAIN);
def meow() {
Clip audioClip = clip
try
{
//float gainValue = (((float) config.volume()) * 40f / 100f) - 35f;
//gainControl.setValue(gainValue);
audioClip.setFramePosition(0);
audioClip.start();
ThreadUtil.wait(10);
double len =(double) audioClip.getMicrosecondLength()
try{
while(audioClip.isRunning()&& !Thread.interrupted()){
double pos =(double) (audioClip.getMicrosecondPosition())
def percent = len/pos
System.out.println("Current "+pos +" Percent = "+percent+" ");
ThreadUtil.wait(5);
}
println "Done!"
}catch(Throwable t){
//BowlerStudio.printStackTrace(t)
t.printStackTrace(System.out)
}
println "stopping..."
audioClip.stop()
}
catch (Exception e)
{
e.printStackTrace(System.out)
//BowlerStudio.printStackTrace(e)
return null;
}
println "Returning"
}
println "Starting code"
try{
def lasttrig=0;
while(!Thread.interrupted() ){
Thread.sleep(msAttempted)
TransformNR changed=new TransformNR()
changed.setX(170+(x*30))
def headRnage=30
def analogy = -straif*70
def analogz = -ljud*35
changed.setZ(200+analogz)
changed.setY(analogy)
def analogup = -rz*headRnage *1.5
double tiltTarget = tilt*-15
changed.setRotation(new RotationNR(0,179.96+analogup,-50.79))
//TransformNR tilted= new TransformNR(0,0,0, RotationNR.getRotationZ(-90))
TransformNR tilted= new TransformNR(0,0,0, RotationNR.getRotationZ(-90 ))
changed=changed.times(tilted).times(new TransformNR(0,0,0, new RotationNR(0,-tiltTarget,tiltTarget)))
DHParameterKinematics arm = base.getAllDHChains().get(0)
if(trig>0)
trig=-trig
try {
double[] jointSpaceVect = arm.inverseKinematics(arm.inverseOffset(changed));
fixVector(jointSpaceVect,arm)
double bestsecs = arm.getBestTime(jointSpaceVect);
double normalsecs = ((double)msAttempted)/1000.0
def vect;
if(bestsecs>normalsecs) {
double percentpossible = normalsecs/bestsecs*2
TransformNR starttr=arm.getCurrentTaskSpaceTransform()
TransformNR delta = starttr.inverse().times(changed);
TransformNR scaled = delta.scale(percentpossible)
TransformNR newTR= starttr.times(scaled)
vect = arm.inverseKinematics(arm.inverseOffset(newTR));
fixVector(vect,arm)
TransformNR finaltr= arm.forwardOffset( arm.forwardKinematics(vect))
if(!arm.checkTaskSpaceTransform(finaltr)) {
// println "\n\npercentage "+percentpossible
// println "Speed capped\t"+jointSpaceVect
// println "to\t\t\t"+vect
// println "changed"+changed
// println "starttr"+starttr
// println "delta"+delta
// println "scaled"+scaled
// println "newTR"+newTR
// println "ERROR, cant get to "+newTR
//continue;
}
}else
vect = jointSpaceVect
msActual=normalsecs*1000
try {
//vect[6]=trig;
}catch(Throwable t) {
//BowlerStudio.printStackTrace(t)
}
arm.setDesiredJointSpaceVector(vect, normalsecs);
}catch(Throwable t) {
t.printStackTrace(System.out)
//arm.setDesiredJointAxisValue(6, trig, 0)
}
MobileBase head = arm.getDhLink(5).getSlaveMobileBase()
//println head
DHParameterKinematics mouth=head.getAllDHChains().get(0)
//println mouth
if(trig<-10 && lasttrig>-10) {
println "Mouthing"
if(meowThread==null||!meowThread.isAlive()) {
meowThread=new Thread() {
public void run() {
println "MEOW"
meow()
interrupt()
meowThread=null;
println "Meow Thread Exit"
}
}
meowThread.start()
}
}
//println trig
lasttrig=trig;
//println "Mouth ="+trig
try {
mouth.setDesiredJointAxisValue(0, trig, 0)
}catch(Exception ex) {
//println "ERROR Mouth ="+trig+" "+ex.getLocalizedMessage()
//BowlerStudio.printStackTrace(ex)
}
}
}catch(java.lang.InterruptedException ex) {
//exit sig
}catch(Throwable t){
t.printStackTrace(System.out)
}
//remove listener and exit
g.removeListeners(listener);
Clip audioClip = clip
audioClip.close()
((AudioInputStream)audioStream).close()