Table of Contents
Table of Contents
Python Libraries
All of our Python modules can be installed directly from the PIP Python repository (PiPy) simply by typing sudo pip install pi-plates from the command line. If you're installing for the first time then visit our Documentation page to get detailed instructions or execute the following sequence of commands from a terminal window:
1 2 3 4 5 |
sudo apt-get update sudo apt-get install python-pip sudo pip install pi-plates |
The above installs the Pi-Plate Python libraries into a folder located at /usr/local/lib/python2.7/dist-packages/piplates.
If you need to uninstall the library then simply type sudo pip uninstall pi-plates.
If you want to update a library, the cleanest approach is to run sudo pip uninstall pi-plates from the command line followed by sudo pip install pi-plates.
We also have a set of Python 3 modules that can be downloaded with sudo pip3 install Pi-Plates
ppLOGGER
Go Here to read all about how to download and use our free data logger application, ppLOGGER.
Examples
In addition to the code below, you can find a number of code snippets in our documentation.
This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License.
Object Oriented MOTORplate Demo Using Tkinter
Here is an object oriented Tkinter program we wrote to drive two stepper motors and four DC motors using two MOTORplates. The code has classes for DC motors, stepper motors and the title block. When run, the program puts up the following display:
We wrote this program to provide an easy method to demonstrate all of the capabilities of the MOTORplate as well as to teach ourselves object oriented programming with Python and Tkinter. You can copy and paste the code below or install it directly to your Raspberry Pi using these steps from the command line:
- Download the files from our server with: sudo wget https://pi-plates.com/downloads/MOTORdemo.tar.gz
- Then decompress them into your home directory: sudo tar -xzvf MOTORdemo.tar.gz
The program assumes two MOTORplates attached to the stack at addresses 0 and 1. In our demo, we had two stepper motors connected to the board at address 0 and four DC motors driven by the board at address 1. Besides learning teaching ourselves the basics of object oriented programming, we also learned about manipulating font sizes with the tkFont module. For example, we wanted a large title so we created a bold, 40 point version of the built in Helvetica font with: self.title = tkFont.Font(family='Helvetica', size=40, weight='bold')
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 |
import piplates.MOTORplate as MOTOR import time import os import string from Tkinter import * import tkFont class titleBLOCK: def __init__(self,master,r,c): self.master=master self.tit=Frame(self.master,padx=4,pady=4,bd=2,bg='white',relief='sunken') self.tit.grid(row=r,column=c, columnspan=2,sticky=N+S+W+E) ##Create Fonts self.title = tkFont.Font(family='Helvetica', size=40, weight='bold') self.heading = tkFont.Font(family='Helvetica', size=18, weight='bold') self.logo = PhotoImage(file = '3D-ppLogo.gif') self.pp=Label(self.tit,image=self.logo,anchor="center").grid() self.labelt = Label(self.tit, text="MOTORplate Controller", bg='white',fg='#000666000',padx=4,pady=4,font=self.title,anchor="center") self.labelt.grid(sticky=W+E) #self.pp.bind("<ButtonRelease-1>", self.shutdown) self.close_button = Button(self.tit, text="X", command=root.quit).grid(sticky=E) def shutdown(self,event): print "clicked at", event.x, event.y class stepperGUI: def __init__(self,master,title,stepper,r,c): self.stepState=0 #stopped smColor="yellow" slwidth=30 slength=120 self.mVal=stepper self.master=master self.sm=Frame(self.master,padx=4,pady=4,bd=2,relief='sunken') self.sm.grid(row=r,column=c, rowspan=3, sticky=N+S+W+E) ##Create Fonts self.title = tkFont.Font(family='Helvetica', size=25, weight='bold') self.heading = tkFont.Font(family='Helvetica', size=18, weight='bold') ##Title self.labelt = Label(self.sm, text=title, padx=4,pady=4, bg=smColor, fg='black',font=self.title) self.labelt.grid(row=0,column=0,columnspan=2,sticky=W+E) ##Step Rate Control self.labels = Label(self.sm, text="Step Rate",padx=4,pady=4,font=self.heading) self.labels.grid(row=1,column=0) self.rate=IntVar() self.rate.set(0) self.RateSet=Scale(self.sm,variable=self.rate,from_=2000,to=0,width=slwidth,length=slength) self.RateSet.bind("<ButtonRelease-1>", self.ratedelta) self.RateSet.grid(row=2,column=0,rowspan=2) ##Direction Control self.labeld = Label(self.sm, text="Direction",padx=4,pady=4,font=self.heading) self.labeld.grid(row=1,column=1,sticky=W) self.direction = IntVar() self.direction.set(0) self.cwb = Radiobutton(self.sm, text='Clockwise', variable=self.direction, value=0) self.ccwb = Radiobutton(self.sm, text=' Counter Clockwise', variable=self.direction, value=1) self.direction.set(0) self.cwb.grid(row=2,column=1,sticky=W) self.ccwb.grid(row=3,column=1,sticky=W) ##Acceleration Control self.labela = Label(self.sm, text="Acceleration",padx=4,pady=4,font=self.heading) self.labela.grid(row=4,column=0) self.acc=DoubleVar() self.acc.set(0.0) self.accSet=Scale(self.sm,variable=self.acc,from_=5.0,to=0.0, resolution=0.1,width=slwidth,length=slength) self.accSet.grid(row=5,column=0,rowspan=4) ##Step Size Control rStart = 4 self.labeld = Label(self.sm, text="Step Size",padx=4,pady=4,font=self.heading) self.labeld.grid(row=rStart,column=1,sticky=W) rStart+=1 self.ss = IntVar() self.ss.set(2) # initializing the choice: Full sizes = [ ("Full",0), ("Half",1), ("1/4 - microstep",2), ("1/8 - microstep",3) ] for txt, val in sizes: Radiobutton(self.sm, text=txt, variable=self.ss, value=val).grid(row=rStart+val,column=1,sticky=W) ##Step Count Control self.labelstep = Label(self.sm, text="Step Count",padx=4,pady=4,font=self.heading) self.labelstep.grid(row=9,column=0) self.steps=IntVar() self.steps.set(0) self.StepSet=Scale(self.sm,variable=self.steps,from_=2000,to=0,width=slwidth,length=slength) self.StepSet.grid(row=10,column=0,rowspan=3) ##Jog Button self.startButton=Button(self.sm,text="JOG",fg="black",bg="green",height=3, width=6,command=self.jog) self.startButton.grid(row=10,column=1) ##Move Button self.startButton=Button(self.sm,text="MOVE",fg="white",bg="blue",height=3, width=6,command=self.move) self.startButton.grid(row=11,column=1) ##Stop Button self.stopButton=Button(self.sm,text="STOP",fg="white",bg="red",height=3, width=6,command=self.stop) self.stopButton.grid(row=12,column=1) ##Off Button self.stopButton=Button(self.sm,text="OFF",fg="black",bg="yellow",height=3, width=6,command=self.off) self.stopButton.grid(row=13,column=1) self.stop() #ensure motor is off at start self.off() #ensure motor is off at start def ratedelta(self,val): if (self.stepState==1): MOTOR.stepperRATE(0,self.mVal,self.rate.get()) def jog(self): if (self.direction.get() == 0): dir='cw' else: dir='ccw' MOTOR.stepperCONFIG( 0,self.mVal,dir,self.ss.get(),self.rate.get(),self.acc.get()) MOTOR.stepperJOG(0,self.mVal) self.stepState=1 def move(self): if (self.direction.get() == 0): dir='cw' else: dir='ccw' MOTOR.stepperCONFIG(0,self.mVal,dir,self.ss.get(),self.rate.get(),self.acc.get()) MOTOR.stepperMOVE(0,self.mVal,self.steps.get()) self.stepState=2 def stop(self): MOTOR.stepperSTOP(0,self.mVal) self.stepState=0 def off(self): MOTOR.stepperOFF(0,self.mVal) self.stepState=0 class dcGUI: def __init__(self,master,title,mN,r,c): self.dcState=0 #stopped dcColor="red" slwidth=30 self.mNum=mN self.master=master self.dcm=Frame(master,padx=4,pady=4,bd=2,relief='sunken') self.dcm.grid(row=r,column=c, columnspan=1, rowspan=2, sticky=N+S+W+E) ##Font Setup self.title = tkFont.Font(family='Helvetica', size=25, weight='bold') self.heading = tkFont.Font(family='Helvetica', size=18, weight='bold') ##Title self.labelt = Label(self.dcm, text=title, padx=4,pady=4, fg="white", bg=dcColor, font=self.title) self.labelt.grid(row=0,column=0,columnspan=2,sticky=W+E) #Speed Control self.labels = Label(self.dcm, text="Speed",padx=4,pady=4,font=self.heading) self.labels.grid(row=1,column=0) self.speed=IntVar() self.speed.set(0) self.SpeedSet=Scale(self.dcm,variable=self.speed,from_=100,to=0,width=slwidth) self.SpeedSet.bind("<ButtonRelease-1>", self.speeddelta) self.SpeedSet.grid(row=2,column=0,rowspan=2) ##Direction Control self.labeld = Label(self.dcm, text="Direction",padx=4,pady=4,font=self.heading) self.labeld.grid(row=1,column=1,sticky=W) self.direction = IntVar() self.direction.set(0) self.cwb = Radiobutton(self.dcm, text='Clockwise', variable=self.direction, value=0) self.ccwb = Radiobutton(self.dcm, text=' Counter Clockwise', variable=self.direction, value=1) self.direction.set(0) self.cwb.grid(row=2,column=1,sticky=W) self.ccwb.grid(row=3,column=1,sticky=W) ##Acceleration Control self.labela = Label(self.dcm, text="Acceleration",padx=4,pady=4,font=self.heading) self.labela.grid(row=4,column=0) self.acc=DoubleVar() self.acc.set(0.0) self.accSet=Scale(self.dcm,variable=self.acc,from_=5.0,to=0.0, resolution=0.1,length=100,width=slwidth) self.accSet.grid(row=5,column=0,rowspan=2) ##RPM Display self.rpm=StringVar() self.rpm.set('0') self.labelt = Label(self.dcm, text="RPM",padx=4,pady=4,font=self.heading) self.labelt.grid(row=7,column=0,columnspan=2) self.labelt = Label(self.dcm, textvariable=self.rpm ,padx=4,pady=4,font=self.heading) self.labelt.grid(row=8,column=0,columnspan=2) ##Start Button self.startButton=Button(self.dcm,text="GO",fg="black",bg="green",height=3, width=6,command=self.go) self.startButton.grid(row=5,column=1) ##Stop Button self.stopButton=Button(self.dcm,text="STOP",fg="white",bg="red",height=3, width=6,command=self.stop) self.stopButton.grid(row=6,column=1) self.UpdateRPM() def go(self): if (self.direction.get() == 0): dir='cw' else: dir='ccw' MOTOR.dcCONFIG(1,self.mNum,dir,self.speed.get(),self.acc.get()) MOTOR.dcSTART(1,self.mNum) self.dcState=1 def stop(self): MOTOR.dcSTOP(1,self.mNum) self.dcState=0 def speeddelta(self,val): #print self.dcState,self.speed.get() if (self.dcState): MOTOR.dcSPEED(1,self.mNum,self.speed.get()) def UpdateRPM(self): t=MOTOR.getTACHfine(1,self.mNum) self.rpm.set(str(t*60/300)) self.dcm.after(1000,self.UpdateRPM) MOTOR.RESET(0) MOTOR.RESET(1) root = Tk() root.config(bg="black") root.attributes("-fullscreen", True) swidth=root.winfo_screenwidth() sheight=root.winfo_screenheight() #print tkFont.families() container=Frame(root,bg="white") container.place(relx=0.5, rely=0.5, anchor=CENTER) ma_gui = stepperGUI(container,"Stepper Motor A",'a',1,1) mb_gui = stepperGUI(container,"Stepper Motor B",'b',1,2) m1_gui = dcGUI(container,"DC Motor 1",1,0,0) m2_gui = dcGUI(container,"DC Motor 2",2,2,0) m3_gui = dcGUI(container,"DC Motor 3",3,0,3) m4_gui = dcGUI(container,"DC Motor 4",4,2,3) title = titleBLOCK(container,0,1) root.mainloop() |