| 1 |
#--------------------------------------------------------------------------- |
| 2 |
# |
| 3 |
# Title : Emesary based 'real time' module executive |
| 4 |
# |
| 5 |
# File Type : Implementation File |
| 6 |
# |
| 7 |
# Description : Uses Emesary notifications to permit Nasal subsystems to be invoked in |
| 8 |
# : a controlled manner. |
| 9 |
# : |
| 10 |
# : Sends out a FrameNotification for each frame recipient can implement |
| 11 |
# : workload reduction as appropriate based on skipping frames (2=half, |
| 12 |
# : 4=quarter etc.) because some code can safely be run at quarter rate |
| 13 |
# : (e.g. ~10hz). |
| 14 |
# : |
| 15 |
# : The developer should interleave slower rate modules to spread out |
| 16 |
# : workload A frame is defined by the timer rate; which is usually the |
| 17 |
# : maximum rate as determined by the FPS. |
| 18 |
# : |
| 19 |
# : This is an alternative to the timer based or explicit function calling |
| 20 |
# : way of invoking aircraft systems. It has the advantage of using less |
| 21 |
# : timers and remaining modular, as each aircraft subsytem can simply |
| 22 |
# : register itself with the global transmitter to receive the frame |
| 23 |
# : notification. |
| 24 |
# |
| 25 |
# See Also : https://wiki.flightgear.org/Nasal_Optimisation#Emesary_real_time_executive |
| 26 |
# : F-15 and F-14 for examples of how to use this. |
| 27 |
# |
| 28 |
# Author : Richard Harrison ([email protected]) |
| 29 |
# |
| 30 |
# Creation Date : 4 June 2018 |
| 31 |
# |
| 32 |
# Copyright (C) 2018 Richard Harrison Released under GPL V2 |
| 33 |
# |
| 34 |
#---------------------------------------------------------------------------*/ |
| 35 |
|
| 36 |
# |
| 37 |
# |
| 38 |
# This is the notification that is sent out to all recipients each frame. |
| 39 |
# The notification contains a hash of property values. |
| 40 |
# Frame modules can request that the hash includes key/property pairs |
| 41 |
# by using the FrameNotificationAddProperty |
| 42 |
# |
| 43 |
# An instance of this class is be contained within the EmesaryExecutive. |
| 44 |
var FrameNotification = |
| 45 |
{ |
| 46 |
debug: 0, |
| 47 |
# The rate and the transmitter to use |
| 48 |
new: func(_rate, transmitter=nil) |
| 49 |
{ |
| 50 |
if (transmitter == nil) |
| 51 |
transmitter = emesary.GlobalTransmitter; |
| 52 |
|
| 53 |
var new_class = emesary.Notification.new("FrameNotification", _rate, 0); |
| 54 |
append(new_class.parents, FrameNotification); |
| 55 |
new_class.Rate = _rate; |
| 56 |
new_class.FrameCount = 0; |
| 57 |
new_class.ElapsedSeconds = 0; |
| 58 |
new_class.monitored = {}; |
| 59 |
new_class.properties = {}; |
| 60 |
new_class.transmitter = transmitter; |
| 61 |
|
| 62 |
# |
| 63 |
# embed a recipient within this notification to allow the monitored property |
| 64 |
# mapping list to be modified. |
| 65 |
new_class.Recipient = emesary.Recipient.new("FrameNotification"); |
| 66 |
new_class.Recipient.Receive = func(notification) |
| 67 |
{ |
| 68 |
if (notification.NotificationType == "FrameNotificationAddProperty") |
| 69 |
{ |
| 70 |
var root_node = props.globals; |
| 71 |
if (notification.root_node != nil) { |
| 72 |
root_node = notification.root_node; |
| 73 |
} |
| 74 |
if (new_class.properties[notification.property] != nil |
| 75 |
and new_class.properties[notification.property] != notification.variable) |
| 76 |
logprint(1,"FrameNotification: (",notification.module,") FrameNotification: already have variable ",new_class.properties[notification.property]," for ",notification.variable, " referencing property ",notification.property); |
| 77 |
|
| 78 |
if (new_class.monitored[notification.variable] != nil |
| 79 |
and new_class.monitored[notification.variable].getPath() != notification.property |
| 80 |
and new_class.monitored[notification.variable].getPath() != "/"~notification.property) |
| 81 |
logprint(1,"FrameNotification: (",notification.module,") FrameNotification: already have variable ",notification.variable,"=",new_class.monitored[notification.variable].getPath(), " using different property ",notification.property); |
| 82 |
# else if (new_class.monitored[notification.variable] == nil) |
| 83 |
# print("[INFO]: (",notification.module,") FrameNotification.",notification.variable, " = ",notification.property); |
| 84 |
|
| 85 |
new_class.monitored[notification.variable] = root_node.getNode(notification.property,1); |
| 86 |
new_class.properties[notification.property] = notification.variable; |
| 87 |
|
| 88 |
logprint(4,"(",notification.module,") FrameNotification.",notification.variable, " = ",notification.property, " -> ", new_class.monitored[notification.variable].getPath() ); |
| 89 |
return emesary.Transmitter.ReceiptStatus_OK; |
| 90 |
} |
| 91 |
return emesary.Transmitter.ReceiptStatus_NotProcessed; |
| 92 |
}; |
| 93 |
new_class.transmitter.Register(new_class.Recipient); |
| 94 |
return new_class; |
| 95 |
}, |
| 96 |
fetchvars : func() { |
| 97 |
foreach (var mp; keys(me.monitored)){ |
| 98 |
if(me.monitored[mp] != nil){ |
| 99 |
if (FrameNotification.debug > 1) |
| 100 |
logprint(5," ",mp, " = ",me.monitored[mp].getValue()); |
| 101 |
me[mp] = me.monitored[mp].getValue(); |
| 102 |
} |
| 103 |
} |
| 104 |
}, |
| 105 |
}; |
| 106 |
|
| 107 |
# |
| 108 |
# request to add a property to the frame notification |
| 109 |
var FrameNotificationAddProperty = |
| 110 |
{ |
| 111 |
new: func(module, variable, property, root_node=nil) |
| 112 |
{ |
| 113 |
var new_class = emesary.Notification.new("FrameNotificationAddProperty", variable, 0); |
| 114 |
if (root_node == nil) |
| 115 |
root_node = props.globals; |
| 116 |
new_class.module = module ; |
| 117 |
new_class.variable = variable; |
| 118 |
new_class.property = property; |
| 119 |
new_class.root_node = root_node; |
| 120 |
return new_class; |
| 121 |
}, |
| 122 |
}; |
| 123 |
|
| 124 |
# |
| 125 |
# The way that the core measures frame rate gives invalid values after a pause so instead |
| 126 |
# we will measure specific to our needs. |
| 127 |
var PerformanceMeasurement = { |
| 128 |
new : func { |
| 129 |
return { |
| 130 |
parents: [PerformanceMeasurement], |
| 131 |
lp : aircraft.lowpass.new(0.1), |
| 132 |
period : props.globals.getNode("/sim/emexec/monitor-period",1), |
| 133 |
frame_count_node : props.globals.getNode("/sim/frame-number"), |
| 134 |
frame_count : getprop("/sim/frame-number") or 0, |
| 135 |
emexec_frame_rate_node : props.globals.getNode("/sim/emexec/frame-rate",1), |
| 136 |
start_time : systime(), |
| 137 |
frame_rate : 30, |
| 138 |
}; |
| 139 |
}, |
| 140 |
update: func { |
| 141 |
# calculate the average (mean) rate for the monitored period and then let this |
| 142 |
# filter into our output frame rate |
| 143 |
me.delta_seconds = systime() - me.start_time; |
| 144 |
me.frame_count_delta = me.frame_count_node.getValue() - me.frame_count; |
| 145 |
me.frame_rate = me.frame_count_delta / me.delta_seconds; |
| 146 |
me.elapsed_seconds= systime(); |
| 147 |
me.emexec_frame_rate_node.setDoubleValue((int)(me.lp.filter(me.frame_rate))); |
| 148 |
|
| 149 |
# reset at the end of the defined period as over a long period the average isn't as relevant for our purposes |
| 150 |
if (me.delta_seconds > me.period.getValue()){ |
| 151 |
me.start_time = systime(); |
| 152 |
me.frame_count = me.frame_count_node.getValue(); |
| 153 |
} |
| 154 |
}, |
| 155 |
start : func { |
| 156 |
me.timer = maketimer(2, me, func { me.update() }); |
| 157 |
me.timer.simulatedTime = 0; |
| 158 |
me.timer.start(); |
| 159 |
return me; |
| 160 |
} |
| 161 |
}; |
| 162 |
|
| 163 |
performanceMeasurement = PerformanceMeasurement.new(); |
| 164 |
|
| 165 |
# |
| 166 |
# the main exeuctive class. |
| 167 |
# There will be one of these as emexec.ExceModule however multiple instances could be |
| 168 |
# created - but only by those who understand scheduling - because it is not necessary |
| 169 |
# to have more than one - unless we mange to enable some sort of per core threading. |
| 170 |
var EmesaryExecutive = { |
| 171 |
new : func(_ident="EMEXEC", transmitter=nil) { |
| 172 |
|
| 173 |
# by default use global transmitter |
| 174 |
if (transmitter == nil) |
| 175 |
transmitter = emesary.GlobalTransmitter; |
| 176 |
|
| 177 |
var new_class = { |
| 178 |
parents: [EmesaryExecutive], |
| 179 |
Ident: _ident, |
| 180 |
frameNotification : FrameNotification.new(1, transmitter), |
| 181 |
emexecRate : props.globals.getNode("/sim/emexec/rate-hz",1), |
| 182 |
emexecMaxRate : props.globals.getNode("/sim/emexec/max-rate-hz",1), |
| 183 |
frame_inc : 0, |
| 184 |
cur_frame_inc : 0.033, # start off at 33hz |
| 185 |
}; |
| 186 |
new_class.transmitter = transmitter; |
| 187 |
new_class.set_rate(30); |
| 188 |
|
| 189 |
# setup the properties to monitor for this system |
| 190 |
var exec_prop_list = { |
| 191 |
frame_rate : "/sim/frame-rate", |
| 192 |
frame_rate_worst : "/sim/frame-rate-worst", |
| 193 |
elapsed_seconds : "/sim/time/elapsed-sec", |
| 194 |
eframe_rate : "/sim/emexec/frame-rate", |
| 195 |
}; |
| 196 |
new_class.monitor_properties(exec_prop_list); |
| 197 |
|
| 198 |
# now setup the timer. |
| 199 |
# - initially use update rate of 30hz. |
| 200 |
# - use simulated time as otherwise will continue to be called when paused |
| 201 |
# - start timer now, as the listener will effectively block module calls until sim init |
| 202 |
new_class.frameNotification.running = 0; |
| 203 |
setlistener("sim/signals/fdm-initialized", func(v) { |
| 204 |
logprint(1,"started ",new_class.Ident); |
| 205 |
new_class.frameNotification.dT = 0; # seconds |
| 206 |
new_class.frameNotification.curT = 0; |
| 207 |
new_class.frameNotification.running = 1; |
| 208 |
}); |
| 209 |
new_class.execTimer = maketimer(new_class.cur_frame_inc, new_class, new_class.timerCallback); |
| 210 |
new_class.execTimer.simulatedTime = 1; |
| 211 |
|
| 212 |
return new_class; |
| 213 |
}, |
| 214 |
set_rate : func(ratehz){ |
| 215 |
me.emexecRate.setValue(ratehz); |
| 216 |
me.cur_frame_inc = 1.0/ratehz; |
| 217 |
me.frame_inc = me.cur_frame_inc; |
| 218 |
}, |
| 219 |
start : func { |
| 220 |
me.execTimer.start(); |
| 221 |
performanceMeasurement.start(); |
| 222 |
}, |
| 223 |
stop : func { |
| 224 |
me.execTimer.stop(); |
| 225 |
performanceMeasurement.start(); |
| 226 |
}, |
| 227 |
# request monitoring of a list of hash value pairs. |
| 228 |
monitor_properties : func(input){ |
| 229 |
# this uses a notification to isolate the implementation which is also in this module; so it could |
| 230 |
# call directly; however the design is that a FrameNotification add property could also trigger other |
| 231 |
# logic that we do not know about. |
| 232 |
foreach (var name; keys(input)) { |
| 233 |
me.transmitter.NotifyAll(FrameNotificationAddProperty.new(me.Ident, name, input[name])); |
| 234 |
} |
| 235 |
}, |
| 236 |
|
| 237 |
# ident: String (e.g F-15 HUD) |
| 238 |
# inputs: hash of properties to monitor |
| 239 |
# : e.g |
| 240 |
# { |
| 241 |
# AirspeedIndicatorIndicatedMach : "instrumentation/airspeed-indicator/indicated-mach", |
| 242 |
# Alpha : "orientation/alpha-indicated-deg", |
| 243 |
# } |
| 244 |
# : object - must have an update(notification) method that will receive a frame notification |
| 245 |
# : rate is the frame skip update rate (1/update rate). 0 or 1 means full rate |
| 246 |
# : offset is the offset to permit interleave |
| 247 |
# - e.g for two objects to interleave we could have a rate of two and an offset of 0 and 1 which |
| 248 |
# would result in one object being processed per frame |
| 249 |
register: func(ident, properties_to_monitor, object, rate=1, frame_offset=0) { |
| 250 |
var new_class = emesary.Recipient.new(ident); |
| 251 |
|
| 252 |
me.monitor_properties(properties_to_monitor); |
| 253 |
new_class.object = object; |
| 254 |
new_class.Receive = func(notification) |
| 255 |
{ |
| 256 |
if (notification.NotificationType == "FrameNotification"){ |
| 257 |
if (rate <= 1 or 0 == math.mod(notification.FrameCount + frame_offset,rate)){ |
| 258 |
new_class.object.update(notification); |
| 259 |
} |
| 260 |
return emesary.Transmitter.ReceiptStatus_OK; |
| 261 |
} |
| 262 |
return emesary.Transmitter.ReceiptStatus_NotProcessed; |
| 263 |
}; |
| 264 |
me.transmitter.Register(new_class); |
| 265 |
return new_class; |
| 266 |
}, |
| 267 |
timerCallback : func { |
| 268 |
|
| 269 |
if (!me.frameNotification.running){ |
| 270 |
logprint(3, me.Ident~": Waiting for start"); |
| 271 |
return; |
| 272 |
} |
| 273 |
me.frameNotification.fetchvars(); |
| 274 |
me.frameNotification.dT = me.frameNotification.elapsed_seconds - me.frameNotification.curT; |
| 275 |
|
| 276 |
if (me.frameNotification.dT > 1.0) |
| 277 |
me.frameNotification.curT = me.frameNotification.elapsed_seconds; |
| 278 |
|
| 279 |
me.transmitter.NotifyAll(me.frameNotification); |
| 280 |
me.frameNotification.FrameCount = me.frameNotification.FrameCount + 1; |
| 281 |
|
| 282 |
# this permits us to go up to 1/32 rate (which could be less than 1hz) |
| 283 |
if (me.frameNotification.FrameCount > 32) { |
| 284 |
me.frameNotification.FrameCount = 0; |
| 285 |
|
| 286 |
# adjust exec rate based on frame rate. |
| 287 |
# calculate exec update rate based on frame rate; this a quadratic function from a curve fit. |
| 288 |
me.frame_inc = (math.round((0.33227017+0.10041432*me.frameNotification.eframe_rate+0.01681707*me.frameNotification.eframe_rate*me.frameNotification.eframe_rate)/5)*5+5); |
| 289 |
|
| 290 |
# limit to: 1 <= update rate <= maxRate (default 50) |
| 291 |
me.frame_inc = math.max(1,math.min(me.emexecMaxRate.getValue(), me.frame_inc)); |
| 292 |
me.frame_inc = 1/me.frame_inc; |
| 293 |
|
| 294 |
# Adjust timer if new value |
| 295 |
if (me.frame_inc != me.cur_frame_inc) { |
| 296 |
me.set_rate(1.0/me.frame_inc); |
| 297 |
me.execTimer.restart(me.cur_frame_inc); |
| 298 |
} |
| 299 |
} |
| 300 |
} |
| 301 |
}; |
| 302 |
|
| 303 |
# profiling aid: embed within a class |
| 304 |
# and each frame call log("something") to trace time |
| 305 |
# e.g. to create using the default log level of INFO: |
| 306 |
# ot = OperationTimer.new("VSD"); |
| 307 |
# or for log level debug |
| 308 |
# ot = OperationTimer.new("VSD",2); |
| 309 |
# ... |
| 310 |
# ot.reset(); |
| 311 |
# ot.log("start"); |
| 312 |
# ... code ... |
| 313 |
# ot.log("half way"); |
| 314 |
# ... code ... |
| 315 |
# ot.log("finished"); |
| 316 |
|
| 317 |
OperationTimer = { |
| 318 |
new : func (ident="timer", level=3) { |
| 319 |
{ |
| 320 |
parents: [OperationTimer], |
| 321 |
timestamp: maketimestamp(), |
| 322 |
ident: ident, |
| 323 |
resolution_uS: 1000.0, |
| 324 |
level : level, |
| 325 |
} |
| 326 |
}, |
| 327 |
log : func( text){ |
| 328 |
logprint(me.level, sprintf("%10s: %8.3f : %s",me.ident, me.timestamp.elapsedUSec()/me.resolution_uS, text)); |
| 329 |
}, |
| 330 |
reset : func { |
| 331 |
me.timestamp.stamp(); |
| 332 |
} |
| 333 |
}; |
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
var xmit = emesary.Transmitter.new("exec"); |
| 339 |
var ExecModule = EmesaryExecutive.new("EMEXEC", xmit); |
| 340 |
ExecModule.start(); |