Skip to content

Instantly share code, notes, and snippets.

@jschoch
Created January 1, 2025 17:34
Show Gist options
  • Save jschoch/8cbde484c70d5555a80cb824dd802016 to your computer and use it in GitHub Desktop.
Save jschoch/8cbde484c70d5555a80cb824dd802016 to your computer and use it in GitHub Desktop.
from Cheetah.Template import Template
# Template string
template_str = """
#include <stdio.h>
#ifdef __cplusplus
extern "C" {
#endif
void greet(const char* name) {
#for $i in range(3):
printf("Hello, $name! (Iteration $i+1)\\n");
#end for
}
int main() {
greet("$greeting_target");
return 0;
}
#ifdef __cplusplus
}
#endif
"""
# Context (data to be passed to the template)
context = {
'greeting_target': "World",
}
try:
# Create the template object
template = Template(template_str, searchList=[context])
# Generate the C code
generated_c_code = str(template)
# Print the generated code (or write it to a file)
print(generated_c_code)
# Optionally, write to a file:
with open("generated_code.c", "w") as f:
f.write(generated_c_code)
except Exception as e:
print(f"Error generating code: {e}")
from Cheetah.Template import Template
# HAL File Template
hal_template_str = """
# Generated HAL Configuration
loadrt [KINS]KINEMATICS
loadrt [EMCMOT]EMCMOT base_period_nsec=[BASE_PERIOD] servo_period_nsec=[SERVO_PERIOD]
# Axis setup
#for $axis in $axes:
setp hm2_[HOSTMOT2](BOARD).0.axis-$axis.scale $scales[$axis]
net axis-$axis-pos-cmd joint-$axis.pos-cmd => hm2_[HOSTMOT2](BOARD).0.axis-$axis.pos-cmd
net axis-$axis-fb joint-$axis.f-pos-fb <= hm2_[HOSTMOT2](BOARD).0.axis-$axis.f-pos-fb
#end for
# Other components...
loadusr -Wn halscope halscope
# Net signals
net estop-out <= iocontrol.0.user-enable-out
net estop-in iocontrol.0.emc-enable-in <= estop-out
# ... more nets
"""
# INI File Template
ini_template_str = """
# Generated INI Configuration
[GLOBAL]
VERSION = 1.1
MACHINE = My Machine
DEBUG = 0
[EMC]
MACHINE_NAME = My Machine
NML_FILE = mymachine.nml
HALFILE = mymachine.hal
HALUI = halui
[RS274NGC]
PARAMETER_FILE = mymachine.var
[EMCMOT]
EMCMOT = emcmot
COMM_TIMEOUT = 1.0
SERVO_PERIOD = $SERVO_PERIOD
BASE_PERIOD = $BASE_PERIOD
[TASK]
TASK = milltask
CYCLE_TIME = 0.001
# Axes Configuration
#for $axis in $axes:
[AXIS_$axis]
TYPE = LINEAR
MAX_VELOCITY = $max_velocities[$axis]
MAX_ACCELERATION = $max_accelerations[$axis]
SCALE = $scales[$axis]
FERROR = 0.05
MIN_LIMIT = $min_limits[$axis]
MAX_LIMIT = $max_limits[$axis]
HOME = $homes[$axis]
HOME_OFFSET = $home_offsets[$axis]
HOME_SEQUENCE = 1
#end for
[DISPLAY]
DISPLAY = gmoccapy
EDITOR = gedit
POSITION_OFFSET = RELATIVE
POSITION_FEEDBACK = ACTUAL
MAX_FEED_OVERRIDE = 1.2
CYCLE_TIME = 0.1
INTRO_GRAPHIC = linuxcnc.gif
INTRO_TIME = 5
[INPUT]
# ...
[OUTPUT]
# ...
"""
# Context data
context = {
'axes': ['X', 'Y', 'Z'],
'scales': [250, 250, 250], # steps/mm for example.
'max_velocities': [5000, 5000, 5000],
'max_accelerations': [50000, 50000, 50000],
'min_limits': [-100, -100, -100],
'max_limits': [100, 100, 100],
'homes': [0, 0, 0],
'home_offsets': [0, 0, 0],
'BASE_PERIOD': 100000, # nanoseconds
'SERVO_PERIOD': 1000000, # nanoseconds
'HOSTMOT2': "hm2", # example hostmot2 name.
'BOARD': 0 # example board number.
}
try:
# Generate HAL file
hal_template = Template(hal_template_str, searchList=[context])
generated_hal = str(hal_template)
with open("mymachine.hal", "w") as hal_file:
hal_file.write(generated_hal)
# Generate INI file
ini_template = Template(ini_template_str, searchList=[context])
generated_ini = str(ini_template)
with open("mymachine.ini", "w") as ini_file:
ini_file.write(generated_ini)
print("HAL and INI files generated successfully.")
except Exception as e:
print(f"Error generating files: {e}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment