RSeries astromech firmware
Gripper.h
Go to the documentation of this file.
1 #ifndef Gripper_h
2 #define Gripper_h
3 
4 #include "core/AnimatedEvent.h"
5 #include "core/CommandEvent.h"
6 
37 class Gripper :
39 {
40 public:
44  Gripper(byte idPin, byte in1Pin, byte in2Pin, byte pwmPin, byte standbyPin,
45  byte gripperOpenPin, byte gripperClosePin) :
46  fID(idPin),
47  fIn1Pin(in1Pin),
48  fIn2Pin(in2Pin),
49  fPWMPin(pwmPin),
50  fStandbyPin(standbyPin),
51  fGripperOpenPin(gripperOpenPin),
52  fGripperClosePin(gripperClosePin)
53  {
54  pinMode(fID, INPUT_PULLUP);
55  pinMode(fGripperOpenPin, INPUT_PULLUP);
56  pinMode(fGripperClosePin, INPUT_PULLUP);
57  pinMode(fIn1Pin, OUTPUT);
58  pinMode(fIn2Pin, OUTPUT);
59  pinMode(fPWMPin, OUTPUT);
60  pinMode(fStandbyPin, OUTPUT);
61  gripperDisarm();
62  }
63 
64  bool isAttached()
65  {
66  return !digitalRead(fID);
67  }
68 
70  {
71  return !digitalRead(fGripperOpenPin);
72  }
73 
75  {
76  return !digitalRead(fGripperClosePin);
77  }
78 
82  void gripperArm()
83  {
84  fArmed = true;
85  }
86 
91  {
92  fArmed = false;
93  gripperOff();
94  }
95 
99  void gripperOn()
100  {
101  if (isGripperOpen())
102  {
103  DEBUG_PRINTLN("OPEN");
104  }
105  if (isGripperClosed())
106  {
107  DEBUG_PRINTLN("CLOSED");
108  }
109  if (fArmed && fCount == 0)
110  {
111  if (isGripperClosed())
112  {
113  fCount = random(20, 40);
114  fDirection = 1000;
115  fNextTime = millis();
116  }
117  else
118  {
119  DEBUG_PRINTLN("GRIPPER NOT CLOSED");
120  }
121  }
122  }
123 
127  void gripperOff()
128  {
129  fCount = 0;
130  fDirection = 0;
131  fNextTime = 0;
132  standby();
133  }
134 
138  virtual void handleCommand(const char* cmd) override
139  {
140  if (*cmd++ == 'G' && *cmd++ == 'P')
141  {
142  // Cannot arm/disarm welder using commands. Gripper should be automatically
143  // armed after opening door and raising arm. As soon as arm starts lowering
144  // the gripper should be disarmed.
145  if (cmd[0] == 'O' && cmd[1] == 'N' && cmd[2] == '\0')
146  {
147  gripperOn();
148  }
149  else if (cmd[0] == 'O' && cmd[1] == 'F' && cmd[2] == 'F' && cmd[3] == '\0')
150  {
151  gripperOff();
152  }
153  }
154  }
155 
159  virtual void animate() override
160  {
161  if (!fArmed || fDirection == 0)
162  return;
163  uint32_t currentTime = millis();
164  if (fNextTime < currentTime)
165  {
166  fNextTime = currentTime + random(150,350);
167  if (fCount > 0)
168  {
169  fDirection = -fDirection;
170  fCount--;
171  }
172  else
173  {
174  // Closing
175  fDirection = -1000;
176  }
177  DEBUG_PRINT("fDirection: ");
178  DEBUG_PRINTLN(fDirection);
179  }
180  if (fDirection > 0 && isGripperOpen())
181  {
182  // Fully open reverse direction
183  fDirection = -fDirection;
184  DEBUG_PRINTLN("Fully open reverse");
185  }
186  else if (fDirection < 0 && isGripperClosed())
187  {
188  // Fully closed reverse direction unless count is zero
189  fDirection = (fCount > 0) ? -fDirection : 0;
190  DEBUG_PRINTLN("Fully closed reverse");
191  }
192 
193  if (fDirection == 0)
194  {
195  DEBUG_PRINTLN("******STOP");
196  stop();
197  }
198  else
199  {
200  // DEBUG_PRINT("******DRIVE");
201  // DEBUG_PRINTLN(fDirection);
202  drive(fDirection);
203  }
204  }
205 
206 private:
207  byte fID;
208  byte fIn1Pin;
209  byte fIn2Pin;
210  byte fPWMPin;
211  byte fStandbyPin;
212  byte fGripperOpenPin;
213  byte fGripperClosePin;
214  bool fArmed;
215  int fDirection;
216  uint8_t fCount;
217  uint32_t fNextTime;
218 
219  void drive(int speed)
220  {
221  digitalWrite(fStandbyPin, HIGH);
222  if (speed >= 0)
223  {
224  digitalWrite(fIn1Pin, HIGH);
225  digitalWrite(fIn2Pin, LOW);
226  analogWrite(fPWMPin, speed);
227  }
228  else
229  {
230  digitalWrite(fIn1Pin, LOW);
231  digitalWrite(fIn2Pin, HIGH);
232  analogWrite(fPWMPin, -speed);
233  }
234  }
235 
236  void stop()
237  {
238  digitalWrite(fIn1Pin, HIGH);
239  digitalWrite(fIn2Pin, HIGH);
240  analogWrite(fPWMPin, 0);
241  }
242 
243  void standby()
244  {
245  digitalWrite(fStandbyPin, LOW);
246  }
247 };
248 
250 
251 #endif
CommandEvent
Base class for all command enabled devices. CommandEvent::handleCommand() is called for each device e...
Definition: CommandEvent.h:17
Gripper::isGripperOpen
bool isGripperOpen()
Definition: Gripper.h:69
DEBUG_PRINT
#define DEBUG_PRINT(s)
Definition: ReelTwo.h:189
AnimatedEvent
Base class for all animated devices. AnimatedEvent::animate() is called for each device once through ...
Definition: AnimatedEvent.h:18
Gripper::animate
virtual void animate() override
Perform a single frame of welder animation.
Definition: Gripper.h:159
AnimatedEvent.h
DEBUG_PRINTLN
#define DEBUG_PRINTLN(s)
Definition: ReelTwo.h:188
Gripper::Gripper
Gripper(byte idPin, byte in1Pin, byte in2Pin, byte pwmPin, byte standbyPin, byte gripperOpenPin, byte gripperClosePin)
Constructor.
Definition: Gripper.h:44
Gripper::gripperDisarm
void gripperDisarm()
Disarm gripper.
Definition: Gripper.h:90
Gripper::gripperOn
void gripperOn()
Turn on the gripper.
Definition: Gripper.h:99
Gripper::gripperArm
void gripperArm()
Arm gripper.
Definition: Gripper.h:82
Gripper::isGripperClosed
bool isGripperClosed()
Definition: Gripper.h:74
Gripper
Controls a TB662 motor controller to open/close gripper hand.
Definition: Gripper.h:37
Gripper::gripperOff
void gripperOff()
Turn off the smoke machine.
Definition: Gripper.h:127
Gripper::handleCommand
virtual void handleCommand(const char *cmd) override
Gripper Commands start with 'GP'.
Definition: Gripper.h:138
Gripper::isAttached
bool isAttached()
Definition: Gripper.h:64
CommandEvent.h