-
Notifications
You must be signed in to change notification settings - Fork 1
/
MapsHandler.cpp
182 lines (178 loc) · 8.24 KB
/
MapsHandler.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
//
// Created by ofir8 on 12/19/18.
//
#include "MapsHandler.h"
#include "ComunicateWithSimulator.h"
pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
map<string, double> MapsHandler::addresses;
map<string, double> MapsHandler::symbolTable;
map<string, string> MapsHandler::binds;
map<string, double> MapsHandler::notXML;
/**
* the method creates the addresses map
*/
void MapsHandler::createAddressTable() {
pthread_mutex_lock(&lock);
addresses.insert(pair<string,double>("/instrumentation/airspeed-indicator/indicated-speed-kt", 0));
addresses.insert(pair<string, double>("/instrumentation/altimeter/indicated-altitude-ft", 0));
addresses.insert(pair<string, double>("/instrumentation/altimeter/pressure-alt-ft", 0));
addresses.insert(pair<string, double>("/instrumentation/attitude-indicator/indicated-pitch-deg", 0));
addresses.insert(pair<string, double>("/instrumentation/attitude-indicator/indicated-roll-deg", 0));
addresses.insert(pair<string, double>("/instrumentation/attitude-indicator/internal-pitch-deg", 0));
addresses.insert(pair<string, double>("/instrumentation/attitude-indicator/internal-roll-deg", 0));
addresses.insert(pair<string, double>("/instrumentation/encoder/indicated-altitude-ft", 0));
addresses.insert(pair<string, double>("/instrumentation/encoder/pressure-alt-ft", 0));
addresses.insert(pair<string, double>("/instrumentation/gps/indicated-altitude-ft", 0));
addresses.insert(pair<string, double>("/instrumentation/gps/indicated-ground-speed-kt", 0));
addresses.insert(pair<string, double>("/instrumentation/gps/indicated-vertical-speed", 0));
addresses.insert(pair<string, double>("/instrumentation/heading-indicator/indicated-heading-deg", 0));
addresses.insert(pair<string, double>("/instrumentation/magnetic-compass/indicated-heading-deg", 0));
addresses.insert(pair<string, double>("/instrumentation/slip-skid-ball/indicated-slip-skid", 0));
addresses.insert(pair<string, double>("/instrumentation/turn-indicator/indicated-turn-rate", 0));
addresses.insert(pair<string, double>("/instrumentation/vertical-speed-indicator/indicated-speed-fpm", 0));
addresses.insert(pair<string, double>("/controls/flight/aileron", 0));
addresses.insert(pair<string, double>("/controls/flight/elevator", 0));
addresses.insert(pair<string, double>("/controls/flight/rudder", 0));
addresses.insert(pair<string, double>("/controls/flight/flaps", 0));
addresses.insert(pair<string, double>("/controls/engines/current-engine/throttle", 0));
addresses.insert(pair<string, double>("/engines/engine/rpm", 0));
pthread_mutex_unlock(&lock);
}
/**
* the method updates the map with the values the simulator sent us
*/
void MapsHandler::updateFromSimulator(vector<double> params) {
pthread_mutex_lock(&lock);
addresses.find("/instrumentation/airspeed-indicator/indicated-speed-kt")->second = params.at(0);
addresses.find("/instrumentation/altimeter/indicated-altitude-ft")->second = params.at(1);
addresses.find("/instrumentation/altimeter/pressure-alt-ft")->second = params.at(2);
addresses.find("/instrumentation/attitude-indicator/indicated-pitch-deg")->second = params.at(3);
addresses.find("/instrumentation/attitude-indicator/indicated-roll-deg")->second = params.at(4);
addresses.find("/instrumentation/attitude-indicator/internal-pitch-deg")->second = params.at(5);
addresses.find("/instrumentation/attitude-indicator/internal-roll-deg")->second = params.at(6);
addresses.find("/instrumentation/encoder/indicated-altitude-ft")->second = params.at(7);
addresses.find("/instrumentation/encoder/pressure-alt-ft")->second = params.at(8);
addresses.find("/instrumentation/gps/indicated-altitude-ft")->second = params.at(9);
addresses.find("/instrumentation/gps/indicated-ground-speed-kt")->second = params.at(10);
addresses.find("/instrumentation/gps/indicated-vertical-speed")->second = params.at(11);
addresses.find("/instrumentation/heading-indicator/indicated-heading-deg")->second = params.at(12);
addresses.find("/instrumentation/magnetic-compass/indicated-heading-deg")->second = params.at(13);
addresses.find("/instrumentation/slip-skid-ball/indicated-slip-skid")->second = params.at(14);
addresses.find("/instrumentation/turn-indicator/indicated-turn-rate")->second = params.at(15);
addresses.find("/instrumentation/vertical-speed-indicator/indicated-speed-fpm")->second = params.at(16);
addresses.find("/controls/flight/aileron")->second = params.at(17);
addresses.find("/controls/flight/elevator")->second = params.at(18);
addresses.find("/controls/flight/rudder")->second = params.at(19);
addresses.find("/controls/flight/flaps")->second = params.at(20);
addresses.find("/controls/engines/current-engine/throttle")->second = params.at(21);
addresses.find("/engines/engine/rpm")->second = params.at(22);
pthread_mutex_unlock(&lock);
}
/**
* the method adds/updates values to one of the addresses tables
*/
void MapsHandler::addToAddresses(string address,double val) {
pthread_mutex_lock(&lock);
if (addresses.find(address) == addresses.end()) {
if(notXML.find(address) == notXML.end()) {
// notXML.insert(pair<string, double>(address, ComunicateWithSimulator::getFromServer(address)));
notXML.insert(pair<string, double>(address, 0));
}
else {
notXML.find(address)->second = val;
}
} else {
addresses.find(address)->second = val;
}
pthread_mutex_unlock(&lock);
}
/**
* the method checks if the address is in one of the addresses tables
*/
bool MapsHandler::isAddressExist(string address) {
pthread_mutex_lock(&lock);
pthread_mutex_unlock(&lock);
return (addresses.find(address) != addresses.end() ||
notXML.find(address) != notXML.end());
}
/**
* the method returns the value of the address
*/
double MapsHandler::getValOfAddress (string address) {
pthread_mutex_lock(&lock);
if (addresses.find(address) == addresses.end()) {
if (notXML.find(address) == notXML.end()) {
cout << "wrong input" << endl;
pthread_mutex_unlock(&lock);
return NULL;
}
//notXML.find(address)->second = ComunicateWithSimulator::getFromServer(address);
pthread_mutex_unlock(&lock);
return notXML.find(address)->second;
}
pthread_mutex_unlock(&lock);
return addresses.find(address)->second;
}
/**
* the method adds a new var to the symbol table or updates an existing var
*/
void MapsHandler::addVar(string varName,double val) {
if (symbolTable.find(varName) == symbolTable.end()) {
symbolTable.insert(pair<string, double> (varName, val));
} else {
symbolTable.find(varName)->second = val;
}
}
/**
* the method check if a var is in the symbol table
*/
bool MapsHandler::isVarExist(string varName) {
return (symbolTable.find(varName) != symbolTable.end());
}
/**
* the method returns the value of the var
*/
double MapsHandler::getVarValue (string varName) {
pthread_mutex_lock(&lock);
if (symbolTable.find(varName) == symbolTable.end()) {
cout << "wrong input" << endl;
pthread_mutex_unlock(&lock);
return NULL;
}
// if it binds to another var/address
if (binds.find(varName) != binds.end()) {
if(addresses.find(binds.find(varName)->second) != addresses.end() ||
notXML.find(binds.find(varName)->second) != notXML.end()) {
pthread_mutex_unlock(&lock);
return getValOfAddress(binds.find(varName)->second);
}
else {
pthread_mutex_unlock(&lock);
return MapsHandler::getVarValue(binds.find(varName)->second);
}
}
pthread_mutex_unlock(&lock);
return symbolTable.find(varName)->second;
}
/**
* create a bind
*/
void MapsHandler::addBind(string varName,string address) {
binds.insert(pair<string, string> (varName, address));
}
/**
* check if the bind exist
*/
bool MapsHandler::isBindExist(string varName) {
return (binds.find(varName) != binds.end());
}
/**
* return the address the var binds to
*/
string MapsHandler::getVarAddress (string varName) {
if (binds.find(varName) == binds.end()) {
cout << "wrong input" << endl;
return NULL;
}
return binds.find(varName)->second;
}