Jump to content
The simFlight Network Forums

Custom Offsets errors


Recommended Posts

Hello, I've been making a LUA script to get outputs of systems in A2A Comanche 250. With the code, the display in the lua window shows the correct and steady numbers.

When I look at the data, for use with link2fs, the data is "jittering". Changing from correct to incorrect. This only happens, however, when I have more than 4 ipc.write's active (turning off with comments),

four or less and it fine. I've tried changing the delay and it doesn't help.

Is there something I'm' doing wrong? 

Any help is greatly appreciated.

 

Here is the code...

 -- rounds the integer
function round(num)
num = tonumber(num)
if num == nil then return 0 end
if num >= 0 then return math.floor(num+.5)
else return math.ceil(num-.5) end
end
 
 
 while 1 do 

       Amps = round(ipc.readLvar("L:Ammeter1"))
	   EGT = round(ipc.readLvar("L:Eng1_EGTGauge"))
	   FuelFlow = round(ipc.readLvar("L:Eng1_GPH"))
	   OilTemp = round(ipc.readLvar("L:Eng1_OilTemp"))
	   OilPressure = round(ipc.readLvar("L:Eng1_OilPressure"))
	   EngRpm = round(ipc.readLvar("L:Eng1_RPMGauge"))
	   FuelPressure = round(ipc.readLvar("L:Eng1_FuelPressure"))
	   BattVolts = round(ipc.readLvar("L:Voltmeter"))
	   CylHeadTemp = round(ipc.readLvar("L:Eng1_CHTGauge"))
	   
	   
	   
	   ipc.writeUW(0x66c0,Amps)
	   --ipc.writeUW(0x66c1,EGT)
	   ipc.writeUW(0x66c2,FuelFlow)
	   --ipc.writeUW(0x66c3,OilTemp)
	   ipc.writeUW(0x66c4,OilPressure)
	   --ipc.writeUW(0x66c5,EngRpm)
	   ipc.writeUW(0x66c6,FuelPressure)
	   --ipc.writeUW(0x66c7,BattVolts)
	   ipc.writeUW(0x66c8,CylHeadTemp)
	   
	   ipc.display("Amps: "..Amps.."\nEGT: "..EGT.."\nFuel Flow: "..FuelFlow.."\nOil Temp: "..OilTemp.."\nOil Pressure: "..OilPressure.."\
Eng RPM: "..EngRpm.."\nFuel Pressure: "..FuelPressure.."\nBatt Volts: "..BattVolts.."\nCyl Head Temp: "..CylHeadTemp,1,"")
	   
	   
    ipc.sleep(1000) 

 end 

Thanks

StevenT

Link to comment
Share on other sites

It isn't anything to do with how many writes you are doing, it is simply that all your values are overlapping.

 

"writeUW" means "write an Unsigned Word". A word is 16 bits in size, or 2 bytes. You are writing 2 bytes at 1 byte spacing -- 66C0, 66C1 ...66C8. So every subsequent write is overwriting the 2nd byte (the high 8 bits) of each previous value!

 

BTW your code would be much more efficient if you used ipc.writeStruct. You could write all the values to offsets in one call the.

 

Pete

Link to comment
Share on other sites

Thank you so much for the reply. I am new to the offset thing and didn't realize how the bits and bytes worked. I set the offsets at 2 byte spacing and everything is working as expected.

 

Tried to use the ipc.writeStruct but I think I'm not understanding how it works. 

If I write it as... "ipc.writeStruct(0x66c0,Amps,EGT,FuelFlow,OilTemp,OilPressure,EngRpm,FuelPressure,BattVolts,CylHeadTemp)" will it use the 2 byte spacing or fit in as needed?

Do I need to add "9UW" after the offset or will it just know?

 

Thanks again,

 

StevenT

Link to comment
Share on other sites

Tried to use the ipc.writeStruct but I think I'm not understanding how it works. 

If I write it as... "ipc.writeStruct(0x66c0,Amps,EGT,FuelFlow,OilTemp,OilPressure,EngRpm,FuelPressure,BattVolts,CylHeadTemp)" will it use the 2 byte spacing or fit in as needed?

Do I need to add "9UW" after the offset or will it just know?

 

It needs the count and size, of course. It cannot guess! So it should be:

 

ipc.writeStruct(0x66c0,"9UW",Amps,EGT,FuelFlow,OilTemp,OilPressure,EngRpm,FuelPressure,BattVolts,CylHeadTemp)

 

Incidentally, using "UW" you are assuming all of the values will be positive (U = Unsigned). That won't always be true for some -- temperatures and voltages, for instance. If you treat a negative value as unsigned it will appear as a very large positive number! You need really to use SW for the type. But be sure the values don't go outside the range -32768 to +32767. The range of UW is 0-65535.

 

Pete

 

 

 

Pete

Link to comment
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
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use. Guidelines Privacy Policy We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.