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

Last change on this file since 8618 was 8377, checked in by tbretz, 18 years ago
*** empty log message ***
File size: 19.6 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 lout << "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 lout << "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 lout << "Warning: Azimuth speed limit (" << maxtrack*100 << "%) exceeded (" << fabs(vt->Az()) << " > " << maxtrack*vraz << ")... limited." << endl;
226 lout << "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 lout << "Warning: Altitude speed limit (" << maxtrack*100 << "%) exceeded (" << fabs(vt->Zd()) <<" > " << maxtrack*vrzd << ")... limited." << endl;
233 lout << "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 lout << 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 lout << "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 lout << "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 // StartThread
372 Start();
373
374 //
375 // Init accelerations and Rpm Mode
376 //
377 if (!InitTracking())
378 {
379 fCosy->StopMovement();
380 return;
381 }
382
383 // Get current nominal local position
384 sla.Now();
385 ZdAz pos = sla.CalcZdAz(fCosy->fRaDec);
386
387 // Some output
388 XY xy(Rad2Deg(dst.Ra())*24/360, Rad2Deg(dst.Dec()));
389 lout << sla.GetTime() << " - Start Tracking: Ra=" << xy.X() << "h Dec=";
390 lout << xy.Y() << "\xb0 @ Zd=" << pos.Zd()*kRad2Deg <<"deg Az=" << pos.Az()*kRad2Deg <<"deg" << endl;
391
392 //
393 // We want to reach the theoretical position exactly in about 0.5s
394 //
395 // *OLD*const float dt = 1; // 1 second
396 const float dt = 5;//3; // 2 second
397 while (!Break())
398 {
399 //
400 // Request Target position for Now+dt
401 //
402 sla.Now(dt);
403
404 //
405 // Request nominal position for this time in the future (To+dt)
406 //
407 const ZdAz pointing = sla.CalcZdAz(fCosy->fRaDec); // [rad]
408 ZdAz dest = fCosy->AlignTrackingPos(pointing); // fix ambiguity
409
410 //ZdAz vcalc = sla.GetApproxVel(fCosy->fRaDec);
411 //vcalc *= fCosy->kGearRatio2*4./60.; // [re/min]
412
413 float dtime = -1;
414 //if (kFALSE /*fUseStarguider*/)
415 // dtime = Starguider(sla.GetMjd(), dest);
416
417 ZdAz repos;
418 if (dtime<0)
419 {
420 dest = fCosy->fBending(dest); // [rad]
421 if (!fCosy->CheckRange(dest))
422 break;
423
424 // Destination position at t+dt in re-units
425 dest *= fCosy->kGearTot/TMath::TwoPi(); // [re]
426
427 // Request absolute position of rotary encoder from Macs
428 // Such that the RE position used in GetPointingPos is
429 // as up-to-date as possible.
430// DO I NEED THIS OR IS THE PDOPOS ENOUGH?
431 if (!RequestRePos())
432 break;
433
434 // *NEW* offset handling
435 // Get current position of the telescope and
436 // forward this position to MCosy
437 ZdAz sepos = GetPointingPosRE(); //[re]
438 fCosy->SetTrackingPosRE(sepos);
439
440 // distance between (To+dt) and To [re]
441 // position time difference < 5usec
442 // fRePos does the synchronization between the
443 // Shaft- and the rotary encoders
444 repos = fCosy->GetRePos();
445
446 // Now calculate the distance to move from now
447 // to a time in t+dt.
448 dest -= sepos;
449
450 dtime = dt;
451 }
452
453 //
454 // Velocity to go [re/min] to reach the right position at time t+dt
455 // correct for the duration of RaDec2AltAz
456 //
457 /* --- OLD --- */
458 ZdAz v = dest*60.0/dtime; //[re/min]
459 /* --- NEW --- seems to work worse! */
460 //const Double_t dtaz = sla.GetTime() - fCosy->fMac1->GetPosTime();
461 //const Double_t dtzd = sla.GetTime() - fCosy->fMac2->GetPosTime();
462 //
463 //ZdAz v = dest*60.0;
464 //v.Zd(v.Zd()/dtzd);
465 //v.Az(v.Az()/dtaz);
466 /* --- END --- */
467
468 //*fCosy->fOutRep << "> Dt: " << dtaz << " " << dtzd << endl;
469
470 if (LimitSpeed(&v, sla))
471 {
472 lout << "vt: " << v.Zd() << " " << v.Az() << "re/min" << endl;
473 lout << "Dest: " << dest.Zd() << " " << dest.Az() << endl;
474 }
475
476 //
477 // calculate real velocity of future [re/min]
478 // believing the Macs manual '/4' shouldn't be necessary, but it is.
479 //
480 ZdAz vt = v/4; //[re'/min]
481 //lout << " " << vt.Zd() << " " << vt.Az() << " ";
482 vt.Round();
483 //lout << " " << vt.Zd() << " " << vt.Az() << endl;
484
485 //
486 // check if the drive is fast enough to follow the star
487 //
488 if (vt.Zd()>.9*fCosy->fMac1->GetVelRes() || vt.Az()>.9*fCosy->fMac2->GetVelRes())
489 {
490 lout << "Error: Tracking speed faster than 90% of possible maximum velocity." << endl;
491 break;
492 }
493
494 //
495 // Set theoretical velocity (as early after calculation as possible)
496 // Maybe we should attenuate the changes
497 //
498 //*fCosy->fOutRep << "> SetVelocity1: " << vt.Zd() << " " << vt.Az() << endl;
499 if (!SetVelocity(vt))
500 break;
501 //*fCosy->fOutRep << "> SetVelocity2 " << endl;
502
503 //
504 // Now do 'unnecessary' things (timing)
505 //
506 fCosy->fVelocity = vt*4/fCosy->kGear; // [U_mot/min]
507 // *OLD* fVelocity = vt/kGearRatio2*4;
508
509 if (fOut)
510 {
511 fOut->Lock("MTracking::TrackPosition");
512 *fOut << "RE-REPORT 00 " << MTime(-1) << " " << repos.Zd() << " " << repos.Az() <<" " << vt.Zd() << " " << vt.Az() << endl;
513 fOut->UnLock("MTracking::TrackPosition");
514 }
515
516 //
517 // Update speed as often as possible.
518 // make sure, that dt is around 10 times larger than the
519 // update time
520 //
521 // The loop should not be executed faster than the ramp of
522 // a change in the velocity can be followed.
523 // (This is important on fast machines >500MHz)
524 //
525 usleep(1000000); // 1s
526// *****FIXME**** cout << "." << flush;
527 }
528
529 sla.Now();
530
531 // StopThread
532 Stop();
533
534 fCosy->StopMovement();
535
536 lout << sla.GetTime() << " - Tracking stopped @ Zd=";
537 lout << fCosy->fZdAzSoll.Zd()*TMath::RadToDeg() <<"deg Az=";
538 lout << fCosy->fZdAzSoll.Az()*TMath::RadToDeg() <<"deg" << endl;
539}
540
541void *MTracking::Thread()
542{
543 if (fCosy->fZd1->IsZombieNode() && fCosy->fZd2->IsZombieNode())
544 return (void*)1;
545
546 if (fCosy->fAz->IsZombieNode())
547 return (void*)2;
548
549 if (!fCosy->fMac1 || !fCosy->fMac2)
550 return (void*)3;
551
552 lout << "- Tracking Thread started (" << MTime(-1) << ")" << endl;
553
554 //const XY re2se = fCosy->kGearTot/fCosy->kResSE; //[re/se]
555
556 SlaStars sla(fCosy->fObservatory);
557 sla.Now();
558
559 //ZdAz time;
560
561 ZdAz soll = sla.CalcZdAz(fCosy->fRaDec); // [rad]
562
563 //
564 // only update fTrackingError while tracking
565 //
566 bool phca1=false;
567 bool phca2=false;
568 bool phcaz=false;
569
570 //ZdAz wasse = fCosy->GetSePos();
571 //ZdAz oldse = fCosy->GetSePos();
572
573 while (!HasStopFlag())
574 {
575 // Make changes (eg wind) smoother - attenuation of control function
576 // This is the time constant which defines how fast
577 // you correct for external influences (like wind)
578 //const float weight = 1.; //0.3;
579
580 // Check for changes of the shaftencoder values
581 //*fCosy->fOutRep << "> ResetPosHasChanged" << endl;
582 fCosy->fZd1->ResetPosHasChanged();
583 fCosy->fZd2->ResetPosHasChanged();
584 fCosy->fAz->ResetPosHasChanged();
585 //*fCosy->fOutRep << "> Check for PosHasChanged" << endl;
586 do
587 {
588 phca1 = fCosy->fZd1->PosHasChanged();
589 phca2 = fCosy->fZd2->PosHasChanged();
590 phcaz = fCosy->fAz->PosHasChanged();
591 usleep(1);
592 } while (!phca1 && !phca2 && !phcaz && !HasStopFlag());
593
594 // Get time from last shaftencoder position change (position: ist)
595 // FIXME: Is this correct?
596 // time.Az(fCosy->fMac1->GetMjd());
597 // time.Zd(fCosy->fMac2->GetMjd());
598
599 //Double_t mjd1 = fCosy->fZd1->GetMjd();
600 //Double_t mjd2 = fCosy->fZd2->GetMjd();
601 //Double_t mjd0 = fCosy->fAz->GetMjd();
602
603 Double_t mjdaz = fCosy->fMac1->GetPdoMjd();//mjd0;
604 Double_t mjdzd = fCosy->fMac2->GetPdoMjd();//TMath::Max(mjd1, mjd2);
605
606 // get current position of shaftencoders (interpolated
607 // using motor encoders)
608 const ZdAz istse = GetPointingPosRE(kTRUE)/fCosy->kGearTot*TMath::TwoPi();
609 //const ZdAz istse = fCosy->GetSePosPdo();
610
611 // calculate offset for both axis (only one is needed)
612 // *NEW* offset handling
613 //.const ZdAz offset = istre; //(istse*re2se - istre)*weight + fRePos*(weight-1);
614 // if Shaftencoder changed position, calculate nominal position
615 if (phca1 || phca2)
616 {
617 ZdAz dummy = sla.CalcZdAz(fCosy->fRaDec, mjdzd);
618 dummy = fCosy->AlignTrackingPos(dummy);
619 fCosy->fZdAzSoll.Zd(dummy.Zd());
620 soll.Zd(fCosy->fBending(dummy).Zd()); // [rad]
621 }
622 if (phcaz)
623 {
624 ZdAz dummy = sla.CalcZdAz(fCosy->fRaDec, mjdaz);
625 dummy = fCosy->AlignTrackingPos(dummy);
626 fCosy->fZdAzSoll.Az(dummy.Az());
627 soll.Az(fCosy->fBending(dummy).Az()); // [rad]
628 }
629
630 //fCosy->fZdAzSoll = soll;
631
632 // Calculate the aligned tracking posotion from 'soll'-position
633 if (phca1 || phca2)
634 fCosy->fTrackingError.Zd(soll.Zd()-istse.Zd());
635 if (phcaz)
636 fCosy->fTrackingError.Az(soll.Az()-istse.Az());
637 }
638
639 lout << "- Tracking Thread done. (" << MTime(-1) << ")" << endl;
640 return 0;
641}
Note: See TracBrowser for help on using the repository browser.