lab experiment 8: putting everything together - ee.ic.ac.uk 8 - self balancing... · lab 8:...
TRANSCRIPT
Lab8:Self-balancingSegway ImperialCollegeLondon
V1.0-PYKCheung,14March2018 Lab8- 1
DysonSchoolofDesignEngineering
ImperialCollegeLondon
DE2.3Electronics2
LabExperiment8:PuttingEverythingTogether
(webpage:http://www.ee.ic.ac.uk/pcheung/teaching/DE2_EE/)
INTRODUCTION
ThislabinstructionisintendedtohelpyoutoputeverythingthatyouhavelearnedtogethertocompletetheTeamproject.Itcontainsvarioustipsandguidelines,snippetsofPythoncode,pseudo-codeofthetop-levelprogram,relatedtothedifferenttasksthatyourteamisexpectedtoachieve.
TIPS1:FROMIDLETORUNNING
RunninganembeddedprogrammeonthePyBenchboardcancauseproblembecauseassoonasyoupresstheRESETbutton,youusuallystarttheprogramme(whichprogrammeyourundependsontheDIPswitchsetting).ItisoftenhelpfuliftheSegwayisinanidlemode,andonlyrunyourprogrammewhenyoupresstheUSERbutton.
HereweusetheOLEDdisplaytotelluswhattheSegwayisdoing.RememberthatdrawinganythingontheOLEDisVERYSLOW.Therefore,onlydothatoutsidethemainprogramloop.Wealsoincludetheprintstatementstohelpdebugging.ThesewillshowuponthePuttyortheTerminalwindowonyourlaptop.
TIPS2:MODIFYTHEMAIN.PYFILE
Sofar,thewayIhaveorganizedthePybenchsystemontheSegwayistorunPybenchdriverpybench_main.py,withtheDIPswitchesaresetto00,runaself-testprogramifSW=01or10,andfinallyrunuser.pyifSW=11.Tomakethingseasierfortheteamproject,Irecommendthatyoumodifytheprogrammain.pysothatyourunthevariousmilestonesdependingontheDIPswitchsettingasshownbelow.
Lab8:Self-balancingSegway ImperialCollegeLondon
V1.0-PYKCheung,14March2018 Lab8- 2
TIPS3:PSEUDO-CODEFORMILESTONE3
I assume that by now, you should have completedmilestones 1 and 2. Formilestone 3, your goal is to useyour improved beat-detection routine to synchronousmusicwith dancingmoveswith the stabilizer (i.e. no self-balancing). You should store thedancemove ina text file(ASCIIformat),readthisfileatthestartoftheprogramandstorethemovesinanarrayBEFOREthemainprogramloop.This is because readingASCII characters froma text file isvery slow. It is farbetter to store information inmemory(i.e.anarray)andaccessthisarrayinsidetheprogramloop.
Furthermore,ifyouhavenotlearnedPythonKeywordsTryand Finally, you should. Here is the pseudo-code forMilestone3.
TIPS4:PITCHANGLEESTIMATION
BynowyoushouldbefamiliarwithusingtheIMUtoestimatethepitchangleusingComplementaryFilter.Hereisafunctionthatestimatethepitchangle,justtohelpyoualongabitquicker.dtisdeltatime,thetime since the last reading in the program loop. You finddtwith tic andpyb.millis() orpyb.micros().Don’tforgettoadjustdttosecondsinyourequation.
TIPS5:PIDCONTROLLER
ThebasicPIDcontrollerequationisstraigthforward:
IrecommendyoutocreateaPIDcontrollerfunction(orifyouaregoodinPythoncoding,createaclass)thatdothefollowing:
Inputtofunction:pitchangle,(rateofchangeofpitch(pitch_dot),target(orset-point),cumulativepitcherror(integralterm).
Outputofthefunction:PWMdrivevaluelimitedto±100.
Youshouldalsolimittheset-pointtoasmallvalue,suchas±3degrees(say).
Finally,youwillfindthattheIMUmayNOTreturnapitchangleofzerowhenitisupright.ThisisbecausethecentreofgravityoftheSegwaymaynotbedeadcentre.Thisdependsonthepositionofthebatteryandotherfactors.Youthereforemayneedtotakethisintoaccount.
𝑤(𝑡) = 𝐾(𝑒(𝑡) + 𝐾+�̇�(𝑡) + 𝐾- . 𝑒(𝜏)𝑑𝜏
Lab8:Self-balancingSegway ImperialCollegeLondon
V1.0-PYKCheung,14March2018 Lab8- 3
Thepseudo-codeforthecontrollerfunctionis:
TIPS6:TUNINGTHEPIDCONTROLLER
Finally, youwould need to tune the PID controller. Oneway to do this is tomodify the programandchangethethreegainvalues:Kp,KiandKdinthePythoncode.Amuchbetterwaytodothis(thankstoBen Greenberg last year) is the use the potentiometer on the Pybench board together with the USRswitchtoadjustthesegainvalueslive!Hereisthecodetodothat:
Onceyouhave tuned thePIDcontroller, youcan replace thepot.read() statementwith the tunedgainvalue.Thentostarttheprogram,youjustpresstheUSRswitchfewtimesandtheself-balancingprogramwillrun.
Lab8:Self-balancingSegway ImperialCollegeLondon
V1.0-PYKCheung,14March2018 Lab8- 4
TIPS7:PSEUDO-CODEFORSELF-BALANCING,DANCINGSEGWAY
Nowputallthesetogether,hereisthepseudo-codeofapossiblefinalprogramtodobothself-balancinganddancingtomusic:
Notethat thisprogram loophas twotimescales: thecontrolleruses tic1, inmicroseconds. Thepollingloopchecksfor5msecelapsetime.Thismeansthecontrolloopisrunningatround200Hz.
Thebeatdetectionusestic2inmilliseconds.Theloopisdetectingbeatsatmuchlowerfrequency.