lab experiment 8: putting everything together - ee.ic.ac.uk 8 - self balancing... · lab 8:...

4
Lab 8: Self-balancing Segway Imperial College London V1.0 - PYK Cheung, 14 March 2018 Lab 8 - 1 Dyson School of Design Engineering Imperial College London DE2.3 Electronics 2 Lab Experiment 8: Putting Everything Together (webpage: http://www.ee.ic.ac.uk/pcheung/teaching/DE2_EE/) INTRODUCTION This lab instruction is intended to help you to put everything that you have learned together to complete the Team project. It contains various tips and guidelines, snippets of Python code, pseudo-code of the top-level program, related to the different tasks that your team is expected to achieve. TIPS 1: FROM IDLE TO RUNNING Running an embedded programme on the PyBench board can cause problem because as soon as you press the RESET button, you usually start the programme (which programme you run depends on the DIP switch setting). It is often helpful if the Segway is in an idle mode, and only run your programme when you press the USER button. Here we use the OLED display to tell us what the Segway is doing. Remember that drawing anything on the OLED is VERY SLOW. Therefore, only do that outside the main program loop. We also include the print statements to help debugging. These will show up on the Putty or the Terminal window on your laptop. TIPS 2: MODIFY THE MAIN.PY FILE So far, the way I have organized the Pybench system on the Segway is to run Pybench driver pybench_main.py, with the DIP switches are set to 00, run a self-test program if SW=01 or 10, and finally run user.py if SW=11. To make things easier for the team project, I recommend that you modify the program main.py so that you run the various milestones depending on the DIP switch setting as shown below.

Upload: hahanh

Post on 30-Apr-2018

214 views

Category:

Documents


1 download

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.