ADVANCEDEV3PROGRAMMINGLESSON
UsingtheGyroSensorandDealingwithDrift
BySanjayandArvindSeshan
1. LearnwhattheGyroSensordoes
2. Learnabout2commonproblemswithusingthegyrosensor(driftandlag)
3. Learnwhat“drift”means
4. Learnhowtocorrectfordriftwithagyro“calibration”technique
5. Understandwhyitisimportanttoconsidermultiplesolutionstoaproblemsuchasgyrodrift
Prerequisites:Datawires,Loops,Logic&ComparisonBlocks
Update:Please read the“Gyro SensorRevisited”lesson forimportantupdates (8/6/2017)after you completethis lesson.
©2016EV3Lessons.com
LessonObjectives
8/6/17 2
ì Gyrosensordetectsrotationalmotion
ì Thesensormeasurestherateofrotationindegreespersecond(rate)
ì Italsokeepstrackofthetotalrotationalangleandthereforeletsyoumeasurehowfaryourrobothasturned(angle)
ì Theaccuracyofthesensoris±3degreesfor90degreeturn
©2016EV3Lessons.com
WhatistheGyroSensor?
8/6/17 3
ì Thereare2commonGyroissues– driftandlagì Drift– readingskeepchangingevenwhentherobotisstillì Lag– readingsaredelayed
ì Inthislesson,wefocusonthefirstproblem:drift.ì WewillcoverlagintheGyroTurnlesson
ì Solutiontodrift:gyrocalibrationì Thesourceofthedriftproblemisthatthegyromust“learn”whatis
still.ì Foracolorsensor,youhaveto“teach”therobotwhatisblackand
whiteì Foryourgyro,youneedtocalibratethesensortounderstandwhatis
“still”
©2016EV3Lessons.com
GyroSensorProblems
8/6/17 4
• Thegyroauto-calibrateswhentherobotisturnedonorthegyrowireisconnected.Iftherobotismovingduringcalibration,thegyro“learns”thewrongvaluefor“still”– thiscausesdrift!
• Unfortunately,thereisnogyrocalibrationblock.Thereafewwaystomakethesensorrecalibrate.
©2016EV3Lessons.com
GyroCalibrationtoSolveProblem1:Lag
8/6/17 5
• Thebelowarecriticalnotesforusingthegyrocorrectly!!!!!
• THEROBOTMUSTBESTILLWHENYOURUNANYOFTHESECALIBRATIONPROGRAMS!!!!
• JUSTLIKETHECOLORCALIBRATION,YOUSHOULDN’TRUNTHISEVERYTIMEYOUNEEDTOREADTHEGYRO.YOUSHOULDCALIBRATEINASEPARATEPROGRAMJUSTBEFOREYOURUNYOURPROGRAMORONCEATTHEBEGINNINGOFYOURPROGRAM.
©2016EV3Lessons.com
IMPORTANTNOTES
8/6/17 6
©2016EV3Lessons.com
Calibration:Strategy1
Thegyrorecalibrateswhenitswitchesmodes.So,a“rate”readingfollowedbyan“angle”readingcalibratesthegyro.
Second,addawaitblocktogivethesensorabitoftimetofullyreset.Ourmeasurementsshowthat0.1secondsissufficient.
Notethatintherestofyourprogram,youshouldonlyusethe“angle”modesofthegyro.Usingthe“rate”or“rateandangle”modewillcausethegyrotorecalibrate.
8/6/17 7
©2016EV3Lessons.com
Calibration:Strategy2
Thisversionofthecalibrationleavesthegyroinrate+anglemode.Thisisusefulifyouusetherateoutput.
Thedownsideofthisversionisthatittakeslonger(about3seconds).Also,youcannotusegyroresetanymore!
Notethatintherestofyourprogram,youshouldonlyusethe“rate+angle”modesofthegyro.Usingthe"angle"or“rate”modewillcausethegyrotorecalibrate.Also,***DONOT***usethegyroreset-thisforcesthegyrointoanglemodewhichwillcausealong3secondrecalibration.
8/6/17 8
ì Having afixed timewait forthegyro tocalibratemay not always work.
ì Thegyro returns “Not aNumber”(NaN)until it has actually resetandNaNs arenot >,=,or<any number.This is because they arenot numbers
ì Theonlywayyoucanknowwhenitisfullyresetistomakesureyouaregettingbackarealnumber,insteadofaNot-a-Numbervalueì STEP1:Recalibratethegyroì STEP2:startaloopì STEP3:readangleì STEP4:check angle>=0ì STEP5:check angle<0ì STEP6:ORoutputs ofsteps 4&5ì STEP7:If theoutputofstep 6is true,exitloop
ì Atthis point,thesensor drift should begone.
©2016EV3Lessons.com
Strategy3:Pseudocode
8/6/17 9
©2016EV3Lessons.com
Strategy3Solution
Notethatintherestofyourprogram,youshouldonlyusethe“angle”modesofthegyro.Usingthe“rate”or“rateandangle”modewillcausethegyrotorecalibrate.
Thisversionofthecalibrationleavesthegyroinanglemode.Thisisprobablythemostcommonwaytousethegyro.Thiscodetakesabout0.1sectorun.
8/6/17 10
©2016EV3Lessons.com
Strategy4Solution
Notethatintherestofyourprogram,youshouldonlyusethe“rate+angle”modesofthegyro.Usingthe"angle"or“rate”modewillcausethegyrotorecalibrate.Also,***DONOT***usethegyroreset-thisforcesthegyrointoanglemodewhichwillcausealong3secondrecalibration.
Thisversionofthecalibrationleavesthegyroinrate+anglemode.Thisisusefulifyouusetherateoutput.
8/6/17 11
©2016EV3Lessons.com
DiscussionGuide
1. What are2commonproblems when programming withthegyro?Ans.Gryo drift andGyro lag
2. What does Gyro drift mean?Ans.TheGyro readings keep changing even when therobotis still
3. Canyou move your robotwhen you calibrateyour gyro?Ans.No!!Keep therobotstill.
4. Doyou need tocalibrateyour gryo before every move?Ans.No.Oncebefore you run your entire program
5. Why might it beimportant toconsider multiplesolutions toaproblem?Ans.Inrobotics,there aremultiplewaystosolveaproblem andthere might betradeoffs between thesolutions (e.g.how longthecodetakes torun thecode,canyou useboth rateandanglereadings?)
Update:Please read the“Gyro SensorRevisited”lesson forimportant updates (8/6/2017)
8/6/17 12
ì ThistutorialwaswrittenbySanjaySeshan andArvindSeshanandusescodesharedbyHoosierGirlz(http://www.fllhoosiergirlz.com)
ì Morelessonsatwww.ev3lessons.com
©2016EV3Lessons.com
Credits
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.
8/6/17 13