RaspberryとArduinoの無線通信

nRF24L01(その2)

前回、nRF24L01モジュールを使ってRaspberryとArduinoの 1対1の無線通信を紹介しましたが、
今回は複数のArduinoとの通信を試してみました。
以下のようなイメージになります。



【Raspberry側】

Raspberry側のコード(RecvMulti.cpp)は以下の通りです。
2Nodeと3Nodeの2つのアドレスからデータを受信する設定です。
#include <cstdlib>
#include <iostream>
#include <sstream>
#include <string>
#include <RF24/RF24.h>


using namespace std;
//
// Hardware configuration
//

// CE Pin, CSN Pin, SPI Speed

// Setup for GPIO 22 CE and CE1 CSN with SPI Speed @ 4Mhz
//RF24 radio(RPI_V2_GPIO_P1_22, BCM2835_SPI_CS1, BCM2835_SPI_SPEED_4MHZ);

// Setup for GPIO 22 CE and CE0 CSN with SPI Speed @ 4Mhz
RF24 radio(RPI_V2_GPIO_P1_22, BCM2835_SPI_CS0, BCM2835_SPI_SPEED_4MHZ);

// Setup for GPIO 22 CE and CE0 CSN with SPI Speed @ 8Mhz
//RF24 radio(RPI_V2_GPIO_P1_22, BCM2835_SPI_CS0, BCM2835_SPI_SPEED_8MHZ);

// Radio pipe addresses for the 2 nodes to communicate.
const uint8_t pipes[][6] =
        {"1Node", "2Node", "3Node"};
//      {"1Node", "2Node", "3Node", "4Node", "5Node", "6Node"};

int main(int argc, char** argv){

  // Setup and configure rf radio
  radio.begin();
  radio.openWritingPipe(pipes[0]);
  radio.openReadingPipe(1,pipes[1]);
  radio.openReadingPipe(2,pipes[2]);
#if 0
  radio.openReadingPipe(3,pipes[3]);
  radio.openReadingPipe(4,pipes[4]);
  radio.openReadingPipe(5,pipes[5]);
#endif
  radio.startListening();
  radio.printDetails();

  // forever loop
  uint8_t pipe;
  while (1) {
    // if there is data ready
    if (radio.available(&pipe)) {
      // Dump the payloads until we've gotten everything
      unsigned long got_time;

      // Fetch the payload.
      radio.read(&got_time, sizeof(unsigned long));

      // Spew it
      printf("Got pipe=%d payload(%d) %lu...\n", pipe, sizeof(unsigned long), got_time);
      radio.stopListening();

      radio.write(&got_time, sizeof(unsigned long));

      // Now, resume listening so we catch the next packets.
      radio.startListening();

      delay(925); //Delay after payload responded to, minimize RPi CPU time

    }

  } // end while
}


examples_linuxディレクトリにあるMakefileを修正し、PROGRAMS変数の末尾に以下を追加します。
PROGRAMS = gettingstarted ..... RecvMulti

nRF24L01とRaspberryの結線は前回と同じですが、 nRF24L01に対する設定値を変更する場合、
一度nRF24L01の電源を入れなおす必要が有ります。
これをしないと、nRF24L01のレジスターが初期化されません。
モジュールを接続して実行すると以下の表示となります。
$ make
$ sudo ./RecvMulti



【Arduino側】

今回、2台のArduinoを使用します。
1台目のArduinoのスケッチは以下の内容です。
2Nodeのアドレスに送信し、応答は1Nodeのアドレスで受け取ります。
/*
 * nRF24L01 Send Sample Program
 *
 * VCC  3.3V
 * MISO 12
 * MOSI 11
 * SCK  13
 * CE   7
 * CSN  8
 *
 */
#include <SPI.h>
#include "RF24.h"

/* Hardware configuration: Set up nRF24L01 radio on SPI bus plus pins 7 & 8 */
RF24 radio(7,8);

byte addresses[][6] = {"1Node","2Node"};

void setup() {
  Serial.begin(115200);
 
  radio.begin();

  // Set the PA Level low to prevent power supply related issues since this is a
  // getting_started sketch, and the likelihood of close proximity of the devices. RF24_PA_MAX is default.
  radio.setPALevel(RF24_PA_LOW);

  // Open a writing and reading pipe on each radio, with opposite addresses
  radio.openWritingPipe(addresses[1]);
  radio.openReadingPipe(1,addresses[0]);

  // Start the radio listening for data
  radio.startListening();
}

void loop() {
  // First, stop listening so we can talk.
  radio.stopListening();
  Serial.println(F("Now sending"));

  // Take the time, and send it.  This will block until complete
  unsigned long start_time = micros();
  if (!radio.write( &start_time, sizeof(unsigned long) )){
    Serial.println(F("failed"));
  }
     
  radio.startListening();                         // Now, continue listening
 
  unsigned long started_waiting_at = micros();    // Set up a timeout period, get the current microseconds
  boolean timeout = false;                        // Set up a variable to indicate if a response was received or not
 
  while ( ! radio.available() ){                  // While nothing is received
    if (micros() - started_waiting_at > 200000 ){ // If waited longer than 200ms, indicate timeout and exit while loop
      timeout = true;
      break;
    }     
  }
     
  if ( timeout ){                                 // Describe the results
    Serial.println(F("Failed, response timed out."));
  }else{
    unsigned long got_time;                       // Grab the response, compare, and send to debugging spew
    radio.read( &got_time, sizeof(unsigned long) );
    unsigned long end_time = micros();
     
    // Spew it
    Serial.print(F("Sent "));
    Serial.print(start_time);
    Serial.print(F(", Got response "));
    Serial.print(got_time);
    Serial.print(F(", Round-trip delay "));
    Serial.print(end_time-start_time);
    Serial.println(F(" microseconds"));
  }

  // Try again 1s later
  delay(1000);


} // Loop

2台目のArduinoのスケッチは赤字の部分だけ変更します。
3Nodeのアドレスに送信し、応答は1Nodeのアドレスで受け取ります。
/*
 * nRF24L01 Send Sample Program
 *
 * VCC  3.3V
 * MISO 12
 * MOSI 11
 * SCK  13
 * CE   7
 * CSN  8
 *
 */
#include <SPI.h>
#include "RF24.h"

// Set up nRF24L01 radio on SPI bus plus pins 7 & 8
RF24 radio(7, 8); // CE,CSN

// Radio pipe addresses for the 2 nodes to communicate.
byte addresses[][6] = {"1Node","3Node"};

{以下省略}


最初に1台目のArduinoでスケッチを実行するとRaspberry側の表示が以下のようになります。
pipe番号がArduinoの番号です。
きちんとデータが受信できています。


Arduno側も送ったデータ(Sent)をそのまま応答(Got)として受け取っています。


次に2台目のArdinoでスケッチを実行すると、Raspberry側では2台目からのデータ受信が正しく動きません。


2台目のArduinoではほとんどデータが送信できていません。




送信側で送信が完了したか失敗したかの判定はEnhanced ShockBurstを使っています。
ArduinoがUNO1台の時は
・1Node(UNO)→2Node(Rpi)に送信
・2Node(Rpi)→1Node(UNO)にACK応答
となり正常に動きますが、ArduinoがUNOとNANOの2台になると
・1Node(UNO)→2Node(Rpi)に送信
・2Node(Rpi)→1Node(UNO)にACK応答
・3Node(NANO)→2Node(Rpi)に送信
・2Node(Rpi)→1Node(UNO)にACK応答
となるので、NANOはACK応答を正しく受信できなくなります。
UNOも期待していないACKがいきなり飛び込んでくるので、送信がエラーとなります。

ダイナミックに送受信を切り替えて双方向の通信を行うときは、1対1にする必要が有ります。
このあたりのことは最初にnRF24L01のドキュメントを読んでおかないと理屈が分かりません。

続く...