halcmd excels in specifying components and connections but these scripts offer 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
[HAL]
HALFILE = conventional_file.hal
HALFILE = tcl_based_file.tcl

With appropriate care, HAL and Tcl files can be intermixed.

1. 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.

2. 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:

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

3. 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:

[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:

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 INI files can repeat 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) then 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:

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]

4. 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

5. 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)}]

6. 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.

[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:

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]

7. 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

8. 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.