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:
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?
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):
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.
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()?
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.
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?
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?