questions about threads on teensy 4.1

msaine

Active member
I have a couple of questions about threads on teensy 4.1

1: Can you call threads.suspend() from in side a thread? If I know the id of the thread can I suspend it from within that thread with the plan on restarting it from outside in the main loop. i.e.
threads.suspend(Lthread_id );

2. if a thread is suspended the while loop inside the thread is not executing, correct??

The situation I have is two threads running a PID on each for two separate motors, one motor per thread. The problem I am trying to solve is the annoying whine when the error is so small that the PWM period signal generated is not long enough to actually move the motors any longer but the error has not gone to 0. So I thought I could just suspend the thread when the error has reached an acceptable limit and then restart the thread when a new position is required.

What happened when I tried this was the while loop appears to still being executed as indicated by a simple serial.print of a "." at the top of the while and a print of "sus" at the error checking portion. to my surprise I got a continuous ".sus" which tells me the while loop is still executing. After adding a couple more prints to second thread using different char for second thread ("_" and "Lsus") I get a series of ".Rsus" followed by a series of "_Lsus" as if they are still running for their time slice.

I also noticed that the "SUSPENDED" state value is not recognized as defined by Arduino IDE

MazeToy:219: error: 'SUSPENDED' was not declared in this scope
219 | if ( threads.getState(Rthread_id) == SUSPENDED)
| ^~~~~~~~~

if I replace the SUSPENDED with the 4 value it works

obviously I am missing something :) but cannot see it.

please be kind to the attached code, it is a work in progress.
 

Attachments

  • MazeToy.ino
    9.8 KB · Views: 23
Linking real time behaviour to alogorithm values is not a good idea. Hardly reusable, and unpredictable reactions.
Implement a fixed real time behaviour and let the algorithm check if PWM should be updated, depending on limit values.
 
Yes servomotors can make noise when "stationary". Even with an I term you can get limit cycles. Servo motors usually have very high resolution encoders for this reason. In the old days tachogenerators and resolvers were used for PID loop feedback, both analog.
 
I would still like answers to the two questions. Can a task suspend itself and if suspended why is the loop still executing? I understand what I am/was doing may not be the best way but would still like an answer. Thanks in advance.
 
I would still like answers to the two questions. Can a task suspend itself and if suspended why is the loop still executing? I understand what I am/was doing may not be the best way but would still like an answer. Thanks in advance.
I've never used TeensyThreads, but I looked at the source code. suspend() takes an argument of a task id, and all it does is set the task state to SUSPENDED. It doesn't trigger a task switch, so I think if a task called it with its own id, the task would at least run to the end of its time slice. What you might want to do instead is call sleep(ms), which sets the calling task to SUSPENDED for the specified number of milliseconds and does a task switch.
 
This seems like the wrong way to go about things, if you want a thread to block/sleep make it wait on a semaphore that gets signalled whenever it should resume.
 
Back
Top