Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add logging to syslog #8

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Python projects/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.pyc
43 changes: 43 additions & 0 deletions Python projects/PiCmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

import QBOtalk

import syslog


cmd = ""
angle = 0
Expand Down Expand Up @@ -65,8 +67,10 @@ def get_command():
cmd = str(arg)
else:
print "wrong command"
syslog.syslog("wrong command")
except:
print "command param error"
syslog.syslog("command param error")


def get_angle():
Expand All @@ -78,8 +82,10 @@ def get_angle():
angle = int(arg)
else:
print "wrong angle value"
syslog.syslog("wrong angle value")
except:
print "wrong angle value"
syslog.syslog("wrong angle value")


def get_text():
Expand All @@ -98,6 +104,7 @@ def get_text():
text += " " + arg
except:
print "wrong text value"
syslog.syslog("wrong text value")

def get_pid():
global pid
Expand All @@ -117,7 +124,9 @@ def get_pid():
i += 1
except:
print "wrong pid value"
syslog.syslog("wrong pid value")
print "PID: " + str(pid)
syslog.syslog("PID: " + str(pid))

def get_axis():
global axis
Expand All @@ -128,6 +137,7 @@ def get_axis():
axis = int(arg)
else:
print "wrong axis value"
syslog.syslog("wrong axis value")

def get_speed():
global speed
Expand All @@ -139,8 +149,10 @@ def get_speed():
speed = int(arg)
else:
print "wrong speed value"
syslog.syslog("wrong speed value")
except:
print "wrong speed value"
syslog.syslog("wrong speed value")

def get_color():
global color
Expand All @@ -152,8 +164,10 @@ def get_color():
color = arg
else:
print "wrong color value"
syslog.syslog("wrong color value")
except:
print "color param error"
syslog.syslog("color param error")


def get_language():
Expand All @@ -166,8 +180,10 @@ def get_language():
lang = arg
else:
print "wrong language value"
syslog.syslog("wrong language value")
except:
print "laguage param error"
syslog.syslog("laguage param error")

def get_mouth_expression():
global expression
Expand All @@ -179,27 +195,34 @@ def get_mouth_expression():
expression = arg
else:
print "wrong expression value"
syslog.syslog("wrong expression value")
except:
print "expression param error"
syslog.syslog("expression param error")


def get_mouth_matrix():
global matrix
global idx

print "GET_MOUTH_MATRIX"
syslog.syslog("GET_MOUTH_MATRIX")
print "idx: " + str(idx)
syslog.syslog("idx: " + str(idx))
try:
i = 0
while i<4:
arg = scan_argument(idx)
print "arg: " + arg
syslog.syslog("arg: " + arg)
matrix |= int(arg) << (8*(3-i))
print "arg matrix: " + str(matrix)
syslog.syslog("arg matrix: " + str(matrix))
i = i + 1
idx = idx + 1
except:
print "mouth matrix param error"
syslog.syslog("mouth matrix param error")


def say():
Expand Down Expand Up @@ -241,13 +264,17 @@ def CommandOK_Action():

if cmd == "servo" and angle != 0 and axis != 0 and speed >= 0:
print "Sending: " + cmd + "(" + str(axis) + "," + str(angle) + "," + str(speed) + ")"
syslog.syslog("Sending: " + cmd + "(" + str(axis) + "," + str(angle) + "," + str(speed) + ")")
HeadServo.SetServo(axis, angle, speed)
elif cmd == "move" and angle != 0 and axis != 0:
syslog.syslog("Sending: " + cmd + "(" + str(axis) + "," + str(angle) + ")")
HeadServo.SetAngle(axis, angle)
elif cmd == "move_rel" and angle != 0 and axis != 0:
syslog.syslog("Sending: " + cmd + "(" + str(axis) + "," + str(angle) + ")")
HeadServo.SetAngleRelative(axis, angle)
elif cmd == "nose" and color != "":
print "Sending: " + cmd + "(" + str(color) + ")"
syslog.syslog("Sending: " + cmd + "(" + str(color) + ")")
# se apaga el led de la nariz
# llamada al comando "nose"
if (color == "none"):
Expand All @@ -261,6 +288,7 @@ def CommandOK_Action():
elif cmd == "say" and text != "":
# /* write "Hi" to the FIFO */
print("Opening FIFO..." + FIFO_say)
syslog.syslog("Opening FIFO..." + FIFO_say)
fifo_say = os.open(FIFO_say, os.O_WRONLY)
os.write(fifo_say, text)
os.close(fifo_say)
Expand All @@ -269,22 +297,27 @@ def CommandOK_Action():
# fifo.write(text)
# fifo.close()
print "Saying: " + text
syslog.syslog("Saying: " + text)
elif cmd == "voice":
config = yaml.safe_load(open("/home/pi/Documents/config.yml"))
print "CONFIG " + str(config)
syslog.syslog("CONFIG " + str(config))

# actualizacion del fichero config
config["language"] = lang
with open('/home/pi/Documents/config.yml', 'w') as f:
yaml.dump(config, f)
print "Setting: " + cmd + " = " + str(lang)
syslog.syslog("Setting: " + cmd + " = " + str(lang))
f.close()
elif cmd == "mouth":
if (matrix != 0):
print "Sending " + cmd + "(" + str(matrix) + ")"
syslog.syslog("Sending " + cmd + "(" + str(matrix) + ")")
HeadServo.SetMouth(matrix)
elif (expression != ""):
print "Sending " + cmd + "(" + expression + ")"
syslog.syslog("Sending " + cmd + "(" + expression + ")")
if expression == "smile":
HeadServo.SetMouth(0x110E00)
elif expression == "sad":
Expand All @@ -302,6 +335,7 @@ def CommandOK_Action():

else:
print "Command error. Type ? to help"
syslog.syslog("Command error. Type ? to help")


options = {"-c" : get_command,
Expand Down Expand Up @@ -339,6 +373,9 @@ def CommandOK_Action():

expressions = {"smile", "sad", "serious", "love"}

# initialize syslog
syslog.openlog("QBO_PiCmd")

# start read key thread
#thread.start_new_thread( dispach_UI,() )

Expand Down Expand Up @@ -368,10 +405,12 @@ def CommandOK_Action():
# Open serial port
ser = serial.Serial(port, baudrate=115200, bytesize = serial.EIGHTBITS, stopbits = serial.STOPBITS_ONE, parity = serial.PARITY_NONE, rtscts = False, dsrdtr =False, timeout = 0)
print "Open serial port sucessfully."
syslog.syslog("Open serial port sucessfully.")
print(ser.name)
HeadServo = QboCmd.Controller(ser)
except:
print "Error opening serial port."
syslog.syslog("Error opening serial port.")
sys.exit()

# Qbo speak and voice recognition init.
Expand All @@ -397,14 +436,18 @@ def CommandOK_Action():
while (1):
idx = 0
print("Opening FIFO..." + FIFO_cmd)
syslog.syslog("Opening FIFO..." + FIFO_cmd)
with open(FIFO_cmd) as fifo_cmd:
print("FIFO opened" + FIFO_cmd)
syslog.syslog("FIFO opened" + FIFO_cmd)
line = fifo_cmd.read()
fifo_cmd.close()
if len(line) == 0:
print("Writer closed")
syslog.syslog("Writer closed")
continue
print('line_cmd: ' + line)
syslog.syslog('line_cmd: ' + line)
# line = raw_input('QBO_>> ')

if (line == "exit" or line == "quit"):
Expand Down
31 changes: 27 additions & 4 deletions Python projects/QBOtalk.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@
import os
import wave
# from gtts import gTTS
import syslog
from time import localtime, strftime
import traceback

syslog.openlog("QBOtalk")

class QBOtalk:
def __init__(self):
Expand All @@ -38,6 +43,7 @@ def __init__(self):
self.m = sr.Microphone(i)
with self.m as source:
self.r.adjust_for_ambient_noise(source)
syslog.syslog("init done.")

def Decode(self, audio):
try:
Expand All @@ -48,11 +54,13 @@ def Decode(self, audio):
else:
str = self.r.recognize_google(audio)
print "LISTEN: " + str
syslog.syslog("LISTEN: " + str)
request = self.ai.text_request()
# request.lang = 'es'
request.query = str
response = request.getresponse()
jsonresp = response.read()
syslog.syslog(jsonresp)
data = json.loads(jsonresp)
str_resp = data["result"]["fulfillment"]["speech"]

Expand Down Expand Up @@ -152,6 +160,7 @@ def SpeechText_2(self, text_to_speech, text_spain):
result = subprocess.call(speak, shell = True)

def callback(self, recognizer, audio):
syslog.syslog("callback")
try:
self.Response = self.Decode(audio)
self.GetResponse = True
Expand All @@ -162,10 +171,16 @@ def callback(self, recognizer, audio):

def callback_listen(self, recognizer, audio):
print("callback listen")
syslog.syslog("callback listen")
try:
#strSpanish = self.r.recognize_google(audio,language="es-ES")
# with open("microphone-results.wav", "wb") as f:
# f.write(audio.get_wav_data())
if (self.config.has_key("listen_audio_dump") and self.config['listen_audio_dump']):
filename = "/tmp/microphone-results-"
filename += strftime("%Y%m%d-%H%M%S", localtime())
filename += ".wav"
with open(filename, "wb") as f:
f.write(audio.get_wav_data())
syslog.syslog("Dumped audio to " + filename)
if (self.config["language"] == "spanish"):
self.strAudio = self.r.recognize_google(audio, language="es-ES")
else:
Expand All @@ -174,11 +189,17 @@ def callback_listen(self, recognizer, audio):
self.strAudio = self.r.recognize_google(audio)
self.GetAudio = True
print("listen: " + self.strAudio)
syslog.syslog("listen: " + self.strAudio)
#print("listenSpanish: ", strSpanish)
#self.SpeechText(self.Response)
except:
except Exception as e:
print("callback listen exception")
self.strAudio = ""
syslog.syslog("callback listen exception: " + str(e))
syslog.syslog(traceback.format_exc())
# If an error occured, give this back so the user can be notified
# that we didn't understand the command
self.strAudio = "ERROR"
self.GetAudio = True
return

def Start(self):
Expand All @@ -197,6 +218,7 @@ def StartBack(self):
self.r.adjust_for_ambient_noise(source)

print("start background listening")
syslog.syslog("start background listening")

return self.r.listen_in_background(self.m, self.callback)

Expand All @@ -205,6 +227,7 @@ def StartBackListen(self):
self.r.adjust_for_ambient_noise(source)

print("start background only listening")
syslog.syslog("start background only listening")

return self.r.listen_in_background(self.m, self.callback_listen)

Expand Down
9 changes: 9 additions & 0 deletions Python projects/feel.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
import sys
import time
import yaml
import syslog


syslog.openlog("QBO_feel")

port = '/dev/serial0'

Expand All @@ -20,6 +24,8 @@
ser = serial.Serial(port, baudrate=115200, bytesize = serial.EIGHTBITS, stopbits = serial.STOPBITS_ONE, parity = serial.PARITY_NONE, rtscts = False, dsrdtr =False, timeout = 0)
print "Open serial port sucessfully."
print(ser.name)
syslog.syslog("Open serial port sucessfully.")
syslog.syslog(ser.name)
except:
print "Error opening serial port."
sys.exit()
Expand All @@ -33,10 +39,13 @@ def WaitForTouch():
if touch:
if touch == [1]:
touch_str = "Touch: right"
syslog.syslog("Touch right")
elif touch == [2]:
touch_str = "Touch: up"
syslog.syslog("Touch up")
elif touch == [3]:
touch_str = "Touch: left"
syslog.syslog("Touch left")

if touch == [1] or touch == [2] or touch == [3]:
print(touch_str + "TO FIFO FEEL" )
Expand Down
Loading