RSeries astromech firmware
StealthController.h
Go to the documentation of this file.
1 #ifndef StealthController_h
2 #define StealthController_h
3 
4 #include "ReelTwo.h"
5 #include "core/SetupEvent.h"
6 #include "core/AnimatedEvent.h"
7 #include <Wire.h>
8 
21 {
22 public:
23  const byte S1_FLAG = 1<<0;
24  const byte S2_FLAG = 1<<1;
25  const byte J1_FLAG = 1<<2;
26  const byte J2_FLAG = 1<<3;
27  const byte LEGMOTORS_FLAG = 1<<4;
28  const byte DOMEMOTOR_FLAG = 1<<5;
29 
35  const byte pinS1 = 26,
36  const byte pinJ1 = 28,
37  const byte pinS2 = 27,
38  const byte pinJ2 = 29,
39  const byte pinLegMotors = 10,
40  const byte pinDomeMotor = 11) :
41  fPosDegrees(~0),
42  fPinS1(pinS1),
43  fPinJ1(pinJ1),
44  fPinS2(pinS2),
45  fPinJ2(pinJ2),
46  fPinLegMotors(pinLegMotors),
47  fPinDomeMotor(pinDomeMotor)
48  {
49  }
50 
54  virtual void setup() override
55  {
56  pinMode(fPinS1, INPUT_PULLUP);
57  pinMode(fPinJ1, INPUT_PULLUP);
58  pinMode(fPinS2, INPUT_PULLUP);
59  pinMode(fPinJ2, INPUT_PULLUP);
60  pinMode(fPinLegMotors, INPUT);
61  pinMode(fPinDomeMotor, INPUT);
62  }
63 
64  void setDomePosition(uint16_t degrees)
65  {
66  if (degrees != fPosDegrees)
67  {
68  fPosDegrees = degrees;
69  int output = map(degrees, 0, 360, 0, 4095);
70 
71  Wire.beginTransmission(0x62);
72  Wire.write(MCP4726_CMD_WRITEDAC);
73  // Upper bits (D11.D10.D9.D8.D7.D6.D5.D4)
74  Wire.write(output / 16);
75  // Lower bits (D3.D2.D1.D0.x.x.x.x)
76  Wire.write((output % 16) << 4);
77  Wire.endTransmission();
78  }
79  }
80 
81  inline bool getS1()
82  {
83  return ((fStatus & S1_FLAG) != 0);
84  }
85 
86  inline bool getS2()
87  {
88  return ((fStatus & S2_FLAG) != 0);
89  }
90 
91  inline bool getJ1()
92  {
93  return ((fStatus & J1_FLAG) != 0);
94  }
95 
96  inline bool getJ2()
97  {
98  return ((fStatus & J2_FLAG) != 0);
99  }
100 
101  inline byte getStatusFlags()
102  {
103  return fStatus;
104  }
105 
106  inline bool getStatusChanged()
107  {
108  return fChanged;
109  }
110 
111  inline bool didLegMotorsStart()
112  {
113  return ((fPrevStatus & LEGMOTORS_FLAG) == 0 &&
114  (fStatus & LEGMOTORS_FLAG) != 0);
115  }
116 
117  inline bool didLegMotorsStop()
118  {
119  return ((fPrevStatus & LEGMOTORS_FLAG) != 0 &&
120  (fStatus & LEGMOTORS_FLAG) == 0);
121  }
122 
123  inline bool getLegMotorsMoving()
124  {
125  return ((fStatus & LEGMOTORS_FLAG) != 0);
126  }
127 
128  inline bool didDomeMotorStart()
129  {
130  return ((fPrevStatus & DOMEMOTOR_FLAG) == 0 &&
131  (fStatus & DOMEMOTOR_FLAG) != 0);
132  }
133 
134  inline bool didDomeMotorStop()
135  {
136  return ((fPrevStatus & DOMEMOTOR_FLAG) != 0 &&
137  (fStatus & DOMEMOTOR_FLAG) == 0);
138  }
139 
140  inline bool getDomeMotorMoving()
141  {
142  return ((fStatus & DOMEMOTOR_FLAG) != 0);
143  }
147  virtual void animate() override
148  {
149  byte status = 0;
150  fChanged = false;
151  if (digitalRead(fPinS1) == HIGH)
152  status |= S1_FLAG;
153  if (digitalRead(fPinS2) == HIGH)
154  status |= S2_FLAG;
155  if (digitalRead(fPinJ1) == HIGH)
156  status |= J1_FLAG;
157  if (digitalRead(fPinJ2) == HIGH)
158  status |= J2_FLAG;
159  if (digitalRead(fPinLegMotors) == HIGH)
160  status |= LEGMOTORS_FLAG;
161  if (digitalRead(fPinDomeMotor) == HIGH)
162  status |= DOMEMOTOR_FLAG;
163  if (status != fStatus)
164  {
165  DEBUG_PRINT("STEALTH STATUS CHANGE: ");
166  DEBUG_PRINT_HEX(status);
167  DEBUG_PRINTLN();
168  fPrevStatus = fStatus;
169  fStatus = status;
170  fChanged = true;
171  }
172  }
173 
174 private:
175  const byte MCP4726_CMD_WRITEDAC = 0x40; // Writes data to the DAC
176  const byte MCP4726_CMD_WRITEDACEEPROM = 0x60; // Writes data to the DAC and the EEPROM (persisting the assigned value after reset)
177  uint16_t fPosDegrees;
178  bool fChanged;
179  byte fStatus;
180  byte fPrevStatus;
181  byte fPinS1;
182  byte fPinJ1;
183  byte fPinS2;
184  byte fPinJ2;
185  byte fPinLegMotors;
186  byte fPinDomeMotor;
187 };
188 
189 #endif
StealthController::getS2
bool getS2()
Definition: StealthController.h:86
StealthController::setup
virtual void setup() override
Setup pin mode for analog read.
Definition: StealthController.h:54
StealthController::DOMEMOTOR_FLAG
const byte DOMEMOTOR_FLAG
Definition: StealthController.h:28
DEBUG_PRINT
#define DEBUG_PRINT(s)
Definition: ReelTwo.h:189
ReelTwo.h
SetupEvent.h
AnimatedEvent
Base class for all animated devices. AnimatedEvent::animate() is called for each device once through ...
Definition: AnimatedEvent.h:18
StealthController
Stealth Controller directly attached to the Arduino to read J1/J2 and S1/S2 status header and using a...
Definition: StealthController.h:19
StealthController::didLegMotorsStart
bool didLegMotorsStart()
Definition: StealthController.h:111
SetupEvent
Base class for all devices that require setup that cannot happen in the constructor....
Definition: SetupEvent.h:15
StealthController::S2_FLAG
const byte S2_FLAG
Definition: StealthController.h:24
StealthController::didDomeMotorStop
bool didDomeMotorStop()
Definition: StealthController.h:134
AnimatedEvent.h
StealthController::getStatusFlags
byte getStatusFlags()
Definition: StealthController.h:101
StealthController::getJ1
bool getJ1()
Definition: StealthController.h:91
StealthController::getS1
bool getS1()
Definition: StealthController.h:81
DEBUG_PRINTLN
#define DEBUG_PRINTLN(s)
Definition: ReelTwo.h:188
StealthController::getStatusChanged
bool getStatusChanged()
Definition: StealthController.h:106
StealthController::LEGMOTORS_FLAG
const byte LEGMOTORS_FLAG
Definition: StealthController.h:27
StealthController::didLegMotorsStop
bool didLegMotorsStop()
Definition: StealthController.h:117
StealthController::didDomeMotorStart
bool didDomeMotorStart()
Definition: StealthController.h:128
StealthController::J1_FLAG
const byte J1_FLAG
Definition: StealthController.h:25
StealthController::S1_FLAG
const byte S1_FLAG
Definition: StealthController.h:23
DEBUG_PRINT_HEX
#define DEBUG_PRINT_HEX(s)
Definition: ReelTwo.h:192
StealthController::getDomeMotorMoving
bool getDomeMotorMoving()
Definition: StealthController.h:140
StealthController::getLegMotorsMoving
bool getLegMotorsMoving()
Definition: StealthController.h:123
StealthController::getJ2
bool getJ2()
Definition: StealthController.h:96
StealthController::J2_FLAG
const byte J2_FLAG
Definition: StealthController.h:26
StealthController::setDomePosition
void setDomePosition(uint16_t degrees)
Definition: StealthController.h:64
StealthController::StealthController
StealthController(const byte pinS1=26, const byte pinJ1=28, const byte pinS2=27, const byte pinJ2=29, const byte pinLegMotors=10, const byte pinDomeMotor=11)
Default Constructor.
Definition: StealthController.h:34
StealthController::animate
virtual void animate() override
Read the pot once through the loop.
Definition: StealthController.h:147