0
関数の外にある埋め込みCで行番号を呼び出すことはできますか?関数の外側の埋め込みCの行番号に移動
私は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
}
いいえ、コンパイラは行番号をほとんど知りません。標準Cの 'goto'メカニズムは、' setjmp() 'と' longjmp() 'を数えずに現在の関数のラベルに制約されています - それらは' goto'ではなく '' back to 'メカニズムです。なぜあなたはそれをしたいと思いますか?あなたはいくつかの関数の中でコードにジャンプしなければならないでしょう(コードとラベルは関数の外には存在できません)。そして、関数間のgotoを渡ってコールスタックを設定するのは、それは良い考えではありません! –
解決策は適切なプログラム設計であり、このように 'goto'を悪用することは非常に悪い考えです。しかし、このコードを見ると、それはあなたの問題の中で最も少なくなります。なぜこれらのような簡単な計算に浮動小数点数を使用していますか?なぜ整数値を格納するために 'char'を使用していますか?インプリメンテーションで定義されたcharと暗黙の整数型宣言の署名が分かっていますか?なぜstdint.hを使用していないのですか?このコードは完全なオーバーホールが必要です。 – Lundin
観測: 'right - =((error)* k);'のような行は、少し読んでください。あなたがポインタ 'k'を逆参照していて、その結果を' error'と打ち込むように見えます。しかし、より厳密な調査では、 'k'は' char'で、 'error'は' float'であることが分かります。実際はRHSでの単純な乗算です。括弧はすべて余分です: 'right - = error * k;'と書くことができますが、これは理解しやすいものです。 –