How do .comp files work?

More
7 years 5 months ago #135313 by AgentWD40
Replied by AgentWD40 on topic How do .comp files work?

AgentWD40 wrote: Okay, so looking at the basic hal tutorial: You use loadrt to load the component, like importing a library in c like #import <>. Then you call addf to initialize it however many times you need it, kind of like a c++ object. Right? But in the case of the plasmac it's a "singleton" so it can only be intialized once.


Okay, that was a poor analogy.

loadrt loads the component into realtime, addf selects to which thread to add it's FUNCTION(_){}.

What is the scope of functions (besides the main FUNCTION(_) {} ) defined in a .comp file?

Please Log in to join the conversation.

More
7 years 5 months ago #135337 by andypugh
Replied by andypugh on topic How do .comp files work?
It might help to look at a component that is written in C rather than comp, and that exports multiple functions.

github.com/LinuxCNC/linuxcnc/blob/master...onents/mux_generic.c

This does much the same a the mux.comp files, but was written directly in C because it needs a hint more introspection, and also because I wanted it to export more than one function.
(look for the calls to "hal_export_funct"). This comp either exports "write_fp" or "write_nofp" depending on the configuration.
So the HAL code
Code:
addf mux_generic.0 servo-thread
will periodically call a different function in the code depending on the mux config. (the point being that if all data types are non-floating-point then the component is allowed to run in the base thread)

Please Log in to join the conversation.

More
7 years 5 months ago #135340 by rodw
Replied by rodw on topic How do .comp files work?

AgentWD40 wrote:

AgentWD40 wrote: What is the scope of functions (besides the main FUNCTION(_) {} ) defined in a .comp file?


I think there are two parts to the answer. There are functions that can be defined as being part of the comp. The pid component is an example you'll probably find in your hal file already.
Code:
pid.N.do−pid−calcs (uses floating-point) Does the PID calculations for control loop N.

My hal
Code:
loadrt pid names=pid.y,pid.y1,pid.x,pid.z addf pid.y.do-pid-calcs servo-thread addf pid.y1.do-pid-calcs servo-thread addf pid.x.do-pid-calcs servo-thread addf pid.z.do-pid-calcs servo-thread

But there is nothing wrong in using functions within a component to make the file more readable in which case they would be private to the component.

By now you've probably realised that halcompile processes a .comp C file into a format understood by the compiler (gcc I guess), compiles it and with --install, copies the object file to the correct directory where it can be found when your hal file loads it.

Please Log in to join the conversation.

Time to create page: 0.037 seconds
Powered by Kunena Forum