School of Computer Science and Engineering The Uno cial Guide to Lego Mindstorms Robots

Size: px
Start display at page:

Download "School of Computer Science and Engineering The Uno cial Guide to Lego Mindstorms Robots"

Transcription

1 Acknowledgments Programming robots with NQC This presentation borrows material from two sources: A tutorial on NQC by Mark Overmars, entitled Programming Lego Robots using NQC, Eric Martin which is available at A book by Jonathan Knudsen, published by O'Reilly, entitled School of Computer Science and Engineering The Uno cial Guide to Lego Mindstorms Robots University of New South Wales Sydney, Australia with building instructions available at ENGG1000 (2007) Programming robots with NQC CSE, UNSW 1 / 75 Hank the bumper car ENGG1000 (2007) Programming robots with NQC CSE, UNSW 2 / 75 Typing, compiling and running programs We will test our rst programs and meet our rst challenges with a Launch the RCX command center. robot called Hank. Type in, compile, and run the following program, after predicting what Hank is a bumper car equipped with two touch sensors. it is OnFwd(OUT_A); OnFwd(OUT_C); Wait(400); OnRev(OUT_A+OUT_C); Wait(400); Build Hank following the instructions on your CD. The layout of the program, and in particular, the indentation and the position of the curly braces, does not matter for the program to compile, but it is essential to achieve maximum readability. ENGG1000 (2007) Programming robots with NQC CSE, UNSW 3 / 75 ENGG1000 (2007) Programming robots with NQC CSE, UNSW 4 / 75

2 Tasks and statements A few statements (1) Programs in NQC consist of tasks. Our rst program has just one task, named main. Each program has a main task, possibly with other tasks. A program starts by executing its main task. A task consists of a number of commands, also called statements. Tasks in the body of a task are surrounded by curly braces. Each statement ends in a semicolon. So a task looks in general as follows: task task_name() statement1; statement2;... The body of the main task of our rst program consists of 6 statements: OnFwd(OUT_A) Tells the robot to start output A. The motor connected to output A will spin forwards. By default it will spin with maximal speed. OnFwd(OUT_C) Same statement, but for output C. After both statements are executed, the robot is moving forwards at full speed. Wait(400) Tells the program to wait for 4 seconds. The argument to Wait, i.e., the number between the parentheses, gives the number of ticks. Each tick is 1/100 of a second. So for 4 seconds, the program does do nothing and the robot keeps moving. ENGG1000 (2007) Programming robots with NQC CSE, UNSW 5 / 75 ENGG1000 (2007) Programming robots with NQC CSE, UNSW 6 / 75 A few statements (2) Changing the speed OnRev(OUT_A+OUT_C) Tells the robot to spin both motors in reverse direction. Note how OUT_A+OUT_C allows to combine two statements into one. Wait(400) Tells the program to wait for 4 seconds again. O(OUT_A+OUT_C) Tells the robot to switch both motors o. So the whole program moves both motors forwards for 4 seconds, then backwards for 4 seconds, and nally switches them o. To make the robot move more slowly, we can use the command SetPower(). This command takes 2 arguments. The power is a number between 0 and 7; 7 is the fastest, 0 the slowest (but the robot will still move). Here is a new version of the rst program in which the robot does not move as fast. SetPower(OUT_A+OUT_C, 2); Wait(400); OnRev(OUT_A+OUT_C); Wait(400); ENGG1000 (2007) Programming robots with NQC CSE, UNSW 7 / 75 ENGG1000 (2007) Programming robots with NQC CSE, UNSW 8 / 75

3 Making turns Constants #define MOVE_TIME 100 #define TURN_TIME 85 Wait(MOVE_TIME); The rst two lines of the previous program dene two constants. Constants can be used throughout the program. Dening constants has two purposes: it makes the program more readable; it makes it easier to change the values. You might have to change the value of TURN_TIME to achieve a precise 90 degree turn. ENGG1000 (2007) Programming robots with NQC CSE, UNSW 9 / 75 ENGG1000 (2007) Programming robots with NQC CSE, UNSW 10 / 75 Repeating commands The repeat command (1) #define MOVE_TIME 100 #define TURN_TIME 85 repeat(4) Wait(MOVE_TIME); The number provided as an argument to repeat indicates how many times the statements in the body of the repeat loop must be executed. The robot should move forward and make turns four times, hence should drive in a square. Note that the statements that make up the body of the repeat statement are enclosed between curly braces, just like the statements in the body of a task. Note the layout of the programthe dierent levels of indenting and the position of the curly bracesthat maximises readability. ENGG1000 (2007) Programming robots with NQC CSE, UNSW 11 / 75 ENGG1000 (2007) Programming robots with NQC CSE, UNSW 12 / 75

4 The repeat command (2) Comments (1) Repeat commands can be nested. The next program makes the robot run 3 times in a square. #define MOVE_TIME 100 #define TURN_TIME 85 repeat(3) repeat(4) Wait(MOVE_TIME); Again, note how the dierent levels of indenting and the position of the curly braces maximise readability. ENGG1000 (2007) Programming robots with NQC CSE, UNSW 13 / 75 Comments make a program even more readable. There are two kinds of comments: // What comes to the right of // on the same line is ignored. /*... */ Everything between /* and */ is ignored. This kind of comment can span multiple lines. Comments should be informative, but not redundant. For instance, there is no point to comment the statement with // Turn motors off Indeed, the statement is clear enough by itself. As an example, the previous program can be enhanced with comments as shown next. ENGG1000 (2007) Programming robots with NQC CSE, UNSW 14 / 75 Comments (2) Using variables /* Driving 3 times in a square by Mark Overmars, modified by Eric Martin */ #define MOVE_TIME 100 // Time for a straight move #define TURN_TIME 85 // Time for turning 90 degrees repeat(3) repeat(4) Wait(MOVE_TIME); #define TURN_TIME 85 int move_time; move_time = 20; repeat(50) Wait(move_time); move_time += 5; ENGG1000 (2007) Programming robots with NQC CSE, UNSW 15 / 75 ENGG1000 (2007) Programming robots with NQC CSE, UNSW 16 / 75

5 Dening and initialising variables Modifying the value of variables (1) A variable is dened by typing the keyword int, followed by the name of the variable. int means integer. A name is a string of (upper case or lower case) letters, underscore or digits that does not start with a digit. A usual convention is to use no upper case letter for variable names, and no lower case letters for constants. Still this is not a requirement. To maximise readability, variables should be given meaningful names. The statement move_time = 20; initialises of move_time to 20. Alternatively, move_time could have been dened and initialised at the same time with the statement move_time = 20; Also, many variables can be dened (and possibly initialised) in one statement: they just have to be separated by commas. The value of move_time is changed 50 times, every time the repeat loop is executed. When the repeat loop begins execution, the value of move_time is equal to 20. At the end of each execution of the repeat loop, the value is incremented by 5. So the robot will move forward for 20 ticks (how much is that in seconds?), turn right, move forward for 25 ticks, turn right, move forward for 30 ticks, turn right, etc. For how long will the robot move forward the last time the repeat loop is executed? ENGG1000 (2007) Programming robots with NQC CSE, UNSW 17 / 75 ENGG1000 (2007) Programming robots with NQC CSE, UNSW 18 / 75 Modifying the value of variables (2) Unpredictable behaviour Besides adding values to a variable, we can also multiply, subtract and divide a variable with a number using, respectively, *=, -=, and /=. Division rounds the result to the nearest integer. For instance, 8 divided by 3 yields 2. We can also add one variable to the other, and write down more complicated expressions. For instance: int a; int b, c; a = 10; b = 20 * 5; c = b; c /= a; c -= 5; a = 10 * (c + 3); // What is the value of a now? int move_time, turn_time; while(true) move_time = Random(60); turn_time = Random(40); Wait(move_time); OnRev(OUT_A); Wait(turn_time); ENGG1000 (2007) Programming robots with NQC CSE, UNSW 19 / 75 ENGG1000 (2007) Programming robots with NQC CSE, UNSW 20 / 75

6 Random numbers and while loops Making decisions We can generate random numbers to make the robot react in an unpredictable way. The previous program denes two variables, and then assigns random numbers to them. Random(60) generates a random number between 0 and 60 (included), while Random(40) generates a random number between 0 and 40 (included). The previous program also introduces a construct that denes a second kind of loop (besides repeat), using the while keyword. The while statement keeps executing the statements in its body as long as the condition provided as an argument is true. The keyword true always evaluates to true, so the statements in the body of the loop are repeated forever, creating an innite loop. #define MOVE_TIME 100 #define TURN_TIME 85 while(true) Wait(MOVE_TIME); if (Random(1) == 1) else OnRev(OUT_A); ENGG1000 (2007) Programming robots with NQC CSE, UNSW 21 / 75 ENGG1000 (2007) Programming robots with NQC CSE, UNSW 22 / 75 The if/else statement Comparisons The if/else statement is used to have one part of the program execute in particular situations, and another part of the program execute in the other situations. The previous program makes the robot turn left or right after driving along a straight line. The decision to turn left or right is made at random. The test Random(1) == 1 could also be simplied to Random(1). Indeed, the latter evaluates to 1 i the former evaluates to true, and evaluating to a nonnull value is equivalent to evaluating to true. The else statement is optional. If the condition of the if test evaluates to false (i.e., 0), then the body of the if statement is skipped and execution resumes afterwards. Distinguish between a == b, which tests whether a and b have equal values, evaluates to true if that is the case and to false otherwise, and a = b, which assigns the value of b to a. To compare values, you can use other comparison operators: a!= b evaluates to true i a and b have distinct values; a < b evaluates to true i the value of a is strictly smaller than the value of b; a <= b evaluates to true i the value of a is smaller than or equal to the value of b; a > b evaluates to true i the value of a is strictly greater than the value of b; a >= b evaluates to true i the value of a is greater than or equal to the value of b. ENGG1000 (2007) Programming robots with NQC CSE, UNSW 23 / 75 ENGG1000 (2007) Programming robots with NQC CSE, UNSW 24 / 75

7 Boolean operators Using touch sensors Simple comparisons can be combined into more complex conditions using one or more Boolean operator. && whose meaning is and; whose meaning is or;! whose meaning is not. Here are some examples of conditions. true always true false never true (a >= 5) && (a <= 10) true i a lies between 5 and 10 (a == 10) (b == 10) true i a or b equals 10!((a > 5) && (a < 9)) true i a is distinct to 6, 7 or 8 Parentheses are used to give priority to some operators over others. SetSensor(SENSOR_1, SENSOR_TOUCH); until (SENSOR_1 == 1); ENGG1000 (2007) Programming robots with NQC CSE, UNSW 25 / 75 ENGG1000 (2007) Programming robots with NQC CSE, UNSW 26 / 75 Statements dealing with sensors First programming exercise The rst statement in the previous program tells the robot what type of sensor is connected to which port. SENSOR_1 is the number of the input to which the sensor is connected. The other two sensor inputs are called SENSOR_2 and SENSOR_3. Of course, SENSOR_TOUCH indicates that a touch sensor is being used; for a light sensor we would write SENSOR_LIGHT. The third statement waits until the condition SENSOR_1 == 1which could be simplied to SENSOR_1evaluates to true, indicating that the the sensor is being pressed. As long as the touch sensor connected to port 1 is not pressed, SENSOR_1 evaluates to 0 (i.e., to false). Write an NQC program for Hank to avoid obstacles. Whenever the robot hits an object, it should move back a bit, make a turn, and then continue. The robot should turn left if the right bumper has rst hit the obstacle, and right otherwise, as shown in the movie below. ENGG1000 (2007) Programming robots with NQC CSE, UNSW 27 / 75 ENGG1000 (2007) Programming robots with NQC CSE, UNSW 28 / 75

8 Solution (1) Solution (2) #define BACK_TIME 50 #define TURN_TIME 80 SetSensor(SENSOR_1, SENSOR_TOUCH); SetSensor(SENSOR_3, SENSOR_TOUCH); while (true) if (SENSOR_1) OnRev(OUT_A+OUT_C); Wait(BACK_TIME); OnFwd(OUT_A); OnFwd(OUT_C); if (SENSOR_3 ) OnRev(OUT_A+OUT_C); Wait(BACK_TIME); OnFwd(OUT_C); OnFwd(OUT_A); ENGG1000 (2007) Programming robots with NQC CSE, UNSW 29 / 75 ENGG1000 (2007) Programming robots with NQC CSE, UNSW 30 / 75 Programs with many tasks (1) SetSensor(SENSOR_1, SENSOR_TOUCH); start check_sensors; start move_square; task move_square() while (true) Wait(100); Wait(85); Programs with many tasks (2) task check_sensors() while (true) if (SENSOR_1 == 1) stop move_square; OnRev(OUT_A+OUT_C); Wait(50); OnFwd(OUT_A); Wait(85); start move_square; ENGG1000 (2007) Programming robots with NQC CSE, UNSW 31 / 75 ENGG1000 (2007) Programming robots with NQC CSE, UNSW 32 / 75

9 Starting and stopping tasks Second programming exercise An NQC program consists of at most 10 tasks. Many tasks can run simultaneously. Any task some_task except main will only be executed when a running task asks for some_task to start execution using the start command. Imagine that Hank is a living creature which is attacked by a predator. If one of Hank's bumpers is hit again soon after Hank has been hit by the predator, then it is better to let Hank back up further: instead of nishing a sequence of actions triggered by a hit, it is better to start that kind of sequence again. Hence Hank's behaviour is better in the right movie than in the left movie below. Modify your program to improve Hank's behaviour. A running task can also stop another running task by using the stop command. A task that has been stopped can be restarted again, but it will start from the beginning; not from the place where it was stopped. ENGG1000 (2007) Programming robots with NQC CSE, UNSW 33 / 75 ENGG1000 (2007) Programming robots with NQC CSE, UNSW 34 / 75 Solution An incorrect solution (1) #define BACK_TIME 50 #define TURN_TIME 80 The following programs are very similar, and at rst sight might both be thought to be valid. The time during which is a touch sensor returns a value of 1 is much larger than the execution time of a command. This is a problem with the rst program, but not with the second one. Remove the comments in the programs to make use of the variable count. This variable can have its value displayed to indicate how many times the test that one of the bumpers has hit an obstacle succeeds. // int count = 0; SetSensor(SENSOR_1, SENSOR_TOUCH); SetSensor(SENSOR_3, SENSOR_TOUCH); while (true) if (SENSOR_1 SENSOR_3) // count++; stop back_up; start back_up; ENGG1000 (2007) Programming robots with NQC CSE, UNSW 35 / 75 ENGG1000 (2007) Programming robots with NQC CSE, UNSW 36 / 75

10 An incorrect solution (2) A correct solution (1) task back_up() if (SENSOR_1) OnRev(OUT_A+OUT_C); Wait(BACK_TIME); OnFwd(OUT_A); OnFwd(OUT_C); if (SENSOR_3) OnRev(OUT_A+OUT_C); Wait(BACK_TIME); OnFwd(OUT_C); OnFwd(OUT_A); #define BACK_TIME 50 #define TURN_TIME 80 // int count = 0; SetSensor(SENSOR_1, SENSOR_TOUCH); SetSensor(SENSOR_3, SENSOR_TOUCH); ENGG1000 (2007) Programming robots with NQC CSE, UNSW 37 / 75 ENGG1000 (2007) Programming robots with NQC CSE, UNSW 38 / 75 A correct solution (2) A correct solution (3) while (true) if (SENSOR_1) // count++; stop back_up_1; stop back_up_3; start back_up_1; if (SENSOR_3) // count++; stop back_up_1; stop back_up_3; start back_up_3; task back_up_1() OnRev(OUT_A+OUT_C); Wait(BACK_TIME); OnFwd(OUT_A); OnFwd(OUT_C); task back_up_3() OnRev(OUT_A+OUT_C); Wait(BACK_TIME); OnFwd(OUT_C); OnFwd(OUT_A); ENGG1000 (2007) Programming robots with NQC CSE, UNSW 39 / 75 ENGG1000 (2007) Programming robots with NQC CSE, UNSW 40 / 75

11 Semaphores semaphore.nqc (1) When you stop and restart a task, it starts at the beginning. OK for small tasks, but we really should stop and resume at the same place in the task. One way to assure that happens: use a semaphore Semaphore : a global variable accessed by both tasks Semaphore = 0 means no task is driving motors Semaphore = 1 means a task is driving motors When a task wants to use the motors, execute following code: until (semaphore == 0); semaphore = 1; // Use the motors semaphore = 0; int sem; task main() sem = 0; SetSensor(SENSOR_1,SENSOR_TOUCH); start check_sensors; start move_square; ENGG1000 (2007) Programming robots with NQC CSE, UNSW 41 / 75 ENGG1000 (2007) Programming robots with NQC CSE, UNSW 42 / 75 semaphore.nqc (2) semaphore.nqc (3) task move_square() while (true) until (sem == 0); sem = 1; sem = 0; Wait(100); until (sem == 0); sem = 1; sem = 0; Wait(85); task check_sensors() while (true) if (SENSOR_1 == 1) until (sem == 0); sem = 1; OnRev(OUT_A+OUT_C); Wait(50); OnFwd(OUT_A); Wait(85); sem = 0; ENGG1000 (2007) Programming robots with NQC CSE, UNSW 43 / 75 ENGG1000 (2007) Programming robots with NQC CSE, UNSW 44 / 75

12 Access Control Access Control Resources Setting task priorities for accessing resources Automates and generalizes the idea of semaphores Allows a task to request ownership of a resource (Motor, speaker, or a user-dened resource) acquire(list of resources) catch body // If resource is not owned by a higher // priority task the task gets the resource // and the body executes ; // If resource is owned by a higher priority task // it is taken away, body doesn't execute, & catch // handler executes Motors: ACQUIRE_OUT_A Speaker: ACQUIRE_SOUND User-dened resources: ACQUIRE_USER_1 When ownership of motor is lost, default action is to stop motor When ownership of speaker is lost, sound is turned o No default action for user-dened resource SetPriority(priority_level) sets a priority from 0 to 255 lower levels are higher priorities ENGG1000 (2007) Programming robots with NQC CSE, UNSW 45 / 75 ENGG1000 (2007) Programming robots with NQC CSE, UNSW 46 / 75 acquire.nqc (1) acquire.nqc (2) task main() SetSensor(SENSOR_1,SENSOR_TOUCH); start check_sensors; start move_square; task move_square() SetPriority(2); while (true) acquire(acquire_user_1) Wait(100); Wait(85); ENGG1000 (2007) Programming robots with NQC CSE, UNSW 47 / 75 task check_sensors() SetPriority(1); while (true) if (SENSOR_1 == 1) acquire(acquire_user_1) OnRev(OUT_A+OUT_C); Wait(50); OnFwd(OUT_A); Wait(85); ENGG1000 (2007) Programming robots with NQC CSE, UNSW 48 / 75

13 Event Monitoring eventmonitor.nqc 1 More ecient than polling sensors 2 16 types of events can be monitored and responses programmed (See NQC documentation for types) 3 associate event numbers with event sources and types, e.g., SetEvent(1, SENSOR_1, EVENT_TYPE_PRESSED); SetEvent(1, SENSOR_1, EVENT_TYPE_RELEASED); 4 Monitor those events monitor (EVENT_MASK(1) + EVENT_MASK(2)) Normal code when events have not occurred catch (EVENT_MASK(1)) event 1 handler code catch (EVENT_MASK(2)) event 2 handler code ENGG1000 (2007) Programming robots with NQC CSE, UNSW 49 / 75 task main() SetSensor(SENSOR_1, SENSOR_TOUCH); SetEvent(0, SENSOR_1, EVENT_TYPE_PRESSED); while(true) monitor(event_mask(0)) while(true); catch PlaySound(SOUND_CLICK); ENGG1000 (2007) Programming robots with NQC CSE, UNSW 50 / 75 The switch statement (1) The switch statement (2) The general syntax of the switch statement is: switch (expression) case constant_1: statements_1 break;... case constant_n: statements_n break; // what follows is optional default: statements_d break; A switch statement can be used to execute one of several dierent blocks of code, depending on the value of an expression. Each block of code is preceded by a case label, and ends with a break statement. Each case label must be a constant. The switch statement evaluates the expression, then looks for the rst case label that matches, if one exists. It then executes the of statements that follow the matching case. A optional default label may also be at the end: it will match any value that could not be matched by any of preceding case labels. ENGG1000 (2007) Programming robots with NQC CSE, UNSW 51 / 75 ENGG1000 (2007) Programming robots with NQC CSE, UNSW 52 / 75

14 Playing music (1) Playing music (2) The following program uses the switch statement to play one of 3 notes, depending on the value of a randomly generated number equal to either 1, 2 or 3. #define WHOLE_NOTE 100 #define HALF_NOTE WHOLE_NOTE / 2 #define QUARTER_NOTE WHOLE_NOTE / 4 #define SILENCE 20 int note; repeat(10) note = Random(2); switch (note) case 0: PlayTone(195, WHOLE_NOTE ); Wait(WHOLE_NOTE + SILENCE); break; case 1: PlayTone(246, HALF_NOTE ); Wait(HALF_NOTE + SILENCE); break; case 2: PlayTone(293, QUARTER_NOTE ); Wait(QUARTER_NOTE + SILENCE); break; ENGG1000 (2007) Programming robots with NQC CSE, UNSW 53 / 75 ENGG1000 (2007) Programming robots with NQC CSE, UNSW 54 / 75 The do statement The do statement is a looping construct very similar to the while statement. It has the form: do statements; while (condition); The statements in the body of the do loop are executed as long as the condition evaluates to true. In a while statement, the condition is tested before executing the statements, while in the do statement the condition is tested at the end. Hence with a while statement, the statements in the body of the loop might never be executed, whereas with a do statement, they are executed at least once. Moving around randomly int move_time, turn_time, total_time; total_time = 0; do move_time = Random(100); turn_time = Random(100); Wait(move_time); Wait(turn_time); total_time += move_time; total_time += turn_time; while (total_time < 2000); ENGG1000 (2007) Programming robots with NQC CSE, UNSW 55 / 75 ENGG1000 (2007) Programming robots with NQC CSE, UNSW 56 / 75

15 Timers Using timers The RCX has four built-in timers, numbered from 0 to 3. A timers ticks in increments of 1/10 of a second. The value of a timer can be reset with the command ClearTimer(), providing the timer's number as argument. The current value of a timer with the command Timer(), providing the timer's number as argument. Exercise: change the previous program by making use of a timer, with no need for the variables move_time, turn_time and total_time. SetSensor(SENSOR_1, SENSOR_TOUCH); ClearTimer(3); until ((SENSOR_1 == 1) (Timer(3) > 100)); ENGG1000 (2007) Programming robots with NQC CSE, UNSW 57 / 75 ENGG1000 (2007) Programming robots with NQC CSE, UNSW 58 / 75 A programming challenge Avoiding an obstacle without hitting it We want Hank to be able to: back up, turn right by about 45 degrees, and move forward in case the left sensor only hits an obstacle; back up, turn left by about 45 degrees, and move forward in case the right sensor only hits an obstacle; back up, turn around by 180 degrees, and move in case both sensors hit an obstacle. When both sensors hit an obstacle an within a very short period of time, but not exactly at the same instant, we still want Hank to completely turn around. Write a program that achieves this task. Hint: You might want to use a timer from the moment a sensor has hit an obstacle to check whether the other sensor will also hit the obstacle within a short time interval. ENGG1000 (2007) Programming robots with NQC CSE, UNSW 59 / 75 Hank could react just before it hits something if it could know that it is near to some obstacle. The RCX has an infra-red port with which it can communicate with the computer, or with other robots. As a light sensor is very sensitive to infra-red light, it is possible to build a proximity sensor using the infra-red port on the RCX together with a light sensor. The basic idea is to dene two tasks: one task should send out infra-red messages; the other task should measure uctuations in the light intensity that is reected from objects: the higher the uctuation, the closer the robot is to an object. The light sensor should be placed on the robot above the infra-red port, pointing forwards. The raw mode is used to because it is more precise: it makes the sensor return a value between 0 and ENGG1000 (2007) Programming robots with NQC CSE, UNSW 60 / 75

16 Using a proximity sensor (1) Using a proximity sensor (2) #define SAMPLE_TIME 20 #define TURN_TIME 200 #define LIGHT_DIFFERENCE 5 task check_signal() do lastlevel = SENSOR_2; Wait(SAMPLE_TIME); if (SENSOR_2 > lastlevel + LIGHT_DIFFERENCE) while(true); int lastlevel; SetSensorType(SENSOR_2, SENSOR_TYPE_LIGHT); SetSensorMode(SENSOR_2, SENSOR_MODE_RAW); start send_signal; start check_signal; ENGG1000 (2007) Programming robots with NQC CSE, UNSW 61 / 75 Our next challenges use a robot called Minerva. Programming robots with NQC CSE, UNSW 62 / 75 CSE, UNSW 64 / 75 A movie tells more than a long story... Miverva is equipped with one light sensor. Here is Minerva in action: Build Minerva following the instructions on your CD. Programming robots with NQC ENGG1000 (2007) Minerva in action Minerva the object picker ENGG1000 (2007) task send_signal() while(true) SendMessage(0); Wait(10); CSE, UNSW 63 / 75 ENGG1000 (2007) Programming robots with NQC

17 Subroutines and inline functions (1) Using subroutines To program Minerva, it is convenient to use subroutines and inline functions rather than tasks only. When you need the same piece of code at multiple places in a program, you can put that piece of code in a subroutine and give it a name preceded with the keyword sub. The subroutine can then be executed simply by calling its name from a task. Subroutines cannot be called from other subroutines. ENGG1000 (2007) Programming robots with NQC CSE, UNSW 65 / 75 sub turn_around() Wait(340); Wait(100); turn_around(); Wait(200); turn_around(); Wait(100); turn_around(); ENGG1000 (2007) Programming robots with NQC CSE, UNSW 66 / 75 Subroutines and inline functions (2) Subroutines are stored only once in the RCX; this saves memory. One problem with subroutines is that they cannot use complicated expressions if called from dierent tasks. Inline functions oer an alternative to subroutines. Inline functions are not stored separately: they are copied at each place they are used. So using inline functions costs more memory, but avoids the problems related to using complicated expressions. Dening and calling inline functions is similar to dening and calling subroutines. The only dierence that that the keyword void is used instead of sub. ENGG1000 (2007) Programming robots with NQC CSE, UNSW 67 / 75 Using inline functions The above program, modied to use inline functions instead of subroutines, looks as follows: void turn_around() Wait(340); Wait(100); turn_around(); Wait(200); turn_around(); Wait(100); turn_around(); ENGG1000 (2007) Programming robots with NQC CSE, UNSW 68 / 75

18 Programming Minerva Minerva's program (1) #define TURNAROUND_TIME 325 int i; Minerva's mission is to nd an object to pick up, and bring it back to the starting point. We assume that the objects to pick up are darker than the surface on which Minerva is driving. To return to the starting point, Minerva will measure how long it should drive forward to pick up an object; then it will turn around and drive back for the same amount of time. ENGG1000 (2007) Programming robots with NQC CSE, UNSW 69 / 75 Minerva's program (2) // Arm limit sensor and grabber light sensor. SetSensor(SENSOR_3, SENSOR_LIGHT); SetPower(OUT_A + OUT_C, OUT_FULL); calibrate(); i = 0; while (i < 5) retrieve(); i += 1; Off(OUT_A + OUT_C); ENGG1000 (2007) Programming robots with NQC CSE, UNSW 70 / 75 Minerva's program (3) #define NUMBER_OF_SAMPLES 10 int runningtotal; int threshold; sub calibrate() // Take an average light reading. i = 0; runningtotal = 0; while (i < NUMBER_OF_SAMPLES) runningtotal += SENSOR_3; Wait(10); i += 1; threshold = runningtotal / NUMBER_OF_SAMPLES; void grab() // Run the motor until we hit the limit switch. OnFwd(OUT_A); until (SENSOR_3 == 100); // Back off from the switch. OnRev(OUT_A); until (SENSOR_3!= 100); Off(OUT_A); ENGG1000 (2007) Programming robots with NQC CSE, UNSW 71 / 75 ENGG1000 (2007) Programming robots with NQC CSE, UNSW 72 / 75

Programming robots with NQC

Programming robots with NQC Programming robots with NQC Eric Martin School of Computer Science and Engineering University of New South Wales Sydney, Australia ENGG1000 (2007) Programming robots with NQC CSE, UNSW 1 / 75 Acknowledgments

More information

Programming Lego Robots using NQC

Programming Lego Robots using NQC Programming Lego Robots using NQC William J. Taffe November 2002 Modified from the tutorial originally written by Mark Overmars (Version 3.03, Oct 2, 1999) Preface The Lego MindStorms kits are wonderful

More information

Programming Lego Robots using NQC

Programming Lego Robots using NQC Programming Lego Robots using NQC (Version 3.04, Feb 14, 2002) by Mark Overmars (revisions by John Hansen) Department of Computer Science Utrecht University P.O. Box 80.089, 3508 TB Utrecht the Netherlands

More information

Mobile Agents. Uwe Lämmel. Hochschule Wismar Wismar University of Technology, Business, and Design Business School. Mobile Agents LEGO RCX -1 -

Mobile Agents. Uwe Lämmel. Hochschule Wismar Wismar University of Technology, Business, and Design Business School. Mobile Agents LEGO RCX -1 - Mobile Agents Hochschule Wismar Wismar University of Technology, Business, and Design Business School u.laemmel@wi.hs-wismar.de www.wi.hs-wismar.de/~laemmel/ -1 - Contents RCX-LEGO-Robot: The Hardware

More information

Computers in Engineering Lab #2 Maze Treasure Searching

Computers in Engineering Lab #2 Maze Treasure Searching Computers in Engineering Lab #2 Maze Treasure Searching Objective The objective of this lab project is to program the robot to tracking the maze. The goal is to pick up all the silver treasure in the maze.

More information

RCX Tutorial. Commands Sensor Watchers Stack Controllers My Commands

RCX Tutorial. Commands Sensor Watchers Stack Controllers My Commands RCX Tutorial Commands Sensor Watchers Stack Controllers My Commands The following is a list of commands available to you for programming the robot (See advanced below) On Turns motors (connected to ports

More information

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

12/22/11. Java How to Program, 9/e. Help you get started with Eclipse and NetBeans integrated development environments. Java How to Program, 9/e Education, Inc. All Rights Reserved. } Java application programming } Use tools from the JDK to compile and run programs. } Videos at www.deitel.com/books/jhtp9/ Help you get started

More information

Instructions For Constructing A Braitenberg Vehicle 2 Robot From LEGO Mindstorms Components

Instructions For Constructing A Braitenberg Vehicle 2 Robot From LEGO Mindstorms Components Instructions For Constructing A Braitenberg Vehicle 2 Robot From LEGO Mindstorms Components Michael R.W. Dawson, Biological Computation Project, University of Alberta, Edmonton, Alberta September, 2003

More information

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Fall 2016 Howard Rosenthal

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Fall 2016 Howard Rosenthal Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Fall 2016 Howard Rosenthal Lesson Goals Understand Control Structures Understand how to control the flow of a program

More information

Introduction to C Programming

Introduction to C Programming 1 2 Introduction to C Programming 2.6 Decision Making: Equality and Relational Operators 2 Executable statements Perform actions (calculations, input/output of data) Perform decisions - May want to print

More information

3 The L oop Control Structure

3 The L oop Control Structure 3 The L oop Control Structure Loops The while Loop Tips and Traps More Operators The for Loop Nesting of Loops Multiple Initialisations in the for Loop The Odd Loop The break Statement The continue Statement

More information

Robotics II. Module 2: Application of Data Programming Blocks

Robotics II. Module 2: Application of Data Programming Blocks Robotics II Module 2: Application of Data Programming Blocks PREPARED BY Academic Services Unit December 2011 Applied Technology High Schools, 2011 Module 2: Application of Data Programming Blocks Module

More information

C: How to Program. Week /Mar/05

C: How to Program. Week /Mar/05 1 C: How to Program Week 2 2007/Mar/05 Chapter 2 - Introduction to C Programming 2 Outline 2.1 Introduction 2.2 A Simple C Program: Printing a Line of Text 2.3 Another Simple C Program: Adding Two Integers

More information

Intro to Programming. Unit 7. What is Programming? What is Programming? Intro to Programming

Intro to Programming. Unit 7. What is Programming? What is Programming? Intro to Programming Intro to Programming Unit 7 Intro to Programming 1 What is Programming? 1. Programming Languages 2. Markup vs. Programming 1. Introduction 2. Print Statement 3. Strings 4. Types and Values 5. Math Externals

More information

Learning to Program with Haiku

Learning to Program with Haiku Learning to Program with Haiku Lesson 4 Written by DarkWyrm All material 2010 DarkWyrm It would be incredibly hard to write anything useful if there weren't ways for our programs to make decisions or to

More information

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved. Assoc. Prof. Dr. Marenglen Biba (C) 2010 Pearson Education, Inc. All rights reserved. Java application A computer program that executes when you use the java command to launch the Java Virtual Machine

More information

What you can do: Use Transparency #10

What you can do: Use Transparency #10 Additional Programming Highlights This section contains information on Robotics Invention System programming concepts that can be introduced at different points in the sequenced activities. When appropriate,

More information

Chapter 2 - Introduction to C Programming

Chapter 2 - Introduction to C Programming Chapter 2 - Introduction to C Programming 2 Outline 2.1 Introduction 2.2 A Simple C Program: Printing a Line of Text 2.3 Another Simple C Program: Adding Two Integers 2.4 Memory Concepts 2.5 Arithmetic

More information

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

IT 374 C# and Applications/ IT695 C# Data Structures IT 374 C# and Applications/ IT695 C# Data Structures Module 2.1: Introduction to C# App Programming Xianrong (Shawn) Zheng Spring 2017 1 Outline Introduction Creating a Simple App String Interpolation

More information

Not Quite C Compiler version 1.0 b1

Not Quite C Compiler version 1.0 b1 Not Quite C Compiler version 1.0 b1 Introduction NQC is a simple language for programming the LEGO RCX. The preprocessor and control structures of NQC are very similar to C. NQC is not a general purpose

More information

EV3 Programming Workshop for FLL Coaches

EV3 Programming Workshop for FLL Coaches EV3 Programming Workshop for FLL Coaches Tony Ayad 2017 Outline This workshop is intended for FLL coaches who are interested in learning about Mindstorms EV3 programming language. Programming EV3 Controller

More information

Final Report. [Establish User Requirements] User must be able to press the run button on the robot, as well as use the NQC software.

Final Report. [Establish User Requirements] User must be able to press the run button on the robot, as well as use the NQC software. Team 3 David Letteer Greg Broadwell Kathryn della Porta BE 1200 Basic Engineering Final Report Final Report [Clarify Objectives] Design, build, and program a robot that will acknowledge the presence of

More information

G. Tardiani RoboCup Rescue. EV3 Workshop Part 1 Introduction to RobotC

G. Tardiani RoboCup Rescue. EV3 Workshop Part 1 Introduction to RobotC RoboCup Rescue EV3 Workshop Part 1 Introduction to RobotC Why use RobotC? RobotC is a more traditional text based programming language The more compact coding editor allows for large programs to be easily

More information

8. Control statements

8. Control statements 8. Control statements A simple C++ statement is each of the individual instructions of a program, like the variable declarations and expressions seen in previous sections. They always end with a semicolon

More information

This is the Arduino Uno: This is the Arduino motor shield: Digital pins (0-13) Ground Rail

This is the Arduino Uno: This is the Arduino motor shield: Digital pins (0-13) Ground Rail Reacting to Sensors In this tutorial we will be going over how to program the Arduino to react to sensors. By the end of this workshop you will have an understanding of how to use sensors with the Arduino

More information

lab A.3: introduction to RoboLab vocabulary materials cc30.03 Brooklyn College, CUNY c 2006 Name: RoboLab communication tower canvas icon

lab A.3: introduction to RoboLab vocabulary materials cc30.03 Brooklyn College, CUNY c 2006 Name: RoboLab communication tower canvas icon cc30.03 Brooklyn College, CUNY c 2006 lab A.3: introduction to RoboLab Name: vocabulary RoboLab communication tower canvas icon drag-and-drop function palette tools palette program algorithm syntax error

More information

JME Language Reference Manual

JME Language Reference Manual JME Language Reference Manual 1 Introduction JME (pronounced jay+me) is a lightweight language that allows programmers to easily perform statistic computations on tabular data as part of data analysis.

More information

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Spring 2016 Howard Rosenthal

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Spring 2016 Howard Rosenthal Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Spring 2016 Howard Rosenthal Lesson Goals Understand Control Structures Understand how to control the flow of a program

More information

Topic 2: Introduction to Programming

Topic 2: Introduction to Programming Topic 2: Introduction to Programming 1 Textbook Strongly Recommended Exercises The Python Workbook: 12, 13, 23, and 28 Recommended Exercises The Python Workbook: 5, 7, 15, 21, 22 and 31 Recommended Reading

More information

SPARK-PL: Introduction

SPARK-PL: Introduction Alexey Solovyev Abstract All basic elements of SPARK-PL are introduced. Table of Contents 1. Introduction to SPARK-PL... 1 2. Alphabet of SPARK-PL... 3 3. Types and variables... 3 4. SPARK-PL basic commands...

More information

CMP 108: Programming for Non-Majors

CMP 108: Programming for Non-Majors CMP 108: Programming for Non-Majors Lecture Notes 9 February 2007 Prof. Katherine St. John Lehman College & the Graduate Center City Universtity of New York Katherine St. John City University of New York

More information

Chapter 17. Fundamental Concepts Expressed in JavaScript

Chapter 17. Fundamental Concepts Expressed in JavaScript Chapter 17 Fundamental Concepts Expressed in JavaScript Learning Objectives Tell the difference between name, value, and variable List three basic data types and the rules for specifying them in a program

More information

Sphero Lightning Lab Cheat Sheet

Sphero Lightning Lab Cheat Sheet Actions Tool Description Variables Ranges Roll Combines heading, speed and time variables to make the robot roll. Duration Speed Heading (0 to 999999 seconds) (degrees 0-359) Set Speed Sets the speed of

More information

Programming for Engineers Introduction to C

Programming for Engineers Introduction to C Programming for Engineers Introduction to C ICEN 200 Spring 2018 Prof. Dola Saha 1 Simple Program 2 Comments // Fig. 2.1: fig02_01.c // A first program in C begin with //, indicating that these two lines

More information

CGS 3066: Spring 2015 JavaScript Reference

CGS 3066: Spring 2015 JavaScript Reference CGS 3066: Spring 2015 JavaScript Reference Can also be used as a study guide. Only covers topics discussed in class. 1 Introduction JavaScript is a scripting language produced by Netscape for use within

More information

In this lab, you will learn more about selection statements. You will get familiar to

In this lab, you will learn more about selection statements. You will get familiar to Objective: In this lab, you will learn more about selection statements. You will get familiar to nested if and switch statements. Nested if Statements: When you use if or if...else statement, you can write

More information

CSE 142 Su 04 Computer Programming 1 - Java. Objects

CSE 142 Su 04 Computer Programming 1 - Java. Objects Objects Objects have state and behavior. State is maintained in instance variables which live as long as the object does. Behavior is implemented in methods, which can be called by other objects to request

More information

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

VARIABLES. Aim Understanding how computer programs store values, and how they are accessed and used in computer programs. Lesson 2 VARIABLES Aim Understanding how computer programs store values, and how they are accessed and used in computer programs. WHAT ARE VARIABLES? When you input data (i.e. information) into a computer

More information

2/29/2016. Definition: Computer Program. A simple model of the computer. Example: Computer Program. Data types, variables, constants

2/29/2016. Definition: Computer Program. A simple model of the computer. Example: Computer Program. Data types, variables, constants Data types, variables, constants Outline.1 Introduction. Text.3 Memory Concepts.4 Naming Convention of Variables.5 Arithmetic in C.6 Type Conversion Definition: Computer Program A Computer program is a

More information

All copyrights reserved - KV NAD, Aluva. Dinesh Kumar Ram PGT(CS) KV NAD Aluva

All copyrights reserved - KV NAD, Aluva. Dinesh Kumar Ram PGT(CS) KV NAD Aluva All copyrights reserved - KV NAD, Aluva Dinesh Kumar Ram PGT(CS) KV NAD Aluva Overview Looping Introduction While loops Syntax Examples Points to Observe Infinite Loops Examples using while loops do..

More information

VISUAL GUIDE to. RX Scripting. for Roulette Xtreme - System Designer 2.0. L J Howell UX Software Ver. 1.0

VISUAL GUIDE to. RX Scripting. for Roulette Xtreme - System Designer 2.0. L J Howell UX Software Ver. 1.0 VISUAL GUIDE to RX Scripting for Roulette Xtreme - System Designer 2.0 L J Howell UX Software 2009 Ver. 1.0 TABLE OF CONTENTS INTRODUCTION...ii What is this book about?... iii How to use this book... iii

More information

\n is used in a string to indicate the newline character. An expression produces data. The simplest expression

\n is used in a string to indicate the newline character. An expression produces data. The simplest expression Chapter 1 Summary Comments are indicated by a hash sign # (also known as the pound or number sign). Text to the right of the hash sign is ignored. (But, hash loses its special meaning if it is part of

More information

Algorithmic "imperative" language

Algorithmic imperative language Algorithmic "imperative" language Undergraduate years Epita November 2014 The aim of this document is to introduce breiy the "imperative algorithmic" language used in the courses and tutorials during the

More information

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

Lecture 05 I/O statements Printf, Scanf Simple statements, Compound statements Programming, Data Structures and Algorithms Prof. Shankar Balachandran Department of Computer Science and Engineering Indian Institute of Technology, Madras Lecture 05 I/O statements Printf, Scanf Simple

More information

CS125 : Introduction to Computer Science. Lecture Notes #6 Compound Statements, Scope, and Advanced Conditionals

CS125 : Introduction to Computer Science. Lecture Notes #6 Compound Statements, Scope, and Advanced Conditionals CS125 : Introduction to Computer Science Lecture Notes #6 Compound Statements, Scope, and Advanced Conditionals c 2005, 2004, 2003, 2002, 2001, 2000 Jason Zych 1 Lecture 6 : Compound Statements, Scope,

More information

Advanced Activities - Information and Ideas

Advanced Activities - Information and Ideas Advanced Activities - Information and Ideas Congratulations! You successfully created and controlled the robotic chameleon using the program developed for the chameleon project. Here you'll learn how you

More information

CS 106 Introduction to Computer Science I

CS 106 Introduction to Computer Science I CS 106 Introduction to Computer Science I 06 / 04 / 2015 Instructor: Michael Eckmann Today s Topics Questions / comments? Calling methods (noting parameter(s) and their types, as well as the return type)

More information

Java+- Language Reference Manual

Java+- Language Reference Manual Fall 2016 COMS4115 Programming Languages & Translators Java+- Language Reference Manual Authors Ashley Daguanno (ad3079) - Manager Anna Wen (aw2802) - Tester Tin Nilar Hlaing (th2520) - Systems Architect

More information

YOLOP Language Reference Manual

YOLOP Language Reference Manual YOLOP Language Reference Manual Sasha McIntosh, Jonathan Liu & Lisa Li sam2270, jl3516 and ll2768 1. Introduction YOLOP (Your Octothorpean Language for Optical Processing) is an image manipulation language

More information

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

Lecture Programming in C++ PART 1. By Assistant Professor Dr. Ali Kattan Lecture 08-1 Programming in C++ PART 1 By Assistant Professor Dr. Ali Kattan 1 The Conditional Operator The conditional operator is similar to the if..else statement but has a shorter format. This is useful

More information

Loops and Switches Pre-Quiz

Loops and Switches Pre-Quiz Loops and Switches Loops and Switches Pre-Quiz 1. What kind of blocks are these? 2. Name two kinds of controls that can be specified to determine how long a loop repeats. 3. Give an example of a program

More information

Basics of Java Programming

Basics of Java Programming Basics of Java Programming Lecture 2 COP 3252 Summer 2017 May 16, 2017 Components of a Java Program statements - A statement is some action or sequence of actions, given as a command in code. A statement

More information

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

c) Comments do not cause any machine language object code to be generated. d) Lengthy comments can cause poor execution-time performance. 2.1 Introduction (No questions.) 2.2 A Simple Program: Printing a Line of Text 2.1 Which of the following must every C program have? (a) main (b) #include (c) /* (d) 2.2 Every statement in C

More information

9. Elementary Algebraic and Transcendental Scalar Functions

9. Elementary Algebraic and Transcendental Scalar Functions Scalar Functions Summary. Introduction 2. Constants 2a. Numeric Constants 2b. Character Constants 2c. Symbol Constants 2d. Nested Constants 3. Scalar Functions 4. Arithmetic Scalar Functions 5. Operators

More information

Unit 3. Operators. School of Science and Technology INTRODUCTION

Unit 3. Operators. School of Science and Technology INTRODUCTION INTRODUCTION Operators Unit 3 In the previous units (unit 1 and 2) you have learned about the basics of computer programming, different data types, constants, keywords and basic structure of a C program.

More information

Creating a C++ Program

Creating a C++ Program Program A computer program (also software, or just a program) is a sequence of instructions written in a sequence to perform a specified task with a computer. 1 Creating a C++ Program created using an

More information

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

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 8: SEP. 29TH INSTRUCTOR: JIAYIN WANG CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 8: SEP. 29TH INSTRUCTOR: JIAYIN WANG 1 Notice Prepare the Weekly Quiz The weekly quiz is for the knowledge we learned in the previous week (both the

More information

Typescript on LLVM Language Reference Manual

Typescript on LLVM Language Reference Manual Typescript on LLVM Language Reference Manual Ratheet Pandya UNI: rp2707 COMS 4115 H01 (CVN) 1. Introduction 2. Lexical Conventions 2.1 Tokens 2.2 Comments 2.3 Identifiers 2.4 Reserved Keywords 2.5 String

More information

LME Software Block Quick Reference 1. Common Palette

LME Software Block Quick Reference 1. Common Palette LME Software Block Quick Reference Common Palette Move Block Use this block to set your robot to go forwards or backwards in a straight line or to turn by following a curve. Define how far your robot will

More information

Control Flow. COMS W1007 Introduction to Computer Science. Christopher Conway 3 June 2003

Control Flow. COMS W1007 Introduction to Computer Science. Christopher Conway 3 June 2003 Control Flow COMS W1007 Introduction to Computer Science Christopher Conway 3 June 2003 Overflow from Last Time: Why Types? Assembly code is typeless. You can take any 32 bits in memory, say this is an

More information

CSc 10200! Introduction to Computing. Lecture 2-3 Edgardo Molina Fall 2013 City College of New York

CSc 10200! Introduction to Computing. Lecture 2-3 Edgardo Molina Fall 2013 City College of New York CSc 10200! Introduction to Computing Lecture 2-3 Edgardo Molina Fall 2013 City College of New York 1 C++ for Engineers and Scientists Third Edition Chapter 2 Problem Solving Using C++ 2 Objectives In this

More information

Chapter 4 Defining Classes I

Chapter 4 Defining Classes I Chapter 4 Defining Classes I This chapter introduces the idea that students can create their own classes and therefore their own objects. Introduced is the idea of methods and instance variables as the

More information

GE U111 Engineering Problem Solving & Computation Lecture 6 February 2, 2004

GE U111 Engineering Problem Solving & Computation Lecture 6 February 2, 2004 GE U111 Engineering Problem Solving & Computation Lecture 6 February 2, 2004 Functions and Program Structure Today we will be learning about functions. You should already have an idea of their uses. Cout

More information

CPS122 Lecture: From Python to Java last revised January 4, Objectives:

CPS122 Lecture: From Python to Java last revised January 4, Objectives: Objectives: CPS122 Lecture: From Python to Java last revised January 4, 2017 1. To introduce the notion of a compiled language 2. To introduce the notions of data type and a statically typed language 3.

More information

CS112 Lecture: Working with Numbers

CS112 Lecture: Working with Numbers CS112 Lecture: Working with Numbers Last revised January 30, 2008 Objectives: 1. To introduce arithmetic operators and expressions 2. To expand on accessor methods 3. To expand on variables, declarations

More information

Fundamental of Programming (C)

Fundamental of Programming (C) Borrowed from lecturer notes by Omid Jafarinezhad Fundamental of Programming (C) Lecturer: Vahid Khodabakhshi Lecture 3 Constants, Variables, Data Types, And Operations Department of Computer Engineering

More information

Introduction to C Programming. Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan

Introduction to C Programming. Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan Introduction to C Programming Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan Outline Printing texts Adding 2 integers Comparing 2 integers C.E.,

More information

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

LESSON 1. A C program is constructed as a sequence of characters. Among the characters that can be used in a program are: LESSON 1 FUNDAMENTALS OF C The purpose of this lesson is to explain the fundamental elements of the C programming language. C like other languages has all alphabet and rules for putting together words

More information

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

Control Structures. Code can be purely arithmetic assignments. At some point we will need some kind of control or decision making process to occur Control Structures Code can be purely arithmetic assignments At some point we will need some kind of control or decision making process to occur C uses the if keyword as part of it s control structure

More information

The C Programming Language Guide for the Robot Course work Module

The C Programming Language Guide for the Robot Course work Module The C Programming Language Guide for the Robot Course work Module Eric Peasley 2018 v6.4 1 2 Table of Contents Variables...5 Assignments...6 Entering Numbers...6 Operators...7 Arithmetic Operators...7

More information

CMPE 8 Lab 3: Introduction to the Scribbler 2 and SPIN

CMPE 8 Lab 3: Introduction to the Scribbler 2 and SPIN CMPE 8 Lab 3: Introduction to the Scribbler 2 and SPIN Lab Objectives By the end of this lab you should be able to: 1. Identify the key components of the Scribbler 2 robot. 2. Use the Propeller Tool IDE

More information

Chapter 2: Programming Concepts

Chapter 2: Programming Concepts Chapter 2: Programming Concepts Objectives Students should Know the steps required to create programs using a programming language and related terminology. Be familiar with the basic structure of a Java

More information

Robotics Study Material School Level 1 Semester 2

Robotics Study Material School Level 1 Semester 2 Robotics Study Material School Level 1 Semester 2 Contents UNIT-3... 4 NXT-PROGRAMMING... 4 CHAPTER-1... 5 NXT- PROGRAMMING... 5 CHAPTER-2... 6 NXT-BRICK PROGRAMMING... 6 A. Multiple choice questions:...

More information

Wentworth Institute of Technology. Engineering & Technology WIT COMP1000. Java Basics

Wentworth Institute of Technology. Engineering & Technology WIT COMP1000. Java Basics WIT COMP1000 Java Basics Java Origins Java was developed by James Gosling at Sun Microsystems in the early 1990s It was derived largely from the C++ programming language with several enhancements Java

More information

A PROGRAM IS A SEQUENCE of instructions that a computer can execute to

A PROGRAM IS A SEQUENCE of instructions that a computer can execute to A PROGRAM IS A SEQUENCE of instructions that a computer can execute to perform some task. A simple enough idea, but for the computer to make any use of the instructions, they must be written in a form

More information

CS112 Lecture: Repetition Statements

CS112 Lecture: Repetition Statements CS112 Lecture: Repetition Statements Objectives: Last revised 2/18/05 1. To explain the general form of the java while loop 2. To introduce and motivate the java do.. while loop 3. To explain the general

More information

Arithmetic Operators. Binary Arithmetic Operators. Arithmetic Operators. A Closer Look at the / Operator. A Closer Look at the % Operator

Arithmetic Operators. Binary Arithmetic Operators. Arithmetic Operators. A Closer Look at the / Operator. A Closer Look at the % Operator 1 A Closer Look at the / Operator Used for performing numeric calculations C++ has unary, binary, and ternary s: unary (1 operand) - binary ( operands) 13-7 ternary (3 operands) exp1? exp : exp3 / (division)

More information

NQC Programmer's Guide

NQC Programmer's Guide Version 2.5 a4 by Dave Baum Contents 1 Introduction... 1 2 The NQC Language... 2 2.1 Lexical Rules... 2 2.1.1 Comments... 2 2.1.2 Whitespace... 3 2.1.3 Numerical Constants... 3 2.1.4 Identifiers and Keywords...

More information

CPSC W2 Midterm #2 Sample Solutions

CPSC W2 Midterm #2 Sample Solutions CPSC 320 2014W2 Midterm #2 Sample Solutions March 13, 2015 1 Canopticon [8 marks] Classify each of the following recurrences (assumed to have base cases of T (1) = T (0) = 1) into one of the three cases

More information

Loops and Switches Pre-Quiz

Loops and Switches Pre-Quiz Loops and Switches Loops and Switches Pre-Quiz 1. What kind of blocks are these? 2. Name two kinds of controls that can be specified to determine how long a loop repeats. 3. Give an example of a program

More information

Getting started 7. Saving data 23

Getting started 7. Saving data 23 Contents 1 2 3 4 Getting started 7 Programming code 8 Setting up 10 Exploring IDLE 12 Getting help 14 Saving programs 16 Storing values 18 Adding comments 20 Naming rules 21 Summary 22 Saving data 23 Storing

More information

Robolab. Table of Contents. St. Mary s School, Panama. Robotics. Ch. 5: Robolab, by: Ernesto E. Angulo J.

Robolab. Table of Contents. St. Mary s School, Panama. Robotics. Ch. 5: Robolab, by: Ernesto E. Angulo J. Robolab 5 Table of Contents Objectives...2 Starting the program...2 Programming...3 Downloading...8 Tools...9 Icons...9 Loops and jumps...11 Multiple tasks...12 Timers...12 Variables...14 Sensors...15

More information

STUDENT OUTLINE. Lesson 8: Structured Programming, Control Structures, if-else Statements, Pseudocode

STUDENT OUTLINE. Lesson 8: Structured Programming, Control Structures, if-else Statements, Pseudocode STUDENT OUTLINE Lesson 8: Structured Programming, Control Structures, if- Statements, Pseudocode INTRODUCTION: This lesson is the first of four covering the standard control structures of a high-level

More information

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

Full file at   C How to Program, 6/e Multiple Choice Test Bank 2.1 Introduction 2.2 A Simple Program: Printing a Line of Text 2.1 Lines beginning with let the computer know that the rest of the line is a comment. (a) /* (b) ** (c) REM (d)

More information

The first program: Little Crab

The first program: Little Crab Chapter 2 The first program: Little Crab topics: concepts: writing code: movement, turning, reacting to the screen edges source code, method call, parameter, sequence, if-statement In the previous chapter,

More information

AN OVERVIEW OF C, PART 3. CSE 130: Introduction to Programming in C Stony Brook University

AN OVERVIEW OF C, PART 3. CSE 130: Introduction to Programming in C Stony Brook University AN OVERVIEW OF C, PART 3 CSE 130: Introduction to Programming in C Stony Brook University FANCIER OUTPUT FORMATTING Recall that you can insert a text field width value into a printf() format specifier:

More information

Variables. Data Types.

Variables. Data Types. Variables. Data Types. The usefulness of the "Hello World" programs shown in the previous section is quite questionable. We had to write several lines of code, compile them, and then execute the resulting

More information

Chapter 2, Part I Introduction to C Programming

Chapter 2, Part I Introduction to C Programming Chapter 2, Part I Introduction to C Programming C How to Program, 8/e, GE 2016 Pearson Education, Ltd. All rights reserved. 1 2016 Pearson Education, Ltd. All rights reserved. 2 2016 Pearson Education,

More information

In Java, data type boolean is used to represent Boolean data. Each boolean constant or variable can contain one of two values: true or false.

In Java, data type boolean is used to represent Boolean data. Each boolean constant or variable can contain one of two values: true or false. CS101, Mock Boolean Conditions, If-Then Boolean Expressions and Conditions The physical order of a program is the order in which the statements are listed. The logical order of a program is the order in

More information

Chapter 2. C++ Basics. Copyright 2014 Pearson Addison-Wesley. All rights reserved.

Chapter 2. C++ Basics. Copyright 2014 Pearson Addison-Wesley. All rights reserved. Chapter 2 C++ Basics 1 Overview 2.1 Variables and Assignments 2.2 Input and Output 2.3 Data Types and Expressions 2.4 Simple Flow of Control 2.5 Program Style Slide 2-3 2.1 Variables and Assignments 2

More information

Lesson 2 Variables and I/O

Lesson 2 Variables and I/O Lesson 2 Variables and I/O Pic 10A Ricardo Salazar Free form layout C++ lets you use spaces and returns (enter key) wherever you see fit. cout

More information

2.8. Decision Making: Equality and Relational Operators

2.8. Decision Making: Equality and Relational Operators Page 1 of 6 [Page 56] 2.8. Decision Making: Equality and Relational Operators A condition is an expression that can be either true or false. This section introduces a simple version of Java's if statement

More information

UNIT- 3 Introduction to C++

UNIT- 3 Introduction to C++ UNIT- 3 Introduction to C++ C++ Character Sets: Letters A-Z, a-z Digits 0-9 Special Symbols Space + - * / ^ \ ( ) [ ] =!= . $, ; : %! &? _ # = @ White Spaces Blank spaces, horizontal tab, carriage

More information

Princess Nourah bint Abdulrahman University. Computer Sciences Department

Princess Nourah bint Abdulrahman University. Computer Sciences Department Princess Nourah bint Abdulrahman University 1 And use http://www.w3schools.com/ JavaScript Objectives Introduction to JavaScript Objects Data Variables Operators Types Functions Events 4 Why Study JavaScript?

More information

Getting Started. Office Hours. CSE 231, Rich Enbody. After class By appointment send an . Michigan State University CSE 231, Fall 2013

Getting Started. Office Hours. CSE 231, Rich Enbody. After class By appointment send an  . Michigan State University CSE 231, Fall 2013 CSE 231, Rich Enbody Office Hours After class By appointment send an email 2 1 Project 1 Python arithmetic Do with pencil, paper and calculator first Idle Handin Help room 3 What is a Computer Program?

More information

Honu. Version November 6, 2010

Honu. Version November 6, 2010 Honu Version 5.0.2 November 6, 2010 Honu is a family of languages built on top of Racket. Honu syntax resembles Java. Like Racket, however, Honu has no fixed syntax, because Honu supports extensibility

More information

Programming with C++ Language

Programming with C++ Language Programming with C++ Language Fourth stage Prepared by: Eng. Samir Jasim Ahmed Email: engsamirjasim@yahoo.com Prepared By: Eng. Samir Jasim Page 1 Introduction: Programming languages: A programming language

More information

Data types, variables, constants

Data types, variables, constants Data types, variables, constants Outline 2.1 Introduction 2.2 A Simple C Program: Printing a Line of Text 2.3 Another Simple C Program: Adding Two Integers 2.4 Memory Concepts 2.5 Arithmetic in C 2.6 Decision

More information

Chapter 2. C++ Basics. Copyright 2014 Pearson Addison-Wesley. All rights reserved.

Chapter 2. C++ Basics. Copyright 2014 Pearson Addison-Wesley. All rights reserved. Chapter 2 C++ Basics Overview 2.1 Variables and Assignments 2.2 Input and Output 2.3 Data Types and Expressions 2.4 Simple Flow of Control 2.5 Program Style 3 2.1 Variables and Assignments Variables and

More information

Chapter 2. C++ Basics

Chapter 2. C++ Basics Chapter 2 C++ Basics Overview 2.1 Variables and Assignments 2.2 Input and Output 2.3 Data Types and Expressions 2.4 Simple Flow of Control 2.5 Program Style Slide 2-2 2.1 Variables and Assignments Variables

More information