3 import static com.kuka.roboticsAPI.motionModel.BasicMotions.positionHold;
     4 import static com.kuka.roboticsAPI.motionModel.BasicMotions.ptp;
    21 import java.io.IOException;
    22 import java.net.UnknownHostException;
    23 import java.util.concurrent.TimeUnit;
    24 import java.util.concurrent.TimeoutException;
    25 import java.util.concurrent.locks.Lock;
    26 import java.util.concurrent.locks.ReentrantLock;
    28 import com.google.flatbuffers.FlatBufferBuilder;
    29 import com.google.flatbuffers.Table;
    38 import com.kuka.common.ThreadUtil;
    39 import com.kuka.connectivity.fastRobotInterface.FRIConfiguration;
    40 import com.kuka.connectivity.fastRobotInterface.FRIJointOverlay;
    41 import com.kuka.connectivity.fastRobotInterface.FRISession;
    42 import com.kuka.connectivity.fastRobotInterface.FRIChannelInformation.FRISessionState;
    43 import com.kuka.connectivity.motionModel.smartServo.ISmartServoRuntime;
    44 import com.kuka.connectivity.motionModel.smartServo.ServoMotion;
    45 import com.kuka.connectivity.motionModel.smartServo.ServoMotionJP;
    46 import com.kuka.connectivity.motionModel.smartServo.SmartServo;
    49 import com.kuka.roboticsAPI.applicationModel.RoboticsAPIApplication;
    50 import com.kuka.roboticsAPI.controllerModel.Controller;
    51 import com.kuka.roboticsAPI.controllerModel.recovery.IRecovery;
    52 import com.kuka.roboticsAPI.deviceModel.JointLimits;
    53 import com.kuka.roboticsAPI.deviceModel.JointPosition;
    54 import com.kuka.roboticsAPI.deviceModel.LBR;
    55 import com.kuka.roboticsAPI.executionModel.CommandInvalidException;
    56 import com.kuka.roboticsAPI.executionModel.ExecutionState;
    57 import com.kuka.roboticsAPI.executionModel.IExecutionContainer;
    58 import com.kuka.roboticsAPI.geometricModel.CartDOF;
    59 import com.kuka.roboticsAPI.geometricModel.LoadData;
    60 import com.kuka.roboticsAPI.geometricModel.PhysicalObject;
    61 import com.kuka.roboticsAPI.geometricModel.Tool;
    62 import com.kuka.roboticsAPI.geometricModel.math.Vector;
    63 import com.kuka.roboticsAPI.motionModel.HandGuidingMotion;
    64 import com.kuka.roboticsAPI.motionModel.IMotion;
    65 import com.kuka.roboticsAPI.motionModel.IMotionContainer;
    66 import com.kuka.roboticsAPI.motionModel.IMotionContainerListener;
    67 import com.kuka.roboticsAPI.motionModel.MotionBatch;
    68 import com.kuka.roboticsAPI.motionModel.controlModeModel.AbstractMotionControlMode;
    69 import com.kuka.roboticsAPI.motionModel.controlModeModel.CartesianImpedanceControlMode;
    70 import com.kuka.roboticsAPI.motionModel.controlModeModel.CartesianSineImpedanceControlMode;
    71 import com.kuka.roboticsAPI.motionModel.controlModeModel.HandGuidingControlMode;
    72 import com.kuka.roboticsAPI.motionModel.controlModeModel.JointImpedanceControlMode;
    73 import com.kuka.roboticsAPI.motionModel.controlModeModel.PositionControlMode;
    74 import static com.kuka.roboticsAPI.motionModel.MMCMotions.*;
    82     private Controller _lbrController;
    95     private FRIConfiguration _friConfiguration = null;
    96     private FRISession       _friSession = null;
    97     private FRIJointOverlay  _motionOverlay = null;
    98     private FRIMode _FRIModeRunnable = null;
    99     private Thread _FRIModeThread = null;
   100     boolean waitingForUserToEndFRIMode = 
false;
   102     private SmartServo         _smartServoMotion = null;
   103     private ISmartServoRuntime _smartServoRuntime = null;
   107     private AbstractMotionControlMode _activeMotionControlMode;
   108     private AbstractMotionControlMode _smartServoMotionControlMode;
   110     private IRecovery _pausedApplicationRecovery = null;
   111     private PhysicalObject _toolAttachedToLBR;
   112     private HandGuidingMotion _handGuidingMotion;
   119     private Tool    _flangeAttachment;
   120     private JointLimits _jointLimits;
   121     private double[] _maxAllowedJointLimits;
   122     private double[] _minAllowedJointLimits;
   123     private JointImpedanceControlMode _teachControlMode;
   124     private TeachMode _teachModeRunnable = null;
   125     private Thread _teachModeThread = null;
   126     boolean waitingForUserToEndTeachMode = 
false;
   130     static final boolean _flexFellowPresent = 
false; 
   131     static final boolean _mediaFlangeIOPresent = 
false;
   133     private boolean _canServo = 
true;
   135     int message_counter = 0;
   136     int message_counter_since_last_mode_change = 0;
   145         _lbrController = (Controller) getContext().getControllers().toArray()[0];
   146         _lbr = (LBR) _lbrController.getDevices().toArray()[0];
   149         if(_mediaFlangeIOPresent) {
   155         _friConfiguration.setSendPeriodMilliSec(4);
   157         _friSession = 
new FRISession(_friConfiguration);
   159         _flangeAttachment = getApplicationData().createFromTemplate(
"FlangeAttachment");
   160         _pausedApplicationRecovery = getRecovery();
   162         LoadData _loadData = 
new LoadData();
   169         _toolAttachedToLBR = _flangeAttachment;
   170         _toolAttachedToLBR.attachTo(_lbr.getFlange());
   172         _jointLimits = _lbr.getJointLimits();
   175         _maxAllowedJointLimits = _jointLimits.getMaxJointPosition().get();
   176         _minAllowedJointLimits = _jointLimits.getMinJointPosition().get();
   177         for (
int i = 0; i < _lbr.getJointCount(); i++) {
   178             _maxAllowedJointLimits[i] -= 0.05;
   179             _minAllowedJointLimits[i] += 0.05;
   183         _teachControlMode = 
new JointImpedanceControlMode(_lbr.getJointCount())
   184                                 .setStiffnessForAllJoints(0.1)
   185                                 .setDampingForAllJoints(0.7);
   190                 _maxAllowedJointLimits,
   191                 _minAllowedJointLimits);
   192         _teachModeRunnable.
setLogger(getLogger());
   193         _teachModeThread = 
new Thread(_teachModeRunnable);
   194         _teachModeThread.start();
   198         _FRIModeRunnable = 
new FRIMode(
   202         _FRIModeThread = 
new Thread(_FRIModeRunnable);
   203         _FRIModeThread.start();
   211         getLogger().info(
"GRL_Driver from github.com/ahundt/grl starting...\nUDP Connecting to: " + _processDataManager.
get_ZMQ_MASTER_URI());
   219         IMotionContainer currentMotion = null;
   221         boolean newConfig = 
false;
   239             if( _currentKUKAiiwaState != null && _currentKUKAiiwaState.
armConfiguration() != null
   253             if( message_counter == 1 || (_previousKUKAiiwaState != null && _previousKUKAiiwaState.
armControlState() != null &&
   257                 .info(
"Switching mode: "   259                 message_counter_since_last_mode_change  = 0;
   262                 message_counter_since_last_mode_change++;
   269                 .info(
"ShutdownArm command received, stopping GRL_Driver...");
   278                 if(message_counter!=1 && (_previousKUKAiiwaState == null || _previousKUKAiiwaState.
armControlState()==null)) {
   281                 else if(message_counter==1
   283                         || (currentMotion != null && currentMotion.isFinished()) ) {
   286                     if (currentMotion != null) {
   287                         if(!currentMotion.isFinished()) {
   288                             currentMotion.cancel();
   290                             getLogger().info(
"Hand Guiding Motion has finished!");
   294                     if(!cancelSmartServo()) 
continue;
   296                     getLogger().warn(
"Enabling Teach Mode with gravity compensation. mode id code = " +
   314                     boolean useHandGuidingMotion = 
false;
   316                     if(useHandGuidingMotion)
   319                         _teachModeRunnable.
enable();
   321                         getLogger().warn(
"Started teach thread!");
   325                         currentMotion = _flangeAttachment.moveAsync(positionHold(_teachControlMode, -1, TimeUnit.SECONDS));
   334                 getLogger().error(
"MoveArmTrajectory Not Yet Implemented!");
   337                 JointPosition destination = 
new JointPosition(
   338                         _lbr.getJointCount());
   340                 if(!cancelSmartServo()) 
continue;
   341                 if(!cancelTeachMode()) 
continue;
   343                 if (currentMotion != null) currentMotion.cancel();
   352                     getLogger().error(
"Received null armControlState in servo!");
   359                     JointPosition pos = 
new JointPosition(_lbr.getCurrentJointPosition());
   361                     for (
int k = 0; k < destination.getAxisCount(); ++k)
   365                         currentMotion = _lbr.moveAsync(ptp(pos));
   377                 if(!cancelTeachMode()) 
continue;
   380                 JointPosition destination = 
new JointPosition(_lbr.getJointCount());
   383                     getLogger().error(
"Received null armControlState in servo!");
   391                 if( _smartServoMotion == null) {
   394                     destination = 
new JointPosition(_lbr.getCurrentJointPosition());
   395                     _smartServoMotion = 
new SmartServo(destination);
   408                         if (!ServoMotion.validateForImpedanceMode(_flangeAttachment))
   410                             getLogger().info(
"Validation of torque model failed - correct your mass property settings");
   411                             getLogger().info(
"SmartServo will be available for position controlled mode only, until validation is performed");
   415                             _smartServoMotion.setMode(_smartServoMotionControlMode);
   418                     catch (IllegalStateException e)
   420                         getLogger().info(
"Omitting validation failure for this sample\n"   427                         .setSpeedTimeoutAfterGoalReach(0.1)
   430                         .setMinimumTrajectoryExecutionTime(20e-3);
   434                     _toolAttachedToLBR.getDefaultMotionFrame().moveAsync(_smartServoMotion);
   435                     getLogger().info(
"Setting up SmartServo");
   439                 if (_smartServoRuntime == null) {
   440                     getLogger().info(
"Setting up Smart Servo runtime");
   443                         _smartServoRuntime = _smartServoMotion.getRuntime();
   444                         _smartServoRuntime.activateVelocityPlanning(
true);
   446                         _activeMotionControlMode = _smartServoMotionControlMode;
   447                     } 
catch (java.lang.IllegalStateException ex) {
   448                         getLogger().error(
"Could not retrieve SmartServo runtime!");
   449                         _smartServoRuntime = null;
   450                         _smartServoMotion = null;
   451                         getLogger().error(ex.getMessage());
   461                     if(    _friSession.getFRIChannelInformation().getFRISessionState().compareTo(FRISessionState.COMMANDING_ACTIVE) != 0
   462                         && _friSession.getFRIChannelInformation().getFRISessionState().compareTo(FRISessionState.COMMANDING_WAIT) != 0)
   464                         getLogger().info(
"FRI MotionOverlay starting...");
   466                         _FRIModeRunnable.
enable();
   469                     if(message_counter % 100 == 0) getLogger().info(
"FRI MotionOverlay Quality Sample: " + _friSession.getFRIChannelInformation().getQuality().toString());
   470                 } 
else if(_smartServoRuntime != null )  {
   473                     if(jointState.positionLength()!=destination.getAxisCount()){
   474                         if(message_counter_since_last_mode_change % 500 == 0) getLogger().error(
"Didn't receive correct number of joints! skipping to start of loop...\n");
   479                     for (
int k = 0; k < destination.getAxisCount(); ++k)
   481                         double position = jointState.
position(k);
   482                         destination.set(k, position);
   483                         pos = pos + 
" " + k + 
": " + position;
   488                         if(message_counter % 1000 == 0) getLogger().info(
"Sample of new Smart Servo Joint destination " + pos);
   489                         _smartServoRuntime.setDestination(destination);
   490                     } 
catch (CommandInvalidException e) {
   491                         getLogger().error(
"Could not update smart servo destination! Clearing SmartServo.");
   492                         _smartServoMotion = null;
   493                         _smartServoRuntime = null;
   495                     } 
catch (java.lang.IllegalStateException ex) {
   496                         getLogger().error(
"Could not update smart servo destination and missed the real exception type! Clearing SmartServo.");
   497                         _smartServoMotion = null;
   498                         _smartServoRuntime = null;
   499                         getLogger().error(ex.getMessage());
   503                     getLogger().error(
"Couldn't issue motion command, smartServo motion was most likely reset. retrying...");
   508                 _smartServoRuntime.stopMotion();
   509                 if (currentMotion != null) {
   510                     currentMotion.cancel();
   512                 if(!cancelTeachMode()) 
continue;
   517                 if (currentMotion != null) currentMotion.cancel();
   518                 if(!cancelTeachMode()) 
continue;
   519                 if(!cancelSmartServo()) 
continue;
   521                 PositionControlMode controlMode = 
new PositionControlMode();
   522                 if(message_counter_since_last_mode_change < 2 || message_counter_since_last_mode_change % 100 == 0){
   523                     _lbr.move(positionHold(controlMode,10,TimeUnit.MILLISECONDS));
   527                 System.out.println(
"Unsupported Mode! stopping");
   540                 Vector force = _lbr.getExternalForceTorque(_lbr.getFlange()).getForce();
   542                 double force_x = force.getX();
   543                 double force_y = force.getY();
   544                 double force_z = force.getZ();
   549                 FlatBufferBuilder builder = 
new FlatBufferBuilder(0);
   551                 int fb_wrench = 
Wrench.
createWrench(builder, force_x, force_y, force_z, torque_x, torque_y, torque_z, 0, 0, 0);
   559                 int[] statesOffset = 
new int[1];
   569                 builder.finish(KUKAiiwaStatesOffset);
   570                 byte[] msg = builder.sizedByteArray();
   574             } 
catch (IOException e) {
   585         _teachModeRunnable.stop();
   586         if (_updateConfiguration!=null && _updateConfiguration.
get_FRISession() != null) {
   591         .info(
"UDP connection closed.\nExiting...\nThanks for using the\nGRL_Driver from github.com/ahundt/grl\nGoodbye!");
   599     private boolean cancelTeachMode() {
   600         if (_teachModeThread != null) {
   602             _canServo = _teachModeRunnable.
cancel();
   605             if(!_teachModeRunnable.isEnableEnded() &&
   608                 if(message_counter % 30 == 0) getLogger().warn(
"Can't Exit Teach Mode Yet,\n Did You Press The Hand Guiding Button?");
   609                 waitingForUserToEndTeachMode = 
true;
   613             else if(_teachModeRunnable.isEnableEnded()
   615                     && waitingForUserToEndTeachMode)
   617                     PositionControlMode controlMode = 
new PositionControlMode();
   618                     _lbr.move(positionHold(controlMode,10,TimeUnit.MILLISECONDS));
   619                     waitingForUserToEndTeachMode = 
false;
   632     private boolean cancelFRIMode() {
   633         if (_FRIModeThread != null) {
   635             _canServo = _FRIModeRunnable.
cancel();
   638             if(!_FRIModeRunnable.isEnableEnded() &&
   641                 if(message_counter % 30 == 0) getLogger().warn(
"Can't Exit FRI Mode Yet, waiting...");
   642                 waitingForUserToEndFRIMode = 
true;
   646             else if(_FRIModeRunnable.isEnableEnded()
   648                     && waitingForUserToEndFRIMode)
   650                     PositionControlMode controlMode = 
new PositionControlMode();
   651                     _lbr.move(positionHold(controlMode,10,TimeUnit.MILLISECONDS));
   652                     waitingForUserToEndFRIMode = 
false;
   663     private boolean cancelSmartServo(){
   664         if (_smartServoRuntime != null) {
   665             _smartServoRuntime.stopMotion();
   666             _smartServoMotion = null;
   667             _smartServoRuntime = null;
   669             getLogger().info(
"Ending smart servo!");
   683         AbstractMotionControlMode mcm = null;
   686             mcm = 
new PositionControlMode();
   691             CartesianImpedanceControlMode cicm = 
new CartesianImpedanceControlMode()
   692                                         .setMaxCartesianVelocity(1000, 1000, 1000, 6.3, 6.3, 6.3)
   693                                         .setMaxPathDeviation(1000, 1000, 1000, 5, 5, 5)
   694                                         .setNullSpaceDamping(0.5)
   695                                         .setNullSpaceStiffness(2)
   696                                         .setMaxControlForce(200, 200, 200, 200, 200, 200, 
true);
   715             JointImpedanceControlMode jicm =  
new JointImpedanceControlMode(_lbr.getJointCount())
   730         if(newConfig == null) 
return false;
   735             _smartServoRuntime.changeControlModeSettings(_activeMotionControlMode);
   749     public static void main(
final String[] args)
   752         app.runApplication();
 static void startKUKAiiwaState(FlatBufferBuilder builder)
 
void setLogger(ITaskLogger logger)
 
void setLogger(ITaskLogger logger)
 
static void addCartesianWrench(FlatBufferBuilder builder, int CartesianWrenchOffset)
 
double get_CartesianImpedenceStiffnessA()
 
double get_CartesianImpedenceDampingX()
 
double get_JointImpedenceStiffness()
 
String get_ZMQ_MASTER_URI()
 
static final byte MoveArmTrajectory
 
grl.flatbuffer.KUKAiiwaState getPrevMessage()
 
static final byte CART_IMP_CONTROL_MODE
 
void setControlMode(AbstractMotionControlMode teachModeControlMode)
 
String get_controllingLaptopIPAddress()
 
static int createStatesVector(FlatBufferBuilder builder, int[] data)
 
boolean sendMessage(byte[] msg, int size)
 
double get_CartesianImpedenceStiffnessY()
 
static void startKUKAiiwaMonitorState(FlatBufferBuilder builder)
 
double get_CartesianImpedenceDampingA()
 
static final byte POSITION_CONTROL_MODE
 
double get_CartesianImpedenceStiffnessC()
 
double get_CartesianImpedenceDampingC()
 
String get_controllingLaptopJAVAPort()
 
grl.flatbuffer.KUKAiiwaState waitForNextMessage()
 
double get_CartesianImpedenceDampingY()
 
static void main(final String[] args)
 
static final byte SmartServo
 
static String name(int e)
 
static final byte JOINT_IMP_CONTROL_MODE
 
static int endKUKAiiwaState(FlatBufferBuilder builder)
 
static void startKUKAiiwaStates(FlatBufferBuilder builder)
 
double getEndEffectorWeight()
 
double get_CartesianImpedenceStiffnessX()
 
static final byte MoveArmJointServo
 
static final byte ShutdownArm
 
static void addMonitorState(FlatBufferBuilder builder, int monitorStateOffset)
 
static final byte TeachArm
 
String get_FRI_KONI_LaptopIPAddress()
 
double get_jointAccelRel()
 
double get_CartesianImpedenceDampingB()
 
ArmControlState armControlState()
 
static int endKUKAiiwaStates(FlatBufferBuilder builder)
 
static final byte StopArm
 
static int createWrench(FlatBufferBuilder builder, double force_x, double force_y, double force_z, double torque_x, double torque_y, double torque_z, double force_offset_x, double force_offset_y, double force_offset_z)
 
KUKAiiwaArmConfiguration armConfiguration()
 
double get_CartesianImpedenceStiffnessB()
 
double get_JointImpedenceDamping()
 
static int endKUKAiiwaMonitorState(FlatBufferBuilder builder)
 
static final byte PauseArm
 
static void addStates(FlatBufferBuilder builder, int statesOffset)
 
double get_CartesianImpedenceDampingZ()
 
AbstractMotionControlMode getMotionControlMode(byte controlMode)
 
FRISession get_FRISession()
 
double get_CartesianImpedenceStiffnessZ()