source: trunk/MagicSoft/Cosy/main/MTracking.cc@ 5027

Last change on this file since 5027 was 4256, checked in by tbretz, 21 years ago
*** empty log message ***
File size: 17.9 KB
Line 
1#include "MTracking.h"
2
3#include "macs.h"
4#include "shaftencoder.h"
5
6#include "MCosy.h"
7#include "SlaStars.h"
8
9#include "MDriveCom.h"
10
11ClassImp(MTracking);
12
13//#define EXPERT
14#undef EXPERT
15
16// --------------------------------------------------------------------------
17//
18// request the current positions from the rotary encoders.
19// use GetRePos to get the psotions. If the request fails the function
20// returns kFALSE, otherwise kTRUE
21//
22bool MTracking::RequestRePos()
23{
24 //
25 // Send request
26 //
27 fCosy->fMac2->RequestSDO(0x6004);
28 fCosy->fMac1->RequestSDO(0x6004);
29
30 //
31 // Wait until the objects are received.
32 //
33 fCosy->fMac2->WaitForSdo(0x6004);
34 fCosy->fMac1->WaitForSdo(0x6004);
35
36 //
37 // If waiting was not interrupted everything is ok. return.
38 //
39 if (!Break())
40 return true;
41
42 //
43 // If the waiting was interrupted due to a network error,
44 // print some logging message.
45 //
46 if (fCosy->HasError())
47 lout << "Error while requesting re pos from Macs (SDO #6004)" << endl;
48
49 return false;
50}
51
52// --------------------------------------------------------------------------
53//
54// Initializes Tracking mode
55//
56// Initializes the accelerations of both axes with 90% of the maximum
57// acceleration. Set the status for moving and tracking and starts thr
58// revolution mode.
59//
60bool MTracking::InitTracking()
61{
62 // FIXME? Handling of Zombie OK?
63 if (fCosy->fMac1->IsZombieNode() || fCosy->fMac2->IsZombieNode())
64 return false;
65
66 //
67 // Start revolution mode
68 //
69 if (!SetAccDec(fCosy->fMac2, fTrackAcc, fTrackDec))
70 return false;
71
72 if (!SetAccDec(fCosy->fMac1, fTrackAcc, fTrackDec))
73 return false;
74
75 fCosy->fMac2->SetRpmMode(TRUE);
76 if (fCosy->fMac2->IsZombieNode())
77 return false;
78
79 fCosy->fMac1->SetRpmMode(TRUE);
80 if (fCosy->fMac1->IsZombieNode())
81 return false;
82
83 fCosy->SetStatus(MDriveCom::kMoving | MDriveCom::kTracking);
84
85 return true;
86}
87/*
88void MTracking::StopTracking()
89{
90 //
91 // Set status to Stopping
92 //
93 fCosy->SetStatus(MDriveCom::kStopping);
94
95 //
96 // set deceleration to 50%
97 //
98 cout << "Stopping tracking (dec=20%)..." << endl;
99 fCosy->fMac1->SetDeceleration(0.2*fMac1->GetVelRes());
100 fCosy->fMac2->SetDeceleration(0.2*fMac2->GetVelRes());
101
102 fCosy->fMac2->SendSDO(0x3006, 1, (LWORD_t)0); // SetRpmVelocity [re/min]
103 fCosy->fMac1->SendSDO(0x3006, 1, (LWORD_t)0); // SetRpmVelocity [re/min]
104 fCosy->fMac2->WaitForSdo(0x3006, 1);
105 fCosy->fMac1->WaitForSdo(0x3006, 1);
106
107 cout << "Waiting for end of movement..." << endl;
108 fCosy->WaitForEndMovement();
109
110 //
111 // Wait for the objects to be OKed.
112 //
113 fCosy->fMac1->SetRpmMode(FALSE);
114 fCosy->fMac2->SetRpmMode(FALSE);
115
116 //
117 // Wait for the movement to really be finished.
118 //
119 //cout << "Waiting for end of movement..." << endl;
120 //WaitForEndMovement();
121
122 //
123 // Check whether everything works fine.
124 //
125 fCosy->CheckForError();
126 cout << "Movement stopped." << endl;
127}
128*/
129// --------------------------------------------------------------------------
130//
131// Limits the speed.
132//
133// This function should work as a limiter. If a tracking error is too large
134// to be corrected fast enough we would get enormous velocities. These
135// velocities are limited to the maximum velocity.
136//
137Bool_t MTracking::LimitSpeed(ZdAz *vt, const SlaStars &sla) const
138{
139 // vt[re/min]
140
141 // Calculate approximate velocity of both axis
142 ZdAz vcalc = sla.GetApproxVel(fCosy->fRaDec); // [rad/rad]
143
144 //vcalc *= 1./(24*60); // [U_tel/min]
145 //vcalc *= fCosy->kGearTot; // [U_mot/min]
146 //vcalc *= fCosy->kResRE; // [re/min]
147
148 vcalc *= fCosy->kGearTot*fCosy->kResRE/(24*60); // [re/min]
149
150 // Set return code
151 Bool_t rc = kFALSE;
152
153 //
154 // How to limit the speed. If the wind comes and blowes
155 // we cannot forbid changing of the sign. But on the other hand
156 // we don't want fast changes!
157 //
158 ULong_t vrzd = fCosy->fMac1->GetVelRes();
159 ULong_t vraz = fCosy->fMac2->GetVelRes();
160
161#define sgn(x) (x<0?-1:1)
162
163 //
164 // When speed changes sign, the maximum allowed speed
165 // is 25% of the |v|
166 //
167 //const Float_t limit = 0.25;
168
169 //
170 // The maximum allowed speed while tracking is 10%
171 //
172 const Float_t maxtrack = 0.1;
173
174 if (fabs(vt->Az()) > maxtrack*vraz*4)
175 {
176 vt->Az(maxtrack*vraz*4*sgn(vcalc.Az()));
177 lout << "Warning: Azimuth speed limit (" << maxtrack*100 << "%) exceeded (" << fabs(vt->Az()) << " > " << maxtrack*vraz << ")... limited." << endl;
178 lout << "Vcalc: " << vcalc.Zd() << " " << vcalc.Az() << "re/min" <<endl;
179 rc=kTRUE;
180 }
181 if (fabs(vt->Zd()) > maxtrack*vrzd*4)
182 {
183 vt->Zd(maxtrack*vrzd*4*sgn(vcalc.Zd()));
184 lout << "Warning: Altitude speed limit (" << maxtrack*100 << "%) exceeded (" << fabs(vt->Zd()) <<" > " << maxtrack*vrzd << ")... limited." << endl;
185 lout << "Vcalc: " << vcalc.Zd() << " " << vcalc.Az() << "re/min" <<endl;
186 rc=kTRUE;
187 }
188 return rc;
189}
190
191// --------------------------------------------------------------------------
192//
193// Sets the tracking velocity
194//
195// The velocities are given in a ZdAz object in re/min. Return kTRUE
196// in case of success, kFALSE in case of failure.
197//
198Bool_t MTracking::SetVelocity(const ZdAz &v)
199{
200 //
201 // Send the new velocities for both axes.
202 //
203 fCosy->fMac2->SendSDO(0x3006, 1, (LWORD_t)v.Zd()); // SetRpmVelocity [re/min]
204 fCosy->fMac1->SendSDO(0x3006, 1, (LWORD_t)v.Az()); // SetRpmVelocity [re/min]
205
206 //
207 // Wait for the objects to be acknoledged.
208 //
209 fCosy->fMac2->WaitForSdo(0x3006, 1);
210 fCosy->fMac1->WaitForSdo(0x3006, 1);
211
212 //
213 // If the waiting for the objects wasn't interrupted return kTRUE
214 //
215 if (!Break())
216 return kTRUE;
217
218 //
219 // print a message if the interruption was due to a Can-node Error
220 //
221 if (fCosy->HasError())
222 lout << "Error while setting tracking velocity (SDO #3006)" << endl;
223
224 return kFALSE;
225}
226
227// --------------------------------------------------------------------
228//
229// Return pointing position of the telescope based on the
230// Shaftencoders with interpolation with motor encoders.
231//
232// GetPointingPos [re]
233//
234ZdAz MTracking::GetPointingPosRE(Bool_t pdo) const
235{
236 // Conversion factor from se to re
237 const XY re = fCosy->kGearTot/fCosy->kResSE; //[re/se]
238
239 // Get current shaftencoder position of the telescope
240 Double_t seposzd1 = ((fCosy->fZd1->GetPos()+8192)%16384)*re.X();
241 Double_t seposzd2 = ((fCosy->fZd2->GetPos()+8192)%16384)*re.X();
242 Double_t seposaz = fCosy->fAz->GetPos() *re.Y();
243
244 // distance between (To+dt) and To [re]
245 // position time difference < 5usec
246 // fRePos does the synchronization between the
247 // Shaft- and the rotary encoders
248 const ZdAz repos = pdo ? fCosy->GetRePosPdo() : fCosy->GetRePos();
249
250 // Calculate the part of one SE which the motors moved
251 // since the last SE has changed its value
252 const Double_t offzd1 = repos.Zd() - fCosy->fZd1->GetOffset();
253 const Double_t offzd2 = repos.Zd() - fCosy->fZd2->GetOffset();
254 const Double_t offaz = repos.Az() - fCosy->fAz->GetOffset();
255
256 // Correct for the direction in which the motor is moving
257 // (in which the shaftencoders should change its values)
258 if (offaz<0)
259 seposaz += re.Y();
260 if (offzd1<0)
261 seposzd1 += re.X();
262 if (offzd2<0)
263 seposzd2 += re.X();
264
265 // and interpolate the shaftencoder steps using the motor
266 // encoder positon (Be carefull the minus-sign is important)
267 seposzd1 += offzd1;
268 seposzd2 -= offzd2;
269 seposaz += offaz;
270
271 return ZdAz((seposzd1-seposzd2)/2, seposaz);
272}
273
274void MTracking::TrackPosition(const RaDec &dst) // ra, dec [rad]
275{
276 SlaStars sla(fCosy->fObservatory);
277
278 //
279 // Position to actual position
280 //
281 sla.Now();
282 ZdAz dest = sla.CalcZdAz(dst);
283
284 lout << sla.GetTime() << ": Track Position " << dst.Ra()*kRad2Deg/15 << "h, " << dst.Dec()*kRad2Deg <<"deg" << endl;
285
286 // If the star is culminating behind the zenith (South) we want to
287 // align the azimuth angle between -180 and 180deg. If the star is
288 // culminating before the zenith (north) we want the star to be
289 // aligned between -180 and 180deg (which is the default of CalcZdAz)
290 if (sla.GetPhi()>dst.Dec() && dest.Az()<0)
291 {
292 // align az from -180/180 to 0/360
293 lout << "Star culminating behind zenith: Adding 360deg to Azimuth " << dest.Az()*kRad2Deg << endl;
294 dest.Az(dest.Az() + TMath::TwoPi());
295 }
296
297 // Position the telescope to the current local position of the
298 // star. Do not reposition but start the tracking after the
299 // first positioning step
300 if (!SetPosition(dest, kTRUE))
301 {
302 lout << "Error: Cannot start tracking, positioning failed." << endl;
303 return;
304 }
305
306 //
307 // calculate offset from present se position
308 //
309 //const ZdAz sepos = fCosy->GetSePos()*fCosy->kGearTot/fCosy->kResSE; //[re]
310 if (!RequestRePos())
311 return;
312
313 // Estimate Offset before starting to track
314 ZdAz repos = fCosy->GetRePos();
315 fCosy->fZd1->SetOffset(repos.Zd());
316 fCosy->fZd2->SetOffset(repos.Zd());
317 fCosy->fAz->SetOffset(repos.Az());
318
319 fCosy->SetTrackingPosRE(GetPointingPosRE());
320
321 // Initialize Tracker (slalib or starguider)
322 fCosy->fRaDec = dst;
323
324 // StartThread
325 Start();
326
327 //
328 // Init accelerations and Rpm Mode
329 //
330 if (!InitTracking())
331 {
332 fCosy->StopMovement();
333 return;
334 }
335
336 // Get current nominal local position
337 sla.Now();
338 ZdAz pos = sla.CalcZdAz(fCosy->fRaDec);
339
340 // Some output
341 XY xy(Rad2Deg(dst.Ra())*24/360, Rad2Deg(dst.Dec()));
342 lout << sla.GetTime() << " - Start Tracking: Ra=" << xy.X() << "h Dec=";
343 lout << xy.Y() << "\xb0 @ Zd=" << pos.Zd()*kRad2Deg <<"deg Az=" << pos.Az()*kRad2Deg <<"deg" << endl;
344
345 //
346 // We want to reach the theoretical position exactly in about 0.5s
347 //
348 // *OLD*const float dt = 1; // 1 second
349 const float dt = 5;//3; // 2 second
350 while (!Break())
351 {
352 //
353 // Request Target position for Now+dt
354 //
355 sla.Now(dt);
356
357 //
358 // Request nominal position for this time in the future (To+dt)
359 //
360 const ZdAz pointing = sla.CalcZdAz(fCosy->fRaDec); // [rad]
361 ZdAz dest = fCosy->AlignTrackingPos(pointing); // fix ambiguity
362
363 //ZdAz vcalc = sla.GetApproxVel(fCosy->fRaDec);
364 //vcalc *= fCosy->kGearRatio2*4./60.; // [re/min]
365
366 float dtime = -1;
367 //if (kFALSE /*fUseStarguider*/)
368 // dtime = Starguider(sla.GetMjd(), dest);
369
370 ZdAz repos;
371 if (dtime<0)
372 {
373 dest = fCosy->fBending(dest); // [rad]
374 if (!fCosy->CheckRange(dest))
375 break;
376
377 // Destination position at t+dt in re-units
378 dest *= fCosy->kGearTot/TMath::TwoPi(); // [re]
379
380 // Request absolute position of rotary encoder from Macs
381 // Such that the RE position used in GetPointingPos is
382 // as up-to-date as possible.
383// DO I NEED THIS OR IS THE PDOPOS ENOUGH?
384 if (!RequestRePos())
385 break;
386
387 // *NEW* offset handling
388 // Get current position of the telescope and
389 // forward this position to MCosy
390 ZdAz sepos = GetPointingPosRE(); //[re]
391 fCosy->SetTrackingPosRE(sepos);
392
393 // distance between (To+dt) and To [re]
394 // position time difference < 5usec
395 // fRePos does the synchronization between the
396 // Shaft- and the rotary encoders
397 repos = fCosy->GetRePos();
398
399 // Now calculate the distance to move from now
400 // to a time in t+dt.
401 dest -= sepos;
402
403 dtime = dt;
404 }
405
406 //
407 // Velocity to go [re/min] to reach the right position at time t+dt
408 // correct for the duration of RaDec2AltAz
409 //
410 /* --- OLD --- */
411 ZdAz v = dest*60.0/dtime; //[re/min]
412 /* --- NEW --- seems to work worse! */
413 //const Double_t dtaz = sla.GetTime() - fCosy->fMac1->GetPosTime();
414 //const Double_t dtzd = sla.GetTime() - fCosy->fMac2->GetPosTime();
415 //
416 //ZdAz v = dest*60.0;
417 //v.Zd(v.Zd()/dtzd);
418 //v.Az(v.Az()/dtaz);
419 /* --- END --- */
420
421 //*fCosy->fOutRep << "> Dt: " << dtaz << " " << dtzd << endl;
422
423 if (LimitSpeed(&v, sla))
424 {
425 lout << "vt: " << v.Zd() << " " << v.Az() << "re/min" << endl;
426 lout << "Dest: " << dest.Zd() << " " << dest.Az() << endl;
427 }
428
429 //
430 // calculate real velocity of future [re/min]
431 // believing the Macs manual '/4' shouldn't be necessary, but it is.
432 //
433 ZdAz vt = v/4; //[re'/min]
434 //lout << " " << vt.Zd() << " " << vt.Az() << " ";
435 vt.Round();
436 //lout << " " << vt.Zd() << " " << vt.Az() << endl;
437
438 //
439 // check if the drive is fast enough to follow the star
440 //
441 if (vt.Zd()>.9*fCosy->fMac1->GetVelRes() || vt.Az()>.9*fCosy->fMac2->GetVelRes())
442 {
443 lout << "Error: Tracking speed faster than 90% of possible maximum velocity." << endl;
444 break;
445 }
446
447 //
448 // Set theoretical velocity (as early after calculation as possible)
449 // Maybe we should attenuate the changes
450 //
451 //*fCosy->fOutRep << "> SetVelocity1: " << vt.Zd() << " " << vt.Az() << endl;
452 if (!SetVelocity(vt))
453 break;
454 //*fCosy->fOutRep << "> SetVelocity2 " << endl;
455
456 //
457 // Now do 'unnecessary' things (timing)
458 //
459 fCosy->fVelocity = vt*4/fCosy->kGear; // [U_mot/min]
460 // *OLD* fVelocity = vt/kGearRatio2*4;
461
462 if (fOut)
463 {
464 fOut->Lock("MTracking::TrackPosition");
465 *fOut << "RE-REPORT " << MTime(-1) << " " << repos.Zd() << " " << repos.Az() <<" " << vt.Zd() << " " << vt.Az() << endl;
466 fOut->UnLock("MTracking::TrackPosition");
467 }
468
469 //
470 // Update speed as often as possible.
471 // make sure, that dt is around 10 times larger than the
472 // update time
473 //
474 // The loop should not be executed faster than the ramp of
475 // a change in the velocity can be followed.
476 // (This is important on fast machines >500MHz)
477 //
478 usleep(1000000); // 1s
479// *****FIXME**** cout << "." << flush;
480 }
481
482 sla.Now();
483
484 // StopThread
485 Stop();
486
487 fCosy->StopMovement();
488
489 lout << sla.GetTime() << " - Tracking stopped @ Zd=";
490 lout << fCosy->fZdAzSoll.Zd()*TMath::RadToDeg() <<"deg Az=";
491 lout << fCosy->fZdAzSoll.Az()*TMath::RadToDeg() <<"deg" << endl;
492}
493
494void *MTracking::Thread()
495{
496 if (fCosy->fZd1->IsZombieNode() && fCosy->fZd2->IsZombieNode())
497 return (void*)1;
498
499 if (fCosy->fAz->IsZombieNode())
500 return (void*)2;
501
502 if (!fCosy->fMac1 || !fCosy->fMac2)
503 return (void*)3;
504
505 lout << "- Tracking Thread started..." << endl;
506
507 //const XY re2se = fCosy->kGearTot/fCosy->kResSE; //[re/se]
508
509 SlaStars sla(fCosy->fObservatory);
510 sla.Now();
511
512 //ZdAz time;
513
514 ZdAz soll = sla.CalcZdAz(fCosy->fRaDec); // [rad]
515
516 //
517 // only update fTrackingError while tracking
518 //
519 bool phca1=false;
520 bool phca2=false;
521 bool phcaz=false;
522
523 //ZdAz wasse = fCosy->GetSePos();
524 //ZdAz oldse = fCosy->GetSePos();
525
526 while (!HasStopFlag())
527 {
528 // Make changes (eg wind) smoother - attenuation of control function
529 // This is the time constant which defines how fast
530 // you correct for external influences (like wind)
531 //const float weight = 1.; //0.3;
532
533 // Check for changes of the shaftencoder values
534 //*fCosy->fOutRep << "> ResetPosHasChanged" << endl;
535 fCosy->fZd1->ResetPosHasChanged();
536 fCosy->fZd2->ResetPosHasChanged();
537 fCosy->fAz->ResetPosHasChanged();
538 //*fCosy->fOutRep << "> Check for PosHasChanged" << endl;
539 do
540 {
541 phca1 = fCosy->fZd1->PosHasChanged();
542 phca2 = fCosy->fZd2->PosHasChanged();
543 phcaz = fCosy->fAz->PosHasChanged();
544 usleep(1);
545 } while (!phca1 && !phca2 && !phcaz && !HasStopFlag());
546
547 // Get time from last shaftencoder position change (position: ist)
548 // FIXME: Is this correct?
549 // time.Az(fCosy->fMac1->GetMjd());
550 // time.Zd(fCosy->fMac2->GetMjd());
551
552 //Double_t mjd1 = fCosy->fZd1->GetMjd();
553 //Double_t mjd2 = fCosy->fZd2->GetMjd();
554 //Double_t mjd0 = fCosy->fAz->GetMjd();
555
556 Double_t mjdaz = fCosy->fMac1->GetPdoMjd();//mjd0;
557 Double_t mjdzd = fCosy->fMac2->GetPdoMjd();//TMath::Max(mjd1, mjd2);
558
559 // get current position of shaftencoders (interpolated
560 // using motor encoders)
561 const ZdAz istse = GetPointingPosRE(kTRUE)/fCosy->kGearTot*TMath::TwoPi();
562 //const ZdAz istse = fCosy->GetSePosPdo();
563
564 // calculate offset for both axis (only one is needed)
565 // *NEW* offset handling
566 //.const ZdAz offset = istre; //(istse*re2se - istre)*weight + fRePos*(weight-1);
567 // if Shaftencoder changed position, calculate nominal position
568 if (phca1 || phca2)
569 {
570 ZdAz dummy = sla.CalcZdAz(fCosy->fRaDec, mjdzd);
571 dummy = fCosy->AlignTrackingPos(dummy);
572 fCosy->fZdAzSoll.Zd(dummy.Zd());
573 soll.Zd(fCosy->fBending(dummy).Zd()); // [rad]
574 }
575 if (phcaz)
576 {
577 ZdAz dummy = sla.CalcZdAz(fCosy->fRaDec, mjdaz);
578 dummy = fCosy->AlignTrackingPos(dummy);
579 fCosy->fZdAzSoll.Az(dummy.Az());
580 soll.Az(fCosy->fBending(dummy).Az()); // [rad]
581 }
582
583 //fCosy->fZdAzSoll = soll;
584
585 // Calculate the aligned tracking posotion from 'soll'-position
586 if (phca1 || phca2)
587 fCosy->fTrackingError.Zd(soll.Zd()-istse.Zd());
588 if (phcaz)
589 fCosy->fTrackingError.Az(soll.Az()-istse.Az());
590 }
591
592 lout << "- Tracking Thread done." << endl;
593
594 return 0;
595}
Note: See TracBrowser for help on using the repository browser.