Search code examples
c++visual-studioros

Why isn´t my If-Else statement working properly?


I have a project where I have to change some parametres depending on the field of rotation I want for a magnetic field generator. I am not a developer and c++ is not my program of expertice, but I need to find a way to change between two different configurations using a toggle function. I tried using an If-Else statement, but it doesn´t work. Changing the parameters manually does work, so I believe the if-else may not be loading or something. Any input would be greatly appreciated.

void ParticleControlPanel::processTimer()

{    
//original field orientation (B-field aligned with z-axis)
tf::Vector3 field_orientation(0.,0.,1.);
//original rotiation axis; (rotation axis aligned with x-axis)
tf::Vector3 rot_axis(1.,0.,0.);
rot_axis.setX(cos(azimuth_/180.*pi));
rot_axis.setY(sin(azimuth_/180.*pi));
rot_axis.setZ(0); 

//toggle drill and cube
if (toggle_)
{
    tf::Vector3 field_orientation(1.,0.,0.);
    tf::Vector3 rot_axis(0.,0.,1.);
    rot_axis.setX(0);
    rot_axis.setY(sin(azimuth_/180.*pi));
    rot_axis.setZ(cos(azimuth_/180.*pi));         

}

 else
{
    tf::Vector3 field_orientation(0.,0.,1.);
    tf::Vector3 rot_axis(1.,0.,0.);
    rot_axis.setX(cos(azimuth_/180.*pi));
    rot_axis.setY(sin(azimuth_/180.*pi));
    rot_axis.setZ(0);  

}

edit: I define the toggle_ function at the beginning of the code.

namespace mag_control {

ParticleControlPanel::ParticleControlPanel(QWidget *parent) :
rviz::Panel(parent),
magfield_topic_("/desired_field"),
magfield_grad_topic_("/desired_field_grad"),
holding_lock_(false),
activated_(false),
controlling_(false),
wobble_(false),
toggle_(false),
frequency_(0.00),
azimuth_(0.0),
rot_angle_(0.0),
t_prev_(0.0),
pi(std::acos(-1)),
gradient_(0.,0.,0.),
position_(0.,0.,0.),
z_control_(false),
gradient_z_(0.0),
thresh_bin_(0),
thresh_hough_(0),
config_("demo.yaml")

{

Edit: I hear that the variables field_orientation and rot_axis are not the same inside of the if-else and below it. How can I change this? Again, this is something I have not a lot experience with, but I need to fix because of work circunstances.

void ParticleControlPanel::processCheckboxToggle(int value)
{
    if(value){
        toggle_ = true;
}
    else {
        toggle_ = false;
}
}

Solution

  • Variables field_orientation and rot_axis you declare inside if or else blocks are completely unrelated to variables field_orientation and rot_axis you declared before if statement. Because they share names, variables inside smaller scopes shadow the names in the outer scope, and you can only access variables from smaller scope. Also, since these are variables with automatic lifetime, their are destroyed when their scope ends (in this case - when } closing if block is reached).

    See a simplified example of your code:

    #include <iostream>
    
    int main(void)
    {
        int myVar = 5;
        std::cout << "myVar before if: " << myVar << '\n';
        if (true) 
        {
            int myVar = 13; // a new myVar, completely different than the first myVar
            std::cout << "myVar inside if: " << myVar << '\n';
        }  // myVar from inside if is gone here
        std::cout << "myVar after if: " << myVar << '\n';
    }
    

    The output is (see it online)

    myVar before if: 5
    myVar inside if: 13
    myVar after if: 5
    

    If you want to change the values of original field_orientation and rot_axis, do not declare new variables in if scope, simply refer to those variables:

    void ParticleControlPanel::processTimer()
    
    {    
    //original field orientation (B-field aligned with z-axis)
    tf::Vector3 field_orientation(0.,0.,1.);
    //original rotiation axis; (rotation axis aligned with x-axis)
    tf::Vector3 rot_axis(1.,0.,0.);
    rot_axis.setX(cos(azimuth_/180.*pi));
    rot_axis.setY(sin(azimuth_/180.*pi));
    rot_axis.setZ(0); 
    
    //toggle drill and cube
    if (toggle_)
    {
        field_orientation.setX(1.); // I guess, I don't know the library you are using
        field_orientation.setY(0.);
        field_orientation.setZ(0.);
    
        rot_axis.setX(0);
        rot_axis.setY(sin(azimuth_/180.*pi));
        rot_axis.setZ(cos(azimuth_/180.*pi));         
    }
    else
    {
        field_orientation.setX(0.);
        field_orientation.setY(0.);
        field_orientation.setZ(1.);
    
        rot_axis.setX(cos(azimuth_/180.*pi));
        rot_axis.setY(sin(azimuth_/180.*pi));
        rot_axis.setZ(0);  
    }