Connect with us

Concurrent operation in real time embedded systems (C)

Discussion in 'Electronics Homework Help' started by Joshua Wilkinson, Nov 15, 2017.

  1. Joshua Wilkinson

    Joshua Wilkinson

    4
    1
    Nov 15, 2017
    Good morning,

    Please bear with me as I am very new to programming and I am currently working on a third year university project (thrown in at the deep end!). I am trying to get two tasks to run simultaneously and I am not sure of the process. I really don't understand how programming works and I am trying my best to look online. So far I have found some information on parallelism and queuing. I don't know if this is what I need and the information I have found seems quite complex and pitched at a level where prior knowledge is assumed. Can anybody help explain, in simple terms, what it is I need to be looking at?

    Many thanks,

    Josh
     
  2. Harald Kapp

    Harald Kapp Moderator Moderator

    9,360
    1,900
    Nov 17, 2011
    Welcome to EP, Josh.

    Unless your processor has two (or more) cores you cannot run two tasks simultaneously. You can simulate quasi-simultaneous operation by alternatingly serving either task 1 or task 2 in quick succession which looks like the processor is doing both tasks in paralel as long as the timing requirements of the tasks are fulfilled. The process is know as 'multitasking'.

    A scheduler assigns the available processing power and time to the tasks. A very simple scheme uses a timer interrupt which triggers the scheduler to switch between the tasks.

    Here are a few ressources that may help you along:
    https://www.embedded.com/electronics-news/4434501/Writing-a-simple-cooperative-scheduler
    https://www.embedded.com/design/pro...n-as-close-as-you-can-get-to-one-line-of-code
    http://www.best-microcontroller-projects.com/multitasking.html

    btw: I will move the thread to the homework section as it is related to your university project.

    Cheers,
    Harald
     
  3. hevans1944

    hevans1944 Hop - AC8NS

    4,246
    1,996
    Jun 21, 2012
    What are the two tasks that you want to run simultaneously? Do you really need simultaneity, or is concurrently more apropos to your situation? As Harald said in his post #2, you need multiple processors or cores to execute two or more tasks simultaneously. One core, programmed to time-share, can run two or more tasks concurrently. Please explain what you are trying to DO.

    A computer is a deterministic machine that rapidly executes, one at a time, a series of sequential instructions to perform a task. Programming is the procedure you use to create the series of sequential instructions. If a computer is designed with more than one core processor, appropriate programming can cause each core to execute instructions independently of the all other cores, as well as simultaneously with the other cores. Independent operation of two or more cores is hardly, if ever, done because generally multiple cores are programmed to operate cooperatively to accomplish an overall task more quickly than a single core operating alone. Coordinating the operation of multiple cores and distributing the task execution load evenly among them is a non-trivial programming task.

    An alternative to multiple cores is sharing the time used by a single processor core. Here, programming allows multiple tasks to execute in a pre-determined sequence as determined by the programmer. Each task is allowed a certain amount of time to either execute or run until blocked. When the allotted time expires, or the task becomes blocked, the next task is allowed to execute. A special program, called a task scheduler, determines the order of task execution. Clearly, the task scheduler must run concurrently with the scheduled tasks. That means there must always be a mechanism that interrupts each executing task to allow the task scheduler to run. There are many ways to accomplish this, but a hardware timer that interrupts the processor periodically is about as simple as it gets.

    A task is said to be "blocked" when it can no longer accomplish anything because it is waiting for something else to happen. Computers are very fast at executing programmed instructions, so to allow a task to simply sit idle in a "wait loop" while it waits for something else to happen is not to be allowed. As soon as a task is blocked, the task scheduler must interrupt the blocked task and allow another task to execute, even if the allotted time for the blocked task has not yet expired.

    All of the processing required to launch tasks and arbitrate their execution is overhead, time that is not spent executing task instructions. To minimize overhead, and relieve the burden on programmers, operating systems (OS) were created. That is where you should begin your investigation of how to get two tasks to run "simultaneously". Study the Application Programming Interface (API) for the OS of your choice to see how to launch two tasks that the OS will schedule to run either simultaneously or concurrently, depending on what kind of hardware the OS is running on.

    You appear to be ill-prepared to solve your "problem" of how to get two tasks to run simultaneously. If you are "new to programming, and don't understand how programming works," trying to accomplish two tasks simultaneously will probably be impossible, unless you choose to use two computers, dedicating each task to a specific computer. There is nothing wrong with that idea, because computers are becoming cheap as dirt, but generally there is more to the problem than just "getting two tasks to run simultaneously." Almost always, the tasks need to communicate with each other and with the real world to accomplish an overall goal. And that, again, is not a trivial programming exercise.

    So, again, we ask you: What are you really trying to DO, so we can better advise you on HOW to do it? On what kind of computer, or computers, will the two tasks execute? What operating system, if any, will be used? What is the program development environment? What programming language will you use? And what, exactly, did you learn in your first two years at university that is in any way applicable to solving your problem?
     
    Harald Kapp likes this.
  4. Joshua Wilkinson

    Joshua Wilkinson

    4
    1
    Nov 15, 2017
    Many thanks for both of your replies, you have already been very helpful.

    Some more detail of what I am actually trying to achieve:

    We have a conveyor belt. It has two sensors at the start, then a gate that opens and closes to remove small blocks from the conveyor and let large ones pass, there is then a sensor at the end of the conveyor. Very simply, small blocks should be knocked off the conveyor using the gate and large ones are allowed to pass to the end. We must also use the sensors to count and display the numbers of each size of blocks counted.

    I am progressing quite well at the moment. The system is counting and displaying the numbers of each size of block and it is sorting the blocks. The problem however is that I am using a delay from reading the sensors before the block reaches the gate and again once the gate closes to sort the small blocks. This delay is blocking the read sensors command and as such if there are two small blocks in quick succession the sensor misses the second small block and allows it to pass. I need to try and find some way to maintain the delay but to also read the sensors at the same time.

    My employer has sent me to university and I have not done the first two years. this year (3rd year) is my first year, I was accepted on the program because of recognised prior learning achieved through work. The software I am using is VxWorks and the language is C.

    Thanks again for taking the time to read through my issues and for the great advice.
     
    hevans1944 likes this.
  5. Harald Kapp

    Harald Kapp Moderator Moderator

    9,360
    1,900
    Nov 17, 2011
    Obviously you're using a delay() function or macro. This is not well suited for this task as it blocks all other operations of the processor by putting it into a loop to achieve the delay. Concurrent programming will not be very useful here as the timing of the delay loop will be distorted if a scheduler interrupts it for other tasks.

    In your case I recommend you use a timer and a queue:

    • Set up a timer such that it will create an interrupt every few milliseconds - the exact timing you'll have to base on the timing of your mechanisms, see below.
    • Set up a queue that can hold a bunch of entries consisting of a time stamp and a marker for block size.

    • In the main routine, look for small of large blocks. Whenever you detect a block, enter the block size and the current time into the queue and wait for the next block to arrive.

    • In the interrupt routine look for the next available entry (by the time the interrupt becomes active there may be more than one entry). If the time stamp matches the current time minus the time it takes for the block to be moved from the sensor to the gate (including some spare time to operate the gate) put the gate in the position matching the block size marked in the entry of the queue. When the time matches the time stamp plus the time it takes the block to pass the gate, reset the gate and remove the active entry from the queue.
    • With the next interrupt the next entry in the queue becomes active and the cycle starts anew.

    Alternatively you could employ a second set of sensors to detect when the block has passed the gate (or has been knocked off the belt) to advance the queue.

    Plus you could discard the timer completely and replace it by another sensor which detects the approach of a block to the gate and triggers the above sequence. This sensor doesn't have to be aware of the size of the block as the size is marked in the queue. In this case it will also not be necessary to store the time stamp in the queue as the sequence of the blocks on the conveyor is the same as the sequence of entries within the queue: Each entry corresponds to on block. This implementation is charming in that it is free from the timing of the conveyor belt. Even if the belt moves unsteady or is stopped in between the sequence of blocks and entries in the queue still will correspond.

    The timer for creating the interrupts should be set such that it is ensured that it becomes active well enough before the blocks reach the gate to set the gate in the correct position, but not too fast to avoid unnecessary interrupts.

    This way the operation of the sensors and the gate are decoupled.

    Of course other implementations are possible, the above is just one way of doing this.
     
    hevans1944 likes this.
  6. hevans1944

    hevans1944 Hop - AC8NS

    4,246
    1,996
    Jun 21, 2012
    Excellent choices for the embedded processing tasks you described. As you are probably aware by now, VxWorks is a Real Time Operating System or RTOS, and of course C or C++ is becoming the language of choice for complex embedded systems. It sounds like your application needs pre-emptive scheduling of tasks, based on hardware interrupts from the sensors.

    There is a lot of information on the Internet that will help you write and launch such tasks to process your sensor inputs, control the mechanical gate output, and count the sorted blocks according to size as measured by the first two sensors.

    I am not personally familiar with VxWorks, so I can't help you with the application programming interface, but essentially you probably want to have each sensor assigned to a task, independent of the other sensors, and allow each sensor task to generate an interrupt event when it senses a block of either size passing by. The interrupt event handler is a pre-emptive routine responsible for opening/closing the gate and incrementing either of two counters corresponding to the two different block sizes. The sensor at the end of the conveyor counts just the larger of two blocks, but it does so independently of the first two counters. This sensor could even have its own interrupt service routine since it has nothing to do with what happened earlier on the conveyor.

    What sort of processor does VxWorks execute on? What kind of input/output peripherals are you using for your sensor inputs and your mechanical gate output? Just curiosity on my part. I have no intention of purchasing VxWorks or learning how to use it. The job you described is simple enough to implement with two or three Microchip PIC processors running independently and using assembly language programming. As @Harald Kapp mentioned, there are many ways to accomplish the same thing. Clearly your employer wants you to become familiar with VxWorks and program in C. I wish you the best of luck with that, and hope you won't have to spend too much time climbing the learning curve to take advantage of RTOS.
     
Ask a Question
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.
Electronics Point Logo
Continue to site
Quote of the day

-