为什么我的If-Else语句正常工作?

发布于 2025-02-05 08:09:59 字数 1793 浏览 2 评论 0原文

我有一个项目,必须根据我想要的磁场发生器的旋转场更改一些参数。我不是开发人员,C ++不是我的专家计划,但是我需要找到一种使用Toggle函数在两种不同配置之间进行更改的方法。我尝试使用if-else语句,但这不起作用。手动更改参数确实可以工作,因此我相信IF-ELSE可能不会加载或其他内容。任何意见都将不胜感激。

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);  

}

编辑:我在代码开头定义了toggle_函数。

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")

{

编辑:我听说变量field_orientation和rot_axis在if-else内部和下方的变量不同。我该如何更改?同样,这是我没有很多经验的事情,但是由于工作电路,我需要修复。

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

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;
}
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

秉烛思 2025-02-12 08:09:59

变量field_orientationrot_axis您在内声明或else块完全无关与变量field_orientation 和rot_axis您在语句之前声明之前声明。因为它们共享名称,所以较小范围内的变量 shadow 外部范围中的名称,并且您只能从较小的范围访问变量。另外,由于这些是具有自动寿命的变量,因此当它们的范围结束时,它们会被破坏(在这种情况下 - }关闭如果达到 block)。

的简化示例:

#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';
}

输出为(

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

请参阅代码 原始field_orientationrot_axis的值,请勿在中声明新变量,如果范围,只需参考这些变量:

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);  
}

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);  
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文