2
\$\begingroup\$

I am learning to use a RTOS, CMSIS RTOS in this case, so my question is basic. I want to know what is the best way to share information between two tasks.

What I am trying to do is basically a controller with some external communication. This system has two tasks.

Task 1. Controller task:

  • It is executed at fixed intervals, each X ms, as fast as possible
  • It has a setpoint
  • It reads several sensors
  • It calculates and sets several outputs

Task 2. Communication task:

  • It’s a slave in a master/slave communication (Modbus). So, it only communicates when it receives a request from master
  • It must be able to set the setpoint
  • It must be able to read the value/status of sensors and outputs

How should these two tasks communicate? If I was not using an RTOS I will use private global variables, wrapped with some public get/set functions. But as far as I understand that is not the way to do it in a RTOS.

Should I use a queue where the communication task writes a request message, that the controller task will check every iteration? The problem I see with this solution is that the controller task must execute as fast as possible, so either the controller task only replies to one request each iteration making the communication task less responsive or, it responses to all the request making its execution time longer.

\$\endgroup\$
  • \$\begingroup\$ You're saying that when a comms request comes in, you need to send out a sensor value? If so, what is the required response time from that message being sent out from the sending device to the value being returned by your device with the RTOS? \$\endgroup\$ – DiBosco Sep 14 '17 at 7:55
  • \$\begingroup\$ Multi-zone furnace? Or why is there only one setpoint? How are the measurements fused? \$\endgroup\$ – jonk Sep 14 '17 at 12:07
  • \$\begingroup\$ I think the private variables with public get/set functions would still be fine in your RTOS design. Just make sure the get/set functions are thread safe. Why does the RTOS make you doubt this design? \$\endgroup\$ – kkrambo Sep 14 '17 at 13:20
  • \$\begingroup\$ @kkrambo I am new using RTOS. I thought I should try to avoid global variables and only use variables defined inside the tasks. I don't know when the use of globals is acceptable. \$\endgroup\$ – JHG Sep 15 '17 at 5:46
  • \$\begingroup\$ @jonk It is not strictly a control system. It is more like a state machine where first I read the sensors and the actuate on the outputs. Different setpoint values will use different parts of the system \$\endgroup\$ – JHG Sep 15 '17 at 5:48
1
\$\begingroup\$

This is a good way to pass data between tasks, yes. If it's anything like FreeRTOS, you'll also be able to block the receiving task until something is in the queue (if you wish). Or, you can check and drop through or wait for a specified time before dropping through to the next instruction in the task. This blocking, to my mind, is one of the real beauties of running an RTOS. To be able to have one task waiting for another task to pass it data can make your life a lot easier and queues enable this in a thread safe manner.

You also have the facilities to check whether a queue is full before writing to it and, if it is full, the possibility of waiting until there is space before writing to it.

You will have the option, I assume, to write lots of different types of data to the queues, including structures. Queues are very useful and a safe way of passing data.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.