Программирование LEGO NXT роботов на языке NXC - Более интересная программа — различия между версиями

Материал из roboforum.ru Wiki
Перейти к: навигация, поиск
(Создана новая страница размером =Более интересная программа= Наша первая программа была не сильно впечатля...)
 
Строка 1: Строка 1:
 +
<p align=center><b>Автор: Daniele Benedettelli</b><br><br><i>Перевод: © Ботов Антон aka =DeaD=, 2009<br><br>Эксклюзивно для www.roboforum.ru<br> копирование на другие ресурсы и публикация перевода<br>без разрешения его автора запрещены</i></p>
 +
 
=Более интересная программа=
 
=Более интересная программа=
 
Наша первая программа была не сильно впечатляющая. Так что давайте попробуем сделать её более интересной. Мы будем делать это в несколько шагов, показывая некоторые важные возможности изучаемого языка программирования NXC.
 
Наша первая программа была не сильно впечатляющая. Так что давайте попробуем сделать её более интересной. Мы будем делать это в несколько шагов, показывая некоторые важные возможности изучаемого языка программирования NXC.

Версия 15:01, 16 мая 2009

Автор: Daniele Benedettelli

Перевод: © Ботов Антон aka =DeaD=, 2009

Эксклюзивно для www.roboforum.ru
копирование на другие ресурсы и публикация перевода
без разрешения его автора запрещены

Более интересная программа

Наша первая программа была не сильно впечатляющая. Так что давайте попробуем сделать её более интересной. Мы будем делать это в несколько шагов, показывая некоторые важные возможности изучаемого языка программирования NXC.

Making turns

You can make your robot turn by stopping or reversing the direction of one of the two motors. Here is an example. Type it in, download it to your robot and let it run. It should drive a bit and then make a 90-degree right turn. task main() { OnFwd(OUT_AC, 75); Wait(800); OnRev(OUT_C, 75); Wait(360); Off(OUT_AC); } You might have to try some slightly different numbers than 500 in the second Wait() command to make a 90 degree turn. This depends on the type of surface on which the robot runs. Rather than changing this in the program it is easier to use a name for this number. In NXC you can define constant values as shown in the following program.

  1. define MOVE_TIME 1000
  2. define TURN_TIME 360

task main() { OnFwd(OUT_AC, 75); Wait(MOVE_TIME); OnRev(OUT_C, 75); Wait(TURN_TIME); Off(OUT_AC); } The first two lines define two constants. These can now be used throughout the program. Defining constants is good for two reasons: it makes the program more readable, and it is easier to change the values. Note that BricxCC gives the define statements its own color. As we will see in Chapter VI, you can also define things other than constants.

Repeating commands

Let us now try to write a program that makes the robot drive in a square. Going in a square means: driving forwards, turning 90 degrees, driving forwards again, turning 90 degrees, etc. We could repeat the above piece of code four times but this can be done a lot easier with the repeat statement.

  1. define MOVE_TIME 500
  2. define TURN_TIME 500

task main() { repeat(4) { OnFwd(OUT_AC, 75); Wait(MOVE_TIME); OnRev(OUT_C, 75); Wait(TURN_TIME); } Off(OUT_AC); } The number inside the repeat statement's parentheses indicates how many times the code inside its brackets must be repeated. Note that, in the above program, we also indent the statements. This is not necessary, but it makes the program more readable. As a final example, let us make the robot drive 10 times in a square. Here is the program:

  1. define MOVE_TIME 1000
  2. define TURN_TIME 500

task main() { repeat(10) { repeat(4) { OnFwd(OUT_AC, 75); Wait(MOVE_TIME); OnRev(OUT_C, 75); Wait(TURN_TIME); } } Off(OUT_AC); } There is now one repeat statement inside the other. We call this a “nested” repeat statement. You can nest repeat statements as much as you like. Take a careful look at the brackets and the indentation used in the program. The task starts at the first bracket and ends at the last. The first repeat statement starts at the second bracket and ends at the fifth. The nested repeat statement starts at the third bracket and ends at the fourth. As you see the brackets always come in pairs and the piece between the brackets we indent.

Adding comments

To make your program even more readable, it is good to add some comment to it. Whenever you put // on a line, the rest of that line is ignored and can be used for comments. A long comment can be put between /* and

  • /. Comments are syntax highlighted in the BricxCC. The full program could look as follows:

- 12 - /* 10 SQUARES This program make the robot run 10 squares

  • /
  1. define MOVE_TIME 500 // Time for a straight move
  2. define TURN_TIME 360 // Time for turning 90 degrees

task main() { repeat(10) // Make 10 squares { repeat(4) { OnFwd(OUT_AC, 75); Wait(MOVE_TIME); OnRev(OUT_C, 75); Wait(TURN_TIME); } } Off(OUT_AC); // Now turn the motors off }

Summary

In this chapter you learned the use of the repeat statement and the use of comment. Also you saw the function of nested brackets and the use of indentation. With all you know so far you can make the robot move along all sorts of paths. It is a good exercise to try and write some variations of the programs in this chapter before continuing with the next chapter.