An Introduction to C Programming for FIRST Robotics Applications

95 pages
176 views

Please download to get full document.

View again

of 95
All materials on our website are shared by users. If you have any questions about copyright issues, please report us to resolve them. We are always happy to assist you.
Share
Description
An Introduction to C Programming for FIRST Robotics Applications∗ Eugene Brooks and David Brooks FIRST Team 1280 December 28, 2005 Abstract The goal of this manual is to introduce the C programming concepts that are useful in developing a program for the robot controller in a FIRST competition robot. It is not intended to cover C programming in general as a lot of the C programming language is not used in robot programming. This manual contains both basic material, material that is specific to t
Tags
Transcript
  An Introduction to C Programmingfor FIRST Robotics Applications ∗ Eugene Brooks and David BrooksFIRST Team 1280 December 28, 2005 Abstract The goal of this manual is to introduce the C programming con-cepts that are useful in developing a program for the robot controllerin a FIRST competition robot. It is not intended to cover C program-ming in general as a lot of the C programming language is not usedin robot programming. This manual contains both basic material,material that is specific to the PIC micro-controller, and a detaileddiscussion of a number of advanced concepts that are difficult to findelsewhere. Although the intent is to be reasonably self contained,the pace is rapid and a programming novice will likely find it usefulto augment the instruction contained herein with material from thereferences. ∗ Copyrightc  2004, 2005 by Eugene D. Brooks III. Permission is granted to acquire apersonal copy, electronic or printed, by any means, and use this document for the purposeof learning C programming for FIRST robotics applications. This copyright places norestrictions on any third party who may be helping you use this document for its intendedpurpose, or may be helping you to acquire a copy of the document for its intended purpose.The author encourages such assistance. All other rights are reserved. 1  Contents 1 Introduction 52 Extensible Interactive C (EiC) 53 Hello World 74 The C Programming Language 9 4.1 The Composition of a C Program . . . . . . . . . . . . . . . . 94.2 Integer Types and their Properties . . . . . . . . . . . . . . . 104.3 Variable Definitions and Declarations . . . . . . . . . . . . . . 124.4 Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134.5 Data Hiding . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154.6 Expressions and Operators . . . . . . . . . . . . . . . . . . . . 164.6.1 Arithmetic Operators . . . . . . . . . . . . . . . . . . . 164.6.2 Conditional Operators . . . . . . . . . . . . . . . . . . 164.6.3 Logical Operators . . . . . . . . . . . . . . . . . . . . . 174.6.4 Assignment Operators, and Expressions . . . . . . . . . 174.6.5 Bitwise Operators . . . . . . . . . . . . . . . . . . . . . 184.6.6 Cast Expressions . . . . . . . . . . . . . . . . . . . . . 194.6.7 Conditional Expressions . . . . . . . . . . . . . . . . . 194.6.8 Compound Expressions . . . . . . . . . . . . . . . . . . 204.7 Conditional Code . . . . . . . . . . . . . . . . . . . . . . . . . 204.8 The Switch Statement . . . . . . . . . . . . . . . . . . . . . . 224.9 Looping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 234.10 Pointers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 254.11 Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 254.12 Character Constants . . . . . . . . . . . . . . . . . . . . . . . 264.13 Type Qualifiers . . . . . . . . . . . . . . . . . . . . . . . . . . 27 5 Storage Class, Scope and Linkage 28 5.1 Storage Class Modifiers . . . . . . . . . . . . . . . . . . . . . . 29 6 Binary 31 6.1 Positive Integers . . . . . . . . . . . . . . . . . . . . . . . . . . 316.2 Twos-Compliment Signed Arithmetic . . . . . . . . . . . . . . 336.3 Fixed-Point Arithmetic . . . . . . . . . . . . . . . . . . . . . . 372  7 Special Issues/Limits on the Robot Controller 398 Formatted Output 40 8.1 The printf() library function . . . . . . . . . . . . . . . . . . . 408.2 Printf() in the Robot Controller . . . . . . . . . . . . . . . . . 418.3 Extending Formatted Output . . . . . . . . . . . . . . . . . . 43 9 The C Pre-Processor 48 9.1 File Inclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . 489.2 Macro Expansion . . . . . . . . . . . . . . . . . . . . . . . . . 489.3 Conditional Compilation . . . . . . . . . . . . . . . . . . . . . 50 10 Classic C Programming Errors 51 10.1 if(expr) without braces . . . . . . . . . . . . . . . . . . . . . . 5110.2 The = and == operators are distinct . . . . . . . . . . . . . . 5210.3 The bitwise and logical operators are distinct . . . . . . . . . . 5210.4 Seeing the ++ trees in the forest . . . . . . . . . . . . . . . . 53 11 The Robot Packet Loop 5312 Checking Your Robot Controller 5513 Polled Wheel Counters 5814 Measuring time 5915 Controlling the Air Compressor 6116 Analog Feedback 6117 Time Integration 6418 Integrating a Rotational Rate Gyro 6619 Interrupt Programming 69 19.1 Saving and Restoring Program Context . . . . . . . . . . . . . 7119.2 Race Conditions . . . . . . . . . . . . . . . . . . . . . . . . . . 72 20 Interrupt Based Wheel Counter and Gyro 77 3  21 State Machine Programming 83Glossary 85References 94Index 95 List of Tables 1 Integer types provided by EiC . . . . . . . . . . . . . . . . . . 102 Integer Types Provided by the C18 C Compiler . . . . . . . . 113 Arithmetic Operators in C . . . . . . . . . . . . . . . . . . . . 164 Conditional Operators in C . . . . . . . . . . . . . . . . . . . 175 Logical Operators in C . . . . . . . . . . . . . . . . . . . . . . 176 Assignment Operators in C . . . . . . . . . . . . . . . . . . . 187 Bitwise operators in C . . . . . . . . . . . . . . . . . . . . . . 194
Related Search
We Need Your Support
Thank you for visiting our website and your interest in our free products and services. We are nonprofit website to share and download documents. To the running of this website, we need your help to support us.

Thanks to everyone for your continued support.

No, Thanks
SAVE OUR EARTH

We need your sign to support Project to invent "SMART AND CONTROLLABLE REFLECTIVE BALLOONS" to cover the Sun and Save Our Earth.

More details...

Sign Now!

We are very appreciated for your Prompt Action!

x