Y
Y
Yaroslav Malyarenko2021-03-28 23:32:44
Arduino
Yaroslav Malyarenko, 2021-03-28 23:32:44

Multiple parallel programs on AVR (or stm)?

Good day to all!
Direct the right newcomer on the path) Thanks in advance to everyone who responded!
How to execute several processes (subject to constant monitoring and instant response to the conditions in the "if" loop) without stopping the main program loop?
How to implement it?
Including the operation of the condition on the elapsed time.
For example - we read readings from four sensors - each sensor has its own control channels - one or more
, when the desired time arrives according to the schedule, we control one or several channels independently ..
while the stepper motor is spinning according to the schedule - the temperature sensor worked - the relay turned on
here the sensor humidity worked - turned on the fan
(and the motor turns still)
then the time has come - and the door opens for ventilation ..
and so on ..
How to implement this?

Answer the question

In order to leave comments, you need to log in

7 answer(s)
A
Alexander Gusev, 2021-03-28
@Sanchogus

The main cycle is spinning, and additional. interrupt tasks (from timers, from changing the input signal, etc.), or in idle time to check conditions. Judging by the description of the task, hard real-time is not needed and the run-up in microseconds-milliseconds is not critical.
Or, if you want something interesting and informative, then stm32 has freeRTOS out of the box, you will have a whole OS with its own processes. :) (for the rest, I think, you can also find your own RTOS) At least you can read the principle of operation and there will be ideas on how to make your multitasking.

B
BasiC2k, 2021-03-28
@BasiC2k

Through variables - flags.
Flag - the motor is spinning or not
Flag (value) of sensor 1
, etc.

L
lonelymyp, 2021-03-28
@lonelymyp

What kind of main loop is it that cannot be interrupted?
Everything can be interrupted, if not for long.
I hope the motor control is done through a hardware PWM? Then, in order to change the motor rotation parameters, you need to change the PWM parameters once, changed it - the motor will continue to spin on its own at a new speed, in the main program, check the sensors or whatever else is needed.

A
Alexander Skusnov, 2021-03-29
@AlexSku

On a good note, you need to learn one of the SFC (State Graph) PLC languages. A parallel structure is possible there (examples in Codesys).
There is an analogue in Matlab - Stateflow (there parallel structures are indicated by a dotted line). In Matlab, you can compile the graph into the ST controller language (structured text like Pascal). Video
example .

V
vanyamba-electronics, 2021-03-29
@vanyamba-electronics

FreeRTOS

E
evgeniy_lm, 2021-03-29
@evgeniy_lm

In any MK there is such a thing as interrupts, for example, in Atmega328 (this is the one in Arduino) there are already 26 of them for all the hardware that is there. There are three timers to synchronize processes. For good, the main program consists of an empty infinite loop, and all the logic of work is scattered over interrupt handlers.
You need to understand that in the AVR MK, interrupts do not have priority and are executed as they occur. While the interrupt handler is running, processing of other interrupts is automatically disabled, and the RETI exit field is enabled. If during the operation of one interrupt there were others, they will be processed upon completion in the order of their numbering. If there is an interrupt of the same type that is currently being processed, then it will not be processed.
In STM, the interrupt system is more complex and there is also DMA

Y
Yaroslav Malyarenko, 2021-03-30
@djsnakebest

e option with flags how will it work? .. can it be better?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question