From 874970fd3e0b422a0a01a87165df4cc0af29ca60 Mon Sep 17 00:00:00 2001 From: Hoa Ben The Nguyen <hbnguye@stud.ntnu.no> Date: Tue, 14 May 2024 11:58:00 +0200 Subject: [PATCH] update: arduino code --- droneCode/lidar_setup/lidar_setup.ino | 96 +++++++++++++++------------ 1 file changed, 52 insertions(+), 44 deletions(-) diff --git a/droneCode/lidar_setup/lidar_setup.ino b/droneCode/lidar_setup/lidar_setup.ino index cb9bcd35..31c7ddc4 100644 --- a/droneCode/lidar_setup/lidar_setup.ino +++ b/droneCode/lidar_setup/lidar_setup.ino @@ -4,54 +4,55 @@ #include <TinyGPS++.h> #include <SoftwareSerial.h> -// #include <TimerOne.h> #include <Wire.h> #include <LIDARLite.h> #include <SD.h> +#include <SPI.h> LIDARLite myLidarLite; // LiDar object TinyGPSPlus gps; // GPS object SoftwareSerial ss(RXPin, TXPin); // serial connection to GPS device +// structrue of each measurement in waterbody struct WaterBody { + int measurementID; const char* name; float latitude; float longitude; }; -// if new coordinates or body of water is added then add it here +// NB: if new coordinates or body of water is added then add it here // define an array of waterbody structures WaterBody waterCoords = { - {"mjosa",60.000000,10.000000}, - {"mjosa",60.000000,10.000000}, - {"mjosa",60.000000,10.000000}, - {"mjosa",60.000000,10.000000}, + {"mjosa",60.000000,12.000000, 1}, + {"mjosa",65.000000,11.000000, 2}, + {"mjosa",59.000000,12.000000, 3}, + {"mjosa",63.000000,13.000000, 4}, }; -// #define DIR_PIN 2 -// #define STEP_PIN 3 +#define CHIP_SELECT_PIN 10 \\ Pin connected to the CS pin of SD card moudle +#define MAX_READING 1000 \\ Maximum number of LIDAR readings to store +#define RXPIN 2 +#define TXPIN 3 -static const int RXPin = 4, TXPin = 3; static const uint32_t GPSBaoud = 9600; // baud rate const float maxRadius = 5; // max distance from location to gps coordinates - -// volatile int stepCount = 0; -// volatile int roundCount = 0; - void setup() { Serial.begin(GPSBaud); // Initialize serial connection to display distances readings + pinMode(CHIP_SELECT_PIN, OUTPUT); + + if (!SD.begin(CHIP_SELECT_PIN)){ + Serial.println("SD card initialization failed"); + return; + } + + Serial.println("SD card initialized"); + myLidarLite.begin(0, true); // Start sensor and I2C myLidarLite.configure(0); // set to default mode, balanced performance ss.begin(GPSBaud); // Start GPS tracking - - // pinMode(DIR_PIN, OUTPUT); - // pinMode(STEP_PIN, OUTPUT); - - // digitalWrite(DIR_PIN, HIGH); - - // Timer1.initialize(); - // Timer1.attachInterrupt(tick, 1000000.0f / (3200 * 1)); + } // extra: Reciever bias correction is performed 1 out of every 100 readings @@ -60,15 +61,36 @@ void loop() { while (ss.available() > 0){ gps.encode(ss.read()); if (gps.location.isUpdated()){ - for (int i = 0; i< waterCoors.size(); i++){ + for (int i = 0; i< sizeof(waterCoords)/sizeof(waterCoords[0]); i++){ // scan after lidar reached designated location - if withinRadius(gps.location.lat, gps.location.lng, waterCoordinates[i][1], waterCoordinates[i][2], maxRadius) { - // Take a measurement with receiver bias correction and print to serial terminal - receive(true); - - // Take 99 measurement without receiver bias correction and print to serial terminal - for(int i = 0; i < 99; i++){ - receive(false); + if withinRadius(gps.location.lat, gps.location.lng, waterCoords[i].latitude, waterCoords[i].longitude, maxRadius) { + String filename = String(waterCoords[i].name) + "_ID_" + String(waterCoords[i].measurementID) + ".txt"; + + if (SD.exist(filename)){ + SD.remove(filename); + Serial.println("File deleted:" + filename); + } + + // Create a new file on the SD card + File dataFile = SD.open(filename, FILE_WRITE); + + if (!dataFile){ + Serial.println("SD card initialized"); + + // Take 99 measurement without receiver bias correction and print to file + for(int i = 0; i < 10; i++){ + // convert it to (XYZ) format + sprintf(coordinate,"%d %d %d\n\0",(int)(0,0,myLidarLite.distance(false)); + datafile.println(coordinate); + } + + dataFile.close(); + // Convert and send them + Serial.print(s); + Serial.println("File created.", filename); + } + else{ + Serial.println("Error opening file for writing: ", filename); } } } @@ -87,18 +109,4 @@ void receive(bool bias) bool withinRadius(float x1,float y1,float x2,float y2, float radius){ float distance = sqrt(pow((x1-x2),2)+pow((y1-y2),2)); return (distance <= radius); -} - -// void tick() -// { -// digitalWrite(STEP_PIN, HIGH); -// //delay(1); -// digitalWrite(STEP_PIN, LOW); - -// stepCount++; -// if(stepCount >= 3200) -// { -// stepCount = 0; -// roundCount++; -// } -// } \ No newline at end of file +} \ No newline at end of file -- GitLab