You are on page 1of 11

CreatingaMatlabGUIforDCMOTORS

TableofContents
1 Objective 1.1 Credits 1.2 GeneralComment(s) TestingyourMotorswithMATLAB 2.2.1 SetuptheGyroforMATLAB 2.3 Step3:Uploadthesrv_gyro.pdeServer 2.3.1 HowitWorks 2.4 Step4:Addarduino.mtoMATLABPath 2.4.1 HowitWorks 2.5 Step5:InstantiatetheMATLABArduinoClass 2.5.1 FindingtheSerialPort 2.6 Step6MatlabMotorLab UsingtheMATLABGUI 3.1 Help LabDeliverable(s) FutureWork

3 4 5

Objective

InthislabyouwillimplementaMATLABGUItocontrolyourDCmotorsandthestepper motor.Specifically,youwillcreateacontrolpanelwithtwoslidersandbuttons.Asyou increase(moveup)theslidersyouwillbeabletoseethespeedofyourDCmotors increase.Youwillalsobeabletocontrolthesteppermotorwithpushbuttonsforleftand rightpanning.Thesesimplecontrolsallowyoutomanuallytestthemotorsofyourrover andhavealittlefundrivingitaround. Forthislabyouwillneedthefollowingmaterials: AssembledRoverfromBuild Instructions USBCable MATLABSoftware Version2008orhigher

Thislabassumesthatyouhavealreadydownloadedandinstalledthe... ArduinoIDE AVRStudio4

1.1

Credits

ThislabwasoriginallywrittenbyJuanCadenaandElizabethSandoval(Veterans)during theFall2011semester.Additionalcontributor'sincludeKevinNguyen(GyroneersS11)and
NicFlenghi(RangeRoverF11).Thislatestrevisionreflectsthecommentsprovidedbythe Spring2011class.

1.2

GeneralComment(s)

Whenindoubt,closeeverythingandunplugeverythingtostartfromsquareone.

TestingyourMotorswithMATLAB

InstructionalVideo http://www.mathworks.com/company/events/webinars/wbnr43537.html 2

2.1 2.2

Step1:CheckSoftwareVersion Step2:DownloadFiles

TocompletethislabyoumusthaveMATLAB2008orhigher.

For the s r v . p d e sketch to compile correctly, the Adafruit Motor Shield library must first be installed.DownloadthelibraryfromAdafruit,

Extract the files, and place them in a new folder (AFMotor) in the Arduino libraries folder (usually: My Documents\Arduino\libraries\AFMotor), as per the instructions at LadyAda.Youcan findthe

The MATLAB support packageis required for serial communications with the Arduino,which is availableatMathWorkswebsite(usetheblueDownloadAllbuttonatthetopright).

To startcommunicatingwiththeArduino,thegeneralserver files r v . p d e withinthesrvdirectory

needstobeuploadedtotheArduinousingtheArduinoIDE. This labdoesnotrequiresupportforthegyroonyourrover.If youdonotwanttoaddthisfeature nowyoucanskipthenextstepandworkwiththegenericsrv.pdesketch.

2.2.1 SetuptheGyroforMATLAB
For the s r v _ g y r o . p d e sketch to compile correctly, thePololu L3G4200Dlibrarymustfirstbe installed.DownloadthelibraryfromPololu.

NOTE:Makesuretodownloadeveryfile,notjustthe.hfiles,tothelibraryfolderfromthe abovelinks.
DownloadandExtractthefilesintheG y r o _ m a t l a b _ c o d e . z i p filesincludedinthisfolder.The extractedfoldershouldincludethefollowingfiles. 1. s r v _ g y r o . p d e 2. i n s t a l l _ a r d u i n o . m 3. a r d u i n o . m 4. g y r o _ t e s t . m Note:Whilesomeofthefilesnamesinthefilesdownloadedhavethesamenamesasthose foundatMathworksNichasmodifiedthemforourclass.

2.3

Step3:Uploadthes r v _ g y r o . p d e Server

IfyouhaveneveruploadedanArduinosketch(i.e.,program),Iwouldhighlyrecommendyou completetheLesson1:Lettherebeblink!ArduinoTutorialfoundhere. Uploads r v _ g y r o . p d e (ors r v . p d e ifyouskippedstep2.1.1)toyourArduino.TheAdafruit motorlibraryandPololuL3G4200(s r v _ g y r o . p d e o n l y )mustbeinstalledforthistocompile. Thes r v _ g y r o . p d e programcontainsallofthecommandsnecessarytorunthemotors, read/writetoports,andreadfromourgyro.ThisArduinoprogramactsasaservertointerface withMATLAB.

2.3.1 HowitWorks
MATLAB(ortheserialmonitorintheArduinoIDE)sendsanalphanumericcodetoindicatewhich

commandwewanttorunandthenlistensfortheresultsentovertheUSBport.Thelistof commandscanbefoundatthetopofthes r v _ g y r o . p d e file.

2.4

Step4:Adda r d u i n o . m toMATLABPath

Addthemodifieda r d u i n o . m filetoyourMATLABpath.Youmaydothismanually,orbyrunning i n s t a l l _ a r d u i n o . m .YoucanfindhelponaddingafoldertotheMatlabsearchpathhere.

2.4.1 HowitWorks
Thea r d u i n o . m filetakescareofsendingandreceivingcommandsanddatafromtheArduino server(s r v _ g y r o . p d e ).ItalsogivesusacollectionofMATLABfunctionstouseinsteadof manuallyenteringthealphanumericcodes.Forexample,a . g y r o R e a d ( z ) sendsthecode G z overtheserialinterfaceandlistensforthevaluesentbackfromtheboard. Ifyouarerunningsrv_gyro.pdemakesuretousethearduino.mfiledownloadedwiththis software.

2.5

Step5:InstantiatetheMATLABArduinoClass

Note:TheMATLABMotorServerfoldermustbeopenwhenperformingcommandssuchas instantiatingtheArduino(Figure7).

Figure7MATLABMotorServerfolder Onces r v _ g y r o . p d e isuploadedtotheboard,thecodetoinstantiatetheArduinoobjectis: a = a r d u i n o ( ' C O M X ' ) WhereC O M X istheporttheArduinoisconnectedto(thiscanbedeterminedusingtheArduino IDEorDeviceManager).

2.5.1 FindingtheSerialPort
5

ThesimplestwaytofindoutwhichUSBport(comport)yourArduinoisconnectedtoisfromthe ArduinoIDE.IfyouarenewtotheArduinoIDEIwouldrecommendcompletingtheLesson1:Let therebeblink!ArduinoTutorialfoundhere.FindingtheUSBportofanArduinoUNOonWindows 7isalsofairlyeasy:StartDevicesandPrinters.

2.6

Step6MatlabMotorLab

DownloadtheDCMotorfolderandplaceitinaplacewhereyoucanaccessitthrough Matlab(i.e.,thecurrentorworkingdirectory). MATLABshouldshowitasfollows:

OpenMATLABandrunthea r d u i n o . m file.

Youshouldgetthefollowingwindowoncearduino.mfileisrun

TypethefollowinglineinthecommandlinetomakesuretheArduinoandMATLABare talkingtoeachother.InmycasemyArduinoisonportCOM14(makesureyouknowwhich COMyyourarduinoisusing).MakesuretoreplaceCOM14withyyourcorresponding COM#. E D U > > a = a r d u i n o ( ' C O M 1 4 ' ) Youshouldseethefollowingoutput.


E D U > > a = a r d u i n o ( ' C O M 1 4 ' ) A t t e m p t i n g c o n n e c t i o n . . . . . . G e n e r a l S c r i p t d e t e c t e d ! A r d u i n o s u c c e s s f u l l y c o n n e c t e d ! a = a r d u i n o o b j e c t c o n n e c t e d t o C O M 1 4 p o r t G e n e r a l S h i e l d S e r v e r r u n n i n g o n t h e a r d u i n o b o a r d S e r v o 1 i s D E T A C H E D S e r v o 2 i s D E T A C H E D T h e s p e e d o f m o t o r n u m b e r 1 i s s e t t o : 0 o v e r 2 5 5 T h e s p e e d o f m o t o r n u m b e r 2 i s s e t t o : 0 o v e r 2 5 5 T h e s p e e d o f m o t o r n u m b e r 3 i s s e t t o : 0 o v e r 2 5 5 T h e s p e e d o f m o t o r n u m b e r 4 i s s e t t o : 0 o v e r 2 5 5 T h e s p e e d o f s t e p p e r n u m b e r 1 i s s e t t o : 0 o v e r 2 5 5 T h e s p e e d o f s t e p p e r n u m b e r 2 i s s e t t o : 0 o v e r 2 5 5 S e r v o M e t h o d s : s e r v o S t a t u s s e r v o A t t a c h s e r v o D e t a c h s e r v o R e a d s e r v o W r i t e D C M o t o r s a n d S t e p p e r M e t h o d s : m o t o r S p e e d m o t o r R u n s t e p p e r S p e e d s t e p p e r S t e p

D i g i t a l P i n 0 2 i s c u r r e n t l y U N A S S I G N E D D i g i t a l P i n 0 3 i s c u r r e n t l y U N A S S I G N E D D i g i t a l P i n 0 4 i s c u r r e n t l y U N A S S I G N E D D i g i t a l P i n 0 5 i s c u r r e n t l y U N A S S I G N E D D i g i t a l P i n 0 6 i s c u r r e n t l y U N A S S I G N E D D i g i t a l P i n 0 7 i s c u r r e n t l y U N A S S I G N E D D i g i t a l P i n 0 8 i s c u r r e n t l y U N A S S I G N E D D i g i t a l P i n 0 9 i s c u r r e n t l y U N A S S I G N E D D i g i t a l P i n 1 0 i s c u r r e n t l y U N A S S I G N E D D i g i t a l P i n 1 1 i s c u r r e n t l y U N A S S I G N E D D i g i t a l P i n 1 2 i s c u r r e n t l y U N A S S I G N E D D i g i t a l P i n 1 3 i s c u r r e n t l y U N A S S I G N E D D i g i t a l P i n 1 4 i s c u r r e n t l y U N A S S I G N E D D i g i t a l P i n 1 5 i s c u r r e n t l y U N A S S I G N E D D i g i t a l P i n 1 6 i s c u r r e n t l y U N A S S I G N E D D i g i t a l P i n 1 7 i s c u r r e n t l y U N A S S I G N E D D i g i t a l P i n 1 8 i s c u r r e n t l y U N A S S I G N E D D i g i t a l P i n 1 9 i s c u r r e n t l y U N A S S I G N E D P i n I O M e t h o d s : p i n M o d e d i g i t a l R e a d d i g i t a l W r i t e a n a l o g R e a d a n a l o g W r i t e

ClickonmotorRunforMatlabcodingexamples.

LetstakeacloserlookattheinstanceandargumentsofmotorRun.
a . m o t o r R u n ( n u m , d i r ) r u n s a g i v e n D C m o t o r .

Thefirstargumentbeforethefunctionname,a,isthearduinoobject.Thesecondargument, num,isthenumberofthemotor,whichcangofrom1to4(themotorportsarenumbered onthemotorshield).Thethirdargument,dir,shouldbeastringthatcanbe'forward'(runs themotorforward)'backward'(runsthemotorbackward)or'release',(stopsthemotor). Notethatsinceversion3.0,a+1isinterpretedas'forward',a0isinterpretedas'release', anda1isinterpretedas'backward'.ThemotorRunmethodreturnsrandomresultsif motorshieldisnotconnected.


E x a m p l e s : a . m o t o r R u n ( 1 , ' f o r w a r d ' ) % r u n s m o t o r 1 f o r w a r d a . m o t o r R u n ( 3 , ' b a c k w a r d ' ) % r u n s m o t o r 3 b a c k w a r d a . m o t o r R u n ( 2 , 1 ) % r u n s m o t o r 2 b a c k w a r d a . m o t o r R u n ( 1 , ' r e l e a s e ' ) % r e l e a s e s m o t o r 1

AddthefollowinglinesofcodetomakesureyouhavecommunicationandthatMatlabis workingproperly.
E D U > > a . m o t o r S p e e d ( 1 , 1 0 0 )

E D U > > a . m o t o r R u n ( 1 , ' f o r w a r d ' ) E D U > > a . m o t o r R u n ( 1 , ' r e l e a s e ' ) L i n e 1 w i l l s e t t h e s p e e d o f y o u r m o t o r L i n e 2 w i l l s e t y o u r m o t o r t o g o f o r w a r d L i n e 3 w i l l s e t t e l l y o u r m o t o r t o s t o p

NowyoucanseethatyourArduinoworkswithMATLAB,butitisnotveryconvenientto keeptypingallthelinesofcodetocontrolyourmotors.Soyouwillnowimplementa MATLABGUItocontrolyourmotors.ByusingtheGUIyouwillbeabletocontrolboth motorsandseeyourmotorsworkdependingonyourparameters.

UsingtheMATLABGUI

Doubleclickonthedc_slider.mfiletoopenthem.fileeditor.Thisiswhereyouwillmake thefollowingchanges. Inthedc_slider.mfilemakesuretochangetheCOMportcorrespondingtoyourArduino.

InthiscasetheArduinocomportissetto14. Makesuretoalsochangethespeedofthemotorsatwhichyouwantthemtostartwhen youpresstheforwardpushbutton.

Ideallyyouwanttotestyourmotorstoseeatwhatspeedtheybeginrunning.Inmycasemy motorsbegintoturnatspeed80. Forthesteppermotormakesurethattheseparametersmakeasmoothtransitionwhen 9

executingthesteppermovingrightorleft
% h a n d l e s s t r u c t u r e w i t h h a n d l e s a n d u s e r d a t a ( s e e G U I D A T A ) h a n d l e s . a . s t e p p e r S p e e d ( 2 , 5 0 ) h a n d l e s . a . s t e p p e r S t e p ( 2 , ' f o r w a r d ' , ' i n t e r l e a v e ' , 1 0 )

Whenyouexecutethedc_slider.mfileyoushouldgetthefollowinginthecommandlineif everythingisworkingproperly. A t t e m p t i n g c o n n e c t i o n . . . . . . G e n e r a l S c r i p t d e t e c t e d ! A r d u i n o s u c c e s s f u l l y c o n n e c t e d ! E D U > > Ifeverythinggoeswellthefollowingshouldappear

Ifyougeterrorsinthisstep,pleasecheckthefollowing: 1. MakesuretheusbcableispluggedinandtheroverstillcommunicateswiththePC throughtheCOMXport(XistheportnumberthatthePCassignedtotheArduino board).YoucancheckthisthroughtheDeviceManager.Unpluggingandplugging theroverbackinwhilelookingatthelistofComswillallowyoutoseeexactlywhich oneisconnectedtotherover. 2. CloseMatlabandtrytorunthearduino.manddc_slider.magain.Closingand reloadingMatlabseemstobenecessaryeverytimeyouwishtoalterthecode. 10

Otherwisetheroverwontrespond. 3. Andisthebatterycharged!Ifthecodeuploadsbuttheroverdoesntmovethen mostlikelythebatteryistoolowtomovetherover.

3.1

Help

WhenfirsttestingtheroverwithMatlabcode,itcreatesaconnectiontotheArduino.When goingfromthearduino.mtotheGUI,anerrormayoccurwithaconnectionalreadybeing established.ThisiseasilysolvedbyclosingandreopeningMatlab,whichactuallysolvesa lotofproblems.IfconnectioncannotbeestablishedwiththeroverafterreopeningMatlab, thenchecktheCOMportandtryunpluggingtheArduino.

LabDeliverable(s)
1. Demonstratetheoperationofyourrover 2. Findandrecordinyourlabnotebooksthestallspeedofyourrover

FutureWork

AsaresultoftheresearchIfoundthattheGUIisverylimitedinparametersthatyouwould wanttoachieve,suchasmakingthetwoDCmotorsstartatthesametime.UsingtheGUI themotorshavetobeindividuallystartedandmodifiedbecauseyouonlyhaveone mouse/touchpadtoworkwithonacomputer/laptop.Iwouldsuggestthatforfuture developmentoftheGUI,useRB11becauseithasupdatesfortheGUIwithbetterediting options.

11

You might also like