私はロボットに取り組んでいます。これは私たちの大学の夏のロボット工学ワークショップの一部です。A-WITのC-STAMPマイクロコントローラーを使用しています。動かす、左に曲がる、右に曲がる、後退することができました。コントラスト センサーを使用して、黒いテープに沿って移動することさえできました。
テーブルの黒いテープに向かって 30 ~ 45 度の角度でロボットを送ると、ロボットは自動的に整列し、黒いテープに沿って動き始めます。おそらく以下の私のプログラミング ロジックが原因で、少しぎくしゃくします。while ループを実行し、常に if ステートメントをチェックしているため、数ミリ秒ごとに左右に回転しようとします。しかし、大丈夫です。動作します。動作するほどスムーズではありませんが、動作します! 問題は、ロボットを黒いテープの長方形の経路に入れないことです。黒いテープの角に達するとすぐに、左または右に曲がるのではなく、まっすぐ進みます。
私の 2 つのセンサーは、ロボットの真下、前輪の隣、ほぼ床レベルにあります。0 から 8 までの「インデックス」値があります。8 が最も明るいコントラストで、0 が最も暗いコントラストです。したがって、ロボットが黒いテープ ゾーンに移動すると、インデックス値が低下し、それに基づいて、ロボットに左または右に曲がるように指示する if ステートメントがあります。
これが私の試みです。混乱を避けるために、ソース コード全体は投稿しませんでしたが、黒いテープに沿ったロボットの動きに関与する論理部分のみを投稿しました。
while(1) {
// don't worry about these.
// 10 and 9 represent Sensor's PIN location on the motherboard
V = ANALOGIN(10, 1, 0, 0, 0);
V2 = ANALOGIN(9, 1, 0, 0, 0);
// i got this "formula" from the example in my Manual.
// V stands for voltage of the sensor.
// it gives me the index value of the sensor. 0 = darkest, 8 = lightest.
index = ((-(V - 5) / 5) * 8 + 0.5);
index2 = ((-(V2 - 5) / 5) * 8 + 0.5);
// i've tweaked the position of the sensors so index > 7 is just right number.
// the robot will move anywhere on the table just fine with index > 7.
// as soon as it drops to or below 7 (i.e. finds black tape), the robot will
// either turn left or right and then go forward.
// lp & rp represent left-wheel pin and right-wheel pin, 1 means run forever.
// if i change it from 1 to 100, it will go forward for 100ms.
if (index > 7 && index2 > 7)
goForward(lp, rp, 1);
if (index <= 7) {
turnLeft(lp, rp, 1);
goForward(lp, rp, 1);
// this is the tricky part. i've added this code last minute
// trying to make my robot turn, but i didn't work.
if (index > 4) {
turnLeft(lp, rp, 1);
goForward(lp, rp, 1);
}
}
else if (index2 <= 7) {
turnRight(lp, rp, 1);
goForward(lp, rp, 1);
// this is also the last minute addition. it's same code as above
// but it's for the 2nd sensor.
if (index2 > 4) {
turnRight(lp, rp, 1);
goForward(lp, rp, 1);
}
}
}
私はそれを理解しようとして一日中過ごしました。私はほとんどすべての道を使い果たしました。スタックオーバーフローで解決策を求めることは、私の最後の選択肢です。
前もって感謝します!コードについて質問がある場合はお知らせください。ただし、コメントは一目瞭然です。
これは、誰かが疑問に思った場合に備えて、私の goForward 関数です。
void goForward(BYTE lp, BYTE rp, WORD t)
{
WORD i;
for(i = 0; i < t; i = i + 1){
PULSOUT(lp, 400, 1, 1);
PULSOUT(rp, 800, 1, 1);
PAUSE(17);
}
}
更新:これが私がこれまでに思いついたことです。以前に投稿したすべての if ステートメントを消去し、ロジックをゼロから作成することにしました。
// if there's enough bright light in both sensors at the same time
// robot will move forward forever.
if (index > 7 && index2 > 7)
goForward(lp, rp, 1);
// but if there's not enough bright light anymore (i.e. reached black tape)
// proceed to the else-statement.
else {
// if left sensor detects the black tape then turn right
// if doesn't detect the black tape then keep going forward
if (index2 <= 7)
turnRight(lp, rp, 1);
else
goForward(lp, rp, 1);
// if right sensor detects the black tape then turn left
// if it doesn't detect the black tape then keep going forward
if (index <= 7)
turnLeft(lp, rp, 1);
else
goForward(lp, rp, 1);
}
// The reason for turnLeft and turnRight is to keep robot re-alligning
// to the black tape. It will happen so fast (every 1ms) that the jerking
// shouldn't even be noticeable.