8 years ago.

Unable to use mutiple tickers for my application

Hello,

I am working on Nucelo L053r8t6 custom board for one of my application. where i need to implement 5 tickers may be some times at equal intervals of time the event must work for all the tickers. But when using mutiple number of tickers happen to see only one ticker is being executed and rest of them are being ignored. I here by attch few sample snippets that i am working on. Could you please tell me the solution for the problem i am facing .

Interval at which each function to be executed

void prot_Communicate :: Sampling_Rate_Temprature(uint8_t Sampling_val)
{
	 switch(Sampling_val) 
	 {
		 case SAMPLING_1SEC :
		 {
			 Temp_Sense_Update.attach(this,&prot_Communicate :: Temperature_Sensor,1);
		 }
		 break;
		 case SAMPLING_2SEC :
		 {
			 Temp_Sense_Update.attach(this,&prot_Communicate :: Temperature_Sensor,2);
		 }
		 break;
		 case SAMPLING_5SEC :
		 {
			 Temp_Sense_Update.attach(this,&prot_Communicate :: Temperature_Sensor,5);
		 }

Decleration of tickers and the indication flags

Ticker Temp_Sense_Update;
Ticker Humid_Sense_Update;
Ticker Baro_Sense_Update;
Ticker Lux_Sense_Update; 

bool flag_temp_sensor,flag_humid_sensor,flag_baro_sensor,flag_lux_sensor,flag_accelro_sensor;

Attching the ticker with function

 void prot_Communicate :: Temperature_Sensor(void)
{
		flag_temp_sensor = 1;
}

void prot_Communicate :: Humid_Sensor(void)
{
		flag_humid_sensor = 1;
}
void prot_Communicate :: Baro_Sensor(void)
{
		flag_baro_sensor = 1;
}

void prot_Communicate :: Lux_Sensor(void)
{
		flag_lux_sensor = 1;
}

Start the device with the given sampling rate

void prot_Communicate :: Start_Stop_logger(void)
{ 
    if (f_start)
    {
				if(flag_temp_sensor)
				{
						 flag_temp_sensor = 0;
						 Temp = sht.readTemp();
						 fprintf(logFile,"\r\n%3.2f °C",Temp);
					//	 FloatSep(Temp,&package.Data[i],&package.Data[i+2]);
					//	 i += 4;
				}
				if(flag_humid_sensor)
				{
						flag_humid_sensor = 0;
						Humd = sht.readHumidity();
					  fprintf(logFile,"\r\n%3.2f RH", Humd);
						// FloatSep(Humd,&package.Data[i],&package.Data[i+2]);	
						// i += 4;
				}	
				if(flag_baro_sensor)
				{
						flag_baro_sensor = 0; 
				//		Pressure = ms.calcPressure();
					  //fprintf(logFile,"\r\n%.1f mB\n", Pressure);
						// FloatSep(Pressure,&package.Data[i],&package.Data[i+2]);
						// i += 4;
				}	
				if(flag_lux_sensor)
				{ 
						 flag_lux_sensor = 0;
						 Lux = max44009.getLUXReading();
					   fprintf(logFile,"\r\n%f lux",Lux);
						// FloatSep(Lux,&package.Data[i],&package.Data[i+2]);
						// i += 4;
				}

here comes the problem that only the temprature flag is being raised but but any other flags. I have tested using keil compiler by inserting break points at required sections but found that only temparture flag is being raised but no other flags are being raised. could you please help me in this issue. I thought size would be problem in order to create object for the tickers but normally i removed everything and brought it to 42 kb code size. even though i have the same issue. I feel mostly its happening in the attach function. May be i am unclear about the syntax. could any one provide me suggestions. Thank you.

/media/uploads/sandeepmalladi/dl_ticker_test_uvision5_nucleo_l053r8.rar

kk

posted by san m 15 Oct 2016

1 Answer

8 years ago.

When you have variables that change in an interrupt, such as a ticker, always define them as volatile. So in this case as volatile bool.

This tells the compiler the variable may change without the compiler knowing this. So it will always get the latest version from its memory. Otherwise it optimizes part of the code away, since it doesn't understand interrupts, and assumes those variables won't change anyway. And this gives unpredictable behavior.

Hello erik,

As per the attchment that i had sent has the same concept as variables declared as votaile bool. But even after the declaration as volatile bool i got to see no improvement in the code. Jua go though the attachment and please let me know the actual problem. Thank you.

posted by san m 13 Oct 2016

Your code is huge. And it is a bit confusing that your code does not correspond apparently to what you posted here.

My advice: Make a simple test case and see if it works. If yes, then the problem is probably not the tickers but something in your code. I was planning to see if I could figure it out for you, but setting the ticker was called by a function, which in turn was called by another function, which again was a generic switch statement (tbh there seem to be more switch statements than really needed).

Then add that the single file we are talking about already has more than 2000 lines of code. So really: Simplify your problem and we can help if you still got it. But I am not going to debug 2000 lines of code, without even having the setup to run it.

posted by Erik - 13 Oct 2016

Or to put it another way: Rather than wasting peoples time do what I suggested last time you asked this question rather than reposting the exact same question a second time and getting the same answer from a second person.

posted by Andy A 14 Oct 2016

Hello Erik,

I had made it simple way for you to understand how i am calling a function.

include the mbed library with this snippet

#include "mbed.h"

Ticker 1
Ticker 2
Ticker 3
Ticker 4

void Function()
{
	if(flag1)
	{
		do some work for flag 1();
	}
	that contnious for flag 2,3 and 4 as i am using 4 tickers
}

//passing time duration for the tickers

void function time (volatile uint8_t sampling rate)
{
	function.attach(this,&attached function,sampling rate); // function dynamically changes the value of sampling rate attched to ticker time
}


void attached function()
{
	flag 1 = 1;
}

The problem i am facing is that I am able to get an interrupt for only 2 tickers.but there is no interrupt generated from other 2.

But i am able generate interrupt for all the 4 tickers if i do in this method by calling initialize function before main.

Tickers works if i initialize it first before main

#include "mbed.h"
void init()
{
	function.attach(this,&attached function,1); // Ticker1
	function.attach(this,&attached function,2); // Ticker2
}
void main()
{
	
}

if i do this way i am able to get interrupt generated. I have no idea what would be the reason for this kind of strange behavior it makes no sense for me if i would like to change time value dynamically from the user input. As per Andy suggestion i had removed unnecessary code and made to 39Kb size but there was no improvement. can any one know how to pass the time value dynamically from the user input. Sorry andy but unfortunately i had clicked the option assign so i felt other people suggestions are also more important for me thats the only reason why i have to repost.

posted by san m 15 Oct 2016

The init function is still called from main this way, so executed within main. But can you just make a simple example where you for example flash some LEDs, or print some messages to your PC, which demonstrates the problem.

posted by Erik - 15 Oct 2016

Hello Thanks for your suggestion and i had achieved by using inline function. So that code has become more faster than before and was able to access the timers too. Thanky Erik and Andy.

posted by san m 25 Oct 2016