2016-09-23 22 views
0

是否可以在函数外的嵌入式C中调用行号?转到函数外的嵌入式C中的行号

简单的goto标签命令不能工作,因为我在if语句循环:我要重新启动的功能,一旦误差大于200

void followWall(void) 
{ 

begin: 

    int desired = getADC();              //store the first ADC reading as the desired distance from the wall 
    int right = 200;                //pre set all variables to be called in the drive function 
    int left = 200;                //ints are needed here for bit masking later on to stop overflow 
    char rightH = 0; 
    char leftH = 0; 
    float k = 0.5;                //constant to times the error to fine tune the response 
    drive(rightH,right,leftH,left);            //drive the robot to the pre set variables 

    while(bumpFlag == 0)              //while the robot has not been bumped or encounted a cliff 
    { 
     int actual = getADC();             //call a new ADC reading everytime at start of while as the current reading 
     rightH = 0; 
     leftH = 0;                //set variables back to default to stop constant increase and decrease in variables 
     left = 200; 
     right = 200; 
     char error = abs(actual-desired);          //calculate the error by minusing the actual distance to the desired 

     if(motorAngle < 180)              //wall on left between 1st and 2nd quadrant 
     { 
      if(error > 200) 
      { 
       stop(); 
       moveStepper(33,0); 
       goto begin; 
      } 
      if (actual > desired)             
      { 
       right -=((error)*k);            
       left += ((error)*k);            
      } 
      else if (actual < desired)           
      { 
       left -=((error)*k);            
       right +=((error)*k);            
      } 
     } 
     else if (motorAngle > 180)            
     { 
      if(error > 200) 
      { 
       stop(); 
       moveStepper(33,1); 
       goto begin; 
      } 
      if (actual > desired)             
      { 
       left -=((error)*k);            
       right +=((error)*k);            
      } 
      else if (actual < desired)           
      { 
       right -=((error)*k);            
       left +=((error)*k);            
      } 

     } 
    drive(rightH,right,leftH,left);              bumpSensor();                
    setLCDCursor(0x09); 
    writeLCDNumber(convert(actual));           //constantly write the converted AC value on the LCD 
    } 
    stop();                  //stop the robot 
} 
+2

不;编译器几乎不知道或关心行号;标准C中的goto机制被限制为当前函数中的标签(不包括'setjmp()'和'longjmp()' - 它们不是'goto',而是'返回'机制。你为什么要这样做;而且你必须跳转到某个函数内部的代码(代码和标签不能在函数之外存在),并且通过“函数间goto”设置调用堆栈将是多毛的,并且不合理,并且......一般来说,这不是一个好主意! –

+0

解决方案是正确的程序设计,这样滥用'goto'是一个非常糟糕的主意。然而,看这个代码,这是你的问题中最少的。为什么你使用浮点数来进行像这样的简单计算?为什么你用'char'来存储整数值?您是否知道char和隐式整数促销的实现定义的签名?你为什么不使用stdint.h?等等这个代码需要一个彻底的检修。 – Lundin

+0

观察:像'right - =((error)* k)'这样的行;'读取一些数据。它看起来像是取消引用一个指针'k'并强制结果输入'error'。然而,更仔细的检查表明'k'是一个'char','error'是一个'float',所以实际上它是RHS的简单乘法。括号是多余的:它可以写成'right - = error * k;',这更容易理解。 –

回答

2

你应该调整你的代码,以避免去。另一个循环是需要的。像这样的东西。

void followWall(void) 
{ 
    repeat = 0; 
    do // add this loop here 
    { 
     ... 
     ... 
     while(bumpFlag == 0) 
     { 
     repeat = 0; 
     .. 
     .. 
     if(motorAngle < 180) 
     { 
      .. 
      if(error > 200) 
      { 
      .. 
      .. 
      repeat = 1; 
      break; 
      } 
     } 
     else if (motorAngle > 180) 
     { 
      .. 
      if(error > 200) 
      { 
      .. 
      .. 
      repeat = 1; 
      break; 
      } 
     } 
     } 
    } while (repeat == 1); 
}