Reconnect wrapper v1 complete

This commit is contained in:
cs-powell
2025-03-29 03:23:26 -04:00
parent 07526ca5a3
commit fc068ef1d0
2 changed files with 231 additions and 145 deletions

View File

@@ -29,8 +29,19 @@ class AircraftLandingModel(pyactr.ACTRModel):
brakeDREF = "sim/cockpit2/controls/parking_brake_ratio"
wheelSpeedDREF = "sim/flightmodel2/gear/tire_rotation_speed_rad_sec"
wheelWeightDREF = "sim/flightmodel/parts/tire_vrt_def_veh"
self.sources = [airspeedDREF,rollDREF,magneticHeadingDREF,verticalSpeedDREF,altitudeAGLDREF,pitchDREF,brakeDREF,wheelSpeedDREF,wheelWeightDREF]
# self.sources = [airspeedDREF,rollDREF,magneticHeadingDREF,verticalSpeedDREF,altitudeAGLDREF,pitchDREF,brakeDREF,wheelSpeedDREF,wheelWeightDREF]
self.sources = {
"airspeed" : airspeedDREF,
"roll" : rollDREF,
"heading" : magneticHeadingDREF,
"verticalSpeed" : verticalSpeedDREF,
"altitude": altitudeAGLDREF,
"pitch" : pitchDREF,
"brake": brakeDREF,
"wheelSpeed": wheelSpeedDREF,
"wheelWeight": wheelWeightDREF
}
"""
Initial Initialization of destination Variables and loading into destinations array
"""
@@ -54,8 +65,19 @@ class AircraftLandingModel(pyactr.ACTRModel):
self.brakes = brake[0]
self.wheelSpeed = wheelS[0]
self.wheelWeight = wheelW[0]
self.destinations = [self.airspeed,self.roll,self.heading,self.descent_rate,self.altitude,self.pitch,self.brakes,self.wheelSpeed,self.wheelWeight]
# self.destinations = [self.airspeed,self.roll,self.heading,self.descent_rate,self.altitude,self.pitch,self.brakes,self.wheelSpeed,self.wheelWeight]
self.destinations = {
"airspeed" : self.airspeed,
"roll" : self.roll,
"heading" : self.heading,
"verticalSpeed" : self.descent_rate,
"altitude": self.altitude,
"pitch" : self.pitch,
"brake": self.brakes,
"wheelSpeed": self.wheelSpeed,
"wheelWeight": self.wheelWeight
}
"""
Initial Initialization of target Values
"""
@@ -105,18 +127,26 @@ class AircraftLandingModel(pyactr.ACTRModel):
# while(idx < len(self.sources) - 1):
# self.variableAtlas[0] = [self.sources[idx],self.destinations[idx],self.targets[idx]]
# idx += 1
def reassignClient(self,newClient):
self.client = newClient
def getAndLoadDREFS(self):
results = self.client.getDREFs(self.sources)
results = self.client.getDREFs(self.sources.values())
idx = 0
while(idx < len(results) - 2):
self.destinations[idx] = results[idx][0]
if(idx == 1):
print("getDrefs: " + str(results[idx][0]))
print("current destination: " + str(self.destinations[idx]))
print("current main Airspeed: " + str(self.airspeed))
idx += 1
for key in self.sources:
self.destinations[key] = results[idx]
idx+=1
print("getDrefs: " + str(results[1][0]))
print("current destination: " + str(self.destinations["airspeed"]))
print("current main Airspeed: " + str(self.airspeed))
# while(idx < len(results) - 2):
# self.destinations[idx] = results[idx][0]
# if(idx == 1):
# print("getDrefs: " + str(results[idx][0]))
# print("current destination: " + str(self.destinations[idx]))
# print("current main Airspeed: " + str(self.airspeed))
# idx += 1
def printControls(self,calculated,errors,yokePull,yokeSteer,rudder,throttle):
@@ -129,7 +159,7 @@ class AircraftLandingModel(pyactr.ACTRModel):
# print("*Parameter,Target,Current,Throttle: " + "Descent Rate, " + str(self.target_descent_rate) + "," + str(self.descent_rate)+ "," + str(throttle))
parameter = ["Airspeed","Roll","Heading","Descent Rate","Altitude","Flare: Pitch", "Brakes: Wheel Speed", "Brakes: Wheel Weight"]
target = [str(round(self.target_airspeed)),str(round(self.target_roll)),str(round(self.target_heading,3)),str(round(self.target_descent_rate,3)),str(round(self.altitude,3)),str(round(self.target_pitch,3)),0, 0]
current = [str(round(self.airspeed,3)),str(round(self.roll,3)),str(round(self.heading,3)),str(round(self.descent_rate,3)),str(round(self.altitude,3)),str(round(self.pitch,3)),str(round(self.wheelSpeed,3)),str(round(self.wheelWeight,3))]
current = [str(round(self.destinations["airspeed"],3)),str(round(self.roll,3)),str(round(self.heading,3)),str(round(self.descent_rate,3)),str(round(self.altitude,3)),str(round(self.pitch,3)),str(round(self.wheelSpeed,3)),str(round(self.wheelWeight,3))]
controlVal = [str(round(yokePull,3)),str(round(yokeSteer,3)),str(round(rudder,3)),str(round(throttle,3)),str(round(self.altitude,3)),str(self.flare),str(round(self.brakes,3)),str(round(self.brakes,3))]
header_row = "{:<20} {:<20} {:<20} {:>10}"
@@ -307,7 +337,7 @@ class AircraftLandingModel(pyactr.ACTRModel):
self.Ki = 0.01 ## Increase Control Authority to compensate for decreasing airspeed
print("Altitude < 500; Flare Set True")
if(self.wheelWeight > 0.01 and self.wheelSpeed > 1 and self.airspeed < 1 and self.brakes == 1):
if(self.wheelWeight > 0.01 and self.wheelSpeed > 1 and self.destinations["airspeed"] < 1 and self.brakes == 1):
self.inProgress = False
def simulationStatus(self):

View File

@@ -9,140 +9,196 @@ def runExperiment(title):
print("Model Test: Xplane setting up connection")
print("Setting up simulation")
with xpc.XPlaneConnect() as client:
# Verify connection
startTime = 0
endTime = 0
difference = endTime - startTime
while(difference < 10):
print("Time Elapsed: -----> " + str(difference))
try:
# If X-Plane does not respond to the request, a timeout error
# will beff raised.
client.getDREF("sim/test/test_float")
with xpc.XPlaneConnect() as client:
# Verify connection
# try:
# If X-Plane does not respond to the request, a timeout error
# will beff raised.
client.getDREF("sim/test/test_float")
# except:
# print("Error establishing connection to X-Plane.")
# print("Exiting...")
# return
cogModel = AircraftLandingModel(client)
# # Set position of the player aircraft
# print("Setting position")
# # Lat Lon Alt Pitch Roll Yaw Gear
# posi = [37.524, -122.06899, 2500, 0, 0, 0, 1]
# client.sendPOSI(posi)
# # Set position of a non-player aircraft
# print("Setting NPC position")
# # Lat Lon Alt Pitch Roll Yaw Gear
# posi = [37.52465, -122.06899, 2500, 0, 20, 0, 1]
# client.sendPOSI(posi, 1)
# # Set angle of attack, velocity, and orientation using the DATA command
# print("Setting orientation")
# data = [\
# [18, 0, -998, 0, -998, -998, -998, -998, -998],\
# [ 3, 130, 130, 130, 130, -998, -998, -998, -998],\
# [16, 0, 0, 0, -998, -998, -998, -998, -998]\
# ]
# client.sendDATA(data)
# client.pauseSim(True)
# # Set position of the player aircraft
# print("Setting Experiment Position: Denver Airport Runway")
# # Lat Lon Alt Pitch Roll Yaw Gear
# posi = [39.945, -104.70, 2500, 0, 0, 0, 1] #3NM Approach for Denver Intl Runway 16R (16,000 feet)
# client.sendPOSI(posi)
# quat = (1.0,0.0,0.0,173.0)
# quatDref = "sim/flightmodel/position/q"
# client.sendDREF(quatDref,quat)
# speed = (1.0,0.0,0.0,173.0)
# client.sendDREF("sim/flightmodel/position/indicated_airspeed",speed)
# client.pauseSim(False)
# # Set angle of attack, velocity, and orientation using the DATA command
# print("Setting orientation")
# data = [\
# [18, 0, -998, 0, -998, -998, -998, -998, -998],\
# [ 3, 130, 130, 130, 130, -998, -998, -998, -998],\
# [16, 0, 0, 0, -998, -998, -998, -998, -998]\
# ]
# client.sendDATA(data)
groundLevel = 5434
offset = 4000
altitudeFEET = groundLevel + offset
altitudeMETERS = altitudeFEET/3.281
altitude = altitudeMETERS
"""
Set orientation and Position
1 - Time
3- Speeds: [3, V-indicated, 3, 2, 2, -998, VindMPH, V trueMPHas, vtrue MPHgs],\
16 - Angular Velocities
17 - PitchRoll and Headings: [17, Pitch, Roll, Heading True, -998, -998, -998, -998, -998],\
18 - Angle of Attack
20 - Latitude and Longitude
"""
location1 = [20, groundLevel + 3000, 39.96239, -104.69713, -998, -998, -998, -998, -998]
testLocation = [20, -998, 27.20579, -80.08621, altitude, -998, -998, -998, -998] # 27.20579°N/80.08621°W
kias = -998
keas = -998
ktas = -998
ktgs = -998
mph = -998
mphas = 80
mphgs = 80
print("Setting orientation for test")
# client.sendDREF("sim/operation/override/override_planepath",1)
# client.pauseSim(True)
# client.sendDREF("sim/flightmodel/position/local_y",2000)
# client.sendDREFs(["sim/flightmodel/position/P","sim/flightmodel/position/Q","sim/flightmodel/position/R"],[0,0,0])
# client.sendDREF("sim/flightmodel/position/q",[0.3, 0, 0, 0.6])
# data = [\
# [3, -998, -998, -998, -998, -998, -998, mphas, mphgs],\
# [16, 0, 0, 0, -998, -998, -998, -998, -998],\
# [17, 0, 0, 0, -998, -998, -998, -998, -998],\
# [18, -998, -998, -998, -998, -998, -998, -998, -998],\
# [19, -998, -998, -998, -998, -998, -998, -998, -998],\
# testLocation\
# ]
# client.sendDATA(data)
# client.sendDREF("sim/operation/override/override_planepath",0)
client.pauseSim(False)
# 39.96239°N/104.69713°W
runModel = True
# Set control surfaces and throttle of the player aircraft using sendCTRL
print("Setting controls")
ctrl = [0.0, 0.0, 0.0, 0.0]
client.sendCTRL(ctrl)
# Pitch, Roll, Rudder, Throttle
# Pause the sim
# client.pauseSim(False)
count = 0
innercount = 0
clockStart = time.time()
#Doing stuff In between Test SECOND INCREMENTS
retry = 0
while(cogModel.simulationStatus() and runModel and retry < 50):
clockStart = time.time() #START TIMER
# client.pauseSim(True) # Pause Simulator
#Run Model
cogModel.update_aircraft_state()
cogModel.update_controls_simultaneously()
client.pauseSim(False) #Unpause Simulator
clockEnd = time.time() # STOP TIMER
count+=1
# print("Clock Time: " + str(clockEnd - clockStart)) ## LOG TIME TAKEN
sleep(0.05) # LET Simulator Run 50 Milliseconds
##EXCEPTION TEST BED
# try:
# clockStart = time.time() #START TIMER
# # client.pauseSim(True) # Pause Simulator
# #Run Model
# cogModel.update_aircraft_state()
# cogModel.update_controls_simultaneously()
# client.pauseSim(False) #Unpause Simulator
# clockEnd = time.time() # STOP TIMER
# count+=1
# # print("Clock Time: " + str(clockEnd - clockStart)) ## LOG TIME TAKEN
# sleep(0.05) # LET Simulator Run 50 Milliseconds
# except TimeoutError as e:
# clockStart = time.time()
# difference = 0
# print("Outer exception retry: " + str(retry))
# while(difference<10):
# print("Top of while loop")
# try:
# print("Top of TRY")
# with xpc.XPlaneConnect() as client:
# print("Top of reassign")
# cogModel.reassignClient(client)
# print("Bottom of reassign")
# ##Test the connection
# print("TOP: Testing connection")
# cogModel.client.getDREF("sim/test/test_float") ##EXCEPTION OCCURING HERE
# print("CONNECTION SUCCESFUL!!!!!!")
# print("TRY BLOCK, go back to start: " + str(retry))
# except:
# print("Top of EXCEPT")
# print("EXCEPT BLOCK, go back to start: " + str(retry))
# clockEnd = time.time() # STOP TIMER
# difference = clockEnd - clockStart
# retry+=1
# print("Time Elapsed: -----> " + str(difference))
# ##Do Nothing, go back to start of difference loop
# else:
# print("Breaking out of Timeout exception loop")
# break
startTime = time.time()
except:
print("Error establishing connection to X-Plane.")
print("Exiting...")
return
cogModel = AircraftLandingModel(client)
# # Set position of the player aircraft
# print("Setting position")
# # Lat Lon Alt Pitch Roll Yaw Gear
# posi = [37.524, -122.06899, 2500, 0, 0, 0, 1]
# client.sendPOSI(posi)
# # Set position of a non-player aircraft
# print("Setting NPC position")
# # Lat Lon Alt Pitch Roll Yaw Gear
# posi = [37.52465, -122.06899, 2500, 0, 20, 0, 1]
# client.sendPOSI(posi, 1)
# # Set angle of attack, velocity, and orientation using the DATA command
# print("Setting orientation")
# data = [\
# [18, 0, -998, 0, -998, -998, -998, -998, -998],\
# [ 3, 130, 130, 130, 130, -998, -998, -998, -998],\
# [16, 0, 0, 0, -998, -998, -998, -998, -998]\
# ]
# client.sendDATA(data)
# client.pauseSim(True)
# # Set position of the player aircraft
# print("Setting Experiment Position: Denver Airport Runway")
# # Lat Lon Alt Pitch Roll Yaw Gear
# posi = [39.945, -104.70, 2500, 0, 0, 0, 1] #3NM Approach for Denver Intl Runway 16R (16,000 feet)
# client.sendPOSI(posi)
# quat = (1.0,0.0,0.0,173.0)
# quatDref = "sim/flightmodel/position/q"
# client.sendDREF(quatDref,quat)
# speed = (1.0,0.0,0.0,173.0)
# client.sendDREF("sim/flightmodel/position/indicated_airspeed",speed)
# client.pauseSim(False)
print("except detected")
endTime = time.time()
difference = endTime - startTime
continue
# # Set angle of attack, velocity, and orientation using the DATA command
# print("Setting orientation")
# data = [\
# [18, 0, -998, 0, -998, -998, -998, -998, -998],\
# [ 3, 130, 130, 130, 130, -998, -998, -998, -998],\
# [16, 0, 0, 0, -998, -998, -998, -998, -998]\
# ]
# client.sendDATA(data)
print("Model has finished running")
#Copy data.txt to the cloudddddd using python magic and accurate filepaths
now = datetime.datetime
shutil.copy("/Users/flyingtopher/X-Plane 11/Data.txt", "/Users/flyingtopher/Desktop/Test Destination/" + title + "_" + str(now.now()) + "_" + ".txt")
groundLevel = 5434
offset = 4000
altitudeFEET = groundLevel + offset
altitudeMETERS = altitudeFEET/3.281
altitude = altitudeMETERS
"""
Set orientation and Position
1 - Time
3- Speeds: [3, V-indicated, 3, 2, 2, -998, VindMPH, V trueMPHas, vtrue MPHgs],\
16 - Angular Velocities
17 - PitchRoll and Headings: [17, Pitch, Roll, Heading True, -998, -998, -998, -998, -998],\
18 - Angle of Attack
20 - Latitude and Longitude
"""
location1 = [20, groundLevel + 3000, 39.96239, -104.69713, -998, -998, -998, -998, -998]
testLocation = [20, -998, 27.20579, -80.08621, altitude, -998, -998, -998, -998] # 27.20579°N/80.08621°W
kias = -998
keas = -998
ktas = -998
ktgs = -998
mph = -998
mphas = 80
mphgs = 80
print("Setting orientation for test")
# client.sendDREF("sim/operation/override/override_planepath",1)
# client.pauseSim(True)
# client.sendDREF("sim/flightmodel/position/local_y",2000)
# client.sendDREFs(["sim/flightmodel/position/P","sim/flightmodel/position/Q","sim/flightmodel/position/R"],[0,0,0])
# client.sendDREF("sim/flightmodel/position/q",[0.3, 0, 0, 0.6])
# data = [\
# [3, -998, -998, -998, -998, -998, -998, mphas, mphgs],\
# [16, 0, 0, 0, -998, -998, -998, -998, -998],\
# [17, 0, 0, 0, -998, -998, -998, -998, -998],\
# [18, -998, -998, -998, -998, -998, -998, -998, -998],\
# [19, -998, -998, -998, -998, -998, -998, -998, -998],\
# testLocation\
# ]
# client.sendDATA(data)
# client.sendDREF("sim/operation/override/override_planepath",0)
client.pauseSim(False)
# 39.96239°N/104.69713°W
runModel = True
# Set control surfaces and throttle of the player aircraft using sendCTRL
print("Setting controls")
ctrl = [0.0, 0.0, 0.0, 0.0]
client.sendCTRL(ctrl)
# Pitch, Roll, Rudder, Throttle
# Pause the sim
# client.pauseSim(False)
count = 0
innercount = 0
clockStart = time.time()
#Doing stuff In between Test SECOND INCREMENTS
while(cogModel.simulationStatus() and runModel):
clockStart = time.time() #START TIMER
# client.pauseSim(True) # Pause Simulator
#Run Model
cogModel.update_aircraft_state()
cogModel.update_controls_simultaneously()
client.pauseSim(False) #Unpause Simulator
clockEnd = time.time() # STOP TIMER
count+=1
# print("Clock Time: " + str(clockEnd - clockStart)) ## LOG TIME TAKEN
sleep(0.05) # LET Simulator Run 50 Milliseconds
print("Model has finished running")
#Copy data.txt to the cloudddddd using python magic and accurate filepaths
now = datetime.datetime
shutil.copy("/Users/flyingtopher/X-Plane 11/Data.txt", "/Users/flyingtopher/Desktop/Test Destination/" + title + "_" + str(now.now()) + "_" + ".txt")
input("Press any key to exit...")
## Copy Data.txt to the repository file for analysis
##Reset the sim with the keyboard shortcut (wrapper around model that waits for reconnection)
input("Press any key to exit...")
## Copy Data.txt to the repository file for analysis
##Reset the sim with the keyboard shortcut (wrapper around model that waits for reconnection)
def ex():
##Store Experiment Battery
@@ -152,7 +208,7 @@ def ex():
##wind conditions, pilot conditions
title = input("Please Enter Experiment Set Title, leave blank for trial runs")
count = 0
while(count<2):
while(count<1):
runExperiment(title)
count+=1
print("Experiment Battery Complete")