Hallo liebe Community
Ich bin Hans aus der Schweiz. Ich bin 45 Jahre alt und habe alle möglichen Hobbys. Jetzt habe ich mit dem 3D-Drucker einen Panzer gebaut und möchte auch die Steuerung selber machen. Ich habe Dans Transmitter nachgebaut. https://howtomechatronics.com/projects/diy-arduino-rc-transmitter/
Als Empfänger habe ich ein Arduino Uno oder ein Arduino Mega zur Verfügung. Dan hat auch alle möglichen Beispiele auf seiner Seite, wie diesen Empfänger hier z.B. DIY Arduino RC Receiver for RC Models and Arduino Projects
Wie gesagt, ich möchte meinen Panzer über den Uno oder Mega mit dem oben genannten Sender steuern.
Aber ich bin nicht so vertraut, dass ich meine komplizierte Idee umsetzen kann. Kann mir bitte jemand den Code für den Empfänger schreiben? Die Vorlage wäre also der Code von Dan auf seinem Empfänger. Ich wäre sehr dankbar. ICh bin überzeugt, dass ich anhand vom Code einiges verstehen werde. Wäre jemand so nett und hilfreich?
Der Sendercode lautet wie folgt:
/*
DIY Arduino based RC Transmitter Project
== Receiver Code - ESC and Servo Control ==
by Dejan Nedelkovski, www.HowToMechatronics.com
Library: TMRh20/RF24, https://github.com/tmrh20/RF24/
*/
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#include <Servo.h>
RF24 radio(10, 9); // nRF24L01 (CE, CSN)
const byte address[6] = "00001";
unsigned long lastReceiveTime = 0;
unsigned long currentTime = 0;
Servo esc; // create servo object to control the ESC
Servo servo1;
Servo servo2;
int escValue, servo1Value, servo2Value;
// Max size of this struct is 32 bytes - NRF24L01 buffer limit
struct Data_Package {
byte j1PotX;
byte j1PotY;
byte j1Button;
byte j2PotX;
byte j2PotY;
byte j2Button;
byte pot1;
byte pot2;
byte tSwitch1;
byte tSwitch2;
byte button1;
byte button2;
byte button3;
byte button4;
};
Data_Package data; //Create a variable with the above structure
void setup() {
Serial.begin(9600);
radio.begin();
radio.openReadingPipe(0, address);
radio.setAutoAck(false);
radio.setDataRate(RF24_250KBPS);
radio.setPALevel(RF24_PA_LOW);
radio.startListening(); // Set the module as receiver
resetData();
esc.attach(9);
servo1.attach(3);
servo2.attach(4);
}
void loop() {
// Check whether we keep receving data, or we have a connection between the two modules
currentTime = millis();
if ( currentTime - lastReceiveTime > 1000 ) { // If current time is more then 1 second since we have recived the last data, that means we have lost connection
resetData(); // If connection is lost, reset the data. It prevents unwanted behavior, for example if a drone jas a throttle up, if we lose connection it can keep flying away if we dont reset the function
}
// Check whether there is data to be received
if (radio.available()) {
radio.read(&data, sizeof(Data_Package)); // Read the whole data and store it into the 'data' structure
lastReceiveTime = millis(); // At this moment we have received the data
}
// Controlling servos
servo1Value = map(data.j2PotX, 0, 255, 0, 180);
servo2Value = map(data.j2PotY, 0, 255, 0, 180);
servo1.write(servo1Value);
servo2.write(servo2Value);
// Controlling brushless motor with ESC
escValue = map(data.pot1, 0, 255, 1000, 2000); // Map the receiving value form 0 to 255 to 0 1000 to 2000, values used for controlling ESCs
esc.writeMicroseconds(escValue); // Send the PWM control singal to the ESC
}
void resetData() {
// Reset the values when there is no radio connection - Set initial default values
data.j1PotX = 127;
data.j1PotY = 127;
data.j2PotX = 127;
data.j2PotY = 127;
data.j1Button = 1;
data.j2Button = 1;
data.pot1 = 1;
data.pot2 = 1;
data.tSwitch1 = 1;
data.tSwitch2 = 1;
data.button1 = 1;
data.button2 = 1;
data.button3 = 1;
data.button4 = 1;
}
Die bisherigen Daten für den Empfänger sind:
/*
DIY RC Receiver - Servos and Brushless motors control
by Dejan, www.HowToMechatronics.com
Library: TMRh20/RF24, https://github.com/tmrh20/RF24/
*/
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#include <Servo.h>
RF24 radio(3, 2); // nRF24L01 (CE, CSN)
const byte address[6] = "00001";
unsigned long lastReceiveTime = 0;
unsigned long currentTime = 0;
Servo esc; // create servo object to control the ESC
Servo servo1;
Servo servo2;
int escValue, servo1Value, servo2Value;
// Max size of this struct is 32 bytes - NRF24L01 buffer limit
struct Data_Package {
byte j1PotX;
byte j1PotY;
byte j1Button;
byte j2PotX;
byte j2PotY;
byte j2Button;
byte pot1;
byte pot2;
byte tSwitch1;
byte tSwitch2;
byte button1;
byte button2;
byte button3;
byte button4;
};
Data_Package data; //Create a variable with the above structure
void setup() {
Serial.begin(9600);
radio.begin();
radio.openReadingPipe(0, address);
radio.setAutoAck(false);
radio.setDataRate(RF24_250KBPS);
radio.setPALevel(RF24_PA_LOW);
radio.startListening(); // Set the module as receiver
resetData();
esc.attach(10); // Arduino digital pin D10 - CH9 on PCB board
servo1.attach(4); // D4 - CH1
servo2.attach(5); // D5 - CH2
}
void loop() {
// Check whether we keep receving data, or we have a connection between the two modules
currentTime = millis();
if ( currentTime - lastReceiveTime > 1000 ) { // If current time is more then 1 second since we have recived the last data, that means we have lost connection
resetData(); // If connection is lost, reset the data. It prevents unwanted behavior, for example if a drone jas a throttle up, if we lose connection it can keep flying away if we dont reset the function
}
// Check whether there is data to be received
if (radio.available()) {
radio.read(&data, sizeof(Data_Package)); // Read the whole data and store it into the 'data' structure
lastReceiveTime = millis(); // At this moment we have received the data
}
// Controlling servos
servo1Value = map(data.j2PotX, 0, 255, 0, 180); // Map the receiving value form 0 to 255 to 0 to 180(degrees), values used for controlling servos
servo2Value = map(data.j2PotY, 0, 255, 0, 180);
servo1.write(servo1Value);
servo2.write(servo2Value);
// Controlling brushless motor with ESC
escValue = map(data.j1PotY, 127, 255, 1000, 2000); // Map the receiving value form 127 to 255 to 1000 to 2000, values used for controlling ESCs
esc.writeMicroseconds(escValue); // Send the PWM control singal to the ESC
}
void resetData() {
// Reset the values when there is no radio connection - Set initial default values
data.j1PotX = 127;
data.j1PotY = 127;
data.j2PotX = 127;
data.j2PotY = 127;
data.j1Button = 1;
data.j2Button = 1;
data.pot1 = 1;
data.pot2 = 1;
data.tSwitch1 = 1;
data.tSwitch2 = 1;
data.button1 = 1;
data.button2 = 1;
data.button3 = 1;
data.button4 = 1;
}
Meine Idee wäre so;
Joystick links nach oben - Tank fährt vorwärts (über Motortreiber L298N zu den Motoren)
Joystick links nach unten - Tank fährt rückwärts (über Motortreiber L298N zu den Motoren)
Joystick links nach links - Während der Fahrt lenkt der Panzer nach links, Im Stehen dreht die linke Raupe rückwärts, die rechte vorwärts (auf der Stelle drehen)
Joystick rechts nach rechts - Während der Fahrt lenkt der Panzer nach rechts, Im Stehen dreht die rechte Raupe rückwärts, die linke vorwärts (auf der Stelle drehen)
Joystick links-Druckknopf - Panzer schießt - (steuert einen Motor, eine motorisierte Feuereinheit) gleichzeitig wird der Kanonenrohr vor- und zurückgeschoben (Rückstoß) - wird von einem Servo gesteuert
Joystick rechts nach links - Turm dreht sich nach links (steuert einen Motor mit Getriebe)
Joystick rechts nach rechts - Strom dreht sich nach rechts (steuert Motor mit Gang)
Joystick rechts hoch - Panzerrohr hebt sich (steuert Servo)
Joystick rechts nach unten - Panzerrohr senkt sich (steuert Servo)
Joystick rechts - Druckknopf - Laser ein- / ausschalten
4 Drucktasten unten:
1 Licht ein- und ausschalten
1 Ton ein und aus
1 Rauchmodul ein- und ausschalten
1 Kamermodul im Turm ein- und ausschalten
linkes oder rechtes Drehpotentiometer - Lautstärke Soundmodul regeln
Kann mir bitte jemand helfen und diesen Code schreiben?
Danke vielmals