Skip to content
This repository has been archived by the owner on Nov 28, 2022. It is now read-only.

Commit

Permalink
Merge pull request #2 from hapi-robo/devel
Browse files Browse the repository at this point in the history
Devel
  • Loading branch information
r-oung authored Aug 18, 2020
2 parents ea87017 + 1bcd66d commit 1e6c730
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 82 deletions.
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
temipy/__pycache__/
temipy.egg-info/
pytemi/__pycache__/
pytemi.egg-info/
venv/
test.py
29 changes: 12 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,37 +1,30 @@
# temi Connect Python Package
# pytemi
Control temi using Python scripts over MQTT.


## Prerequisites
* [Python 3](https://www.python.org/downloads/)
* Python [virtualenv](https://virtualenv.pypa.io/en/stable/installation.html)
* Connect APK installed on temi, see [here](https://github.com/hapi-robo/connect/tree/devel/android)
* [Connect app](https://github.com/hapi-robo/connect/releases) installed on temi
* MQTT broker. Free brokers for testing:
* [Eclipse](http://test.mosquitto.org/)
* [Mosquitto](http://mqtt.eclipse.org)
* [HiveMQ](http://broker.hivemq.com)


## Ubuntu / MacOS Setup
Clone this repository:
## Setup
Clone this repository and install all dependencies with pip:
```
git clone ...
pip install -r requirements.txt
```

Create a Python virtual environment (`venv`) and install all dependencies:
For Linux users, there's a script that will create a Python virtual environment (assuming it's installed) and install all dependencies:
```
cd temipy/
./setup.sh
```

Activate the virtual environment:
```
source venv/bin/activate
```


## Usage
Make sure temi is connected to an MQTT broker via the Connect app.
Make sure the robot is connected to an MQTT broker via the [Connect app](https://github.com/hapi-robo/connect/releases).

Edit the `sample.py` script and adjust the `parameters` appropriately, then run:
```
Expand All @@ -41,12 +34,14 @@ python sample.py

## Sample Script
```
import temipy as temi
import pytemi as temi
MQTT_HOST = "test.mosquitto.org"
MQTT_PORT = 1883
TEMI_SERIAL = "01234567890"
# connect to the MQTT server
mqtt_client = temi.connect("test.mosquitto.org", 1883)
# connect to the MQTT broker
mqtt_client = temi.connect(MQTT_HOST, MQTT_PORT)
# create robot object
robot = temi.Robot(mqtt_client, TEMI_SERIAL)
Expand Down
File renamed without changes.
File renamed without changes.
75 changes: 49 additions & 26 deletions temipy/robot.py → pytemi/robot.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,73 +18,85 @@ def __init__(self, mqtt_client, temi_serial):
"""
self.client = mqtt_client
self.serial = temi_serial
self.id = temi_serial

def rotate(self, angle):
"""Rotate robot
"""Rotate
"""
print("[CMD] Rotate: {} [deg]".format(angle))

topic = "temi/" + self.serial + "/command/move/turn_by"
payload = json.dumps({"angle": angle})
if (angle != 0):
topic = "temi/" + self.id + "/command/move/turn_by"
payload = json.dumps({"angle": angle})

self.client.publish(topic, payload, qos=0)
self.client.publish(topic, payload, qos=0)

def translate(self, value):
"""Translate robot
"""Translate
"""
print("[CMD] Translate: {} [unitless]".format(value))

if math.copysign(1, value):
topic = "temi/" + self.serial + "/command/move/forward"
elif math.copysign(1, value):
topic = "temi/" + self.serial + "/command/move/backward"
if math.copysign(1, value) > 0:
topic = "temi/" + self.id + "/command/move/forward"
self.client.publish(topic, "{}", qos=0)
elif math.copysign(1, value) < 0:
topic = "temi/" + self.id + "/command/move/backward"
self.client.publish(topic, "{}", qos=0)
else:
pass # do nothing

self.client.publish(topic, "{}", qos=0)

def tilt(self, angle):
"""Tilt robot's head (absolute angle)
"""Tilt head (absolute angle)
"""
print("[CMD] Tilt: {} [deg]".format(angle))

topic = "temi/" + self.serial + "/command/move/tilt"
topic = "temi/" + self.id + "/command/move/tilt"
payload = json.dumps({"angle": angle})

self.client.publish(topic, payload, qos=0)

def tilt_by(self, angle):
"""Tilt robot's head (relative angle)
"""Tilt head (relative angle)
"""
print("[CMD] Tilt By: {} [deg]".format(angle))

topic = "temi/" + self.serial + "/command/move/tilt_by"
topic = "temi/" + self.id + "/command/move/tilt_by"
payload = json.dumps({"angle": angle})

self.client.publish(topic, payload, qos=0)

def stop(self):
"""Follow
"""Stop
"""
print("[CMD] Stop")

topic = "temi/" + self.serial + "/command/move/stop"
topic = "temi/" + self.id + "/command/move/stop"

self.client.publish(topic, "{}", qos=1)

def follow(self):
"""Follow
"""
print("[CMD] Follow")

topic = "temi/" + self.id + "/command/follow/unconstrained"

self.client.publish(topic, "{}", qos=1)

def goto(self, location_name):
"""Go to a specified location
"""Go to a saved location
"""
print("[CMD] Go-To: {}".format(location_name))

topic = "temi/" + self.serial + "/command/waypoint/goto"
topic = "temi/" + self.id + "/command/waypoint/goto"
payload = json.dumps({"location": location_name})

self.client.publish(topic, payload, qos=1)
Expand All @@ -95,7 +107,7 @@ def tts(self, text):
"""
print("[CMD] TTS: {}".format(text))

topic = "temi/" + self.serial + "/command/tts"
topic = "temi/" + self.id + "/command/tts"
payload = json.dumps({"utterance": text})

self.client.publish(topic, payload, qos=1)
Expand All @@ -106,7 +118,7 @@ def tts(self, text):
# """
# print("[CMD] Play Audio: {}".format(url))

# topic = "temi/" + self.serial + "/command/media/audio"
# topic = "temi/" + self.id + "/command/media/audio"
# payload = json.dumps({"url": url})

# self.client.publish(topic, payload, qos=1)
Expand All @@ -117,7 +129,7 @@ def video(self, url):
"""
print("[CMD] Play Video: {}".format(url))

topic = "temi/" + self.serial + "/command/media/video"
topic = "temi/" + self.id + "/command/media/video"
payload = json.dumps({"url": url})

self.client.publish(topic, payload, qos=1)
Expand All @@ -128,7 +140,7 @@ def youtube(self, video_id):
"""
print("[CMD] Play YouTube: {}".format(video_id))

topic = "temi/" + self.serial + "/command/media/youtube"
topic = "temi/" + self.id + "/command/media/youtube"
payload = json.dumps({"video_id": video_id})

self.client.publish(topic, payload, qos=1)
Expand All @@ -139,18 +151,29 @@ def webview(self, url):
"""
print("[CMD] Show Webview: {}".format(url))

topic = "temi/" + self.serial + "/command/media/webview"
topic = "temi/" + self.id + "/command/media/webview"
payload = json.dumps({"url": url})

self.client.publish(topic, payload, qos=1)

def app(self, package_name):
"""Start Android app
"""
print("[CMD] Start App: {}".format(package_name))

topic = "temi/" + self.id + "/command/app"
payload = json.dumps({"package_name": package_name})

self.client.publish(topic, payload, qos=1)

def call(self, room_name):
"""Start a call
"""
print("[CMD] Call: {}".format(room_name))

topic = "temi/" + self.serial + "/command/call/start"
topic = "temi/" + self.id + "/command/call/start"
payload = json.dumps({"room_name": room_name})

self.client.publish(topic, payload, qos=1)
Expand All @@ -161,6 +184,6 @@ def hangup(self):
"""
print("[CMD] Hangup")

topic = "temi/" + self.serial + "/command/call/end"
topic = "temi/" + self.id + "/command/call/end"

self.client.publish(topic, "{}", qos=1)
60 changes: 26 additions & 34 deletions sample.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"""Sample script
"""
import temipy as temi
import pytemi as temi
import time


Expand All @@ -12,7 +12,7 @@
MQTT_PORT = 1883
TEMI_SERIAL = "01234567890"

# connect to the MQTT server
# connect to the MQTT broker
mqtt_client = temi.connect(MQTT_HOST, MQTT_PORT)

# create robot object
Expand All @@ -22,62 +22,54 @@
# --------------------------------------------------------------
# TEXT-TO-SPEECH COMMANDS
# --------------------------------------------------------------
# command the robot to speak
robot.tts("Going to the Entrance")
robot.tts("Going to the Entrance") # command the robot to speak
time.sleep(1) # wait some time for action to complete


# --------------------------------------------------------------
# WAYPOINT COMMANDS
# --------------------------------------------------------------
# command the robot to go to a saved location
robot.goto("entrance")
robot.goto("entrance") # command the robot to go to a saved location
time.sleep(1) # wait some time for action to complete


# --------------------------------------------------------------
# MOVE COMMANDS
# --------------------------------------------------------------
# tilt the robot's head to +55 degrees (absolute angle)
robot.tilt(+45)
time.sleep(3) # wait 3 seconds for action to complete
robot.tilt(+45) # tilt the robot's head (absolute angle)
time.sleep(3) # wait some time for action to complete

# tilt the robot's head to -15 degrees (absolute angle)
robot.tilt(-15)
time.sleep(3) # wait 3 seconds for action to complete
robot.tilt(-15) # tilt the robot's head (absolute angle)
time.sleep(3) # wait some time for action to complete

# tilt the robot's head to +30 degrees (relative angle)
robot.tilt_by(+30)
time.sleep(3) # wait 3 seconds for action to complete
robot.tilt_by(+30) # tilt the robot's head (relative angle)
time.sleep(3) # wait some time for action to complete

# tilt the robot's head to -10 degrees (relative angle)
robot.tilt_by(-10)
time.sleep(3) # wait 3 seconds for action to complete
robot.tilt_by(-10) # tilt the robot's head (relative angle)
time.sleep(3) # wait some time for action to complete

# rotate the robot by 90 degrees (relative angle)
robot.rotate(90)
time.sleep(5) # wait 5 seconds for action to complete
robot.rotate(90) # rotate the robot (relative angle)
time.sleep(5) # wait some time for action to complete

# rotate the robot by -30 degrees (relative angle)
robot.rotate(-30)
time.sleep(5) # wait 5 seconds for action to complete
robot.rotate(-30) # rotate the robot (relative angle)
time.sleep(5) # wait some time for action to complete


# --------------------------------------------------------------
# MEDIA COMMANDS
# --------------------------------------------------------------
# play YouTube video by passing in a video ID
robot.youtube("ZsEano0qwcg")
time.sleep(30) # wait 30 seconds before performing next action
robot.youtube("ZsEano0qwcg") # play YouTube video by passing in a YouTube video ID
time.sleep(30) # wait some time for action to complete

# play online video by passing a URL
robot.video(
"https://roboteam-assets.s3.eu-central-1.amazonaws.com/ui/skills/tutorials/videos/intorducing+temi.mp4"
)
time.sleep(30) # wait 30 seconds before performing next action
) # play online video by passing a URL
time.sleep(30) # wait some time for action to complete

# show webview by passing a URL
robot.webview("https://www.robotemi.com/")
time.sleep(5) # wait 5 seconds before performing next action
robot.webview("https://www.his.co.jp/en/")
time.sleep(5) # wait 5 seconds before performing next action
time.sleep(5)
robot.webview("https://hapi-robo.com/")
time.sleep(5) # wait 5 seconds before performing next action
time.sleep(5)
robot.webview("https://www.his.co.jp/en/")
time.sleep(5)
9 changes: 6 additions & 3 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,21 @@ def readme():
with open("README.md", "r") as f:
return f.read()

def requirements():
with open("requirements.txt", "r") as f:
return f.read().splitlines()

setuptools.setup(
name="temipy",
name="pytemi",
version="1.0.0",
description="MQTT bridge for temi",
description="A Python client that can be used with Connect MQTT bridge for temi.",
long_description=readme(),
long_description_content_type="text/markdown",
author="R. Oung",
author_email="r.oung@hapi-robo.com",
url="https://github.com/",
packages=setuptools.find_packages(),
install_requires=["paho-mqtt",],
install_requires=requirements(),
classifiers=[
"Programming Language :: Python :: 3",
"License :: OSI Approved :: MIT License",
Expand Down

0 comments on commit 1e6c730

Please sign in to comment.