Use conversion functions without NewPing object?

Issue #6 resolved
Christopher Chavez created an issue

Should the conversion functions convert_cm and convert_in be declared as static for use without a NewPing object? (I am computing the difference between µs readings of two sensors before converting it to units of distance, and for clarity I tried using NewPing::convert_cm() instead of calling the method through one of the objects.)

Comments (7)

  1. Tim Eckel repo owner

    I believe I understand what you're asking.

    First, you can use the convert _cm() and convert_in() with NewPing by just calling it like this: sonar.ping_cm(). All it does is divide by a defined value. If you want to do the conversion when not using a NewPing object, you can do it by simply dividing the ping time by US_ROUNDTRIP_CM or US_ROUNDTRIP_IN.

  2. Tim Eckel repo owner

    Function is called via sonar.ping_cm() or sonar.ping_in(). But, you can also do the conversion on a non-NewPing object by dividing ping time by US_ROUNDTRIP_CM or US_ROUNDTRIP_IN.

  3. Christopher Chavez reporter

    I have since modified the sketch to instead only convert a single reading, but for reference here's an example of what I was trying to do:

    // ---------------------------------------------------------------------------
    // Based on Example NewPing library sketch
    // ---------------------------------------------------------------------------
    
    #include <NewPing.h>
    
    //first sensor is SR04
    #define TRIGGER_PIN_1  44  // Arduino pin tied to trigger pin on the ultrasonic sensor.
    #define ECHO_PIN_1     45  // Arduino pin tied to echo pin on the ultrasonic sensor.
    //second sensor is SRF05 (single pin mode)
    #define TRIGGER_PIN_2  12  // Arduino pin tied to trigger pin on the ultrasonic sensor.
    #define ECHO_PIN_2     12  // Arduino pin tied to echo pin on the ultrasonic sensor.
    #define MAX_DISTANCE 200 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.
    
    //create NewPing objects
    NewPing sonar1(TRIGGER_PIN_1, ECHO_PIN_1, MAX_DISTANCE); // NewPing setup of pins and maximum distance.
    NewPing sonar2(TRIGGER_PIN_2, ECHO_PIN_2, MAX_DISTANCE); // NewPing setup of pins and maximum distance.
    
    /*  Purpose of offset is to measure microseconds between sensors
        that should be returning identical readings when parallel to object
        i.e. t1 == t2 + offset
    */
    const unsigned int offset = 79;
    
    void setup() {
      Serial.begin(115200); // Open serial monitor at 115200 baud to see ping results.
    }
    
    void loop() {
      //read us from each sensor
      unsigned int t1 = sonar1.ping();
      delay(50);                     // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.
      unsigned int t2 = sonar2.ping() + offset;
      delay(50);                     // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.
    
      //compute difference
      unsigned int difference_us = abs((int)(t1 - t2));
    
      //convert difference to cm and print
      unsigned int difference_cm = NewPing::convert_cm(difference_us);
    
      Serial.print("Difference: ");
      Serial.print(difference_cm);
      Serial.println("cm");
    }
    

    Compiler error:

    /Users/christopherchavez/git/khakis/static_test/static_test.ino: In function 'void loop()':
    static_test:40: error: cannot call member function 'unsigned int NewPing::convert_cm(unsigned int)' without object
       unsigned int difference_cm = NewPing::convert_cm(difference_us);
                                                                     ^
    Using library NewPing in folder: /Users/christopherchavez/Documents/Arduino/libraries/NewPing (legacy)
    exit status 1
    cannot call member function 'unsigned int NewPing::convert_cm(unsigned int)' without object
    

    Were the convert_cm() and convert_in() methods are declared as static in NewPing.h, the code compiles and functions properly.

    To me, the point of declaring the conversion methods as static was because they do not use any object methods or attributes, or for using the NewPing conversions where a sensor might not even be present (although I am not immediately aware of examples). It may be appropriate to leave them as object methods if there is any intention to make them dependent on the object.

    Also, because I am converting a difference in my case, I must use abs() beforehand since the input and output of the conversions is unsigned; this may limit their practical use as static methods. As you've explained, the conversions are trivial enough to perform usng the constants directly. Thanks for looking into this though!

  4. Tim Eckel repo owner

    Yes, that wouldn't work. You would need to do it like this:

      //convert difference to cm and print
      unsigned int difference_cm = sonar1.convert_cm(difference_us);
    

    Or, like this:

      //convert difference to cm and print
      unsigned int difference_cm = difference_us/US_ROUNDTRIP_CM;
    

    The library objects can't be called without the object. But, the above two should work perfectly.

    Tim

  5. Christopher Chavez reporter

    I noticed that conversion methods are now static in v1.8, so just wanted to leave a note here for reference. Thanks for including this!

  6. Log in to comment