Timer for pololu 3pi robot

Hello! Currently working on 3pi robot coding program, that will show Watt hour charged.
I got most of the coding done, but have a problem with determining how long the 3pi robot have charged (Seconds.)

#include "mbed.h"
#include "m3pi.h"
#include "m3pi.cpp"

//Watt hour function
void Wh_func(void);
void Wh_func(void)
{
    //Variable with all collected data
    float Wh=0;
    //Placeholder - Collects temporary data and adds to "Wh"
    float Wh_0=0;
    m3pi pi;
    //Placeholder for pi.battery function (Gets float value for voltage)
    float bat = pi.battery();
    while(pi.left_motor(0) & pi.right_motor(0)==1)
    {
        //Clear LCD
        pi.cls();
        //Locate the cursor on the display 
        pi.locate(0,0); 
        pi.print("Charging: Watt hour",19);
        // Watt calculation - "0.16" = Current (160 mA)
        Wh_0 = bat*0.16*("Chargetime"*3600);
        Wh+=Wh_0;
        //Clear LCD
        pi.cls();
        //Locate the cursor on the display 
        pi.locate(0,0); 
        pi.print("%.2f Wh",Wh);
        wait(5.0);
    }
    return;
}

m3pi pi;

int main()
{
        pi.locate(0,1);
        wait(3.0);
        Wh_func();
        wait(2.0);
}

Is there any function/parameter or module on the 3pi, that can help me with determining how long the 3pi have been standing still (Left and right motor speed = 0).

(Assuming that 3pi will only charge when its standing still).

Thanks in advance!

Hello.

I moved your post to our “Robots” subforum, which seemed more appropriate.

There is no function for the 3pi robot that specifically measures the time it has been stationary. Since it looks like you are using the m3pi library and probably programming an mbed development board for the m3pi robot, you could use the mbed’s Timer library to keep track of the time when the motors are stopped and the time when they are restarted then return the difference of the two values.

By the way, does your code compile successfully, including that function? As far as we know, the m3pi library’s left_motor and right_motor functions do not return values.

Also, in the future, please post your code as text (that is properly formatted by putting three backticks (```) before and after a code block) instead of screenshots.

- Amanda

Hi. Thanks for the response!

The mbed’s timer library sure will come in handy.

About the left and right motor functions, i can see that, as you mentioned, they can’t return values.
So it seems i have to find another way, to determine when the robot is stationary.
Any suggestions? :open_mouth:

It looks like in your first post, you replaced the screenshot of your original code with an updated version as text, which makes it difficult for others to follow this discussion. In the future, if you want to show your updated code, please copy and paste that code in to your new post.

As for your question, in your original code, you could have changed the data type for Lspeed0 and Rspeed0 to bool and assign them to true only after setting the left and right motor to speed 0, respectively, like so:

pi.left_motor(0);
bool Lspeed0 = true;

Since it sounds like you are new to programming, you might consider looking at some online C++ tutorials before continuing on.

- Amanda