-
Notifications
You must be signed in to change notification settings - Fork 7
/
DriveEngine.groovy
147 lines (139 loc) · 5.57 KB
/
DriveEngine.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
//Your code here
import com.neuronrobotics.sdk.addons.kinematics.AbstractLink
import com.neuronrobotics.sdk.addons.kinematics.DHLink
import com.neuronrobotics.sdk.addons.kinematics.DHParameterKinematics
import com.neuronrobotics.sdk.addons.kinematics.LinkConfiguration
import com.neuronrobotics.sdk.addons.kinematics.MobileBase
import com.neuronrobotics.sdk.addons.kinematics.math.TransformNR
import Jama.Matrix;
import javafx.scene.transform.*;
return new com.neuronrobotics.sdk.addons.kinematics.IDriveEngine (){
public HashMap<DHParameterKinematics,MobileBase> getAllDHChains(MobileBase source) {
HashMap<DHParameterKinematics,MobileBase> copy = new HashMap<>();
copy.putAll(getSteerable(source))
copy.putAll(getDrivable(source))
return copy;
}
public HashMap<DHParameterKinematics,MobileBase> getSteerable(MobileBase source) {
HashMap<DHParameterKinematics,MobileBase> copy = new HashMap<>();
for(def k:source.getSteerable())
copy.put(k,source);
for(DHParameterKinematics k:source.getAllDHChains()) {
for(int i=0;i<k.getNumberOfLinks();i++) {
if(k.getFollowerMobileBase(i)!=null)
copy.putAll(getSteerable(k.getFollowerMobileBase(i)))
}
}
return copy;
}
public HashMap<DHParameterKinematics,MobileBase> getDrivable(MobileBase source) {
HashMap<DHParameterKinematics,MobileBase> copy = new HashMap<>();
for(def k:source.getDrivable())
copy.put(k,source);
for(DHParameterKinematics k:source.getAllDHChains()) {
for(int i=0;i<k.getNumberOfLinks();i++) {
if(k.getFollowerMobileBase(i)!=null)
copy.putAll(getDrivable(k.getFollowerMobileBase(i)))
}
}
return copy;
}
@Override
public void DriveArc(MobileBase ogSource, TransformNR newPose, double seconds) {
newPose = newPose.inverse()
HashMap<DHParameterKinematics,MobileBase> wheels = getAllDHChains( ogSource)
HashMap<DHParameterKinematics,MobileBase> steerable = getSteerable(ogSource);
//println "\n\n"
for(DHParameterKinematics LimbWithWheel:wheels.keySet()){
MobileBase wheelSource=wheels.get(LimbWithWheel);
// Get the current pose of the robots base
TransformNR global= ogSource.getFiducialToGlobalTransform();
// set a new one if null
if(global==null){
global=new TransformNR()
ogSource.setGlobalToFiducialTransform(global)
}
global=global.times(newPose);// new global pose
int wheelIndex =LimbWithWheel.getNumberOfLinks()>=3?1:0;
ArrayList<TransformNR> tipChain = LimbWithWheel.getChain().getChain(LimbWithWheel.getCurrentJointSpaceTarget())
// get the pose of this wheel
TransformNR wheelStarting;
if(wheelIndex==0)
wheelStarting = wheelSource.forwardOffset(LimbWithWheel.getRobotToFiducialTransform());
else
wheelStarting= wheelSource.forwardOffset(tipChain.get(LimbWithWheel.getNumberOfLinks()-3))
Matrix btt = wheelStarting.getMatrixTransform();
Matrix ftb = global.getMatrixTransform();// our new target
Matrix mForward = ftb.times(btt)
TransformNR inc =new TransformNR( mForward);// this wheels new increment
//println thisWheel.getScriptingName()+" pose "+wheelStarting.getX()+" "+wheelStarting.getY()+" "+wheelStarting.getZ()
TransformNR vect =new TransformNR(btt.inverse().times(mForward));// this wheels new increment
double xyplaneDistance = Math.sqrt(
Math.pow(vect.getX(),2)+
Math.pow(vect.getY(),2)
)
if(Math.abs(xyplaneDistance)<0.01){
xyplaneDistance=0;
}
double steer =90-Math.toDegrees( Math.atan2(vect.getX(),vect.getY()))
boolean reverseWheel = false
if(steer>90){
steer=steer-180
reverseWheel=true;
}
if(steer<-90){
steer=steer+180
reverseWheel=true;
}
ArrayList<DHLink> dhLinks = LimbWithWheel.getChain().getLinks()
if(steerable.get(LimbWithWheel)!=null){
//println "\n\n"+i+" XY plane distance "+xyplaneDistance
//println LimbWithWheel.getScriptingName()+" Steer angle "+steer
try{
double[] joints=LimbWithWheel.getCurrentJointSpaceVector();
double delta = joints[wheelIndex]-steer;
joints[wheelIndex]=steer;
double bestTime = LimbWithWheel.getBestTime(joints)
// if(delta>1)
// println "Speed for steering link "+(delta/bestTime)+" degrees per second"
LimbWithWheel.setDesiredJointAxisValue(wheelIndex,steer,bestTime)
}catch(Exception e){
e.printStackTrace(System.out)
}
wheelIndex+=1;
}else {
}
DHLink dh = dhLinks.get(wheelIndex)
// Hardware to engineering units configuration
LinkConfiguration conf = LimbWithWheel.getLinkConfiguration(wheelIndex);
// Engineering units to kinematics link (limits and hardware type abstraction)
AbstractLink abstractLink = LimbWithWheel.getAbstractLink(wheelIndex);// Transform used by the UI to render the location of the object
// Transform used by the UI to render the location of the object
Affine manipulator = dh.getListener();
double radiusOfWheel = dh.getR()
double theta=0
if(Math.abs(xyplaneDistance)>0.01){
theta=Math.toDegrees(xyplaneDistance/radiusOfWheel)*(reverseWheel?-1:1)
}
double[] currVal=LimbWithWheel.getCurrentJointSpaceVector();
try{
currVal[wheelIndex]+=theta
double best = LimbWithWheel.getBestTime(currVal);
if(best>seconds)
seconds=best;
LimbWithWheel.setDesiredJointAxisValue(wheelIndex,currVal[wheelIndex],seconds);
}catch(Exception e){
e.printStackTrace(System.out)
}
}
}
@Override
public void DriveVelocityStraight(MobileBase source, double cmPerSecond) {
// TODO Auto-generated method stub
}
@Override
public void DriveVelocityArc(MobileBase source, double degreesPerSecond,
double cmRadius) {
// TODO Auto-generated method stub
}
}