chore: replace print with logging and make robot conditional
All print statements in the main program, and components used by the main program, have been replaced with appropriate logging statements. The connection to the robot now only gets made when it's possible, otherwise only the microphone will be run. ref: N25B-119
This commit is contained in:
15
README.md
15
README.md
@@ -24,7 +24,11 @@ python -m virtualenv .venv
|
||||
source .venv/bin/activate
|
||||
```
|
||||
|
||||
Before installing the Python packages, you'll need to have the `portaudio` system package installed.
|
||||
To be able to install the PyAudio Python package, you'll need to have the `portaudio` system package installed. On Debian or Ubuntu:
|
||||
|
||||
```shell
|
||||
sudo apt install portaudio19-dev
|
||||
```
|
||||
|
||||
Then you can install the required packages with
|
||||
|
||||
@@ -61,7 +65,14 @@ python -c "import qi; print(qi)"
|
||||
You should now be able to run this project.
|
||||
|
||||
### MacOS
|
||||
...
|
||||
|
||||
On ARM CPU's, pyenv doesn't want to install Python 2. You can download and install it from [the Python website](https://www.python.org/downloads/release/python-2718/).
|
||||
|
||||
Create a virtual environment as described in the Linux section.
|
||||
|
||||
Then build `portaudio` for x86_64 CPU's.
|
||||
|
||||
Then follow the remaining installation instructions in the Linux section.
|
||||
|
||||
## Running
|
||||
Assuming you have the virtual environment activated (`source .venv/bin/activate` on Linux) and that you have a virtual robot running on localhost you should be able to run this project by typing
|
||||
|
||||
30
main.py
30
main.py
@@ -1,4 +1,7 @@
|
||||
import qi
|
||||
import sys
|
||||
import logging
|
||||
logging.
|
||||
|
||||
import zmq
|
||||
|
||||
from src.audio_streaming import AudioStreaming
|
||||
@@ -19,23 +22,38 @@ def listen_for_messages(session):
|
||||
poller = zmq.Poller()
|
||||
poller.register(socket, zmq.POLLIN)
|
||||
|
||||
print("Listening for messages")
|
||||
logging.info("Listening for messages")
|
||||
while not state.exit_event.is_set():
|
||||
if not poller.poll(200): continue # At most 200 ms delay after CTRL+C
|
||||
# We now know there's a message waiting for us
|
||||
message = socket.recv_string()
|
||||
print("Received message: {}".format(message))
|
||||
logging.debug("Received message: {}".format(message))
|
||||
|
||||
if session: say(session, message)
|
||||
|
||||
|
||||
def main():
|
||||
def get_session():
|
||||
if "--qi-url" not in sys.argv:
|
||||
logging.info("No Qi URL argument given. Running in stand-alone mode.")
|
||||
return None
|
||||
|
||||
try:
|
||||
import qi
|
||||
except ImportError:
|
||||
logging.info("Unable to import qi. Running in stand-alone mode.")
|
||||
return None
|
||||
|
||||
try:
|
||||
app = qi.Application()
|
||||
app.start()
|
||||
session = app.session
|
||||
return app.session
|
||||
except RuntimeError:
|
||||
session = None
|
||||
logging.info("Unable to connect to the robot. Running in stand-alone mode.")
|
||||
return None
|
||||
|
||||
|
||||
def main():
|
||||
session = get_session()
|
||||
|
||||
audio_streamer = AudioStreaming()
|
||||
audio_streamer.run()
|
||||
|
||||
5
state.py
5
state.py
@@ -1,3 +1,4 @@
|
||||
import logging
|
||||
import signal
|
||||
import threading
|
||||
|
||||
@@ -16,12 +17,12 @@ class State(object):
|
||||
|
||||
def initialize(self):
|
||||
if self.is_initialized:
|
||||
print("Already initialized")
|
||||
logging.warn("Already initialized")
|
||||
return
|
||||
|
||||
self.exit_event = threading.Event()
|
||||
def handle_exit(_, __):
|
||||
print("Exiting.")
|
||||
logging.info("Exiting.")
|
||||
self.exit_event.set()
|
||||
signal.signal(signal.SIGINT, handle_exit)
|
||||
signal.signal(signal.SIGTERM, handle_exit)
|
||||
|
||||
Reference in New Issue
Block a user