BRW KUKA Post Processor
=======================

Turns a trajectory simulated in BabaCAD Robotics Web (BRW) into a KUKA KRL robot program
(.src and .dat). You copy the program to the robot controller with a USB stick and run it there.
No network connection to the robot and no extra software are needed.

Supported BRW models (KR C2 / KR C4 / KR C5 controllers, KSS with the standard BAS.SRC):

  BRW model                 Axis speeds used for the program
  Kuka kr6                  KR 6-2 datasheet (156/156/156/335/355/647 deg/s)
  Kuka kr15                 estimate (KR 15/2)
  Kuka kr16                 KR 16-2 datasheet
  Kuka kr 6 r700 sixx       datasheet
  Kuka kr 70 r2100          estimate
  Kuka kr 210 r2700 extra   datasheet (motion ranges from the datasheet as well)
  Kuka kr 500 r2830         estimate (KR 500 R2830 C-F datasheet)

Any other model whose name starts with "Kuka" is post-processed too, with slow generic axis
speeds and a warning. The axis speeds only set how fast each motion runs ($VEL_AXIS); the
override on the smartPAD / KCP always scales them.


Commands in BRW
---------------

  User Command 1   post-process the active arm's simulation and save the KRL program for a
                   KR C4 / KR C5 controller (KSS 8.x).
  User Command 2   the same for a KR C2 controller (KSS 5.x).

Both save the program as two files, <name>.src and <name>.dat. Chrome and Edge ask once for
the folder; Firefox and Safari save both files to Downloads.

KR C4 and KR C2 programs differ only in how the path is cut into motions. Very short PTP
motions cannot be approximated within the controller's interpolation cycle and make the robot
slow down or stop, and the KR C2 has less memory and computing power:

                    shortest motion   most PTP points
  KR C4 / KR C5     0.05 s            20000
  KR C2             0.1 s             3000

If a long path needs more points, the plugin thins it more coarsely and says so in the console.
Older KR C2 cabinets (before edition 2005) have no USB port; copy the files over the network
or a floppy disk there.


Steps
-----

1. In BRW pick the KUKA model of your robot. With several arms in the scene, click the KUKA
   arm so it is the active one.
2. Import / Export -> select the folder "brw-plugin" from this zip.
3. Build the posture queue and press Play full once, so BRW computes the trajectory. Check the
   motion on screen.
4. Press User Command 1 (KR C4 / KR C5) or User Command 2 (KR C2) and select the folder for
   the program, for example your USB stick. The files are named like BRW_KR16_C4_1008_1530.src
   and .dat (BRW_<model>_<C4 or C2>_<month day>_<hour minute>).
5. On the robot (KR C4 / KR C5, KSS 8):
   - Log in as Expert (main menu -> Configuration -> User group).
   - Plug the USB stick into the controller's USB port.
   - In the Navigator select the .src and .dat files on the USB drive, Edit -> Copy, open
     KRC:\R1\Program (or a sub-folder) and Edit -> Paste.
   On a KR C2 (KSS 5) the steps are the same: Navigator, File -> Copy / Paste.
6. Select the program. In T1 hold the enabling switch and press Start: the robot first moves
   slowly to the start pose (BCO run, 10 %), then follows the simulated path.
7. Run it completely in T1 with a low override before you increase the override or switch to
   T2 / AUT.


What the program contains
-------------------------

- A joint (axis) program: every point is PTP {A1 .., A6 ..} in degrees in the KUKA convention.
  It does not use $TOOL or $BASE, so it runs the same whatever tool and base are selected.
- The simulated path is thinned to the points needed to stay within 0.05 deg of it (no motion
  shorter than the controller's minimum, so the largest deviation can be a few tenths of a
  degree; the program header states it), and the points are approximated (C_PTP, $APO.CPTP = 10 %) so the robot moves without stopping.
- Every motion gets the axis speed (BAS (#VEL_PTP, %)) that makes it take about as long as in
  the simulation. A pause of the simulated arm becomes WAIT SEC with an exact stop before it.
- The files are plain ASCII with Windows line ends, as both controller generations expect.
- Every point is checked against the robot's motion ranges. If one is outside, no program is
  written and the BRW console names the axis and the time.

Settings are at the top of brw-plugin.js (CONFIG): toleranceDeg, apoCptp, approachVelPercent,
maxVelPercent, minDwell, programName, and noBas (write $VEL_AXIS[1..6] directly for a
controller without BAS.SRC).


Joint convention
----------------

KUKA axis = sign * simulator angle + offset:

            A1     A2     A3     A4     A5     A6
  sign      -1     +1     +1     -1     +1     -1
  offset    0     -90    +90     0      0      0     (deg)

The simulator's Home pose is the KUKA pose A1 0, A2 -90, A3 90, A4 0, A5 0, A6 0 (upper arm
vertical, forearm horizontal). KUKA's A1, A4 and A6 turn the other way round than the
simulator's joints (as in ROS-Industrial's kuka_experimental models), so their sign is -1.
The plugin converts from the simulator's own angles, so it does not depend on the joint
mapping set in BRW.

Before the first run, make a short test program: in BRW queue the Home pose and a pose that
turns only joint 1 by about 20 deg, generate the program and run it in T1 with a low
override. The robot must turn the same way as the model on screen. Repeat it for joints 4
and 6 if the tool allows.


Limits
------

- External axes (linear rail, rotary table) are not post-processed yet: a trajectory that moves
  them is refused.
- Only the robot's axes are programmed: no tool I/O, gripper or welding commands. Add them in
  the program on the controller if you need them.
- Timing is close to the simulation, not exact: the controller plans its own acceleration.


SAFETY. The real robot moves exactly like the simulation, including through anything not
modelled in BRW. Clear the work cell, start in T1 with a low override and keep the enabling
switch and the emergency stop within reach.

DISCLAIMER OF WARRANTY. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND.
