Battlebot m.b.v. nRF24L01 & L298N

Als U een gezamenlijk project wil starten of aan projecten van anderen mee wilt werken.
Berichten: 2
Geregistreerd: 23 Feb 2022, 21:22
Woonplaats: Eindhoven

Re: Battlebot m.b.v. nRF24L01 & L298N

Berichtdoor FPProductions » 02 Apr 2022, 20:03

Het is al even stil.

Ik heb inmiddels de transmitter geüpgrade met een "NRF24L01 + PA + LNA", met de hoop dat het stabieler zou werken.
Maar helaas nog steeds hetzelfde probleem.

Alvast bedankt voor de reacties. :D

Advertisement

Berichten: 2
Geregistreerd: 23 Feb 2022, 21:22
Woonplaats: Eindhoven

Battlebot m.b.v. nRF24L01 & L298N

Berichtdoor FPProductions » 02 Apr 2022, 20:06

Beste mensen,

Ik zal me snel even voorstellen. Ik ben Falco 28 jaar, en ik heb in het verleden een opleiding applicatieontwikkelaar gedaan.
Daarna ben ik Industriële Automatisering gaan studeren, want ik vond Electronica eigenlijk veel interessanter.
Inmiddels ben ik al een tijdje afgestudeerd en werd het eens tijd om een projectje te gaan beginnen.

Nu loop ik tegen wat dingentjes aan, en denk ik dat jullie mij een eind de juiste richting op kunnen sturen.
Wellicht spreekt dit project meerdere mensen aan, en is het zo leuk om een groter topic te starten. :D :D

Mijn idee was om een soort "Battlebot" te ontwikkelen, zoals vele van jullie misschien wel kennen van tv.

Dus stap 1 is om een werkende besturing te maken met behulp van een nRF24L01.

Ik heb als eerste test de code op onderstaande website gebruikt, om "simpel" te kunnen beginnen....
https://dronebotworkshop.com/nrf24l01-wireless-joystick/

De auto is bestuurbaar, alleen heeft hij ongeveer 2 seconde vertraging tussen de input van de TX en de reactie van de motoren.
De serial monitor geeft netjes "sendtoWait failed" , maar ik kan de oorzaak niet gevonden krijgen. Het lijkt mij wel dat ik in deze richting moet zoeken, de "ReliableDatagram" testsketch werkt wel naar behoren.
Er staat al een 470uF elco over de voeding van beide tranceivers, ook heb ik een nRF24L01 adapterboard gebruikt (5v>3.3v spanningsregelaar).

Ik moet eerlijk bekennen dat ik al lang niets meer met Arduino gedaan heb, laat staan draadloze communicatie. :roll:
Waarschijnlijk hebben jullie hier meer ervaring mee dan mij. Alvast bedankt voor de reacties.

Hieronder de code, voor de nieuwschierige onder ons :P :

RX:
Code: Alles selecteren
/*
  nRF24L01+ Joystick Receiver for Robot Car
  nrf24l01-joy-rcv-car.ino
  nRF24L01+ Receiver and L298N driver for Robot Car
  Use with Joystick Transmitter for Robot Car
  DroneBot Workshop 2018
  https://dronebotworkshop.com
*/
 
// Include RadioHead ReliableDatagram & NRF24 Libraries
#include <RHReliableDatagram.h>
#include <RH_NRF24.h>
 
// Include dependant SPI Library
#include <SPI.h>
 
// Define addresses for radio channels
#define CLIENT_ADDRESS 1   
#define SERVER_ADDRESS 2
 
// Motor A Connections
int enA = 9;
int in1 = 14;
int in2 = 4;
 
// Motor B Connections
int enB = 5;
int in3 = 7;
int in4 = 6;
 
// Create an instance of the radio driver
RH_NRF24 RadioDriver;
 
// Sets the radio driver to NRF24 and the server address to 2
RHReliableDatagram RadioManager(RadioDriver, SERVER_ADDRESS);
 
// Define a message to return if values received
uint8_t ReturnMessage[] = "JoyStick Data Received";
 
// Define the Message Buffer
uint8_t buf[RH_NRF24_MAX_MESSAGE_LEN];
 
void setup()
{
  // Setup Serial Monitor
  Serial.begin(9600);
 
  // Set all the motor control pins to outputs
  pinMode(enA, OUTPUT);
  pinMode(enB, OUTPUT);
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);
  pinMode(in3, OUTPUT);
  pinMode(in4, OUTPUT);
 
  // Initialize RadioManager with defaults - 2.402 GHz (channel 2), 2Mbps, 0dBm
  if (!RadioManager.init())
    Serial.println("init failed");
}
 
void loop()
{
  if (RadioManager.available())
  {
 // Wait for a message addressed to us from the client
    uint8_t len = sizeof(buf);
    uint8_t from;
    if (RadioManager.recvfromAck(buf, &len, &from))
 
    {
     
      //Serial Print the values of joystick
      Serial.print("got request from : 0x");
      Serial.print(from, HEX);
      Serial.print(": MotorA = ");
      Serial.print(buf[0]);
      Serial.print(" MotorB = ");
      Serial.print(buf[1]);
      Serial.print(" Dir = ");
      Serial.println(buf[2]);
     
     
      // Set Motor Direction
      if (buf[2] == 1)
      {
    // Motors are backwards
    digitalWrite(in1, LOW);
    digitalWrite(in2, HIGH);
    digitalWrite(in3, LOW);
    digitalWrite(in4, HIGH);
    }else{
    // Motors are forwards
    digitalWrite(in1, HIGH);
    digitalWrite(in2, LOW);
    digitalWrite(in3, HIGH);
    digitalWrite(in4, LOW);
     }
 
     
      // Drive Motors
      analogWrite(enA, buf[1]);
      analogWrite(enB, buf[0]);
     
      // Send a reply back to the originator client, check for error
      if (!RadioManager.sendtoWait(ReturnMessage, sizeof(ReturnMessage), from))
        Serial.println("sendtoWait failed");
    }
  }             


TX:
Code: Alles selecteren
/*
  nRF24L01+ Joystick Transmitter
  nrf24l01-joy-xmit-car.ino
  nRF24L01+ Transmitter with Joystick for Robot Car
  Use with Joystick Receiver for Robot Car
  DroneBot Workshop 2018
  https://dronebotworkshop.com
*/
 
// Include RadioHead ReliableDatagram & NRF24 Libraries
#include <RHReliableDatagram.h>
#include <RH_NRF24.h>
 
// Include dependant SPI Library
#include <SPI.h>
 
// Define Joystick Connections
#define joyVert    A2
#define joyHorz    A3
 
// Define Joystick Values - Start at 512 (middle position)
int joyposVert = 512;
int joyposHorz = 512;
 
// Define addresses for radio channels
#define CLIENT_ADDRESS 1   
#define SERVER_ADDRESS 2
 
// Create an instance of the radio driver
RH_NRF24 RadioDriver;
 
// Sets the radio driver to NRF24 and the client address to 1
RHReliableDatagram RadioManager(RadioDriver, CLIENT_ADDRESS);
 
// Declare unsigned 8-bit motorcontrol array
// 2 Bytes for motor speeds plus 1 byte for direction control
uint8_t motorcontrol[3];
 
// Define the Message Buffer
uint8_t buf[RH_NRF24_MAX_MESSAGE_LEN];
 
void setup()
{
  // Setup Serial Monitor
  Serial.begin(9600);
 
  // Initialize RadioManager with defaults - 2.402 GHz (channel 2), 2Mbps, 0dBm
  if (!RadioManager.init())
    Serial.println("init failed");
 
  // Set initial motor direction as forward
  motorcontrol[2] = 0;
 
}
 
void loop()
{
  // Print to Serial Monitor
  Serial.println("Reading motorcontrol values ");
 
  // Read the Joystick X and Y positions
  joyposVert = analogRead(joyVert);
  joyposHorz = analogRead(joyHorz);
 
  // Determine if this is a forward or backward motion
  // Do this by reading the Verticle Value
  // Apply results to MotorSpeed and to Direction
 
  if (joyposVert < 460)
  {
    // This is Backward
    // Set Motors backward
    motorcontrol[2] = 1;
 
    //Determine Motor Speeds
    // As we are going backwards we need to reverse readings
    motorcontrol[0] = map(joyposVert, 460, 0, 0, 255);
    motorcontrol[1] = map(joyposVert, 460, 0, 0, 255);
 
  }
  else if (joyposVert > 564)
  {
    // This is Forward
    // Set Motors forward
    motorcontrol[2] = 0;
 
    //Determine Motor Speeds
    motorcontrol[0] = map(joyposVert, 564, 1023, 0, 255);
    motorcontrol[1] = map(joyposVert, 564, 1023, 0, 255);
 
  }
  else
  {
    // This is Stopped
    motorcontrol[0] = 0;
    motorcontrol[1] = 0;
    motorcontrol[2] = 0;
 
  }
 
  // Now do the steering
  // The Horizontal position will "weigh" the motor speed
  // Values for each motor
 
  if (joyposHorz < 460)
  {
    // Move Left
    // As we are going left we need to reverse readings
    // Map the number to a value of 255 maximum
    joyposHorz = map(joyposHorz, 460, 0, 0, 255);
 
    motorcontrol[0] = motorcontrol[0] - joyposHorz;
    motorcontrol[1] = motorcontrol[1] + joyposHorz;
 
    // Don't exceed range of 0-255 for motor speeds
    if (motorcontrol[0] < 0)motorcontrol[0] = 0;
    if (motorcontrol[1] > 255)motorcontrol[1] = 255;
 
  }
  else if (joyposHorz > 564)
  {
    // Move Right
    // Map the number to a value of 255 maximum
    joyposHorz = map(joyposHorz, 564, 1023, 0, 255);
 
    motorcontrol[0] = motorcontrol[0] + joyposHorz;
    motorcontrol[1] = motorcontrol[1] - joyposHorz;
 
    // Don't exceed range of 0-255 for motor speeds
    if (motorcontrol[0] > 255)motorcontrol[0] = 255;
    if (motorcontrol[1] < 0)motorcontrol[1] = 0;     
 
  }
 
  // Adjust to prevent "buzzing" at very low speed
  if (motorcontrol[0] < 8)motorcontrol[0] = 0;
  if (motorcontrol[1] < 8)motorcontrol[1] = 0;

 
  //Display the Motor Control values in the serial monitor.
  Serial.print("Motor A: ");
  Serial.print(motorcontrol[0]);
  Serial.print(" - Motor B: ");
  Serial.print(motorcontrol[1]);
  Serial.print(" - Direction: ");
  Serial.println(motorcontrol[2]);
 
 
  //Send a message containing Motor Control data to manager_server
  if (RadioManager.sendtoWait(motorcontrol, sizeof(motorcontrol), SERVER_ADDRESS))
  {
    // Now wait for a reply from the server
    uint8_t len = sizeof(buf);
    uint8_t from;
    if (RadioManager.recvfromAckTimeout(buf, &len, 2000, &from))
    {
      Serial.print("got reply from : 0x");
      Serial.print(from, HEX);
      Serial.print(": ");
      Serial.println((char*)buf);
    }
    else
    {
      Serial.println("No reply, is nrf24_reliable_datagram_server running?");
    }
  }
  else
    Serial.println("sendtoWait failed");
 
  delay(100);  // Wait a bit before next transmission
}

Berichten: 4064
Geregistreerd: 16 Okt 2013, 14:31
Woonplaats: s hertogenbosch

Re: Battlebot m.b.v. nRF24L01 & L298N

Berichtdoor shooter » 02 Apr 2022, 22:01

het lijkt erop dat de radiomanager available moet zijn om de motoren aan te sturen? maar ook om iets anders te doen dus je stuurt een bericht om te starten maar dan moet je ook een bericht sturen om te stoppen. komt misschien door de accoloades.
paul deelen
shooter@home.nl

Terug naar Gezamenlijke projecten

Wie is er online?

Gebruikers in dit forum: Geen geregistreerde gebruikers en 7 gasten