RaspberryとArduinoの無線通信

nRF24L01(その5)

こちらで、複数台のArduinoから1台のRaspberryへの双方向通信を 試しましたが、
Enhanced ShockBurstの制限で、1対nの環境では、Raspberryは正しく応答を返すことができません。




今度は逆に、1台のArduinoから複数台のRaspberryへの双方向通信を試してみます。





【Raspberry側】

1台目のRaspberry側のコード(RecvSimple.cpp)は以下の様になります。
2Nodeのアドレスから受信したデータを1Nodeのアドレスに応答します。
Enhanced ShockBurstを使う場合、送信側のReadingPipeの0番目は空けておく必要が有ります。
ライブラリの中でReadingPipeの0番目に送信元のアドレス(Ackを待つアドレス)を設定しています。
#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"};

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

  // Setup and configure rf radio
  radio.begin();
  radio.openWritingPipe(pipes[0]);
  radio.openReadingPipe(1,pipes[1]); // 0番目は空けておく
  radio.startListening();
  radio.printDetails();

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

      // Fetch the payload, and see if this was the last one.
      while (radio.available()) {
        radio.read(&got_time, sizeof(unsigned long));
      }
      radio.stopListening();

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

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

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

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

    }

  } // end while
}

2台目のRaspberry側のコード(RecvSimple.cpp)は赤字の部分だけ変更します。
3Nodeのアドレスから受信したデータを1Nodeのアドレスに応答します。
#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", "3Node"};

{以下省略}


実行する前には必ずnRF24L01の電源を入れなおしてレジスターをクリアする必要が有ります。

【Arduino側】

Arduinoのスケッチは以下の通りです。
2Nodeのアドレスと3Nodeのアドレスに交互に送信し、応答は1Nodeで待ちます。
/*
 * nRF24L01 Sample Program (Send to 2 Server)
 *
 * 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);

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


void setup(void)
{
  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 radio_send(unsigned long put_time) {
  // 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( &put_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(put_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"));
  }
 
}

void loop(void)
{
  static unsigned long put_time = 0;

  // Open a writing and reading pipe on each radio, with opposite addresses
  if ((put_time % 2) == 0) {
    radio.openWritingPipe(addresses[1]);
    radio.openReadingPipe(1,addresses[0]);
  } else {
    radio.openWritingPipe(addresses[2]);
    radio.openReadingPipe(1,addresses[0]);
  }

  radio_send(put_time);
  put_time++;
  // Try again 1s later
  delay(1000);
}


Arduinoでスケッチを実行すると1台目のRaspberryは以下のように、偶数のデータだけを受信します。



2台目のRaspberryは以下のように奇数のデータだけを受信します。
受信側を自由に選べるのも、このモジュールの特徴です。


Arduinoはきちんと両方のRaspberryからの応答を受けています。


Enhanced ShockBurstのAckを待つArduinoが1台だけなので、正しく動きます。
Arduino(1Node) RPi#1(2Node) RPi#2(3Node)
偶数データを2Nodeに送信
受信

Ack受信
1NodeにAck応答

奇数データを3Nodeに送信

受信
Ack受信

1NodeにAck応答



Arduinoのスケッチを少し変えるだけで、両方のRaspberryに同じデータを送信することができます。
 /*
 * nRF24L01 Sample Program (Send to 2 Server at One Time)
 *
 * 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);

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


void setup(void)
{
  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 radio_send(unsigned long put_time) {
  // 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( &put_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(put_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"));
  }
 
}

void loop(void)
{
  static unsigned long put_time = 0;

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

  radio.openWritingPipe(addresses[2]);
  radio.openReadingPipe(1,addresses[0]);
  radio_send(put_time);
 
  put_time++;
  // Try again 1s later
  delay(1000);
}

どちらのRaspberryも同じ値を受信します。




Arduinoは両方からの応答を受信しています。


続く...