Parameter's manipulation

Firmware/software/electronics
RyanMco
Expert
Posts: 156
Joined: Tue Apr 09, 2019 6:15 am

Parameter's manipulation

Post by RyanMco » Thu Dec 12, 2019 6:42 am

Hi !
I want to define new variable called switch but I need it globally which means to use it through all cpp files that I want to use this variable inside them, I mean for instance I want to use this variable in file ranger2.c and to use the same variable with the same value that it has in file multiranger.c (the value of variable switch can be changed within those two file and what's matter for me is to use the last updated value in variable switch .. that's why I made it globally) so what I have did , I defined into "param.h" like this:

Code: Select all

static int switch=0
and passed this header file to ranger2.c, multiranger.c .. but I still has a problem that if I change the value of that variable in ranger2.c to 5, then if I want to use that variable in file multiranger.c then its value is returned to 0 and not 5 .. I want it to be the last updated value (I guess because in param.h the variable defined as zero) although I made it globally so I must get the last updated value! .. any solution for that problem ? this variable is defined into firmware .. I'm not trying to make it as "parameter" to control it from out ..just in firmware itself I need to use it as global variable and to use the last updated value ... any help please?



arnaud
Bitcraze
Posts: 2275
Joined: Tue Feb 06, 2007 12:36 pm

Re: Parameter's manipulation

Post by arnaud » Thu Dec 12, 2019 6:53 am

You do not need the param subsystem to achieve that.

For you example, in ranger2.c you need to define the variable without static in the global scope, ie. outside any function (static actually makes it private to this file, you want the oposite):

Code: Select all

int switch;
Then in multiranger.c you can declare the same variable as extern, this tells the compiler that the variable is declared somewhere outside the current file and the linker will later connect it to the switch variable in ranger2.c. This also needs to be in global scope.

Code: Select all

extern int switch;

RyanMco
Expert
Posts: 156
Joined: Tue Apr 09, 2019 6:15 am

Re: Parameter's manipulation

Post by RyanMco » Thu Dec 12, 2019 7:44 am

So if I change the switch into file ranger2.c to switch =1
so the other file will consider the value switch as 1 .. yeah?

arnaud
Bitcraze
Posts: 2275
Joined: Tue Feb 06, 2007 12:36 pm

Re: Parameter's manipulation

Post by arnaud » Thu Dec 12, 2019 7:52 am

Yes, it will be the same variable stored in the same memory location. So setting it from one side will be seen from the other side.

RyanMco
Expert
Posts: 156
Joined: Tue Apr 09, 2019 6:15 am

Re: Parameter's manipulation

Post by RyanMco » Thu Dec 12, 2019 7:55 am

appreciated!

RyanMco
Expert
Posts: 156
Joined: Tue Apr 09, 2019 6:15 am

Re: Parameter's manipulation

Post by RyanMco » Thu Dec 12, 2019 8:11 am

Any do you know please how I define variable as const at just first update?
I mean I want to do something like this analogy:
function() : (I may enter this function many times .. )
if (it's first time then enter this code and do it just once although may I enter this function
many times)
//do this code block just one time as your first time although I may enter function() many
//times

I don't want to define local variable in function() because it will make problems like I faced before , any suggestion how to do that concept without using local variable within function()?

thanks

arnaud
Bitcraze
Posts: 2275
Joined: Tue Feb 06, 2007 12:36 pm

Re: Parameter's manipulation

Post by arnaud » Thu Dec 12, 2019 9:19 am

Local variable will not help in this case anyway. This can be done with a global or static variable, for example:

Code: Select all

void function()
{
  static bool firstRun = true;

  if (firstRun) {
    firstRun = false;
    // This is executed only the first time the function is call
  } else {
    // This is executed only the second and more time the function is called
  }
  
  // This is executed all the time the function is called
}
We are using this scheme everywhere in the firmware with the "isInit" static variables: is the init function is called when isInit is already true, the init function usually returns directly.

RyanMco
Expert
Posts: 156
Joined: Tue Apr 09, 2019 6:15 am

Re: Parameter's manipulation

Post by RyanMco » Thu Dec 12, 2019 9:23 am

So what I understand from you, whenever I use static/global variables there wouldn't be hard-fault as what I get(in my second thread) when I use local variables?

arnaud
Bitcraze
Posts: 2275
Joined: Tue Feb 06, 2007 12:36 pm

Re: Parameter's manipulation

Post by arnaud » Thu Dec 12, 2019 9:52 am

In C a static local variable is actually a global variable that can only be accessed from within the function.

The hard-fault you are experiencing is likely due to stack overflow. The solution, beside avoiding local variable, is to increase the stack size.

By the way, you can see the stack status of each tasks in the the console tab in the client by clicking the "task dump" button.

RyanMco
Expert
Posts: 156
Joined: Tue Apr 09, 2019 6:15 am

Re: Parameter's manipulation

Post by RyanMco » Thu Dec 12, 2019 1:42 pm

but how to know which stack am I using?
I guess the problem with stack that's managing the packets that I get from ROS, expositionhandler ..something like that(the function that gets the packet from ROS),
maybe you know what's that stack called?

Post Reply