Changed speed match function to use target distance instead of airspeed ... not perfected yet ,but better ...
This commit is contained in:
parent
514f2c1ce7
commit
a7f90a8cb8
1 changed files with 183 additions and 170 deletions
|
@ -8,7 +8,11 @@ var R2D = 180 / math.pi;
|
||||||
var sin = func(v) math.sin(v * D2R);
|
var sin = func(v) math.sin(v * D2R);
|
||||||
var cos = func(v) math.cos(v * D2R);
|
var cos = func(v) math.cos(v * D2R);
|
||||||
var atan2 = func(v, w) math.atan2(v, w) * R2D;
|
var atan2 = func(v, w) math.atan2(v, w) * R2D;
|
||||||
|
var time=0;
|
||||||
|
var lasttime=0;
|
||||||
|
var seconds =0;
|
||||||
|
var dist=0;
|
||||||
|
var lastdist=0;
|
||||||
var panel_dialog = gui.Dialog.new("/sim/gui/dialogs/cam/panel/dialog",
|
var panel_dialog = gui.Dialog.new("/sim/gui/dialogs/cam/panel/dialog",
|
||||||
"Aircraft/ufo/Dialogs/cam.xml");
|
"Aircraft/ufo/Dialogs/cam.xml");
|
||||||
var callsign_dialog = gui.Dialog.new("/sim/gui/dialogs/cam/select/dialog",
|
var callsign_dialog = gui.Dialog.new("/sim/gui/dialogs/cam/select/dialog",
|
||||||
|
@ -34,35 +38,35 @@ var goal_rollN = props.globals.getNode("sim/current-view/goal-roll-offset-deg");
|
||||||
|
|
||||||
|
|
||||||
var mode = {
|
var mode = {
|
||||||
chase : props.globals.getNode("/sim/cam/chase"),
|
chase : props.globals.getNode("/sim/cam/chase"),
|
||||||
focus : props.globals.getNode("/sim/cam/focus"),
|
focus : props.globals.getNode("/sim/cam/focus"),
|
||||||
speed : props.globals.getNode("/sim/cam/speed"),
|
speed : props.globals.getNode("/sim/cam/speed"),
|
||||||
alt : props.globals.getNode("/sim/cam/alt"),
|
alt : props.globals.getNode("/sim/cam/alt"),
|
||||||
lock : props.globals.getNode("/sim/cam/lock"),
|
lock : props.globals.getNode("/sim/cam/lock"),
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
var lowpass = {
|
var lowpass = {
|
||||||
viewheading : aircraft.angular_lowpass.new(0.2),
|
viewheading : aircraft.angular_lowpass.new(0.2),
|
||||||
viewpitch : aircraft.lowpass.new(0.1),
|
viewpitch : aircraft.lowpass.new(0.1),
|
||||||
viewroll : aircraft.lowpass.new(0.1),
|
viewroll : aircraft.lowpass.new(0.1),
|
||||||
|
|
||||||
hdg : aircraft.angular_lowpass.new(0.6),
|
hdg : aircraft.angular_lowpass.new(0.6),
|
||||||
alt : aircraft.lowpass.new(3),
|
alt : aircraft.lowpass.new(3),
|
||||||
speed : aircraft.lowpass.new(2),
|
speed : aircraft.lowpass.new(2),
|
||||||
throttle : aircraft.lowpass.new(0.3),
|
throttle : aircraft.lowpass.new(0.3),
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
controls.flapsDown = func(x) {
|
controls.flapsDown = func(x) {
|
||||||
if (!x)
|
if (!x)
|
||||||
return;
|
return;
|
||||||
elsif (x < 0 and current > 0)
|
elsif (x < 0 and current > 0)
|
||||||
current -= 1;
|
current -= 1;
|
||||||
elsif (x > 0 and current < size(speed) - 1)
|
elsif (x > 0 and current < size(speed) - 1)
|
||||||
current += 1;
|
current += 1;
|
||||||
|
|
||||||
maxspeed = speed[current];
|
maxspeed = speed[current];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -78,198 +82,206 @@ controls.flapsDown = func(x) {
|
||||||
|
|
||||||
# sort function
|
# sort function
|
||||||
var by_callsign = func(a, b) {
|
var by_callsign = func(a, b) {
|
||||||
cmp(a.getNode("callsign").getValue(), b.getNode("callsign").getValue());
|
cmp(a.getNode("callsign").getValue(), b.getNode("callsign").getValue());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
var update_aircraft_list = func {
|
var update_aircraft_list = func {
|
||||||
var ac = [];
|
var ac = [];
|
||||||
var n = props.globals.getNode("/ai/models");
|
var n = props.globals.getNode("/ai/models");
|
||||||
|
|
||||||
if (getprop("/sim/cam/target-ai"))
|
if (getprop("/sim/cam/target-ai"))
|
||||||
ac ~= n.getChildren("aircraft") ~ n.getChildren("tanker");
|
ac ~= n.getChildren("aircraft") ~ n.getChildren("tanker");
|
||||||
if (getprop("/sim/cam/target-mp"))
|
if (getprop("/sim/cam/target-mp"))
|
||||||
ac ~= n.getChildren("multiplayer");
|
ac ~= n.getChildren("multiplayer");
|
||||||
|
|
||||||
aircraft_list = [];
|
aircraft_list = [];
|
||||||
|
|
||||||
foreach (var model; ac) {
|
foreach (var model; ac) {
|
||||||
var model_node = model.getNode("valid");
|
var model_node = model.getNode("valid");
|
||||||
if (model_node != nil and model_node.getBoolValue()) {
|
if (model_node != nil and model_node.getBoolValue()) {
|
||||||
append(aircraft_list, model);
|
append(aircraft_list, model);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
aircraft_list = sort(aircraft_list, by_callsign);
|
aircraft_list = sort(aircraft_list, by_callsign);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
var update = func {
|
var update = func {
|
||||||
# data acquisition
|
# data acquisition
|
||||||
target.set_latlon(
|
|
||||||
targetN.getNode("position/latitude-deg").getValue(),
|
target.set_latlon(
|
||||||
targetN.getNode("position/longitude-deg").getValue(),
|
targetN.getNode("position/latitude-deg").getValue(),
|
||||||
targetN.getNode("position/altitude-ft").getValue() * geo.FT2M);
|
targetN.getNode("position/longitude-deg").getValue(),
|
||||||
|
targetN.getNode("position/altitude-ft").getValue() * geo.FT2M);
|
||||||
|
|
||||||
self = geo.aircraft_position();
|
self = geo.aircraft_position();
|
||||||
|
|
||||||
if (0) {
|
if (0) {
|
||||||
if (mode.lock.getValue()) {
|
if (mode.lock.getValue()) {
|
||||||
self.set(target);
|
self.set(target);
|
||||||
self.apply_course_distance(rel.course, rel.distance);
|
self.apply_course_distance(rel.course, rel.distance);
|
||||||
self.set_alt(target.alt() + rel.alt);
|
self.set_alt(target.alt() + rel.alt);
|
||||||
setprop("/position/latitude-deg", self.lat());
|
setprop("/position/latitude-deg", self.lat());
|
||||||
setprop("/position/longitude-deg", self.lon());
|
setprop("/position/longitude-deg", self.lon());
|
||||||
setprop("/position/altitude-ft", self.alt() * geo.M2FT);
|
setprop("/position/altitude-ft", self.alt() * geo.M2FT);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var self_heading = getprop("/orientation/heading-deg");
|
var self_heading = getprop("/orientation/heading-deg");
|
||||||
var self_pitch = getprop("/orientation/pitch-deg");
|
var self_pitch = getprop("/orientation/pitch-deg");
|
||||||
var self_roll = getprop("/orientation/roll-deg");
|
var self_roll = getprop("/orientation/roll-deg");
|
||||||
|
|
||||||
# check whether to unlock altitude/heading
|
# check whether to unlock altitude/heading
|
||||||
if (abs(self_pitch) > 2)
|
if (abs(self_pitch) > 2)
|
||||||
mode.alt.setValue(0);
|
mode.alt.setValue(0);
|
||||||
if (abs(self_roll) > 2)
|
if (abs(self_roll) > 2)
|
||||||
mode.chase.setValue(0);
|
mode.chase.setValue(0);
|
||||||
|
|
||||||
# calculate own altitude
|
# calculate own altitude
|
||||||
var min_alt_asl = target.alt() - getprop("/position/altitude-agl-ft") * geo.FT2M + min_alt_agl;
|
var min_alt_asl = target.alt() - getprop("/position/altitude-agl-ft") * geo.FT2M + min_alt_agl;
|
||||||
if (self.alt() <= min_alt_asl)
|
if (self.alt() <= min_alt_asl)
|
||||||
setprop("/position/altitude-ft", lowpass.alt.filter(min_alt_asl) * geo.M2FT);
|
setprop("/position/altitude-ft", lowpass.alt.filter(min_alt_asl) * geo.M2FT);
|
||||||
elsif (mode.alt.getValue())
|
elsif (mode.alt.getValue())
|
||||||
setprop("/position/altitude-ft", lowpass.alt.filter(target.alt()) * geo.M2FT);
|
setprop("/position/altitude-ft", lowpass.alt.filter(target.alt()) * geo.M2FT);
|
||||||
else
|
else
|
||||||
lowpass.alt.filter(self.alt());
|
lowpass.alt.filter(self.alt());
|
||||||
|
|
||||||
# calculate position relative to target
|
# calculate position relative to target
|
||||||
var distance = self.direct_distance_to(target);
|
var distance = self.direct_distance_to(target);
|
||||||
if (distance < 1) {
|
if (distance < 1) {
|
||||||
var course = targetN.getNode("orientation/true-heading-deg").getValue();
|
var course = targetN.getNode("orientation/true-heading-deg").getValue();
|
||||||
mode.focus.setValue(0);
|
mode.focus.setValue(0);
|
||||||
mode.chase.setValue(0);
|
mode.chase.setValue(0);
|
||||||
} else {
|
} else {
|
||||||
var course = self.course_to(target);
|
var course = self.course_to(target);
|
||||||
}
|
}
|
||||||
var elevation = atan2(target.alt() - lowpass.alt.get(), distance);
|
var elevation = atan2(target.alt() - lowpass.alt.get(), distance);
|
||||||
|
|
||||||
# set own heading
|
# set own heading
|
||||||
if (mode.chase.getValue())
|
if (mode.chase.getValue())
|
||||||
setprop("/orientation/heading-deg", self_heading = lowpass.hdg.filter(course));
|
setprop("/orientation/heading-deg", self_heading = lowpass.hdg.filter(course));
|
||||||
|
|
||||||
# calculate focus view direction
|
# calculate focus view direction
|
||||||
if (mode.focus.getValue()) {
|
if (mode.focus.getValue()) {
|
||||||
var h = lowpass.viewheading.filter(self_heading - course);
|
var h = lowpass.viewheading.filter(self_heading - course);
|
||||||
var p = elevation - self_pitch * cos(h);
|
var p = elevation - self_pitch * cos(h);
|
||||||
var r = -self_roll * sin(h);
|
var r = -self_roll * sin(h);
|
||||||
goal_headingN.setValue(h);
|
goal_headingN.setValue(h);
|
||||||
goal_pitchN.setValue(lowpass.viewpitch.filter(p));
|
goal_pitchN.setValue(lowpass.viewpitch.filter(p));
|
||||||
goal_rollN.setValue(lowpass.viewroll.filter(r));
|
goal_rollN.setValue(lowpass.viewroll.filter(r));
|
||||||
}
|
}
|
||||||
|
|
||||||
# calculate own speed
|
# calculate own speed
|
||||||
if (mode.speed.getValue())
|
if (mode.speed.getValue()){
|
||||||
maxspeed = targetN.getNode("velocities/true-airspeed-kt").getValue() * MPS * 2;
|
dist=targetN.getNode("radar/range-nm").getValue();
|
||||||
else
|
var nmps =dist-lastdist*seconds;
|
||||||
maxspeed = speed[current];
|
nmps =nmps*1852;
|
||||||
maxspeedN.setDoubleValue(lowpass.speed.filter(maxspeed));
|
maxspeed = nmps * 2;
|
||||||
|
lastdist=dist;
|
||||||
|
}else{
|
||||||
|
maxspeed = speed[current];
|
||||||
|
}
|
||||||
|
maxspeedN.setDoubleValue(lowpass.speed.filter(maxspeed));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
var loop = func {
|
var loop = func {
|
||||||
if (view_number == cam_view and targetN != nil)
|
time=getprop("/sim/time/elapsed-sec");
|
||||||
update();
|
seconds=time-lasttime;
|
||||||
|
if (view_number == cam_view and targetN != nil)
|
||||||
settimer(loop, 0);
|
update();
|
||||||
|
lasttime=time;
|
||||||
|
settimer(loop, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
var select_aircraft = func(index) {
|
var select_aircraft = func(index) {
|
||||||
update_aircraft_list();
|
update_aircraft_list();
|
||||||
|
|
||||||
var number = size(aircraft_list);
|
var number = size(aircraft_list);
|
||||||
var name = "";
|
var name = "";
|
||||||
targetN = nil;
|
targetN = nil;
|
||||||
|
|
||||||
if (number) {
|
if (number) {
|
||||||
if (index < 0)
|
if (index < 0)
|
||||||
index = number - 1;
|
index = number - 1;
|
||||||
elsif (index >= number)
|
elsif (index >= number)
|
||||||
index = 0;
|
index = 0;
|
||||||
|
|
||||||
targetN = aircraft_list[index];
|
targetN = aircraft_list[index];
|
||||||
name = targetN.getNode("callsign").getValue();
|
name = targetN.getNode("callsign").getValue();
|
||||||
}
|
}
|
||||||
setprop("/sim/cam/target-number", index);
|
setprop("/sim/cam/target-number", index);
|
||||||
setprop("/sim/cam/target-name", name);
|
setprop("/sim/cam/target-name", name);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
var target_name_changed = func() {
|
var target_name_changed = func() {
|
||||||
|
|
||||||
var target_name = getprop("/sim/cam/target-name");
|
var target_name = getprop("/sim/cam/target-name");
|
||||||
|
|
||||||
if (target_name == nil or cmp(target_name, "") == 0)
|
if (target_name == nil or cmp(target_name, "") == 0)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
forindex (var i; aircraft_list) {
|
forindex (var i; aircraft_list) {
|
||||||
if (cmp(target_name,
|
if (cmp(target_name,
|
||||||
aircraft_list[i].getNode("callsign").getValue()) == 0) {
|
aircraft_list[i].getNode("callsign").getValue()) == 0) {
|
||||||
if (i != getprop("/sim/cam/target-number")) {
|
if (i != getprop("/sim/cam/target-number")) {
|
||||||
# found the index for the new target-name
|
# found the index for the new target-name
|
||||||
select_aircraft(i);
|
select_aircraft(i);
|
||||||
}
|
}
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
# no matches, select the current index again
|
# no matches, select the current index again
|
||||||
update_aircraft();
|
update_aircraft();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
# called from the dialog
|
# called from the dialog
|
||||||
var goto_target = func {
|
var goto_target = func {
|
||||||
targetN != nil or return;
|
targetN != nil or return;
|
||||||
self != nil or return;
|
self != nil or return;
|
||||||
var lat = targetN.getNode("position/latitude-deg").getValue();
|
var lat = targetN.getNode("position/latitude-deg").getValue();
|
||||||
var lon = targetN.getNode("position/longitude-deg").getValue();
|
var lon = targetN.getNode("position/longitude-deg").getValue();
|
||||||
var alt = targetN.getNode("position/altitude-ft").getValue() * geo.FT2M;
|
var alt = targetN.getNode("position/altitude-ft").getValue() * geo.FT2M;
|
||||||
var speed = targetN.getNode("velocities/true-airspeed-kt").getValue() * MPS * 2;
|
var speed = targetN.getNode("velocities/true-airspeed-kt").getValue() * MPS * 2;
|
||||||
var course = targetN.getNode("orientation/true-heading-deg").getValue();
|
var course = targetN.getNode("orientation/true-heading-deg").getValue();
|
||||||
self.set_latlon(lat, lon, alt).apply_course_distance(course + 180, 100);
|
self.set_latlon(lat, lon, alt).apply_course_distance(course + 180, 100);
|
||||||
setprop("/position/latitude-deg", self.lat());
|
setprop("/position/latitude-deg", self.lat());
|
||||||
setprop("/position/longitude-deg", self.lon());
|
setprop("/position/longitude-deg", self.lon());
|
||||||
lowpass.alt.set(self.alt());
|
lowpass.alt.set(self.alt());
|
||||||
lowpass.speed.set(speed);
|
lowpass.speed.set(speed);
|
||||||
mode.chase.setValue(1);
|
mode.chase.setValue(1);
|
||||||
mode.focus.setValue(1);
|
mode.focus.setValue(1);
|
||||||
mode.alt.setValue(1);
|
mode.alt.setValue(1);
|
||||||
mode.speed.setValue(1);
|
mode.speed.setValue(1);
|
||||||
maxspeed = speed * 2;
|
maxspeed = speed * 2;
|
||||||
# props.setAll("/controls/engines/engine", "throttle", lowpass.throttle.set(0.5));
|
# props.setAll("/controls/engines/engine", "throttle", lowpass.throttle.set(0.5));
|
||||||
}
|
}
|
||||||
|
|
||||||
var update_goto = func {
|
var update_goto = func {
|
||||||
if (!getprop("/sim/cam/goto"))
|
if (!getprop("/sim/cam/goto"))
|
||||||
return;
|
return;
|
||||||
|
|
||||||
goto_target();
|
goto_target();
|
||||||
setprop("/sim/cam/goto", 0);
|
setprop("/sim/cam/goto", 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
var update_aircraft = func {
|
var update_aircraft = func {
|
||||||
select_aircraft(getprop("/sim/cam/target-number"));
|
select_aircraft(getprop("/sim/cam/target-number"));
|
||||||
}
|
}
|
||||||
|
|
||||||
var ai_removed = func {
|
var ai_removed = func {
|
||||||
var node_str = getprop("/ai/models/model-removed") ~ "/callsign";
|
var node_str = getprop("/ai/models/model-removed") ~ "/callsign";
|
||||||
var target_name = getprop(node_str);
|
var target_name = getprop(node_str);
|
||||||
if (cmp(target_name, getprop("/sim/cam/target-name")) == 0) {
|
if (cmp(target_name, getprop("/sim/cam/target-name")) == 0) {
|
||||||
update_aircraft();
|
update_aircraft();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
setlistener("/sim/cam/target-number", update_aircraft);
|
setlistener("/sim/cam/target-number", update_aircraft);
|
||||||
|
@ -280,36 +292,37 @@ setlistener("/sim/cam/goto", update_goto);
|
||||||
setlistener("/ai/models/model-removed", ai_removed);
|
setlistener("/ai/models/model-removed", ai_removed);
|
||||||
|
|
||||||
setlistener("/sim/current-view/view-number", func {
|
setlistener("/sim/current-view/view-number", func {
|
||||||
view_number = cmdarg().getValue();
|
view_number = cmdarg().getValue();
|
||||||
if (view_number == cam_view)
|
if (view_number == cam_view)
|
||||||
panel_dialog.open();
|
panel_dialog.open();
|
||||||
else
|
else
|
||||||
panel_dialog.close();
|
panel_dialog.close();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
if (0) {
|
if (0) {
|
||||||
var rel = { course: 0, distance: 0, alt: 0 };
|
var rel = { course: 0, distance: 0, alt: 0 };
|
||||||
setlistener("/sim/cam/lock", func {
|
setlistener("/sim/cam/lock", func {
|
||||||
if (cmdarg().getValue()) {
|
if (cmdarg().getValue()) {
|
||||||
rel.course = target.course_to(self);
|
rel.course = target.course_to(self);
|
||||||
rel.distance = target.distance_to(self);
|
rel.distance = target.distance_to(self);
|
||||||
rel.alt = self.alt() - target.alt();
|
rel.alt = self.alt() - target.alt();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
setlistener("/sim/signals/fdm-initialized", func {
|
setlistener("/sim/signals/fdm-initialized", func {
|
||||||
var views = props.globals.getNode("/sim").getChildren("view");
|
var views = props.globals.getNode("/sim").getChildren("view");
|
||||||
forindex (var i; views)
|
forindex (var i; views)
|
||||||
if (views[i].getNode("name").getValue() == "Cam View")
|
if (views[i].getNode("name").getValue() == "Cam View")
|
||||||
cam_view = i;
|
cam_view = i;
|
||||||
|
|
||||||
setprop("/sim/current-view/view-number", cam_view);
|
setprop("/sim/current-view/view-number", cam_view);
|
||||||
setprop("/engines/engine/speed-max-mps", 500);
|
setprop("/engines/engine/speed-max-mps", 500);
|
||||||
update_aircraft();
|
update_aircraft();
|
||||||
loop();
|
lasttime=getprop("/sim/time/elapsed-sec");
|
||||||
|
loop();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue