Docsity
Docsity

Prepare-se para as provas
Prepare-se para as provas

Estude fácil! Tem muito documento disponível na Docsity


Ganhe pontos para baixar
Ganhe pontos para baixar

Ganhe pontos ajudando outros esrudantes ou compre um plano Premium


Guias e Dicas
Guias e Dicas


Squeak Learn Programming with Robots - Stephane Ducasse, Notas de estudo de Mecatrônica

mecatronica e robotica

Tipologia: Notas de estudo

2014

Compartilhado em 02/07/2014

danilo-magalhaes-maga-12
danilo-magalhaes-maga-12 🇧🇷

4.7

(32)

52 documentos

1 / 362

Toggle sidebar

Esta página não é visível na pré-visualização

Não perca as partes importantes!

bg1
Squeak
Learn Programming with Robots
STÉPHANE DUCASSE
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38
pf39
pf3a
pf3b
pf3c
pf3d
pf3e
pf3f
pf40
pf41
pf42
pf43
pf44
pf45
pf46
pf47
pf48
pf49
pf4a
pf4b
pf4c
pf4d
pf4e
pf4f
pf50
pf51
pf52
pf53
pf54
pf55
pf56
pf57
pf58
pf59
pf5a
pf5b
pf5c
pf5d
pf5e
pf5f
pf60
pf61
pf62
pf63
pf64

Pré-visualização parcial do texto

Baixe Squeak Learn Programming with Robots - Stephane Ducasse e outras Notas de estudo em PDF para Mecatrônica, somente na Docsity!

Squeak

Learn Programming with Robots

STÉPHANE DUCASSE

Squeak: Learn Programming with Robots Copyright © 2005 by Stéphane Ducasse Lead Editor: Jonathan Hassell Editorial Board: Steve Anglin, Dan Appleman, Ewan Buckingham, Gary Cornell, Tony Davis, Jason Gilmore, Jonathan Hassell, Chris Mills, Dominic Shakeshaft, Jim Sumser Assistant Publisher: Grace Wong Project Manager: Kylie Johnston Copy Manager: Nicole LeClerc Copy Editor: David Kramer Production Manager: Kari Brooks-Copony Production Editor: Kelly Winquist Compositor: Diana Van Winkle, Van Winkle Design Group Proofreader: Elizabeth Berry Indexer: Valerie Perry Artist: Diana Van Winkle, Van Winkle Design Group Cover Designer: Kurt Krames Manufacturing Manager: Tom Debolski Library of Congress Cataloging-in-Publication Data Ducasse, Stéphane. Squeak : learn programming with robots / Stéphane Ducasse. p. cm. ISBN 1-59059-491-

  1. Robots--Programming. I. Title. TJ211.45.D83 2005 629.8'925117--dc 2005013248 All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying, recording, or by any information storage or retrieval system, without the prior written permission of the copyright owner and the publisher. Printed and bound in the United States of America 9 8 7 6 5 4 3 2 1 Trademarked names may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, we use the names only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. Distributed to the book trade in the United States by Springer-Verlag New York, Inc., 233 Spring Street, 6th Floor, New York, NY 10013, and outside the United States by Springer-Verlag GmbH & Co. KG, Tiergartenstr. 17, 69112 Heidelberg, Germany. In the United States: phone 1-800-SPRINGER, fax 201-348-4505, e-mail [email protected], or visit http://www.springer-ny.com. Outside the United States: fax +49 6221 345229, e-mail [email protected], or visit http://www.springer.de. For information on translations, please contact Apress directly at 2560 Ninth Street, Suite 219, Berkeley, CA 94710. Phone 510-549-5930, fax 510-549-5939, e-mail [email protected], or visit http://www.apress.com. The information in this book is distributed on an “as is” basis, without warranty. Although every precaution has been taken in the preparation of this work, neither the author(s) nor Apress shall have any liability to any person or entity with respect to any loss or damage caused or alleged to be caused directly or indirectly by the information contained in this work. The source code for this book is available to readers at http://www.apress.com in the Downloads section.

CONTENTS AT A GLANCE v PART 4 ■ ■ ■^ Conditionals ■ CHAPTER 18 Conditions.................................................... 209 ■ CHAPTER 19 Conditional Loops............................................ 221 ■ CHAPTER 20 Boolean and Boolean Expressions............................. 233 ■ CHAPTER 21 Coordinates, Points, and Absolute Moves...................... 243 ■ CHAPTER 22 Advanced Robot Behavior..................................... 261 ■ CHAPTER 23 Simulating Animal Behavior................................... 269 PART 5 ■ ■ ■^ Other Squeak Worlds ■ CHAPTER 24 A Tour of eToy................................................ 289 ■ CHAPTER 25 A Tour of Alice................................................ 315 ■ INDEX....................................................................... 337

Contents

Foreword........................................................................ xvii

About the Author................................................................. xxiii

Acknowledgments............................................................... xxv

Preface........................................................................ xxvii

  • ■ CHAPTER 1 Installation and Creating a Robot PART 1 ■ ■ ■ Getting Started
    • Installing the Environment
      • Installation on a Macintosh
      • Installation under Windows
    • Opening the Environment
      • Tips for Installation
    • First Interactions with a Robot
      • Sending Messages to a Robot
    • Creating a New Robot
    • Quitting and Saving
    • Installation Troubleshooting
    • Summary
  • ■ CHAPTER 2 A First Script and Its Implications
    • Using a Cascade to Send Multiple Messages
    • A First Script
    • Squeak and Smalltalk
      • Programming Languages
      • Smalltalk and Squeak
    • Programs, Expressions, and Messages
      • Typing and Executing Programs
      • The Anatomy of a Script
      • About Pixels
      • Expressions, Messages, and Methods
      • Message Separation
      • Method
      • Cascade
      • Creating New Robots
    • Errors in Programs
      • Misspelling a Message Selector
      • Misspelling a Variable Name
      • Unused Variables
      • Uppercase or Lowercase?
      • Forgetting a Period
      • Words That Change Color
    • Summary
  • ■ CHAPTER 3 Of Robots and Men
    • Creating Robots
    • Drawing Line Segments
    • Changing Directions
    • The ABC of Drawing
    • Controlling Robot Visibility
    • Summary
  • ■ CHAPTER 4 Directions and Angles
    • Right or Left?
      • A Directional Convention
    • Absolute Versus Relative Orientation
    • The Right Angle of Things
    • A Robot Clock
    • Simple Drawings
    • Regular Polygons
    • Summary
  • ■ CHAPTER 5 Pica’s Environment
    • The Main Menu
      • Obtaining a Bot Workspace
    • Interacting with Squeak
    • Using the Bot Workspace to Save a Script
    • Loading a Script
    • Capturing a Drawing
    • Message Result
    • Executing a Script
      • Hints
      • Two Examples
    • Summary
  • ■ CHAPTER 6 Fun with Robots
    • Robot Handles
    • Pen Size and Color
    • More about Colors
    • Changing a Robot’s Shape and Size
    • Drawing Your Own Robot
    • Saving and Restoring Graphics
      • The “Save Graphics” Handle
      • Retooling the Robot Factory
      • Graphics Operations Using Scripts
    • Summary
  • ■ CHAPTER 7 Looping PART 2 ■ ■ ■ Elementary Programming Concepts
    • A Star Is Born
      • Loops to the Rescue
      • Loops at Work
      • Code Indentation
    • Drawing Regular Geometric Figures
    • Rediscovering the Pyramids
    • Further Experiments with Loops
    • Summary
  • ■ CHAPTER 8 Variables
    • Brought to You by the Letter A
      • Variations on the Theme of A
    • Variables to the Rescue
      • Declaring a Variable
      • Assigning a Value to a Variable
      • Referring to Variables
      • And What About Pica?
    • Using Variables.
      • The Power of Variables
      • Expressing Relationships Between Variables
    • Experimenting with Variables
      • The Pyramids Rediscovered
    • Automated Polygons Using Variables
    • Regular Polygons with Fixed Sizes
    • Summary
  • ■ CHAPTER 9 Digging Deeper into Variables
    • Naming Variables
    • Variables as Boxes
    • Assignment: The Right and Left Parts of :=
    • Analyzing Some Simple Scripts
    • Summary
  • ■ CHAPTER 10 Loops and Variables
    • A Bizarre Staircase
    • Practice with Loops and Variables: Mazes, Spirals, and More
    • Some Important Points for Using Variables and Loops
      • Variable Initialization
      • Using and Changing the Value of a Variable
    • Advanced Experiments
    • Summary
  • ■ CHAPTER 11 Composing Messages
    • The Three Types of Messages
    • Identifying Messages
    • The Three Types of Messages in Detail
      • Unary Messages
      • Binary Messages
      • Keyword-Based Messages
    • Order of Execution
      • Rule 1: Unary > Binary > Keywords
      • Rule 2: Parentheses First
      • Rule 3: From Left to Right
    • Summary
  • ■ CHAPTER 12 Methods: Named Message Sequences PART 3 ■ ■ ■ Bringing Abstraction into Play
    • Scripts versus Methods
    • How Do We Define a Method? - A Class Bot Browser - Creating a New Method Category - Defining Your First Method
    • What’s in a Method? - Scripts versus Methods: An Analysis
    • Returning a Value
    • Drawing Patterns
    • Summary
    • Glossary
  • ■ CHAPTER 13 Combining Methods
    • Nothing Really New: The Square Method Revisited
    • Other Graphical Patterns
    • What Do These Experiments Tell You?
    • Squares Everywhere
    • Summary
  • ■ CHAPTER 14 Parameters and Arguments - What Is a Parameter? - A Method for Drawing Squares
    • Practice with Parameters
    • Variables in Methods.
    • Experimenting with Multiple Arguments
    • Parameters and Variables
    • Arguments and Parameters - About Method Execution
    • Summary
  • ■ CHAPTER 15 Errors and Debugging
    • The Default Value of a Variable
    • Looking at Message Execution
    • A First Look at the Debugger
    • Stepping through the Stack
    • Fixing Errors
      • Example
      • Example
    • Summary
  • ■ CHAPTER 16 Decomposing to Recompose
    • Mazes and Spirals
      • Centered Squares
      • Spirals
    • Golden Rectangles
      • A One-Line-per-Rectangle Solution
    • Tiling
    • Summary
  • ■ CHAPTER 17 Strings, and Tools for Understanding Programs
    • Strings
    • Communicating with the User
    • Strings and Characters
    • Strings and Numbers
    • Using the Transcript
    • Generating and Understanding a Trace
    • Summary
  • ■ CHAPTER 18 Conditions PART 4 ■ ■ ■ Conditionals
    • A Robot’s True Colors
      • Adding a Trace to See What Is Going On
      • The Value Returned by a Method
    • Conditional Expressions with Only One Branch
      • Choose the Right Conditional Method
    • Nesting Conditional Expressions
      • Robot Coloring with Three Colors
    • Learning from Your Mistakes
    • Interpreting a Tiny Language
      • Further Experiments
    • Summary
  • ■ CHAPTER 19 Conditional Loops
    • Conditional Loops
      • An Example
    • Experiences with Traces
    • Stopping an Infinite Loop
    • Deeper into Conditional Loops
    • A Simple Interactive Application
    • When to Use Square Brackets
    • Summary
  • ■ CHAPTER 20 Boolean and Boolean Expressions
    • Boolean Values and Boolean Expressions
      • Boolean Values
      • Boolean Expressions
    • Combining Basic Boolean Expressions
      • Negation (not)
      • Conjunction (and)
      • Alternation (or)
      • All of the Above
    • Some Smalltalk Points
    • Missing Parentheses (a Frequent Mistake)
      • A Case Study
      • Using the Debugger
      • Understanding the Problem
      • Similar Problems and Solutions
    • Summary
  • ■ CHAPTER 21 Coordinates, Points, and Absolute Moves
    • Points
    • Using Grids
    • A Source of Errors with Points
      • Decomposing 50@60 + 200@400
      • Decomposing (50@60) + (200@400)
    • Absolute Moves
    • Relative versus Absolute Motion
    • Some Experiments
    • Translations
      • Translating Triangles
      • Flying Geese
    • Absolute Moves at Work
    • Loops and Translations
    • Further Experiments
    • Summary
  • ■ CHAPTER 22 Advanced Robot Behavior
    • Obtaining a Robot’s Direction
    • Pointing in a Direction
    • Distance from a Point
    • Back in the Center of the Screen
    • Location If It Moved
      • In a Box
    • Heading toward a Point
    • Center versus Position
    • Summary
  • ■ CHAPTER 23 Simulating Animal Behavior
    • Wandering
      • Separating Influences
      • Studying the Influence of the Length
      • Studying the Influence of the Side to Which the Animal Turns
    • Trapped in a Box
      • Following Borders
      • Flying to the Opposite Border
      • Random Direction
      • Introducing an Exit in the Box
    • Staying in a Healthy Environment
      • Further Experiments
    • Finding Food
      • Comparing Distance
      • Taking One’s Bearings
      • Simulating Vision
    • Summary
  • ■ CHAPTER 24 A Tour of eToy PART 5 ■ ■ ■ Other Squeak Worlds - Steering an Airplane. - Step 1: Drawing an Airplane - Step 2: Playing with the Halo - Step 3: Dragging and Dropping a Method to Create New Scripts - Step 4: Adding Methods
    • Joysticks in Action - Step 1: Creating a Joystick - Step 2: Experimenting with a Joystick - Step 3: Linking the Joystick and the Script
    • Creating an Animation - Step 1: Creating the Holder - Step 2: Drawing Animation Elements - Step 3: Dropping the Pictures into the Holder - Step 4: Creating a Simple Sketch Recipient of the Animation - Step 5: Creating a Script with lookLike - Step 6: Displaying the Selected Animation Element - Step 7: Changing the Currently Selected Element of a Holder - Another Way
    • Cars and Drivers - Step 1: Draw a Car and a Steering Wheel - Step 2: Turning the Car in a Circle - Step 3: Using the Wheel’s Heading - Step 1: Sensors - Step 2: The Road - Step 3: Conditions and Tests in eToy - Step 4: Customizing Color-Based Tests - Step 5: Adding Actions
    • Some Tricks - Running Several Scripts - Clearing - Creating a Tile - Internationalization
    • Summary
  • ■ CHAPTER 25 A Tour of Alice
    • Getting Started with Alice
    • Interacting Directly with Actors
    • The Environment
    • Scripts
      • Analyzing a First Script
      • Moving, Turning, and Rolling
    • Actor Parts
    • Other Operations
      • Getting Bigger
      • Quantified Moves
      • Standing Up
      • Coloring
      • Destruction
      • Visibility
      • Absolute Moves and Rotations
      • Pointing At
      • Relative Placement of Actors
    • Time-Related Actions
    • Animation
    • Your Own Wonderland
    • Multiple Cameras and Other Special Effects
    • Alarms
    • Introducing User Interaction
    • Hidden Aspects of Alice and Pooh
      • Mapping 2D Morphs to 3D
      • Pooh: Generating 3D Forms from 2D
    • Summary
  • ■ INDEX

Perhaps the single most profound set of language ideas and representations happened earlier than Algol, but took much longer for most computer people to understand (and many never did), in part because of the different and difficult-to-read notation (for outsiders at least), and because many of LISP’s greatest contributions were “really meta.” One of the great contributions of LISP was its evaluator written in itself in a half page of code. This was a kind of “Maxwell’s Equations” for programming, and it allowed many things to be thought about that were essentially unthinkable in more normal approaches. LISP itself was driven into existence to be the programming system for an interactive commonsense agent—The Advice Taker—that could take the wishes of a human user given in normal vernacular and turn them into computer processes that would carry out those wishes. Some very interesting intermediate languages, such as FLIP, and attempts at doing some of the Advice Taker properties, such as PILOT, were created in the mid 1960s. Sketchpad was perhaps the most radical of the early systems because it tried to leap all the way to a reasonable interactive framework for people who wanted to use the computer for what it was best suited: interactive simulations of all kinds. The three cosmic contributions of Sketchpad were

  • The first usable approach to interactive computer graphics
  • A real object structure for all of its entities
  • A nonprocedural way to program in terms of the desired end results, where the system could employ various automatic problem-solving processes to come up with the desired results This was helped tremendously by a “tolerance approach” to solving constraints, which instead of trying for perfect logic/symbolic solutions of the sets of constraints instead tried to solve the constraints within global tolerances. This approach allowed many important problems to be dealt with that are still difficult or intractable symbolically today. JOSS was a very different cup of tea: it did “almost nothing” (basically numerical calcula- tions using numbers and array structures), but what it did do was done perfectly and in the form of what is still one of the great user interface designs in history. A Programming Language was the name of a book by Kenneth Iverson that took a highly mathematical approach to programming via functions and metafunctions expressed as a kind of algebra. In those days the language was called “Iverson.” An actual system in which you could program a computer was still just an IBM rumor at the time, but many paper programs were written using these ideas. The best thing about Iverson was that it really paid off if you thought of it as mathematical transforms and relationships, and didn’t worry about how many operations would be required. Not worrying about number of operations was almost unthink- able in those days of 1 MHz clocks on multimillion-dollar building-sized computers, so Iverson and LISP were both very liberating vehicles for thinking ahead to the future, when machines would be smaller physically, and larger and faster logically. The Simula designers wanted to model large, complex dynamic structures and realized that Algol blocks would do the job if you could cut them loose from Algol’s hierarchical control structure. In the creation of Simula I in the mid 1960s, they were able to see that their ideas had great relevance to the language and its programming, and when they did Simula 67 they could replace many formerly built-in data types, such as string, with a Simula 67 class. xviii ■FOREWORD

The idea of extending the syntax, semantics, and pragmatics of programming languages constituted an entire genre of investigation in the mid-to-late 1960s. One of the reasons for this is that it had become abundantly clear that programming was going to be difficult to scale, and that scalability in most dimensions was going to be critical to the health of comput- ing. Where complexity is a central issue, architecture dominates materials. This realization started to make programming appear as something different from math, and it started to reveal itself as a new form of engineering. There were calls for the formation of a discipline to be called “software engineering” and to have a conference to try to figure out what this might mean (how to cope if you can’t just do math?). ARPA Information Processing Techniques Office (IPTO) was in full swing by the time I went to graduate school in 1966, and it had already made some great starts toward its collective dream of having interactive computing for everyone pervasively connected via an “intergalactic net- work.” Just how to create this network (which had huge scaling requirements) generated some of the best systems thinking of the time, and was an important part of my own thinking about the future of programming. The ARPA funders were wise and did not turn the vision of their dream into funding goals, but instead tried to find and fund talents that had their own ideas about what the dream meant and how it could be done. This resulted in about 17 sites in universities and companies, most of which had come up with very interesting and different designs and demos. This constituted a community of both “agreement and argument” that made everyone in it much smarter than they were before they joined the great dream. Of course, given Jean Sammit’s 3,000 languages, there is much I haven’t mentioned, and much interesting design that happened from 1967 to the end of the decade that has to be omit- ted here. To pick just five developments of particular relevance to the readers of this book, I would choose the conception of objects that I came up with, and how they were supposed to be useful to end users of personal computers; Carl Hewitt’s PLANNER system, which was the most cohesive system for doing “programming as reasoning”; Ned Irons’s IMP system, which repre- sents perhaps the first really useful completely extensible language; and Dave Fisher’s Control Definition Language, which illuminated extensibility in general and with respect to control structures in particular. My background was in mathematics and molecular biology (I worked my way through school as a journeyman programmer) and in the arts. Circumstances forced me to try to understand Sketchpad, Simula, and the proposed ARPA intergalactic network in my first week in graduate school, and the reaction I had was cataclysmic. They were similar in some ways and very different in others, but they were different species of the same genus if one took both a biological and mathematical perspective. Biologically, they were “almost cells” crying out to be cells. Mathematically, they were “almost algebras” crying out to be algebras. So my initial fusion of these metaphors with computing was that you could make everything from entities that were logical computers that could send messages (which would also have to be logical computers). The logical computers would act the part of cells, and the protocols devised could be very algebraic—what today is (incorrectly) called polymorphism. This would result in great simplicity and scalability at the “materials level,” and would open the door for advancements in simplicity and scalability at the “expression level” where the programmer lived. Several years later I found Hewitt’s PLANNER, and realized that it was the basis of a way to get programs to be both more meaningful and more scalable. (Many of the ideas of PLAN- NER also turned up in the later language called Prolog.) It was pretty clear that trying to send messages that were goal-oriented could greatly help scalability, in part because there are far ■FOREWORD xix

devoted to making something so much better as to render Smalltalk obsolete (a fate I would dearly love to see happen). So, I would like to encourage the readers of this excellent new book to not think of Smalltalk as a bunch of features from the vendor gods that must be adhered to, but as a sys- tem that is capable of great extension in all dimensions that will reward those who come up with better ways to program. At PARC we changed Smalltalk every few weeks, and in a major way every two years. Though it has hardly changed since then, please do and put those big changes out on the Internet for all of us to learn from and enjoy! ■FOREWORD xxi

About the Author

STÉPHANE DUCASSE obtained his Ph.D. at the University of Nice-Sophia Antipolis and his habilitation at the University of Paris 6. He was recipient of the SNF 2002 Professeur Boursier Award. He is now Professor at the University of Berne and the Université of Savoie. Stéphane’s fields of interests are design of reflective systems, object-oriented languages design, composition of software components, design and implementation of applications, reengineering of object-oriented applications, and teaching novices. He is the main developer of the Moose reengineering environment. He loves programming in Smalltalk and is the presi- dent of the European Smalltalk User Group. Stéphane has written several books in French and English: La programmation: une approche fonctionnelle et recursive en Scheme (Eyrolles 96), Squeak (Eyrolles 2001), and Object-Oriented Reengineering Patterns (MKP 2002). If you want to discover why Stéphane is having fun with Squeak and actively participating in its development, check out http://www.squeak.org/. Check out http://smallwiki.unibe.ch/ botsinc/ for the web site of this book. xxiii