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

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