-1

I'm working with the pi2go lite robot. This is my code

import pi2go, time 
import sys
import tty
import termios
import time
pi2go.init()

def stepCount():
  countL += 0
  countR += 0


speed = 60
try:
   pi2go.stepForward(60,16)
   print stepCount

finally:
   pi2go.cleanup()

The question is I am wondering how to count everytime the "pi2go.stepForward(60,16)" is used.

OmamArmy
  • 120
  • 1
  • 6
  • 2
    Is this your whole code? As it is it should raise numerous errors as `countL` and `countR` are not defined. Also, printing `stepCount` (or `stepCount()` as a matter of fact) will not be useful. – DeepSpace Aug 16 '16 at 12:46
  • This link will solve the problem, the answer already exist: http://stackoverflow.com/questions/21716940/is-there-a-way-to-track-the-number-of-times-a-function-is-called – Derick Alangi Aug 16 '16 at 12:47

2 Answers2

0

You were very close. stepCount is a function, so you should call it - that is add the parentheses at the end.

speed = 60
try:
   pi2go.stepForward(60,16)
   stepCount()

Also you didn't define countL and countR. So you need to define those before hand.

But the best way would be to wrap the pi2go.stepForward(60,16) in another function.

Like:

countL = 0
countR = 0

def stepForward(x, y):
    countL += 1
    countR += 1
    pi2go.stepForward(x,y)

And then you can just call stepForward(60, 16).

masnun
  • 11,635
  • 4
  • 39
  • 50
0
counter = dict(ok=0, fail=0, all=0)
try:
    pi2go.stepForward(60,16)
    counter['ok'] += 1
except:
    counter['fail'] += 1
finally:
    counter['all'] += 1
    pi2go.cleanup()
vadim vaduxa
  • 221
  • 6
  • 16