Revolution PiでLチカまで! パート3

C言語でLチカ

RevPi DIO

パート2ではターミナルからLEDの点灯消灯を行いました。今回はモジュールを追加してスイッチも扱います!

追加するモジュールは『RevPi Digital I/O モジュール』(以下DIOモジュール)です。

入力-14 出力-14となっています。

RevPi DIOモジュール

より詳しくは Kunbusで↓

https://revolution.kunbus.com/io-modules/
Kunbus DIOモジュールチュートリアル

ドイツ語(英字のテロップもあり)動画でも説明動画があります↓

Kunbus ビデオチュートリアル DIOモジュール

Revlution Pi と DIOモジュールの接続は 

左側DIOモジュール 右側Revolution Pi  

となるように接続します。逆に取り付けると壊れる危険があるとのことなので注意が必要です。

Revolution Pi と DIOモジュール

DIOモジュールRevolution Piの接続はPiBridgeと呼ばれるプラグで行います。両機体の上部に橋をかけるように接続します。

PiBridgeの画像
PiBridgeで接続した画像

イッチとLEDを配線する

LEDとスイッチ
24V使用のLED

DIOモジュールへの配線を行う際のピンの割り当てなどの情報はKunbusより↓

https://revolution.kunbus.com/tutorials/overview-revpi-io-modules/digital-in-and-outputs/
DIOモジュール 各ピンの情報

下の写真のように配線し、取り付けました。

Revolutio Pi と フィッシャーテクニックのLED・スイッチ

PiCtoryで準備

Revolution Pi に電源を入れ、PiCtoryにログインします。

Revolution Piの電源の入れ方や、PiCtoryの起動はパート2で行っていますのでわからない方はぜひそちらをご覧ください。

PiCtory設定画面

画面左側端にある、【Device Catalog】の中からドラッグアンドドロップで Revolution Pi Core3の画像の左側へ配置していきます。

【I/O Devices】の中にある RevPi DIO をドラッグアンドドロップします。

左側DIOモジュール 右側Revolution Pi

となるように配置します。 

DIOモジュールの場所
左側にDIOモジュール 右側にRaspnerry Pi Core3

画面下の方にある『Value Editor』の【Typename】を把握しておきます。

今回は下画像内の 『I_1』と『O_1』を指定して使用します。

Input
Output

C言語でLチカ

とうとうプログラミングでLチカです!

Kunbusのホームページにあるダウンロードページにはサンプルプログラムがあるのでそれを使用します。

【Sample codes】の中の Sample code from video tutorial no. 13 というファイルをダウンロードします。『Sample code used in our video tutorial no. 13 “PiTest Sourcecode”.』と書かれているファイルです。

ダウンロードしたファイルZipファイルになっているので解凍する必要があります。

https://revolution.kunbus.com/tutorials/downloads/
Kunbus サンプルプダウンロードページ
解凍したフォルダの中身

次はRevolution Pi の【demoフォルダ】中に解凍したファイルを転送、配置します。

demoフォルダの中

LightSwitch.c中身

  1. /*=======================================================================================
  2.  *
  3.  *     KK KK UU UU NN NN BBBBBB UU UU SSSSSS
  4.  *     KK KK UU UU NNN NN BB BB UU UU SS
  5.  *     KK KK UU UU NNNN NN BB BB UU UU SS
  6.  *    +----- KKKKK UU UU NN NN NN BBBBB UU UU SSSSS
  7.  *    | KK KK UU UU NN NNNN BB BB UU UU SS
  8.  *    | KK KK UU UU NN NNN BB BB UU UU SS
  9.  *    | KK KKK UUUUUU NN NN BBBBBB UUUUUU SSSSSS GmbH
  10.  *    |
  11.  *    | [#] I N D U S T R I A L C O M M U N I C A T I O N
  12.  *    | |
  13.  *    +-------------+
  14.  *
  15.  *---------------------------------------------------------------------------------------
  16.  *
  17.  * (C) KUNBUS GmbH, Heerweg 15C, 73770 Denkendorf, Germany
  18.  *
  19.  * This program is free software; you can redistribute it and/or modify
  20.  * it under the terms of the GNU General Public License V2 as published by
  21.  * the Free Software Foundation
  22.  *
  23.  * This program is distributed in the hope that it will be useful,
  24.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  25.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  26.  * GNU General Public License for more details.
  27.  *
  28.  * For licencing details see COPYING
  29.  *
  30.  *=======================================================================================
  31.  */
  32. #include <piControlIf.h>
  33. #include <piControl.h>
  34. #include <string.h>
  35. #include <stdio.h>
  36. int main(int argc, char ** argv)
  37. {
  38.     int i = 0;
  39.     int iLastInputValue = 0;
  40.     char *pchInput = NULL;
  41.     char *pchOutput = NULL;
  42.     // structures containing variable information: Name, Offset, Bit, Length
  43.     SPIVariable spiVariableIn = {"", 0, 0, 0};
  44.     SPIVariable spiVariableOut = {"", 0, 0, 0};
  45.     // structures containing variable value: Offset, Bit, Value
  46.     SPIValue sValueIn = {0, 0, 0};
  47.     SPIValue sValueOut = {0, 0, 0};
  48.     if(argc != 3)
  49.     {
  50.         printf("Usage: %s <PiCtory Input Pin> <PiCtory Output Pin>\n"
  51.                " i.e. %s Input_Pin_1 Output_Pin_1\n", argv[0], argv[0]);
  52.         return 0;
  53.     }
  54.     pchInput = argv[1];    // PiCtory input pin for Switch
  55.     pchOutput = argv[2];    // PiCtory output pin for Light
  56.     strncpy(spiVariableIn.strVarName, pchInput, sizeof(spiVariableIn.strVarName));
  57.     strncpy(spiVariableOut.strVarName, pchOutput, sizeof(spiVariableOut.strVarName));
  58.     i = piControlGetVariableInfo(&spiVariableIn);        // PiBridge - get variable info
  59.     if(0 != i)                                            // handle error
  60.     {
  61.         fprintf(stderr, "Error: piControlGetVariableInfo() returned %d for variable '%s' \n",
  62.             i, spiVariableIn.strVarName);
  63.         return -1;
  64.     }
  65.     i = piControlGetVariableInfo(&spiVariableOut);        // PiBridge - get variable info
  66.     if(0 != i)                                            // handle error
  67.     {
  68.         fprintf(stderr, "Error: piControlGetVariableInfo() returned %d for variable '%s' \n",
  69.             i, spiVariableOut.strVarName);
  70.         return -1;
  71.     }
  72.     sValueIn.i16uAddress = spiVariableIn.i16uAddress;
  73.     sValueIn.i8uBit = spiVariableIn.i8uBit;
  74.     sValueIn.i8uValue = 0;
  75.     sValueOut.i16uAddress = spiVariableOut.i16uAddress;
  76.     sValueOut.i8uBit = spiVariableOut.i8uBit;
  77.     sValueOut.i8uValue = 0;
  78.     printf("%s is running waiting for switch '%s' \n", argv[0], pchInput);
  79.     
  80.     while(1)
  81.     {
  82.         i = piControlGetBitValue(&sValueIn);            // PiBridge - read input pin
  83.         if(0 != i)                                        // handle error
  84.         {
  85.             fprintf(stderr, "Error: piControlGetBitValue() returned %d\n", i);
  86.             return -1;
  87.         }
  88.         if(iLastInputValue != sValueIn.i8uValue)        // if button state changed
  89.         {                                                // show the change
  90.             printf("%-32s : %d \n", pchInput, sValueIn.i8uValue);
  91.             if(0 == sValueIn.i8uValue)                    // if button is released
  92.             {
  93.                 if(1 == iLastInputValue)                // and was pressed before
  94.                 {                                        // switch light
  95.                     sValueOut.i8uValue = ~sValueOut.i8uValue;
  96.                                                         // PiBridge - set output pin
  97.                     i = piControlSetBitValue(&sValueOut);
  98.                     printf("%-32s : %s \n", pchOutput, sValueOut.i8uValue ? "On" : "Off");
  99.                     if(0 != i)
  100.                     {
  101.                         fprintf(stderr, "Error: piControlSetBitValue() returned %d\n", i);
  102.                         return -1;
  103.                     }
  104.                 }
  105.             }
  106.         }
  107.         
  108.         iLastInputValue = sValueIn.i8uValue;            // remember last input value
  109.     }
  110.     return 0;
  111. }

次ははRevolution Piのターミナルへ

ターミナル画面

lsでフォルダの中を確認後、demoフォルダへ移動しフォルダのにLightSwitch.cがあることを確認

ls
cd demo
ls
LightSwitch.cの確認

ファイル名をLightSwitch.c から piTest.c へと変更します。もともとある piTest.c は別のファイル名にと変更しておきます。今回はold_piTestとします。

piTest.c →old_piTest.c

LightSwitch.c → piTest.c へと変更します。

mv piTest old_piTest
mv LightSwitch.c piTest.c

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

ファイル名を変更

プログラムを実行します。PiCtoryでのTypenameを指定します。

make
./piTest I_1 O_1

できました!

しっかりと点灯消灯しています!

https://twitter.com/inFT37207295/status/1427914246931685376
完成の実行動画

http://Amazon.co.jpアソシエイト・プログラムの参加者です。

コメントする

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