Docsity

Programming Robots with ROBOTC: Understanding Firmware, Statements, and Control Structures, Slides of Robotics

Partial preview of the text

Download Programming Robots with ROBOTC: Understanding Firmware, Statements, and Control Structures and more Slides Robotics in PDF only on Docsity!

RobotC Programming for LEGO

Mindstorms NXT

Loading Firmware

  • Robots require “Operating Systems” to run

properly

  • ROBOTC has it’s own Operating System, called

Firmware.

  • The firmware must be loaded onto the robot

controller before.

Programming in

RobotC for practical

robotic applications

ROBOTC Fundamentals

  • Every program starts with a “task main”
    • Exception: Competition Template programming
      • The “task main” is taken care of you behind the scenes.

task main()

Example RobotC Program - BEHAVIOR

task main() {

SensorType[S4] = sensorSONAR;

forward();

while(true) {

if (SensorValue[S4] < 25)

spin();

else forward();

} Explain the role of behaviors in robot programming

Syntax and statements

  • Programs consist of a number of statements.
  • Statements tell the robot controller what to do.
  • Motor[port1] = 127; is a statement
  • All statements end with semicolons “ ; ”
  • Proper syntax (semicolons, brackets, etc) is very

important.

Comments in ROBOTC

  • Comments are very important for memory

and understanding

  • // - Single Line Comment
  • /*
  • Multiple line comments
  • */

Code conventions. Lower Case and Uppercase.

  1. Text written as part of a program is called code.
  2. You type code just like you type normal text.
  3. Keep in mind that capitalization is important to the computer.
  4. Replacing a lowercase letter with a capital letter (or a capital letter with a lowercase letter) will cause the robot to become confused.

Statements

  1. Statements are instructions for the robot.
  2. The most basic kind of statement in ROBOTC simply gives a command to the robot.
  3. The motor[port3] = 127; statement in the sample program you downloaded is a simple statement that gives a command.
  4. It instructs the motor plugged into Motor Port 3 to turn on at full power.

Order the Statements

  1. Statements are run in order as quickly as the robot is able to

reach them.

  1. Running this program on the robot turns the motor on, then

waits for 3000 milliseconds (3 seconds) with the motor still running, and then ends.

ROBOTC Rules: the role of spacing

  1. So why ARE they on separate lines?
    • For the programmer.
  2. Programming languages are designed for humans and machines to communicate.
  3. Using spaces, tabs, and lines helps human programmers read the code more easily.
  4. Making good use of spacing in your program is a very good habit for your own sake.

Punctuation! Semicolons!

  1. But what about ROBOTC?
  2. How DID it know where one statement ended and the other began?
  3. It knew because of the semicolon ( ; ) at the end of each line.
  4. Every statement ends with a semicolon. It’s like the period at the end of a sentence.

Punctuation Pairs: Square Brackets

  1. Different commands make use of different kinds of paired punctuation.
  2. The motor command uses square brackets and the wait1Msec command uses parentheses.
  3. 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.

Control Structures

1. Simple statements do the work in ROBOTC, but control structures do the thinking.

  1. 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.
  2. One important control structure is task main.
  3. Every ROBOTC program includes a special section called task main.
  4. This control structure determines which code the robot will run as part of the main program. Docsity.com