edensheiko

Untitled

Jan 22nd, 2024
965
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.83 KB | None | 0 0
  1. /*
  2.    RadioLib SX128x Ranging Example
  3.  
  4.    This example performs ranging exchange between two
  5.    SX1280 LoRa radio modules. Ranging allows to measure
  6.    distance between the modules using time-of-flight
  7.    measurement.
  8.  
  9.    Only SX1280 and SX1282 without external RF switch support ranging!
  10.  
  11.    Note that to get accurate ranging results, calibration is needed!
  12.    The process is described in Semtech SX1280 Application Note AN1200.29
  13.  
  14.    For default module settings, see the wiki page
  15.    https://github.com/jgromes/RadioLib/wiki/Default-configuration#sx128x---lora-modem
  16.  
  17.    For full API reference, see the GitHub Pages
  18.    https://jgromes.github.io/RadioLib/
  19. */
  20.  
  21. // include the library
  22. #include <RadioLib.h>
  23. #include "boards.h"
  24.  
  25. SX1280 radio = new Module(RADIO_CS_PIN, RADIO_DIO1_PIN, RADIO_RST_PIN, RADIO_BUSY_PIN);
  26.  
  27.  
  28. void setup()
  29. {
  30.     initBoard();
  31.     // When the power is turned on, a delay is required.
  32.     delay(1500);
  33.  
  34.  
  35.     if (u8g2) {
  36.         u8g2->clearBuffer();
  37.         u8g2->setCursor(0, 16);
  38.         u8g2->println( "RangingSlave");
  39.         u8g2->sendBuffer();
  40.     }
  41.  
  42.     // initialize SX1280 with default settings
  43.     Serial.print(F("[SX1280] Initializing ... "));
  44.     int state = radio.begin(2400,812.5,9,7,RADIOLIB_SX128X_SYNC_WORD_PRIVATE,10,12);
  45.     if (state == RADIOLIB_ERR_NONE) {
  46.         Serial.println(F("success!"));
  47.     } else {
  48.         Serial.print(F("failed, code "));
  49.         Serial.println(state);
  50.         while (true);
  51.     }
  52.  
  53. #if defined(RADIO_RX_PIN) && defined(RADIO_TX_PIN)
  54.     //Set ANT Control pins
  55.     radio.setRfSwitchPins(RADIO_RX_PIN, RADIO_TX_PIN);
  56. #endif
  57.  
  58. #ifdef LILYGO_T3_S3_V1_0
  59.     int8_t TX_Power = 3;
  60. #else
  61.     int8_t TX_Power = 13;
  62. #endif
  63.     // Set output power to 3 dBm    !!Cannot be greater than 3dbm!!
  64.     if (radio.setOutputPower(TX_Power) == RADIOLIB_ERR_INVALID_OUTPUT_POWER) {
  65.         Serial.println(F("Selected output power is invalid for this module!"));
  66.         while (true);
  67.     }
  68.  
  69.     Serial.println("SX128x_RangingSlave start!");
  70. }
  71.  
  72. void loop()
  73. {
  74.     Serial.print(F("[SX1280] Ranging ... "));
  75.  
  76.     // start ranging exchange
  77.     // range as master:             true
  78.     // slave address:               0x12345678
  79.     // int state = radio.range(true, 0x12345678);
  80.  
  81.     // the other module must be configured as slave with the same address
  82.     int state = radio.range(false, 0x12345678);
  83.  
  84.     // if ranging calibration is known, it can be provided
  85.     // this should improve the accuracy and precision
  86.     /*
  87.       uint16_t calibration[3][6] = {
  88.         { 10299, 10271, 10244, 10242, 10230, 10246 },
  89.         { 11486, 11474, 11453, 11426, 11417, 11401 },
  90.         { 13308, 13493, 13528, 13515, 13430, 13376 }
  91.       };
  92.  
  93.       int state = radio.range(true, 0x12345678, calibration);
  94.     */
  95.     if (u8g2) {
  96.         u8g2->clearBuffer();
  97.         u8g2->setCursor(0, 16);
  98.         u8g2->println( "RangingSlave");
  99.         u8g2->setCursor(0, 32);
  100.     }
  101.  
  102.     if (state == RADIOLIB_ERR_NONE) {
  103.         // ranging finished successfully
  104.  
  105.         float raw = radio.getRangingResult();
  106.         Serial.println(F("success!"));
  107.         Serial.print(F("[SX1280] Distance:\t\t\t"));
  108.         Serial.print(raw);
  109.         Serial.println(F(" meters (raw)"));
  110.  
  111.         if (u8g2) {
  112.             u8g2->print( "Distance:"); u8g2->print( raw); u8g2->print("meters(raw)");
  113.         }
  114.  
  115.     } else if (state == RADIOLIB_ERR_RANGING_TIMEOUT) {
  116.         // timed out waiting for ranging packet
  117.         Serial.println(F("timed out!"));
  118.         if (u8g2) {
  119.             u8g2->print( "Timed out!");
  120.         }
  121.     } else {
  122.         // some other error occurred
  123.         Serial.print(F("failed, code "));
  124.         Serial.println(state);
  125.         if (u8g2) {
  126.             u8g2->print( "Failed!");
  127.         }
  128.  
  129.     }
  130.  
  131.  
  132.  
  133.     // wait for a second before ranging again
  134.     delay(1000);
  135. }
  136.  
Tags: Slave LoRA
Advertisement
Add Comment
Please, Sign In to add comment