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

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