Skocz do zawartości

Arduino nrf24l01 zdalne sterowanie, zatrzymanie pojazdu po utracie sygnału


rikitiki

Pomocna odpowiedź

Witam.

Otóż borykam się ostatnio z problemem dotyczącym oprogramowania mojego autka rc.

Wszystko działa wyśmienicie tylko nie mam pomysłu w jaki sposób zmusić układ odbiorczy w autku do zatrzymania go w przypadku utraty sygnału bądź połączenia z nadajnikiem.

W chwili obecnej jeśli wyjadę za daleko samochodzikiem i utraci on połączenie z moim nadajnikiem to w odbiorniku zostaje zapamiętany ostatni sygnał z nadajnika i w przypadku kiedy był to gaz na full to trzeba mieć szybkie nogi 🤣

Chciałbym aby w jakiś sposób urządzenia sprawdzały czy się widzą i w przypadku utraty połączenia odbiornik wyłącza silnik.

Poniżej zamieszczam kody i do nadajnika i do odbiornika.

KOD NADAJNIKA

/*
Written by: Mujahed Altahle

This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.
To view a copy of this license, visit http://creativecommons.org/licenses/by-nc-sa/3.0/ 
or send a letter to Creative Commons, 444 Castro Street, Suite 900, Mountain View, California, 94041, USA.
*/

/* A simple Project for Remote Controlling with nRF24L01+ radios. 
We have 2 nodes, the transmitter (this one) and the receiver which is attached to the Car.
The idea is to transmit  2 parameters , one is Direction (Backward, or Forward with the speed) the other is the Steering (Left, or Right with the degree of rotation).
The nRF24L01 transmit values in type of "uint8_t" with maximum of 256 for each packet, so the values of direction is divided by (10) at the Tx side,
then it is multiplied by 10 again at the Rx side.
The Rx rules is to output the received parameters to port 3 and 6 where the Servo and the ESC are are attached
a condition is required to prevent the controller from transmitting values that is not useful (like 1480-1530 for ESC and 88-92 for Servo) to save more power as much as we can
*/

#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);

uint8_t data[2] ; //buffer to store received data
const uint8_t buffer_size = sizeof(data);
//pins that used for the Joystick
const int analogInPinY= A0; // 
const int analogInPinX = A1;// 
const int StearRight = 3;
const int StearLeft = 5;
//const int tx_led=2;// transmission indicator
int Y_value = 0;        // values read from the pot 
int X_value = 0; 
int outputValue = 0; 
/*
const int transmit_pin=6;
int transmit_enable;
int ForwrdButton=2;
int BackwrdButton=3;
*/
//
// Topology
//

// Single radio pipe address for the 2 nodes to communicate.
const uint64_t pipe = 0xE8E8F0F0E1LL;


//
// Setup
//

void setup(void)
{

 Serial.begin(57600);
 printf_begin();

 //
 // Setup and configure rf radio
 //

 radio.begin();
 radio.setDataRate(RF24_250KBPS);
 radio.setPALevel(RF24_PA_MAX);
 radio.setCRCLength(RF24_CRC_8);
 radio.setChannel(108);

 radio.openWritingPipe(pipe);
 //
 // Dump the configuration of the rf unit for debugging
 //

 radio.printDetails();

 //
 // Set up buttons
 //

 /*to be used later
  pinMode(ForwrdButton,INPUT );
  pinMode(BackwrdButton,INPUT);
  pinMode(transmit_enable,INPUT);
  */
 //pinMode(tx_led,OUTPUT);
 //digitalWrite(tx_led,LOW);
 pinMode(StearRight,INPUT);
 pinMode(StearLeft,INPUT);
 digitalWrite(StearRight,HIGH);
 digitalWrite(StearLeft,HIGH);

}

//
// Loop
//

void loop(void)
{

 X_value = analogRead(analogInPinX); 
 data[0] = map(X_value, 0, 1024, 100, 250); 
 Y_value = analogRead(analogInPinY); 
 data[1] = map(Y_value, 0, 1024, 45, 135);
   if(!StearRight)
     data[1]=45;
   else if(!StearLeft)
     data[1]=135;   
 //  transmit_enable=!digitalRead(transmit_pin);
 //an error ratio around +3,-3 appears  coming from the Joystick  all the time,
 //so we neglect them (using the following if statement) because they make the system transmitting data always and they are useless and waste of power

 //if((data[0]>153 || data[0] <=149) ||  (data[1]>=92 || data[1]<88))
 {
   //printf("Now sending...");
   bool ok = radio.write( data, buffer_size );
   //if (ok)
   //printf("ok\n\r");
   // else
   //printf("failed\n\r");
   // delay(15);
   //digitalWrite(tx_led,HIGH); //turn led on after transmission
 }
//  else digitalWrite(tx_led,LOW);//keep led off when no transmission
}

KOD ODBIORNIKA

/*
Written by: Mujahed Altahle

This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.
To view a copy of this license, visit http://creativecommons.org/licenses/by-nc-sa/3.0/ 
or send a letter to Creative Commons, 444 Castro Street, Suite 900, Mountain View, California, 94041, USA.

*/
/* A simple Project for Remote Controlling with nRF24L01+ radios. 
We have 2 nodes, the transmitter (this one) and the receiver which is attached to the Car.
The idea is to transmit  2 parameters , one is Direction (Backward, or Forward with the speed) the other is the Steering (Left, or Right with the degree of rotation).
The nRF24L01 transmit values in type of "uint8_t" with maximum of 256 for each packet, so the values of direction is divided by (10) at the Tx side,
then it is multiplied by 10 again at the Rx side.
The Rx rules is to output the received parameters to port 3 and 6 where the Servo and the ESC are are attached
a condition is required to prevent the controller from transmitting values that is not useful (like 1480-1530 for ESC and 88-92 for Servo) to save more power as much as we can
*/

#include <Servo.h> 
#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);


Servo servo; //steering servo declaration 
Servo esc; // Electronic Speed Contoller declaration

// Single radio pipe address for the 2 nodes to communicate.
const uint64_t pipe = 0xE8E8F0F0E1LL;


//
// Payload
//

uint8_t received_data[2];
uint8_t num_received_data =sizeof(received_data);


//
// Setup
//


void setup(void)
{
 delay(3000); //wait until the esc starts in case of Arduino got power first
 servo.attach(4);  // attaches the servo on pin 3 to the servo object 
 esc.attach(6);  // attaches the ESC on pin 6 to the ese object  
 servo.write(90);

 //
 // Print preamble
 //

 Serial.begin(57600);
 printf_begin();

 //
 // Setup and configure rf radio
 //

 radio.begin();
 radio.setDataRate(RF24_250KBPS);
 radio.setPALevel(RF24_PA_MAX);
 radio.setCRCLength(RF24_CRC_8);
 radio.setChannel(108);//Begin operation of the chip.
 // This simple sketch opens a single pipes for these two nodes to communicate
 // back and forth.  One listens on it, the other talks to it.

 radio.openReadingPipe(1,pipe);
 radio.startListening();
 //
 // Dump the configuration of the rf unit for debugging
 //
 radio.printDetails(); 
}


void loop(void)
{
 // if there is data ready
 if ( radio.available() )

 {
   bool done = false;
   int ESC_value;
   while (!done)
   {
     // Fetch the payload, and see if this was the last one.
     done = radio.read( received_data, num_received_data );
     ESC_value=received_data[0]*10; //Multiplication by 10 because the ESC operates for vlues around 1500 and the nRF24L01 can transmit maximum of 255 per packet 
     esc.writeMicroseconds(ESC_value);
   Serial.println(ESC_value);
     servo.write((received_data[1]));
    //Serial.println(received_data[1]);
   }
 }
}

Link do komentarza
Share on other sites

Zarejestruj się lub zaloguj, aby ukryć tę reklamę.
Zarejestruj się lub zaloguj, aby ukryć tę reklamę.

jlcpcb.jpg

jlcpcb.jpg

Produkcja i montaż PCB - wybierz sprawdzone PCBWay!
   • Darmowe płytki dla studentów i projektów non-profit
   • Tylko 5$ za 10 prototypów PCB w 24 godziny
   • Usługa projektowania PCB na zlecenie
   • Montaż PCB od 30$ + bezpłatna dostawa i szablony
   • Darmowe narzędzie do podglądu plików Gerber
Zobacz również » Film z fabryki PCBWay

1. Skoro rozumiesz miganie diodą to tutaj potrzebujesz coś praktycznie takiego samego.. tyle, że na przerwaniu 😉 - wersja optymalna.

Zasada działania:

a) Robisz jakiś licznik, który liczy poprzez przerwanie czas np. zlicza 1 co 10ms.

b) Do warunki if (radio.available) dodajesz kasowanie licznika, czyli licznik = 0;

c) W pętli loop na samym końcu (za warunkiem if (radio.available)) robisz coś w stylu if ( licznik > 100 ) { wyłącz silnik... }

Czyli jak licznik zliczy do 100 * 10ms czyli 1000ms i nie będzie nowego pakietu z danymi to automatycznie wyłączymy silniki. Czas itd. oczywiście możesz dobrać do swoich potrzeb.

2. Prostszy sposób to zamiast przerwań oszukać system i używać pętli dodając do niej jakieś dosyć stałe, znane opóźnienie. Ma to swoje wady, ale nie będę się rozpisywał...

Kod takiej zmiany to mniejwięcej ( sama pętla loop )

uint8 licznik = 0; /* definiujemy zmienną na początku */

void loop(void)
{
 delay(10); // zakładam, że to jest w ms, więc każda pętla to min. 10ms
 if (licznik++>25) // pętla 10ms * 25 = 250ms
 {
     esc.writeMicroseconds(1500); // nie wiem jaka wartość to zatrzymanie silnika, ale przyjme 1500
     licznik = 25; // nie chcemy, żeby licznik liczył dalej, bo mógłby się przepełnić
 }
 // if there is data ready
 if ( radio.available() )
 {
  licznik = 0; // tutaj kasujemy wartość licznika, bo jest połączenie
   bool done = false;
   int ESC_value;
   while (!done)
   {
     // Fetch the payload, and see if this was the last one.
     done = radio.read( received_data, num_received_data );
     ESC_value=received_data[0]*10; //Multiplication by 10 because the ESC operates for vlues around 1500 and the nRF24L01 can transmit maximum of 255 per packet
     esc.writeMicroseconds(ESC_value);
   Serial.println(ESC_value);
     servo.write((received_data[1]));
    //Serial.println(received_data[1]);
   }
 }
}
  • Pomogłeś! 1
Link do komentarza
Share on other sites

a) Robisz jakiś licznik, który liczy poprzez przerwanie czas np. zlicza 1 co 10ms.

To jest Arduino, już jest taki licznik w systemie, nazywa się "millis()" i liczy co 1ms.

deshipu - taki zajebisty jesteś a nie potrafiłeś mi pomóc tylko dałeś do zrozumienia że nie gadacie tutaj z zielonymi i w dodatku jeszcze poprawiasz kolegę MirekCz, który bardzo mi rozjaśnił temat... robi się tutaj druga elektroda widzę..

Koledze MirekCz bardzo dziękuję a co do deshipu - skoro nie zamierzasz wnieść nic w wątek/rozmowę to po cholerę piszesz i tylko wkur***sz? Spadaj do tematów, w których masz coś konstruktywnego do powiedzenia.

  • Lubię! 1
Link do komentarza
Share on other sites

Koledze MirekCz bardzo dziękuję a co do deshipu - skoro nie zamierzasz wnieść nic w wątek/rozmowę to po cholerę piszesz i tylko wkur***sz? Spadaj do tematów, w których masz coś konstruktywnego do powiedzenia.

Najmocniej przepraszam Pana, że tak chamsko śmiałem się wtrącić w rozmowę, do której nie pasuję ani doświadczeniem, ani wychowaniem. Obiecuję, że na przyszłość postaram się dwa razy zastanowić zanim odpowiem na pytanie od kogoś najwyraźniej nie z mojej ligi. Życzę dużo szczęścia z projektem oraz bardzo owocnych rozmów na tym forum, jestem pewien, że na pewno zaskarbił Pan sobie serca wszystkich tutejszych bywalców. Pozdrawiam.

Link do komentarza
Share on other sites

Dołącz do dyskusji, napisz odpowiedź!

Jeśli masz już konto to zaloguj się teraz, aby opublikować wiadomość jako Ty. Możesz też napisać teraz i zarejestrować się później.
Uwaga: wgrywanie zdjęć i załączników dostępne jest po zalogowaniu!

Anonim
Dołącz do dyskusji! Kliknij i zacznij pisać...

×   Wklejony jako tekst z formatowaniem.   Przywróć formatowanie

  Dozwolonych jest tylko 75 emoji.

×   Twój link będzie automatycznie osadzony.   Wyświetlać jako link

×   Twoja poprzednia zawartość została przywrócona.   Wyczyść edytor

×   Nie możesz wkleić zdjęć bezpośrednio. Prześlij lub wstaw obrazy z adresu URL.

×
×
  • Utwórz nowe...

Ważne informacje

Ta strona używa ciasteczek (cookies), dzięki którym może działać lepiej. Więcej na ten temat znajdziesz w Polityce Prywatności.