Code
package application;
import javax.inject.Inject;
import com.kuka.roboticsAPI.applicationModel.RoboticsAPIApplication;
import static com.kuka.roboticsAPI.motionModel.BasicMotions.*;
import com.kuka.roboticsAPI.deviceModel.LBR;
import com.kuka.roboticsAPI.geometricModel.Frame;
import com.kuka.roboticsAPI.geometricModel.Tool;
import com.kuka.roboticsAPI.motionModel.CIRC;
import com.kuka.roboticsAPI.motionModel.CartesianPTP;
import com.kuka.roboticsAPI.motionModel.LIN;
import com.kuka.roboticsAPI.motionModel.MotionBatch;
public class TicTacToe extends RoboticsAPIApplication
{
@Inject
Tool tool;
LBR robot;
Frame canvas = new Frame();
Frame homePosition = new Frame();
// Contains the corners in the drawCross method.
Frame corner1 = new Frame();
Frame corner2 = new Frame();
Frame corner3 = new Frame();
Frame corner4 = new Frame();
Frame square1 = new Frame(canvas);
Frame square2 = new Frame(canvas);
Frame square3 = new Frame(canvas);
Frame square4 = new Frame(canvas);
Frame square5 = new Frame(canvas);
Frame square6 = new Frame(canvas);
Frame square7 = new Frame(canvas);
Frame square8 = new Frame(canvas);
Frame square9 = new Frame(canvas);
Frame playGround = new Frame(canvas);
// Contains the two points of the circle in the drawCircle method.
Frame auxiliarry = new Frame();
Frame StartEnd = new Frame();
private double canvasWidthHeight = 300;
double squareWidthHeight = canvasWidthHeight / 3;
@Override
public void initialize()
{
robot = getContext().getDeviceFromType(LBR.class);
tool = getApplicationData().createFromTemplate("tool");
// Get the canvas base.
this.canvas = getApplicationData().getFrame("/canvas").copy();
this.homePosition = getApplicationData().getFrame("/homePosition").copy();
tool.attachTo(robot.getFlange());
square1.setX(squareWidthHeight / 2);
square1.setY(squareWidthHeight / 2);
square2.setX(squareWidthHeight / 2 + squareWidthHeight);
square2.setY(squareWidthHeight / 2);
square3.setX(squareWidthHeight / 2 + squareWidthHeight * 2);
square3.setY(squareWidthHeight / 2);
square4.setX(squareWidthHeight / 2);
square4.setY(squareWidthHeight / 2 + squareWidthHeight);
square5.setX(squareWidthHeight / 2 + squareWidthHeight);
square5.setY(squareWidthHeight / 2 + squareWidthHeight);
square6.setX(squareWidthHeight / 2 + squareWidthHeight * 2);
square6.setY(squareWidthHeight / 2 + squareWidthHeight);
square7.setX(squareWidthHeight / 2);
square7.setY(squareWidthHeight / 2 + squareWidthHeight * 2);
square8.setX(squareWidthHeight / 2 + squareWidthHeight);
square8.setY(squareWidthHeight / 2 + squareWidthHeight * 2);
square9.setX(squareWidthHeight / 2 + squareWidthHeight * 2);
square9.setY(squareWidthHeight / 2 + squareWidthHeight * 2);
}
@Override
public void run()
{
drawPlayGround();
drawCross(1);
drawCircle(2);
}
private void drawPlayGround()
{
Frame point1 = playGround;
Frame point2 = playGround;
Frame point3 = playGround;
Frame point4 = playGround;
Frame point5 = playGround;
Frame point6 = playGround;
Frame point7 = playGround;
Frame point8 = playGround;
point1.setX(point1.getX() + squareWidthHeight); // Y = 0
point2.setX(point2.getX() + squareWidthHeight*2); // Y = 0
point3.setX(point3.getX() + canvasWidthHeight);
point3.setY(point3.getY() + squareWidthHeight);
point4.setX(point4.getX() + canvasWidthHeight);
point4.setY(point4.getY() + squareWidthHeight*2);
point5.setX(point5.getX() + squareWidthHeight*2);
point5.setY(point5.getY() + canvasWidthHeight);
point6.setX(point6.getX() + squareWidthHeight);
point6.setY(point6.getY() + canvasWidthHeight);
point7.setY(point7.getY() + squareWidthHeight*2); // X = 0
point8.setY(point8.getY() + squareWidthHeight); // X = 0
// Create all the motions and send them as a batch to the controller
CartesianPTP toPrePosition = ptp(point1.setZ(50));
LIN toPoint1 = lin((point1).setZ(50));
LIN toDrawPoint1 = lin(point1.copy().setZ(30));
LIN toDrawPoint6 = lin(point6.copy().setZ(30));
LIN toPoint6 = lin((point6).setZ(50));
LIN toPoint5 = lin((point5).setZ(50));
LIN toDrawPoint5 = lin(point5.copy().setZ(30));
LIN toDrawPoint2 = lin(point2.copy().setZ(30));
LIN toPoint2 = lin((point2).setZ(50));
LIN toPoint3 = lin((point3).setZ(50));
LIN toDrawPoint3 = lin(point3.copy().setZ(30));
LIN toDrawPoint8 = lin(point8.copy().setZ(30));
LIN toPoint8 = lin((point8).setZ(50));
LIN toPoint7 = lin((point7).setZ(50));
LIN toDrawPoint7 = lin(point7.copy().setZ(30));
LIN toDrawPoint4 = lin(point4.copy().setZ(30));
LIN toPoint4 = lin((point4).setZ(50));
// Move the tool to a start position so we know it can plan the motions
robot.move(ptp(homePosition).setJointVelocityRel(0.3));
MotionBatch batch = new MotionBatch(toPrePosition,
toPoint1, toDrawPoint1, toDrawPoint6, toPoint6,
toPoint5, toDrawPoint5, toDrawPoint2, toPoint2,
toPoint3, toDrawPoint3, toDrawPoint8, toPoint8,
toPoint7, toDrawPoint7, toDrawPoint4, toPoint4);
batch.setJointVelocityRel(0.3);
tool.move(batch);
// Return to the start position
robot.move(ptp(homePosition).setJointVelocityRel(0.3));
}
private void drawCross(int square)
{
switch (square)
{
case 1:
corner1 = square1.copy();
corner2 = square1.copy();
corner3 = square1.copy();
corner4 = square1.copy();
break;
case 2:
corner1 = square2.copy();
corner2 = square2.copy();
corner3 = square2.copy();
corner4 = square2.copy();
break;
case 3:
corner1 = square3.copy();
corner2 = square3.copy();
corner3 = square3.copy();
corner4 = square3.copy();
break;
case 4:
corner1 = square4.copy();
corner2 = square4.copy();
corner3 = square4.copy();
corner4 = square4.copy();
break;
case 5:
corner1 = square5.copy();
corner2 = square5.copy();
corner3 = square5.copy();
corner4 = square5.copy();
break;
case 6:
corner1 = square6.copy();
corner2 = square6.copy();
corner3 = square6.copy();
corner4 = square6.copy();
break;
case 7:
corner1 = square7.copy();
corner2 = square7.copy();
corner3 = square7.copy();
corner4 = square7.copy();
break;
case 8:
corner1 = square8.copy();
corner2 = square8.copy();
corner3 = square8.copy();
corner4 = square8.copy();
break;
case 9:
corner1 = square9.copy();
corner2 = square9.copy();
corner3 = square9.copy();
corner4 = square9.copy();
break;
default:
getLogger().error("This square is not supported: " + square);
break;
}
corner1.setX(corner1.getX() + squareWidthHeight / 2);
corner1.setY(corner1.getY() + squareWidthHeight / 2);
corner2.setX(corner2.getX() - squareWidthHeight / 2);
corner2.setY(corner2.getY() - squareWidthHeight / 2);
corner3.setX(corner3.getX() - squareWidthHeight / 2);
corner3.setY(corner3.getY() + squareWidthHeight / 2);
corner4.setX(corner4.getX() + squareWidthHeight / 2);
corner4.setY(corner4.getY() - squareWidthHeight / 2);
// Create all the motions and send them as a batch to the controller
CartesianPTP toPrePosition = ptp((corner1).setZ(50));
LIN toCorner1 = lin((corner1).setZ(50));
LIN toDrawCorner1 = lin(corner1.copy().setZ(30));
LIN toDrawCorner2 = lin(corner2.copy().setZ(30));
LIN toCorner2 = lin((corner2).setZ(50));
LIN toCorner3 = lin((corner3).setZ(50));
LIN toDrawCorner3 = lin((corner3.copy()).setZ(30));
LIN toDrawCorner4 = lin((corner4.copy()).setZ(30));
LIN toCorner4 = lin((corner4).setZ(50));
// Move the tool to a start position so we know it can plan the motions
robot.move(ptp(homePosition).setJointVelocityRel(0.3));
MotionBatch batch = new MotionBatch(toPrePosition,
toCorner1, toDrawCorner1,
toDrawCorner2, toCorner2,
toCorner3, toDrawCorner3,
toDrawCorner4, toCorner4);
batch.setJointVelocityRel(0.3);
tool.move(batch);
// Return to the start position
robot.move(ptp(homePosition).setJointVelocityRel(0.3));
}
private void drawCircle(int square)
{
switch (square)
{
case 1:
auxiliarry = square1.copy();
StartEnd = square1.copy();
break;
case 2:
auxiliarry = square2.copy();
StartEnd = square2.copy();
break;
case 3:
auxiliarry = square3.copy();
StartEnd = square3.copy();
break;
case 4:
auxiliarry = square4.copy();
StartEnd = square4.copy();
break;
case 5:
auxiliarry = square5.copy();
StartEnd = square5.copy();
break;
case 6:
auxiliarry = square6.copy();
StartEnd = square6.copy();
break;
case 7:
auxiliarry = square7.copy();
StartEnd = square7.copy();
break;
case 8:
auxiliarry = square8.copy();
StartEnd = square8.copy();
break;
case 9:
auxiliarry = square9.copy();
StartEnd = square9.copy();
break;
default:
getLogger().error("This square is not supported: " + square);
break;
}
StartEnd.setX(StartEnd.getX() - ((squareWidthHeight / 2) - (squareWidthHeight / 10)));
auxiliarry.setX(auxiliarry.getX() + ((squareWidthHeight / 2) - (squareWidthHeight / 10)));
// Move the tool to a start position so we know it can plan the motions
robot.move(ptp(homePosition).setJointVelocityRel(0.3));
// Create all the motions and send them as a batch to the controller
CartesianPTP toPrePosition = ptp(StartEnd.copy().setZ(50));
LIN toStartCircle = lin((StartEnd).setZ(30));
CIRC drawCircle = circ(auxiliarry, StartEnd);
LIN toEndCircle = lin((StartEnd).setZ(50));
MotionBatch batch = new MotionBatch(toPrePosition, toStartCircle, drawCircle, toEndCircle);
batch.setJointVelocityRel(0.3);
tool.move(batch);
// Return to the start position
robot.move(ptp(homePosition).setJointVelocityRel(0.3));
}
}
Display More
We have now modified the code according to what you suggested, as well as declaring the robot and the tool. Now we are able to make the robot move to home position, which is located 500mm above the canvas, but now we get the error shown below.
What should we do to solve this? We cant locate the culprint.