programing lecture

Embed Size (px)

Citation preview

  • 7/25/2019 programing lecture

    1/10

    CS 141: Introduction to ProgrammingLecture 19

    Dr. Susan Bergin

    Email: [email protected]

    Room 2.108, Callan Building,

    Department of Computer Science, NUI Maynooth

  • 7/25/2019 programing lecture

    2/10

    Robots Revisited

    ! What do you need to know about the

    RoboCar class?

    " You need to know how to create an instance of

    a RoboCar

    " You need to know how to use the methods

    covered in lectures and in labs

  • 7/25/2019 programing lecture

    3/10

    Moving on to the sensors

    ! The NXT comes with a number of sensors andwere going to use the touch sensor.

    ! The touch sensor should be plugged into Port 1 of

    the NXT brick (see RoboCar API).! The method to check if the sensor has been

    pressed is the following:

    robot.touchSensorIsPressed();

  • 7/25/2019 programing lecture

    4/10

    Using the touch sensor - example

    if(touch sensor is pressed)

    {

    // Do something e.g. reverse the robot

    }

    else

    {

    // Do something different e.g. go Forward

    }

  • 7/25/2019 programing lecture

    5/10

    Touch sensor in action..

    publicclassTouchRobot {publicstaticvoidmain(String[] args) {

    RoboCar robot = newRoboCar();

    robot.setMotorSpeed(360);

    if(robot.touchSensorIsPressed())

    {

    robot.moveBackward(1000);

    }

    else

    {

    robot.moveForward(1000);

    }

    }

    }! But how do you get the program to continually check the selection statment?

  • 7/25/2019 programing lecture

    6/10

    Fixing this with a loop

    while(true)// creates an infinite loop

    {

    if(robot.touchSensorIsPressed())

    {

    robot.moveBackward(1000);

    }

    else

    {

    robot.moveForward(1000);

    }

    }

  • 7/25/2019 programing lecture

    7/10

    Putting It Together

    publicclassTouchRobotLoop1{

    publicstaticvoidmain(String[] args) {

    RoboCar robot = newRoboCar();

    robot.setMotorSpeed(360);

    while(true)

    {

    if ( robot.touchSensorIsPressed() ){

    robot.moveBackward(1000);

    }

    else

    {

    robot.moveForward(1000);

    }

    }

    }

    }

  • 7/25/2019 programing lecture

    8/10

    More methods

    ! moveForwardForDistance(float distance)

    " Method to move RoboCar forward the specified

    distance (approximately).

    ! moveBackwardForDistance(float distance

    " Method to move RoboCar backward the

    specified distance (approximately).

  • 7/25/2019 programing lecture

    9/10

    Something to think about..

    !How would you solve this problem?The robot needs to drive forwardfor an unknown distance until ithits an obstacle. It then needs tonavigate around the obstacle(distances specified) and continueagain until it hits the red wall. Trysolving this on paper now!

    !What if you dont know in advancehow many obstacles there arebefore the red wall but you knowthat all the obstacles are all thesame size and the red wall ismuch longer. Try solving this on

    paper now!

  • 7/25/2019 programing lecture

    10/10

    Preparing for your January exam

    ! Lets begin to work through an exam paper

    together