welcome: please sign in

Cerca

Link Dipartimentali

Link Esterni

Allegato "sol14Set2010.cpp"

Scarica

   1 #include <JTC/jtc.h>
   2 #include <time.h>
   3 #include <iostream>
   4 
   5 using namespace std;
   6 
   7 int getTime()
   8 
   9 {
  10 	return time(0);
  11 }
  12 
  13 
  14 
  15 class Lock : public JTCMonitor
  16 
  17 {
  18 	bool occupato;
  19 public:
  20 	void lock()
  21 	{
  22 		JTCSynchronized sync(*this);
  23 		while(occupato)
  24 			wait();
  25 		occupato = true;
  26 	}
  27 	void unlock()
  28 	{
  29 		JTCSynchronized sync(*this);
  30 		occupato = false;
  31 		notifyAll();
  32 	}
  33 };
  34 Lock coutSync;
  35 
  36 
  37 enum tipoMessaggio { GPS, IMMAGINE };
  38 
  39 class Messaggio
  40 {
  41 public:
  42 	tipoMessaggio codice;
  43 	int codiceThread;
  44 	void* dato;
  45 	Messaggio(tipoMessaggio cod, int codThread, void* d)
  46 	{
  47 		codice = cod;
  48 		codiceThread = codThread;
  49 		dato = d;
  50 	}
  51 };
  52 
  53 
  54 #define LEN 40
  55 template <class T>
  56 class BufferPrivato : public JTCMonitor
  57 
  58 {
  59 protected:
  60 
  61 	T thebuffer[LEN];
  62 	int in;
  63 	int out;
  64 	int chars;
  65 
  66 public:
  67 	BufferPrivato(T initValue = 0)
  68 	{
  69 		in = 0;
  70 		out = 0;
  71 		chars = 0;
  72 		for(int i = 0; i < LEN; i++)
  73 			thebuffer[i] = initValue;
  74 	}
  75 
  76 	T get()
  77 	{
  78 		T c = thebuffer[out];
  79 		//cout << this << " Getting:" << c << " in=" << in << " out=" << out << " Chars=" << chars << endl;
  80 		out = ( out + 1 ) % LEN;
  81 		chars--;
  82 		return c;
  83 	}
  84 
  85 	void put(T c)
  86 	{
  87 		//cout << this << " Putting:" << c << " in=" << in << " out=" << out << " Chars=" << chars <<endl;
  88 		chars++;
  89         thebuffer[in] = c;
  90 		in = (in + 1) % LEN;
  91 	}
  92 	bool vuota()
  93 	{ return chars == 0; }
  94 };
  95 
  96 
  97 template <class T>
  98 class BufferCondiviso : public BufferPrivato<T>
  99 {
 100 
 101 public:
 102 	BufferCondiviso(T initValue = 0) : BufferPrivato<T>(initValue)
 103 	{}
 104 
 105 	T get()
 106 	{
 107 		JTCSynchronized sync(*this);
 108 		while (BufferPrivato<T>::chars == 0) {
 109 			JTCMonitor::wait();
 110 		}
 111 		return ((BufferPrivato<T>*)this)->get();
 112 	}
 113 
 114 	void put(T c)
 115 	{
 116 		JTCSynchronized sync(*this);
 117 		while (BufferPrivato<T>::chars == LEN) {
 118 			JTCMonitor::wait();
 119 		}
 120 		((BufferPrivato<T>*)this)->put(c);
 121 		JTCMonitor::notifyAll();
 122 	}
 123 };
 124 
 125 typedef BufferCondiviso<Messaggio*> CodaCondivisa;
 126 typedef BufferPrivato<Messaggio*> CodaPrivata;
 127 
 128 
 129 
 130 class DatoGPS
 131 
 132 {
 133 public:
 134     double latitudine;
 135     double longitudine;
 136     int dataRilevamento;
 137     DatoGPS(double lat, double lon, int data) : latitudine(lat), longitudine(lon), dataRilevamento(data) {};
 138 };
 139 
 140 class Immagine
 141 {
 142 public:
 143 	char* dati;
 144 	int dataScatto;
 145 	double latitudine;
 146 	double longitudine;
 147     Immagine(char* d,int data) : dati(d), dataScatto(data) {};
 148 };
 149 
 150 
 151 class CameraThread : public JTCThread
 152 {
 153      CodaCondivisa* c;
 154      int codiceCamera;
 155 public:
 156     CameraThread(int codiceCamera, CodaCondivisa* c)
 157 	{
 158 		this->c = c;
 159 		srand(time(0));
 160 		this->codiceCamera = codiceCamera;
 161 	}
 162 	virtual void run()
 163 	{
 164 		while(true) {
 165 			int t = rand() % 2500;
 166 			coutSync.lock();
 167 			cout << "--CameraThread " << codiceCamera << "--S=" << t << endl;
 168 			coutSync.unlock();
 169 			sleep(t);
 170 			c->put(new Messaggio(IMMAGINE, codiceCamera,new Immagine(0,getTime())));
 171 			}
 172 	}
 173 
 174 };
 175 
 176 #define DELTA 0.01
 177 
 178 class GPSThread : public JTCThread
 179 
 180 {
 181 	double latitudine;
 182 	double longitudine;
 183 	CodaCondivisa* c;
 184 
 185 public:
 186 	GPSThread(CodaCondivisa* c)
 187 	{
 188 		latitudine = rand() % 180 - 90;
 189 		longitudine = rand() % 360 - 180;
 190 		this->c = c;
 191 		srand(time(0));
 192 	}
 193 
 194 	virtual void run()
 195 	{
 196 		while(true) {
 197 		//
 198 		//  modifica posizione corrente di un valore casuale
 199 		//
 200 		latitudine  += (rand() % 10 - 10) * DELTA;
 201 		longitudine += (rand() % 10 - 10) * DELTA;
 202 		//
 203 		//  Qui mettere codice che arrotonda nel caso i valori escano dal range ammesso
 204 		//  -90 +90 per latitudine
 205 		//  -180 +180 per longitudine
 206 		//
 207         // ...
 208 		//
 209         c->put(new Messaggio(GPS,0,new DatoGPS(latitudine,longitudine,getTime())));
 210 		coutSync.lock();
 211 		cout << "--GPSTHREAD--\n";
 212 		coutSync.unlock();
 213 
 214         sleep(5000);
 215 		}
 216 	}
 217 };
 218 
 219 
 220 class GeoTagThread : public JTCThread
 221 
 222 {
 223 	double ultimaLat;
 224 	double ultimaLong;
 225 	int  tempoUltimoGPS;
 226 	double precLat;
 227 	double precLong;
 228 	bool primoDato;
 229 	int tempoPrecGPS;
 230 	CodaPrivata* codaPrivata;
 231 	CodaCondivisa* codaComune;
 232 
 233 public:
 234 	GeoTagThread(CodaCondivisa* c)
 235 	{
 236 		codaComune = c;
 237 		codaPrivata = new CodaPrivata();
 238 
 239 	}
 240 	virtual void run() {
 241         Messaggio* m;
 242     	while (true)
 243 		{
 244 			m = codaComune->get();
 245 			coutSync.lock();
 246 			cout << "Message from" << m->codiceThread << endl;
 247 			coutSync.unlock();
 248 
 249 			if (m->codice == GPS)
 250 			{
 251 				precLat = ultimaLat;
 252 				precLong = ultimaLong;
 253 				tempoPrecGPS = tempoUltimoGPS;
 254 				ultimaLat = ((DatoGPS*)(m->dato))->latitudine;
 255 				ultimaLong = ((DatoGPS*)(m->dato))->longitudine;
 256 				tempoUltimoGPS = ((DatoGPS*)(m->dato))->dataRilevamento;
 257 
 258 				//
 259 				//  Pone come identici ultimo e penultimo rilevamento alla prima lettura
 260 				//
 261 				if( primoDato )
 262                 {
 263                     primoDato = false;
 264                     precLat = ultimaLat;
 265                     precLong = ultimaLong;
 266                 }
 267 				//
 268 				// Elabora le immagini acquisite tra un rilevamento GPS e l'altro
 269 				//
 270 
 271 				while(!codaPrivata->vuota())
 272 				{
 273 					Messaggio* n = codaPrivata->get();
 274 					Immagine* imm = (Immagine*)(n->dato);
 275 					//
 276 					// Assegna le coordinate dell'ultimo rilevamento GPS temporalmente più vicino
 277 					//
 278 					if ( abs(imm->dataScatto - tempoUltimoGPS) < abs(imm->dataScatto - tempoPrecGPS) )
 279 					{
 280 						imm->latitudine = ultimaLat;
 281 						imm->longitudine = ultimaLong;
 282 					}
 283 					else
 284 					{
 285 						imm->latitudine = precLat;
 286 						imm->longitudine = precLong;
 287 					}
 288 					coutSync.lock();
 289 					cout << "--GEOTAGTHREAD-\n";
 290 					cout << "Immagine proveniente da M" << n << "-" << n->codiceThread << endl;
 291 					cout << "DataScatto:" << imm->dataScatto << endl;
 292 					cout << "Posizione Geografica: LAT=" << imm->latitudine << " LONG=" << imm->longitudine << endl;
 293 					cout << "---------------\n";
 294 					coutSync.unlock();
 295 				}
 296 			}
 297 			else
 298 			// Codice == IMMAGINE
 299 			{
 300 				//
 301 				//  Salva il messaggio contenente l'immagine per successiva elaborazione
 302 				//
 303 				codaPrivata->put(m);
 304 			} // big if
 305 		}
 306     }
 307 };
 308 
 309 class GeoTaggingModule
 310 {
 311 	CameraThread* M1;
 312 	CameraThread* M2;
 313 	GPSThread* g;
 314 	CodaCondivisa* c;
 315 	GeoTagThread* geo;
 316 
 317 public:
 318 	GeoTaggingModule()
 319 	{
 320 		c = new CodaCondivisa();
 321 		M1 = new CameraThread(1,c);
 322 		M2 = new CameraThread(2,c);
 323 		g = new GPSThread(c);
 324 		geo = new GeoTagThread(c);
 325 		M1->start();
 326 		M2->start();
 327 		g->start();
 328 		geo->start();
 329 	}
 330 
 331 };
 332 
 333 
 334 int main()
 335 {
 336    JTCInitialize init;
 337    GeoTaggingModule* module = new GeoTaggingModule();
 338    return 0;
 339 }

Allegati

Non è consentito inserire allegati su questa pagina.