lorawan icon indicating copy to clipboard operation
lorawan copied to clipboard

Simulation with LoRa (without LoRaWAN stack)

Open CIRCk8 opened this issue 1 year ago • 0 comments

Hi,

I'm working with LoRaWan module for ns-3.

However, I'm working on LoRa Phy, I don't want LoRaWan Stack in my simulation.

I want that SimpleEndDeviceLoraPhy recive a packet from SimpleGatewayLoraPhy. Gateway send packet, but end-device is not receiving it.

I share with you my logs.

LoRaSim:CnfSim()
LoRaSim:CnfLoRaParams()
LoRa Parameters configured
LoRaSim:CnfLoRaChnn()
LoRa Channel configured.
LoRaSim:CnfLoRaNode()
LoraPhy:SetMobility()
LoraPhy:SetChannel(0x5610829cfbe0, 0x5610829a6f90)
EndDeviceLoraPhy:SwitchToStandby()
LoRa Node configured.
LoRaSim:CnfLoRaHub()
SimpleGatewayLoraPhy:SimpleGatewayLoraPhy()
LoraPhy:SetMobility()
LoraPhy:SetChannel(0x5610829cfea0, 0x5610829a6f90)
LoRa Hub configured.
LoRaSim:Simulate()
Running LoRaSim ...
EndDeviceLoraPhy:SwitchToRx()
EndDeviceLoraPhy:SwitchToStandby()
SimpleGatewayLoraPhy:Send(0x5610829cfea0, 0x5610829d0430, 868.1, 14)
LoraPhy:GetOnAirTime(0x5610829d0430, SF: 7, headerDisabled: 0, codingRate: 1, bandwidthHz: 125000, nPreamble: 8, crcEnabled: 1, lowDataRateOptimizationEnabled: 0))
Packet of size 100 bytes
Time computation: num = 816, den = 28, payloadSymbNb = 158, tSym = 0.001024
tPreamble = 0.012544
tPayload = 0.161792
Total time = 0.174336
Duration of packet: +1.74336e+08ns, SF7
LoraPhy:GetMobility()
Starting cycle over all 2 PHYs
Sender mobility: 0:0:0
LoraPhy:GetMobility()
Receiver mobility: 5:5:0
Propagation: txPower=14dbm, rxPower=-25.6406dbm, distance=7.07107m, delay=+24ns
No net device connected to the PHY, using context 0
Scheduling reception of the packet
EndDeviceLoraPhy:SwitchToRx()
EndDeviceLoraPhy:SwitchToStandby()
End of LoRasim

I share my code here.

LoRaSim.h

#include "ns3/log.h"
#include "ns3/lora-helper.h"
#include "ns3/simple-end-device-lora-phy.h"
#include "ns3/simple-gateway-lora-phy.h"
#include "ns3/mobility-helper.h"
#include "ns3/one-shot-sender-helper.h"
#include "ns3/constant-position-mobility-model.h"
#include "ns3/test.h"
#include "ns3/command-line.h"
#include "ns3/nstime.h"
#include "ns3/simulator.h"
#include <unistd.h>

using namespace ns3;
using namespace lorawan;

/**********************************************
* Define component of logging
***********************************************/
NS_LOG_COMPONENT_DEFINE ("LoRaSim");

/**********************************************
* LoRaSim Class
***********************************************/
class LoRaSim {

public:
    /**********************************************
     * Constructor
     ***********************************************/
    LoRaSim(); 
    virtual ~LoRaSim();

    /**********************************************
     * Config. Simulation
     ***********************************************/
    void CnfSim(uint8_t sf, double freq, double dbm); 

    /**********************************************
     * Run simulation
     ***********************************************/
    void Simulate();    

private:

    Ptr<LoraChannel> LoRaChnn;                // LoRa Channel
    Ptr<SimpleEndDeviceLoraPhy> LoRaNode;     // LoRa Node
    Ptr<SimpleGatewayLoraPhy> LoRaGtw;        // LoRa Hub
    LoraTxParameters LoRaParams;              // LoRa Comm. Params
    double LoRaFreq;                          // Frequency. Usually 868.1
    double LoRaTxPowerDbm;                    // Tx Power in dBm. Usually 14
    uint16_t nTxMessages;                     // Couter of tx messages (node)
    uint16_t nRxMessages;                     // Couter of rx messages (node)
    uint16_t gTxMessages;                     // Couter of tx messages (hub)
    uint16_t gRxMessages;                     // Couter of rx messages (hub)

    /**********************************************
     * NS-3 Logging Level
     ***********************************************/
    void LoggingEnable();

    /**********************************************
     * Config. LoRa Phy Params
     ***********************************************/
    void CnfLoRaParams(uint8_t sf, double freq, double dbm);

    /**********************************************
     * Config. LoRa Channel
     ***********************************************/
    Ptr<LoraChannel> CnfLoRaChnn();

    /**********************************************
     * Config. LoRa Node
     ***********************************************/
    Ptr<SimpleEndDeviceLoraPhy> CnfLoRaNode(Ptr<LoraChannel> Chnn, const Vector& xyz);

    /**********************************************
     * Config. LoRa Hub
     ***********************************************/
    Ptr<SimpleGatewayLoraPhy> CnfLoRaHub(Ptr<LoraChannel> Chnn, const Vector& xyz);

    // /**********************************************
    //  * Simulate of sending x bytes (Node)
    //  ***********************************************/
    // void NodeSendDummyBytes(Ptr<SimpleEndDeviceLoraPhy> LoRaNode, int payloadlen);
    
    // /**********************************************
    //  * Simulate the receiving of packets (Node)
    //  ***********************************************/
    // void NodeRevDummyBytes(Ptr<SimpleEndDeviceLoraPhy> LoRaNode);
    
    // /**********************************************
    //  * Simulate of sending x bytes (Hub)
    //  ***********************************************/
    // void GwSendDummyBytes(Ptr<SimpleGatewayLoraPhy> LoRaNode, int payloadlen);
    
    // /**********************************************
    //  * Simulate the receiving of packets (Hub)
    //  ***********************************************/
    // void GwRevDummyBytes(Ptr<SimpleGatewayLoraPhy> LoRaNode);

};

/**********************************************
* Constructor
***********************************************/
LoRaSim::LoRaSim ()
{
}
LoRaSim::~LoRaSim ()
{
}

/**********************************************
* NS-3 Logging Level
***********************************************/
void LoRaSim::LoggingEnable()
{
  LogComponentEnable("LoRaSim", LOG_LEVEL_ALL);
  LogComponentEnable("SimpleGatewayLoraPhy", LOG_LEVEL_ALL);
  LogComponentEnable("EndDeviceLoraPhy", LOG_LEVEL_ALL);
  LogComponentEnable("LoraChannel", LOG_LEVEL_INFO);
  LogComponentEnable("LoraPhy", LOG_LEVEL_ALL);
}

/**********************************************
* Config. LoRa Phy Params
***********************************************/
void LoRaSim::CnfLoRaParams(uint8_t sf, double freq, double dbm)
{
    NS_LOG_FUNCTION_NOARGS();
    LoRaParams.sf = sf;             
    LoRaFreq = freq;
    LoRaTxPowerDbm = dbm;
    NS_LOG_DEBUG ("LoRa Parameters configured");
}

/**********************************************
 * Config. LoRa Channel
 ***********************************************/
Ptr<LoraChannel> LoRaSim::CnfLoRaChnn()
{
    NS_LOG_FUNCTION_NOARGS();

    // Loss model
    Ptr<LogDistancePropagationLossModel> loss = CreateObject<LogDistancePropagationLossModel> ();
    loss->SetPathLossExponent (3.76);
    loss->SetReference (1, 7.7);

    // Delay model
    Ptr<PropagationDelayModel> delay = CreateObject<ConstantSpeedPropagationDelayModel> ();
    
    NS_LOG_DEBUG ("LoRa Channel configured.");

    // Create LoRa Channel
    return CreateObject<LoraChannel> (loss, delay);
}

/**********************************************
 * Config. LoRa Node
 ***********************************************/
Ptr<SimpleEndDeviceLoraPhy> LoRaSim::CnfLoRaNode(Ptr<LoraChannel> Chnn, const Vector& xyz)
{
    NS_LOG_FUNCTION_NOARGS();

    // Create mobility model for node
    Ptr<ConstantPositionMobilityModel> mob1 = CreateObject<ConstantPositionMobilityModel> ();
    mob1->SetPosition (xyz);

    // Create node
    Ptr<SimpleEndDeviceLoraPhy> LoRaNode = CreateObject<SimpleEndDeviceLoraPhy> ();

    // Add mobility model to node
    LoRaNode->SetMobility (mob1);

    // Add node to channel
    Chnn->Add (LoRaNode);
    LoRaNode->SetChannel(Chnn);

    // Listen for a specific SpreadingFactor
    LoRaNode->SetSpreadingFactor (LoRaParams.sf);

    // Listen on a specific frequency
    LoRaNode->SetFrequency (LoRaFreq);

    // Switch node to standby
    LoRaNode->SwitchToStandby();

    NS_LOG_DEBUG ("LoRa Node configured.");

    return LoRaNode;
}

/**********************************************
 * Config. LoRa Hub
 ***********************************************/
Ptr<SimpleGatewayLoraPhy> LoRaSim::CnfLoRaHub(Ptr<LoraChannel> Chnn, const Vector& xyz)
{
    NS_LOG_FUNCTION_NOARGS();

    // Create mobility model for node
    Ptr<ConstantPositionMobilityModel> mob1 = CreateObject<ConstantPositionMobilityModel> ();
    mob1->SetPosition (xyz);

    // Create node
    Ptr<SimpleGatewayLoraPhy> LoRaHub = CreateObject<SimpleGatewayLoraPhy> ();

    // Add mobility model to node
    LoRaHub->SetMobility (mob1);

    // Add node to channel
    Chnn->Add (LoRaHub);
    LoRaHub->SetChannel(Chnn);

    // Add packet's received path to hub
    LoRaHub->AddReceptionPath();
 
    NS_LOG_DEBUG ("LoRa Hub configured.");

    return LoRaHub;
}

/**********************************************
* Init simulator
***********************************************/
void LoRaSim::CnfSim(uint8_t sf, double freq, double dbm)
{
    // Enable ns-3 logging
    LoggingEnable();
    NS_LOG_FUNCTION_NOARGS();

    // Configure LoRa Params
    CnfLoRaParams(sf, freq, dbm);

    // Configure LoRa Channel  
    LoRaChnn = CnfLoRaChnn();

    // Configure a node
    LoRaNode = CnfLoRaNode(LoRaChnn, Vector (5.0, 5.0, 0.0));

    // Configure a hub
    LoRaGtw = CnfLoRaHub(LoRaChnn, Vector (0.0, 0.0, 0.0));

    // Init variables
    nTxMessages = 0;
    nRxMessages = 0;                   
    gTxMessages = 0;                     
    gRxMessages = 0;                      
}

// /**********************************************
// * Simulate of sending x bytes over LoRa Phy
// ***********************************************/
// void LoRaSim::NodeSendDummyBytes(Ptr<SimpleEndDeviceLoraPhy> Node, int payloadlen)
// {
//     NS_LOG_FUNCTION_NOARGS();

//     // Create packet of 0s with payloadlen as size
//     Ptr<Packet> packet = Create<Packet> (payloadlen);

//     // Send packet  
//     Node->Send(packet, LoRaParams, LoRaFreq, LoRaTxPowerDbm);
//     Time ToA = Node->ToA(packet, LoRaParams);

//     // Put in standby mode
//     Node->SwitchToStandby();

//     // Update counter
//     nTxMessages ++;

//     // Debug    
//     NS_LOG_DEBUG ("Send LoRa packet of " << payloadlen << " bytes.");
//     NS_LOG_DEBUG ("ToA of packet [seconds]: " << ToA.GetSeconds());
//     NS_LOG_DEBUG ("Total number of Tx messages (Node): " << nTxMessages);
// }

// /**********************************************
//  * Simulate the receiving of packets (Node)
//  ***********************************************/
// void LoRaSim::NodeRevDummyBytes(Ptr<SimpleEndDeviceLoraPhy> LoRaNode)
// {
//     // Update counter
//     nRxMessages ++;
//     NS_LOG_DEBUG ("Total number of Rx messages (Node): " << nRxMessages);
// }

// /**********************************************
//  * Simulate of sending x bytes (Hub)
//  ***********************************************/
// void LoRaSim::GwSendDummyBytes(Ptr<SimpleGatewayLoraPhy> LoRaNode, int payloadlen)
// {
//     // Update counter
//     gTxMessages ++;
//     NS_LOG_DEBUG ("Total number of Tx messages (Hub): " << gTxMessages);

// }
// /**********************************************
//  * Simulate of sending x bytes (Hub)
//  ***********************************************/
// void LoRaSim::GwSendDummyBytes(Ptr<SimpleGatewayLoraPhy> LoRaNode, int payloadlen)
// {
//     // Update counter
//     gTxMessages ++;
//     NS_LOG_DEBUG ("Total number of Tx messages (Hub): " << gTxMessages);

// }

// /**********************************************
//  * Simulate the receiving of packets (Hub)
//  ***********************************************/
// void LoRaSim::GwRevDummyBytes(Ptr<SimpleGatewayLoraPhy> LoRaNode)
// {
//     // Update counter
//     gRxMessages ++;
//     NS_LOG_DEBUG ("Total number of Rx messages (Hub): " << gRxMessages);
// }

/**********************************************
* Run simulation
***********************************************/
void LoRaSim::Simulate()
{
    NS_LOG_FUNCTION_NOARGS();
    NS_LOG_DEBUG ("Running LoRaSim ...");

    // Node sending packet each 20 secs.
    // Ptr<Packet> Dummy = Create<Packet> (5);
    // Simulator::Schedule (Seconds (20), &SimpleEndDeviceLoraPhy::Send, LoRaNode, 
    // Dummy, LoRaParams, LoRaFreq, 14);

    // Node receiving packets
    Ptr<Packet> Rev = Create<Packet> ();
    Simulator::Schedule (Seconds (5), &SimpleEndDeviceLoraPhy::StartReceive, LoRaNode, 
    Rev, 14, LoRaParams.sf, Seconds(60), LoRaFreq);

    Simulator::Stop (Hours (2));
    Simulator::Run ();
    Simulator::Destroy ();

    // Hub receiving packets
    // Ptr<Packet> RevT = Create<Packet> ();
    // Simulator::Schedule (Seconds (5), &SimpleGatewayLoraPhy::StartReceive, LoRaGtw, 
    // RevT, 14, LoRaParams.sf, Seconds(60), LoRaFreq);

    // Hub sending packet each 20 secs.
    Ptr<Packet> Dummy = Create<Packet> (100);
    Simulator::Schedule (Seconds (20), &SimpleGatewayLoraPhy::Send, LoRaGtw, 
    Dummy, LoRaParams, LoRaFreq, 14);

    Simulator::Stop (Hours (2));
    Simulator::Run ();
    Simulator::Destroy ();


    NS_LOG_DEBUG ("End of LoRasim");
}

LoRaSim.cc

#include "LoRaSim.h"

// Main
int main (int argc, char *argv[])
{
  uint8_t sf = 7;
  CommandLine cmd;

  // Command line interface
  cmd.AddValue ("sf", "SF parameter to use in LoRaSim", sf);
  cmd.Parse (argc, argv);

  // Create object for simulate
  LoRaSim sm = LoRaSim();

  // Config. simulations
  sm.CnfSim(sf, 868.1, 14);

  // Run simulation
  sm.Simulate();
}

I don't know why end-device is not receiving anything.

It seems to work with both LoRaPhy layers, but 'No net device connected to the PHY, using context 0' line (in logs) is not cool.

Starting cycle over all 2 PHYs
Sender mobility: 0:0:0
LoraPhy:GetMobility()
Receiver mobility: 5:5:0
Propagation: txPower=14dbm, rxPower=-25.6406dbm, distance=7.07107m, delay=+24ns
No net device connected to the PHY, using context 0
Scheduling reception of the packet
EndDeviceLoraPhy:SwitchToRx()
EndDeviceLoraPhy:SwitchToStandby()

Anyone know about that issue?

CIRCk8 avatar Mar 24 '23 09:03 CIRCk8