ncmreynolds / ld2410

An Arduino library for the Hi-Link LD2410 24Ghz FMCW radar sensor.
GNU Lesser General Public License v2.1
178 stars 37 forks source link

v0.13 getting consistence data / readings #15

Closed WhiteLionATX closed 1 year ago

WhiteLionATX commented 1 year ago

Hello, I tried ESP 32 Wroom 32D module with LD2410B connected to pins 32 and 33. I usually use my own basic code and include libs for connected sensors. - So I did with yours and noticed two different bugs. I commented it in my code. please take a look. as soon as I removed the sendRadarMotion() function everything works like it should be. BUG 1: if setupLD2410 is only called once / initial radar.isConnected gets true only every 12-14 readings. initializing setupLD2410() more often bevor readings seems to help somehow. BUG 2: radar.isConnected - method only gets true after 20-50 attempts. So I tried a loop to fix with a loop. that get it working a view times more often but after about 30 readings it stopped / hangs for unknown reasons.

Any ideas on that ?

uint32_t radarRxPin = 32;
uint32_t radarTxPin = 33;

void setupLD2410()
{
  if (DEBUG_MODE > 2) Serial1.begin(256000, SERIAL_8N1, radarRxPin, radarTxPin); //UART for monitoring the radar
  delay(500);
  if (DEBUG_MODE > 2) Serial.print(F("\nConnect LD2410 radar TX to GPIO:"));
  if (DEBUG_MODE > 2) Serial.println(radarRxPin);
  if (DEBUG_MODE > 2) Serial.print(F("Connect LD2410 radar RX to GPIO:"));
  if (DEBUG_MODE > 2) Serial.println(radarTxPin);
  if (DEBUG_MODE > 2) Serial.print(F("LD2410 radar sensor initialising: "));
  if (radar.begin(Serial1))
  {
    if (DEBUG_MODE > 2) Serial.println(F("OK"));
    if (DEBUG_MODE > 2) Serial.print(F("LD2410 firmware version: "));
    if (DEBUG_MODE > 2) Serial.print(radar.firmware_major_version);
    if (DEBUG_MODE > 2) Serial.print('.');
    if (DEBUG_MODE > 2) Serial.print(radar.firmware_minor_version);
    if (DEBUG_MODE > 2) Serial.print('.');
    if (DEBUG_MODE > 2) Serial.println(radar.firmware_bugfix_version, HEX);
  }
  else
  {
    if (DEBUG_MODE > 2) Serial.println(F("not connected"));
  }
}

void sendRadarMotion(unsigned long currentTime, unsigned long *timeLastMeasurements, unsigned int *measureCount, bool connectedToMqttNetwork)
{
  // main loop for LD2410 sensor readings:
  if(currentTime - *timeLastMeasurements > 1000 )
  {
    if (DEBUG_MODE > 2) Serial.println("*");
    *timeLastMeasurements = millis();
    *measureCount += 1;
    if (DEBUG_MODE > 2) Serial.print(F("Measurement count: "));
    if (DEBUG_MODE > 2) Serial.println(*measureCount);
    DynamicJsonDocument docRadar = DeserializeJson(radarMonitor); 
    // init only once after reboot:   
    if (*measureCount == 1) setupLD2410();
    // BUG 1: if setupLD2410 is not called here again the readings stop even earliear then BUG 2
    //setupLD2410();
    radar.read();
    if (DEBUG_MODE > 2) Serial.print(F("Radar connected: "));
    if (DEBUG_MODE > 2) Serial.println((String)radar.isConnected());
    uint32_t count = 0;

    // BUG 2: try to fix the only get true after 20-50 attempts of "radar.isConnected":
    while (!radar.isConnected() && (count < 100))
    {
      count ++;
      nonBlockingDelay(10);
      //if (DEBUG_MODE > 2) Serial.print(F("Radar connected: "));
      //if (DEBUG_MODE > 2) Serial.print((String)count + " ");
    }

    if(radar.isConnected())
    {    
      if(radar.presenceDetected())
      {
        JsonAddSingleString(docRadar, "Id", WiFi.macAddress());
        JsonAddSingleValue(docRadar, "Restarts", rtcMemory.restartCounter);

        if(radar.stationaryTargetDetected())
        {
          uint32_t distance = radar.stationaryTargetDistance();
          uint32_t energy = radar.stationaryTargetDistance();
          JsonAddSingleString(docRadar, "Target", "Stationary");
          JsonAddSingleValue(docRadar, "Distance", distance);
          JsonAddSingleValue(docRadar, "EnergyCm", energy);      
          if (DEBUG_MODE > 2) Serial.print(F("Stationary target: "));
          if (DEBUG_MODE > 2) Serial.print(distance);
          if (DEBUG_MODE > 2) Serial.print(F("cm energy:"));
          if (DEBUG_MODE > 2) Serial.print(energy);
          if (DEBUG_MODE > 2) Serial.print(' ');
        }
        if(radar.movingTargetDetected())
        {
          uint32_t distance = radar.movingTargetDistance();
          uint32_t energy = radar.movingTargetEnergy();
          JsonAddSingleString(docRadar, "Target", "Moving");
          JsonAddSingleValue(docRadar, "Distance", distance);
          JsonAddSingleValue(docRadar, "EnergyCm", energy);          
          if (DEBUG_MODE > 2) Serial.print(F("Moving target: "));
          if (DEBUG_MODE > 2) Serial.print(distance);
          if (DEBUG_MODE > 2) Serial.print(F("cm energy:"));
          if (DEBUG_MODE > 2) Serial.print(energy);
        }
        if (DEBUG_MODE > 2) Serial.println();
        JsonAddSingleString(docRadar, "Pub", "/SensorTypD");    
        JsonAddSingleString(docRadar, "Sub", "/SensorCommandsD");

        // json to rtc memory:
        radarMonitor = SerializeJson(docRadar);
        if (DEBUG_MODE > 2) Serial.println("Sending ...");
        // send by mqtt if connected
        if(connectedToMqttNetwork) 
        {
          client_global.subscribe("/SensorCommandsD");
          client_global.publish("/SensorTypD", radarMonitor.c_str());  
        }
        // if not connected to mqtt send by esp now 
        else
        {
          broadCastMsgToListOfNeighbours(radarMonitor);
        } 
        radarMonitor = "";
        //*measureCount = 0;
      }
    }
    else
    {
      if (DEBUG_MODE > 2) Serial.println(F("No radar sensor connected."));
    }
  } 
}

EDIT: It gets better calling radar.read() more often than every second.

EDIT2: It does not solve the problem. To get constantan readings I have to call again setupLD2410() and this will sooner or later lead to a freeze at:
if (DEBUG_MODE > 2) Serial1.begin(256000, SERIAL_8N1, radarRxPin, radarTxPin); //UART for monitoring the radar

EDIT3: After more testing I noticed that even nonBlockingDelay(10) is to much. setting nonBlockingDelay(1) seems to work. No idea why radar.read() needs to be called so much often or stops working.