Skip to content

Software Threads

Pascal Roobrouck edited this page Aug 22, 2019 · 1 revision

As a motion controller has to do a mix of real-time tasks (requiring low latency) and some high-level computing (of which the computing time can be unpredictable), we need some different threads to separate those.

All non-real-time computing is done in a main 'background' thread. This is organised as an endless loop, and the highest priority tasks are done first, and when done the next priority is taken etc.

All real-time tasks are running in separate threads, which are triggered from hardware interrupts. So they interrupt the main thread, and if needed they interrupt each other, as interrupts can have priorities as well.

Moovr has the following interrupt threads:

  • a thread triggered by timer interrupts to handle the output of step and direction signals to stepper motor drivers.
  • a thread triggered by a periodic timer interrupt to read all buttons and switches, properly debounce them and detect state changes.
  • a thread to service the serial interface : bytes being received, or transmit buffer empty events.

Using multiple threads requires careful design of how to exchange data between the threads. The different interrupt threads do not exchange data between them. They only exchange data with the main background thread. In order to make this safe, interrupts are disabled briefly when the main thread accesses the shared data members:

  • reading the rxBuffer from the serial hostInterface
  • writing the txBuffer to the serial hostInterface
  • reading the eventBuffer
Clone this wiki locally