edensheiko

Untitled

Jan 22nd, 2024
672
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.04 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.     if (u8g2) {
  35.         u8g2->clearBuffer();
  36.         u8g2->setCursor(0, 16);
  37.         u8g2->println( "RangingMaster");
  38.         u8g2->sendBuffer();
  39.     }
  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.  
  59. #ifdef LILYGO_T3_S3_V1_0
  60.     // T3 S3 V1.1 with PA Version Set output power to 3 dBm    !!Cannot be greater than 3dbm!!
  61.     int8_t TX_Power = 3;
  62. #else
  63.     // T3 S3 V1.2 (No PA) Version Set output power to 3 dBm    !!Cannot be greater than 3dbm!!
  64.     int8_t TX_Power = 13;
  65. #endif
  66.     if (radio.setOutputPower(TX_Power) == RADIOLIB_ERR_INVALID_OUTPUT_POWER) {
  67.         Serial.println(F("Selected output power is invalid for this module!"));
  68.         while (true);
  69.     }
  70.  
  71.     Serial.println("SX128x_RangingMaster start!");
  72.  
  73.  
  74. }
  75.  
  76. void loop()
  77. {
  78.     Serial.print(F("[SX1280] Ranging ... "));
  79.  
  80.     // start ranging exchange
  81.     // range as master:             true
  82.     // slave address:               0x12345678
  83.     //int state = radio.range(true, 0x12345678);
  84.  
  85.     // the other module must be configured as slave with the same address
  86.     /*
  87.     */
  88.     // int state = radio.range(false, 0x12345678);
  89.  
  90.     // if ranging calibration is known, it can be provided
  91.     // this should improve the accuracy and precision
  92.    
  93.       uint16_t calibration[3][6] = {
  94.         { 10299, 10271, 10244, 10242, 10230, 10246 },
  95.         { 11486, 11474, 11453, 11426, 9000, 11401 }, // 11417
  96.         { 13308, 13493, 13528, 13515, 13430, 13376 }
  97.       };
  98.  
  99.       int state = radio.range(true, 0x12345678, calibration);
  100.    
  101.  
  102.     if (u8g2) {
  103.         u8g2->clearBuffer();
  104.         u8g2->setCursor(0, 16);
  105.         u8g2->println( "RangingMaster");
  106.         u8g2->setCursor(0, 32);
  107.     }
  108.  
  109.     if (state == RADIOLIB_ERR_NONE) {
  110.         // ranging finished successfully
  111.  
  112.         float raw = radio.getRangingResult();
  113.         Serial.println(F("success!"));
  114.         Serial.print(F("[SX1280] Distance:\t\t\t"));
  115.         Serial.print(raw);
  116.         Serial.println(F(" meters (raw)"));
  117.  
  118.         if (u8g2) {
  119.             u8g2->print( "Distance:"); u8g2->print( raw); u8g2->print("meters(raw)");
  120.         }
  121.  
  122.    
  123.  
  124.     } else if (state == RADIOLIB_ERR_RANGING_TIMEOUT) {
  125.         // timed out waiting for ranging packet
  126.         Serial.println(F("timed out!"));
  127.  
  128.         if (u8g2) {
  129.             u8g2->print( "Timed out!");
  130.         }
  131.  
  132.     } else {
  133.         // some other error occurred
  134.         Serial.print(F("failed, code "));
  135.         Serial.println(state);
  136.         if (u8g2) {
  137.             u8g2->print( "Failed!");
  138.         }
  139.     }
  140.  
  141.     if (u8g2) {
  142.         u8g2->sendBuffer();
  143.     }
  144.  
  145.     // wait for a second before ranging again
  146.     delay(1000);
  147. }
  148.  
Tags: Master LoRA
Advertisement
Add Comment
Please, Sign In to add comment