[[cha:haltcl]]

= HALTCL Files

The halcmd language excels in specifiying components and connections but
offers no computational capabilities.  As a result, ini files are limited in
the clarity and brevity that is possible with higher level languages.

The haltcl facility provides a means to use tcl scripting and its features for
computation, looping, branching, procedures, etc. in ini files.  To use this
functionality, you use the tcl language and the extension .tcl for halfiles.

The .tcl extension is understood by the main script (linuxcnc) that processes
ini files.  Haltcl files are identified in the the HAL section of ini files
(just like .hal files).

.Example
----
[HAL]
HALFILE = conventional_file.hal
HALFILE = tcl_based_file.tcl
-----

With appropriate care, .hal and .tcl files can be intermixed.

== Compatibility

The halcmd language used in .hal files has a simple syntax that is actually a
subset of the more powerful general-purpose tcl scripting language.


== Haltcl Commands

Haltcl files use the tcl scripting language augmented with the specific
commands of the LinuxCNC hardware abstraction layer (HAL).  The hal-specific
commands are.

....
addf, alias,
delf, delsig,
getp, gets
ptype,
stype,
help,
linkpp, linkps, linksp, list, loadrt, loadusr, lock,
net, newsig,
save, setp, sets, show, source, start, status, stop,
unalias, unlinkp, unload, unloadrt, unloadusr, unlock,
waitusr
....

Two special cases occur for the 'gets' and 'list' commands due to
conflicts with tcl builtin commands.  For haltcl, these
commands must be preceded with the keyword 'hal'.

....
halcmd   haltcl
------   ------
gets     hal gets
list     hal list
....


== Haltcl Inifile variables

Inifile variables are accessible by both halcmd and haltcl but with
differing syntax.

LinuxCNC ini files use SECTION and ITEM specifiers to identify
configuration items.

----
[SECTION_A]
ITEM1 = value_1
ITEM2 = value_2
...
[SECTION_B]
...
----

The ini file values are accessible by text substition in .hal files using the
form.

----
[SECTION]ITEM
----

The same ini file values are accessible in .tcl files using the form of a tcl
global array variable.

----
$::SECTION(ITEM)
----

For example, an ini file item like:

----
[JOINT_0]
MAX_VELOCITY = 4
----

is expressed as   [JOINT_0]MAX_VELOCITY  in .hal files for halcmd +
and as          $::JOINT_0(MAX_VELOCITY) in .tcl files for haltcl +

Because inifiles can repat the same ITEM in the same SECTION multiple times,
$::SECTION(ITEM) is actually a Tcl list of each individual value.

When there is just one value and it is a simple value (all values that
are just letters and numbers without whitespace are in this group), then it is
possible to treat $::SECTION(ITEM) as though it is not a list.

When the value could contain special characters--quote characters,
curly-brace characters, embedded whitespace, and other characters that
have special meaning in Tcl--it is necessary to distinguish between the list of
values and the initial (and possibly only) value in the list.

In Tcl, this is written `[lindex $::SECTION(ITEM) 0]`.

For example: given the following ini values
----
[HOSTMOT2]
DRIVER=hm2_eth
IPADDR="10.10.10.10"
BOARD=7i92
CONFIG="num_encoders=0 num_pwmgens=0 num_stepgens=6"
----
And this loadrt command:
----
loadrt $::HOSTMOT2(DRIVER) board_ip=$::HOSTMOT2(IPADDR) config=$::HOSTMOT2(CONFIG)
----
Here is the actual command that is run:
----
loadrt hm2_eth board_ip={"10.10.10.10"} config={"num_encoders=0 num_pwmgens=0 num_stepgens=6"}
----
This fails because loadrt does not recognize the braces.

So to get the values just as entered in the ini file, re-write the loadrt line like this:
----
loadrt $::HOSTMOT2(DRIVER) board_ip=[lindex $::HOSTMOT2(IPADDR) 0] config=[lindex $::HOSTMOT2(CONFIG) 0]
----

== Converting .hal files to .tcl files

Existing .hal files can be converted to .tcl files by hand editing to adapt to
the differences mentioned above.  The process can be automated with scripts
that convert using these substitutions.

----
[SECTION]ITEM ---> $::SECTION(ITEM)
gets          ---> hal gets
list          ---> hal list
----

== Haltcl Notes

In haltcl, the value argument for the 'sets' and 'setp' commands
is implicitly treated as an expression in the tcl language. 

.Example
----
# set gain to convert deg/sec to units/min for JOINT_0 radius
setp scale.0.gain 6.28/360.0*$::JOINT_0(radius)*60.0
----

Whitespace in the bare expression is not allowed, use quotes
for that:

----
setp scale.0.gain "6.28 / 360.0 * $::JOINT_0(radius) * 60.0"
----

In other contexts, such as 'loadrt', you must explicitly use
the tcl expr command ([expr {}]) for computational expressions.

.Example
----
loadrt motion base_period=[expr {500000000/$::TRAJ(MAX_PULSE_RATE)}]
----

== Haltcl Examples

Consider the topic of 'stepgen headroom'.  Software stepgen runs best with an
acceleration constraint that is "a bit higher" than the one used by the motion
planner.  So, when using halcmd files, we force inifiles to have a manually
calculated value.

----
[JOINT_0]
MAXACCEL = 10.0
STEPGEN_MAXACCEL = 10.5
----

With haltcl, you can use tcl commands to do the computation and
eliminate the STEPGEN_MAXACCEL inifile item altogether.

----
setp stepgen.0.maxaccel $::JOINT_0(MAXACCEL)*1.05
----

Another haltcl feature is looping and testing.  For example, many simulator
configurations use "core_sim.hal" or "core_sim9.hal" hal files.  These differ
because of the requirement to connect more or fewer axes.  The following haltcl
code would work for any combination of axes in a trivkins machine.

----
# Create position, velocity and acceleration signals for each axis
set ddt 0
for {set jnum 0} {$jnum < $::KINS(JOINTS)} {incr jnum} {
  # 'list pin' returns an empty list if the pin doesn't exist
  if {[hal list pin joint.${jnum}.motor-pos-cmd] == {}} {
    continue
  }
  net ${jnum}pos joint.${jnum}.motor-pos-cmd => joint.$axno.motor-pos-fb \
                                             => ddt.$ddt.in
  net ${axis}vel <= ddt.$ddt.out
  incr ddt
  net ${axis}vel => ddt.$ddt.in
  net ${axis}acc <= ddt.$ddt.out
  incr ddt
}
puts [show sig *vel]
puts [show sig *acc]
----

== Haltcl Interactive

The halrun command recognizes haltcl files.  With the -T option,
haltcl can be run interaactively as a tcl interpreter.  This
capability is useful for testing and for standalone hal applications.

.Example
----
$ halrun -T haltclfile.tcl
----


== Haltcl Distribution Examples (sim)

The configs/sim/axis/simtcl directory includes an ini file that uses a .tcl file
to demonstrate a haltcl configuration in conjunction with the usage of
twopass processing.  The example shows the use of tcl procedures, looping, the
use of comments, and output to the terminal.

