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

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