从同一来源分配不同的值。奇怪

时间:2014-02-24 22:24:29

标签: c++ ros

我正在为ROS编写c ++编码。我写了这段代码。

float32[] array1
float32[] array2
int32[] obs_check

std_msgs

这就是StoM.msg文件的样子。请注意,这种语法是ROS语法,我们必须指定为float64或float 32.我们也可以在c +++代码中执行此操作吗?比如std :: vector array_x;

这是cpp文件。

std::vector<float> array_x;
std::vector<float> array_y;
std::vector<int> obs_check1;
std::vector<float> nobs_x;
std::vector<float> nobs_y;
std::vector<float> d_ep_goal;

void SensortoMPNCallback(assign2::StoM obj)
{   
    if (obj.array1.size() != 0)
    {
        array_x.clear();
        array_y.clear();
        nobs_x.clear();
        nobs_y.clear();

        flag = 1;
        for (int i = 0; i < obj.array1.size(); i++)
        {
            obs_check1.push_back(obj.obs_check[i]);
            if (obj.obs_check[i] == 0)
            {
                ROS_INFO("Start of if");
                nobs_x.push_back(obj.array1[i]);
                nobs_y.push_back(obj.array2[i]);
                array_x.push_back(obj.array1[i]);
                array_y.push_back(obj.array2[i]);
            }
            else if (obj.obs_check[i] == 1)
            {   ROS_INFO("Start of else if");
                array_x.push_back(obj.array1[i]);
                array_y.push_back(obj.array2[i]);
            }
            ROS_INFO("Got x,y [%f],[%f]", obj.array1[i],obj.array2[i]);
            ROS_INFO("My x,y [%f],[%f]", array_x[i],array_y[i]);
            ROS_INFO("No obs_x,No obs_y [%f],[%f]", nobs_x[i],nobs_y[i]);
            ROS_INFO("status got [%d]", obj.obs_check[i]);
            ROS_INFO("status my [%d]", obs_check1[i]);

        }
    }
}

输出如下。你可以清楚地看到nobs_x和nobs_y表现得很奇怪。我不明白为什么。在理论上,nobs_x应该完全像array_行为,除了当obj.obs_check == 1.请解释为什么会发生这种情况。由于这次失败,我失去了什么UI,我的程序很乱!!

[ INFO] [1393279223.062336300, 1056.296000000]: Got x,y [4.942642],[2.718085]
[ INFO] [1393279223.062651928, 1056.297000000]: My x,y [4.942642],[2.718085]
[ INFO] [1393279223.062931563, 1056.297000000]: No obs_x,No obs_y [5.076604],[2.735721]
[ INFO] [1393279223.063120705, 1056.297000000]: status got [0]
[ INFO] [1393279223.063418319, 1056.298000000]: status my [0]
[ INFO] [1393279223.063489536, 1056.298000000]: Start of if
[ INFO] [1393279223.063548878, 1056.298000000]: Got x,y [4.950000],[2.713398]
[ INFO] [1393279223.063604675, 1056.298000000]: My x,y [4.950000],[2.713398]
[ INFO] [1393279223.063659180, 1056.298000000]: No obs_x,No obs_y [5.081915],[2.742642]
[ INFO] [1393279223.063712605, 1056.298000000]: status got [0]
[ INFO] [1393279223.063766041, 1056.298000000]: status my [0]
[ INFO] [1393279223.063817998, 1056.298000000]: Start of if
[ INFO] [1393279223.063871795, 1056.298000000]: Got x,y [4.957738],[2.709369]
[ INFO] [1393279223.063926402, 1056.298000000]: My x,y [4.957738],[2.709369]
[ INFO] [1393279223.063982658, 1056.298000000]: No obs_x,No obs_y [5.086603],[2.750000]
[ INFO] [1393279223.064116523, 1056.299000000]: status got [0]
[ INFO] [1393279223.064166647, 1056.299000000]: status my [0]
[ INFO] [1393279223.064217780, 1056.299000000]: Start of if
[ INFO] [1393279223.064270886, 1056.299000000]: Got x,y [4.965798],[2.706031]
[ INFO] [1393279223.064328299, 1056.299000000]: My x,y [4.965798],[2.706031]
[ INFO] [1393279223.064381303, 1056.299000000]: No obs_x,No obs_y [5.090631],[2.757738]
[ INFO] [1393279223.064439096, 1056.299000000]: status got [0]
[ INFO] [1393279223.064493752, 1056.299000000]: status my [0]
[ INFO] [1393279223.064541418, 1056.299000000]: Start of if
[ INFO] [1393279223.064592527, 1056.299000000]: Got x,y [4.974118],[2.703407]
[ INFO] [1393279223.064711455, 1056.299000000]: My x,y [4.974118],[2.703407]
[ INFO] [1393279223.064843991, 1056.299000000]: No obs_x,No obs_y [5.093969],[2.765798]
[ INFO] [1393279223.064972151, 1056.299000000]: status got [0]
[ INFO] [1393279223.066053798, 1056.301000000]: status my [0]
[ INFO] [1393279223.066209830, 1056.301000000]: Start of if
[ INFO] [1393279223.066351915, 1056.301000000]: Got x,y [4.982635],[2.701519]
[ INFO] [1393279223.066400360, 1056.301000000]: My x,y [4.982635],[2.701519]
[ INFO] [1393279223.066490090, 1056.301000000]: No obs_x,No obs_y [5.096592],[2.774118]
[ INFO] [1393279223.066577548, 1056.301000000]: status got [0]
[ INFO] [1393279223.066668389, 1056.301000000]: status my [0]
[ INFO] [1393279223.066755107, 1056.301000000]: Start of if
[ INFO] [1393279223.066841109, 1056.301000000]: Got x,y [4.991284],[2.700381]
[ INFO] [1393279223.066929419, 1056.301000000]: My x,y [4.991284],[2.700381]
[ INFO] [1393279223.067017180, 1056.301000000]: No obs_x,No obs_y [5.098481],[2.782635]
[ INFO] [1393279223.067101128, 1056.301000000]: status got [0]
[ INFO] [1393279223.067184692, 1056.301000000]: status my [0]
[ INFO] [1393279223.067275368, 1056.301000000]: Start of if
[ INFO] [1393279223.067367026, 1056.301000000]: Got x,y [5.000000],[2.700000]
[ INFO] [1393279223.067458201, 1056.301000000]: My x,y [5.000000],[2.700000]
[ INFO] [1393279223.067545444, 1056.301000000]: No obs_x,No obs_y [5.099619],[2.791284]
[ INFO] [1393279223.067629429, 1056.301000000]: status got [0]
[ INFO] [1393279223.067717692, 1056.301000000]: status my [0]
[ INFO] [1393279223.067806051, 1056.301000000]: Start of if
[ INFO] [1393279223.067922643, 1056.302000000]: Got x,y [5.008716],[2.700381]
[ INFO] [1393279223.068055929, 1056.302000000]: My x,y [5.008716],[2.700381]
[ INFO] [1393279223.068204288, 1056.302000000]: No obs_x,No obs_y [0.000000],[0.000000]
[ INFO] [1393279223.068406429, 1056.303000000]: status got [0]
[ INFO] [1393279223.068553781, 1056.303000000]: status my [0]
[ INFO] [1393279223.068703491, 1056.303000000]: Start of if
[ INFO] [1393279223.068853236, 1056.303000000]: Got x,y [5.017365],[2.701519]
[ INFO] [1393279223.069120890, 1056.304000000]: My x,y [5.017365],[2.701519]
[ INFO] [1393279223.069270681, 1056.304000000]: No obs_x,No obs_y [0.000000],[0.000000]
[ INFO] [1393279223.069413183, 1056.304000000]: status got [0]
[ INFO] [1393279223.069559232, 1056.304000000]: status my [0]
[ INFO] [1393279223.069649302, 1056.304000000]: Start of if
[ INFO] [1393279223.069742869, 1056.304000000]: Got x,y [5.025882],[2.703407]
[ INFO] [1393279223.069837317, 1056.304000000]: My x,y [5.025882],[2.703407]
[ INFO] [1393279223.069981090, 1056.304000000]: No obs_x,No obs_y [0.000166],[0.000000]
[ INFO] [1393279223.070146919, 1056.304000000]: status got [0]
[ INFO] [1393279223.070335272, 1056.304000000]: status my [0]
[ INFO] [1393279223.070406590, 1056.304000000]: Start of if
[ INFO] [1393279223.070547986, 1056.304000000]: Got x,y [5.034202],[2.706031]
[ INFO] [1393279223.070689189, 1056.305000000]: My x,y [5.034202],[2.706031]
[ INFO] [1393279223.070886457, 1056.305000000]: No obs_x,No obs_y [0.000000],[0.000000]
[ INFO] [1393279223.070947098, 1056.305000000]: status got [0]
[ INFO] [1393279223.071000829, 1056.305000000]: status my [0]
[ INFO] [1393279223.071088936, 1056.305000000]: Start of if
[ INFO] [1393279223.071312900, 1056.305000000]: Got x,y [5.042262],[2.709369]
[ INFO] [1393279223.071371639, 1056.305000000]: My x,y [5.042262],[2.709369]
[ INFO] [1393279223.071520212, 1056.306000000]: No obs_x,No obs_y [0.000000],[49652160681978098666684022784.000000]
[ INFO] [1393279223.071704500, 1056.306000000]: status got [0]
[ INFO] [1393279223.071765282, 1056.306000000]: status my [0]
[ INFO] [1393279223.071854062, 1056.306000000]: Start of if
[ INFO] [1393279223.072054525, 1056.306000000]: Got x,y [5.050000],[2.713398]
[ INFO] [1393279223.072117290, 1056.306000000]: My x,y [5.050000],[2.713398]
[ INFO] [1393279223.072211950, 1056.306000000]: No obs_x,No obs_y [0.000000],[18128475639337701580720918478454784.000000]
[ INFO] [1393279223.072405258, 1056.306000000]: status got [0]
[ INFO] [1393279223.072512798, 1056.307000000]: status my [0]
[ INFO] [1393279223.072601386, 1056.307000000]: Start of if
[ INFO] [1393279223.073605977, 1056.308000000]: Got x,y [5.057358],[2.718085]
[ INFO] [1393279223.073798499, 1056.308000000]: My x,y [5.057358],[2.718085]
[ INFO] [1393279223.073861531, 1056.308000000]: No obs_x,No obs_y [0.000000],[0.000000]
[ INFO] [1393279223.073948135, 1056.308000000]: status got [0]
[ INFO] [1393279223.074145078, 1056.308000000]: status my [0]
[ INFO] [1393279223.074274259, 1056.308000000]: Start of if
[ INFO] [1393279223.074369951, 1056.308000000]: Got x,y [5.064279],[2.723396]
[ INFO] [1393279223.074644050, 1056.308000000]: My x,y [5.064279],[2.723396]
[ INFO] [1393279223.074750515, 1056.308000000]: No obs_x,No obs_y [0.000000],[17537701944724815872.000000]
[ INFO] [1393279223.074845207, 1056.308000000]: status got [0]
[ INFO] [1393279223.074933693, 1056.308000000]: status my [0]
[ INFO] [1393279223.075017897, 1056.308000000]: Start of if
[ INFO] [1393279223.075109592, 1056.308000000]: Got x,y [5.070711],[2.729289]
[ INFO] [1393279223.075317620, 1056.308000000]: My x,y [5.070711],[2.729289]
[ INFO] [1393279223.075409573, 1056.308000000]: No obs_x,No obs_y [0.000000],[0.000000]
[ INFO] [1393279223.075494045, 1056.308000000]: status got [0]
[ INFO] [1393279223.075694606, 1056.308000000]: status my [0]
[ INFO] [1393279223.075823677, 1056.310000000]: Start of if
[ INFO] [1393279223.075872092, 1056.310000000]: Got x,y [5.076604],[2.735721]
[ INFO] [1393279223.075960848, 1056.310000000]: My x,y [5.076604],[2.735721]
[ INFO] [1393279223.076172643, 1056.310000000]: No obs_x,No obs_y [0.000000],[0.000000]
[ INFO] [1393279223.076260578, 1056.310000000]: status got [0]
[ INFO] [1393279223.076345102, 1056.311000000]: status my [0]
[ INFO] [1393279223.077163899, 1056.311000000]: Start of if
[ INFO] [1393279223.077264851, 1056.311000000]: Got x,y [5.081915],[2.742642]
[ INFO] [1393279223.077476565, 1056.312000000]: My x,y [5.081915],[2.742642]
[ INFO] [1393279223.077595873, 1056.312000000]: No obs_x,No obs_y [0.000000],[793481257882222592.000000]
[ INFO] [1393279223.077658217, 1056.312000000]: status got [0]
[ INFO] [1393279223.077740911, 1056.312000000]: status my [0]
[ INFO] [1393279223.077892263, 1056.312000000]: Start of if
[ INFO] [1393279223.077983340, 1056.312000000]: Got x,y [5.086603],[2.750000]
[ INFO] [1393279223.078161173, 1056.312000000]: My x,y [5.086603],[2.750000]
[ INFO] [1393279223.078224825, 1056.312000000]: No obs_x,No obs_y [0.000000],[0.000000]
[ INFO] [1393279223.078309932, 1056.312000000]: status got [0]
[ INFO] [1393279223.078410652, 1056.312000000]: status my [0]

1 个答案:

答案 0 :(得分:2)

您要在if语句中向nobs_xnobs_y添加项目,因此当您访问nobs_x[i] i时,可能会大于{{1} }。