Store long data into float data

i want to save long data into float data but the result always zero

struct NAV_PVT {
  unsigned char cls;
  unsigned char id;
  unsigned short len;
  unsigned long iTOW;          // GPS time of week of the navigation epoch (ms)

  unsigned short year;         // Year (UTC)
  unsigned char month;         // Month, range 1..12 (UTC)
  unsigned char day;           // Day of month, range 1..31 (UTC)
  unsigned char hour;          // Hour of day, range 0..23 (UTC)
  unsigned char minute;        // Minute of hour, range 0..59 (UTC)
  unsigned char second;        // Seconds of minute, range 0..60 (UTC)
  char valid;                  // Validity Flags (see graphic below)
  unsigned long tAcc;          // Time accuracy estimate (UTC) (ns)
  long nano;                   // Fraction of second, range -1e9 .. 1e9 (UTC) (ns)
  unsigned char fixType;       // GNSSfix Type, range 0..5
  char flags;                  // Fix Status Flags
  unsigned char reserved1;     // reserved
  unsigned char numSV;         // Number of satellites used in Nav Solution

  long lon;                    // Longitude (deg)
  long lat;                    // Latitude (deg)
  long height;                 // Height above Ellipsoid (mm)
  long hMSL;                   // Height above mean sea level (mm)
  unsigned long hAcc;          // Horizontal Accuracy Estimate (mm)
  unsigned long vAcc;          // Vertical Accuracy Estimate (mm)

  long velN;                   // NED north velocity (mm/s)
  long velE;                   // NED east velocity (mm/s)
  long velD;                   // NED down velocity (mm/s)
  long gSpeed;                 // Ground Speed (2-D) (mm/s)
  long heading;                // Heading of motion 2-D (deg)
  unsigned long sAcc;          // Speed Accuracy Estimate
  unsigned long headingAcc;    // Heading Accuracy Estimate
  unsigned short pDOP;         // Position dilution of precision
  short reserved2;             // Reserved
  unsigned long reserved3;     // Reserved
  unsigned char dummy[8];
};

NAV_PVT pvt;

float latitude = (float)(pvt.lat/10000000.0);
float longitude = (float)(pvt.lon/10000000.0);

if i print the latitude it will result 0.0000000

I'd try
float latitude = float(pvt.lat)/10000000.0;

2 Likes

If it is in the declaration area they will always give 0 because PVT does not have data loaded yet.

Outside of that detail, the conversion is correct.
Try this

struct NAV_PVT {
  unsigned char cls;
  unsigned char id;
  unsigned short len;
  unsigned long iTOW;          // GPS time of week of the navigation epoch (ms)

  unsigned short year;         // Year (UTC)
  unsigned char month;         // Month, range 1..12 (UTC)
  unsigned char day;           // Day of month, range 1..31 (UTC)
  unsigned char hour;          // Hour of day, range 0..23 (UTC)
  unsigned char minute;        // Minute of hour, range 0..59 (UTC)
  unsigned char second;        // Seconds of minute, range 0..60 (UTC)
  char valid;                  // Validity Flags (see graphic below)
  unsigned long tAcc;          // Time accuracy estimate (UTC) (ns)
  long nano;                   // Fraction of second, range -1e9 .. 1e9 (UTC) (ns)
  unsigned char fixType;       // GNSSfix Type, range 0..5
  char flags;                  // Fix Status Flags
  unsigned char reserved1;     // reserved
  unsigned char numSV;         // Number of satellites used in Nav Solution

  long lon;                    // Longitude (deg)
  long lat;                    // Latitude (deg)
  long height;                 // Height above Ellipsoid (mm)
  long hMSL;                   // Height above mean sea level (mm)
  unsigned long hAcc;          // Horizontal Accuracy Estimate (mm)
  unsigned long vAcc;          // Vertical Accuracy Estimate (mm)

  long velN;                   // NED north velocity (mm/s)
  long velE;                   // NED east velocity (mm/s)
  long velD;                   // NED down velocity (mm/s)
  long gSpeed;                 // Ground Speed (2-D) (mm/s)
  long heading;                // Heading of motion 2-D (deg)
  unsigned long sAcc;          // Speed Accuracy Estimate
  unsigned long headingAcc;    // Heading Accuracy Estimate
  unsigned short pDOP;         // Position dilution of precision
  short reserved2;             // Reserved
  unsigned long reserved3;     // Reserved
  unsigned char dummy[8];
};

NAV_PVT pvt;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  pvt.lat = 12345678;
  pvt.lon = 34567890;
  float latitude = (float)(pvt.lat/10000000.0);
  float longitude = (float)(pvt.lon/10000000.0);
  Serial.print("Lat: "); Serial.println(latitude,6);
  Serial.print("Lon: "); Serial.println(longitude,6);
}

void loop() {
  // put your main code here, to run repeatedly:
}

And the console shows

Lat: 1.234568
Lon: 3.456789
1 Like

Why ?
Long and float has the same data length, but the long is more accurate. It would be better to transform and operate with data as long type.

this is the full code

void direction_read() {
  if (processGPS()) {
    compass.read();
    heading = compass.getAzimuth();
  
    radlat_UGV = latitude_UGV*toRad;
    radlon_UGV = longitude_UGV*toRad;
    radlat_wp = lat_wp[wp]*toRad;
    radlon_wp = lon_wp[wp]*toRad;
    o = sin(radlon_wp - radlon_UGV)*cos(radlon_wp);
    t = (cos(radlat_UGV)*sin(radlat_wp))-(sin(radlat_UGV)*cos(radlat_wp)*cos(radlon_wp - radlon_UGV));              
    theta = atan2(o,t);
    //ubah ke derajat dari radian
    bearing = theta*toDeg;
    
    //kalo nilai derajat minus, ditambah 360 supaya bisa jadi positif
    if (bearing < 0){
      bearing = bearing + 360;
    }

    direction = heading - bearing;

    // DEBUG:
    Serial.print("HEAD: ");
    Serial.print(heading);
    Serial.print(" | ");
    Serial.print("BEAR: ");
    Serial.print(bearing);
    Serial.print(" | ");
    Serial.print("DIR: ");
    Serial.print(direction);
    Serial.print(" | ");
  } else {
    Serial.print(F("INVALID DIR"));
    Serial.print(" | ");
  }
}

void distance_read() {
  if (processGPS()) {
    latitude_UGV = float(pvt.lat)/10000000.0;
    longitude_UGV = float(pvt.lon)/10000000.0;
    acc = float(pvt.vAcc);
    radlat_UGV = latitude_UGV*toRad;
    radlon_UGV = longitude_UGV*toRad;
    radlat_wp = lat_wp[wp]*toRad;
    radlon_wp = lon_wp[wp]*toRad;
    // haversine
    a = (sq(sin(radlat_wp - radlat_UGV)/2)+cos(radlat_UGV)*cos(radlat_wp)*(sq(sin(radlon_wp-radlon_UGV)/2))); 
    c = 2*atan2(sqrt(a), sqrt(1-a));
    // kali ke radius bumi dalam meter
    distance = c*6371000;

    // DEBUG:
    Serial.print("LAT: ");
    Serial.print(latitude_UGV);
    Serial.print(" | ");
    Serial.print("LON: ");
    Serial.print(longitude_UGV);
    Serial.print(" | ");
    Serial.print("DIST: ");
    Serial.print(distance);
    Serial.print(" | ");
  } else {
    Serial.print(acc);
    Serial.print(" | ");
  }
}

void otomasi() {
  /* Autonomous Switching */
  while (Serial3.available() > 0) {
    if (processGPS()) {
      direction_read();
      distance_read();

      if (latitude_UGV != 0 && longitude_UGV != 0) {
        // Read Input: Jarak
        g_fisInput[0] = distance;
        // Read Input: Sudut
        g_fisInput[1] = direction;

        g_fisOutput[0] = 0;
        g_fisOutput[1] = 0;

        if (distance < 2) {
          UGVControl(0, 165);
          delay(500);
          UGVControl(0, 35);
        } else {
          fis_evaluate();

          UGVSpeed = g_fisOutput[0];
          UGVDirection = g_fisOutput[1];
          int ReverseDirection = map(UGVDirection, 0, 180, 180, 0);

          UGVControl(UGVSpeed, ReverseDirection);

          // DEBUG:
          Serial.print("Speed: ");   
          Serial.print(UGVSpeed);   
          Serial.print(" | "); 
          Serial.print("Direction: ");   
          Serial.println(UGVDirection);
        }

        // DEBUG:
        Serial.print("Speed: ");   
        Serial.print(UGVSpeed);   
        Serial.print(" | "); 
        Serial.print("Direction: ");   
        Serial.println(UGVDirection);
      } else {
        UGVSpeed = 0;
        UGVDirection = 90;

        UGVControl(UGVSpeed, UGVDirection);

        // DEBUG:
        Serial.print("Speed: ");   
        Serial.print(UGVSpeed);   
        Serial.print(" | "); 
        Serial.print("Direction: ");   
        Serial.println(UGVDirection);
      }
    }
  }
}

void setup()
{
  Serial.begin(9600);
  GPS10();
  // Setup Compass
  compass.init();
  compass.setCalibration(-1345, 1112, -1855, 691, -62, 353);
  compass.setSmoothing(10,true);  

  // Setup GPS
  Serial3.begin(115200);

  //Setup Motor DC
  pinMode(motor1EN, OUTPUT);
  pinMode(motor1PWM1, OUTPUT);
  pinMode(motor1PWM2, OUTPUT);

  // Setup Motor Servo
  servo.attach(3);
 
  radio.begin();                    //it activates the modem
  radio.openReadingPipe(1, pipe);   //determines the address of our modem which receive data
  radio.setPALevel(RF24_PA_MAX);
  radio.startListening();   
}

void loop() {
  unsigned long msLastRX;

  if (radio.available() > 0) {
    msLastRX = millis();
    radio.read(data, sizeof(data));
  
    //data X
    datag = map(data[0], 503, 1023, 0, 255);
    if(datag < 0)
    {
      digitalWrite(motor1PWM1, LOW);
      digitalWrite(motor1PWM2, LOW);
      analogWrite(motor1EN, 0);
    } 
    else
    {  
      digitalWrite(motor1PWM1, HIGH);
      digitalWrite(motor1PWM2, LOW);
      analogWrite(motor1EN, datag);
    }

    datar = map(data[1], 0, 1023, 180, 0);
    Serial.println(datar);
    Serial.print(" , ");
    Serial.println(datag);
    servo.write(datar);
  }

  if (millis() - msLastRX >= 2000) {
    otomasi();
    
  }
}

im trying to store data to float latitude_UGV with pvt.lat/10000000.0, so the formulas can use it

the pvt.lat get data from gps, im parsing without using library

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