机器人旋转角度和转向方向

huangapple go评论90阅读模式
英文:

Robot Rotation Angle and Turn Direction

问题

Working on Robot project and need to figure New Heading rotation amount and turn direction.
Input parameters are Current Heading and New Heading, Output values are Rotation amount in degrees and turn direct Port (Left) or Starboard (Right). Have solution below but it does not look very elegant. Any thoughts for more elegant solution?
Many thanks in advance imk

void AngleDiff( int RobotHeading , int NewHeading)
{
int SteeringAngle = RobotHeading - NewHeading;
char TurnDirection; // P = Port (Left) or S = Starboard (Right) 

if(  SteeringAngle > 180 )
 {
 SteeringAngle -= 360;
 SteeringAngle = abs( SteeringAngle );
 TurnDirection = 'S';
 return;
 }

if( SteeringAngle < -180 )
 {
 SteeringAngle += 360;
 TurnDirection = 'P';
 return;
 }

if( SteeringAngle < 0 )
 {
 SteeringAngle = abs( SteeringAngle );
 TurnDirection = 'S';
 }
else
 TurnDirection = 'P';
}
英文:

Working on Robot project and need to figure New Heading rotation amount and turn direction.
Input parameters are Current Heading and New Heading, Output values are Rotation amount in degrees and turn direct Port (Left) or Starboard (Right). Have solution below but it does not look very elegant. Any thoughts for more elegant solution?
Many thanks in advance imk

void AngleDiff( int RobotHeading , int NewHeading)
{
int SteeringAngle = RobotHeading - NewHeading;
char TurnDirection; // P = Port (Left) or S = Starboard (Right) 

if(  SteeringAngle &gt; 180 )
 {
 SteeringAngle -= 360;
 SteeringAngle = abs( SteeringAngle );
 TurnDirection = &#39;S&#39;;
 return;
 }

if( SteeringAngle &lt; -180 )
 {
 SteeringAngle += 360;
 TurnDirection = &#39;P&#39;;
 return;
 }

if( SteeringAngle &lt; 0 )
 {
 SteeringAngle = abs( SteeringAngle );
 TurnDirection = &#39;S&#39;;
 }
else
 TurnDirection = &#39;P&#39;;
}

答案1

得分: 1

也许是这样的:

if (SteeringAngle > 180)
{
    SteeringAngle -= 360;
}
else if (SteeringAngle < -180)
{
    SteeringAngle += 360;
}
TurnDirection = SteeringAngle < 0 ? 'S' : 'P';
SteeringAngle = abs(SteeringAngle);
英文:

Perhaps something like this:

if (SteeringAngle &gt; 180)
{
    SteeringAngle -= 360;
}
else if (SteeringAngle &lt; -180)
{
    SteeringAngle += 360;
}
TurnDirection = SteeringAngle &lt; 0 ? &#39;S&#39; : &#39;P&#39;;
SteeringAngle = abs(SteeringAngle);

huangapple
  • 本文由 发表于 2023年4月17日 19:02:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/76034449.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定