robotc 编程挑战 (9)

任务简介

本文为 virtual world challenge pack 中的传感器(SENSORS) 中的
“vacuum”, 如图 1 所示. 要求从起点 A 出发, 当碰到场地一条边时就转向.
要求必须触碰到每条边. 场地的边开始的时候显示为绿色, 当触碰到边时,
边的绿色会消失.

挑战名称为 “vacuum”, 意为吸尘器. 类似家用扫地机器人的循边.


图 1
图 1 vacuum 界面图

此挑战使用的 robot 为 “Clawbot IQ-arm up”, 基本配置如图 2 所示.


图 2
图 2 robot 基本配置

程序


#pragma config(StandardModel, "Clawbot IQ With Sensors")
//*!!Code automatically generated by 'ROBOTC' configuration wizard               !!*//

task main()
{
    /*   */
    short touched_count = 0;
    const short wall_count = 4;
    const float speed = 100;

    /* four walls should be touched */
    while(touched_count < wall_count){
        // starts to go forward
        setMotorSpeed(leftMotor, speed);
        setMotorSpeed(rightMotor, speed);

        // loop untill touched a wall
        while(getBumperValue(bumpSwitch) == 1){
            ++touched_count;

            // backward a little
            setMotorSpeed(leftMotor, -speed);
            setMotorSpeed(rightMotor, -speed);
            sleep(300);

            // turn left 90 degrees
            setMotorSpeed(leftMotor, -speed);
            setMotorSpeed(rightMotor, speed);
            sleep(470); // when the speed is 100, it is exact 90 degrees
        }
    }

    /*  */
    stopAllMotors();
}

图 3 robot 运行中间截图.


图 3
图 3 运行中间截图

运行结果如图 4 所示.


图 4
图 4 运行结果

猜你喜欢

转载自blog.csdn.net/ding_yingzi/article/details/80146911