Ask Your Question

Revision history [back]

Arduino subscribing gps data not getting update

Hello Everyone, This is my first time working with ROS and integrating my system Teensy3.6 similar to an Arduino interns of sketch. I am working on receiving GPS data (longitude and latitude) from minipc stored from a drone flight. I need to read the data through minipc and store it in the SD-card of teensy. Before you think why I am doing this, the need is to receive GPS data during flight and save it in the SD card of teensy along with other parameters from 3 more sensors.

Now while I am new and looking at different examples and tutorial, i have made a basic script but I am facing issues related to data storing and I have no idea is the problem related to coding maybe serial comm. setup or some other. I have pasted my script for the program and if anyone faces the same issue or someone can point out in right direction will be very helpful. When program is run at first, it generates around 40 50 and sometimes even more files empty and after that it starts writing in the files. The data once written is still notright because many files are almost the same and the data is not updating for most part of the time. I tried sending data from minipc at 1Hz, 10 Hz and 100 Hz but no success and baud rate is 9600. I dont know why its making problem. Serial monitor also does not update the data coordinates although for most part of time, serial data is seen garbaged and jumpled but not right data.

#include <ros.h>
#include <sensor_msgs/NavSatFix.h>

#include <Wire.h>
#include <SPI.h>
#include <SD.h>
#include <SD_t3.h>

File logfile;
ros::NodeHandle  nh;

float lat;
float lon;
char buf1[15];
char buf2[15];
String x, y;

void messageCb(const sensor_msgs::NavSatFix& msg) {
  lat = msg.latitude;
  lon = msg.longitude;

  dtostrf(lon, 13, 9, buf1);
  dtostrf(lat, 13, 10, buf2);

  x = buf1;
  y = buf2;

  digitalWrite(13, HIGH - digitalRead(13)); // blink the led
}

ros::Subscriber<sensor_msgs::NavSatFix> sub("/dji_sdk/gps_position", messageCb);

void setup()
{
  nh.initNode();
  nh.subscribe(sub);
  Serial.begin(9600);
  //Serial.println("Initializing SD card...");
  if (!SD.begin(chipSelect)) {
    //Serial.println("Card failed, or not present");
    return;
  }
  //Serial.println("card initialized.");
  pinMode(ledpin, OUTPUT);
  pinMode(13, OUTPUT);

  //Serial.println("Setup Completed !!");
  while(!nh.connected()) nh.spinOnce();
}

void loop()
{
  String dataString = "";

  logfile.flush();
  char filename[] = "Log000.txt";
  for (uint16_t i = 0; i < 1000; i++)
  {
    filename[3] = i / 100 + '0';
    int Hun = i / 100;
    filename[4] = (i - (Hun * 100)) / 10 + '0';
    filename[5] = i % 10 + '0';
    if (!SD.exists(filename))
    {
      logfile = SD.open(filename, FILE_WRITE);
      break;
    }
  }
  if (!logfile)
  {
    //Serial.println("Error: Logfile could not be created !!");
  }
  if (logfile)
  {
    logfile.println("No.\tLongitude\t\tLattitude");
    for (int k = 1; k < 5; k++) {
      String num = k;
      String dataline = (num + "\t" + x + "\t" + y);
      logfile.println(dataline);
      //delay(1000);
    }
    logfile.close();
  }
  Serial.print(x);
  Serial.print("  ");
  Serial.println(y);
  nh.spinOnce();
  delay(50);
}

Arduino subscribing gps data not getting update

Hello Everyone, This is my first time working with ROS and integrating my system Teensy3.6 similar to an Arduino interns of sketch. I am working on receiving GPS data (longitude and latitude) from minipc stored from a drone flight. I need to read the data through minipc and store it in the SD-card of teensy. Before you think why I am doing this, the need is to receive GPS data during flight and save it in the SD card of teensy along with other parameters from 3 more sensors.

Now while I am new and looking at different examples and tutorial, i have made a basic script but I am facing issues related to data storing and I have no idea is the problem related to coding maybe serial comm. setup or some other. I have pasted my script for the program and if anyone faces the same issue or someone can point out in right direction will be very helpful. When program is run at first, it generates around 40 50 and sometimes even more files empty and after that it starts writing in the files. The data once written is still notright because many files are almost the same and the data is not updating for most part of the time. I tried sending data from minipc at 1Hz, 10 Hz and 100 Hz but no success and baud rate is 9600. I dont know why its making problem. Serial monitor also does not update the data coordinates although for most part of time, serial data is seen garbaged and jumpled but not right data. data.

#include <ros.h>
#include <sensor_msgs/NavSatFix.h>

#include <Wire.h>
#include <SPI.h>
#include <SD.h>
#include <SD_t3.h>

File logfile;
ros::NodeHandle  nh;

float lat;
float lon;
char buf1[15];
char buf2[15];
String x, y;

void messageCb(const sensor_msgs::NavSatFix& msg) {
  lat = msg.latitude;
  lon = msg.longitude;

  dtostrf(lon, 13, 9, buf1);
  dtostrf(lat, 13, 10, buf2);

  x = buf1;
  y = buf2;

  digitalWrite(13, HIGH - digitalRead(13)); // blink the led
}

ros::Subscriber<sensor_msgs::NavSatFix> sub("/dji_sdk/gps_position", messageCb);

void setup()
{
  nh.initNode();
  nh.subscribe(sub);
  Serial.begin(9600);
  //Serial.println("Initializing SD card...");
  if (!SD.begin(chipSelect)) {
    //Serial.println("Card failed, or not present");
    return;
  }
  //Serial.println("card initialized.");
  pinMode(ledpin, OUTPUT);
  pinMode(13, OUTPUT);

  //Serial.println("Setup Completed !!");
  while(!nh.connected()) nh.spinOnce();
}

void loop()
{
  String dataString = "";

  logfile.flush();
  char filename[] = "Log000.txt";
  for (uint16_t i = 0; i < 1000; i++)
  {
    filename[3] = i / 100 + '0';
    int Hun = i / 100;
    filename[4] = (i - (Hun * 100)) / 10 + '0';
    filename[5] = i % 10 + '0';
    if (!SD.exists(filename))
    {
      logfile = SD.open(filename, FILE_WRITE);
      break;
    }
  }
  if (!logfile)
  {
    //Serial.println("Error: Logfile could not be created !!");
  }
  if (logfile)
  {
    logfile.println("No.\tLongitude\t\tLattitude");
    for (int k = 1; k < 5; k++) {
      String num = k;
      String dataline = (num + "\t" + x + "\t" + y);
      logfile.println(dataline);
      //delay(1000);
    }
    logfile.close();
  }
  Serial.print(x);
  Serial.print("  ");
  Serial.println(y);
  nh.spinOnce();
  delay(50);
}