1 / 190

Karel J Robot

Karel J Robot. A Gentle Introduction to the Art of Object Oriented Programming Lesson 2A. Primitive Instructions and Simple Programs. Table of Contents. 2.1 Changing Positions 2.2 Turning in Place 2.3 Finishing a Task 2.4 Handling Beepers 2.5 Robot Descriptions 2.6 A Complete Program

booth
Télécharger la présentation

Karel J Robot

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Karel J Robot A Gentle Introduction to the Art of Object Oriented Programming Lesson 2A

  2. Primitive Instructions and Simple Programs

  3. Table of Contents • 2.1 Changing Positions • 2.2 Turning in Place • 2.3 Finishing a Task • 2.4 Handling Beepers • 2.5 Robot Descriptions • 2.6 A Complete Program • 2.6.1 Executing a Program • 2.6.2 The Form of a Robot Program • Important Ideas From This Lesson • Problem Set

  4. Primitive Instructions and Simple Programs • This lesson begins our study of the robot programming language. • We will start with a detailed explanation of the primitive instructions that are built into every robot's vocabulary.

  5. Primitive Instructions and Simple Programs • Using these instructions, we can instruct any robot to move through the world and handle beepers.

  6. Primitive Instructions and Simple Programs • On the next slide is a complete robot program. Later in this lesson we will discusses the elementary punctuation and grammar rules of the robot programming language.

  7. Primitive Instructions and Simple Programs

  8. Primitive Instructions and Simple Programs • By the end of this lesson you will be able to write programs like the one just shown that instruct robots to perform simple obstacle avoidance and beeper transportation tasks.

  9. The meaning of execute • Before explaining the primitive instructions of the robot programming language, we must first define the technical term execute.

  10. execute instruction • A robot executes an instruction by performing the associated action or actions.

  11. execute program • The robot executes a programby executing a sequence of instructionsthat are given to it by the helicopter pilot.

  12. message • Each instruction in such a sequence is delivered to the robot in a message, which directs one robot to perform one instruction in the program.

  13. 2.1 Changing Position • Every robot understands two primitive instructions that change its position. • The first of these instructions is move, which changes a robot's location.

  14. move • When a robot is sent a move message it executes a move instruction and moves forward one block; it continues to face the same direction. • To avoid damage, a robot will not move forward if it sees a wall section or boundary wall between its current location and the corner to which it would move. • Instead, it turns itself off.

  15. error shutoff • This action, called an error shutoff, will be explained further later in this lesson.

  16. move • From this definition we see that a robot executes a move instruction by moving forward to the next corner. • The wall section is more than one half-block away and cannot block this robot's move.

  17. move • When this robot tries to execute an incorrect attempt to move in this situation, it sees a wall section. • Relying on its self-preservation instinct, it performs an error shutoff.

  18. 2.2 Turning in Place • The second primitive instruction that changes a robot's position is turnLeft . • This instruction changes the direction in which the robot is facing but does not alter its location.

  19. turnLeft • When a robot is sent a turnLeft message it executes a turnLeft instruction by pivoting 90 degrees to the left. • The robot remains on the same street corner while executing a turnLeft instruction.

  20. turnLeft • Because it is impossible for a wall section to block a robot's turn, turnLeft cannot cause an error shutoff.

  21. tasks • A robot always starts a task on some corner, facing either north, south, east, or west. • A robot cannot travel fractions of a block or turn at other than 90 degree angles.

  22. tasks • Although move and turnLeft change the robot's position, after executing either of these instructions, the robot still is on some corner and still is facing one of the four compass directions.

  23. turnRight? • Karel-Werke's designer purposely did not provide a built-in turnRight instruction. • Why not? Think about it. • Would adding a turnRight to the primitive instructions enable the robot to perform any task it cannot already accomplish without turnRight?

  24. turnRight? • A moment's thought - and the right flash of insight - shows that the turnRight instruction is unnecessary; it does not enable robots to accomplish any new tasks.

  25. turnRight? • The key observation for verifying this conclusion is that a robot can manage the equivalent of a turnRight instruction by executing three turnLeft instructions.

  26. 2.3 Finishing a task • Finally, we need a way to tell a robot that its task is finished. • The turnOff instruction fulfills this requirement.

  27. turnOff • When a robot is sent a turnOff message, it executes a turnOff instruction. • It turns off and is incapable of executing any more instructions until restarted on another task.

  28. turnOff • The last instruction executed by every robot in a program is usually a turnOff instruction.

  29. 2.4 Handling beepers • Every robot understands two instructions that permit it to handle beepers. • These two instructions perform opposite actions.

  30. pickBeeper • When a robot is sent a pickBeeper message, it executes a pickBeeper instruction. • It picks up a beeper from the corner on which it is standing and then deposits the beeper in its beeper-bag.

  31. pickBeeper • If a pickBeeper instruction is attempted on a beeperless corner, the robot performs an error shutoff. • On a corner with more than one beeper the robot picks up one, and only one, of the beepers and then places it in the beeper-bag.

  32. putBeeper • When a robot is sent a putBeeper message, it executes a putBeeper instruction by extracting a beeper from its beeper-bag and placing the beeper on the current street corner.

  33. putBeeper • If a robot tries to execute a putBeeper instruction with an empty beeper-bag, the robot performs an error shutoff. • If the robot has more than one beeper in its beeper-bag, it extracts one, and only one, beeper and places it on the current corner.

  34. Beepers are small • Beepers are so small that robots can move right by them; only wall sections and boundary walls can block a robot's movement.

  35. Corners are large • Since corners are quite spacious, robots are very adept at avoiding each other if two or more show up on the same corner simultaneously.

  36. 2.5 Robot Descriptions • All robots produced by Karel-Werke have at least the capabilities just described. • As we will see, such robots are very primitive, and we might like robots with additional abilities.

  37. Robot Language • Therefore, we must have some way to describe those extra abilities so that the factory can build a robot to our specifications. • Karel-Werke employs a simple robot programming language to describe both robot abilities and the lists of robot instructions, called programs.

  38. method / class • The formal name for the description of a robot instruction is method. • The simple model of robot described above is called the ur_Robotclass.

  39. “ur” as in “poor” • ur is a German prefix meaning "original" or "primitive." • The pronunciation of ur is similar to the sound of "oor" in "poor".

  40. Robot class specification • The specification of the Robot class in the robot programming language is as follows... class Robot { void move(){...} void turnOff(){...} void turnLeft(){...} void pickBeeper(){...} void putBeeper(){...} }

  41. methods • Following the model class name is a list of instruction descriptions (methods) for this kind of robot. • The list is always written in braces { and }.

  42. methods • The use of elipsis in the {...} indicates that there are some things that belong here that are not being shown. • These are the descriptions of how an ur_Robot would carry out each of these methods.

  43. methods • The five methods, move through putBeeper, name actions that ur_Robots can perform. • We defined each of these actions in the foregoing sections, and we will see many examples of their use throughout this book.

  44. void methods • The word voidprefixes each of these methods to indicate that they return no feedback when executed. • Instead, they simply change the state of the robot, like changing position, handling beepers, etc.

  45. return methods • Later we will see additional methods that do produce feedback when executed, rather than changing the state of the robot as these instructions all do. • These are called return methods.

  46. void = doreturn = remember • Said differently, void means that these instructions do something as opposed to telling us something that they remember: robots do things and they remember things.

  47. ( ) • The matching parentheses that follow the method names mark them as the names of actions that a robot will be able to carry out.

  48. ( ) • Later on certain methods may have parentheses that contain important data items, but for now they will remain empty.

  49. Sample task • A sample task for an ur_Robot might be to start at the origin, facing east, and then walk three blocks east to a corner known to have a beeper, pick up the beeper, and turnOff on that corner.

  50. Sample task • A complete program to accomplish this task is shown next. • In this program we name the robot Karel , but we could use any convenient name.

More Related