How to control the generation of fixed points within a fixed period of 1ms and gradually generate points (waveforms) according to the progress of the period (time)?

1 vue (au cours des 30 derniers jours)
I generated the code for the model using a C coder, and for real-time performance, I want to push it forward every 1ms. During this 1ms cycle, there are some other tasks that need to be completed in addition to the points that should be generated based on the step size. Therefore, I do not want to simply push the waveform generation based on the step size, as this will result in new points coming before I have finished reading and writing, which may miss or overwrite these points. This is something I do not want to happen.
  1 commentaire
Swastik Sarkar
Swastik Sarkar le 31 Juil 2024
Hi @peter,
Could you please share your model so that it will give a bit more clarity on what you are trying to achieve ?

Connectez-vous pour commenter.

Réponse acceptée

Umar
Umar le 31 Juil 2024

@Peter,

I did encounter a similar problem when working on one of my client projects. T address your query, I implemented a time-based interrupt-driven system that triggers the generation of waveform points at regular intervals. Hope, this will give you clue to help started with your project.Here is a simplified example to illustrate this concept:

#include stdio.h

#include unistd.h

#include signal.h

#include sys/time.h // Include sys/time.h for struct itimerval and timer functions

int waveform_points = 0;

void generateWaveformPoint() {

    // Generate waveform point based on progress
    printf("Generated waveform point: %d\n", waveform_points);
    waveform_points++;
}

void timerHandler(int signal) {

    generateWaveformPoint();
}

int main() {

    // Set up timer interrupt to trigger every 1ms
    struct itimerval timer;
    signal(SIGALRM, timerHandler);
    timer.it_value.tv_sec = 0;
    timer.it_value.tv_usec = 1000; // 1ms
    timer.it_interval = timer.it_value;
    setitimer(ITIMER_REAL, &timer, NULL);
    // Simulate other tasks within the 1ms cycle
    while (1) {
        // Perform other tasks
        usleep(500); // Simulate task taking 0.5ms
    }
    return 0;
}

Please see attached results.

So, The generateWaveformPoint() function generates a waveform point based on the progress of the time period, then the timerHandler() function is called when the timer interrupt occurs (every 1ms) and triggers the generation of a waveform point and the main() function sets up a timer interrupt to trigger every 1ms and simulates other tasks within the 1ms cycle. By using interrupts and handling waveform generation in a time-driven manner, you can ensure that new points are generated at regular intervals without missing or overwriting existing points.

  2 commentaires
peter
peter le 1 Août 2024
@UmarThank you very much for your suggestion, it has been very helpful for me to solve the problem. I will immediately start practicing it.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Simulink Coder dans Help Center et File Exchange

Produits


Version

R2020b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by