当前位置: 首页>>代码示例>>C++>>正文


C++ Motor::setPosition方法代码示例

本文整理汇总了C++中Motor::setPosition方法的典型用法代码示例。如果您正苦于以下问题:C++ Motor::setPosition方法的具体用法?C++ Motor::setPosition怎么用?C++ Motor::setPosition使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Motor的用法示例。


在下文中一共展示了Motor::setPosition方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: playStep

void Motion::playStep() {
  // actuate
  for (unsigned int i = 0; i < mMotors.size(); i++) {
    Motor *motor = mMotors[i];
    
    double beforeValue = 0.0;
    int beforeTime = -1;
    double afterValue = 0.0;
    int afterTime = -1;
    
    // find beforeValue & beforeTime
    vector<Pose *>::iterator poseIt;
    for (poseIt = mPoses.begin() ; poseIt != mPoses.end(); ++poseIt) {
      Pose *pose = *poseIt;
      if (pose->time <= mElapsed) {
        MotorCommand *command = pose->commands[i];
        if (command->defined) {
          beforeTime = pose->time;
          beforeValue = command->value;
        }
      } else
        break;
    }
    
    // find afterValue & afterTime
    vector<Pose *>::reverse_iterator poseRIt;
    for (poseRIt = mPoses.rbegin() ; poseRIt != mPoses.rend(); ++poseRIt) {
      Pose *pose = *poseRIt;
      if (pose->time >= mElapsed) {
        MotorCommand *command = pose->commands[i];
        if (command->defined) {
          afterTime = pose->time;
          afterValue = command->value;
        }
      } else
        break;
    }
    
    // compute position
    bool setPos = false;
    double pos = 0.0;
    if (afterTime != -1 && mElapsed > mDuration) {
      setPos = true;
      pos = afterValue;
    } else if (beforeTime != -1 && afterTime != -1) {
      setPos = true;
      if (afterTime == beforeTime)
        pos = beforeValue;
      else
        pos = beforeValue + (mElapsed - beforeTime) * (afterValue - beforeValue) / (afterTime - beforeTime);
    }
    else if (beforeTime != -1) {
      setPos = true;
      pos = beforeValue;
    }
    
    // apply position
    if (setPos)
      motor->setPosition(pos);
  }
  
  // update internal variables
  Robot *robot = Robot::getInstance();
  int time = robot->getTime() * 1000.0;
  int delta = time - mPreviousTime;
  mPreviousTime = time;
  
  if (mReverse) {
    if (mElapsed <= 0) {
      if (mLoop)
        mElapsed = mDuration;
      else {
        mElapsed = 0;
        mPlaying = false;
      }
    }
    else
      mElapsed -= delta;
  }
  else {
    if (mElapsed >= mDuration) {
      if (mLoop)
        mElapsed = 0;
      else {
        mElapsed = mDuration;
        mPlaying = false;
      }
    }
    else
      mElapsed += delta;
  }
}
开发者ID:DavidMansolino,项目名称:webots-cross-compilation,代码行数:92,代码来源:Motion.cpp


注:本文中的Motor::setPosition方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。