Program cycle.

This forum is about you. Feel free to discuss anything is related to embedded and electronics, your awesome projects, your ideas, your announcements, not necessarily related to ChibiOS but to embedded in general. This forum is NOT for support.
Post Reply
James144
Posts: 1
Joined: Fri May 16, 2014 9:10 am

Program cycle.

Post by James144 »

Hello I started working on my school project with ChibiOS and STM32F4 discovery few weeks ago. I´m absolute newbie. I have already tested reading and writing IO, ADC, PWM, making threads, but nothing complex. So maybe someone with more experience could give me an advice with this:

Im trying to make program which will work in cycle: Reading inputs(DI,AI), Main program(calling subroutine), Setting outputs(DO), Communication in backcround. Something like PLC cykle.


What is the best way to make program like this? How to use semaphores, mutex, threats atp. Im asking this because doing mistake at the beginning of desing, create a lot of problems in future.


Thank you very much and sorry for my english. :oops:
Attachments
program cycle
program cycle
cycle.png (7.65 KiB) Viewed 3109 times
User avatar
Giovanni
Site Admin
Posts: 14891
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: Program cycle.

Post by Giovanni »

It is a simple loop application, probably you will not need any threading from what I can see.

I can suggest an approach, try writing down the "requirements" for your application and start from that for your design analysis. A requirement is a small tagged paragraph that describes a specific aspect of your application, for example:

MYAPP12: The input sensor shall be read ten times per seconds.
MYAPP13: Communication shall be performed in background without affecting the main loop.
MYAPP14: ...
etc


Keep them short and unambiguous. Once you have the full list of your requirements we can start thinking to how to translate those into a design.

Giovanni
User avatar
Vic20
Posts: 15
Joined: Fri Apr 25, 2014 7:45 pm

Re: Program cycle.

Post by Vic20 »

The system requirements are not clear enough to give a proper response.

How real-time is the system?

Do the measures need to be done a exact intervals? What is the interval time?

That could need, at least one GPT.

Do the outputs need to be generated also at exact intervals?

Does the jitter matter to you?

What kind of communication? Serial, TCP/IP...

Does it need user interaction using buttons or an LCD for instance?

As a first approximation I would use a thread for communication that uses a mutex to protect the shared memory area with the main program and a semaphore for the a data update signal.
The main thread could do all the normal cycle.
If there is some need for user interaction a part from the communication it could be done in another thread.

As a general rule I would put anything that could block or could be slow in a thead independent with the main loop thread.

Best regards

Vicente
Post Reply