Print from 2 serials, one LiDAR 3vhp and one gps

Hello,

this is kind of my first project and I'm trying to : take a log from a gps antena, parse it and only take the timestamp from it, then print it, then i want to take all the measurements from LiDAR untill a new time comes from gps antena.

this is my code:

#include <Wire.h> 
#include <LiquidCrystal_I2C.h>
#import "process_log.h"
#import "save_sd.h"
#import "lidar_measurements.h"

//initialize the liquid crystal library
//the first parameter is  the I2C address
//the second parameter is how many rows are on your screen
LiquidCrystal_I2C lcd(0x3F,16,2); 

// only some pins can be used for RX check SoftwareSerial docs
const int chipSelect = 10; //define chip select pin for microSD
#define GPS_RX_PIN 8 // pin rx for GPS
#define GPS_TX_PIN 9 // we don't use it (because we don't transmit anything) but it needs to be as a parameter for GPS serial

SoftwareSerial gpsSerial(GPS_RX_PIN, GPS_TX_PIN); 

#define FAST_I2C

void setup()
{
	Serial.begin(9600); //initialize the serial communication at 9600 baud rate
	gpsSerial.begin(9600); //serial for gps

  // check if micro sd is plugged
  Serial.print("Initializing SD card...");
  if (!SD.begin(chipSelect)) 
  {
    Serial.println("initialization failed!");
    return;
  }
  Serial.println("initialization done.");

	// Initialize Arduino I2C (for communication to LidarLite)
	Wire.begin();
	#ifdef FAST_I2C
	#if ARDUINO >= 157
	    Wire.setClock(400000UL); // set I2C frequency to 400kHz (for Arduino Due)
	#else
	    TWBR = ((F_CPU / 400000UL) - 16) / 2; // set I2C frequency to 400kHz
	#endif
	#endif

  lcd.init();
  lcd.clear();
  lcd.backlight();

}

void loop() 
{
  // declare vars as static to persist between loop iterations
  static uint16_t distanceSum = 0;
  static uint16_t distanceCount = 0;
  static uint32_t lastSecond = 0;


  uint32_t currentSecond = 0;
	uint16_t distance;
	uint8_t result_lidar = distanceFast(&distance);
  Serial.println("result_lidar: " + result_lidar);
  distanceSum += distance;
  distanceCount++;
	// Read data from GPS module if available
	if (gpsSerial.available() > 0) 
	{
        String data = processGPS(gpsSerial, lastSecond, distanceSum, distanceCount, currentSecond, distance);
        // save_data_toSD(&data);
        lcd.setCursor(0,0);
        lcd.print("Time - Distance:");
        lcd.setCursor(0,1);
        lcd.print(data);
        Serial.println(data);
	}
}

this is working fine, because right now i'm taking one measurement and time for gps and print it

uint8_t distanceFast(uint16_t * distance)
{

	myLidarLite.configure(0);

	myLidarLite.waitForBusy();
	myLidarLite.takeRange();
	*distance = myLidarLite.readDistance();
	return *distance;
}

this is sending the data with time and measuremetn

#include "process_log.h"


String processGPS(SoftwareSerial& gpsSerial, uint32_t& lastSecond, uint16_t& distanceSum, uint16_t& distanceCount, uint32_t& currentSecond, uint16_t& distance)
{
	static String nmeaLog = "";
	static String data = "";
	unsigned long startTime = millis(); // store the current time

	// check if GPS serial is available
	while (millis() - startTime < 1000) // timeout period of 1 second 
	{
		if (gpsSerial.available() > 0) 
		{
			char incomingByte = gpsSerial.read();
			// check if incoming byte is the start of a new NMEA log
			if (incomingByte == '$') 
			{
			  nmeaLog = "";  // reset the string for a new log
			  data = "";
			}
			// check if the end of the log is reached
			if (incomingByte == '\n') 
			{
				if (nmeaLog.indexOf("RMC") > -1) 
				{
					String time = getClock(nmeaLog);
					data += String(time);
					//calculate arithmetic mean fir the previous sec
					currentSecond = time.substring(0,2).toInt() * 3600 + time.substring(2,4).toInt() * 60 + time.substring(4,6).toInt();
					if (currentSecond != lastSecond && lastSecond != 0)
					{
						float meanDistance = distanceSum / (float)distanceCount;
						data += ",";
						data += meanDistance;
						// Reset counters
						distanceSum = distance;
						distanceCount = 1;
					}
					lastSecond = currentSecond;
					return data;
				}
			}
			nmeaLog += incomingByte;  // append the incoming byte to the current log
		}
	}
	return "";
}

the output log looks like this :
15:51:13.736 -> 135113,32.00

but i want something like :
15:51:13.736 -> and here take all the measuremetn from lidar (around 50)

please help

Many GPS are configured to deliver a fix at 1Hz so you are likely to get a new time at every full sentence received since the second will have changed,

Also how long will the LiDAR measurements take ? If it’s tool long you might miss some gps data

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.