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

Commit

Permalink
Cleans up code and updates READMEs
Browse files Browse the repository at this point in the history
  • Loading branch information
Raymond Oung committed Aug 30, 2021
1 parent 0fbc421 commit 50ca857
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 113 deletions.
22 changes: 4 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,5 @@
# pytemi
Control temi using Python scripts over MQTT.


## Prerequisites
* [Python 3](https://www.python.org/downloads/)
* [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)
A Python package for controlling temi over MQTT. To be used with [temi-mqtt-client](https://github.com/hapi-robo/temi-mqtt-client/). For prototyping/development only.


## Setup
Expand All @@ -24,15 +15,10 @@ For Linux users, there's a script that will create a Python virtual environment


## Usage
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:
```
python sample.py
```
Make sure the robot is connected to an MQTT broker via the [temi-mqtt-client](https://github.com/hapi-robo/temi-mqtt-client/) app.


## Sample Script
### Sample Script
```
import pytemi as temi
Expand All @@ -53,4 +39,4 @@ robot.tts("Going to the Entrance")
robot.goto("entrance")
```

See `sample.py` for more details.
See `./sample.py` for more details. Other examples can be found in the `scripts/` folder.
24 changes: 24 additions & 0 deletions pytemi/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# MQTT Topics
The following is a summary of MQTT topics used by this package.

## Publish
```
temi/{id}/command/move/turn_by
temi/{id}/command/move/joystick
temi/{id}/command/move/tilt
temi/{id}/command/move/tilt_by
temi/{id}/command/move/stop
temi/{id}/command/follow/unconstrained
temi/{id}/command/waypoint/goto
temi/{id}/command/tts
temi/{id}/command/media/video
temi/{id}/command/media/webview
```

## Subscribe
```
temi/{id}/status/info
temi/{id}/status/utils/battery
temi/{id}/event/waypoint/goto
temi/{id}/event/user/detection
```
73 changes: 10 additions & 63 deletions pytemi/robot.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,9 @@ def __init__(self, mqtt_client, temi_serial, silent=True):

# attach subscription callbacks
self.client.message_callback_add("temi/{}/status/info".format(temi_serial), _on_status)
# self.client.message_callback_add("temi/{}/status/utils/battery".format(temi_serial), _on_battery)
self.client.message_callback_add("temi/{}/status/utils/battery".format(temi_serial), _on_battery)
self.client.message_callback_add("temi/{}/event/waypoint/goto".format(temi_serial), _on_goto)
# self.client.message_callback_add("temi/{}/event/user/detection".format(temi_serial), _on_user)

self.client.message_callback_add("temi/{}/event/user/detection".format(temi_serial), _on_user)

def rotate(self, angle):
"""Rotate
Expand All @@ -79,7 +78,6 @@ def rotate(self, angle):

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


def translate(self, value):
"""Translate
Expand All @@ -92,24 +90,6 @@ def translate(self, value):

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


# def translate(self, value):
# """Translate

# """
# if not self.silent:
# print("[CMD] Translate: {} [unitless]".format(value))

# 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


def tilt(self, angle):
"""Tilt head (absolute angle)
Expand Down Expand Up @@ -203,17 +183,17 @@ def video(self, url):

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

def youtube(self, video_id):
"""Play YouTube
# def youtube(self, video_id):
# """Play YouTube

"""
if not self.silent:
print("[CMD] Play YouTube: {}".format(video_id))
# """
# if not self.silent:
# print("[CMD] Play YouTube: {}".format(video_id))

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

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

def webview(self, url):
"""Show webview
Expand All @@ -227,39 +207,6 @@ def webview(self, url):

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

def app(self, package_name):
"""Start Android app
"""
if not self.silent:
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.id + "/command/call/start"
# payload = json.dumps({"room_name": room_name})

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

# def hangup(self):
# """End a call

# """
# print("[CMD] Hangup")

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

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

@property
def locations(self):
"""Return a list of locations
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
python-dotenv
paho-mqtt
inputs
40 changes: 16 additions & 24 deletions sample.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
"""
import pytemi as temi

from time import sleep
import time


# parameters
Expand All @@ -23,54 +22,47 @@
# --------------------------------------------------------------
# TEXT-TO-SPEECH COMMANDS
# --------------------------------------------------------------
robot.tts("Going to the Entrance") # command the robot to speak
robot.tts("Hello World!") # command the robot to speak
time.sleep(1) # wait some time for action to complete


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


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

robot.tilt(-15) # tilt the robot's head (absolute angle)
time.sleep(3) # wait some time for action to complete
# robot.tilt(-15) # tilt the robot's head (absolute angle)
# time.sleep(3) # wait some time 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
# robot.tilt_by(+30) # tilt the robot's head (relative angle)
# time.sleep(3) # wait some time 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
# robot.tilt_by(-10) # tilt the robot's head (relative angle)
# time.sleep(3) # wait some time for action to complete

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

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


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

robot.video(
"https://roboteam-assets.s3.eu-central-1.amazonaws.com/ui/skills/tutorials/videos/intorducing+temi.mp4"
) # 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)
robot.webview("https://hapi-robo.com/")
time.sleep(5)
robot.webview("https://www.his.co.jp/en/")
robot.webview("https://www.google.com/")
time.sleep(5)
10 changes: 5 additions & 5 deletions scripts/README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# pytemi/scripts
Add a `.env` into this folder with the MQTT server parameters:
Add a `.env` file to this folder with the MQTT server parameters:
```
# MQTT server parameters
MQTT_HOST="HOSTNAME"
MQTT_PORT="PORT"
MQTT_USERNAME="USERNAME"
MQTT_PASSWORD="PASSWORD"
MQTT_HOST="<mqtt-broker-hostname>"
MQTT_PORT="<mqtt-broker-port>"
MQTT_USERNAME="<mqtt-broker-username>"
MQTT_PASSWORD="<mqtt-broker-password>"
```
2 changes: 1 addition & 1 deletion scripts/goto_random.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@


# robot parameters
TEMI_SERIAL = "00119260058" # tony
TEMI_SERIAL = "00119260058"

BATTERY_THRESHOLD_LOW = 20 # [%]
BATTERY_THRESHOLD_CHARGED = 90 # [%]
Expand Down
3 changes: 1 addition & 2 deletions scripts/joystick.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@


# robot parameters
# TEMI_SERIAL = "00119260058" # tony
TEMI_SERIAL = "00119140088" # gary
TEMI_SERIAL = "00119260058"

# MQTT server parameters
MQTT_HOST = os.getenv("MQTT_HOST")
Expand Down

0 comments on commit 50ca857

Please sign in to comment.