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

Last change on this file since 7788 was 7313, checked in by tbretz, 19 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
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 // Check wether moving direction has changed
250 const bool bool1 = fCosy->fZd1->DirHasChanged();
251 const bool bool2 = fCosy->fZd2->DirHasChanged();
252
253 // If both directions have changed reset the flags
254 if (bool1 && bool2)
255 {
256 fCosy->fZd1->ResetDirHasChanged();
257 fCosy->fZd2->ResetDirHasChanged();
258 }
259
260 // Get shaftencoder positions
261 // Ignore the shaftencoder which has not yet changed its value
262 const Int_t pzd1 = fCosy->fZd1->GetPosDirCorrected();
263 const Int_t pzd2 = fCosy->fZd2->GetPosDirCorrected();
264 const Int_t paz = fCosy->fAz->GetPos();
265
266 // Get current shaftencoder position of the telescope
267 Double_t seposzd1 = ((pzd1+8192)%16384)*re.X();
268 Double_t seposzd2 = ((pzd2+8192)%16384)*re.X();
269 Double_t seposaz = paz *re.Y();
270
271 // distance between (To+dt) and To [re]
272 // position time difference < 5usec
273 // fRePos does the synchronization between the
274 // Shaft- and the rotary encoders
275 const ZdAz repos = pdo ? fCosy->GetRePosPdo() : fCosy->GetRePos();
276
277 // Get rotary encoder positions
278 // Get stored offset if one SE has not changed its direction yet
279 const Int_t offset1 = fCosy->fZd1->GetOffsetDirCorrected();
280 const Int_t offset2 = fCosy->fZd2->GetOffsetDirCorrected();
281
282 // Calculate the part of one SE which the motors moved
283 // since the last SE has changed its value
284 const Double_t offzd1 = repos.Zd() - offset1;
285 const Double_t offzd2 = repos.Zd() - offset2;
286 const Double_t offaz = repos.Az() - fCosy->fAz->GetOffset();
287
288 // Correct for the direction in which the motor is moving
289 // (in which the shaftencoders should change its values)
290 if (offaz<0)
291 seposaz += re.Y();
292 if (offzd1<0)
293 seposzd1 += re.X();
294 if (offzd2<0)
295 seposzd2 += re.X();
296
297 // If the correction exceeds one shaftencoder step stop interpolation
298 // of shaftencoder positions using rotary encoder values.
299 //ofstream fout("offsets.log", ios::app);
300 //fout << MTime(-1) << " " << offaz << " " << offzd1 << " " << offzd2 << endl;
301 /*
302 if (TMath::Abs(offaz)>re.Y())
303 offaz = TMath::Sign(re.Y(), offaz);
304 if (TMath::Abs(offzd1)>re.X())
305 offzd1 = TMath::Sign(re.X(), offzd1);
306 if (TMath::Abs(offzd2)>re.X())
307 offzd2 = TMath::Sign(re.X(), offzd2);
308 */
309
310 // and interpolate the shaftencoder steps using the motor
311 // encoder positon (Be carefull the minus-sign is important)
312 seposzd1 += offzd1;
313 seposzd2 -= offzd2;
314 seposaz += offaz;
315
316 return ZdAz((seposzd1-seposzd2)/2, seposaz);
317}
318
319void MTracking::TrackPosition(const RaDec &dst) // ra, dec [rad]
320{
321 SlaStars sla(fCosy->fObservatory);
322
323 //
324 // Position to actual position
325 //
326 sla.Now();
327 ZdAz dest = sla.CalcZdAz(dst);
328
329 lout << sla.GetTime() << ": Track Position " << dst.Ra()*kRad2Deg/15 << "h, " << dst.Dec()*kRad2Deg <<"deg" << endl;
330
331 // If the star is culminating behind the zenith (South) we want to
332 // align the azimuth angle between -180 and 180deg. If the star is
333 // culminating before the zenith (north) we want the star to be
334 // aligned between -180 and 180deg (which is the default of CalcZdAz)
335 if (sla.GetPhi()>dst.Dec() && dest.Az()<0)
336 {
337 // align az from -180/180 to 0/360
338 lout << "Star culminating behind zenith: Adding 360deg to Azimuth " << dest.Az()*kRad2Deg << endl;
339 dest.Az(dest.Az() + TMath::TwoPi());
340 }
341
342 // Position the telescope to the current local position of the
343 // star. Do not reposition but start the tracking after the
344 // first positioning step
345 if (!SetPosition(dest, kTRUE))
346 {
347 lout << "Error: Cannot start tracking, positioning failed." << endl;
348 return;
349 }
350
351 //
352 // calculate offset from present se position
353 //
354 //const ZdAz sepos = fCosy->GetSePos()*fCosy->kGearTot/fCosy->kResSE; //[re]
355 if (!RequestRePos())
356 return;
357
358 // Estimate Offset before starting to track
359 ZdAz repos = fCosy->GetRePos();
360 fCosy->fZd1->SetOffset(repos.Zd());
361 fCosy->fZd2->SetOffset(repos.Zd());
362 fCosy->fAz->SetOffset(repos.Az());
363
364 fCosy->SetTrackingPosRE(GetPointingPosRE());
365
366 // Initialize Tracker (slalib or starguider)
367 fCosy->fRaDec = dst;
368
369 // StartThread
370 Start();
371
372 //
373 // Init accelerations and Rpm Mode
374 //
375 if (!InitTracking())
376 {
377 fCosy->StopMovement();
378 return;
379 }
380
381 // Get current nominal local position
382 sla.Now();
383 ZdAz pos = sla.CalcZdAz(fCosy->fRaDec);
384
385 // Some output
386 XY xy(Rad2Deg(dst.Ra())*24/360, Rad2Deg(dst.Dec()));
387 lout << sla.GetTime() << " - Start Tracking: Ra=" << xy.X() << "h Dec=";
388 lout << xy.Y() << "\xb0 @ Zd=" << pos.Zd()*kRad2Deg <<"deg Az=" << pos.Az()*kRad2Deg <<"deg" << endl;
389
390 //
391 // We want to reach the theoretical position exactly in about 0.5s
392 //
393 // *OLD*const float dt = 1; // 1 second
394 const float dt = 5;//3; // 2 second
395 while (!Break())
396 {
397 //
398 // Request Target position for Now+dt
399 //
400 sla.Now(dt);
401
402 //
403 // Request nominal position for this time in the future (To+dt)
404 //
405 const ZdAz pointing = sla.CalcZdAz(fCosy->fRaDec); // [rad]
406 ZdAz dest = fCosy->AlignTrackingPos(pointing); // fix ambiguity
407
408 //ZdAz vcalc = sla.GetApproxVel(fCosy->fRaDec);
409 //vcalc *= fCosy->kGearRatio2*4./60.; // [re/min]
410
411 float dtime = -1;
412 //if (kFALSE /*fUseStarguider*/)
413 // dtime = Starguider(sla.GetMjd(), dest);
414
415 ZdAz repos;
416 if (dtime<0)
417 {
418 dest = fCosy->fBending(dest); // [rad]
419 if (!fCosy->CheckRange(dest))
420 break;
421
422 // Destination position at t+dt in re-units
423 dest *= fCosy->kGearTot/TMath::TwoPi(); // [re]
424
425 // Request absolute position of rotary encoder from Macs
426 // Such that the RE position used in GetPointingPos is
427 // as up-to-date as possible.
428// DO I NEED THIS OR IS THE PDOPOS ENOUGH?
429 if (!RequestRePos())
430 break;
431
432 // *NEW* offset handling
433 // Get current position of the telescope and
434 // forward this position to MCosy
435 ZdAz sepos = GetPointingPosRE(); //[re]
436 fCosy->SetTrackingPosRE(sepos);
437
438 // distance between (To+dt) and To [re]
439 // position time difference < 5usec
440 // fRePos does the synchronization between the
441 // Shaft- and the rotary encoders
442 repos = fCosy->GetRePos();
443
444 // Now calculate the distance to move from now
445 // to a time in t+dt.
446 dest -= sepos;
447
448 dtime = dt;
449 }
450
451 //
452 // Velocity to go [re/min] to reach the right position at time t+dt
453 // correct for the duration of RaDec2AltAz
454 //
455 /* --- OLD --- */
456 ZdAz v = dest*60.0/dtime; //[re/min]
457 /* --- NEW --- seems to work worse! */
458 //const Double_t dtaz = sla.GetTime() - fCosy->fMac1->GetPosTime();
459 //const Double_t dtzd = sla.GetTime() - fCosy->fMac2->GetPosTime();
460 //
461 //ZdAz v = dest*60.0;
462 //v.Zd(v.Zd()/dtzd);
463 //v.Az(v.Az()/dtaz);
464 /* --- END --- */
465
466 //*fCosy->fOutRep << "> Dt: " << dtaz << " " << dtzd << endl;
467
468 if (LimitSpeed(&v, sla))
469 {
470 lout << "vt: " << v.Zd() << " " << v.Az() << "re/min" << endl;
471 lout << "Dest: " << dest.Zd() << " " << dest.Az() << endl;
472 }
473
474 //
475 // calculate real velocity of future [re/min]
476 // believing the Macs manual '/4' shouldn't be necessary, but it is.
477 //
478 ZdAz vt = v/4; //[re'/min]
479 //lout << " " << vt.Zd() << " " << vt.Az() << " ";
480 vt.Round();
481 //lout << " " << vt.Zd() << " " << vt.Az() << endl;
482
483 //
484 // check if the drive is fast enough to follow the star
485 //
486 if (vt.Zd()>.9*fCosy->fMac1->GetVelRes() || vt.Az()>.9*fCosy->fMac2->GetVelRes())
487 {
488 lout << "Error: Tracking speed faster than 90% of possible maximum velocity." << endl;
489 break;
490 }
491
492 //
493 // Set theoretical velocity (as early after calculation as possible)
494 // Maybe we should attenuate the changes
495 //
496 //*fCosy->fOutRep << "> SetVelocity1: " << vt.Zd() << " " << vt.Az() << endl;
497 if (!SetVelocity(vt))
498 break;
499 //*fCosy->fOutRep << "> SetVelocity2 " << endl;
500
501 //
502 // Now do 'unnecessary' things (timing)
503 //
504 fCosy->fVelocity = vt*4/fCosy->kGear; // [U_mot/min]
505 // *OLD* fVelocity = vt/kGearRatio2*4;
506
507 if (fOut)
508 {
509 fOut->Lock("MTracking::TrackPosition");
510 *fOut << "RE-REPORT 00 " << MTime(-1) << " " << repos.Zd() << " " << repos.Az() <<" " << vt.Zd() << " " << vt.Az() << endl;
511 fOut->UnLock("MTracking::TrackPosition");
512 }
513
514 //
515 // Update speed as often as possible.
516 // make sure, that dt is around 10 times larger than the
517 // update time
518 //
519 // The loop should not be executed faster than the ramp of
520 // a change in the velocity can be followed.
521 // (This is important on fast machines >500MHz)
522 //
523 usleep(1000000); // 1s
524// *****FIXME**** cout << "." << flush;
525 }
526
527 sla.Now();
528
529 // StopThread
530 Stop();
531
532 fCosy->StopMovement();
533
534 lout << sla.GetTime() << " - Tracking stopped @ Zd=";
535 lout << fCosy->fZdAzSoll.Zd()*TMath::RadToDeg() <<"deg Az=";
536 lout << fCosy->fZdAzSoll.Az()*TMath::RadToDeg() <<"deg" << endl;
537}
538
539void *MTracking::Thread()
540{
541 if (fCosy->fZd1->IsZombieNode() && fCosy->fZd2->IsZombieNode())
542 return (void*)1;
543
544 if (fCosy->fAz->IsZombieNode())
545 return (void*)2;
546
547 if (!fCosy->fMac1 || !fCosy->fMac2)
548 return (void*)3;
549
550 lout << "- 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 (!HasStopFlag())
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 usleep(1);
590 } while (!phca1 && !phca2 && !phcaz && !HasStopFlag());
591
592 // Get time from last shaftencoder position change (position: ist)
593 // FIXME: Is this correct?
594 // time.Az(fCosy->fMac1->GetMjd());
595 // time.Zd(fCosy->fMac2->GetMjd());
596
597 //Double_t mjd1 = fCosy->fZd1->GetMjd();
598 //Double_t mjd2 = fCosy->fZd2->GetMjd();
599 //Double_t mjd0 = fCosy->fAz->GetMjd();
600
601 Double_t mjdaz = fCosy->fMac1->GetPdoMjd();//mjd0;
602 Double_t mjdzd = fCosy->fMac2->GetPdoMjd();//TMath::Max(mjd1, mjd2);
603
604 // get current position of shaftencoders (interpolated
605 // using motor encoders)
606 const ZdAz istse = GetPointingPosRE(kTRUE)/fCosy->kGearTot*TMath::TwoPi();
607 //const ZdAz istse = fCosy->GetSePosPdo();
608
609 // calculate offset for both axis (only one is needed)
610 // *NEW* offset handling
611 //.const ZdAz offset = istre; //(istse*re2se - istre)*weight + fRePos*(weight-1);
612 // if Shaftencoder changed position, calculate nominal position
613 if (phca1 || phca2)
614 {
615 ZdAz dummy = sla.CalcZdAz(fCosy->fRaDec, mjdzd);
616 dummy = fCosy->AlignTrackingPos(dummy);
617 fCosy->fZdAzSoll.Zd(dummy.Zd());
618 soll.Zd(fCosy->fBending(dummy).Zd()); // [rad]
619 }
620 if (phcaz)
621 {
622 ZdAz dummy = sla.CalcZdAz(fCosy->fRaDec, mjdaz);
623 dummy = fCosy->AlignTrackingPos(dummy);
624 fCosy->fZdAzSoll.Az(dummy.Az());
625 soll.Az(fCosy->fBending(dummy).Az()); // [rad]
626 }
627
628 //fCosy->fZdAzSoll = soll;
629
630 // Calculate the aligned tracking posotion from 'soll'-position
631 if (phca1 || phca2)
632 fCosy->fTrackingError.Zd(soll.Zd()-istse.Zd());
633 if (phcaz)
634 fCosy->fTrackingError.Az(soll.Az()-istse.Az());
635 }
636
637 lout << "- Tracking Thread done. (" << MTime(-1) << ")" << endl;
638 return 0;
639}
Note: See TracBrowser for help on using the repository browser.