noam76

receiver Lora

Feb 17th, 2026
12,888
0
Never
3
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <SoftwareSerial.h>
  2. #include <Servo.h>
  3.  
  4. #define servoPin 9
  5. #define escMotorPin 12
  6. // הגדרות פינים קבועות
  7. const int PIN_RX = 10;
  8. const int PIN_TX = 11;
  9. const int M0 = 7;
  10. const int M1 = 8;
  11.  
  12. SoftwareSerial loraSerial(PIN_RX, PIN_TX);
  13. Servo steeringServo;
  14. Servo escMotor;
  15.  
  16. // מבנה נתונים ארוז - חייב להיות זהה לשלט
  17. struct __attribute__((packed)) ControlData {
  18.   byte header;    // 0xAA
  19.   byte throttle;  // 0-255
  20.   byte steering;  // 0-255
  21.   byte trimT;     // 0-255
  22.   byte trimS;     // 0-255
  23.   byte checksum;  
  24. };
  25.  
  26. ControlData receiverdat;
  27. unsigned long lastPacketTime = 0;
  28. bool connected = false;
  29.  
  30. // ערכים סופיים לפקודות (גלובליים כדי שיהיו נגישים לכל הקוד)
  31. int finalThrottle = 127;
  32. int finalSteering = 127;
  33. int escSignal = 1500;      // Ajouté ici pour être global
  34. int steeringSignal = 1500;
  35.  
  36. void setup() {
  37.   steeringServo.attach(servoPin);
  38.   escMotor.attach(escMotorPin);
  39.   // הגדרת מצבי עבודה למודול LoRa
  40.   pinMode(M0, OUTPUT);
  41.   pinMode(M1, OUTPUT);
  42.   digitalWrite(M0, LOW);
  43.   digitalWrite(M1, LOW);
  44.  
  45.   Serial.begin(115200);
  46.   loraSerial.begin(9600);
  47.   Serial.println("System Initialized. Waiting for Remote...");
  48.  
  49.   // אתחול זמן ראשוני כדי למנוע כניסה מיידית ל-Failsafe
  50.   lastPacketTime = millis();
  51. }
  52.  
  53. void loop() {
  54.   // 1. ניקוי באפר במקרה של הצטברות "זבל" אלקטרוני
  55.   if (loraSerial.available() > 30) {
  56.     while (loraSerial.available()) loraSerial.read();
  57.   }
  58.  
  59.   // 2. קליטת נתונים ואימות
  60.   if (loraSerial.available() >= sizeof(ControlData)) {
  61.     byte potentialHeader = loraSerial.read();
  62.    
  63.     if (potentialHeader == 0xAA) {
  64.       byte* structPtr = (byte*)&receiverdat;
  65.       structPtr[0] = potentialHeader;
  66.      
  67.       loraSerial.readBytes(&structPtr[1], sizeof(ControlData) - 1);
  68.  
  69.       // אימות מתמטי של החבילה
  70.       byte calculatedChecksum = receiverdat.throttle + receiverdat.steering +
  71.                                  receiverdat.trimT + receiverdat.trimS;
  72.  
  73.       if (calculatedChecksum == receiverdat.checksum) {
  74.         // סימון קשר תקין ועדכון זמן
  75.         lastPacketTime = millis();
  76.        
  77.         if (!connected) {
  78.           connected = true;
  79.           Serial.println(">>> LINK ESTABLISHED <<<");
  80.         }
  81.  
  82.         // א. חישוב ערכים סופיים כולל טרמים (מרכז ב-127)
  83.         finalThrottle = receiverdat.throttle + (receiverdat.trimT - 127);
  84.         finalSteering = receiverdat.steering + (receiverdat.trimS - 127);
  85.  
  86.         // ב. הגבלת הטווח ל-0-255
  87.         finalThrottle = constrain(finalThrottle, 0, 255);
  88.         finalSteering = constrain(finalSteering, 0, 255);
  89.  
  90.         // ג. החלת Deadzone (שטח מת) למניעת זמזום במנוחה
  91.         if (finalThrottle > 123 && finalThrottle < 131) finalThrottle = 127;
  92.         if (finalSteering > 123 && finalSteering < 131) finalSteering = 127;
  93.  
  94.         escSignal = map(finalThrottle, 0, 255, 1000, 2000);
  95.         escMotor.writeMicroseconds(escSignal);
  96.  
  97.         steeringSignal = map(finalSteering, 0, 255, 1000, 2000);
  98.         steeringServo.writeMicroseconds(steeringSignal);
  99.  
  100.         // דוגמה להדפסת הנתונים הסופיים
  101.         printStatus();
  102.       }
  103.     }
  104.   }
  105.  
  106.   // 3. מנגנון Failsafe דינמי (עצירה אם אין קשר מעל 1.5 שניות)
  107.   if (millis() - lastPacketTime > 1500) {
  108.     if (connected) {
  109.       connected = false;
  110.       Serial.println("!!! LINK LOST - FAILSAFE ACTIVATED !!!");
  111.     }
  112.    
  113.     finalThrottle = 127; // עצירה
  114.     finalSteering = 127; // יישור גלגלים
  115.    
  116.     // מניעת הצפה של ה-Serial Monitor
  117.     lastPacketTime = millis() - 1400;
  118.   }
  119. }
  120.  
  121. // פונקציית עזר להדפסת נתונים
  122. void printStatus() {
  123.   static unsigned long lastPrint = 0;
  124.   if (millis() - lastPrint > 300) { // הדפסה פעם ב-200 מילי-שניות בלבד
  125.     // נתונים גולמיים מהשלט (Raw)
  126.     Serial.print("RAW -> Thr: "); Serial.println(receiverdat.throttle);
  127.     Serial.print("Str: "); Serial.println(receiverdat.steering);
  128.     //Serial.print("TrimT: "); Serial.println(receiverdat.trimT);
  129.     //Serial.print("TrimS: "); Serial.println(receiverdat.trimS);
  130.     Serial.println("____________");
  131.     //Serial.print("T_Total: "); Serial.print(finalThrottle);
  132.     //Serial.print(" | S_Total: "); Serial.println(finalSteering);
  133.     //Serial.println("____________");
  134.     Serial.print("ESC_Signal: "); Serial.println(escSignal);
  135.     Serial.print("STERRING_Signal: "); Serial.println(steeringSignal);
  136.     Serial.println("____________");
  137.     lastPrint = millis();
  138.   }
  139. }
Advertisement
Comments
  • User was banned
  • User was banned
  • User was banned
Add Comment
Please, Sign In to add comment