Saturday, 8 October 2016

TThread: Achieve Basic Concurrency, Real-Time Systems without using RTOS on any microcontroller

I previously worked in an automotive industry, and the way we did concurrency on a non-RTOS system is through case switches of time in multiples of 2^n:
case 1: // 1 millisecond
case 2: // 2 millisecond
case 4: // 4 millisecond
case 8: // 8 millisecond
case 16: // 16 millisecond
case 32: // 32 millisecond
... so on so forth

But as the program grows, we found that we need to implement many internal state machines and it could be problematic in terms of maintenance and code readability (because we need to jump from one place to another to figure out the flow of the program). But this is still one of the techniques that I may use someday.

I had been using TThread years ago and I think this is a good way to achieve concurrency. It doesn't have the heaviness of an RTOS and although it doesn't have the full features of an RTOS, it is very space efficient, it works, and can be used on any microcontrollers (note: it should have at least a timer interrupt)!  Here is one of my personal project in Arduino which is all about a concurrent, real time system which does the following:
1. pulsed laser for theft detection
2. trip wire for theft detection
3. water level monitoring for flood detection

Written in C++ (and with abstraction/polymorphism), Source code here:http://codepad.org/cb1H2QAa  (This is just a hobby code and everything was written on 1 file because Arduino IDE doesn't play well with multiple files) . On this code, I made some modifications on the TThread code to be able to accomodate up to more than 254 lines of code.


Credits to the author of TThread.  http://www.microchipc.com/sourcecode/tiny_threads/tthread.h

No comments:

Post a Comment