de mirageiii2009 » Ven 20 Jan 2017 00:03
Bonsoir,
Merci pour vos réponses intéressantes qui me dépassent un peu, vues mes faibles connaissances..
Voici les fichiers lua (adhemar.lua), .ino et le message de plantage sur le FSUIPC4.log :
=======================================================================================================
=======================================================================================================
=======================================================================================================
=======================================================================================================
--------------- LUA Script Program for FSX/Arduino communication ---------------
---------------------------- Switch and relays ---------------------------------
-----------------------------16 Janvier 2017 -----------------------------------
------------------ File Name : ADHEMAR.lua ----------------------------
------------------ Information :CARTE ADHEMAR - COM 19-----------
speed = 115200 --Communication setting at 115200 baud
handshake = 0
serial_wait = 100
dev1 = com.open("COM19", speed, handshake)--RelayBoard N°1
if dev1 == 0 then
ipc.display("Error in opening device(s) port(s)")
ipc.sleep(5000)
ipc.exit()
end
ipc.display("Connected to COM 19 ")--Display message
ipc.setdisplay(5,50,280,400) --Size of display window (initx,inity,width,length)
--------------- Relayssection---------------
----Readings from simulator
Batteryvoltage =ipc.readDBL(0x2834)-- 64 bit
MasterBattSwitch =ipc.readSD (0x281C)-- 32 bit
GeneratorSwitch =ipc.readSD (0x3B78)-- 32 bit
EngineOilpressure =ipc.readUW (0x08BA)-- 16 bit
BpCarPressure =ipc.readSD (0x08F8)-- 32 bit
AutoC =ipc.readSD (0x07BC)-- 32 bit
Hyd1 =ipc.readSD (0x08D8)-- 32 bit
KiasRaw =ipc.readSD (0x02BC)-- 32 bit
AirBrakes =ipc.readSD (0x0BD4)-- 32 bit
RpmRaw =ipc.readUW (0x0898)-- 16 bit
GenAlternBusVolt =ipc.readDBL(0x2880)-- 64 bit
GroundFlight =ipc.readUW(0x0366)-- 16 bit
GearCtl =ipc.readSD(0x0BE8)-- 32 bit
LhGear =ipc.readSD(0x0BF4)-- 32 bit
RhGear =ipc.readSD(0x0BF0)-- 32 bit
NoseGear =ipc.readSD(0x0BEC)-- 32 bit
RhExtRaw =ipc.readSD (0x1254)-- 32 bit
CenTankARaw =ipc.readSD (0x0B74)-- 32 bit
CenTankBRaw =ipc.readSD (0x1244)-- 32 bit
CenTankCRaw =ipc.readSD (0x124C)-- 32 bit
LhExtRaw =ipc.readSD (0x125C)-- 32 bit
LhTankRaw =ipc.readSD (0x0B7C)-- 32 bit
RhTankRaw =ipc.readSD (0x0B94)-- 32 bit
TasRaw =ipc.readUW (0x11D0)-- 16 bit
ThrotPos =ipc.readUW (0x088C)-- 16 bit -- Throtlle position
TrueAirSpeedRaw =ipc.readSD (0x02B8)-- 32 bit -- TAS
TotalAirTemp =ipc.readSW (0x11D0)-- 16 bit -- TAT
OutsideAirTempRaw =ipc.readSW (0x0E8C)-- 16 bit -- OAT
FuelTotQtyLbs =ipc.readSD (0x126C)-- 32 bit
MachFsxRaw =ipc.readUW (0x11C6)-- 16 bit -- Mach number FSX
EngineFailureFire =ipc.readSD (0x3AE0)-- 32 bit -- When Engine Falure - FIRE light comes ON
--Convert-
Rpmpercent = math.floor(((RpmRaw * 100) / 16384)+ .5)
Kias = math.floor(( KiasRaw /128)+ .5)
Tas = math.floor ((TasRaw /128) + .5)
Hyd1 = Hyd1/4 --appears to be 4*psi said Peter Dowson !! so divided by 4 to get real psi
RhExt = math.floor((RhExtRaw*100 /( 128 * 65536))+.5) --Get % ->0% = empty
CenTankA = math.floor((CenTankARaw*100 /( 128 * 65536))+.5) --Get % ->0% = empty
CenTankB = math.floor((CenTankBRaw*100 /( 128 * 65536))+.5)
CenTankC = math.floor((CenTankCRaw*100 /( 128 * 65536))+.5)
LhExt = math.floor((LhExtRaw*100 /( 128 * 65536))+.5)
LhTank = math.floor((LhTankRaw*100 /( 128 * 65536))+.5)
RhTank = math.floor((RhTankRaw*100
/( 128 * 65536))+.5)
TatCelsius = math.floor((TotalAirTemp -32) * (5/9) / 142.8)
TatFsx = math.floor( (TatCelsius * 256)+0.5)
TrueAirSpeed = math.floor ((TrueAirSpeedRaw / 128) +.5)
OutsideAirTemp = math.floor ((OutsideAirTempRaw / 256) +.5)
SpeedOfSound =((math.sqrt (OutsideAirTemp + 273))* 39)
FuelTotQtyLiter = math.floor((FuelTotQtyLbs /1.77 )+.5)
MachFsxAjusted = ((math.floor ((MachFsxRaw / 20480)*100)+ .5)/100)
-- 1ERE CARTE (ADHEMAR SYSTEM)
Close = 0
---- Relay 38 #1
SendToRelay(dev1,"R", "38", Close )-- call function and set relay
---- Relay 39 #2
SendToRelay(dev1,"R", "39", Close )-- call function and set relay
---- Relay 40 #3
SendToRelay(dev1,"R", "40", Close )-- call function and set relay
---- Relay 41 #4
SendToRelay(dev1,"R", "41", Close )-- call function and set relay
---- Relay 42 #5
SendToRelay(dev1,"R", "42", Close )-- call function and set relay
---- Relay 43---ADHEMAR - LO SPEED #6
if GearCtl == 16383 and Kias <= 195 then LoAdhemLight = 1 end
if GearCtl == 16383 and Kias > 195 then LoAdhemLight = 0 end
if GearCtl == 0 and Kias <= 184 then LoAdhemLight = 1 end
if GearCtl == 0 and Kias > 184 then LoAdhemLight = 0 end
SendToRelay(dev1,"R", "43" , LoAdhemLight )-- call function and set relay
---- Relay 44---ADHEMAR - MID SPEED #7
if GroundFlight == 1 then MidAdhemLight = 0 end
if GearCtl == 16383 and Kias >= 168 and Kias <= 250 then MidAdhemLight = 1 end
if GearCtl == 16383 and Kias > 250 then MidAdhemLight = 0 end
if GearCtl == 16383 and Kias < 168 then MidAdhemLight = 0 end
if GearCtl == 0 and Kias >= 158 and Kias <= 230 then MidAdhemLight = 1 end
if GearCtl == 0 and Kias > 230 then MidAdhemLight = 0 end
if GearCtl == 0 and Kias < 158 then MidAdhemLight = 0 end
SendToRelay(dev1,"R", "44" , MidAdhemLight )-- call function and set relay
---- Relay 45---ADHEMAR - HI SPEED #8
if GroundFlight == 1 then HiAdhemLight = 0 end
if GearCtl == 16383 and Kias >= 222 and Kias <= 275 then HiAdhemLight = 1 end
if GearCtl == 16383 and Kias > 275 then HiAdhemLight = 0 end
if GearCtl == 16383 and Kias < 222 then HiAdhemLight = 0 end
if GearCtl == 0 and Kias >= 205 and Kias <= 245 then HiAdhemLight = 1 end
if GearCtl == 0 and Kias > 245 then HiAdhemLight = 0 end
if GearCtl == 0 and Kias < 205 then HiAdhemLight = 0 end
SendToRelay(dev1,"R", "45" , HiAdhemLight )-- call function and set relay
-- 2EME CARTE (LOI SOL-VOL)
---- Relay 46---LOI SOL/VOL
if GroundFlight == 0 and GearCtl == 16383 then Solenoid = 1 else Solenoid = 0 end
SendToRelay(dev1,"R", "46" , Solenoid )-- call function and set relay
---- Relay 47
SendToRelay(dev1,"R", "47", Close )-- call function and set relay
---- Relay 48
SendToRelay(dev1,"R", "48", Close )-- call function and set relay
---- Relay 49
SendToRelay(dev1,"R", "49", Close )-- call function and set relay
---- Relay 50
SendToRelay(dev1,"R", "50", Close )-- call function and set relay
---- Relay 51
SendToRelay(dev1,"R", "51", Close )-- call function and set relay
---- Relay 52
SendToRelay(dev1,"R", "52", Close )-- call function and set relay
---- Relay 53
SendToRelay(dev1,"R", "53", Close )-- call function and set relay
-------------- FUNCTIONS ----------------
function SendToRelay (Dev,Board,RelNber,RelValue) -- declare Send to relay function
com.write(Dev,Board)--marker for relay board --> name of 1st board is "R" (other boards will be identified as : S...T...U... etc)
com.write(Dev,RelNber)--marker for relay number --> will be 1 for relay1
com.write(Dev,",")--separator --> Data separator
com.write(Dev,RelValue)--write value 0 if Switch is Off or battery voltage <20 vdc
com.write(Dev,",")--separator --> Data separator
end
=======================================================================================================
=======================================================================================================
FICHIER .ino
==============================================================================================================================================================================================================
//-------- Controlling x relays receiving data from a LUA script thru serial ----------//
//--------- JP jan 2017 ------------------------------------------------------------//
int RelayNber,State;
int Dx ;// delay if needed
int RelayPin []= {53, 52, 51, 50, 49, 48, 47, 46, 45, 44, 43, 42, 41, 40, 39, 38}
;//
int Relnber =16; //How many relays on this board ,here 11 relays
int LogicON = LOW;
int LogicOFF = HIGH;
int RelayState ;
char n,Marker;
////Note : Format of data if console to be used --> R1,1 (relay1 On) R1,0 (relay1 Off) R2,1 (relay2 On) R2,0 (relay2 Off)///
void setup()
{
Serial.begin(115200);//COM speed(baud rate)
for(n=0;n<Relnber;n++ )
{
pinMode(RelayPin [n], OUTPUT);
}
}
void loop()
{//loop
Dx=0;//delay is set to zero millisec
if(Serial.available()>0)
{//sa
Marker = Serial.read();//read Marker for category
if( Marker == 'R' )
{
RelayNber = Serial.parseInt();//get relay nber
State = Serial.parseInt(); // get numeric character BEFORE the comma
RelayBoard (RelayNber,State);// call function
}
}//sa
} //loop
/////// function for relay board
void RelayBoard (int RelayNber,int State)
{
if(State == 0)//off
{
digitalWrite(RelayPin[(RelayNber-38)],LogicOFF);// Turns Relay --> Off
}
if(State == 1)//On
{
digitalWrite(RelayPin[(RelayNber-38)],LogicON); // Turns Relays -->On
}
}
/////////
=======================================================================================================
=======================================================================================================
Message FSUIPC4.log
=======================================================================================================
=======================================================================================================
44125 *** LUA Error: P:\Lockheed Martin\Prepar3D v3\Modules\ADHEMAR.lua:92: attempt to call global 'SendToRelay' (a nil value)