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

Материал из roboforum.ru Wiki
Перейти к: навигация, поиск
(Учимся делать повороты)
Строка 5: Строка 5:
  
 
==Учимся делать повороты==
 
==Учимся делать повороты==
You can make your robot turn by stopping or reversing the direction of one of the two motors. Here is an
+
Вы можете заставить робота поворачиваться путём остановки или включении в обратном направлении одного из двух моторов. Вот пример того, как это можно сделать. Введите его, загрузите полученную программу в робота и запустите. Робот должен проехать немного вперед, после чего повернуться на 90 градусов вправо.
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
+
task main()
turn.
+
{
task main()
+
OnFwd(OUT_AC, 75);
{
+
Wait(800);
OnFwd(OUT_AC, 75);
+
OnRev(OUT_C, 75);
Wait(800);
+
Wait(360);
OnRev(OUT_C, 75);
+
Off(OUT_AC);
Wait(360);
+
}
Off(OUT_AC);
+
 
}
+
You might have to try some slightly different numbers than 360 in the second Wait() command to make a 90
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
 
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
 
program it is easier to use a name for this number. In NXC you can define constant values as shown in the
 
following program.
 
following program.
#define MOVE_TIME 1000
+
#define MOVE_TIME 1000
#define TURN_TIME 360
+
#define TURN_TIME 360
task main()
+
task main()
{
+
{
OnFwd(OUT_AC, 75);
+
OnFwd(OUT_AC, 75);
Wait(MOVE_TIME);
+
Wait(MOVE_TIME);
OnRev(OUT_C, 75);
+
OnRev(OUT_C, 75);
Wait(TURN_TIME);
+
Wait(TURN_TIME);
Off(OUT_AC);
+
Off(OUT_AC);
}
+
}
 
The first two lines define two constants. These can now be used throughout the program. Defining constants is
 
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
 
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
 
BricxCC gives the define statements its own color. As we will see in Chapter VI, you can also define things
 
other than constants.
 
other than constants.
 +
 
==Повторяем команды==
 
==Повторяем команды==
 
Let us now try to write a program that makes the robot drive in a square. Going in a square means: driving
 
Let us now try to write a program that makes the robot drive in a square. Going in a square means: driving

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

Автор: Daniele Benedettelli

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

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

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

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

Учимся делать повороты

Вы можете заставить робота поворачиваться путём остановки или включении в обратном направлении одного из двух моторов. Вот пример того, как это можно сделать. Введите его, загрузите полученную программу в робота и запустите. Робот должен проехать немного вперед, после чего повернуться на 90 градусов вправо.

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 360 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.

#define MOVE_TIME 1000
#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.

Повторяем команды

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.

Добавляем комментарии

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 }

Подводим итоги

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.