Skip to content

Commit

Permalink
update the auto-correction code.
Browse files Browse the repository at this point in the history
  • Loading branch information
LiuYuancheng committed Nov 18, 2023
1 parent 8b569fd commit 7f9da9d
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 3 deletions.
8 changes: 7 additions & 1 deletion src/metroEmuUI/railwayAgent.py
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,12 @@ def checkNear(self, posX, posY, threshold):
if dist <= threshold: return True
return False

def checkTHsensor(self, posX, posY, threshold):
""" Check the train head sensor detection."""
pos = self.pos[0]
dist = math.sqrt((pos[0] - posX)**2 + (pos[1] - posY)**2)
return dist <= threshold

#--AgentTrain------------------------------------------------------------------
def checkCollFt(self, frontTrain, threshold = 25):
""" Check whether their is possible collision to the front train.
Expand All @@ -505,7 +511,7 @@ def checkSignal(self, signalList):
"""
for singalObj in signalList:
x, y = singalObj.getPos()
if self.checkNear(x, y, 20):
if self.checkNear(x, y, 5) or self.checkTHsensor(x, y, 20):
speed = 0 if singalObj.getState() else gv.gTrainDefSpeed
self.setTrainSpeed(speed)
break
Expand Down
29 changes: 27 additions & 2 deletions src/metroEmuUI/railwayMgr.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ def _initSignal(self):
signal = agent.AgentSignal(self, info['id'], info['pos'], dir=info['dir'])
signal.setTriggerOnSensors(info['tiggerS'], info['onIdx'])
signal.setTriggerOffSensors(info['tiggerS'], info['offIdx'])
self.signals['nsline'] .append(signal)
self.signals['nsline'].append(signal)

# set all the signal on track ccline
trackSignalConfig_cc = [
Expand Down Expand Up @@ -461,6 +461,29 @@ def updateSignalState(self, key):
for signal in self.signals[lineKey]:
signal.updateSingalState()

def autoCorrectSignalState(self):
""" Correct the CC line signal if got error, if both signal are on, turn
off the cc line signal to make the cc line train pass 1st. This function
actived by the
"""
checkPair = [
('nsline', (0, 1)),
('nsline', (2,)),
('nsline', (3,)),
('weline', (6, 7)),
('weline', (4, 5)),
('weline', (2, 3)),
('weline', (0, 1))
]
for i, signal in enumerate(self.signals['ccline']):
checkRst = signal.getState()
if checkRst:
key, val = checkPair[i]
for idx in val:
checkRst = checkRst and self.signals[key][idx].getState()
# If both signal on state happens correct the CC line signal.
if checkRst: signal.setState(False)

#-----------------------------------------------------------------------------
def periodic(self , now):
""" Periodicly call back function. This function need to be called before the
Expand Down Expand Up @@ -492,7 +515,9 @@ def periodic(self , now):
# updaste all the signal, if test mode (not connect to PLC) call the
# buildin signal control logic, else the data manager will read the signal
# infromation from PLC then do the auto update.
if gv.gTestMD or gv.gJuncAvoid : self.updateSignalState(key)
if gv.gTestMD or gv.gJuncAvoid :
self.updateSignalState(key)
self.autoCorrectSignalState()

# update the station train's docking state
for key, val in self.stations.items():
Expand Down

0 comments on commit 7f9da9d

Please sign in to comment.