calculating thread frequency

28 Jan 2017

Hi,

How can I calculate a threads frequency?

I am using Nucleo F767ZI which is running at 216Mhz system clock.

I have 3 thread (speed calculation, ROSserial communication, velocity control) and there are 10ms wait in each of them. Here is the main function that I use.

main

int main()
{
    
    
    HAL_Init();
    
/* Initialize all configured peripherals */
    MX_GPIO_Init();
    MX_DMA_Init();
    MX_ADC2_Init();
    MX_TIM1_Init();
    MX_TIM3_Init();
    MX_TIM4_Init();
    
    
    /*Initialize necessery configuration*/
    HAL_ADC_Start_DMA(&hadc2, (uint32_t *)ADC_buffer, 2);
    HAL_TIM_Encoder_Start(&htim1, TIM_CHANNEL_1|TIM_CHANNEL_2);
    HAL_TIM_Encoder_Start(&htim3, TIM_CHANNEL_1|TIM_CHANNEL_2);
    HAL_TIM_PWM_Start(&htim4, TIM_CHANNEL_1);
    HAL_TIM_PWM_Start(&htim4, TIM_CHANNEL_2);

    nh.initNode();
    nh.advertise(RobotFeedback);
    nh.subscribe(sub);

    printf("\n\n*** RTOS basic example ***\n");
    
    Thread thread(feedback_thread, NULL, osPriorityNormal, DEFAULT_STACK_SIZE);
    Thread thread2(velocity_calculations, NULL, osPriorityNormal, DEFAULT_STACK_SIZE);
    Thread thread3(motor_drive, NULL, osPriorityNormal, DEFAULT_STACK_SIZE);
    
    
    while (true) {
        led1 = !led1;
        //nh.getParam("update_frequency", &update_frequency);
       
       
        Thread::wait(500);
    }
}

Do you have any suggestion? Is there a trick that i can use for printing threads frequency?

30 Jan 2017

Hey Oz :)

Assuming that the active part of each thread runs much faster than 10ms, your thread frequency will roughly be 10ms + duration of active part of longest thread.

For a simple test, since 1/10ms = 100 Hz, i.e. audio band, simply toggle a spare output pin every time you enter the thread. If you connect a speaker/headphone can listen to the tone - there are several apps on your smartphone that will measure the audio frequency. There are also several PC programs that measure frequency using your PC's sound card.
Of course, if you have an oscilloscope, use that!

30 Jan 2017

Hi Nicolas,

Nicolas Nackel wrote:

Of course, if you have an oscilloscope, use that!

Sometimes one might not see a simple solution. I stuck to calculating with code and missed that easy way. Thank you so much :)

30 Jan 2017

Haha, I'm happy to hear it does not only happen to me :)

Richard Feynman:

“In general we look for a new law by the following process: First we guess it. Then we compute the consequences of the guess to see what would be implied if this law that we guessed is right. Then we compare the result of the computation to nature, with experiment or experience, compare it directly with observation, to see if it works. If it disagrees with experiment it is wrong. In that simple statement is the key to science. It does not make any difference how beautiful your guess is. It does not make any difference how smart you are, who made the guess, or what his name is – if it disagrees with experiment it is wrong.”