Programming in ROBOTC ROBOTC Rules

Similar documents
Download Program. Setup ROBOTC. task main() { motor[motorc] = 100; wait1msec(3000);

Getting Started in RobotC. // Comment task main() motor[] {} wait1msec() ; = Header Code Compile Download Run

Getting Started in RobotC. // Comment task main() motor[] {} wait1msec() ; = Header Code Compile Download Run

ROBOTC Basic Programming

The C++ Language. Arizona State University 1

Java Programming Fundamentals - Day Instructor: Jason Yoon Website:

Activity Robot Behaviors and Writing Pseudocode

TOPIC 2 INTRODUCTION TO JAVA AND DR JAVA

Chapter 2 Author Notes

Variables and Functions. ROBOTC Software

Lecture 05 I/O statements Printf, Scanf Simple statements, Compound statements

Chapter 2: Programming Concepts

Lesson 1A - First Java Program HELLO WORLD With DEBUGGING examples. By John B. Owen All rights reserved 2011, revised 2015

Taking Apart Numbers and Shapes

Variables and. What Really Happens When You Run hello_world.py

Introduction to C Programming

Perl Basics. Structure, Style, and Documentation

LESSON 3. In this lesson you will learn about the conditional and looping constructs that allow you to control the flow of a PHP script.

Troubleshooting ROBOTC with Cortex

Java Style Guide. 1.0 General. 2.0 Visual Layout. Dr Caffeine

Programming for Engineers Introduction to C

Office Wo Office W r o d r 2007 Revi i ng and R d Refifini ng a D Document

COMP 110 Project 1 Programming Project Warm-Up Exercise

The name of our class will be Yo. Type that in where it says Class Name. Don t hit the OK button yet.

Part II Composition of Functions

2 Sets. 2.1 Notation. last edited January 26, 2016

MLA Format. Example and Formatting Instructions. Prepared by the Clarendon College English Department and Computer Science Department

Autonomy/Encoders Forward for Distance

CS 177 Recitation. Week 1 Intro to Java

Full file at C How to Program, 6/e Multiple Choice Test Bank

Introduction to Programming Style

Learning to Program with Haiku

Computer Programming : C++

Chapter 17. Fundamental Concepts Expressed in JavaScript

Lesson 1: Writing Your First JavaScript

2.2 Order of Operations

HTML/CSS Lesson Plans

LESSON 1. A C program is constructed as a sequence of characters. Among the characters that can be used in a program are:

Session 2: Skips and Prefills

Introduction to Java. Java Programs Classes, Methods, and Statements Comments Strings Escape Sequences Identifiers Keywords

Lab # 2. For today s lab:

Welcome to Python 3. Some history

IT 374 C# and Applications/ IT695 C# Data Structures

VARIABLES. Aim Understanding how computer programs store values, and how they are accessed and used in computer programs.

Advanced Handle Definition

2.1. Chapter 2: Parts of a C++ Program. Parts of a C++ Program. Introduction to C++ Parts of a C++ Program

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 3: SEP. 13TH INSTRUCTOR: JIAYIN WANG

Lecture (01) Getting started. Dr. Ahmed ElShafee

Movement using Shaft Encoders

Functional Programming in Haskell Prof. Madhavan Mukund and S. P. Suresh Chennai Mathematical Institute

The Java Language Rules And Tools 3

C++ Style Guide. 1.0 General. 2.0 Visual Layout. 3.0 Indentation and Whitespace

Fundamentals of Programming. Lecture 3: Introduction to C Programming

Regular Expressions Explained

Fundamentals of Programming Session 4

RobotC. Remote Control

CS125 : Introduction to Computer Science. Lecture Notes #4 Type Checking, Input/Output, and Programming Style

Chapter 1 Operations With Numbers

Boolean Expressions. Is Equal and Is Not Equal

Boolean Expressions. Is Equal and Is Not Equal

MODULE III: NAVIGATING AND FORMULAS

Avoiding Run-on Sentences, Comma Splices, and Fragments, ; Getting Your Punctuation Right!

Lecture Programming in C++ PART 1. By Assistant Professor Dr. Ali Kattan

12/22/11. Java How to Program, 9/e. Help you get started with Eclipse and NetBeans integrated development environments.

Colin Harman and Jonathan Kim

INTRODUCTION 1 AND REVIEW

Introduction to Scientific Typesetting Lesson 1: Getting Started

Learning to Program with Haiku

EE 382 Style Guide. March 2, 2018

Crayon (.cry) Language Reference Manual. Naman Agrawal (na2603) Vaidehi Dalmia (vd2302) Ganesh Ravichandran (gr2483) David Smart (ds3361)

Defining Program Syntax. Chapter Two Modern Programming Languages, 2nd ed. 1

Computer Hardware. Java Software Solutions Lewis & Loftus. Key Hardware Components 12/17/2013

Grade 6 Math Circles November 6 & Relations, Functions, and Morphisms

c) Comments do not cause any machine language object code to be generated. d) Lengthy comments can cause poor execution-time performance.

Basics of Java Programming

Activity Basic Inputs Programming - VEX Clawbot

Computer Programming & Problem Solving ( CPPS ) Turbo C Programming For The PC (Revised Edition ) By Robert Lafore

Control Structures. Code can be purely arithmetic assignments. At some point we will need some kind of control or decision making process to occur

Basic Syntax - First Program 1

T H E I N T E R A C T I V E S H E L L

Sets. Sets. Examples. 5 2 {2, 3, 5} 2, 3 2 {2, 3, 5} 1 /2 {2, 3, 5}

6.096 Introduction to C++

BEGINNER PHP Table of Contents

Shut the Box. By the time you are done with this activity, you and your team should be able to:

CHAPTER 2.1 CONTROL STRUCTURES (SELECTION) Dr. Shady Yehia Elmashad

Working with JavaScript

PROGRAMMING STYLE. Fundamentals of Computer Science I

Assignment Marking Criteria

Programming Logic and Design Sixth Edition Chapter 2 Working with Data, Creating Modules, and Designing High-Quality Programs

Overview of the Ruby Language. By Ron Haley

Working with Questions in MathXL for School

Baltimore Health and Mental Health Study Training Manual Page II - 1

Lesson 1 - Getting Started

Programming Fundamentals and Python

HANDOUT: COMPUTER PARTS

Advanced Activities - Information and Ideas

The simplest way to evaluate the expression is simply to start at the left and work your way across, keeping track of the total as you go:

Java Bytecode (binary file)

For Teachers Engineering Design in Oregon Science Classrooms Page 1 of 6. EDOSC Style Guide. Subtitle

Chapter 2, Part I Introduction to C Programming

Transcription:

Programming in ROBOTC ROBOTC Rules In this lesson, you will learn the basic rules for writing ROBOTC programs. ROBOTC is a text-based programming language Commands to the robot are first written as text on the screen. They are then processed by the ROBOTC compiler into a machine language file that the robot can understand. Finally, they are loaded onto the robot, where they can be run. SetPowerLevel(0); Program Code Commands to the robot written as text. Text written as part of a program is called code. You type code just like you type normal text. Keep in mind that capitalization is important to the computer. Replacing a lowercase letter with a capital letter (or a capital letter with a lowercase letter) will cause the robot to become confused. Task main() Wait(); Capitalization Capitalization (paying attention to UPPERCASE vs. lowercase) is important in ROBOTC. If you capitalize the T in task, ROBOTC no longer recognizes this command. As you type, ROBOTC will try to help you out by coloring the words it recognizes. If a word appears in a different color, it means ROBOTC recognizes it as an important word in the programming language. SetPowerLevel(0); Code coloring ROBOTC automatically colors key words that it recognizes. Compare this correctly-capitalized task command with the incorrectly-capitalized version in the previous example. The correct one is recognized as a command and turns blue. ROBOTC Programming

Now, we will look at some of the important parts of the program code itself. Statements are instructions for the robot. The most basic kind of statement in ROBOTC simply gives a command to the robot. The DriveForward(); statement in the sample program you downloaded is a simple statement that gives a command. It instructs the motor plugged into Motor Port to turn on at full power. DriveForward(); StopOnLine(); Simple statement A straightforward command to the robot. This statement tells the robot to turn on the motor attached to motor port at full power. Simple statement () This is also a simple statement. It tells the robot to wait for 000 milliseconds ( seconds). Statements are run in order as quickly as the robot is able to reach them. Running this program on the robot turns the motor on, then waits for 000 milliseconds ( seconds) with the motor still running, and then ends. v st nd Wait(); End Sequence Statements run in English reading order (left-to-right, top-to-bottom). As soon as a command is complete, the next one runs. These two statements cause the motors to turn on (st command). The robot then immediately begins a three second wait (nd command) while the motors remain on. End When the program runs out of statements and reaches the symbol in task main, all motors stop and the program ends. ROBOTC Programming

How did ROBOTC know that motosr[port]= and waitmsec[000] were two separate commands. Was it because they appeared on two different lines? No. Spaces and line breaks in ROBOTC are only used to separate words from each other in multi-word commands. Spaces, tabs, and lines don t affect the way a program is interpreted by the machine. SetPowerLevel(0); Whitespace Spaces, tabs, and line breaks are generally unimportant to ROBOTC and the robot. They are sometimes needed to separate words in multi-word commands, but are otherwise ignored by the machine. So why ARE they on separate lines? For the programmer. Programming languages are designed for humans and machines to communicate. Using spaces, tabs, and lines helps human programmers read the code more easily. Making good use of spacing in your program is a very good habit for your own sake. motor[port] =;waitmsec(000); No Whitespace To ROBOTC, this program is the same as the last one. To the human programmer, however, this is close to gibberish. Whitespace is used to make programs readable to humans. But what about ROBOTC? How DID it know where one statement ended and the other began? It knew because of the semicolon (;) at the end of each line. Every statement ends with a semicolon. It s like the period at the end of a sentence. motor[port] = ; waitmsec(000); Semicolons Like periods in an English sentence, semicolons mark the end of every ROBOTC statement. Checkpoint Statements are commands for the robot. Each statement ends in a semicolon so that ROBOTC can identify it. Each statement is also usually written on its own line to make it easier for humans to read. Statements are run in reading order, from left to right and top to bottom. Each statement is run as soon as the previous one is complete. When there are no more statements, the program ends. ROBOTC Programming

ROBOTC uses far more punctuation than English. Punctuation in programming languages is generally used to separate important areas of code from each other. Most ROBOTC punctuation comes in pairs. Punctuation pairs, like the parentheses and square brackets in these two statements, are used to mark off special areas of code. Every punctuation pair consists of an opening punctuation mark and a closing punctuation mark. The punctuation pair designates the area between them as having special meaning to the command that they are part of. motor[port] = ; waitmsec(000); motor[port] = ; waitmsec(000); Punctuation pair: Square brackets [ ] The code written between the square brackets of the motor command indicates which motor the command should use. In this case, it is the motor on port. Punctuation pair: Parentheses ( ) The code written between the parentheses of the waitmsec command tell it how many milliseconds to wait before starting a new command. In this case, it waits 000 milliseconds, or three seconds. Checkpoint Paired punctuation marks (such as square brackets and parentheses) are always used together. They surround specific important parts of a statement to set them apart. Different commands make use of different kinds of paired punctuation. The motor command uses square brackets and the waitmsec command uses parentheses. This is just the way the commands are set up. You will have to remember to use the right punctuation with the right commands or plan. ROBOTC Programming

Simple statements do the work in ROBOTC, but control structures do the thinking. Control structures (or control statements) are pieces of code that control the flow of the program s commands, rather than issue direct orders to the robot. Simple statements can only run one after another in order. However, control structures allow the program to choose the order in which statements are run. For instance, a control structure may tell the program to choose between two different groups of statements and only run one of them. Sometimes, control structures repeat a group of statements over and over again. One important control structure is task main. Every ROBOTC program includes a special section called task main. This control structure determines which code the robot will run as part of the main program. SetPowerLevel(0); Control structure: task main The control structure task main directs the program to the main body of the code. When you click the Start button in ROBOTC or turn on the robot, the program immediately goes to task main and runs the code it finds there. The left and right curly braces belong to the task main structure. They surround the commands which will be run in the program. while(sensorvalue(touchsensor) == 0) DriveForward(); Control structure: while loop The while loop repeats the code between its curly braces as long as certain conditions are met. Normally, statements run only once. But with a while loop, they can be told to repeat over and over for as long as you want! Checkpoint Control structures like task main determine which lines of code are run and specify when they are run. They control the order in which commands are executed in your program. Control structures enable your robot to make decisions and respond intelligently to its environment. ROBOTC Programming

Programming languages are meant to be readable by both humans and machines. Sometimes, a programmer needs to leave a note for other human readers to help them understand what the code is doing. ROBOTC allows comments to be made for this purpose. Comments are text that the program ignores. A comment can contain notes, messages, and symbols that may help a human, but would be meaningless to the robot. ROBOTC simply skips over them. Comments appear in green in ROBOTC. 8 9 0 // Motor port forward with 00% power /* Power 0 and go forward Do this for 0 inches. */ SetPowerLevel(0); DriveForward();StopAfterDistance(0); Comments: // Single line Any section of text that follows a // (two forward slash characters) on a line is considered to be a comment. Any text to the left of the // is treated as normal code. Comments: /* Any length */ A comment can be created in ROBOTC using another type of paired punctuation, which starts with /* and ends with */ This type of comment can span multiple lines, so be sure to include both the opening and closing marks! End of Section What you have just seen are some of the primary features of the ROBOTC language. Code is entered as text, which builds statements. Statements are used to issue commands to the robots. Control structures decide which statements to run at what times. Punctuation, both single like semicolons and paired like parentheses, is used to set apart important parts of commands. A number of features in ROBOTC code are designed to help the human, rather than the robot. Comments let programmers leave notes for themselves and others. Whitespace like tabs and spaces helps to keep your code organized and readable. ROBOTC Programming