Need Help debugging this Script

Hey all,

I have written this script with help from Grog and Alessandruino,

Can some one please help me figuring whats wrong.

This Script if for Chess game play, It send and receives the chess moves via serial and gives voice feedback of the game play.

What I cant get to work is getting data into function voice when the computer play its move.

I have highighted the part where i think i might be wrong.

Thanks for the Help :)


 

#Importing the Libraries

 
from time import sleep
from org.myrobotlab.service import Speech
from org.myrobotlab.service import Runtime
 
#Starting the required Services
 
serial = Runtime.createAndStart("serial","Serial")
chessgame = Runtime.createAndStart("chessgame", "ChessGame")
log = Runtime.createAndStart("log", "Log")
speech = Runtime.create("speech","Speech")
 
#Configureing Speech Service
 
speech.startService()
speech.setLanguage("en")
speech.speak("Game Begins!")
 
count = 0
 
#Connecting Arduino via Serial
 
if not serial.isConnected():
    serial.connect("COM5")
 
# Adding Listeners
serial.addListener("publishByte", python.name, "input") 
chessgame.addListener("computerMoved", python.name, "voice") 
chessgame.addListener("computerMoved", log.name, "log")
chessgame.addListener("makeMove", serial.name, "write") 
 
#Function taking I/P from Serial and sending the data to Chess Game
 
def input():
 
 
global count
  newByte = int(serial.readByte())
  #we have reached the end of a new line
  if (newByte == 10) :
    chessMove = ""
    while (newByte != 13):
        newByte = serial.readByte()
        chessMove += chr(newByte)
 
    print chessMove  
    chessgame.move(chessMove)
  part1 = chessMove[0:2]
   part2 = chessMove[2:4]
   feedback = "You Played " + part1 + " to " + part2
   speech.speak(feedback)
   print feedback
 
 
# Function "voice" which decodes the move played by the computer and gives a voice feedback
 
def voice():
 
incoming = msg_chessgame_computerMoved.data[0]
 
x = y = z = m = False
x = incoming.startswith("B")
y = incoming.startswith("N")
z = incoming.startswith("Q")
m = incoming.startswith("R") 
 
if ( x == True):
  part1 = incoming[1:3]
  part2 = incoming[4:6]
  feedback = "Computer played Bishop from " + part1 + " to " + part2
  speech.speak(feedback)
  print feedback
 
if ( y == True):
  part1 = incoming[1:3]
  part2 = incoming[4:6]
  feedback = "Computer played Knight from " + part1 + " to " + part2
  speech.speak(feedback)
  print feedback
 
if ( z == True):
  part1 = incoming[1:3]
  part2 = incoming[4:6]
  feedback = "Computer played Queen from " + part1 + " to " + part2
  speech.speak(feedback)
  print feedback
 
if ( m == True):
  part1 = incoming[1:3]
  part2 = incoming[4:6]
  feedback = "Computer played Rook from " + part1 + " to " + part2
  speech.speak(feedback)
  print feedback
 
if ( (m == False) and (y == False) and (x == False) and (z == False) ):
  part1 = incoming[0:2]
  part2 = incoming[3:5]
  feedback = "Computer played Pawn from " + part1 + " to " + part2
  speech.speak(feedback)
  print feedback

 

[[ChessGame.mehtaatur.py]]
Alessandruino's picture

Can you send a noworky after

Can you send a noworky after you got it running? It would help a ton in debugging since we can see much more stuffs in that way

Thanks :)

mehtaatur's picture

No Worky sent, Just wanted to

No Worky sent,

Just wanted to let you knw that I am using an older v1695.20130912.0508 because I am getting error with serial comms with arduino in the newer versions.

and regarding the script: it works HALF! (dnt knw how to explain it to you)

But in short:

The script has 2 functions: input() and voice(),

If I rename the function "voice()" as function "input()" then tht block works properly, and the other doesnt work.

So which ever functuion is named as "input" will work but other function wont work.

I knw that this must be beacuse I must be making some syntax error but frm seeing all the tutorials on the site (servo and joystick) they all seem to be using the same syntax for using python service as a listener.

 

Please advice the changes.