|
| 1 | +package application; |
| 2 | + |
| 3 | +import static com.kuka.roboticsAPI.motionModel.BasicMotions.positionHold; |
| 4 | + |
| 5 | +import java.util.concurrent.TimeUnit; |
| 6 | +import java.util.concurrent.TimeoutException; |
| 7 | +import java.util.Arrays; |
| 8 | + |
| 9 | +import javax.inject.Inject; |
| 10 | +import javax.inject.Named; |
| 11 | + |
| 12 | +import com.kuka.roboticsAPI.applicationModel.RoboticsAPIApplication; |
| 13 | +import com.kuka.roboticsAPI.controllerModel.Controller; |
| 14 | +import com.kuka.roboticsAPI.deviceModel.LBR; |
| 15 | +import com.kuka.roboticsAPI.geometricModel.CartDOF; |
| 16 | +import com.kuka.roboticsAPI.geometricModel.ObjectFrame; |
| 17 | +import com.kuka.roboticsAPI.geometricModel.Tool; |
| 18 | +import com.kuka.roboticsAPI.uiModel.ApplicationDialogType; |
| 19 | +import com.kuka.roboticsAPI.motionModel.controlModeModel.*; |
| 20 | +import com.kuka.connectivity.fastRobotInterface.*; |
| 21 | + |
| 22 | +public class TorqueControl extends RoboticsAPIApplication { |
| 23 | + // members |
| 24 | + @Inject |
| 25 | + private LBR lbr_; |
| 26 | + private Controller lbr_controller_; |
| 27 | + |
| 28 | + @Inject |
| 29 | + @Named("Tool") |
| 30 | + private Tool mytool; |
| 31 | + |
| 32 | + // control mode |
| 33 | + private enum CONTROL_MODE { |
| 34 | + POSITION_CONTROL, |
| 35 | + JOINT_IMPEDANCE_CONTROL, |
| 36 | + CARTESIAN_IMPEDANCE_CONTROL; |
| 37 | + } |
| 38 | + |
| 39 | + // convert enum to string array, see https://stackoverflow.com/questions/13783295/getting-all-names-in-an-enum-as-a-string |
| 40 | + public static String[] getNames(Class<? extends Enum<?>> e) { |
| 41 | + return Arrays.toString(e.getEnumConstants()).replaceAll("^.|.$", "").split(", "); |
| 42 | + } |
| 43 | + |
| 44 | + // FRI parameters |
| 45 | + private String client_name_ = "172.31.1.148"; |
| 46 | + private int send_period_ = 1; |
| 47 | + |
| 48 | + private FRIConfiguration fri_configuration_; |
| 49 | + private FRISession fri_session_; |
| 50 | + private FRIJointOverlay fri_overlay_; |
| 51 | + |
| 52 | + private AbstractMotionControlMode control_mode_; |
| 53 | + private String[] control_modes_ = getNames(CONTROL_MODE.class); |
| 54 | + private ClientCommandMode command_mode_; |
| 55 | + private String[] command_modes_ = getNames(ClientCommandMode.class); |
| 56 | + // methods |
| 57 | + public void request_user_config() { |
| 58 | + getLogger().info("Send period set to: " + send_period_); |
| 59 | + |
| 60 | + getLogger().info("Remote address set to: " + client_name_); |
| 61 | + |
| 62 | + control_mode_= new JointImpedanceControlMode(0.0,0.0,0.0,0.0,0.0,0.0,0.0); |
| 63 | + command_mode_ = ClientCommandMode.TORQUE; |
| 64 | + getLogger().info("Client command mode set to: " + command_mode_.name()); |
| 65 | + } |
| 66 | + |
| 67 | + public void configure_fri() { |
| 68 | + fri_configuration_ = FRIConfiguration.createRemoteConfiguration(lbr_, client_name_); |
| 69 | + fri_configuration_.setSendPeriodMilliSec(send_period_); |
| 70 | + |
| 71 | + getLogger().info("Creating FRI connection to " + fri_configuration_.getHostName()); |
| 72 | + getLogger().info( |
| 73 | + "SendPeriod: " + fri_configuration_.getSendPeriodMilliSec() + "ms |" |
| 74 | + + " ReceiveMultiplier: " + fri_configuration_.getReceiveMultiplier() |
| 75 | + ); |
| 76 | + |
| 77 | + fri_session_ = new FRISession(fri_configuration_); |
| 78 | + fri_overlay_ = new FRIJointOverlay(fri_session_, command_mode_); |
| 79 | + |
| 80 | + fri_session_.addFRISessionListener(new IFRISessionListener() { |
| 81 | + @Override |
| 82 | + public void onFRISessionStateChanged(FRIChannelInformation friChannelInformation) { |
| 83 | + getLogger().info("Session State change " + friChannelInformation.getFRISessionState().toString() ); |
| 84 | + } |
| 85 | + |
| 86 | + @Override |
| 87 | + public void onFRIConnectionQualityChanged(FRIChannelInformation friChannelInformation) { |
| 88 | + getLogger().info("Quality change signalled "+friChannelInformation.getQuality()); |
| 89 | + getLogger().info("Jitter "+friChannelInformation.getJitter()); |
| 90 | + getLogger().info("Latency "+friChannelInformation.getLatency()); |
| 91 | + } |
| 92 | + }); |
| 93 | + |
| 94 | + // try to connect |
| 95 | + try { |
| 96 | + fri_session_.await(60, TimeUnit.SECONDS); |
| 97 | + } catch (final TimeoutException e) { |
| 98 | + getLogger().error(e.getLocalizedMessage()); |
| 99 | + getLogger().error("Connection timeout: Current Timeout limit = 60 sec"); |
| 100 | + return; |
| 101 | + } |
| 102 | + |
| 103 | + getLogger().info("FRI connection established."); |
| 104 | + } |
| 105 | + |
| 106 | + @Override |
| 107 | + public void initialize() { |
| 108 | + ObjectFrame lbr_flange = lbr_.getFlange(); |
| 109 | + mytool.attachTo(lbr_flange); |
| 110 | + getLogger().info("End Effector position:" + lbr_flange.getX()/1000.0 + " " + lbr_flange.getY()/1000.0 + " " + lbr_flange.getZ()/1000.0); |
| 111 | + |
| 112 | + lbr_controller_ = (Controller) getContext().getControllers().toArray()[0]; |
| 113 | + lbr_ = (LBR) lbr_controller_.getDevices().toArray()[0]; |
| 114 | + |
| 115 | + // set FRI parameters |
| 116 | + request_user_config(); |
| 117 | + |
| 118 | + // configure the FRI |
| 119 | + configure_fri(); |
| 120 | + } |
| 121 | + |
| 122 | + @Override |
| 123 | + public void run() { |
| 124 | + // run the FRI |
| 125 | + lbr_.move(positionHold(control_mode_, -1, null).addMotionOverlay(fri_overlay_)); |
| 126 | + return; |
| 127 | + } |
| 128 | + |
| 129 | + @Override |
| 130 | + public void dispose() { |
| 131 | + // close connection |
| 132 | + getLogger().info("Disposing FRI session."); |
| 133 | + fri_session_.close(); |
| 134 | + |
| 135 | + super.dispose(); |
| 136 | + } |
| 137 | + |
| 138 | + /** |
| 139 | + * main |
| 140 | + * |
| 141 | + * @param args |
| 142 | + */ |
| 143 | + public static void main(final String[] args) { |
| 144 | + TorqueControl app = new TorqueControl(); |
| 145 | + app.runApplication(); |
| 146 | + } |
| 147 | +} |
0 commit comments