我正在尝试创建一个基本的避障行为,但我希望它仅在光传感器超过某个值时才起作用。
while (!myFinch.isBeakDown())
{
myFinch.getLeftLightSensor();
light = myFinch.getLeftLightSensor();
if (light >= 20)
{
if (myFinch.isObstacleLeftSide())
{
myFinch.setWheelVelocities(-255,-255,750);
myFinch.setWheelVelocities(100,-255, 500);
}
else if (myFinch.isObstacleRightSide())
{
myFinch.setWheelVelocities(-255,-255,750);
myFinch.setWheelVelocities(-255, 100, 500);
}
else
{
myFinch.setWheelVelocities(255,255);
}
}
}
编辑 没有灯光预测试的工作避障代码。避障在没有灯光预测试的情况下也能工作,我还在 netbeans 传感器测试中测试了红外传感器。
// Run so long as the Finch is not pointed beak down
while(!myFinch.isBeakDown())
{
if(myFinch.isObstacleLeftSide())
{
myFinch.setWheelVelocities(-255,-255,750);
myFinch.setWheelVelocities(100,-255, 500);
}
// If there's an obstacle on the right, set LED blue, back up for 750 ms
// and turn for 500 ms
else if(myFinch.isObstacleRightSide())
{
myFinch.setWheelVelocities(-255,-255,750);
myFinch.setWheelVelocities(-255, 100, 500);
}
// Else, robot goes straight
else
{
myFinch.setWheelVelocities(255,255);
}
}