Organization of C programs in the context of threads.

Moderators: TomKerekes, dynomotion

Post Reply
Alexanders
Posts: 24
Joined: Wed May 03, 2023 12:54 am

Organization of C programs in the context of threads.

Post by Alexanders » Wed May 03, 2023 1:19 am

In the folder C Programs there are many examples with functions for (;;) and Delay_sec() and others, excluding the correct sequential execution of other functions. There are only 7 threads allocated for the user.
Please explain and give examples of simultaneous use of different C programs in the same stream in real time and without delay.
I also ask you to clarify in detail the operation and scope of the WaitNextTimeSlice() function.

User avatar
TomKerekes
Posts: 2546
Joined: Mon Dec 04, 2017 1:49 am

Re: Organization of C programs in the context of threads.

Post by TomKerekes » Wed May 03, 2023 4:36 pm

In the folder C Programs there are many examples with functions for (;;) and Delay_sec() and others, excluding the correct sequential execution of other functions. There are only 7 threads allocated for the user.
Please explain and give examples of simultaneous use of different C programs in the same stream in real time and without delay.
Sorry I don't understand the question. What are you trying to do?

I also ask you to clarify in detail the operation and scope of the WaitNextTimeSlice() function.
This waits until the beginning of the Threads next Time Slice. See here. Useful when some code should be executed at some periodic rate (multiple of 90us sample times). Or to provide execution of ~40us of code without the possibility of interruptions.
Regards,

Tom Kerekes
Dynomotion, Inc.

Alexanders
Posts: 24
Joined: Wed May 03, 2023 12:54 am

Re: Organization of C programs in the context of threads.

Post by Alexanders » Wed May 10, 2023 10:27 pm

I understand how the WaitNextTimeSlice() function works.
How to apply it correctly? Before the start of the desired function or inside it or after it or inside the brackets { }? And also how many lines of code does it work on?

Alexanders
Posts: 24
Joined: Wed May 03, 2023 12:54 am

Re: Organization of C programs in the context of threads.

Post by Alexanders » Thu May 11, 2023 10:08 am

And two more questions.

1. What is the difference between the Halt and FeedHoldStop functions ?

2. I want to duplicate with an external CycleStart/Halt button. I understand how to pass button commands to KMotionCNC. But I don't know how to read the status of Halt and CycleStart from KMotionCNC ?

User avatar
TomKerekes
Posts: 2546
Joined: Mon Dec 04, 2017 1:49 am

Re: Organization of C programs in the context of threads.

Post by TomKerekes » Thu May 11, 2023 5:47 pm

I understand how the WaitNextTimeSlice() function works.
How to apply it correctly? Before the start of the desired function or inside it or after it or inside the brackets { }?
Like any function it can be placed anywhere where code is executed.

And also how many lines of code does it work on?
The function doesn't operate on any lines of code. It waits till the beginning of the next time slice then returns. Please read my previous response.

What is the difference between the Halt and FeedHoldStop functions ?
What is 'FeedHoldStop '? To access the Help Manuals for a description of a button click 'Help' then click the button.

I want to duplicate with an external CycleStart/Halt button. I understand how to pass button commands to KMotionCNC. But I don't know how to read the status of Halt and CycleStart from KMotionCNC ?
There is a define in KFLOP called JOB_ACTIVE which can be used to determine if GCode is executing or not.
Regards,

Tom Kerekes
Dynomotion, Inc.

Alexanders
Posts: 24
Joined: Wed May 03, 2023 12:54 am

Re: Organization of C programs in the context of threads.

Post by Alexanders » Fri May 12, 2023 9:31 am

The function doesn't operate on any lines of code. It waits till the beginning of the next time slice then returns.
That is, this function loops code execution inside itself until the next time slice?
In other words, in the code:

Code: Select all

main()
{  
      for(;;)
            {
              function 1();
               WaitNextTimeSlice();
              function 2();
               WaitNextTimeSlice();
              function 3();
               WaitNextTimeSlice();
              function 4();
               WaitNextTimeSlice();
            }
}
Each function will be repeated at intervals = (# User Threads + 1) * TIMEBASE * 4 ?
Do I understand correctly?

User avatar
TomKerekes
Posts: 2546
Joined: Mon Dec 04, 2017 1:49 am

Re: Organization of C programs in the context of threads.

Post by TomKerekes » Fri May 12, 2023 3:36 pm

That is, this function loops code execution inside itself until the next time slice?
Yes

Each function will be repeated at intervals = (# User Threads + 1) * TIMEBASE * 4 ?
Do I understand correctly?
Yes. Assuming none of those functions take longer than ~40us to execute.

You can check these things yourself using Time_sec() which has 20ns resolution. WaitNextTimeSlice also returns the time.

Function names can not contain spaces.

When showing your code its nice to indent your code in a consistent manner to make it more readable. See here. And Video:

Regards,

Tom Kerekes
Dynomotion, Inc.

Post Reply