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

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Raymond Oung committed Jul 17, 2020
0 parents commit 6dd8678
Show file tree
Hide file tree
Showing 10 changed files with 402 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
temipy/__pycache__/
temipy.egg-info/
venv/
test.py
19 changes: 19 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Copyright (c) 2020 Raymond Oung

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
54 changes: 54 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# temi Connect Python Package
Classes and functions to control temi using Python scripts.


## 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)
* 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:
```
git clone ...
```

Create a Python virtual environment (`venv`) and install all dependencies:
```
cd temipy/
./setup.sh
```

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


## Usage
Make sure temi is connected to an MQTT broker via the Connect app.

Sample Python script:
```
import temipy as temi
temi_serial = "01234567890"
# connect to the MQTT server
mqtt_client = temi.connect("test.mosquitto.org", 1883)
# create robot object
robot = temi.Robot(mqtt_client, "temi_serial")
# command the robot to speak
robot.tts("Going to the Entrance")
# command the robot to go to a saved location
robot.goto("entrance")
```
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
paho-mqtt
54 changes: 54 additions & 0 deletions sample.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""Sample script
"""
import temipy as temi


temi_serial = "01234567890"

# connect to the MQTT server
mqtt_client = temi.connect("test.mosquitto.org", 1883)

# create robot object
robot = temi.Robot(mqtt_client, "temi_serial")



command the robot to speak
robot.tts("Going to the Entrance")

command the robot to go to a saved location
robot.goto("entrance")


# play YouTube video by passing in a video ID
robot.youtube("ZsEano0qwcg")
time.sleep(10) # wait 10 seconds before performing next action


# tilt the robot's head to +55 degrees (absolute angle)
robot.tilt(+45)
time.sleep(3) # wait 3 seconds 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

# tilt the robot's head to +30 degrees (relative angle)
robot.tilt_by(+30)
time.sleep(3) # wait 3 seconds 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


# rotate the robot by 90 degrees (relative angle)
robot.rotate(90)
time.sleep(5) # wait 5 seconds 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
28 changes: 28 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import setuptools


def readme():
with open("README.md", "r") as f:
return f.read()


setuptools.setup(
name="temipy",
version="1.0.0",
description="MQTT bridge for temi",
long_description=readme(),
long_description_content_type="text/markdown",
author="R. Oung",
author_email="[email protected]",
url="https://github.com/",
packages=setuptools.find_packages(),
install_requires=[
"paho-mqtt",
],
classifiers=[
"Programming Language :: Python :: 3",
"License :: OSI Approved :: MIT License",
"Operating System :: Ubuntu, macOS",
],
python_requires=">=3.6",
)
28 changes: 28 additions & 0 deletions setup.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#!/bin/sh
#
# Auto-generate python virtual environments and install requirements.
#
# Usage
# ./setup.sh
#

VENV_PATH="venv"

# if path does not exist, generate a new virtual environment
if [ ! -d "${VENV_PATH}" ]; then
PYTHON=`which python3`

if [ ! -f "${PYTHON}" ]; then
echo "Could not find Python"
fi
virtualenv -p "${PYTHON}" "${VENV_PATH}"
fi

# activate the virtual environment
. "${VENV_PATH}/bin/activate"

# install requirements
pip install -r requirements.txt

# install private package in editing mode
pip install -e .
3 changes: 3 additions & 0 deletions temipy/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# -*- coding: utf-8 -*-
from .robot import Robot
from .connect import connect
66 changes: 66 additions & 0 deletions temipy/connect.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""MQTT Connection Helper Function
"""
from datetime import datetime

import paho.mqtt.client as mqtt
import socket
import time


def _on_connect(client, userdata, flags, rc):
"""Connect to MQTT broker and subscribe to topics
"""
print(
"[STATUS] Connected to: {} (rc:{})".format(
client._client_id.decode("ascii"), str(rc)
)
)


def _on_disconnect(client, userdata, rc):
"""Disconnect from MQTT broker
"""
print(
"[STATUS] Disconnected from: {} (rc:{})".format(
client._client_id.decode("ascii"), str(rc)
)
)
client.loop_stop()


def connect(host, port, username=None, password=None):
client_id = socket.gethostname() + "-" + datetime.now().strftime("%Y%m%d%H%M%S")

# create a new MQTT client instance
client = mqtt.Client(client_id=client_id)

# attach callbacks
client.on_connect = _on_connect
client.on_disconnect = _on_disconnect

# set username and password
if username and password:
client.username_pw_set(username=username, password=password)

# connect to MQTT broker
client.connect(host=host, port=port, keepalive=60, bind_address="")

# start listening to topics
client.loop_start()

# wait for client to connect
# TODO check using on_connect()
time.sleep(1)

return client


if __name__ == "__main__":
# connect to the MQTT server
mqtt_client = connect("test.mosquitto.org", 1883)
Loading

0 comments on commit 6dd8678

Please sign in to comment.