Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 6 of 6

Thread: Teensy3.6 interface with Minipc through rosserial. Data update issue.

  1. #1
    Junior Member
    Join Date
    Mar 2019
    Posts
    18

    Teensy3.6 interface with Minipc through rosserial. Data update issue.

    Before you all say post on ROS forum, let me tell you I believe the issue is somewhere on the coding becuas eit is somehow reading serial data but not updating frequently.

    Hello Everyone, This is my first time working with ROS and integrating with Teensy3.6. I am working on receiving GPS data (longitude and latitude) from minipc saved from a drone flight. I need to read the data through minipc and store it in the SD-card of teensy. 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 with ROS interface to teensy 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 of my program and if anyone faces the same issue or someone can point out in right direction will be very helpful.

    When program run at first, it generates around 5-10 and sometimes even more empty files and after that it starts writing in the files. The data once written is still not right because many files have same data and 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.

    The data is written right in the files how I want to its just dont update every times the loop runs. Hope someone can help me because im working on serial comm. along while and got hands on minipc yesterday for actual what i want to do. Thank you.


    Code:
    #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);
    }

  2. #2
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    9,023
    with the delay(50) at loop() end it will cycle at most 20 Hz - and perhaps almost exactly 19-20 Hz depending on SD IO time, though SD I/O time could be 100 or more millis and will not be ideal with writes of not 512 bytes and other file overhead.
    it seems the strings are processed with a callback to messageCb()
    There is no obvious logical connection to when a NEW x,y string coordinate pair arrives and when it is processed

    i.e. nothing apparent assures the incoming x,y is processed only once - or once at all before the next message arrives, and potentially the x,y value strings could change during loop() processing - unless that callback only happens through nh.spinOnce(); or some other trigger.

    No idea about how this works or what it does - though the above might be a start to synchronizing the incoming data and reliably processing it - without buffering the data into faster to write blocks SD overhead may limit throughput.

    I'd start by getting it to reliably print the intended records to SerMon and not SD card showing each is properly received and processed - then work out the timing of the SD data storage

  3. #3
    Junior Member
    Join Date
    Mar 2019
    Posts
    18
    Thank you for your timely response.

    The reason I'm not Serial printing is that while rosserial is sending data to teensy, I can not open terminal because if I open terminal the rosserial warns about multiple terminal opening and at the same time I start receiving a lot of garbage and misplaced data in the serial terminal.

    So that's why I have no option than to print data onto SD card and check the contents which is long hectic process of waiting but I can not think of any other way.

  4. #4
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    9,023
    Interesting - don't have a clear picture on this end of what Teensy is connected to with USB and how . . . what ROS is or how the minipc factors in. Without a SerMon to watch Teensy USB output as noted - adding a second Teensy to a PC connected through unused Serial1 or Serial2 to let that Teensy receive that data and display it on an independent machine perhaps.

  5. #5
    Junior Member
    Join Date
    Mar 2019
    Posts
    18
    I have tried one way to simply publish the GPS coordinates longitude and latitude what its receiving but they are updating after some time although the rosserial rate for GPS coordinate was 50 Hz. Even then it takes around sometimes 3 seconds and sometimes as long as 7 8 seconds. Im not quite sure whats going on the serial data although its really looks simple to read the serial data..

  6. #6
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    9,023
    Serial data should be simple to read - 9600 baud is very slow - Teensy can go reliably at 1-3 mega baud or more. Just need a good GND and Rx and Tx connect. I've talked to GPS's that only did 10 Hz, and had them set over 400 Kbaud - but lots of other Teensy to Teensy at 1+ Mbaud - did testing on Beta Teensy 4 using all 7 serial ports between two of them at 5 Mbaud works well. And connected Serial for debug at 115Kbaud or up to 2 Mbaud. With proper connects and servicing of the buffers the FIFO hardware on Teensy are very good, so that shouldn't be any trouble receiving the messages - or if you had a 2nd Teensy to send the data out as noted and then connect that Teensy to another Serial Monitor and echo the Serial data there to USB - even a Teensy LC will work well for that purpose.

    Without something like that to get a good view of the data to debug it, it will be working in the dark to understand what the problem may be. It wasn't noted if there is a 2nd Teensy at hand? Another UART to USB adapter could be used - but I like using Teensy's as they are adjustable and familiar to me. On my github is an echo sample that should work as it should have been recently posted on starting the Teensy 4 beta.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •