Jump to content
Sign in to follow this  
WarpD

Storing a Variable

Recommended Posts

I am currently working on a Hobbs meter (Counting hours of engine/flight time) in C++ and have come to a bit of a road block. My code needs to store an initial value for CLOCK_TOTAL_SECONDS upon loading the sim. The problem I am having is storing this value and preventing it form updating each cycle. All I wont to to is capture what the value is when the sim loads and store it as a static value. Any advice or thoughts on this would be greatly appreciated as its starting to do my brain in.CheersPaul

Share this post


Link to post
Share on other sites

Trap it :)if (!flag){ get current seconds; flag=1;}and put this in the PANEL_CONNECT section of the gauge callback.-Dai

Share this post


Link to post
Share on other sites

>Trap it :)>>if (!flag)>{> get current seconds;> flag=1;>}>>and put this in the PANEL_CONNECT section of the gauge>callback.>>-Dai>>>I'd put it in the PRE_UPDATE section... the PANEL_CONNECT can be called even when the sim isn't actually up and running.


Ed Wilson

Mindstar Aviation
My Playland - I69

Share this post


Link to post
Share on other sites

As Hobbs is typically recorded as elapsed hours + tenths of an hour, I use the following routine to capture the "start time" and keep track of a float variable for totaltime:if ( RECIP_ENGINE1_COMBUSTIONvar.var_value.n == 1 ) { if ( minutes == 6 ) { latch = 1 ; start = 1 ; } if ( seconds < 0 && start == 2 ) { seconds = 0 ; start = 1 ; } if ( start == 1 ) { // grab the exact time engine starts initialize_time = CLOCK_TOTAL_SECONDSvar.var_value.n ; // set start flag to "2" to accumulate time start = 2 ; } if ( start == 2 ) { seconds = CLOCK_TOTAL_SECONDSvar.var_value.n - initialize_time ; }else { seconds = 0 ; } // count the elapsed minutesif ( start == 2 ) { minutes = ( seconds - fmod(seconds,60) ) / 60 ;} else { minutes = 0 ; } // increment the "tenths count" every 6 minutesif ( start == 2 && minutes == 6 && latch == 1 ) { total_time = total_time + .1 ; latch = 0 ;} else { total_time = total_time ; }


Fr. Bill    

AOPA Member: 07141481 AARP Member: 3209010556


     Avsim Board of Directors | Avsim Forums Moderator

Share this post


Link to post
Share on other sites

Thanks heaps guys got it working by adding this to the PANEL_SERVICE_PRE_UPDATE section in the gauge callbackcase PANEL_SERVICE_PRE_UPDATE: if (UPDATE == 0) { MODULE_VAR INITIALCLOCK = {CLOCK_TOTAL_SECONDS}; lookup_var(&INITIALCLOCK); TESTVAR = INITIALCLOCK.var_value.n ; UPDATE = 1; };break; Works a treat :)Many Thanks Paul

Share this post


Link to post
Share on other sites

Hi peoplesi have another problem (always the way just when u think you have it figured)This is what i have......FILE *accum_time;CHAR time[10];FLOAT64 va7 = 0FLOAT64 totaltime = 0;void FSAPI GAUGECALLBACK (PGAUGEHDR pgauge, int service_id, UINT32 extra_data){ switch(service_id) {//reading from file case PANEL_SERVICE_CONNECT_TO_WINDOW: if((accum_time=fopen("VictaHobbs.dat","r"))!=NULL); { fscanf( accum_time, "%s",time); totaltime = atof(time); }; fclose(accum_time); break;//writing to file case PANEL_SERVICE_DISCONNECT: { accum_time=fopen("VictaHobbs.dat","w"); fprintf(accum_time,"%2.3fn",va7) ; fclose(accum_time) ; }; break; }}From what i understand this is what should happen1) as FS loads the file read instructions under PANEL_SERVICE_CONNECT_TO_WINDOW: should be run reading the value from VictaHobbs.dat into totaltime. 2) when the sim is exited or the plane is changed the file write instructions under PANEL_SERVICE_DISCONNECT: should be run writing the value of va7 to VictaHobbs.dat(va7 is the new calculated total time at the end of the session ie initial totaltime + time elapsed = va7)this is what is actually happening :( when FS loads there is an immediate write event and the value in VictaHobbs.dat file is overwritten with 0.000 and so the read value is 0.000 .When i exit the sim the correct value for va7 is written to VictaHobbs.dat which I confirmed by opening the file in notepad after exiting the sim.The thing is if i comment out the write instructions and put a value into VictHobbs.dat it reads the file just fine assigning totaltime the value in the dat file without it being over written .i may be missing something very obvious but i just cant see it.Any help would be much appreciated.

Share this post


Link to post
Share on other sites

Gee, some of that looks SO familiar... ;)You need to add a flag to your routine to prevent the code from being run twice. I also have now moved the file read to the POST_INSTALL case://reading from file case PANEL_SERVICE_POST_INSTALL: if (hobbs_fileread)! if((accum_time=fopen("VictaHobbs.dat","r"))!=NULL); { fscanf( accum_time, "%s",time); totaltime = atof(time); }; fclose(accum_time); hobbs_fileread = 1;You could, if you wish add a similar check to the write routine, but I've never found it necessary.


Fr. Bill    

AOPA Member: 07141481 AARP Member: 3209010556


     Avsim Board of Directors | Avsim Forums Moderator

Share this post


Link to post
Share on other sites

HiI tried your suggestion but to no avail. The problem isn't the fact that its not reading it's that the file gets overwritten by a write event while the sim is loading (during the loading bar, i have watched it happening in real time) i tried setting up an if statement that would only run the file write instructions on the second pass but that didn't work. i just cant see the logic behind this, am i correct in saying that the PANEL_SERVICE_DISCONNECT: code is only called when the panel or sim is closed? if so why is it being called while the sim is loading?? it seems oddly pointless.i don't know i'm learning all this as i go along :) probably missing something quite obvious but still cant see it. Appreciate the help.Paul

Share this post


Link to post
Share on other sites

>Hi>>i just cant see the logic behind this, am i correct in saying>that the PANEL_SERVICE_DISCONNECT: code is only called when>the panel or sim is closed? if so why is it being called while>the sim is loading?? it seems oddly pointless.>>i don't know i'm learning all this as i go along :) probably>missing something quite obvious but still cant see it. >>Appreciate the help.>>Paul>Hi,The best way to figure out what's going on is making your own tests.You could easily capture each one of the PANEL_SERVICE... passes and place it in a file - or store in variable - to see how FS handles this section.Tom

Share this post


Link to post
Share on other sites

That event gets called because when you select the aircraft in the UI, the gauges get loaded... then it gets unloaded and reloaded when you click the "Fly Now" button. It's just the way it works.


Ed Wilson

Mindstar Aviation
My Playland - I69

Share this post


Link to post
Share on other sites

>I tried your suggestion but to no avail. The problem isn't the>fact that its not reading it's that the file gets overwritten>by a write event while the sim is loading (during the loading>bar, i have watched it happening in real time) i tried setting>up an if statement that would only run the file write>instructions on the second pass but that didn't work. Odd, since that is the same code I have used with at least six products over the past five years...case PANEL_SERVICE_DISCONNECT: accum_time_ini=fopen(hobbs_total_name,"w"); fprintf(accum_time_ini,"%7.1fn",total_time + eng_time) ; fclose(accum_time_ini) ;break;


Fr. Bill    

AOPA Member: 07141481 AARP Member: 3209010556


     Avsim Board of Directors | Avsim Forums Moderator

Share this post


Link to post
Share on other sites

Just posting to say thanks for the suggestions but haven't had a chance to take a crack at them yet (dam that life thing getting in the way) will let you know how it goes Thanks again Paul

Share this post


Link to post
Share on other sites

Ok, I took another look at your code.You have two variables... va7 and totaltime. When the gauge is loaded you read the last written Hobbs value into totaltime... when the gauge is unloaded you write the value of va7 out to the file.However, va7 only gets updated if the PRE_UPDATE service call happens... which it does NOT happen when you're at the "Create A Flight" screen. However, the other two services do get called when the gauges load after selecting the aircraft in the "Create A Flight" screen.Since your va7 value never reflects the last value of totaltime... of course when you click the "Fly Now" button, it writes out a zero value to your file.


Ed Wilson

Mindstar Aviation
My Playland - I69

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

  • Tom Allensworth,
    Founder of AVSIM Online


  • Flight Simulation's Premier Resource!

    AVSIM is a free service to the flight simulation community. AVSIM is staffed completely by volunteers and all funds donated to AVSIM go directly back to supporting the community. Your donation here helps to pay our bandwidth costs, emergency funding, and other general costs that crop up from time to time. Thank you for your support!

    Click here for more information and to see all donations year to date.
×
×
  • Create New...