FACT++  1.0
template<class T , class S >
int StateMachineDrive< T, S >::Execute ( )
inlineprivatevirtual

Implements StateMachineAsio< T >.

Definition at line 2545 of file drivectrl.cc.

References Encoder::Abs(), Local::az, data, RaDec::dec, Error(), Time::GetNextSunRise(), Drive::State::kArmed, MessageImp::kError, Drive::State::kInitialized, BIAS::State::kLocked, Drive::State::kMoving, Drive::State::kOnTrack, Drive::State::kParking, Drive::State::kPositioningFailed, Drive::State::kStopping, Drive::State::kTracking, Time::Mjd(), PointingData::pointing, RaDec::ra, PointingSetup::start, t, time, StateMachineDrive< T, S >::TrackingLoop(), and Local::zd.

2546  {
2547  const Time now;
2548  if (now>fSunRise && T::GetCurrentState()!=State::kParking)
2549  {
2550  fSunRise = now.GetNextSunRise();
2551 
2552  ostringstream msg;
2553  msg << "Next sun-rise will be at " << fSunRise;
2554  T::Info(msg);
2555 
2556  if (T::GetCurrentState()>State::kArmed && T::GetCurrentState()!=StateMachineImp::kError)
2557  return Park();
2558  }
2559 
2560  if (T::GetCurrentState()==State::kLocked)
2561  return State::kLocked;
2562 
2563  // FIXME: Send STOP if IsPositioning or RpmActive but no
2564  // Moving or Tracking state
2565 
2566  const int rc = CheckState();
2567  if (rc>0)
2568  return rc;
2569 
2570  // Once every second
2571  static time_t lastTime = 0;
2572  const time_t tm = time(NULL);
2573  if (lastTime!=tm && fDrive.IsInitialized())
2574  {
2575  lastTime=tm;
2576 
2578 
2579  if (T::GetCurrentState()==State::kTracking || T::GetCurrentState()==State::kOnTrack)
2580  return UpdateTrackingPosition();
2581  }
2582 
2583  if (T::GetCurrentState()==State::kStopping && !fDrive.IsMoving())
2584  return State::kArmed;
2585 
2586  if ((T::GetCurrentState()==State::kMoving ||
2587  T::GetCurrentState()==State::kParking) && !fDrive.IsMoving())
2588  {
2589  if (fIsTracking && fStep==1)
2590  {
2591  // Init tracking
2592  fDrive.SetAcceleration(fAccTracking);
2593  fDrive.SetRpmMode(true);
2594 
2595  fDevCount = 0;
2596  fTrackingCounter = 0;
2597 
2598  fTrackingLoop.expires_from_now(boost::posix_time::milliseconds(1));
2599  fTrackingLoop.async_wait(boost::bind(&StateMachineDrive::TrackingLoop,
2600  this, ba::placeholders::error));
2601 
2602  fPointingSetup.start = Time().Mjd();
2603 
2605 
2606  ostringstream out;
2607  out << "Start tracking at Ra=" << data.pointing.ra*12/M_PI << "h Dec=" << data.pointing.dec*180/M_PI << "deg";
2608  T::Info(out);
2609 
2610  return State::kTracking;
2611  }
2612 
2613  // Get feedback 2
2614  const Encoder dest = fMovementTarget*(1./360); // [rev]
2615  const Encoder sepos = fDrive.GetSePos(); // [rev]
2616 
2617  // Calculate residual to move deviation
2618  const Encoder dist = dest - sepos; // [rev]
2619 
2620  // Check which axis should still be moved
2621  Encoder cd = dist; // [rev]
2622  cd *= 1./fMaxPointingResidual; // Scale to units of the maximum residual
2623  cd = cd.Abs();
2624 
2625  // Check if there is a control deviation on the axis
2626  const bool cdzd = cd.zd>1;
2627  const bool cdaz = cd.az>1;
2628 
2629  if (!fIsTracking)
2630  {
2631  // check if we reached the correct position already
2632  if (!cdzd && !cdaz)
2633  {
2634  T::Info("Target position reached in "+to_string(fStep)+" steps.");
2635  return T::GetCurrentState()==State::kParking ? State::kLocked : State::kArmed;
2636  }
2637 
2638  if (fStep==10)
2639  {
2640  T::Error("Target position not reached in "+to_string(fStep)+" steps.");
2642  }
2643  }
2644 
2645  const Encoder t = dist.Abs()/fDrive.GetVelUnit();
2646 
2647  const Velocity vel =
2648  t.zd > t.az ?
2649  Velocity(1, t.zd==0?0:t.az/t.zd) :
2650  Velocity(t.az==0?0:t.zd/t.az, 1);
2651 
2652  if (fDrive.GetVerbosity())
2653  {
2654  T::Out() << "Moving step " << fStep << endl;
2655  T::Out() << "Encoder [deg] " << sepos.zd*360 << " " << sepos.az*360 << endl;
2656  T::Out() << "Destination [deg] " << dest.zd *360 << " " << dest.az *360 << endl;
2657  T::Out() << "Residual [deg] " << dist.zd *360 << " " << dist.az *360 << endl;
2658  T::Out() << "Residual/max [1] " << cd.zd << " " << cd.az << endl;
2659  T::Out() << "Rel. time [1] " << t.zd << " " << t.az << endl;
2660  T::Out() << "Rel. velocity [1] " << vel.zd << " " << vel.az << endl;
2661  }
2662 
2663  fDrive.SetPointingVelocity(vel, fPointingVelocity);
2664  fDrive.StartAbsolutePositioning(dest, cdzd, cdaz);
2665 
2666  ostringstream out;
2667  if (fStep==0)
2668  out << "Moving to encoder Zd=" << dest.zd*360 << "deg Az=" << dest.az*360 << "deg";
2669  else
2670  out << "Moving residual of dZd=" << dist.zd*360*60 << "' dAz=" << dist.az*360*60 << "'";
2671  T::Info(out);
2672 
2673  fStep++;
2674  }
2675 
2676  return T::GetCurrentState()>=State::kInitialized ?
2677  T::GetCurrentState() : State::kInitialized;
2678  }
double fMaxPointingResidual
Definition: drivectrl.cc:1883
void UpdatePointingPosition()
Definition: drivectrl.cc:2400
uint64_t fTrackingCounter
Definition: drivectrl.cc:1594
ba::deadline_timer fTrackingLoop
Definition: drivectrl.cc:1566
PointingSetup fPointingSetup
Definition: drivectrl.cc:1579
Adds some functionality to boost::posix_time::ptime for our needs.
Definition: Time.h:30
Time GetNextSunRise(double horizon) const
Definition: Time.cc:346
RaDec pointing
Definition: drivectrl.cc:165
uint64_t fDevCount
Definition: drivectrl.cc:1592
double zd
Definition: drivectrl.cc:51
Encoder fMovementTarget
Definition: drivectrl.cc:1580
double fPointingVelocity
Definition: drivectrl.cc:1884
PointingData CalcPointingPos(double mjd)
Definition: drivectrl.cc:1815
double start
Definition: drivectrl.cc:153
double az
Definition: drivectrl.cc:52
double dec
Definition: drivectrl.cc:37
void Mjd(double mjd)
Definition: Time.cc:145
int UpdateTrackingPosition()
Definition: drivectrl.cc:2340
Warning because the service this data corrsponds to might have been last updated longer ago than Local time
Definition: smartfact.txt:92
void TrackingLoop(const boost::system::error_code &error=boost::system::error_code())
Definition: drivectrl.cc:2415
float data[4 *1440]
Error, something unexpected happened, but can still be handled by the program.
Definition: MessageImp.h:19
Acceleration fAccTracking
Definition: drivectrl.cc:1881
TT t
Definition: test_client.c:26
Error()
Definition: HeadersFTM.h:197
double ra
Definition: drivectrl.cc:36
Encoder Abs() const
Definition: drivectrl.cc:72

+ Here is the call graph for this function: