RSeries astromech firmware
DomePosition.h
Go to the documentation of this file.
1 #ifndef DomePosition_h
2 #define DomePosition_h
3 
4 #include "ReelTwo.h"
6 
8 {
9 public:
10  enum Mode
11  {
12  kOff = 1,
16  };
17 
19  fProvider(provider)
20  {
21  }
22 
23  inline bool ready()
24  {
25  return fProvider.ready();
26  }
27 
29  {
30  if (!ready())
31  return kOff;
32  return fDomeMode;
33  }
34 
36  {
37  return fDomeDefaultMode;
38  }
39 
40  void setDomeMode(Mode mode)
41  {
42  fDomeMode = mode;
43  fLastChangeMS = millis();
44  }
45 
47  {
48  fDomeDefaultMode = mode;
49  setDomeMode(mode);
50  }
51 
52  bool getDomeFlip()
53  {
54  return fDomeFlip;
55  }
56 
57  float getDomeSpeed()
58  {
59  switch (getDomeMode())
60  {
61  case kHome:
62  return getDomeSpeedHome();
63  case kRandom:
64  return getDomeAutoSpeed();
65  case kTarget:
66  return getDomeSpeedTarget();
67  case kOff:
68  default:
69  return getDomeMinSpeed();
70  }
71  }
72 
74  {
75  return float(fDomeSpeedHome) / 100.0;
76  }
77 
79  {
80  return float(fDomeSpeedTarget) / 100.0;
81  }
82 
84  {
85  return float(fDomeSpeedMin) / 100.0;
86  }
87 
89  {
90  return float(fDomeSpeedAuto) / 100.0;
91  }
92 
93  unsigned getDomeFudge()
94  {
95  return fDomeFudge;
96  }
97 
98  unsigned getDomeAutoLeft()
99  {
100  return fDomeAutoLeft;
101  }
102 
103  unsigned getDomeAutoRight()
104  {
105  return fDomeAutoRight;
106  }
107 
109  {
110  return fDomeAutoMinDelay;
111  }
112 
114  {
115  return fDomeAutoMaxDelay;
116  }
117 
119  {
120  return fDomeHomeMinDelay;
121  }
122 
124  {
125  return fDomeHomeMaxDelay;
126  }
127 
129  {
130  return fDomeTargetMinDelay;
131  }
132 
134  {
135  return fDomeTargetMaxDelay;
136  }
137 
138  unsigned getDomeMinDelay()
139  {
140  switch (getDomeMode())
141  {
142  case kHome:
143  return getDomeHomeMinDelay();
144  case kRandom:
145  return getDomeAutoMinDelay();
146  case kTarget:
147  return getDomeTargetMinDelay();
148  case kOff:
149  default:
150  return 0;
151  }
152  }
153 
154  unsigned getDomeMaxDelay()
155  {
156  switch (getDomeMode())
157  {
158  case kHome:
159  return getDomeHomeMaxDelay();
160  case kRandom:
161  return getDomeAutoMaxDelay();
162  case kTarget:
163  return getDomeTargetMaxDelay();
164  case kOff:
165  default:
166  return 0;
167  }
168  }
169 
170  unsigned getDomeHome()
171  {
172  return fDomeHome;
173  }
174 
176  {
177  return fDomeTargetPos;
178  }
179 
181  {
182  return fDomeRelativeTargetPos;
183  }
184 
185  int shortestDistance(int origin, int target)
186  {
187  int result = 0.0;
188  int diff = fmod(fmod(abs(origin - target), 360), 360);
189 
190  if (diff > 180)
191  {
192  //There is a shorter path in opposite direction
193  result = (360 - diff);
194  if (target > origin)
195  result *= -1;
196  }
197  else
198  {
199  result = diff;
200  if (origin > target)
201  result *= -1;
202  }
203  return result;
204  }
205 
206  unsigned getDomePosition()
207  {
208  unsigned angle = fProvider.getAngle();
209  if (angle != fLastAngle)
210  {
211  if (fLastAngle < angle)
212  fRelativeDegrees += shortestDistance(fLastAngle, angle);
213  else
214  fRelativeDegrees -= shortestDistance(angle, fLastAngle);
215  fLastChangeMS = millis();
216  fLastAngle = angle;
217  }
218  return angle;
219  }
220 
222  {
223  return fRelativeDegrees;
224  }
225 
227  {
229  fDomeRelativeTargetPos = 0;
230  fRelativeDegrees = 0;
231  }
232 
234  {
235  fLastChangeMS = millis();
236  }
237 
238  void setTimeout(uint8_t timeout)
239  {
240  fTimeout = timeout;
241  }
242 
243  bool isTimeout()
244  {
245  return (ready() && fLastAngle != ~0u) ? uint32_t(fTimeout)*1000 < millis() - fLastChangeMS : true;
246  }
247 
249  {
250  return normalize(long(getDomePosition()) - long(getDomeHome()));
251  }
252 
253  long normalize(long degrees)
254  {
255  degrees = (long)fmod(degrees, 360);
256  if (degrees < 0)
257  degrees += 360;
258  return degrees;
259  }
260 
261  bool isAtPosition(long degrees)
262  {
263  long fudge = getDomeFudge();
264  degrees = normalize(degrees);
265  return withinArc(degrees - fudge, degrees + fudge, getDomePosition());
266 
267  }
268 
269  void setDomeHomePosition(long degrees)
270  {
271  degrees = (long)fmod(degrees, 360);
272  if (degrees < 0)
273  degrees += 360;
274  fDomeHome = normalize(degrees);
275  }
276 
277  void setDomeTargetPosition(long degrees)
278  {
279  degrees = (long)fmod(degrees, 360);
280  if (degrees < 0)
281  degrees += 360;
282  fDomeTargetPos = normalize(degrees);
283  fDomeRelativeTargetPos = 0;
284  }
285 
286  void setDomeRelativeTargetPosition(long degrees)
287  {
288  // Save the starting position in the absolute target position
289  fDomeTargetPos = getDomePosition();
290  fDomeRelativeTargetPos = degrees;
291  fRelativeDegrees = 0;
292  }
293 
295  {
296  setDomeTargetPosition(degrees + getDomeHome());
297  }
298 
300  {
301  degrees = (long)fmod(degrees + getDomeHome(), 360);
302  if (degrees < 0)
303  degrees += 360;
304  fDomeHome = normalize(degrees);
305  }
306 
307  inline void setDomeHomeSpeed(uint8_t speed)
308  {
309  fDomeSpeedHome = speed;
310  }
311 
312  inline void setDomeTargetSpeed(uint8_t speed)
313  {
314  fDomeSpeedTarget = speed;
315  }
316 
317  inline void setDomeMinSpeed(uint8_t speed)
318  {
319  fDomeSpeedMin = speed;
320  }
321 
322  inline void setDomeAutoSpeed(uint8_t speed)
323  {
324  fDomeSpeedAuto = speed;
325  }
326 
327  inline void setDomeAutoMinDelay(uint8_t sec)
328  {
329  fDomeAutoMinDelay = sec;
330  }
331 
332  inline void setDomeAutoMaxDelay(uint8_t sec)
333  {
334  fDomeAutoMaxDelay = sec;
335  }
336 
337  inline void setDomeHomeMinDelay(uint8_t sec)
338  {
339  fDomeHomeMinDelay = sec;
340  }
341 
342  inline void setDomeHomeMaxDelay(uint8_t sec)
343  {
344  fDomeHomeMaxDelay = sec;
345  }
346 
347  inline void setDomeTargetMinDelay(uint8_t sec)
348  {
349  fDomeTargetMinDelay = sec;
350  }
351 
352  inline void setDomeTargetMaxDelay(uint8_t sec)
353  {
354  fDomeTargetMaxDelay = sec;
355  }
356 
357  inline void setDomeFudgeFactor(uint8_t fudge)
358  {
359  fDomeFudge = fudge;
360  }
361 
362  inline void setDomeAutoLeftDegrees(uint8_t degrees)
363  {
364  fDomeAutoLeft = degrees;
365  }
366 
367  inline void setDomeAutoRightDegrees(uint8_t degrees)
368  {
369  fDomeAutoRight = degrees;
370  }
371 
372  inline void setTargetReached(void (*reached)())
373  {
374  fTargetReached = reached;
375  }
376 
377  inline void setHomeTargetReached(void (*reached)())
378  {
379  fHomeTargetReached = reached;
380  }
381 
382  inline void setAutoTargetReached(void (*reached)())
383  {
384  fAutoTargetReached = reached;
385  }
386 
388  {
389  if (fTargetReached != nullptr)
390  {
391  fTargetReached();
392  fTargetReached = nullptr;
393  }
394  }
395 
397  {
398  if (fHomeTargetReached != nullptr)
399  fHomeTargetReached();
400  }
401 
403  {
404  if (fAutoTargetReached != nullptr)
405  fAutoTargetReached();
406  }
407 
408 private:
409  DomePositionProvider &fProvider;
410  Mode fDomeMode = kOff;
411  Mode fDomeDefaultMode = kOff;
412  bool fDomeFlip = false;
413  uint16_t fDomeHome = 0;
414  uint16_t fDomeTargetPos = fDomeHome;
415  long fDomeRelativeTargetPos = 0;
416  uint8_t fDomeAutoMinDelay = 6;
417  uint8_t fDomeAutoMaxDelay = 8;
418  uint8_t fDomeHomeMinDelay = 6;
419  uint8_t fDomeHomeMaxDelay = 8;
420  uint8_t fDomeTargetMinDelay = 6;
421  uint8_t fDomeTargetMaxDelay = 8;
422  uint8_t fDomeAutoRight = 80;
423  uint8_t fDomeAutoLeft = 80;
424  uint8_t fDomeFudge = 5;
425  uint8_t fDomeSpeedHome = 40;
426  uint8_t fDomeSpeedTarget = 40;
427  uint8_t fDomeSpeedMin = 15;
428  uint8_t fDomeSpeedAuto = 30;
429  uint8_t fTimeout = 5;
430  unsigned fLastAngle = ~0;
431  uint32_t fLastChangeMS = 0;
432  unsigned fRelativeDegrees = 0;
433  void (*fTargetReached)() = nullptr;
434  void (*fHomeTargetReached)() = nullptr;
435  void (*fAutoTargetReached)() = nullptr;
436 
437  static bool withinArc(double p1, double p2, double p3)
438  {
439  return fmod(p2 - p1 + 2*360, 360) >= fmod(p3 - p1 + 2*360, 360);
440  }
441 };
442 
443 #endif
DomePosition::setTargetReached
void setTargetReached(void(*reached)())
Definition: DomePosition.h:372
DomePosition::getDomeMaxDelay
unsigned getDomeMaxDelay()
Definition: DomePosition.h:154
DomePosition::reachedHomeTarget
void reachedHomeTarget()
Definition: DomePosition.h:396
DomePosition::Mode
Mode
Definition: DomePosition.h:10
DomePosition::setHomeTargetReached
void setHomeTargetReached(void(*reached)())
Definition: DomePosition.h:377
DomePosition::kOff
@ kOff
Definition: DomePosition.h:12
DomePosition::resetWatchdog
void resetWatchdog()
Definition: DomePosition.h:233
DomePosition::setDomeHomeMaxDelay
void setDomeHomeMaxDelay(uint8_t sec)
Definition: DomePosition.h:342
DomePosition::reachedTarget
void reachedTarget()
Definition: DomePosition.h:387
DomePosition::ready
bool ready()
Definition: DomePosition.h:23
DomePosition::getDomeAutoRight
unsigned getDomeAutoRight()
Definition: DomePosition.h:103
DomePosition::setDomeMinSpeed
void setDomeMinSpeed(uint8_t speed)
Definition: DomePosition.h:317
ReelTwo.h
DomePosition::kHome
@ kHome
Definition: DomePosition.h:13
DomePosition::setDomeAutoSpeed
void setDomeAutoSpeed(uint8_t speed)
Definition: DomePosition.h:322
DomePosition::kTarget
@ kTarget
Definition: DomePosition.h:15
DomePosition
Definition: DomePosition.h:7
DomePosition::setDomeAutoMaxDelay
void setDomeAutoMaxDelay(uint8_t sec)
Definition: DomePosition.h:332
DomePosition::getDomeMinDelay
unsigned getDomeMinDelay()
Definition: DomePosition.h:138
DomePosition::getDomeHome
unsigned getDomeHome()
Definition: DomePosition.h:170
DomePosition::kRandom
@ kRandom
Definition: DomePosition.h:14
DomePosition::setDomeHomeRelativeHomePosition
void setDomeHomeRelativeHomePosition(long degrees)
Definition: DomePosition.h:299
DomePosition::getDomeTargetPosition
unsigned getDomeTargetPosition()
Definition: DomePosition.h:175
DomePosition::getDomeTargetMinDelay
unsigned getDomeTargetMinDelay()
Definition: DomePosition.h:128
DomePosition::getDomeHomeMaxDelay
unsigned getDomeHomeMaxDelay()
Definition: DomePosition.h:123
DomePosition::setDomeRelativeTargetPosition
void setDomeRelativeTargetPosition(long degrees)
Definition: DomePosition.h:286
DomePosition::isAtPosition
bool isAtPosition(long degrees)
Definition: DomePosition.h:261
DomePositionProvider::ready
virtual bool ready()=0
DomePosition::getDomeAutoMinDelay
unsigned getDomeAutoMinDelay()
Definition: DomePosition.h:108
DomePosition::getDomeMinSpeed
float getDomeMinSpeed()
Definition: DomePosition.h:83
DomePosition::getDomeAutoLeft
unsigned getDomeAutoLeft()
Definition: DomePosition.h:98
DomePosition::setDomeDefaultMode
void setDomeDefaultMode(Mode mode)
Definition: DomePosition.h:46
DomePosition::setDomeHomeMinDelay
void setDomeHomeMinDelay(uint8_t sec)
Definition: DomePosition.h:337
DomePosition::getDomeFlip
bool getDomeFlip()
Definition: DomePosition.h:52
DomePosition::getDomeRelativeTargetPosition
long getDomeRelativeTargetPosition()
Definition: DomePosition.h:180
DomePositionProvider
Definition: DomePositionProvider.h:4
DomePosition::getDomeTargetMaxDelay
unsigned getDomeTargetMaxDelay()
Definition: DomePosition.h:133
DomePosition::getDomePosition
unsigned getDomePosition()
Definition: DomePosition.h:206
DomePosition::getDomeMode
Mode getDomeMode()
Definition: DomePosition.h:28
DomePosition::setDomeHomePosition
void setDomeHomePosition(long degrees)
Definition: DomePosition.h:269
DomePosition::setDomeTargetMaxDelay
void setDomeTargetMaxDelay(uint8_t sec)
Definition: DomePosition.h:352
DomePosition::resetDefaultMode
void resetDefaultMode()
Definition: DomePosition.h:226
DomePosition::getDomeHomeMinDelay
unsigned getDomeHomeMinDelay()
Definition: DomePosition.h:118
DomePosition::getDomeSpeed
float getDomeSpeed()
Definition: DomePosition.h:57
DomePosition::getDomeSpeedHome
float getDomeSpeedHome()
Definition: DomePosition.h:73
DomePosition::setDomeFudgeFactor
void setDomeFudgeFactor(uint8_t fudge)
Definition: DomePosition.h:357
DomePosition::setDomeAutoMinDelay
void setDomeAutoMinDelay(uint8_t sec)
Definition: DomePosition.h:327
DomePosition::setAutoTargetReached
void setAutoTargetReached(void(*reached)())
Definition: DomePosition.h:382
DomePosition::getDomeSpeedTarget
float getDomeSpeedTarget()
Definition: DomePosition.h:78
DomePosition::setDomeMode
void setDomeMode(Mode mode)
Definition: DomePosition.h:40
DomePosition::setDomeTargetPosition
void setDomeTargetPosition(long degrees)
Definition: DomePosition.h:277
DomePosition::getHomeRelativeDomePosition
unsigned getHomeRelativeDomePosition()
Definition: DomePosition.h:248
DomePosition::getDomeFudge
unsigned getDomeFudge()
Definition: DomePosition.h:93
DomePosition::shortestDistance
int shortestDistance(int origin, int target)
Definition: DomePosition.h:185
DomePosition::getDomeDefaultMode
Mode getDomeDefaultMode()
Definition: DomePosition.h:35
DomePosition::isTimeout
bool isTimeout()
Definition: DomePosition.h:243
DomePosition::setDomeTargetMinDelay
void setDomeTargetMinDelay(uint8_t sec)
Definition: DomePosition.h:347
DomePosition::reachedAutoTarget
void reachedAutoTarget()
Definition: DomePosition.h:402
DomePositionProvider.h
DomePosition::setDomeAutoRightDegrees
void setDomeAutoRightDegrees(uint8_t degrees)
Definition: DomePosition.h:367
DomePosition::setDomeAutoLeftDegrees
void setDomeAutoLeftDegrees(uint8_t degrees)
Definition: DomePosition.h:362
DomePosition::setDomeHomeSpeed
void setDomeHomeSpeed(uint8_t speed)
Definition: DomePosition.h:307
DomePosition::DomePosition
DomePosition(DomePositionProvider &provider)
Definition: DomePosition.h:18
DomePosition::getRelativeDegrees
int getRelativeDegrees()
Definition: DomePosition.h:221
DomePosition::normalize
long normalize(long degrees)
Definition: DomePosition.h:253
DomePosition::setDomeHomeRelativeTargetPosition
void setDomeHomeRelativeTargetPosition(long degrees)
Definition: DomePosition.h:294
DomePosition::getDomeAutoSpeed
float getDomeAutoSpeed()
Definition: DomePosition.h:88
DomePosition::setDomeTargetSpeed
void setDomeTargetSpeed(uint8_t speed)
Definition: DomePosition.h:312
DomePosition::getDomeAutoMaxDelay
unsigned getDomeAutoMaxDelay()
Definition: DomePosition.h:113
DomePosition::setTimeout
void setTimeout(uint8_t timeout)
Definition: DomePosition.h:238
DomePositionProvider::getAngle
virtual int getAngle()=0