Skip to content

Instantly share code, notes, and snippets.

@lvidarte
Created April 4, 2017 01:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lvidarte/65fb19793c6fe23f667d6e1baeb1b5de to your computer and use it in GitHub Desktop.
Save lvidarte/65fb19793c6fe23f667d6e1baeb1b5de to your computer and use it in GitHub Desktop.
ESP Car python shell
#!/usr/bin/python -i
import os
import atexit
import readline
import rlcompleter
import requests
import time
DEBUG = False
ESP_CAR_URL = 'http://192.168.4.1'
DIR_LEFT = 1
DIR_RIGHT = 2
DIR_REVERSE = 3
DIR_FORWARD = 4
DIR_STOP = 5
SLOW = 512
MEDIUM = 768
FAST = 1024
## History and autocomplete
historyPath = os.path.expanduser("~/.espcar-history")
readline.parse_and_bind('tab: complete')
def save_history(historyPath=historyPath):
readline.write_history_file(historyPath)
if os.path.exists(historyPath):
readline.read_history_file(historyPath)
atexit.register(save_history)
## ESP-CAR functions
def move(direction, speed, ms=None):
params = {'dir': direction, 'speed': speed}
r = requests.get(ESP_CAR_URL, params=params)
if DEBUG:
print(r.url)
if ms is not None:
time.sleep(ms / 1000.0)
def right(speed, ms=None):
move(DIR_RIGHT, speed, ms)
def left(speed, ms=None):
move(DIR_LEFT, speed, ms)
def forward(speed, ms=None):
move(DIR_FORWARD, speed, ms)
def reverse(speed, ms=None):
move(DIR_REVERSE, speed, ms)
def stop():
move(DIR_STOP, 0, None)
def demo():
forward(FAST, 1000)
right(FAST, 100)
forward(FAST, 500)
forward(MEDIUM, 500)
forward(SLOW, 500)
stop()
reverse(FAST, 1000)
right(FAST, 100)
forward(FAST, 1000)
left(FAST, 1000)
stop()
print """
Welcome to esp-car shell
Autocompletion and history are enabled
Functions:
demo()
forward(velocity, ms)
reverse(velocity, ms)
left(velocity, ms)
right(velocity, ms)
stop()
Velocity: FAST, MEDIUM, SLOW
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment