-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhidLinkLoader.groovy
More file actions
266 lines (227 loc) · 7.36 KB
/
hidLinkLoader.groovy
File metadata and controls
266 lines (227 loc) · 7.36 KB
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
@GrabResolver(name='nr', root='https://oss.sonatype.org/service/local/repositories/releases/content/')
@GrabResolver(name='mvnRepository', root='https://repo1.maven.org/maven2/')
@Grab(group='net.java.dev.jna', module='jna', version='4.2.2')
@Grab(group='com.neuronrobotics', module='SimplePacketComsJava', version='0.12.0')
@Grab(group='com.neuronrobotics', module='SimplePacketComsJava-HID', version='0.13.0')
@Grab(group='org.hid4java', module='hid4java', version='0.5.0')
import edu.wpi.SimplePacketComs.*;
import edu.wpi.SimplePacketComs.phy.*;
import com.neuronrobotics.bowlerstudio.creature.MobileBaseLoader
import com.neuronrobotics.sdk.addons.kinematics.AbstractLink
import com.neuronrobotics.sdk.addons.kinematics.AbstractRotoryLink
import com.neuronrobotics.sdk.addons.kinematics.INewLinkProvider
import com.neuronrobotics.sdk.addons.kinematics.LinkConfiguration
import com.neuronrobotics.sdk.addons.kinematics.LinkFactory
import com.neuronrobotics.sdk.addons.kinematics.MobileBase
import com.neuronrobotics.sdk.addons.kinematics.imu.*;
import com.neuronrobotics.sdk.common.BowlerAbstractDevice
import com.neuronrobotics.sdk.common.DeviceManager
import com.neuronrobotics.sdk.common.IDeviceAddedListener
import com.neuronrobotics.sdk.common.NonBowlerDevice
import edu.wpi.SimplePacketComs.BytePacketType;
import edu.wpi.SimplePacketComs.FloatPacketType;
import edu.wpi.SimplePacketComs.*;
import edu.wpi.SimplePacketComs.phy.UDPSimplePacketComs;
import edu.wpi.SimplePacketComs.device.gameController.*;
import edu.wpi.SimplePacketComs.device.*
public class SimpleServoHID extends HIDSimplePacketComs {
private PacketType servos = new edu.wpi.SimplePacketComs.BytePacketType(1962, 64);
private PacketType imuData = new edu.wpi.SimplePacketComs.FloatPacketType(1804, 64);
private final double[] status = new double[12];
private final byte[] data = new byte[16];
public SimpleServoHID(int vidIn, int pidIn) {
super(vidIn, pidIn);
servos.waitToSendMode();
addPollingPacket(servos);
addPollingPacket(imuData);
addEvent(1962, {
writeBytes(1962, data);
});
addEvent(1804, {
readFloats(1804,status);
});
}
public double[] getImuData() {
return status;
}
public byte[] getData() {
return data;
}
public byte[] getDataUp() {
return servos.getUpstream();
}
}
public class HIDSimpleComsDevice extends NonBowlerDevice{
AbstractSimpleComsDevice simple;
AbstractSimpleComsDevice simpleServo;
public HIDSimpleComsDevice(def simp, def servo){
simple = simp
simpleServo=servo
setScriptingName("hidbowler")
}
@Override
public void disconnectDeviceImp(){
simple.disconnect()
simpleServo.disconnect()
println "HID device Termination signel shutdown"
}
@Override
public boolean connectDeviceImp(){
simple.connect()
simpleServo.connect()
}
void setValue(int i,int position){
if(position<0){
println "Link commanded to invalid position! "+position
position=0;
}
if(position>180){
println "Link commanded to invalid position! "+position
position=180;
}
simpleServo.getData()[i]=(byte)position;
simpleServo.servos.pollingMode();
}
int getValue(int i){
if(simpleServo.getDataUp()[i]>=0)
return simpleServo.getDataUp()[i]
return ((int)simpleServo.getDataUp()[i])+256
}
public float[] getImuData() {
return simple.getImuData();
}
@Override
public ArrayList<String> getNamespacesImp(){
// no namespaces on dummy
return [];
}
}
public class HIDRotoryLink extends AbstractRotoryLink{
HIDSimpleComsDevice device;
int index =0;
int lastPushedVal = 0;
private static final Integer command =1962
/**
* Instantiates a new HID rotory link.
*
* @param c the c
* @param conf the conf
*/
public HIDRotoryLink(HIDSimpleComsDevice c,LinkConfiguration conf,String devName) {
super(conf);
index = conf.getHardwareIndex()
device=c
if(device ==null)
throw new RuntimeException("Device can not be null")
conf.setDeviceScriptingName(devName)
c.simpleServo.addEvent(command,{
int val= getCurrentPosition();
if(lastPushedVal!=val){
//println "Fire Link Listner "+index+" value "+getCurrentPosition()
try{
fireLinkListener(val);
}catch(Exception e){}
lastPushedVal=val
}else{
//println index+" value same "+getCurrentPosition()
}
})
}
/* (non-Javadoc)
* @see com.neuronrobotics.sdk.addons.kinematics.AbstractLink#cacheTargetValueDevice()
*/
@Override
public void cacheTargetValueDevice() {
device.setValue(index,(int)getTargetValue())
}
/* (non-Javadoc)
* @see com.neuronrobotics.sdk.addons.kinematics.AbstractLink#flush(double)
*/
@Override
public void flushDevice(double time) {
// auto flushing
}
/* (non-Javadoc)
* @see com.neuronrobotics.sdk.addons.kinematics.AbstractLink#flushAll(double)
*/
@Override
public void flushAllDevice(double time) {
// auto flushing
}
/* (non-Javadoc)
* @see com.neuronrobotics.sdk.addons.kinematics.AbstractLink#getCurrentPosition()
*/
@Override
public double getCurrentPosition() {
return (double)device.getValue(index);
}
}
class MyDeviceListener implements IDeviceAddedListener{
String searchName
public MyDeviceListener(String searchName){
this.searchName=searchName
}
public void onNewDeviceAdded(BowlerAbstractDevice bad) {
// wait for the mobile base to be added to the device manager then add the event listenr for the IMU
if(MobileBase.class.isInstance(bad)) {
println "Adding the IMU callback from the device manager listener"
HIDSimpleComsDevice d = DeviceManager.getSpecificDevice( searchName)
MobileBase m =(MobileBase)bad;
d.simple.addEvent(1804, {
double[] imuDataValues = d.simple.getImuData()
m.getImu()
.setHardwareState(
new IMUUpdate(
-imuDataValues[9], -imuDataValues[11], -imuDataValues[10],
imuDataValues[3],//Double rotxAcceleration,
imuDataValues[4],//Double rotyAcceleration,
imuDataValues[5],//Double rotzAcceleration
System.currentTimeMillis()
))
});
DeviceManager.removeDeviceAddedListener(this);
}
}
public void onDeviceRemoved(BowlerAbstractDevice bad) {
// TODO Auto-generated method stub
}
}
INewLinkProvider provider= new INewLinkProvider() {
public AbstractLink generate(LinkConfiguration conf) {
String searchName = conf.getDeviceScriptingName();
int vid=0x16C0
int pid=0x0486
if(searchName.size()>8){
String deviceID = searchName.substring(searchName.size()-8,searchName.size())
String VIDStr = deviceID.substring(0,4)
String PIDStr = deviceID.substring(4,8)
try{
vid = Integer.parseInt(VIDStr,16);
pid = Integer.parseInt(PIDStr,16);
//println "Searching for Device at "+VIDStr+" "+PIDStr
}catch(Throwable t){
BowlerStudio.printStackTrace(t)
}
}
def dev = DeviceManager.getSpecificDevice( searchName,{
//If the device does not exist, prompt for the connection
def simp = null;
def srv = null
simp = new SimpleServoHID(vid ,pid)
srv=simp
HIDSimpleComsDevice d = new HIDSimpleComsDevice(simp,srv)
d.connect(); // Connect to it.
if(simp.isVirtual()){
println "\n\n\nDevice is in virtual mode!\n\n\n"
}
println "Connecting new device: "+searchName
DeviceManager.addDeviceAddedListener(new MyDeviceListener(searchName))
return d
})
return new HIDRotoryLink(dev,conf,searchName);
}
}
if(args==null)
args=["hidSimplePacketLink"]
LinkFactory.addLinkProvider(args[0], provider)
return provider