1 /* |
|
2 * Copyright (c) 2005 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: This defines the engine class for Location satellite info |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 // INCLUDE FILES |
|
20 #include <eikenv.h> |
|
21 #include <e32math.h> |
|
22 #include <e32cmn.h> |
|
23 #include <libc/math.h> |
|
24 #include "SatInfoConsts.h" |
|
25 #include "CSatelliteEng.h" |
|
26 #include "MEngObserver.h" |
|
27 #include "csatellitemeasurementsetting.h" |
|
28 #include "Debug.h" |
|
29 |
|
30 // ============================ FUNCTIONS DECLARATION ========================= |
|
31 TInt SortCriteria(const TSatelliteData& aCurrent, const TSatelliteData& aNext); |
|
32 |
|
33 |
|
34 // ============================ MEMBER FUNCTIONS ============================== |
|
35 // ---------------------------------------------------------------------------- |
|
36 // CSatelliteEng::NewL |
|
37 // Two-phased constructor. |
|
38 // ---------------------------------------------------------------------------- |
|
39 // |
|
40 CSatelliteEng* CSatelliteEng::NewL() |
|
41 { |
|
42 CSatelliteEng* self = new( ELeave ) CSatelliteEng; |
|
43 CleanupStack::PushL( self ); |
|
44 self->ConstructL(); |
|
45 CleanupStack::Pop(); // self |
|
46 return self; |
|
47 } |
|
48 |
|
49 // --------------------------------------------------------- |
|
50 // CSatelliteEng::ConstructL |
|
51 // --------------------------------------------------------- |
|
52 // |
|
53 void CSatelliteEng::ConstructL() |
|
54 { |
|
55 User::LeaveIfError( iServer.Connect() ); |
|
56 User::LeaveIfError(iPositioner.Open( iServer )); |
|
57 |
|
58 // System of Measurements |
|
59 iMeasurementSetting = CSatelliteMeasurementSetting::NewL(*this); |
|
60 |
|
61 // Keplers parameters |
|
62 // Define parameters for the Sun |
|
63 iKeplers[0].iM.iA0=356.0470; |
|
64 iKeplers[0].iM.iA1=0.9856002585; |
|
65 iKeplers[0].iW.iA0=282.9404; |
|
66 iKeplers[0].iW.iA1=4.70935e-005; |
|
67 iKeplers[0].iE.iA0=0.016709; |
|
68 iKeplers[0].iE.iA1=-1.151e-009; |
|
69 |
|
70 //Define parameters for the Moon |
|
71 iKeplers[1].iM.iA0=115.3654; |
|
72 iKeplers[1].iM.iA1=13.0649929509; |
|
73 iKeplers[1].iW.iA0=318.0634; |
|
74 iKeplers[1].iW.iA1=0.1643573223; |
|
75 iKeplers[1].iE.iA0=0.0549; |
|
76 iKeplers[1].iE.iA1=0.0; |
|
77 } |
|
78 |
|
79 // ---------------------------------------------------------------------------- |
|
80 // CSatelliteEng::CSatelliteEng |
|
81 // C++ default constructor can NOT contain any code, that |
|
82 // might leave. |
|
83 // ---------------------------------------------------------------------------- |
|
84 // |
|
85 CSatelliteEng::CSatelliteEng() : CActive(EPriorityStandard), iRequesting( EFalse ) |
|
86 { |
|
87 CActiveScheduler::Add( this ); |
|
88 } |
|
89 |
|
90 // --------------------------------------------------------- |
|
91 // CSatelliteEng::~CSatelliteEng |
|
92 // --------------------------------------------------------- |
|
93 // |
|
94 CSatelliteEng::~CSatelliteEng() |
|
95 { |
|
96 Cancel(); |
|
97 iPositioner.Close(); |
|
98 iServer.Close(); |
|
99 iSortedSatData.Reset(); |
|
100 delete iMeasurementSetting; |
|
101 } |
|
102 |
|
103 // --------------------------------------------------------- |
|
104 // CSatelliteEng::RunL |
|
105 // --------------------------------------------------------- |
|
106 // |
|
107 void CSatelliteEng::RunL() |
|
108 { |
|
109 TInt status = iStatus.Int(); |
|
110 |
|
111 /// Check if we have satellite data |
|
112 if(!(status == KErrNone || |
|
113 status == KPositionPartialUpdate) ) |
|
114 { |
|
115 ClearSatelliteData(); |
|
116 } |
|
117 SortSatelliteDataL(); |
|
118 |
|
119 if ( iObserver ) |
|
120 { |
|
121 iObserver->NotifyL( iStatus.Int() ); |
|
122 } |
|
123 #ifdef _DEBUG |
|
124 DEBUG1( "CSatelliteEng::RunL(): Error = %d", iStatus.Int() ); |
|
125 #endif |
|
126 |
|
127 if ( iRequesting && !IsActive()) |
|
128 { |
|
129 iPositioner.NotifyPositionUpdate( iSatelliteInfo, iStatus); |
|
130 SetActive(); |
|
131 } |
|
132 } |
|
133 |
|
134 // --------------------------------------------------------- |
|
135 // CSatelliteEng::DoCancel |
|
136 // --------------------------------------------------------- |
|
137 // |
|
138 void CSatelliteEng::DoCancel() |
|
139 { |
|
140 iPositioner.CancelRequest( EPositionerNotifyPositionUpdate ); |
|
141 } |
|
142 |
|
143 // --------------------------------------------------------- |
|
144 // CSatelliteEng::RunError |
|
145 // --------------------------------------------------------- |
|
146 // |
|
147 TInt CSatelliteEng::RunError(TInt aError) |
|
148 { |
|
149 if ( iObserver && (aError == KErrCancel) ) |
|
150 { |
|
151 iObserver->NotifyError( aError ); |
|
152 return KErrNone; |
|
153 } |
|
154 return aError; |
|
155 } |
|
156 // --------------------------------------------------------- |
|
157 // CSatelliteEng::SetObserver |
|
158 // --------------------------------------------------------- |
|
159 // |
|
160 void CSatelliteEng::SetObserver(MEngObserver& aObserver) |
|
161 { |
|
162 iObserver = &aObserver; |
|
163 } |
|
164 |
|
165 // ---------------------------------------------------------------------------- |
|
166 // CSatelliteEng::RequestSatelliteDataL |
|
167 // ---------------------------------------------------------------------------- |
|
168 // |
|
169 void CSatelliteEng::RequestSatelliteDataL( const TDesC& aNameOfRule ) |
|
170 { |
|
171 UpdateIntervalL(); |
|
172 |
|
173 User::LeaveIfError(iPositioner.SetRequestor(CRequestor::ERequestorService, |
|
174 CRequestor::EFormatApplication, |
|
175 aNameOfRule )); |
|
176 |
|
177 iPositioner.NotifyPositionUpdate( iSatelliteInfo, iStatus); |
|
178 SetActive(); |
|
179 iRequesting = ETrue; |
|
180 } |
|
181 |
|
182 // ---------------------------------------------------------------------------- |
|
183 // CSatelliteEng::GetSatelliteData |
|
184 // ---------------------------------------------------------------------------- |
|
185 // |
|
186 TInt CSatelliteEng::GetSatelliteData( TUint aIndex, |
|
187 TSatelliteData& aSatelliteData) const |
|
188 { |
|
189 if( aIndex >= iSortedSatData.Count() ) |
|
190 { |
|
191 return KErrNotFound; |
|
192 } |
|
193 aSatelliteData = iSortedSatData[aIndex]; |
|
194 return KErrNone; |
|
195 } |
|
196 |
|
197 // ---------------------------------------------------------------------------- |
|
198 // CSatelliteEng::GetPosition |
|
199 // ---------------------------------------------------------------------------- |
|
200 // |
|
201 void CSatelliteEng::GetPosition(TPosition& aPosition) |
|
202 { |
|
203 iSatelliteInfo.GetPosition(aPosition); |
|
204 } |
|
205 |
|
206 // ---------------------------------------------------------------------------- |
|
207 // CSatelliteEng::GetCourse |
|
208 // ---------------------------------------------------------------------------- |
|
209 // |
|
210 void CSatelliteEng::GetCourse(TCourse& aCourse) |
|
211 { |
|
212 iSatelliteInfo.GetCourse(aCourse); |
|
213 } |
|
214 |
|
215 // ---------------------------------------------------------------------------- |
|
216 // CSatelliteEng::SortSatelliteDataL |
|
217 // ---------------------------------------------------------------------------- |
|
218 // |
|
219 void CSatelliteEng::SortSatelliteDataL() |
|
220 { |
|
221 TSatelliteData tempSatelliteData; |
|
222 TLinearOrder<TSatelliteData> order(SortCriteria); |
|
223 iSortedSatData.Reset(); |
|
224 TInt satellitesInView = iSatelliteInfo.NumSatellitesInView(); |
|
225 for(TInt i = 0;i < satellitesInView ;i++ ) |
|
226 { |
|
227 User::LeaveIfError(iSatelliteInfo.GetSatelliteData(i,tempSatelliteData)); |
|
228 if(tempSatelliteData.SignalStrength() <= KBlidMinSatelliteSignalStrength) |
|
229 { |
|
230 continue; |
|
231 } |
|
232 iSortedSatData.InsertInOrderAllowRepeats(tempSatelliteData, order); |
|
233 } |
|
234 } |
|
235 |
|
236 // ---------------------------------------------------------------------------- |
|
237 // CSatelliteEng::ObjectElevationAzimuth |
|
238 // ---------------------------------------------------------------------------- |
|
239 // |
|
240 void CSatelliteEng::ObjectElevationAzimuth(TInt aIndex, TReal& aAzimuth, TReal& aElevation ) |
|
241 { |
|
242 TTime time = iSatelliteInfo.SatelliteTime(); |
|
243 TDateTime dateTime = time.DateTime(); |
|
244 TReal hours = dateTime.Hour(); |
|
245 TReal minutes = dateTime.Minute(); |
|
246 TReal seconds = dateTime.Second(); |
|
247 TInt day = dateTime.Day() + 1; |
|
248 TInt month = dateTime.Month() + 1; |
|
249 TInt year = dateTime.Year(); |
|
250 TReal julianDay, julianCentury; |
|
251 |
|
252 //Universal time is the number of hours from the begining of the day |
|
253 TReal universalTime = hours + minutes/60.0 + seconds/(60.0*60.0); |
|
254 Julian(julianDay, julianCentury, day, month, year, universalTime); |
|
255 |
|
256 //This algorithm uses a starting point for the Julian day count that is 1.5 days earlier. |
|
257 TReal JD_Mod = julianDay - 2451545.0 + 1.5; |
|
258 |
|
259 //Mean anomaly, in degrees |
|
260 TReal m=iKeplers[aIndex].iM.iA0 + iKeplers[aIndex].iM.iA1 * (JD_Mod); |
|
261 //Argument of perihelion, in degrees |
|
262 TReal w=iKeplers[aIndex].iW.iA0 + iKeplers[aIndex].iW.iA1 * (JD_Mod); |
|
263 //eccentricity |
|
264 TReal e=iKeplers[aIndex].iE.iA0 + iKeplers[aIndex].iE.iA1 * (JD_Mod); |
|
265 |
|
266 // Calculate true anomaly |
|
267 TReal E = Kepler(e, m*KPi/180); |
|
268 |
|
269 TReal tmp, sinE, cosE; |
|
270 Math::Sqrt(tmp, 1 - e * e); |
|
271 Math::Sin(sinE, E); |
|
272 Math::Cos(cosE, E); |
|
273 |
|
274 TReal v; |
|
275 v = atan2(tmp * sinE, cosE - e); //In radians |
|
276 |
|
277 TReal truelongitude = v + (KPi/180) * w; // in radians |
|
278 TReal ra, dd; |
|
279 TrueLongToRaDeltadec(ra, dd, truelongitude); |
|
280 ra = ra * 360/24; //In degrees |
|
281 dd = dd * 180/KPi; // In degrees |
|
282 |
|
283 TPosition position; |
|
284 TLocality locality; |
|
285 |
|
286 iSatelliteInfo.GetPosition(position); |
|
287 |
|
288 TReal tau = SideRealTime(julianDay, julianCentury, position.Longitude(), ra); |
|
289 |
|
290 LocationCoordinates(aAzimuth, aElevation, tau, dd, position.Latitude()); |
|
291 } |
|
292 |
|
293 // ---------------------------------------------------------------------------- |
|
294 // CSatelliteEng::SideRealTime |
|
295 // ---------------------------------------------------------------------------- |
|
296 // |
|
297 TReal CSatelliteEng::SideRealTime(TReal aJulianDay, |
|
298 TReal aJulianCentury, |
|
299 TReal aLongitude, |
|
300 TReal aRa) |
|
301 { |
|
302 TReal theta0 = 280.46061837 + |
|
303 360.98564736629 * (aJulianDay-2451545.0) + |
|
304 0.000387933*aJulianCentury*aJulianCentury - |
|
305 aJulianCentury*aJulianCentury*aJulianCentury/38710000; |
|
306 TReal rem; |
|
307 Math::Mod(rem, theta0, 360); |
|
308 theta0 = rem; |
|
309 TReal theta = theta0 + aLongitude; |
|
310 return (theta - aRa); |
|
311 } |
|
312 |
|
313 // ---------------------------------------------------------------------------- |
|
314 // CSatelliteEng::LocationCoordinates |
|
315 // ---------------------------------------------------------------------------- |
|
316 // |
|
317 void CSatelliteEng::LocationCoordinates(TReal& aAzimuth, |
|
318 TReal& aElevation, |
|
319 TReal aTau, |
|
320 TReal aDd, |
|
321 TReal aLatitude) |
|
322 { |
|
323 //Convert latitude from degrees to radians |
|
324 TReal sinValue; |
|
325 Math::Sin(sinValue, aLatitude * KPi/180); |
|
326 TReal sinValue2; |
|
327 Math::Sin(sinValue2, aDd * KPi/180); |
|
328 |
|
329 TReal cosValue; |
|
330 Math::Cos(cosValue, aLatitude * KPi/180); |
|
331 TReal cosValue2; |
|
332 Math::Cos(cosValue2, aDd * KPi/180); |
|
333 TReal cosValue3; |
|
334 Math::Cos(cosValue3, aTau * KPi/180); |
|
335 |
|
336 TReal sinh = sinValue * sinValue2 + cosValue * cosValue2 * cosValue3; |
|
337 |
|
338 TReal asinValue; |
|
339 Math::ASin(asinValue, sinh); |
|
340 aElevation = (180/KPi) * asinValue; |
|
341 |
|
342 Math::Sin(sinValue, aTau * KPi / 180); |
|
343 TReal a1 = -sinValue; |
|
344 |
|
345 TReal tanValue; |
|
346 Math::Tan(tanValue, aDd * KPi / 180); |
|
347 TReal a2 = cosValue * tanValue - sinValue * cosValue3; |
|
348 |
|
349 TReal atanvalue; |
|
350 atanvalue = atan2(a1, a2); |
|
351 aAzimuth = (180/KPi) * atanvalue; |
|
352 |
|
353 if(aAzimuth < 0) |
|
354 { |
|
355 aAzimuth += 360; |
|
356 } |
|
357 } |
|
358 |
|
359 // ---------------------------------------------------------------------------- |
|
360 // CSatelliteEng::Julian |
|
361 // ---------------------------------------------------------------------------- |
|
362 // |
|
363 void CSatelliteEng::Julian(TReal& aJulianDay, |
|
364 TReal& aJulianCentury, |
|
365 TInt aDay, |
|
366 TInt aMonth, |
|
367 TInt aYear, |
|
368 TReal aUniversalTime) |
|
369 { |
|
370 if(aMonth <= 2) |
|
371 { |
|
372 aMonth += 12; |
|
373 --aYear; |
|
374 } |
|
375 TInt tmp; |
|
376 tmp = 365.25 * aYear; |
|
377 TReal temp = tmp; |
|
378 |
|
379 tmp = 30.6001 * (aMonth+1); |
|
380 TReal temp2 = tmp; |
|
381 |
|
382 aJulianDay = temp + temp2 - 15.0 + 1720996.5 + aDay + aUniversalTime/24; |
|
383 aJulianCentury = (aJulianDay - 2451545.0)/36525.0; |
|
384 } |
|
385 |
|
386 // ---------------------------------------------------------------------------- |
|
387 // CSatelliteEng::Kepler |
|
388 // ---------------------------------------------------------------------------- |
|
389 // |
|
390 TReal CSatelliteEng::Kepler(TReal aE, TReal aM) |
|
391 { |
|
392 //Solving Kepler's equation by the Newton-Raphson iteration |
|
393 //Note aE, aM in radians |
|
394 TReal sinm, cosm; |
|
395 TReal E = aE; |
|
396 Math::Sin(sinm, aM); |
|
397 Math::Cos(cosm, aM); |
|
398 TReal EO = aM + aE * sinm * (1 - aE * cosm); |
|
399 |
|
400 while(1) |
|
401 { |
|
402 TReal sineo, coseo; |
|
403 Math::Sin(sineo, EO); |
|
404 Math::Cos(coseo, EO); |
|
405 E = EO - (EO - aE * sineo - aM) / (1-aE * coseo); |
|
406 |
|
407 if( Abs(E - EO) < (1e-6) ) |
|
408 { |
|
409 break; |
|
410 } |
|
411 EO = E; |
|
412 } |
|
413 return E; |
|
414 } |
|
415 |
|
416 // ---------------------------------------------------------------------------- |
|
417 // CSatelliteEng::TrueLongToRaDeltadec |
|
418 // ---------------------------------------------------------------------------- |
|
419 // |
|
420 void CSatelliteEng::TrueLongToRaDeltadec(TReal& aRa,TReal& aDd,TReal aTrueLongitude) |
|
421 { |
|
422 //This function converts True Longitude (in radians) to Right Ascension (in hours) and |
|
423 //Delta declination (in radians) |
|
424 TReal X; |
|
425 Math::Cos(X, aTrueLongitude); |
|
426 |
|
427 TReal sintl; |
|
428 Math::Sin(sintl, aTrueLongitude); |
|
429 |
|
430 TReal cosvalue; |
|
431 Math::Cos(cosvalue, 23.43999 * KPi/180); |
|
432 TReal Y = cosvalue * sintl; |
|
433 |
|
434 TReal sinvalue; |
|
435 Math::Sin(sinvalue, 23.43999 * KPi/180); |
|
436 TReal Z = sinvalue * sintl; |
|
437 |
|
438 TReal R; |
|
439 Math::Sqrt(R, 1-Z*Z); |
|
440 |
|
441 Math::ATan(aDd, Z/R); |
|
442 |
|
443 TReal tanValue; |
|
444 Math::ATan(tanValue, Y/(X+R) ); |
|
445 aRa = (24/KPi)*tanValue; |
|
446 } |
|
447 |
|
448 // ---------------------------------------------------------------------------- |
|
449 // CSatelliteEng::GetSunAzimuthElevation |
|
450 // ---------------------------------------------------------------------------- |
|
451 // |
|
452 void CSatelliteEng::GetSunAzimuthElevation(TReal& aAzimuth, TReal& aElevation ) |
|
453 { |
|
454 ObjectElevationAzimuth(0, aAzimuth, aElevation ); |
|
455 } |
|
456 |
|
457 // ---------------------------------------------------------------------------- |
|
458 // CSatelliteEng::GetMoonAzimuthElevation |
|
459 // ---------------------------------------------------------------------------- |
|
460 // |
|
461 void CSatelliteEng::GetMoonAzimuthElevation(TReal& aAzimuth, TReal& aElevation ) |
|
462 { |
|
463 ObjectElevationAzimuth(1, aAzimuth, aElevation ); |
|
464 } |
|
465 |
|
466 // ---------------------------------------------------------------------------- |
|
467 // CSatelliteEng::NumberOfSatellites |
|
468 // ---------------------------------------------------------------------------- |
|
469 // |
|
470 TInt CSatelliteEng::NumberOfSatellites() |
|
471 { |
|
472 return iSortedSatData.Count(); |
|
473 } |
|
474 |
|
475 // ---------------------------------------------------------------------------- |
|
476 // CSatelliteEng::NumberOfSatellites |
|
477 // ---------------------------------------------------------------------------- |
|
478 // |
|
479 TInt CSatelliteEng::NumberOfSatellitesUsed() |
|
480 { |
|
481 return iSatelliteInfo.NumSatellitesUsed(); |
|
482 } |
|
483 |
|
484 // ---------------------------------------------------------------------------- |
|
485 // CSatelliteEng::StopRequesting |
|
486 // ---------------------------------------------------------------------------- |
|
487 // |
|
488 void CSatelliteEng::StopRequesting() |
|
489 { |
|
490 iRequesting = EFalse; |
|
491 } |
|
492 |
|
493 // ---------------------------------------------------------------------------- |
|
494 // CSatelliteEng::StartRequestingL |
|
495 // ---------------------------------------------------------------------------- |
|
496 // |
|
497 void CSatelliteEng::StartRequestingL() |
|
498 { |
|
499 if ( !IsActive() ) |
|
500 { |
|
501 iPositioner.NotifyPositionUpdate( iSatelliteInfo, iStatus ); |
|
502 SetActive(); |
|
503 } |
|
504 iRequesting = ETrue; |
|
505 } |
|
506 |
|
507 // ---------------------------------------------------------------------------- |
|
508 // CSatelliteEng::UpdateIntervalL |
|
509 // ---------------------------------------------------------------------------- |
|
510 // |
|
511 void CSatelliteEng::UpdateIntervalL( ) |
|
512 { |
|
513 TPositionUpdateOptions updateOptions; |
|
514 updateOptions.SetUpdateInterval(TTimeIntervalMicroSeconds(KSatelliteIntervalTime)); |
|
515 updateOptions.SetAcceptPartialUpdates( ETrue ); |
|
516 updateOptions.SetUpdateTimeOut(TTimeIntervalMicroSeconds(KSatelliteTimeOut)); |
|
517 User::LeaveIfError( iPositioner.SetUpdateOptions(updateOptions) ); |
|
518 } |
|
519 |
|
520 // ---------------------------------------------------------------------------- |
|
521 // CSatelliteEng::ClearSatelliteData |
|
522 // ---------------------------------------------------------------------------- |
|
523 // |
|
524 void CSatelliteEng::ClearSatelliteData() |
|
525 { |
|
526 iSatelliteInfo.ClearSatellitesInView(); |
|
527 } |
|
528 |
|
529 // ---------------------------------------------------------------------------- |
|
530 // CSatelliteEng::IsSatelliteDataAvailable |
|
531 // ---------------------------------------------------------------------------- |
|
532 // |
|
533 TBool CSatelliteEng::IsSatelliteDataAvailable() |
|
534 { |
|
535 TInt count = iSortedSatData.Count(); |
|
536 if(count == 0) |
|
537 { |
|
538 return EFalse; |
|
539 } |
|
540 return ETrue; |
|
541 } |
|
542 |
|
543 // ---------------------------------------------------------------------------- |
|
544 // CSatelliteEng::SysOfMeasurementL |
|
545 // ---------------------------------------------------------------------------- |
|
546 // |
|
547 TLocSystemofMeasurementValues CSatelliteEng::SysOfMeasurementL() |
|
548 { |
|
549 return iMeasurementSetting->SysofMeasurementL( ); |
|
550 } |
|
551 |
|
552 // ---------------------------------------------------------------------------- |
|
553 // CSatelliteEng::HandleSysMeasureValueL |
|
554 // ---------------------------------------------------------------------------- |
|
555 // |
|
556 void CSatelliteEng::HandleSysMeasureValueL() |
|
557 { |
|
558 iObserver->NotifyL( KErrNone ); |
|
559 } |
|
560 |
|
561 // ---------------------------------------------------------------------------- |
|
562 // SortCriteria |
|
563 // ---------------------------------------------------------------------------- |
|
564 // |
|
565 TInt SortCriteria(const TSatelliteData& aFirst, const TSatelliteData& aNext) |
|
566 { |
|
567 if(aFirst.SignalStrength() > aNext.SignalStrength()) |
|
568 { |
|
569 return 1; |
|
570 } |
|
571 else if(aFirst.SignalStrength() < aNext.SignalStrength()) |
|
572 { |
|
573 return -1; |
|
574 } |
|
575 return 0; |
|
576 } |
|
577 |
|
578 |
|
579 // End of File |
|