Arduino subscribing gps data not getting update

asked 2019-07-07 21:47:41 -0500

Rakeh gravatar image

updated 2019-07-08 02:06:41 -0500

gvdhoorn gravatar image

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);
}
edit retag flag offensive close merge delete

Comments

  Serial.print(x);
  Serial.print("  ");
  Serial.println(y);
  nh.spinOnce();

I don't know about the rest, but one thing that stands out to me: you're using Serial.print(..) and regular rosserial on the same serial port.

That will not work and will lead to data corruption.

Use different serial ports or remove the Serial.print(..) et al.

gvdhoorn gravatar imagegvdhoorn ( 2019-07-08 02:08:35 -0500 )edit

Thank you for ur suggestion. ill make the change to see and Yes, I came to know of these serial issue, so I'm not looking at serial and In-fact i look at the SD card contents without opening serial port..

Rakeh gravatar imageRakeh ( 2019-07-08 02:17:32 -0500 )edit