1+ package us .ihmc .externalControl ;
2+
3+ import org .ejml .data .DMatrixRMaj ;
4+ import us .ihmc .euclid .referenceFrame .FramePose3D ;
5+ import us .ihmc .euclid .referenceFrame .FrameVector3D ;
6+ import us .ihmc .euclid .referenceFrame .ReferenceFrame ;
7+ import us .ihmc .mecano .multiBodySystem .interfaces .OneDoFJointReadOnly ;
8+ import us .ihmc .mecano .multiBodySystem .interfaces .RigidBodyBasics ;
9+ import us .ihmc .robotics .robotSide .RobotSide ;
10+ import us .ihmc .robotics .robotSide .SideDependentList ;
11+ import us .ihmc .sensorProcessing .outputData .JointDesiredOutputBasics ;
12+
13+ import java .util .HashMap ;
14+
15+ public class ExternalControl
16+ {
17+ private final ExternalControlWrapper .ExternalControlImpl externalControlImpl ;
18+ private final DMatrixRMaj robotState ;
19+ private final DMatrixRMaj robotControl ;
20+ private final DMatrixRMaj feetPositions ;
21+ private boolean leftInContact ;
22+ private boolean rightInContact ;
23+ private final DMatrixRMaj solutionRobotState ;
24+ private final DMatrixRMaj solutionTorqueVector ;
25+ private final DMatrixRMaj solutionStiffnessVector ;
26+ private final DMatrixRMaj solutionDampingVector ;
27+ private final RigidBodyBasics baseBody ;
28+ private final OneDoFJointReadOnly [] joints ;
29+ private final FramePose3D basePose = new FramePose3D ();
30+ private final FrameVector3D tempVector = new FrameVector3D ();
31+ private final FrameVector3D tempPoint = new FrameVector3D ();
32+ private final FramePose3D solutionBasePose = new FramePose3D ();
33+ private final HashMap <OneDoFJointReadOnly , SolutionJointData > solutionJointData = new HashMap <>();
34+
35+ public ExternalControl (RigidBodyBasics baseBody , OneDoFJointReadOnly [] joints , double defaultStiffness , double defaultDamping )
36+ {
37+ this .baseBody = baseBody ;
38+ this .joints = joints ;
39+ robotState = new DMatrixRMaj (2 * joints .length + 13 , 1 );
40+ robotControl = new DMatrixRMaj (joints .length , 1 );
41+ feetPositions = new DMatrixRMaj (6 , 1 );
42+ solutionRobotState = new DMatrixRMaj (2 * joints .length + 13 , 1 );
43+ solutionTorqueVector = new DMatrixRMaj (joints .length , 1 );
44+ solutionStiffnessVector = new DMatrixRMaj (joints .length , 1 );
45+ solutionDampingVector = new DMatrixRMaj (joints .length , 1 );
46+ for (OneDoFJointReadOnly joint : joints )
47+ solutionJointData .put (joint , new SolutionJointData ());
48+ externalControlImpl = new ExternalControlWrapper .ExternalControlImpl (defaultStiffness , defaultDamping , joints .length );
49+ }
50+
51+ public void setFootStates (SideDependentList <? extends ReferenceFrame > soleFrames , boolean leftInContact , boolean rightInContact )
52+ {
53+ int start = 0 ;
54+ for (RobotSide robotSide : RobotSide .values )
55+ {
56+ tempPoint .setToZero (soleFrames .get (robotSide ));
57+ tempPoint .changeFrame (ReferenceFrame .getWorldFrame ());
58+ tempPoint .get (start , feetPositions );
59+ start += 3 ;
60+ }
61+ this .leftInContact = leftInContact ;
62+ this .rightInContact = rightInContact ;
63+ }
64+
65+ public void writeRobotState (double currentTime , int hardwareStatus )
66+ {
67+ setRobotState ();
68+ setRobotControl ();
69+ if (!externalControlImpl .updateRobotState (currentTime ,
70+ robotState .data ,
71+ robotState .getNumRows (),
72+ robotControl .data ,
73+ robotControl .getNumRows (),
74+ leftInContact ,
75+ rightInContact ,
76+ feetPositions .data ,
77+ feetPositions .getNumRows (),
78+ hardwareStatus ))
79+ throw new RuntimeException ("Failed to successfully write the hardware state across the barrier." );
80+ }
81+
82+ public void readControlSolution ()
83+ {
84+ if (!externalControlImpl .getSolution (solutionRobotState .data ,
85+ solutionRobotState .numRows ,
86+ solutionTorqueVector .data ,
87+ solutionTorqueVector .numRows ,
88+ solutionStiffnessVector .data ,
89+ solutionStiffnessVector .numRows ,
90+ solutionDampingVector .data ,
91+ solutionDampingVector .numRows ))
92+ {
93+ throw new RuntimeException ("Failed to retrieve solution data." );
94+ }
95+ solutionBasePose .getPosition ().set (solutionRobotState );
96+ solutionBasePose .getOrientation ().set (3 , 0 , solutionRobotState );
97+ int positionStart = 7 ;
98+ int velocityStart = 7 + 6 + joints .length ;
99+ for (int i = 0 ; i < joints .length ; i ++)
100+ {
101+ SolutionJointData data = solutionJointData .get (joints [i ]);
102+ data .desiredPosition = solutionRobotState .get (positionStart + i , 0 );
103+ data .desiredVelocity = solutionRobotState .get (velocityStart + i , 0 );
104+ data .torque = solutionTorqueVector .get (i , 0 );
105+ data .stiffness = solutionStiffnessVector .get (i , 0 );
106+ data .damping = solutionDampingVector .get (i , 0 );
107+ }
108+ }
109+
110+ public SolutionJointData getSolutionData (OneDoFJointReadOnly joint )
111+ {
112+ return solutionJointData .get (joint );
113+ }
114+
115+ private void setRobotState ()
116+ {
117+ basePose .setToZero (baseBody .getBodyFixedFrame ());
118+ basePose .changeFrame (ReferenceFrame .getWorldFrame ());
119+ // set the configuration state for the robot
120+ basePose .getPosition ().get (0 , robotState );
121+ basePose .getOrientation ().get (3 , robotState );
122+ int start = 7 ;
123+ for (int i = 0 ; i < joints .length ; i ++)
124+ {
125+ robotState .set (start + i , joints [i ].getQ ());
126+ }
127+ // set linear velocity in body frame
128+ start += joints .length ;
129+ tempVector .setIncludingFrame (baseBody .getBodyFixedFrame ().getTwistOfFrame ().getLinearPart ());
130+ tempVector .changeFrame (baseBody .getBodyFixedFrame ());
131+ tempVector .get (start , robotState );
132+ // set angular velocity in body frame
133+ start += 3 ;
134+ tempVector .setIncludingFrame (baseBody .getBodyFixedFrame ().getTwistOfFrame ().getAngularPart ());
135+ tempVector .changeFrame (baseBody .getBodyFixedFrame ());
136+ tempVector .get (start , robotState );
137+ // set joint velocity
138+ start += 3 ;
139+ for (int i = 0 ; i < joints .length ; i ++)
140+ {
141+ robotState .set (start + i , joints [i ].getQd ());
142+ }
143+ }
144+
145+ private void setRobotControl ()
146+ {
147+ for (int i = 0 ; i < joints .length ; i ++)
148+ {
149+ robotControl .set (i , joints [i ].getTau ());
150+ }
151+ }
152+
153+ public static class SolutionJointData
154+ {
155+ double desiredPosition ;
156+ double desiredVelocity ;
157+ double stiffness ;
158+ double damping ;
159+ double torque ;
160+
161+ public void getJointDesiredOutput (JointDesiredOutputBasics jointDesiredOutputToPack )
162+ {
163+ jointDesiredOutputToPack .setDesiredPosition (desiredPosition );
164+ jointDesiredOutputToPack .setDesiredVelocity (desiredVelocity );
165+ jointDesiredOutputToPack .setDesiredTorque (torque );
166+ jointDesiredOutputToPack .setStiffness (stiffness );
167+ jointDesiredOutputToPack .setDamping (damping );
168+ }
169+ }
170+ }
0 commit comments