1
0
Fork 0

YASIM add initialization

use Math::clamp in ControlMap
This commit is contained in:
Henning Stahlke 2017-12-01 13:03:36 +01:00
parent b2000e1cca
commit c63ded1c44
4 changed files with 62 additions and 68 deletions

View file

@ -66,9 +66,8 @@ void ControlMap::addMapping(int input, Control control, void* object, int option
void ControlMap::addMapping(int input, Control control, void* object, int options) void ControlMap::addMapping(int input, Control control, void* object, int options)
{ {
// See if the output object already exists // See if the output object already exists
OutRec* out = 0; OutRec* out {nullptr};
int i; for(int i = 0; i < _outputs.size(); i++) {
for(i=0; i<_outputs.size(); i++) {
OutRec* o = (OutRec*)_outputs.get(i); OutRec* o = (OutRec*)_outputs.get(i);
if(o->object == object && o->control == control) { if(o->object == object && o->control == control) {
out = o; out = o;
@ -77,7 +76,7 @@ void ControlMap::addMapping(int input, Control control, void* object, int option
} }
// Create one if it doesn't // Create one if it doesn't
if(out == 0) { if(out == nullptr) {
out = new OutRec(); out = new OutRec();
out->control = control; out->control = control;
out->object = object; out->object = object;
@ -102,29 +101,25 @@ void ControlMap::addMapping(int input, Control control, void* object, int option
void ControlMap::reset() void ControlMap::reset()
{ {
// Set all the values to zero // Set all the values to zero
for(int i=0; i<_outputs.size(); i++) { for(int i = 0; i < _outputs.size(); i++) {
OutRec* o = (OutRec*)_outputs.get(i); OutRec* o = (OutRec*)_outputs.get(i);
for(int j=0; j<o->maps.size(); j++) for(int j = 0; j < o->maps.size(); j++) {
((MapRec*)(o->maps.get(j)))->val = 0; ((MapRec*)(o->maps.get(j)))->val = 0;
}
} }
} }
void ControlMap::setInput(int input, float val) void ControlMap::setInput(int input, float val)
{ {
Vector* maps = (Vector*)_inputs.get(input); Vector* maps = (Vector*)_inputs.get(input);
for(int i=0; i<maps->size(); i++) { for(int i = 0; i < maps->size(); i++) {
MapRec* m = (MapRec*)maps->get(i); MapRec* m = (MapRec*)maps->get(i);
float val2 = val;
float val2 = val; // Do the scaling operation. Clamp to [src0:src1], rescale to
// [0:1] within that range, then map to [dst0:dst1].
// Do the scaling operation. Clamp to [src0:src1], rescale to val2 = Math::clamp(val2, m->src0, m->src1);
// [0:1] within that range, then map to [dst0:dst1]. val2 = (val2 - m->src0) / (m->src1 - m->src0);
if(val2 < m->src0) val2 = m->src0; m->val = m->dst0 + val2 * (m->dst1 - m->dst0);
if(val2 > m->src1) val2 = m->src1;
val2 = (val2 - m->src0) / (m->src1 - m->src0);
val2 = m->dst0 + val2 * (m->dst1 - m->dst0);
m->val = val2;
} }
} }

View file

@ -117,14 +117,14 @@ public:
private: private:
struct OutRec { struct OutRec {
Control control; Control control;
void* object; void* object {nullptr};
Vector maps; Vector maps;
float oldL {0}; float oldL {0};
float oldR {0}; float oldR {0};
float time {0}; float time {0};
}; };
struct MapRec { struct MapRec {
OutRec* out; OutRec* out {nullptr};
int idx {0}; int idx {0};
int opt {0}; int opt {0};
float val {0}; float val {0};

View file

@ -35,10 +35,6 @@ namespace yasim {
FGFDM::FGFDM() FGFDM::FGFDM()
{ {
_vehicle_radius = 0.0f;
_nextEngine = 0;
// Map /controls/flight/elevator to the approach elevator control. This // Map /controls/flight/elevator to the approach elevator control. This
// should probably be settable, but there are very few aircraft // should probably be settable, but there are very few aircraft
// who trim their approaches using things other than elevator. // who trim their approaches using things other than elevator.
@ -534,7 +530,7 @@ void FGFDM::startElement(const char* name, const XMLAttributes &atts)
} else if(eq(name, "weight")) { } else if(eq(name, "weight")) {
parseWeight(a); parseWeight(a);
} else if(eq(name, "stall")) { } else if(eq(name, "stall")) {
Wing* w = (Wing*)_currObj; Wing* w = (Wing*)_currObj;
StallParams sp; StallParams sp;
sp.aoa = attrf(a, "aoa") * DEG2RAD; sp.aoa = attrf(a, "aoa") * DEG2RAD;
sp.width = attrf(a, "width", 2) * DEG2RAD; sp.width = attrf(a, "width", 2) * DEG2RAD;
@ -583,38 +579,34 @@ void FGFDM::startElement(const char* name, const XMLAttributes &atts)
_airplane.addCruiseControl(cm->propertyHandle(axis), value); _airplane.addCruiseControl(cm->propertyHandle(axis), value);
else else
_airplane.addApproachControl(cm->propertyHandle(axis), value); _airplane.addApproachControl(cm->propertyHandle(axis), value);
} else if(eq(name, "control-input")) { } else if(eq(name, "control-input")) {
ControlMap* cm = _airplane.getControlMap(); ControlMap* cm = _airplane.getControlMap();
// A mapping of input property to a control // A mapping of input property to a control
int axis = cm->propertyHandle(a->getValue("axis")); int propHandle = cm->propertyHandle(a->getValue("axis"));
ControlMap::Control control = cm->parseControl(a->getValue("control")); ControlMap::Control control = cm->parseControl(a->getValue("control"));
int opt = 0; int opt = 0;
opt |= a->hasAttribute("split") ? ControlMap::OPT_SPLIT : 0; opt |= a->hasAttribute("split") ? ControlMap::OPT_SPLIT : 0;
opt |= a->hasAttribute("invert") ? ControlMap::OPT_INVERT : 0; opt |= a->hasAttribute("invert") ? ControlMap::OPT_INVERT : 0;
opt |= a->hasAttribute("square") ? ControlMap::OPT_SQUARE : 0; opt |= a->hasAttribute("square") ? ControlMap::OPT_SQUARE : 0;
if(a->hasAttribute("src0")) { if(a->hasAttribute("src0")) {
cm->addMapping(axis, control, _currObj, opt, cm->addMapping(propHandle, control, _currObj, opt, attrf(a, "src0"), attrf(a, "src1"), attrf(a, "dst0"), attrf(a, "dst1"));
attrf(a, "src0"), attrf(a, "src1"), } else {
attrf(a, "dst0"), attrf(a, "dst1")); cm->addMapping(propHandle, control, _currObj, opt);
} else { }
cm->addMapping(axis, control, _currObj, opt); } else if(eq(name, "control-output")) {
}
} else if(eq(name, "control-output")) {
// A property output for a control on the current object // A property output for a control on the current object
ControlMap* cm = _airplane.getControlMap(); ControlMap* cm = _airplane.getControlMap();
ControlMap::Control control = cm->parseControl(a->getValue("control")); ControlMap::Control control = cm->parseControl(a->getValue("control"));
int handle = cm->getOutputHandle(_currObj, control); PropOut* p = new PropOut();
p->prop = fgGetNode(a->getValue("prop"), true);
PropOut* p = new PropOut(); p->handle = cm->getOutputHandle(_currObj, control);
p->prop = fgGetNode(a->getValue("prop"), true); p->control = control;
p->handle = handle; p->left = !(a->hasAttribute("side") &&
p->control = control; eq("right", a->getValue("side")));
p->left = !(a->hasAttribute("side") && p->min = attrf(a, "min", cm->rangeMin(control));
eq("right", a->getValue("side"))); p->max = attrf(a, "max", cm->rangeMax(control));
p->min = attrf(a, "min", cm->rangeMin(control)); _controlProps.add(p);
p->max = attrf(a, "max", cm->rangeMax(control));
_controlProps.add(p);
} else if(eq(name, "control-speed")) { } else if(eq(name, "control-speed")) {
ControlMap* cm = _airplane.getControlMap(); ControlMap* cm = _airplane.getControlMap();

View file

@ -32,10 +32,17 @@ public:
float getVehicleRadius(void) const { return _vehicle_radius; } float getVehicleRadius(void) const { return _vehicle_radius; }
private: private:
struct EngRec { char* prefix; Thruster* eng; }; struct EngRec {
struct WeightRec { char* prop; float size; int handle; }; char* prefix {nullptr};
Thruster* eng {nullptr};
};
struct WeightRec {
char* prop {nullptr};
float size {0};
int handle {0};
};
struct PropOut { struct PropOut {
SGPropertyNode* prop; SGPropertyNode* prop {nullptr};
int handle {0}; int handle {0};
ControlMap::Control control; ControlMap::Control control;
bool left {false}; bool left {false};
@ -79,12 +86,12 @@ private:
Vector _controlProps; Vector _controlProps;
// Radius of the vehicle, for intersection testing. // Radius of the vehicle, for intersection testing.
float _vehicle_radius; float _vehicle_radius {0};
// Parsing temporaries // Parsing temporaries
void* _currObj; void* _currObj {nullptr};
bool _cruiseCurr; bool _cruiseCurr {false};
int _nextEngine; int _nextEngine {0};
class FuelProps class FuelProps
{ {