This article is a Private article. Only a writer and users who know the URL can access it.
Please change open range to public in publish setting if you want to share this article with other users.

More than 5 years have passed since last update.

8.b. Working with Multiple C Files in Atmel Studio (Pololu 3pi Robot User’s Guide) 日本語訳【非公式】

Last updated at Posted at 2019-05-18

これは Pololu 3pi Robot User’s Guide ≫ 8.b. Working with Multiple C Files in Atmel Studio の非公式日本語訳です。
目次
前: 8.a. Solving a Line Maze
次: 8.c. Left Hand on the Wall

8.b. Working with Multiple C Files in Atmel Studio

The C source code for an example line maze solver is available in the folder examples\atmegaxx8\3pi-mazesolver.

ライン迷路解決のサンプルCソースコードはexamples\atmegaxx8\3pi-mazesolverフォルダにあります。

Note: An Arduino-compatible version of this sample program can be downloaded as part of the Pololu Arduino Libraries (see Section 5.g) The Arduino sample sketch is all contained within a single file.

注意:このサンプルプログラムのArduino互換バージョンは Pololu Arduino Libraries の一部としてダウンロードできます(Section 5.gを参照)。Arduinoサンプルスケッチはすべて1つのファイルに収められています。

This program is much more complicated than the examples you have seen so far, so we have split it up into multiple files. Using multiple files makes it easier for you to keep track of your code. For example, the file turn.c contains only a single function, used to make turns at the intersections:

このプログラムは、これまで見てきた例よりもはるかに複雑なため、複数のファイルに分割しました。複数のファイルにすると、コードを追いやすくなります。たとえば、ファイルturn.cは、交差点で曲がるために使われる1つの関数のみを含みます。

libpololu-avr\examples\atmega328p\3pi-mazesolver\turn.c
#include <pololu/3pi.h>

// Turns according to the parameter dir, which should be 'L', 'R', 'S'
// (straight), or 'B' (back).
void turn(char dir)
{
    switch(dir)
    {
    case 'L':
        // Turn left.
        set_motors(-80,80);
        delay_ms(200);
        break;
    case 'R':
        // Turn right.
        set_motors(80,-80);
        delay_ms(200);
        break;
    case 'B':
        // Turn around.
        set_motors(80,-80);
        delay_ms(400);
        break;
    case 'S':
        // Don't do anything!
        break;
    }
}

The first line of the file, like any C file that you will be writing for the 3pi, contains an include command that gives you access to the functions in the Pololu AVR Library. Within turn(), we then use the library functions delay_ms() and set_motors() to perform left turns, right turns, and U-turns. Straight “turns” are also handled by this function, though they don’t require us to take any action. The motor speeds and the timings for the turns are parameters that needed to be adjusted for the 3pi; as you work on making your maze solver faster, these are some of the numbers that you might need to adjust.

ファイルの最初の行は、3pi用に作成するCファイルと同様に Pololu AVR Library の関数にアクセスするためのincludeコマンドを含みます。turn()内で、ライブラリ関数delay_ms()set_motors()を使用して、左折、右折、Uターンを実行します。直進する"ターン"もこの関数で処理されますが、何らかのアクションを起こす必要はありません。モーターの速度と回転のタイミングは、3pi用に調整する必要があるパラメタです。迷路解決をより速くすることに取り組んでいるので、これらはあなたが調整する必要があるかもしれない項目の一部です。

To access this function from other C files, we need a “header file”, which is called turn.h. The header file just contains a single line:

他のCファイルからこの関数にアクセスするにはturn.hと呼ばれるヘッダーファイルが必要です。ヘッダーファイルには1行が含まれます:

libpololu-avr\examples\atmega328p\3pi-mazesolver\turn.h
void turn(char dir);

This line declares the turn() function without actually including a copy of its code. To access the declaration, each C file that needs to call turn() adds the following line:

この行は、関数のコードのコピーを含まずにturn()関数を宣言しています。宣言にアクセスするために、turn()を呼び出す必要があるCファイルごとに以下の行を追加します:

#include "turn.h"

Note the double-quotes being used instead of angle brackets. This signifies to the C compiler that the header file is in the project directory, rather than being a system header file like 3pi.h. Always remember to put the code for your functions in the C file instead of the header file! If you do it the other way, you will be making a separate copy of the code in each file that includes the header.

<>の代わりに""が使われていることに注意して下さい。これは3pi.hのようなシステムヘッダーファイルではなく、ヘッダーファイルがプロジェクトディレクトリにあることをCコンパイラに知らせます。関数のコードはヘッダーファイルではなくCファイルに置くことを忘れないでください。そうしない場合は、ヘッダーファイルをインクルードしているファイル毎に、コードのコピーが個別に作成されます。

The file follow-segment.c also contains a single function, follow_segment(), which will drive 3pi straight along a line segment until it reaches an intersection or the end of the line. This is almost the same as the line following code discussed in Section 7, but with extra checks for intersections and the ends of lines. Here is the function:

follow-segment.cも1つの関数follow_segment()を含みます。これは、交差点か行き止まりに到達するまで、ライン区間をまっすぐに3piを駆動します。これはSection 7で論じたライン追跡コードとほぼ同じですが、追加で交差点と行き止まりのチェックがあります。関数はこちらです:

libpololu-avr\examples\atmega328p\3pi-mazesolver\follow-segment.c
void follow_segment()
{
    int last_proportional = 0;
    long integral=0;

    while(1)
    {
        // Normally, we will be following a line.  The code below is
        // similar to the 3pi-linefollower-pid example, but the maximum
        // speed is turned down to 60 for reliability.

        // Get the position of the line.
        unsigned int sensors[5];
        unsigned int position = read_line(sensors,IR_EMITTERS_ON);

        // The "proportional" term should be 0 when we are on the line.
        int proportional = ((int)position) - 2000;

        // Compute the derivative (change) and integral (sum) of the
        // position.
        int derivative = proportional - last_proportional;
        integral += proportional;

        // Remember the last position.
        last_proportional = proportional;

        // Compute the difference between the two motor power settings,
        // m1 - m2.  If this is a positive number the robot will turn
        // to the left.  If it is a negative number, the robot will
        // turn to the right, and the magnitude of the number determines
        // the sharpness of the turn.
        int power_difference = proportional/20 + integral/10000 + derivative*3/2;

        // Compute the actual motor settings.  We never set either motor
        // to a negative value.
        const int max = 60; // the maximum speed
        if(power_difference > max)
            power_difference = max;
        if(power_difference < -max)
            power_difference = -max;

        if(power_difference < 0)
            set_motors(max+power_difference,max);
        else
            set_motors(max,max-power_difference);

        // We use the inner three sensors (1, 2, and 3) for
        // determining whether there is a line straight ahead, and the
        // sensors 0 and 4 for detecting lines going to the left and
        // right.

        if(sensors[1] < 100 && sensors[2] < 100 && sensors[3] < 100)
        {
            // There is no line visible ahead, and we didn't see any
            // intersection.  Must be a dead end.
            return;
        }
        else if(sensors[0] > 200 || sensors[4] > 200)
        {
            // Found an intersection.
            return;
        }

    }
}

Between the PID code and the intersection detection, there are now about six more parameters that could be adjusted. We’ve picked values here that allow 3pi to solve the maze at a safe, controlled speed; try increasing the speed and you will quickly run in to lots of problems that you’ll have to handle with more complicated code.

PIDコードと交差点検出の間に、調整可能パラメタがさらに6つあります。ここでは、3piが安全で制御された速度で迷路を解くことができる値を取り上げました。速度を上げてみると、すぐに、もっと複雑なコードで対処しなければならない多くの問題に遭遇するでしょう。

Putting the C files and header files into your project is easy with Atmel Studio. On the right side of your screen, in the “Solution Explorer” pane, you should see a list of files in your project. Right click on the name of your project and you will have the option to add files to the list. When you build your project, Atmel Studio will automatically compile all C files in the project together to produce a single hex file.

Atmel Studio であなたのプロジェクトにCファイルとヘッダーファイルを追加することは簡単です。画面の右側の "Solution Explorer" ペインにあなたのプロジェクトのファイルのリストが表示されています。プロジェクト名を右クリックすると、リストにファイルを追加するオプションがあります。プロジェクトをビルドするときに、 Atmel Studio はプロジェクト内のすべてのCファイルをまとめて自動的にコンパイルして、ひとつのhexファイルを生成します。

0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up