首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >代码不在循环内部工作,而是在循环之外工作。

代码不在循环内部工作,而是在循环之外工作。
EN

Stack Overflow用户
提问于 2021-03-08 17:56:59
回答 1查看 69关注 0票数 0

我有一段代码显示了一些奇怪的行为。当将代码排除在for循环之外时,代码的工作原理是一样的,但是一旦我将它放置在for循环中,它就会一次又一次地重复相同的值。

代码语言:javascript
复制
  const double *north = cp->getValues();
  angle1 = atan2(north[1], north[2]);
  bearing = (angle1 - 1.5708) / M_PI * 180.0;
  std::cout<<"Bearing inside : "<<bearing<<std::endl;
  if (bearing < 0.0)
    bearing = bearing + 360.0;
  t = abs(bearing - turn);

这部分代码应该在对象每次旋转时更新*north。每当我将这些代码保存在循环之外时,就会发生这种情况。但是,如果代码保存在如下所示的for循环中,那么*north的值将保持不变。

代码语言:javascript
复制
    for(int i=0;i<100;i++)
    {
      double t_modifier = (180.0 - abs(t)) / 180.0;
      double threshold = 2.0;
      double Speedleft = 1.0;
      double Speedright = 1.0;
      int identifier;
      if(int(t)<0){
        Speedleft = -1;
      }
      else if(int(t)>0){
        Speedright = -1;
      }
      leftSpeed = threshold * Speedleft;
      rightSpeed = threshold * Speedright;
      {
        wheels[0]->setVelocity(leftSpeed);
        wheels[1]->setVelocity(rightSpeed);
        wheels[2]->setVelocity(leftSpeed);
        wheels[3]->setVelocity(rightSpeed);
      }
      
      const double *north = cp->getValues();
      angle1 = atan2(north[1], north[2]);
      bearing = (angle1 - 1.5708) / M_PI * 180.0;
      std::cout<<"Bearing inside : "<<bearing<<std::endl;
      if (bearing < 0.0)
        bearing = bearing + 360.0;
      t = abs(bearing - turn);
    }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-03-11 08:10:24

页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66534802

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档