View unanswered posts | View active topics It is currently Mon Feb 27, 2017 12:18 pm

Reply to topic  [ 2 posts ] 
nPgmTime Help with Gyro 
Author Message

Joined: Wed Oct 05, 2011 2:49 pm
Posts: 12
Post nPgmTime Help with Gyro
So here is the code I'm using to generate a heading from a HiTechnic Gyroscope.
I'm not able to get the nPgmTime to report any times or any differences in times.
I end up with lots of zero's for everything (heading, currTime, prevTime).
I am running the program through the debugger. Is that my problem?

float setHeading()
float heading = 0.0;
float currTime;
float prevTime = nPgmTime;
currTime = prevTime;
heading += ((float)HTGYROreadRot(HTGYRO))*(currTime - prevTime)/1000;

return heading;


Mon Nov 07, 2011 7:13 pm
User avatar

Joined: Sun Nov 15, 2009 5:46 am
Posts: 1523
Post Re: nPgmTime Help with Gyro
There are many issues with your code. You can try the following:
float setHeading()
    static float heading = 0.0;
    static long prevTime = 0;

    if (prevTime == 0)
        prevTime = nPgmTime;
        long currTime = nPgmTime;
        heading += ((float)HTGYROreadRot(HTGYRO))*(currTime - prevTime)/1000.0;
        prevTime = currTime;
    return heading;

Make sure the setHeading() function is called periodically in your robot main loop so it will keep integrating.
With your original code, the first problem is that heading is a local variable and will be destroyed when the setHeading function exits. So every time setHeading is called, the heading variable will get re-initialized back to 0.0 so you were not doing integration at all. So is prevTime, it doesn't survive across function entry and exit. Also, currTime and prevTime has the exact same value, so currTime - prevTime will always give you zero which you multiply to the Gyro reading thus giving you zero and then added to heading which again is 0 and so you get a ZERO.

Mon Nov 07, 2011 8:02 pm
Display posts from previous:  Sort by  
Reply to topic   [ 2 posts ] 

Who is online

Users browsing this forum: No registered users and 2 guests

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  

Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software for PTF.