@@ -223,58 +223,60 @@ inline byte processCommand() {
223223inline void processSerialBuffer () {
224224 int iterationsLeft = 10000 ;
225225 while (iterationsLeft > 0 ) {
226- if (processCommand ()) {
227- iterationsLeft -= 2000 ;
228- } else {
226+ byte result = processCommand ();
227+ if (result == 0 ) {
229228 iterationsLeft -= 1 ;
229+ } else if (result == 1 ) {
230+ iterationsLeft -= 5 ;
231+ } else {
232+ iterationsLeft -= 300 ;
230233 }
231234 serialRead ();
232235 }
233-
234236}
235237
236238// CMD: Returns magic number to indicate that the controller is alive.
237239byte cmdReady () {
238240 // Check if not enough bytes yet.
239241 if (cmdInBuffIndex < 3 ) {
240- return 0 ;
242+ return 1 ;
241243 }
242244 cmdInBuffIndex = 0 ;
243245 if (!checkCrc (cmd, 1 )) {
244- return 0 ;
246+ return 2 ;
245247 }
246248 // Return magic number.
247249 cmd[0 ] = 0x40 ;
248250 write1 (cmd);
249- return 1 ;
251+ return 3 ;
250252}
251253
252254// CMD: Adds a command to the queue.
253255byte cmdSteps () {
254256 // Check if not enough bytes yet.
255257 if (cmdInBuffIndex < 20 ) {
256- return 0 ;
258+ return 1 ;
257259 }
258260 cmdInBuffIndex = 0 ;
259261 if (!checkCrc (cmd, 18 )) {
260- return 0 ;
262+ return 2 ;
261263 }
262264 currGripper = cmd[14 ] | cmd[15 ] << 8 ;
263265 currToolRotation = cmd[16 ] | cmd[17 ] << 8 ;
264266 cmd[0 ] = cmdQueue.appendHead ((ulong*) &cmd[1 ], (ulong*) &cmd[5 ], (ulong*) &cmd[9 ], &cmd[13 ], currGripper, currToolRotation, Move);
265267 write1 (cmd);
266- return 1 ;
268+ return 3 ;
267269}
268270
269271// CMD: Starts calibration routine.
270272byte cmdCalibrateJoint () {
271273 // Check if not enough bytes yet.
272274 if (cmdInBuffIndex < 13 ) {
273- return 0 ;
275+ return 1 ;
274276 }
275277 cmdInBuffIndex = 0 ;
276278 if (!checkCrc (cmd, 11 )) {
277- return 0 ;
279+ return 2 ;
278280 }
279281 // The received command has the following structure:
280282 // long - forward speed
@@ -285,85 +287,85 @@ byte cmdCalibrateJoint() {
285287 byte ctrl = cmd[10 ];
286288 byte joint = ctrl & 0x03 ;
287289 if (joint == 3 ) {
288- return 0 ;
290+ return 2 ;
289291 }
290292 cmdQueue.clear ();
291293 calibrator.start (pin, ctrl, (ulong*) &cmd[1 ], (ulong*) &cmd[5 ], currGripper, currToolRotation);
292294 write0 ();
293- return 1 ;
295+ return 3 ;
294296}
295297
296298// CMD: Returns data read from accelerometers.
297299byte cmdGetAccels () {
298300 // Check if not enough bytes yet.
299301 if (cmdInBuffIndex < 3 ) {
300- return 0 ;
302+ return 1 ;
301303 }
302304 cmdInBuffIndex = 0 ;
303305 if (!checkCrc (cmd, 1 )) {
304- return 0 ;
306+ return 2 ;
305307 }
306308 write22 (cmd, &accelRear, &accelFront);
307- return 1 ;
309+ return 3 ;
308310}
309311
310312// CMD: Stops the arm by clearing command buffer and stopping calibrator
311313// Use in emergency
312314byte cmdEmergencyStop () {
313315 // Check if not enough bytes yet.
314316 if (cmdInBuffIndex < 3 ) {
315- return 0 ;
317+ return 1 ;
316318 }
317319 cmdInBuffIndex = 0 ;
318320 if (!checkCrc (cmd, 1 )) {
319- return 0 ;
321+ return 2 ;
320322 }
321323 cmdQueue.clear ();
322324 calibrator.stop ();
323325 write0 ();
324- return 1 ;
326+ return 3 ;
325327}
326328
327329// CMD: Sets joint counters to the received values.
328330byte cmdSetCounters () {
329331 // Check if not enough bytes yet.
330332 if (cmdInBuffIndex < 15 ) {
331- return 0 ;
333+ return 1 ;
332334 }
333335 cmdInBuffIndex = 0 ;
334336 if (!checkCrc (cmd, 13 )) {
335- return 0 ;
337+ return 2 ;
336338 }
337339 motorPositionBase = ((long )cmd[1 ] << 24 ) | ((long )cmd[2 ] << 16 ) | ((long )cmd[3 ] << 8 ) | (long )cmd[4 ];
338340 motorPositionRear = ((long )cmd[5 ] << 24 ) | ((long )cmd[6 ] << 16 ) | ((long )cmd[7 ] << 8 ) | (long )cmd[8 ];
339341 motorPositionFore = ((long )cmd[9 ] << 24 ) | ((long )cmd[10 ] << 16 ) | ((long )cmd[11 ] << 8 ) | (long )cmd[12 ];
340342 write0 ();
341- return 1 ;
343+ return 3 ;
342344}
343345
344346// CMD: Returns current counters for all joints.
345347byte cmdGetCounters () {
346348 // Check if not enough bytes yet.
347349 if (cmdInBuffIndex < 3 ) {
348- return 0 ;
350+ return 1 ;
349351 }
350352 cmdInBuffIndex = 0 ;
351353 if (!checkCrc (cmd, 1 )) {
352- return 0 ;
354+ return 2 ;
353355 }
354356 write444 (cmd, (ulong*)&motorPositionBase, (ulong*)&motorPositionRear, (ulong*)&motorPositionFore);
355- return 1 ;
357+ return 3 ;
356358}
357359
358360// CMD: Enables/Disables Laser
359361byte cmdLaserOn () {
360362 // Check if not enough bytes yet.
361363 if (cmdInBuffIndex < 4 ) {
362- return 0 ;
364+ return 1 ;
363365 }
364366 cmdInBuffIndex = 0 ;
365367 if (!checkCrc (cmd, 2 )) {
366- return 0 ;
368+ return 2 ;
367369 }
368370 byte on = cmd[1 ];
369371 if (on) {
@@ -372,18 +374,18 @@ byte cmdLaserOn() {
372374 cmd[0 ] = cmdQueue.appendHead (0 , 0 , 0 , 0 , 0 , 0 , LaserOff);
373375 }
374376 write1 (cmd);
375- return 1 ;
377+ return 3 ;
376378}
377379
378380// CMD: Enables/Disables Pump
379381byte cmdPumpOn () {
380382 // Check if not enough bytes yet.
381383 if (cmdInBuffIndex < 4 ) {
382- return 0 ;
384+ return 1 ;
383385 }
384386 cmdInBuffIndex = 0 ;
385387 if (!checkCrc (cmd, 2 )) {
386- return 0 ;
388+ return 2 ;
387389 }
388390 byte on = cmd[1 ];
389391 if (on) {
@@ -392,18 +394,18 @@ byte cmdPumpOn() {
392394 cmd[0 ] = cmdQueue.appendHead (0 , 0 , 0 , 0 , 0 , 0 , PumpOff);
393395 }
394396 write1 (cmd);
395- return 1 ;
397+ return 3 ;
396398}
397399
398400// CMD: Enables/Disables Valve
399401byte cmdValveOn () {
400402 // Check if not enough bytes yet.
401403 if (cmdInBuffIndex < 4 ) {
402- return 0 ;
404+ return 1 ;
403405 }
404406 cmdInBuffIndex = 0 ;
405407 if (!checkCrc (cmd, 2 )) {
406- return 0 ;
408+ return 2 ;
407409 }
408410 byte on = cmd[1 ];
409411 if (on) {
@@ -412,7 +414,7 @@ byte cmdValveOn() {
412414 cmd[0 ] = cmdQueue.appendHead (0 , 0 , 0 , 0 , 0 , 0 , ValveOff);
413415 }
414416 write1 (cmd);
415- return 1 ;
417+ return 3 ;
416418}
417419
418420void laserOn () {
0 commit comments