PROGRAMMING AND CUSTOMIZING THE MULTICORE PROPELLERTM MICROCONTROLLER This page intentionally left blank PROGRAMMING AND CUSTOMIZING THE MULTICORE PROPELLERTM MICROCONTROLLER THE OFFICIAL GUIDE PARALLAX INC. Shane Avery Chip Gracey Vern Graner Martin Hebel Joshua Hintze André LaMothe Andy Lindsay Jeff Martin Hanno Sander New York Chicago San Francisco Lisbon London Madrid Mexico City Milan New Delhi San Juan Seoul Singapore Sydney Toronto Copyright © 2010 by The McGraw-Hill Companies, Inc. All rights reserved. Except as permitted under the United States Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system, without the prior written permission of the publisher. ISBN: 978-0-07-166451-6 MHID: 0-07-166451-3 The material in this eBook also appears in the print version of this title: ISBN: 978-0-07-166450-9, MHID: 0-07-166450-5. All trademarks are trademarks of their respective owners. Rather than put a trademark symbol after every occurrence of a trademarked name, we use names in an editorial fashion only, and to the benefit of the trademark owner, with no intention of infringement of the trademark. Where such designations appear in this book, they have been printed with initial caps. McGraw-Hill eBooks are available at special quantity discounts to use as premiums and sales promotions, or for use in corporate training programs. To contact a representative please e-mail us at [email protected]. Information contained in this work has been obtained by The McGraw-Hill Companies, Inc. (McGraw-Hill) from sources believed to be reliable. However, neither McGraw-Hill nor its authors guarantee the accuracy or completeness of any information published herein, and neither McGraw-Hill nor its authors shall be responsible for any errors, omissions, or dam- ages arising out of use of this information. This work is published with the understanding that McGraw-Hill and its authors are supplying information but are not attempting to render engineering or other professional services. If such services are required, the assistance of an appropriate professional should be sought. TERMS OF USE This is a copyrighted work and The McGraw-Hill Companies, Inc. (McGraw-Hill) and its licensors reserve all rights in and to the work. Use of this work is subject to these terms. Except as permitted under the Copyright Act of 1976 and the right to store and retrieve one copy of the work, you may not decompile, disassemble, reverse engineer, reproduce, modify, create derivative works based upon, transmit, distribute, disseminate, sell, publish or sublicense the work or any part of it without McGraw-Hills prior consent. You may use the work for your own noncommercial and personal use; any other use of the work is strictly prohibited. Your right to use the work may be terminated if you fail to comply with these terms. THE WORK IS PROVIDED AS IS. McGRAW-HILL AND ITS LICENSORS MAKE NO GUARANTEES OR WAR- RANTIES AS TO THE ACCURACY, ADEQUACY OR COMPLETENESS OF OR RESULTS TO BE OBTAINED FROM USING THE WORK, INCLUDING ANY INFORMATION THAT CAN BE ACCESSED THROUGH THE WORK VIA HYPERLINK OR OTHERWISE, AND EXPRESSLY DISCLAIM ANY WARRANTY, EXPRESS OR IMPLIED, INCLUD- ING BUT NOT LIMITED TO IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. McGraw-Hill and its licensors do not warrant or guarantee that the functions contained in the work will meet your requirements or that its operation will be uninterrupted or error free. Neither McGraw-Hill nor its licensors shall be liable to you or anyone else for any inaccuracy, error or omission, regardless of cause, in the work or for any damages result- ing therefrom. McGraw-Hill has no responsibility for the content of any information accessed through the work. Under no cir- cumstances shall McGraw-Hill and/or its licensors be liable for any indirect, incidental, special, punitive, consequential or sim- ilar damages that result from the use of or inability to use the work, even if any of them has been advised of the possibility of such damages. This limitation of liability shall apply to any claim or cause whatsoever whether such claim or cause arises in contract, tort or otherwise. For Ellie, my lovely wife. S. A. To my wife Kym, my children Nic and Sami, and my Mom and Dad for all the support. V. L. G. For BJ and Kris, thanks for being so supportive. M. H. For my wife Crystal and son Hunter. J. M. H. To inventors everywhere. A. L. For Stacy, Kaitlin, and Kaylani who were wonderfully supportive despite being deprived of my attention. J. M. For Mami and Papi, thanks for everything. H. S. This page intentionally left blank CONTENTS About the Authors xi Foreword xv Introduction xvii Chapter 1 The Propeller Chip Multicore Microcontroller 1 Introduction 1 Multicore Defi ned 2 Why Multicore? 3 Multicore Propeller Microcontroller 3 Summary 14 Exercises 14 Chapter 2 Introduction to Propeller Programming 15 Introduction 15 What’s the Secret? 16 Ready to Dive In? 17 Let’s Get Connected! 17 Your First Propeller Application 22 A Blinking LED 25 RAM versus EEPROM 27 A More Powerful Blink 28 All Together Now 32 Wrapping It Up 34 Timing Is Everything 38 Sizing the Stack 41 Propeller Objects and Resources 45 Summary 48 Exercises 49 Chapter 3 Debugging Code for Multiple Cores 51 Propeller Features That Simplify Debugging 52 Object Design Guidelines 56 Common Multiprocessor Coding Mistakes 58 Survey of Propeller Debugging Tools 78 Debugging Tools Applied to a Multiprocessing Problem 86 Summary 116 Exercises 117 vii viii CONTENTS Chapter 4 Sensor Basics and Multicore Sensor Examples 119 Introducing Sensors by Their Microcontroller Interfaces 119 On/Off Sensors 122 Resistive, Capacitive, Diode, Transistor, and Other 137 Pulse and Duty Cycle Outputs 144 Frequency Output 153 Voltage Output 156 Synchronous Serial 168 Asynchronous Serial 174 Questions about Processing and Storing Sensor Data 182 Summary 187 Exercises 188 Chapter 5 Wirelessly Networking Propeller Chips 189 Introduction 189 Overview of Networking and XBee Transceivers 191 Hardware Used in This Chapter 193 Testing and Confi guring the XBee 193 Sending Data from the Propeller to the PC 204 Polling Remote Nodes 208 Using the XBee API Mode 214 A Three-Node, Tilt-Controlled Robot with Graphical Display 221 Summary 231 Exercise 232 Chapter 6 DanceBot, a Balancing Robot 235 Introduction 235 The Challenge 235 Building the DanceBot 238 Controlling the DanceBot 255 Summary 255 Exercises 255 Chapter 7 Controlling a Robot with Computer Vision 257 Introduction 257 Understanding Computer Vision 258 PropCV: A Computer Vision System for the Propeller 259 Apply Filters and Track a Bright Spot in Real Time 265 Following a Line with a Camera 270 Track a Pattern 272 State-of-the-Art Computer Vision with OpenCV 274 OpenCV and Propeller Integration 276 Summary 279 Exercises 280 Chapter 8 Using Multicore for Networking Applications 281 Introduction 281 Ethernet and Internet Protocols 281 EtherX Add-in Card for the Propeller-Powered HYDRA 287 CONTENTS ix Creating a Simple Networked Game 312 Summary 318 Exercises 318 Chapter 9 Portable Multivariable GPS Tracking and Data Logger 319 Introduction 319 Overview of the Sensors 322 Main Spin Object 344 Experiment 346 Summary 351 Exercises 352 Chapter 10 Using the Propeller as a Virtual Peripheral for Media Applications 353 Introduction 353 Overview, Setup, and Demo 354 System Architecture and Constructing the Prototype 362 Remote Procedure Call Primer 366 Virtual Peripheral Driver Overview 370 Client/Host Console Development 372 Exploring the Command Library to the Slave/Server 387 Enhancing and Adding Features to the System 389 Exploring Other Communications Protocols 389 Summary 396 Exercises 396 Chapter 11 The HVAC Green House Model 399 Introduction 399 Exploring the Problem 400 The HVAC Green House Model 402 Summary 423 Exercises 425 Chapter 12 Synthesizing Speech with the Propeller 427 Introduction 427 Using Spectrographs to “See” Speech 427 Exploring the VocalTract Object 431 Summary 441 Exercises 442 Appendix A Propeller Language Reference 443 Categorical Listing of Propeller Spin Language Elements 443 Categorical Listing of Propeller Assembly Language 449 Reserved Word List 457 Appendix B Unit Abbreviations 459 Index 463