:lang: en
:toc:

[[cha:haltcl]]
= HALTCL Files

// Custom lang highlight
// must come after the doc title, to work around a bug in asciidoc 8.6.6
:ini: {basebackend@docbook:'':ini}
:hal: {basebackend@docbook:'':hal}
:ngc: {basebackend@docbook:'':ngc}

The halcmd language excels in specifying 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 HAL files.

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
[source,{ini}]
----
[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:

[source,{hal}]
----
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 INI-file variables

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

LinuxCNC INI files use SECTION and ITEM specifiers to identify
configuration items:

[source,{ini}]
----
[SECTION_A]
ITEM1 = value_1
ITEM2 = value_2
...
[SECTION_B]
...
----

The INI file values are accessible by text substitution in .hal files using the
form:

[source,{hal}]
----
[SECTION]ITEM
----

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

[source,tcl]
----
$::SECTION(ITEM)
----

For example, an INI file item like:

[source,{ini}]
----
[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 INI files 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

[source,{ini}]
----
[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:

[source,tcl]
----
loadrt $::HOSTMOT2(DRIVER) board_ip=$::HOSTMOT2(IPADDR) config=$::HOSTMOT2(CONFIG)
----

Here is the actual command that is run:

[source,{hal}]
----
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:

[source,tcl]
----
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
[source,tcl]
----
# 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:

[source,tcl]
----
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
[source,tcl]
----
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 INI files to have a manually
calculated value.

[source,{ini}]
----
[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 INI file item altogether:

[source,tcl]
----
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.

[source,tcl]
----
# 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.

// vim: set syntax=asciidoc:
