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

Thread: T35 issue with floats/int32s conversion - maybe?

  1. #1
    Senior Member
    Join Date
    Jul 2014
    Location
    New York
    Posts
    1,839

    T35 issue with floats/int32s conversion - maybe?

    Hi all,

    I have been trying to implement a waypoint navigation method that will generate intermediate flyby waypoints that is based on what was implemented in Ardupilot. What I did was to take the sections needed to generate the points and put them in a library. That is seems to work ok at least everything compiles and runs. But in my debugging process to test the code I am running into a problem that I think is a conversion issue (don't know it that's it or something else in the code). Anyway I will try and walk through the code pieces to demonstrate what I am talking about. Again I am using a T3.5 (also tried T3.2 with same results), TD1.40 and IDE1.8.5 on a Windows 10 machine.

    first I setting up a series of test points like so:
    Code:
    Vector2f test_points1[] = {
        Vector2f(-117.041076, 32.844287),
        Vector2f(-117.037579, 32.844269),
        Vector2f(-117.038373, 32.841664),
        Vector2f(-117.035937, 32.840222),
        Vector2f(-117.033448, 32.842854),
        Vector2f(-117.032762, 32.845774),
        Vector2f(-117.039585, 32.846820)
    };
    vector2f comes from ardupilot vector2 library and is based on templates. This is a portion of the lib:
    vector2.h:
    Code:
    #ifndef VECTOR2_H
    #define VECTOR2_H
    
    #include <math.h>
    
    template <typename T>
    struct Vector2
    {
        T x, y;
    
        // trivial ctor
        Vector2<T>() {
            x = y = 0;
        }
    
        // setting ctor
        Vector2<T>(const T x0, const T y0) : x(x0), y(y0) {
        }
    
        // function call operator
        void operator ()(const T x0, const T y0)
        {
            x= x0; y= y0;
        }
    
    ....
    typedef Vector2<int16_t>        Vector2i;
    typedef Vector2<uint16_t>       Vector2ui;
    typedef Vector2<int32_t>        Vector2l;
    typedef Vector2<uint32_t>       Vector2ul;
    typedef Vector2<float>          Vector2f;
    In the ino file I have:
    Code:
    struct Location loc;
    
    static struct Location location_from_point(Vector2f pt)
    {
        loc.lat = pt.x * 1.0e7f;
        loc.lng = pt.y * 1.0e7f;
        loc.alt = 0;
        return loc;
    }
    stuct Location is defined in location.h as:
    Code:
    struct Location
    {
        long lat = 0;
        long lng = 0;
        long alt = 0;
    };
    When I do some debug prints:
    Code:
    test_points1[0].x  = -117.04107666  when it should read -117.041076, very small delta - probably ok
    setting
    Code:
    struct Location Aloc = location_from_point(test_points1[i]);
    results in:
    Code:
    A(1)=-1170410752; A(2)=328442880;
    which should be
    Code:
    -1170410760, 328442870
    I am attaching everything in a couple of zip files - just in case.
    Flyby_Waypoint.zip AP_Math_freeimu.zip

    Any help would be appreciated

    thanks
    Mike

  2. #2
    Senior Member+ manitou's Avatar
    Join Date
    Jan 2013
    Posts
    1,584
    float only has precision of 6 decimal digits, so for higher precision you may need to switch to double. double will make computations slower, since T3.5 can do float's in hardware.

    https://en.wikipedia.org/wiki/Single...g-point_format

  3. #3
    Senior Member
    Join Date
    Jul 2014
    Location
    New York
    Posts
    1,839
    thanks manitou. That seemed to be the problem. As a quick test I changed the 1e7f to 1e6f as a quick test and things got much better - at least now I can continue debugging. At some point I will have to add double to the vectors and change the rest of the libs as well but at least for now is good enough.

    UPDATE 11/26/17
    I changed to doubles and still running into something that is confusing me between doubles and floats so I ran a little test sketch:
    Code:
    void setup() {
      Serial.begin(115200);
      delay(5000);
      // put your setup code here, to run once:
      float x = -73.814664;
      double y = -73.814664;
      int32_t z = -73814664;
    
      Serial.printf("x=%f;\n",x);
      Serial.printf("y=%lf;\n",y);
      Serial.printf("z=%li; ",z);
      Serial.println();
    }
    
    void loop() {}
    The results showed no difference in precision between double and float when I printed it out? I am confused now:
    x=-73.814667;
    y=-73.814667;
    z=-73814664;

    Thanks
    Mike
    Last edited by mjs513; 11-26-2017 at 01:46 PM.

  4. #4
    Senior Member+ manitou's Avatar
    Join Date
    Jan 2013
    Posts
    1,584
    ultimately you are at the mercy of how Serial.printf handles floats/doubles. For higher precision output, look at sketch at end of the following thread
    https://forum.pjrc.com/threads/21479...sion-of-double

    also teensy compiler for T3.5 coerces floating point constants to float, for double constant you need an L, e.g.
    double y = -73.814664L;

  5. #5
    Senior Member
    Join Date
    Jul 2014
    Location
    New York
    Posts
    1,839
    the issue seemed to boil down to your second point. I added "L" to all the double constants and that seemed to do the trick. I never realized that was the case.

    As for your first point I used "%lf" for double precision values in printf and it printed the correct values - I did compare it to the double2s function in the referenced thread and they were exactly the same.

    Thanks again. Now for some more debugging.
    Mike

Posting Permissions

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