Skip to content

Instantly share code, notes, and snippets.

@Robotonics
Last active December 11, 2016 01:01
Show Gist options
  • Save Robotonics/cda77feb106bd08b89a0a6cac2489401 to your computer and use it in GitHub Desktop.
Save Robotonics/cda77feb106bd08b89a0a6cac2489401 to your computer and use it in GitHub Desktop.
#--------------------------------------------------
# Python program to interact with and
# control a MakeBlock Robot.
# Author: David Cotterill-Drew 2016
# Email: [email protected]
#--------------------------------------------------
# Imports
#--------------------------------------------------
from bluepy.btle import *
from Tkinter import *
import Tkinter as tk
from tkFileDialog import askopenfilename
from PIL import Image, ImageTk
root = tk.Tk()
#-------------------------------------------------
# Global variables
#-------------------------------------------------
data2=0
conn = Peripheral("00:0D:19:12:3B:38")
dataflag=0
#-------------------------------------------------
# Speed & Range Frames
#-------------------------------------------------
framespeed = Frame(root,width=600,height=1000)
framespeed.grid(row=1, column=1, padx=10, pady=10)
framerange = Frame(root,width=600,height=1000)
framerange.grid(row=0, column=1, padx=10, pady=10)
#-------------------------------------------------
# Range Data & Label
#-------------------------------------------------
dattext = Label(framerange, text="RANGE cms",fg='black')
dattext.grid(row=0, column=0, padx=10, pady=5)
dat = Label(framerange, text=data2,fg='red',bg='white')
dat.grid(row=2, column=0, padx=0, pady=0)
#-------------------------------------------------
# Handle 0x000e notification
#-------------------------------------------------
class NotificationHandler(DefaultDelegate):
def handleNotification(self, cHandle, data):
data2=ord(data)
dat.configure(text=data2)
#-------------------------------------------------
# Speed Slider
#-------------------------------------------------
speed = IntVar()
speed = Scale(framespeed, from_=1, to=9,orient=HORIZONTAL, variable=speed, fg='red')
speed.grid(row=1, column=0, padx=10, pady=0)
#-------------------------------------------------
# Direction & Arm/Grip Frames
#-------------------------------------------------
frame = Frame(root,width=600,height=1000)
frame.grid(row=0, column=0, padx=10, pady=10)
framearm = Frame(root,width=600,height=1000)
framearm.grid(row=1, column=0, padx=10, pady=10)
#-------------------------------------------------
# More Labels
#-------------------------------------------------
nav= Label(frame, text="DIRECTION",fg='black')
nav.grid(row=0, column=1, padx=0, pady=10)
arm= Label(framearm, text="ARM",fg='black')
arm.grid(row=0, column=1, padx=0, pady=0)
grip= Label(framearm, text="GRIP",fg='black')
grip.grid(row=0, column=0, padx=0, pady=0)
speed1= Label(framespeed, text="SPEED",fg='black')
speed1.grid(row=0, column=0, padx=0, pady=0)
#------------------------------------------------
# Title
#------------------------------------------------
root.wm_title("Bluetooth MakeBlock")
#------------------------------------------------
# Photo's
#------------------------------------------------
photo1 = tk.PhotoImage(file="rsz_left.png")
photo2 = tk.PhotoImage(file="rsz_right.png")
photo3 = tk.PhotoImage(file="rsz_up.png")
photo4 = tk.PhotoImage(file="rsz_down.png")
photo5 = tk.PhotoImage(file="rsz_stop.png")
photo6 = tk.PhotoImage(file="enter.png")
#------------------------------------------------
# Menu
#------------------------------------------------
menu = Menu(root)
root.config(menu=menu)
filemenu = Menu(menu)
helpmenu = Menu(menu)
def About():
print "MakeBlock Bluetooth Robot Control"
def Settings():
print "will write it soon"
filemenu.add_command(label="Exit", command=root.quit)
menu.add_cascade(label="Help", menu=helpmenu)
helpmenu.add_command(label="About...", command=About)
helpmenu.add_command(label="Settings...", command=Settings)
#-------------------------------------------------
# Control Functions
#-------------------------------------------------
def forward():
conn.writeCharacteristic(0x0011, '\x66', False)
conn.disconnect
def back():
conn.writeCharacteristic(0x0011, '\x62', False)
conn.disconnect
def right():
conn.writeCharacteristic(0x0011, '\x72', False)
conn.disconnect
def left():
conn.writeCharacteristic(0x0011, '\x6C', False)
conn.disconnect
def stop():
conn.writeCharacteristic(0x0011, '\x73', False)
conn.disconnect
def Holder_Up():
conn.writeCharacteristic(0x0011, '\x61', False)
conn.disconnect
def Holder_Down():
conn.writeCharacteristic(0x0011, '\x63', False)
conn.disconnect
def Hand_open():
conn.writeCharacteristic(0x0011, '\x64', False)
conn.disconnect
def Hand_close():
conn.writeCharacteristic(0x0011, '\x65', False)
conn.disconnect
def speed_1():
conn.writeCharacteristic(0x0011, '\x31', False)
conn.disconnect
def speed_2():
conn.writeCharacteristic(0x0011, '\x32', False)
conn.disconnect
def speed_3():
conn.writeCharacteristic(0x0011, '\x33', False)
conn.disconnect
def speed_4():
conn.writeCharacteristic(0x0011, '\x34', False)
conn.disconnect
def speed_5():
conn.writeCharacteristic(0x0011, '\x35', False)
conn.disconnect
def speed_6():
conn.writeCharacteristic(0x0011, '\x36', False)
conn.disconnect
def speed_7():
conn.writeCharacteristic(0x0011, '\x37', False)
conn.disconnect
def speed_8():
conn.writeCharacteristic(0x0011, '\x38', False)
conn.disconnect
def speed_9():
conn.writeCharacteristic(0x0011, '\x39', False)
conn.disconnect
def RangeOn():
conn.setDelegate(NotificationHandler())
conn.writeCharacteristic(0x0011, '\x6d', False)
conn.waitForNotifications(1)
conn.disconnect
def RangeOff():
conn.writeCharacteristic(0x0011, '\x6e', False)
dat.configure(text="Range Off")
conn.disconnect
#--------------------------------------------------
# Slider Function
#--------------------------------------------------
def getsliderval():
if (speed.get()==1):
speed_1()
elif (speed.get()==2):
speed_2()
elif (speed.get()==3):
speed_3()
elif (speed.get()==4):
speed_4()
elif (speed.get()==5):
speed_5()
elif (speed.get()==6):
speed_6()
elif (speed.get()==7):
speed_7()
elif (speed.get()==8):
speed_8()
elif (speed.get()==9):
speed_9()
#---------------------------------------------------
# Buttons
#---------------------------------------------------
# Direction Buttons
#---------------------------------------------------
forwardbutton = Button(frame, text="Forward", command=forward, image=photo3)
forwardbutton.grid(row=1, column=1, padx=0, pady=5)
backbutton = Button(frame, text="Reverse",command=back, image=photo4)
backbutton.grid(row=3, column=1, padx=0, pady=5)
rightbutton = Button(frame, text="Right", command =right, image=photo2)
rightbutton.grid(row=2, column=2, padx=0, pady=0)
leftbutton = Button(frame, text="Left", command=left,image=photo1)
leftbutton.grid(row=2, column=0, padx=0, pady=0)
stopbutton = Button(frame, text="Stop", command=stop, image=photo5)
stopbutton.grid(row=2, column=1, padx=0, pady=0)
#----------------------------------------------------
# Arm Buttons
#----------------------------------------------------
HolderUpbutton = Button(framearm,text="Up",command=Holder_Up,image=photo3)
HolderUpbutton.grid(row=1, column=1, padx=10, pady=5)
HolderDownbutton = Button(framearm,text="Down",command=Holder_Down, image=photo4)
HolderDownbutton.grid(row=3, column=1, padx=10, pady=5)
stopbutton2 = Button(framearm, text="Stop", command=stop, image=photo5)
stopbutton2.grid(row=2, column=1, padx=10, pady=5)
#----------------------------------------------------
# Grip Buttons
#----------------------------------------------------
HandClosebutton = Button(framearm, text="Close",command=Hand_close,image=photo4)
HandClosebutton.grid(row=3, column=0, padx=10, pady=5)
HandOpenbutton = Button(framearm,text="Open",command=Hand_open,image=photo3)
HandOpenbutton.grid(row=1, column=0, padx=10, pady=5)
stopbutton3 = Button(framearm, text="Stop", command=stop, image=photo5)
stopbutton3.grid(row=2, column=0, padx=10, pady=5)
#----------------------------------------------------
# Speed Set Button
#----------------------------------------------------
setspeed = Button(framespeed, text="ENTER", command=getsliderval,bg='#0066cc',fg='white')
setspeed.grid(row=2, column=0, padx=10, pady=10)
#----------------------------------------------------
# Range Button
#----------------------------------------------------
rangeonbutton = Button(framerange, text="Range On", command=RangeOn, repeatinterval=5, repeatdelay=5,bg='#0066cc',fg='white')
rangeonbutton.grid(row=1, column=0, padx=10, pady=10)
rangeoffbutton = Button(framerange, text="Range Off", command=RangeOff,bg='#0066cc',fg='white')
rangeoffbutton.grid(row=3, column=0, padx=10, pady=10)
root.mainloop()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment