forked from tissabre/IHMCSimulation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDarwinDemo.java
290 lines (242 loc) · 8.44 KB
/
DarwinDemo.java
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
287
288
289
290
package us.ihmc.demo;
import java.io.IOException;
import java.text.DecimalFormat;
import java.util.Arrays;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.jnativehook.GlobalScreen;
import org.jnativehook.NativeHookException;
import org.jnativehook.keyboard.NativeKeyEvent;
import org.jnativehook.keyboard.NativeKeyListener;
import us.ihmc.robotics.dataStructures.variable.YoVariable;
import us.ihmc.simulationconstructionset.SimulationConstructionSet;
import us.ihmc.simulationconstructionset.SimulationConstructionSetParameters;
import us.ihmc.valkyrie.TestRobotModel;
import us.trec.easyCAT.easyCATController;
public class DarwinDemo implements NativeKeyListener {
// Make scs and easyCAT available for all methods
static SimulationConstructionSet scs;
static easyCATController easyCAT1;
// Get chosen joint
DecimalFormat format = new DecimalFormat("##.00"); // rounding to 2 decimal places
static double JOINT_STEP_SIZE = 0.05;
static boolean IS_JOINT_CHOSEN = false;
static String[] CHOSEN_JOINTS = { "q_j_tilt", "q_j_high_arm_l", "q_j_high_arm_r", "q_j_gripper_l", "q_j_gripper_r",
"q_j_tibia_l", "q_j_tibia_r", "q_j_thigh2_l", "q_j_thigh2_r", "q_j_pan" };
static double[] JOINTS_VALUES = new double[CHOSEN_JOINTS.length];
// specific to CHOSEN_JOINTS
static boolean[] REVERSED_DIRS = { false, true, true, false, false, false, true, false, true, false };
static int JOINT_CHOSEN = 1; // joint chosen at beginning when motor is manually moved
// TODO: Fill remaining joint limits
final static double[] ABS_JOINT_LIM = { 0, 1.6, 0, 0, 0, 0, 0, 0, 0, 0 }; // the limit of the joint (+-)
// code for keyboard presses
final int UP_ARROW = 57416, DOWN_ARROW = 57424, SPACE = 57, SHIFT_L = 42, SHIFT_R = 54, T = 20;
final boolean MOVE_UP = true, MOVE_DOWN = false;
// Target position handler
static double TARGET_POS;
static String TARGET_COMMAND = "";
// Handle loosening motor after arrow presses
static int INCREMENT_TIMER = 0, TIMER_MAX = 150000;
static boolean TURNON = false;
// ARBITER
public enum Arbiter {
INCREMENT, /* TARGET, */ READ, NONE,
}
static Arbiter inCharge = Arbiter.READ;
// Threads
static Runnable updateMotors = new Runnable() {
public void run() {
handleEtherCAT();
}
};
static Thread update = new Thread(updateMotors); // Thread to deal with motor reads
/*
*
*/
public static void main(String[] args) throws IOException {
// Listen to user input
try {
GlobalScreen.registerNativeHook();
} catch (NativeHookException e) {
e.printStackTrace();
}
GlobalScreen.getInstance().addNativeKeyListener(new DarwinDemo());
Logger logger = Logger.getLogger(GlobalScreen.class.getPackage().getName());
logger.setLevel(Level.OFF);
logger.setUseParentHandlers(false);
// end of listening to user input
// Create robot
TestRobotModel robotModel = new TestRobotModel();
// Set all position to 0
Arrays.fill(JOINTS_VALUES, 0.0);
// Parameters for Simulation Environment
SimulationConstructionSetParameters scsParameters = new SimulationConstructionSetParameters();
scs = new SimulationConstructionSet(robotModel.getSdfRobot(), scsParameters);
scs.setGroundVisible(false);
// Set a nice camera angle
scs.setCameraFix(0.0168, 0.0435, -0.1556);
scs.setCameraPosition(2.3212, -0.6235, 0.0574);
// Start simulation
scs.startOnAThread();
startEtherCAT(); // NO INCREMENT
}
/*
*
*/
public static void handleEtherCAT() {
while (true) {
switch (inCharge) {
case INCREMENT:
easyCAT1.writeMode();
INCREMENT_TIMER++;
if (INCREMENT_TIMER > TIMER_MAX) {
inCharge = Arbiter.READ;
sendTurnOnSignal(false);
easyCAT1.quickSend(); //makes sure change is sent before read mode
}
break;
case NONE:
break;
case READ:
easyCAT1.readMode();
int real_position = easyCAT1.getATRPos(0);
double scs_position = byteToSCSposition(real_position);
// System.out.println(real_position);
setRobotPosition(scs_position);
INCREMENT_TIMER = 0;
break;
// case TARGET:
// break;
default:
break;
}
System.out.println(scs.getVariable(CHOSEN_JOINTS[JOINT_CHOSEN]).getValueAsDouble());
}
}
/*
* This function takes a byte it receives from etherCAT 'eth_byte' and converts
* it to a position in SCS-units based on the chosen joint's limits (positive
* and negative)
*/
static double byteToSCSposition(int eth_byte) {
double scs_pos = (-2 * (double) eth_byte * ABS_JOINT_LIM[JOINT_CHOSEN] / 255.0 + ABS_JOINT_LIM[JOINT_CHOSEN]);
// System.out.println(scs_pos);
return scs_pos;
}
/*
* This function takes an SCS position 'scs_pos' and converts it to a byte to be
* sent via etherCAT
*/
static int SCSpositionToByte(double scs_pos) {
double eth_byte = 255 * (scs_pos - ABS_JOINT_LIM[JOINT_CHOSEN]) / (-2 * ABS_JOINT_LIM[JOINT_CHOSEN]);
return (int) eth_byte;
}
/*
*
*/
private static void startEtherCAT() throws IOException {
// create easyCATtry to start communication
int[] message = { 0, 0 };
easyCAT1 = new easyCATController(message);
// COMMUNICATION STARTING POINT
easyCAT1.start();
update.start();
easyCAT1.join();
}
/*
* This function sets a joint position in the SCS simulation based on a given
* target. The chosen joint is decided before entering the function
*/
private static void setRobotPosition(double target) {
YoVariable<?> joint = scs.getVariable(CHOSEN_JOINTS[JOINT_CHOSEN]);
joint.setValueFromDouble(target);
}
/*
* This function sets a joint position in the SCS simulation based on a given
* target and a given joint.
*/
private static void setRobotPosition(double target, int joint) {
JOINT_CHOSEN = joint;
setRobotPosition(target);
}
/*
*
*/
private void handleMovement(int jointChosen, boolean dir) {
inCharge = Arbiter.INCREMENT;
INCREMENT_TIMER = 0; // prevent timer from incrementing since motor is moving
// set turnON bit on to prevent motor from loosening
sendTurnOnSignal(true);
// logic to set joint movement direction
YoVariable<?> joint = scs.getVariable(CHOSEN_JOINTS[jointChosen]); // get joint
boolean reversed = REVERSED_DIRS[jointChosen]; // is joint reversed in scs?
int direction = ((dir && !reversed) || (!dir && reversed)) ? 1 : 0; // make sure up goes up
// get limits for chosen joint
double targetPos = 0.0;
double targetLimit = ABS_JOINT_LIM[jointChosen];
// move joints
switch (direction) {
case 1: // UP
targetPos = joint.getValueAsDouble() + JOINT_STEP_SIZE;
if (targetPos > targetLimit)
targetPos = targetLimit;
joint.setValueFromDouble(targetPos);
break;
case 0: // DOWN
targetPos = joint.getValueAsDouble() - JOINT_STEP_SIZE;
if (targetPos < -targetLimit)
targetPos = -targetLimit;
joint.setValueFromDouble(targetPos);
break;
default:
System.out.println("SHOULD NOT GET HERE.");
break;
}
// update values in array
JOINTS_VALUES[jointChosen] = Double.parseDouble(format.format(targetPos));
// send values via etherCAT
easyCAT1.setArrayPos(0, SCSpositionToByte(targetPos));
}
/*
* This function sends a signal to loosen the motor when 'tf' is false, and
* locks the motor into position when 'tf' is true
*/
public static void sendTurnOnSignal(boolean tf) {
if (tf) {
// turn motor on (locks)
easyCAT1.setArrayPos(1, 1);
} else
// turn motor off (loosen)
easyCAT1.setArrayPos(1, 0);
TURNON = tf;
}
// ----------------------------------------------------------------------------------------------------
@Override
public void nativeKeyPressed(NativeKeyEvent e) {
// Choosing which part to move
try {
JOINT_CHOSEN = (int) Double.parseDouble(NativeKeyEvent.getKeyText(e.getKeyCode()));
inCharge = Arbiter.READ;
System.out.println("Joint " + JOINT_CHOSEN + ": " + CHOSEN_JOINTS[JOINT_CHOSEN] + " chosen.");
} catch (Exception e2) { // when key pressed is not a number
// Choosing which direction to move joint
// Handle movement: True = up | False = down
if (e.getKeyCode() == UP_ARROW)
handleMovement(JOINT_CHOSEN, MOVE_UP);
else if (e.getKeyCode() == DOWN_ARROW)
handleMovement(JOINT_CHOSEN, MOVE_DOWN);
// else if (e.getKeyCode() == SHIFT_R || e.getKeyCode() == SHIFT_L) {
// inCharge = Arbiter.TARGET;
// System.out.print("Set " + CHOSEN_JOINTS[JOINT_CHOSEN] + " position to: ");
// }
}
}
@Override
public void nativeKeyReleased(NativeKeyEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void nativeKeyTyped(NativeKeyEvent arg0) {
// TODO Auto-generated method stub
}
}