本文为 virtual world challenge pack 中的传感器(SENSORS) 中的
“Touch Sensor Challenge”, 如图 1 所示. 要求从起点 A 出发, 前进至箱子处, 利用碰触传感器感知箱子,
并用使用前爪抓住箱子, 回到出发点.
此挑战使用的 robot 为 “Clawbot IQ-arm up”, 基本配置如图 2 所示.
#pragma config(StandardModel, "Clawbot IQ With Sensors")
//*!!Code automatically generated by 'ROBOTC' configuration wizard !!*//
task main()
{
const float motor_speed = 100;
// up the arm
setMotorTarget(armMotor, 100, motor_speed);
waitUntilMotorStop(armMotor);
while(true){
//go forward
setMotorSpeed(leftMotor, motor_speed);
setMotorSpeed(rightMotor, motor_speed);
if(getBumperValue(bumpSwitch) == true){
// backward a little
setMotorSpeed(leftMotor, -motor_speed);
setMotorSpeed(rightMotor, -motor_speed);
sleep(400);
stopAllMotors();
// down the arm
setMotorSpeed(armMotor, -motor_speed);
sleep(400);
// glab with claw
moveMotorTarget(clawMotor, 5, motor_speed);
waitUntilMotorStop(clawMotor);
// up the arm
//setMotorSpeed(armMotor, motor_speed);
//sleep(200);
//stopMotor(armMotor);
// backward
setMotorTarget(leftMotor, 0, motor_speed);
setMotorTarget(rightMotor, 0, motor_speed);
waitUntilMotorStop(rightMotor);
//
break;
}
}
}
图 3 robot 运行中间截图.
运行结果如图 4 所示.