本文为 virtual world challenge pack 中的传感器(SENSORS) 中的
“Arm Position 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()
{
/* */
short touched = false;
const float speed = 100;
/* */
while(true){
// starts to go forward
setMotorSpeed(leftMotor, speed);
setMotorSpeed(rightMotor, speed);
//
if(getBumperValue(bumpSwitch) == 1){
touched = true;
//backward a litte
setMotorSpeed(leftMotor, -speed);
setMotorSpeed(rightMotor, -speed);
sleep(300);
//
stopAllMotors();
// strech claw
setMotorTarget(clawMotor, -10, speed);
waitUntilMotorStop(clawMotor);
// get down the arm
setMotorTarget(armMotor, -280, speed);
waitUntilMotorStop(armMotor);
// grab the block
setMotorTarget(clawMotor, 5, speed);
waitUntilMotorStop(clawMotor);
// get up the arm
setMotorTarget(armMotor, 280, speed);
waitUntilMotorStop(armMotor);
// backword
setMotorTarget(leftMotor, 0, speed);
setMotorTarget(rightMotor, 0, speed);
waitUntilMotorStop(leftMotor);
waitUntilMotorStop(rightMotor);
break;
}
}
}
图 3 robot 运行中间截图.
运行结果如图 4 所示.