RevolutionPi & fischertechnik パンチングマシン パート1

ンチングマシンについて

フィッシャーテクニック・エデュケーションオリジナルのパンチングマシンスクールバージョン

パンチングマシンはフィッシャーテクニックの数あるシリーズの中の、『トレーニングシリーズ』というプログラミングを学ぶ方向けの製品の一つです。

パンチングマシンスクールバージョンの詳しい動画はYoutubeで公開中です!

こだわりや特徴、パンチングマシン(スクールバージョンでない)との違いなども詳しく載っています!

パンチングマシンスクールバージョン

イッチボード

今回はRevolutionPI(以下レボパイ)を使ってパンチングマシンを動かしますが、

いきなりパンチングマシンを動作させるのではなく、スイッチ×4 LED×4スイッチボードを使用して入出力を割り当てるプログラムを作成してからパンチングマシンに挑みます!

フィッシャーテクニック・エデュケーションのオリジナル スイッチボード

スイッチボードの詳しい内容もYoutubeで公開中です!

配線についてなども詳しく動画内で説明があるのでぜひ!

スイッチボードについてのYoutube動画

スイッチボードを実際に使用し動画はTwitterにも載せています!

レボパイでスイッチボードを使用したツイート

C言語で実際にプログラミング

ここからは実際にC言語でプログラミングを行っていきます!

初めてレボパイを触れる方や何をやっているのかわからない方は以前投稿した『RevoluitonPiでLチカ』のシリーズを見ていただけると理解しやすくなるかもしれません。

レボパイに電源を入れます

レボパイのデスクトップ画面

フォルダを開き、demoフォルダの中の『piTest.c』を開きます

demoフォルダ内のファイル

現在のプログラムは『入力×1』と『出力×1』のプログラムです。

このプログラムを『入力×4』・『出力×4』のプログラムへと変更していきます。

まずは入力・出力がそれぞれ1つのコードを確認!


  1. // === 1 ====
  2.     int i = 0;
  3.     int iLastInputValue = 0;
  4.     char *pchInput = NULL;
  5.     char *pchOutput = NULL;
  6.     // structures containing variable information: Name, Offset, Bit, Length
  7.     SPIVariable spiVariableIn = {"", 0, 0, 0};
  8.     SPIVariable spiVariableOut = {"", 0, 0, 0};
  9.     // structures containing variable value: Offset, Bit, Value
  10.     SPIValue sValueIn = {0, 0, 0};
  11.     SPIValue sValueOut = {0, 0, 0};

このコードを基にして 2~4まで合計3つ追加します。


  1. // === 2 ===
  2.     int i2 = 0;
  3.     int iLastInputValue2 = 0;
  4.     char *pchInput2 = NULL;
  5.     char *pchOutput2 = NULL;
  6.     // structures containing variable information: Name, Offset, Bit, Length
  7.     SPIVariable spiVariableIn2 = {"", 0, 0, 0};
  8.       SPIVariable spiVariableOut2 = {"", 0, 0, 0};
  9.     // structures containing variable value: Offset, Bit, Value
  10.     SPIValue sValueIn2 = {0, 0, 0};
  11.     SPIValue sValueOut2 = {0, 0, 0};
  12. //=== 3 ===
  13.     int i3 = 0;
  14.     int iLastInputValue3 = 0;
  15.     char *pchInput3 = NULL;
  16.     char *pchOutput3 = NULL;
  17.     // structures containing variable information: Name, Offset, Bit, Length
  18.     SPIVariable spiVariableIn3 = {"", 0, 0, 0};
  19.     SPIVariable spiVariableOut3 = {"", 0, 0, 0};
  20.     // structures containing variable value: Offset, Bit, Value
  21.     SPIValue sValueIn3 = {0, 0, 0};
  22.     SPIValue sValueOut3 = {0, 0, 0};
  23. // === 4 ====
  24.     int i4 = 0;
  25.     int iLastInputValue4 = 0;
  26.     char *pchInput4 = NULL;
  27.     char *pchOutput4 = NULL;
  28.     // structures containing variable information: Name, Offset, Bit, Length
  29.     SPIVariable spiVariableIn4 = {"", 0, 0, 0};
  30.     SPIVariable spiVariableOut4 = {"", 0, 0, 0};
  31.     // structures containing variable value: Offset, Bit, Value
  32.     SPIValue sValueIn4 = {0, 0, 0};
  33.     SPIValue sValueOut4 = {0, 0, 0};

これから作成する関数のプロトタイプ宣言

  1. int IO_1(void);
  2. int IO_2(void);
  3. int IO_3(void);
  4. int IO_4(void);

そして、入力のチェックと出力を行うプログラムを記述した関数

これを基にしてまた2~4を作成します。

  1. // ----------- 1 --------------
  2. int IO_1(void) {
  3.         i = piControlGetBitValue(&sValueIn);// PiBridge - read input pin
  4.         if(0 != i)            // handle error
  5.         {
  6.             fprintf(stderr, "Error: piControlGetBitValue() returned %d\n", i);
  7.             return -1;
  8.         }
  9.         if(iLastInputValue != sValueIn.i8uValue)        // if button state changed
  10.         {    // show the change
  11.             printf("%-32s : %d \n", pchInput, sValueIn.i8uValue);
  12.             if(0 == sValueIn.i8uValue) // if button is released
  13.             {
  14.                 if(1 == iLastInputValue) // and was pressed before
  15.                 {    // switch light
  16.                     sValueOut.i8uValue = ~sValueOut.i8uValue;
  17.                 // PiBridge - set output pin
  18.                     i = piControlSetBitValue(&sValueOut);
  19.                     printf("%-32s : %s \n", pchOutput, sValueOut.i8uValue ? "On" : "Off");
  20.                     if(0 != i)
  21.                     {
  22.                         fprintf(stderr, "Error: piControlSetBitValue() returned %d\n", i);
  23.                         return -1;
  24.                     }
  25.                 }
  26.             }
  27.         }
  28.         iLastInputValue = sValueIn.i8uValue;    // remember last input value    
  29. }
  1. // ----------- 2 --------------
  2. int IO_2(void) {
  3.         i2 = piControlGetBitValue(&sValueIn2); // PiBridge - read input pin
  4.         if(0 != i2)    // handle error
  5.         {
  6.             fprintf(stderr, "Error: piControlGetBitValue() returned %d\n", i2);
  7.             return -1;
  8.         }
  9.         if(iLastInputValue2 != sValueIn2.i8uValue)        // if button state changed
  10.         {        // show the change
  11.             printf("%-32s : %d \n", pchInput2, sValueIn2.i8uValue);
  12.             if(0 == sValueIn2.i8uValue)     // if button is released
  13.             {
  14.                 if(1 == iLastInputValue2) // and was pressed before
  15.                 {                // switch light
  16.                     sValueOut2.i8uValue = ~sValueOut2.i8uValue;
  17.                         // PiBridge - set output pin
  18.                     i2 = piControlSetBitValue(&sValueOut2);
  19.                     printf("%-32s : %s \n", pchOutput2, sValueOut2.i8uValue ? "On" : "Off");
  20.                     if(0 != i)
  21.                     {
  22.                         fprintf(stderr, "Error: piControlSetBitValue() returned %d\n", i2);
  23.                         return -1;
  24.                     }
  25.                 }
  26.             }
  27.         }
  28.         iLastInputValue2 = sValueIn2.i8uValue;    // remember last input value    
  29. }
  30. // ----------- 3 --------------
  31. int IO_3(void) {
  32.         i3 = piControlGetBitValue(&sValueIn3); // PiBridge - read input pin
  33.         if(0 != i3)        // handle error
  34.         {
  35.             fprintf(stderr, "Error: piControlGetBitValue() returned %d\n", i3);
  36.             return -1;
  37.         }
  38.         if(iLastInputValue3 != sValueIn3.i8uValue)        // if button state changed
  39.         {        // show the change
  40.             printf("%-32s : %d \n", pchInput3, sValueIn3.i8uValue);
  41.             if(0 == sValueIn3.i8uValue)     // if button is released
  42.             {
  43.                 if(1 == iLastInputValue3)    // and was pressed before
  44.                 {            // switch light
  45.                     sValueOut3.i8uValue = ~sValueOut3.i8uValue;
  46.                         // PiBridge - set output pin
  47.                     i3 = piControlSetBitValue(&sValueOut3);
  48.                     printf("%-32s : %s \n", pchOutput3, sValueOut3.i8uValue ? "On" : "Off");
  49.                     if(0 != i3)
  50.                     {
  51.                         fprintf(stderr, "Error: piControlSetBitValue() returned %d\n", i3);
  52.                         return -1;
  53.                     }
  54.                 }
  55.             }
  56.         }
  57.         iLastInputValue3 = sValueIn3.i8uValue;    // remember last input value    
  58. }
  59. // ----------- 4--------------
  60. int IO_4(void) {
  61.         i4 = piControlGetBitValue(&sValueIn4);    // PiBridge - read input pin
  62.         if(0 != i4)    // handle error
  63.         {
  64.             fprintf(stderr, "Error: piControlGetBitValue() returned %d\n", i4);
  65.             return -1;
  66.         }
  67.         if(iLastInputValue4 != sValueIn4.i8uValue)        // if button state changed
  68.         {        // show the change
  69.             printf("%-32s : %d \n", pchInput4, sValueIn4.i8uValue);
  70.             if(0 == sValueIn4.i8uValue)        // if button is released
  71.             {
  72.                 if(1 == iLastInputValue4)    // and was pressed before
  73.                 {    // switch light
  74.                     sValueOut4.i8uValue = ~sValueOut4.i8uValue;
  75.                                // PiBridge - set output pin
  76.                     i4 = piControlSetBitValue(&sValueOut4);
  77.                     printf("%-32s : %s \n", pchOutput4, sValueOut4.i8uValue ? "On" : "Off");
  78.                     if(0 != i4)
  79.                     {
  80.                         fprintf(stderr, "Error: piControlSetBitValue() returned %d\n", i4);
  81.                         return -1;
  82.                     }
  83.                 }
  84.             }
  85.         }
  86.         iLastInputValue4 = sValueIn4.i8uValue; // remember last input value    
  87. }

以下はmainプログラムです。

  1. int main(int argc, char ** argv)
  2. {    
  3.     pchInput = argv[1];    // PiCtory input pin for Switch
  4.     pchOutput = argv[2];    // PiCtory output pin for Light
  5.     pchInput2 = argv[3];    // PiCtory input pin for Switch
  6.     pchOutput2 = argv[4];    // PiCtory output pin for Light
  7.     pchInput3 = argv[5];    // PiCtory input pin for Switch
  8.     pchOutput3 = argv[6];    // PiCtory output pin for Light
  9.     pchInput4 = argv[7];    // PiCtory input pin for Switch
  10.     pchOutput4 = argv[8];    // PiCtory output pin for Light
  11.     strncpy(spiVariableIn.strVarName, pchInput, sizeof(spiVariableIn.strVarName));
  12.     strncpy(spiVariableOut.strVarName, pchOutput, sizeof(spiVariableOut.strVarName));
  13.     strncpy(spiVariableIn2.strVarName, pchInput2, sizeof(spiVariableIn2.strVarName));
  14.     strncpy(spiVariableOut2.strVarName, pchOutput2, sizeof(spiVariableOut2.strVarName));
  15.     strncpy(spiVariableIn3.strVarName, pchInput3, sizeof(spiVariableIn3.strVarName));
  16.     strncpy(spiVariableOut3.strVarName, pchOutput3, sizeof(spiVariableOut3.strVarName));
  17.     strncpy(spiVariableIn4.strVarName, pchInput4, sizeof(spiVariableIn4.strVarName));
  18.     strncpy(spiVariableOut4.strVarName, pchOutput4, sizeof(spiVariableOut4.strVarName));
  19.     i = piControlGetVariableInfo(&spiVariableIn);        // PiBridge - get variable info
  20.     if(0 != i)        // handle error
  21.     {
  22.         fprintf(stderr, "Error: piControlGetVariableInfo() returned %d for variable '%s' \n",
  23.             i, spiVariableIn.strVarName);
  24.         return -1;
  25.     }
  26.     i2 = piControlGetVariableInfo(&spiVariableIn2); // PiBridge - get variable info
  27.     if(0 != i2)            // handle error
  28.     {
  29.         fprintf(stderr, "Error: piControlGetVariableInfo() returned %d for variable '%s' \n",
  30.             i2, spiVariableIn2.strVarName);
  31.         return -1;
  32.     }
  33.     i3 = piControlGetVariableInfo(&spiVariableIn3); // PiBridge - get variable info
  34.     if(0 != i3)            // handle error
  35.     {
  36.         fprintf(stderr, "Error: piControlGetVariableInfo() returned %d for variable '%s' \n",
  37.             i3, spiVariableIn3.strVarName);
  38.         return -1;
  39.     }    
  40.     i4 = piControlGetVariableInfo(&spiVariableIn4); // PiBridge - get variable info
  41.     if(0 != i4)    // handle error
  42.     {
  43.         fprintf(stderr, "Error: piControlGetVariableInfo() returned %d for variable '%s' \n",
  44.             i4, spiVariableIn4.strVarName);
  45.         return -1;
  46.     }    
  47.     // =============================
  48.     i = piControlGetVariableInfo(&spiVariableOut);        // PiBridge - get variable info
  49.     if(0 != i)        // handle error
  50.     {
  51.         fprintf(stderr, "Error: piControlGetVariableInfo() returned %d for variable '%s' \n",
  52.             i, spiVariableOut.strVarName);
  53.         return -1;
  54.     }
  55.     i2 = piControlGetVariableInfo(&spiVariableOut2);        // PiBridge - get variable info
  56.     if(0 != i2)    // handle error
  57.     {
  58.         fprintf(stderr, "Error: piControlGetVariableInfo() returned %d for variable '%s' \n",
  59.             i2, spiVariableOut2.strVarName);
  60.         return -1;
  61.     }
  62.     i3 = piControlGetVariableInfo(&spiVariableOut3);        // PiBridge - get variable info
  63.     if(0 != i3)    // handle error
  64.     {
  65.         fprintf(stderr, "Error: piControlGetVariableInfo() returned %d for variable '%s' \n",
  66.             i3, spiVariableOut3.strVarName);
  67.         return -1;
  68.     }
  69.     i4 = piControlGetVariableInfo(&spiVariableOut4);        // PiBridge - get variable info
  70.     if(0 != i4)    // handle error
  71.     {
  72.         fprintf(stderr, "Error: piControlGetVariableInfo() returned %d for variable '%s' \n",
  73.             i4, spiVariableOut4.strVarName);
  74.         return -1;
  75.     }
  76.     // =============================
  77.     sValueIn.i16uAddress = spiVariableIn.i16uAddress;
  78.     sValueIn.i8uBit = spiVariableIn.i8uBit;
  79.     sValueIn.i8uValue = 0;
  80.     sValueOut.i16uAddress = spiVariableOut.i16uAddress;
  81.     sValueOut.i8uBit = spiVariableOut.i8uBit;
  82.     sValueOut.i8uValue = 0;    
  83.     // --------------- 2
  84.     sValueIn2.i16uAddress = spiVariableIn2.i16uAddress;
  85.     sValueIn2.i8uBit = spiVariableIn2.i8uBit;
  86.     sValueIn2.i8uValue = 0;
  87.     sValueOut2.i16uAddress = spiVariableOut2.i16uAddress;
  88.     sValueOut2.i8uBit = spiVariableOut2.i8uBit;
  89.     sValueOut2.i8uValue = 0;
  90.     // ----------------- 3
  91.     sValueIn3.i16uAddress = spiVariableIn3.i16uAddress;
  92.     sValueIn3.i8uBit = spiVariableIn3.i8uBit;
  93.     sValueIn3.i8uValue = 0;
  94.     sValueOut3.i16uAddress = spiVariableOut3.i16uAddress;
  95.     sValueOut3.i8uBit = spiVariableOut3.i8uBit;
  96.     sValueOut3.i8uValue = 0;
  97.     // ------------------- 4
  98.     sValueIn4.i16uAddress = spiVariableIn4.i16uAddress;
  99.     sValueIn4.i8uBit = spiVariableIn4.i8uBit;
  100.     sValueIn4.i8uValue = 0;
  101.     sValueOut4.i16uAddress = spiVariableOut4.i16uAddress;
  102.     sValueOut4.i8uBit = spiVariableOut4.i8uBit;
  103.     sValueOut4.i8uValue = 0;
  104.     // -----------------
  105.     printf("%s is running waiting for switch '%s' \n", argv[0], pchInput);
  106.     printf("%s is running waiting for switch '%s' \n", argv[1], pchInput2);
  107.     printf("%s is running waiting for switch '%s' \n", argv[2], pchInput3);
  108.     printf("%s is running waiting for switch '%s' \n", argv[3], pchInput4);
  109.     while(1)
  110.     {
  111.         IO_1();
  112.         IO_2();
  113.         IO_3();
  114.         IO_4();
  115.     }
  116.     return 0;
  117. }

プログラムが完成したのでレボパイとスイッチボードをリボンケーブルで接続します

レボパイとリボンケーブル接続

ーミナルで実行

ターミナルをデスクトップ画面から立ち上げます

デスクトップ画面からターミナルの立ち上げ
cd demo

makeコマンド でコンパイルします

make
./piTest I_1 O_1 I_2 O_2 I_3 O_3 I_4 O_4
https://twitter.com/inFT37207295/status/1435096885312712704
スイッチボードレボパイを使用したツイート

スイッチボードの出力は『LED』ですが、DCモーターへと簡単に変更できるので確認も容易でした!

ご覧いただきありがとうございました!

コメントする

メールアドレスが公開されることはありません。 が付いている欄は必須項目です