For this I'm going to assume you have a grasp of the basic principles of robotics and object oriented programming.
Before doing anything you have to create your project with the correct configuration, this is covered fairly well in the manual.
Create your first application, use the provided wizard in workbench. The wizard creates a java class that inherits from the super class RoboticsAPIApplication, because of this your application has access many functions for manipulating the robot. Note, all these functions can be obtained without extending the RoboticsAPI, you just have to import them yourself.
Now it's time to write some code. Inside your application you'll see 2 abstract methods that sunrise has implemented for you, namely "run" and "initialize". The run method is called from the API once when your application is started and the initialize method is called once just before the run method is called. Just as the name suggests, you should initialize your program withing the initialize method. Usually you attach your desired tool to your robot (see sample code).
Now you have a tool that is attached to your robot that you can command to different positions in space. You can calculate where you want the robot move or you can teach it. Teaching is done from the SmartPad. Teaching is simply storing the current robot position in a variable. If you want to learn how to declare and teach positions then I suggest you read the manual.
The most common way to move the robot is by commanding the attached tool to certain position. The tool must be attached to the robot for this to work, see code below.
For your tic tac toe you're going to need to create a base. The main advantage with using a base in this case is it makes calculations easier and you can move the surface you're going to draw on fairly easily.
I'm going to provide you with some code to get started.
package application;
import javax.inject.Inject;
import javax.inject.Named;
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 {
// Here you fetch the tool you created, the injector finds the right tool if
// you add the named annotation
@Inject
@Named("YourToolName")
Tool tool;
// Here we will use the robot to find its flange position so we can attach
// our tool to it.
@Inject
LBR robot;
// Contains the center points of all 9 squares
private Frame[][] squares = new Frame[3][3];
private double canvasWidthHeight;
double squareWidthHeight = canvasWidthHeight / 3;
@Override
public void initialize() {
// Here we attach our tool to the flange, the flange becomes the tools
// transoformation provider, this means that the tool will follow the
// flange when it moves.
tool.attachTo(robot.getFlange());
}
@Override
public void run() {
// Get the canvas base, this can be done by drag and drop.
Frame canvas = getApplicationData().getFrame("/Canvas").copy();
// Populate the square array
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
// Calculate the position on the XY plane.
squares[i][j]
.setX((squareWidthHeight / 2 + (squareWidthHeight * j)));
squares[i][j]
.setY((squareWidthHeight / 2 + (squareWidthHeight * i)));
// The parent becomes the canvas base. Now our squares will
// follow the canvas if you recalibrate the canvas base.
squares[i][j].setParent(canvas);
}
}
// Implement code to wait for command and draw a shape according to
// input
// Remember that grid positions are 0 based
drawCircle(2, 1);
drawCross(2, 2);
}
private void drawCross(int i, int j) {
Frame corner1 = squares[i][j].copy();
Frame corner2 = squares[i][j].copy();
Frame corner3 = squares[i][j].copy();
Frame corner4 = squares[i][j].copy();
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(corner3.getX() + squareWidthHeight / 2);
corner4.setY(corner3.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);
LIN toPreCorner1 = lin(corner1.copy().setZ(30));
LIN toPreCorner2 = lin(corner2.copy().setZ(30));
LIN toCorner2 = lin(corner2);
LIN toPreCorner3 = lin(corner3.copy().setZ(30));
LIN toCorner3 = lin(corner3);
LIN toPreCorner4 = lin(corner4.copy().setZ(30));
LIN toCorner4 = lin(corner4);
// Move the robot to a start position so we know it can plan the motions
robot.move(ptp(getApplicationData().getFrame("/HomePosition"))
.setJointVelocityRel(0.3));
MotionBatch batch = new MotionBatch(toPrePosition, toCorner1,
toPreCorner1, toPreCorner2, toCorner2, toPreCorner2,
toPreCorner3, toCorner3, toPreCorner3, toPreCorner4, toCorner4,
toPreCorner4);
batch.setJointVelocityRel(0.3);
tool.move(batch);
// Return to the start position
robot.move(ptp(getApplicationData().getFrame("/HomePosition"))
.setJointVelocityRel(0.3));
}
private void drawCircle(int i, int j) {
Frame auxiliarry = squares[i][j].copy();
Frame StartEnd = squares[i][j].copy();
StartEnd.setX(StartEnd.getX()
- ((squareWidthHeight / 2) - (squareWidthHeight / 10)));
auxiliarry.setX(auxiliarry.getX()
+ ((squareWidthHeight / 2) - (squareWidthHeight / 10)));
// Move the robot to a start position so we know it can plan the motions
robot.move(ptp(getApplicationData().getFrame("/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);
CIRC drawCircle = circ(auxiliarry, StartEnd);
MotionBatch batch = new MotionBatch(toPrePosition, toStartCircle,
drawCircle);
// Return to the start position
robot.move(ptp(getApplicationData().getFrame("/HomePosition"))
.setJointVelocityRel(0.3));
}
}
Display More
Code never tested.