Real Newbie needs some help and advice

More
09 Feb 2021 22:45 #198210 by tommylight
For me HAL was the best thing ever since i know electronics so that just worked for me.
And it is the single most reason i love EMC2/LinuxCNC.
The following user(s) said Thank You: rodw

Please Log in or Create an account to join the conversation.

More
09 Feb 2021 22:56 #198213 by The Feral Engineer
I like both and will continue to use both :)
The following user(s) said Thank You: Mike_Eitel

Please Log in or Create an account to join the conversation.

More
10 Feb 2021 01:07 #198234 by rodw

For me HAL was the best thing ever since i know electronics so that just worked for me.
And it is the single most reason i love EMC2/LinuxCNC.


But the ability to write your own component to extend the system is the best thing ever since I know programming so that just worked for me.
And it is the single most reason i love EMC2 Linuxcnc....


Really the architecture of Hal where complex systems can be broken up into small discrete components firewalled from the rest of the system and you can write your own which is treated as if its part of the core code is Hal's hidden gem (for those that venture there). And it often gets rid of the obfuscation in hal files some have mentioned...

Please Log in or Create an account to join the conversation.

More
10 Feb 2021 02:08 #198237 by The Feral Engineer
All i know about C is #include<stdio.h> and even that is probably wrong

Please Log in or Create an account to join the conversation.

More
10 Feb 2021 05:31 #198244 by rodw

All i know about C is #include<stdio.h> and even that is probably wrong


Well with halcompile you don't even need that!

Please Log in or Create an account to join the conversation.

More
10 Feb 2021 05:39 #198247 by The Feral Engineer
Can you send me information on getting that to work? I tried to find information on it and it was piecemeal at best. I got busy, so I kinda gave up my search for a while, but I'd love to get into that. If you have anything you can send me to get me started with it, I'd greatly appreciate it.

Please Log in or Create an account to join the conversation.

More
10 Feb 2021 06:35 #198249 by rodw
The halcompile docs are pretty good.
linuxcnc.org/docs/devel/html/hal/comp.html
But then duck over to git and have a look at the source for some of the core components (like and2) in this folder for inspiration. You learn so much just reading code.
github.com/LinuxCNC/linuxcnc/tree/master/src/hal/components

Copy one of these and use it as a template to write your own component. Thats how I do it!

Just ignore all the documentation stuff at the beginning. They are processed to make the man pages.

You just have to remember that the servo thread is a timer interrupt service loop that fires every millisecond and every hal component has to be serviced in that millisecond or following errors will occur. So a component needs to short sharp and sweet! Do not use any for or while loops. You do not need to use them as you can simply loop using the servo loop (eg increment a variable this time round and increment it again the next invocation until you get the result you want!.

You will also need to understand variable scope, eg. global variables, and static variables. Basically, global variables above the function keep their value between loops. variables in the function are created on every call so there is no memory of its value unless it is defined as a static variable.

Often you need to do something on the first pass through a component to set some values or something. I usually do it something like this:
static int first_pass = 1;
if first_pass{
  // do some configuration stuff
  first_pass = 0;
}

Or you want to save a value so you can use it next time (which may also require some config on the first pass.
static int last_value;
int this_value = my_hal_pin;
int delta = this_value - last_value;
// do some stuff
//At the end, save this value to be last value
last_value = this_value;

The other useful construct is a state machine. State machines have several steps that they will pass through and you can use a switch() statement to work though each step. once step 1 is satisfied, set a variable to the next step and so on and then the last state sets it back to the first state.

plasmac.comp has a lot of states defined from line 266 as an ENUM. The state machine begins on line 609 with
        switch(state){
            case IDLE:
and through the code you will see references to the state like
            case TORCH_ON:
And at the end of that section, the next state is set
state = ARC_OK;
So the next pass through the process, execution will commence at
 case ARC_OK:

Mind you Plasmac is not a stellar example of the short sharp sweet components I started with but careful use of states allows this 1800 line component work perfectly! It would be possible to break it up into smaller components at the expense of greater complexity in the hal file. If you look at the core components, they are often just a couple of lines or < 10 lines.

Anyway, that should get you going.
The following user(s) said Thank You: The Feral Engineer

Please Log in or Create an account to join the conversation.

More
10 Feb 2021 23:06 #198334 by The Feral Engineer
Very eager to get on this. Thank you, sir.

Please Log in or Create an account to join the conversation.

Time to create page: 0.209 seconds
Powered by Kunena Forum