我正在NXT lego智力風暴機器人上工作,並且我正在用單反射光傳感器構建線性跟隨器機器人,並使用C語言編程。電機通過C控制
傳感器將原始模擬值的數字值與範圍從0 - 1023
我不得不代碼行是黑色然後電機將向前移動,並且如果不存在線(白基地),然後停下來,如果有灰色(在白線和黑線之間),然後稍微向右移動,然後以不同方式向左移動以找到丟失的黑線。
因此,代碼是像
While (1)
{
a=ecrobot_get_light_sensor(port_led); //storing the A/D converted value in variable
while (a<300) // White area
{
ecrobot_status_monitor("White Area");
nxt_motor_set_speed(port_motor_l, 0, 1); // left motor turns off
nxt_motor_set_speed(port_motor_r, 0, 1); // right motor turns off
}
while (a>=600) // Black Line
{
ecrobot_status_monitor("Black Area");
nxt_motor_set_speed(port_motor_l, 100, 1); // left motor turns on
nxt_motor_set_speed(port_motor_r, 100, 1); // right motor turns on
}
while (a>=300 || a<600) // Robot loosing the black line
{
ecrobot_status_monitor("grey Area");
nxt_motor_set_speed(port_motor_l, 50, 1); // left motor move forward
nxt_motor_set_speed(port_motor_r, -50, 1); // right motor move backward
delay_ms(200)
nxt_motor_set_speed(port_motor_l, -50, 1); // left motor move backward
nxt_motor_set_speed(port_motor_r, 50, 1); // right motor move forward
delay_ms(200)
}
問題是,如果機器人失去線再啓動而無需停止並朝後200毫秒延遲的逆時針移動順時針移動。
我的代碼有什麼問題?
我怎樣才能停止電機此區間和電機後,搜索線的這種間隔後在其他方向應移動,然後它一定會找到一個線
謝謝!
多虧了你們倆,我改變了,而循環到如果條件下,現在的問題是,如果傳感器在黑線上,有時它會檢測到它,否則它只是順時針旋轉200毫秒,逆時針旋轉200毫秒,有沒有什麼辦法可以在移動過程中連續檢測線條,並且如果發現黑線則移動呢? 謝謝 – user2828488