Arduino Kode hjælp (nRF24l01) (Læst 2933x)

Offline Danni-Hansen

  • µProcessoren
  • *
  • Indlæg: 544
  • Antal brugbare Indlæg: 17
    • Vis profil
Arduino Kode hjælp (nRF24l01)
« Dato: Marts 05, 2015, 12:42:25 »
Hej DEF.

Har fået leget lidt med Earnst her på det sidste, og det er på tide at udvide hans 'funktioner'

Jeg arbejder lidt på at min Afstands sensor skal fortælle ham hvad han skal gøre det næste stykke tid.

Her under ser i modtager koden til Earnst som det ser ud nu (Med hjælp fra Gerd)

Kode:
/**
 * Project nRF24L01 receiver
 *
 * @author Gerd Bartelt - www.sebulli.com
 *
 * @brief Receives commands from a nRF24L01 and controls a motor.
 * @brief Based on the RF24 example "GettingStarted"
 *       
 * @copyright GPL2
 */
 
#include <SPI.h>
#include "nRF24L01.h"
#include "RF24.h"
#include "printf.h"
 
//
// Hardware configuration
//
 
// Set up nRF24L01 radio on SPI bus plus pins 9 & 10
RF24 radio(9,10);
 
// Set up motor on pin 4,5,6 and 7
int right01 = 4;
int right02 = 5;
int left01 = 6;
int left02 = 7;
int frontSensor = 1; // Analog pin 1
 
 
// Radio pipe addresses for the 2 nodes to communicate.
const uint64_t pipes[2] = {
  0xF0F0F0F0E1LL, 0xF0F0F0F0D2LL };
 
void setup(void)
{
 
  // Motor pins as output
  pinMode(right01, OUTPUT);
  pinMode(right02, OUTPUT);
  pinMode(left01, OUTPUT);
  pinMode(left02, OUTPUT);
 
  Serial.begin(57600);
  printf_begin();
 
  // Setup and configure rf radio
  radio.begin();
 
  // Increase the delay between retries & # of retries
  radio.setRetries(15,15);
 
  // Open pipes to other nodes for communication
  radio.openWritingPipe(pipes[1]);
  radio.openReadingPipe(1,pipes[0]);
 
  // Start listening
  radio.startListening();
 
  // Dump the configuration of the rf unit for debugging
  radio.printDetails();
}
 
void loop(void)
{
 
  // Receive each packet, dump it out, and send it back
  // if there is data ready
  if ( radio.available() )
  {
    // Dump the payloads until we've gotten everything
    char got_kore_info;
    bool done = false;
    while (!done)
    {
      /*
      //Distance sensor - If less than 10 cm to obj, turn around!
      int read_frontSensor = analogRead(frontSensor);
      int result_frontSensor = (4800/(read_frontSensor - 20));
     
      if (result_frontSensor <= 10) {
        char got_kore_info = 'A';
      }
      //End of obj turn around
      */
     
      // Fetch the payload, and see if this was the last one.
      done = radio.read( &got_kore_info, sizeof(char) );
 
      // Spew it
      printf("Got command '%c'\n\r",got_kore_info);
 
      // Delay just a little bit to let the other unit
      // make the transition to receiver
      delay(20);
 
 
     // First, stop listening so we can talk
    radio.stopListening();
 
      // decode the received "køre info"
      switch (got_kore_info)
      {
      case '8':
        forwards();
        printf("Forwards, Success",got_kore_info);
      break;
 
      case '2':
        backwards();
        printf("Backwards, Success",got_kore_info);
        break;
 
      case '5':
        around();
        printf("Turning Around, Success",got_kore_info);
        break;
 
      case '0':
        stopper();
        printf("Stopping, Success",got_kore_info);
        break;
 
      }
      delay(2500);
 
 
    }
 
    // Send the final one back.
    radio.write( &got_kore_info, sizeof(char) );

    // Now, resume listening so we catch the next packets.
    radio.startListening();
     
  }
}
 
void forwards(){
  printf("Forwards\n\r");
  digitalWrite(right02, LOW);
  digitalWrite(right01, HIGH);
  digitalWrite(left02, LOW);
  digitalWrite(left01, HIGH);
}
 
void backwards(){
  printf("Backwards\n\r");
  digitalWrite(right01, LOW);
  digitalWrite(right02, HIGH);
  digitalWrite(left01, LOW);
  digitalWrite(left02, HIGH);
}
 
void around(){
  printf("Around\n\r");
  digitalWrite(right01, LOW);
  digitalWrite(right02, HIGH);
  digitalWrite(left02, LOW);
  digitalWrite(left01, HIGH);
 
}
 
void stopper(){
  printf("Stop\n\r");
  digitalWrite(right01, LOW);
  digitalWrite(right02, LOW);
  digitalWrite(left02, LOW);
  digitalWrite(left01, LOW);
}

Jeg ville så gerne have både min sender og modtager (på Earnst) til at modtage strings via nRF24l01 signalet.

Så fandt disse 2 koder:

Den her til at sende strings:
https://gist.githubusercontent.com/shnae/8642916/raw/nrf24l01_send_string

De her til at modtage strings:
https://gist.githubusercontent.com/shnae/8642903/raw/nrf24l01_receive_string

Problemet er mest at jeg aner faktisk ikke hvordan jeg umidelbart skal sætte modtager ind i min nuværende kode.

Det er jo min pc der skal tage beslutningerne, udfra hvad afstanden er, så det eneste Earnst egentligt skal, er at tage imod det han får afvide, også levere en afstand i cm. (Push/Pull)

Nogen der kan hjælpe? :)

Mvh. Danni.
Mvh. Danni Hansen.

 

Offline Danni-Hansen

  • µProcessoren
  • *
  • Indlæg: 544
  • Antal brugbare Indlæg: 17
    • Vis profil
Sv: Arduino Kode hjælp (nRF24l01)
« Svar #1 Dato: Marts 05, 2015, 13:31:02 »
Har bikset den her kode sammen (Til Earnst):

Kode:
/**
 * Project nRF24L01 receiver
 *
 * @author Gerd Bartelt - www.sebulli.com
 *
 * @brief Receives commands from a nRF24L01 and controls a motor.
 * @brief Based on the RF24 example "GettingStarted"
 *       
 * @copyright GPL2
 */
 
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#include <RF24_config.h>
#include "printf.h"
 
//
// Hardware configuration
//
 
// Set up nRF24L01 radio on SPI bus plus pins 9 & 10
RF24 radio(9,10);
 
// Set up motor on pin 4,5,6 and 7
int right01 = 4;
int right02 = 5;
int left01 = 6;
int left02 = 7;
int frontSensor = 1; // Analog pin 1
int msg[1];
 
// Radio pipe addresses for the 2 nodes to communicate.
const uint64_t pipes[2] = {
  0xF0F0F0F0E1LL, 0xF0F0F0F0D2LL };
 
void setup(void)
{
 
  // Motor pins as output
  pinMode(right01, OUTPUT);
  pinMode(right02, OUTPUT);
  pinMode(left01, OUTPUT);
  pinMode(left02, OUTPUT);
  pinMode(frontSensor, INPUT);

  Serial.begin(57600);
  printf_begin();
 
  // Setup and configure rf radio
  radio.begin();
 
  // Increase the delay between retries & # of retries
  radio.setRetries(15,15);
 
  // Open pipes to other nodes for communication
  radio.openWritingPipe(pipes[1]);
  radio.openReadingPipe(1,pipes[0]);
 
  // Start listening
  radio.startListening();
 
  // Dump the configuration of the rf unit for debugging
  radio.printDetails();
}
 
void loop(void)
{
 
  // Receive each packet, dump it out, and send it back
  // if there is data ready
  if ( radio.available() )
  {
    // Dump the payloads until we've gotten everything
    char got_kore_info;
    bool done = false;
    while (!done)
    {

      //Distance sensor - If less than 10 cm to obj, turn around!
      int read_frontSensor = analogRead(frontSensor);
      int result_frontSensor = (4800/(read_frontSensor - 20));
     
      send_distance(result_frontSensor);
      stopper();
      radio.powerDown();
      delay(1000);
      radio.powerUp();

      // Fetch the payload, and see if this was the last one.
      done = radio.read( &got_kore_info, sizeof(char) );
 
      // Spew it
      printf("Got command '%c'\n\r",got_kore_info);
 
      // Delay just a little bit to let the other unit
      // make the transition to receiver
      delay(20);
 
 
     // First, stop listening so we can talk
    radio.stopListening();
 
      // decode the received "køre info"
      switch (got_kore_info)
      {
      case '8':
        forwards();
        printf("Forwards, Success",got_kore_info);
      break;
 
      case '2':
        backwards();
        printf("Backwards, Success",got_kore_info);
        break;
 
      case '5':
        around();
        printf("Turning Around, Success",got_kore_info);
        break;
 
      case '0':
        stopper();
        printf("Stopping, Success",got_kore_info);
        break;
 
      }
      delay(2500);
 
 
    }
 
    // Send the final one back.
    radio.write( &got_kore_info, sizeof(char) );

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

void send_distance(int result_frontSensor){
 
      String theMessage = String(result_frontSensor);
      int messageSize = theMessage.length();
      for (int i = 0; i < messageSize; i++) {
        int charToSend[1];
        charToSend[0] = theMessage.charAt(i);
        radio.write(charToSend,1);
      }
     
        msg[0] = 2;
        radio.write(msg,1);
}
 
void forwards(){
  printf("Forwards\n\r");
  digitalWrite(right02, LOW);
  digitalWrite(right01, HIGH);
  digitalWrite(left02, LOW);
  digitalWrite(left01, HIGH);
}
 
void backwards(){
  printf("Backwards\n\r");
  digitalWrite(right01, LOW);
  digitalWrite(right02, HIGH);
  digitalWrite(left01, LOW);
  digitalWrite(left02, HIGH);
}
 
void around(){
  printf("Around\n\r");
  digitalWrite(right01, LOW);
  digitalWrite(right02, HIGH);
  digitalWrite(left02, LOW);
  digitalWrite(left01, HIGH);
 
}
 
void stopper(){
  printf("Stop\n\r");
  digitalWrite(right01, LOW);
  digitalWrite(right02, LOW);
  digitalWrite(left02, LOW);
  digitalWrite(left01, LOW);
}
Mvh. Danni Hansen.