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

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