Kernel threads, one thread at 0.1 Hz the other at 50 Hz.

Good afternoon,

I am developing with the nrf connect SDK and I want to implement the Zephyr RTOS in my project. This is cause i have a "main" thread that needs to run at 50 Hz and i have an aditional thread that only needs to run at 0.1 Hz. However I can't seem to get it to work. I am using timers to keep track of the passed time so it runs at almost perfectly the speed it needs to. But everytime the whole code only runs at 0.1 Hz cause it does them after eachother. My question is: what is the best way to make the two threads run at the speeds they need to.

Kind regards,
Niels Redegeld.

  • Hi Niels,

    Each thread is separate from eachother, and there is no reason they would have to sleep the same ammount of time. I cannot say more about wha tyou have doen wrong without seeing some code, though. Can you show what you have done?

    You can learn the basics of Zephyr threads in lesson 7 of the nRF Connect SDK fundamentals course.

    Einar

  • Hi,

    Thanks for the quick response. I have figured it out now, I have no idea what the problem was at first but I have fixed it using timers like this:

    /*****************************
     * SLAM Ortho 2024
     * Author: Niels Redegeld
     * main.c
     *****************************/
    #include "core.h"
    // #include "ble.h"
    #include "main.h"
    // #include "rotation.h"
    // #include "device.h"
    // #include "force.h"
    // #include "battery.h"
    // #include "ads131.h"
    // #include "gpio.h"
    // #include "device_state.h"
    // #include "clock.h"
    
    struct k_timer thread1_timer;
    struct k_timer thread2_timer;
    
    void thread1_handler(struct k_timer *timer_id)
    {
        // Thread 1 code here
        printk("Thread 1 running at 2 Hz\n");
    }
    
    void thread2_handler(struct k_timer *timer_id)
    {
        // Thread 2 code here
        printk("Thread 2 running at 1 Hz\n");
    }
    
    int main(void)
    {
        printk("abc\n");
    
        k_timer_init(&thread1_timer, thread1_handler, NULL);
        k_timer_init(&thread2_timer, thread2_handler, NULL);
    
        k_timer_start(&thread1_timer, K_MSEC(500), K_MSEC(500));   // 2 Hz
        k_timer_start(&thread2_timer, K_MSEC(1000), K_MSEC(1000)); // 1 Hz
    
        while (1)
        {
            // Main thread code if needed
            k_sleep(K_FOREVER);
        }
    
        return 0;
    }

    This works great :)

    Kind regards,

    Niels Redegeld.

Related