Last active
August 29, 2015 14:23
-
-
Save Pygmalion69/9e85ab8bb6ff06eab59d to your computer and use it in GitHub Desktop.
Pi Rover client, http://goo.gl/tRcYrB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import Tkinter as tk | |
from Tkinter import Button | |
from Tkinter import Scale | |
import json | |
import ast | |
import requests | |
'''Pi Rover client script | |
http://goo.gl/tRcYrB''' | |
__author__ = 'helfrich' | |
STOP = 0 | |
FORWARD = 1 | |
BACKWARD = 2 | |
FORWARD_LEFT = 3 | |
FORWARD_RIGHT = 4 | |
BACKWARD_LEFT = 5 | |
BACKWARD_RIGHT = 6 | |
ROTATE_LEFT = 7 | |
ROTATE_RIGHT = 8 | |
SPEED_LEFT = 9 | |
SPEED_RIGHT = 10 | |
class CommandData: | |
def __init__(self): | |
pass | |
command = "" | |
data = "" | |
class RoverStatus: | |
def __init__(self): | |
pass | |
speed = 90 | |
movement = STOP | |
command_data = CommandData() | |
rover_status = RoverStatus() | |
def send(): | |
rover_status.movement = int(command_data.command) | |
payload = (ast.literal_eval(json.dumps(command_data.__dict__))) | |
print(payload) | |
r = requests.post("http://192.168.2.12/send.php", data=payload) | |
print(r.text) | |
def stop(): | |
command_data.command = str(STOP) | |
command_data.data = rover_status.speed | |
send() | |
def forward(): | |
command_data.command = str(FORWARD) | |
command_data.data = rover_status.speed | |
send() | |
def backward(): | |
command_data.command = str(BACKWARD) | |
command_data.data = rover_status.speed | |
send() | |
def forward_left(): | |
command_data.command = FORWARD_LEFT | |
command_data.data = rover_status.speed | |
send() | |
def forward_right(): | |
command_data.command = FORWARD_RIGHT | |
command_data.data = rover_status.speed | |
send() | |
def backward_left(): | |
command_data.command = BACKWARD_LEFT | |
command_data.data = rover_status.speed | |
send() | |
def backward_right(): | |
command_data.command = BACKWARD_RIGHT | |
command_data.data = rover_status.speed | |
send() | |
def rotate_left(): | |
command_data.command = ROTATE_LEFT | |
command_data.data = rover_status.speed | |
send() | |
def rotate_right(): | |
command_data.command = ROTATE_RIGHT | |
command_data.data = rover_status.speed | |
send() | |
def speed_changed(speed): | |
rover_status.speed = speed | |
command_data.command = str(rover_status.movement) | |
command_data.data = rover_status.speed | |
send() | |
root = tk.Tk() | |
root.title("Pi Rover") | |
btnForward = Button(root, text="Forward", command=forward) | |
btnForward.grid(row=0, column=1, padx=10, pady=10) | |
btnForward.config(width=10) | |
# btnForward.pack() | |
btnStop = Button(root, text="Stop", command=stop) | |
btnStop.grid(row=1, column=1, padx=10, pady=10) | |
btnStop.config(width=10) | |
# btnStop.pack() | |
btnBack = Button(root, text="Back", command=backward) | |
btnBack.grid(row=2, column=1, padx=10, pady=10) | |
btnBack.config(width=10) | |
btnFwdLeft = Button(root, text="Fwd Left", command=forward_left) | |
btnFwdLeft.grid(row=0, column=0, padx=10, pady=10) | |
btnFwdLeft.config(width=10) | |
btnFwdRight = Button(root, text="Fwd Right", command=forward_right) | |
btnFwdRight.grid(row=0, column=2, padx=10, pady=10) | |
btnFwdRight.config(width=10) | |
btnBckLeft = Button(root, text="Bck Left", command=backward_left) | |
btnBckLeft.grid(row=2, column=0, padx=10, pady=10) | |
btnBckLeft.config(width=10) | |
btnBckRight = Button(root, text="Bck Right", command=backward_right) | |
btnBckRight.grid(row=2, column=2, padx=10, pady=10) | |
btnBckRight.config(width=10) | |
btnRotLeft = Button(root, text="Rot Left", command=rotate_left) | |
btnRotLeft.grid(row=1, column=0, padx=10, pady=10) | |
btnRotLeft.config(width=10) | |
btnRotRight = Button(root, text="Rot Right", command=rotate_right) | |
btnRotRight.grid(row=1, column=2, padx=10, pady=10) | |
btnRotRight.config(width=10) | |
scSpeed = Scale(root, from_=80, to=100, orient=tk.HORIZONTAL, command=speed_changed) | |
scSpeed.grid(row=3, column=0, columnspan=3, sticky=tk.W + tk.E + tk.N + tk.S, padx=10, pady=10) | |
root.grid_columnconfigure(1, weight=1) | |
root.mainloop() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment