Clear Filters
Clear Filters

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)?

45 views (last 30 days)
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.

Accepted Answer

Umar
Umar on 31 Jul 2024 at 7:39

@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.

More Answers (0)

Categories

Find more on Deployment, Integration, and Supported Hardware in Help Center and File Exchange

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!