Вы находитесь на странице: 1из 11

8/9/2016

LineFollowerApplicationforArduinoRobotMATLAB&SimulinkExample

LineFollowerApplicationforArduinoRobot
ThisexampleshowshowtocreatealinefolloweralgorithminSimulinkandhowtorunitonan
ArduinoRobot.

OpenThisExample

Introduction
SimulinkSupportPackageforArduinoHardwareenablesyoutocreateandrunSimulinkmodelsonArduinoRobot.This
RobothastwoLeonardo(ATmega32u4)basedboards:ArduinoRobotMotorBoardandArduinoRobotControlBoard.The
ArduinoRobotControlBoardhasperipheralssuchasAnalogInputPins,DigitalInput/OutputPins,PWM,Keypad,
Potentiometer(POT),Compass,Buzzer,etc.TheArduinoRobotMotorBoardhasperipheralssuchasAnalogInputPins,
DigitalInput/OutputPins,PWM,MotorDriver,Motors,Wheels,TrimmingPotentiometer(TRIM),IRsensors,etc.For
moredetails,refertotheArduinoRobotwebsite.
ThisexampleshowshowtocreateaSimulinkmodeltorunalinefolloweralgorithmontheArduinoRobotMotorboardby
accessingtheIRsensorsandmotors.YouwilllearnhowtoaccesstheperipheralsoftheArduinoRobotMotorBoard
usingblocksfromtheSimulinklibraryandthestandardArduinolibraryfromtheSimulinkSupportPackageforArduino
Hardware.
Thisexampleillustrateshowtoaccesstheperipherals(Keypad,Buzzer)oftheArduinoRobotControlBoardusingblocks
fromSimulinklibraryandthestandardArduinolibrary.Youwilllearnhowtoestablishserialcommunicationbetweenthe
ArduinoRobotControlBoardandtheArduinoRobotMotorBoard.

Prerequisites
IfyouarenewtoSimulink,werecommendcompletingInteractiveSimulinkTutorial,readingtheGettingStartedsectionof
theSimulinkdocumentationandrunningSimulinkGettingStartedexample.

RequiredHardware
Torunthisexampleyouwillneedthefollowinghardware:

ArduinoRobot

USBCable

4rechargeableNiMhAAbatteries

9VAdapter

TrackdrawingonA0whitesheetwith~1.5"wideblackline(sampletrack)

http://www.mathworks.com/help/supportpkg/arduino/examples/arduinorobotlinefollowerapplication.html

1/11

8/9/2016

LineFollowerApplicationforArduinoRobotMATLAB&SimulinkExample

Introductiontolinefollowerapplication
AlinefollowermechanismfortheArduinoRobotcanbeshownasbelow:

InthisapplicationyouneedtogetthecurrentdeviationoftheArduinoRobotfromthecenteroftheblacklinetogetthe
Errorsignal.BasedontheErrorsignalyouwilldrivethemotorsontheArduinoRobotMotorBoardtotakecorrective
actionandbringtheArduinoRobotbacktothecenteroftheblackline.
1.OnexaminingtheArduinoRobotyouwillnoticethattheIRsensorspresentontheArduinoRobotMotorBoardcanbe
usedtoobtainthecurrentdeviationoftheArduinoRobotfromthecenteroftheblackline.Youwilllearntoreadthevalues
fromtheIRsensorsusingblocksfromtheSimulinklibraryandthestandardArduinolibrary.
2.Youwilllearnhowtoprovidepulsewidthmodulated(PWM)signalstocontrolthemotorsconnectedtotheArduino
RobotMotorBoardandspinthewheelstomovetheArduinoRobot.

Task1CreateaSimulinksubsystemtoreadIRsensorsontheArduinoRobotMotorBoard
Inthissection,youwilllearnhowtodecodeIRSensorvaluesintoaSimulinksignalofdimensions5correspondingtothe
numberofIRsensorspresentontheArduinoRobotMotorBoard.ThelogicdescribedisimplementedintheIRsensors
subsystem.TheschematicoftheArduinoRobotMotorBoardcanbedownloadedfrom"Schematic&ReferenceDesign"
sectionontheArduinoRobotwebsite.NoticethefollowingconnectionsfortheIRsensors:

http://www.mathworks.com/help/supportpkg/arduino/examples/arduinorobotlinefollowerapplication.html

2/11

8/9/2016

LineFollowerApplicationforArduinoRobotMATLAB&SimulinkExample

The5IRsensors(IR0IR4)areconnectedtotheinputchannelsofamultiplexer(MUX).TheMUXselectorsMUXA,
MUXB,MUXCareconnectedtodigitalpins7,8,and11respectivelyonthemicrocontroller.Themicrocontrollercanonly
readoneIRsensoratatime,byproviding3digitalsignals(MUXA,MUXB,MUXC)toselectthedesiredIRsensorsignal
ontheoutputoftheMUX.3digitalsignalsareenoughtoaccessthe5IRsensors.MUXAistheLSBandMUXCisthe
MSBoftheselectorbyte.Basedontheselectorbyte,thefollowingIRsensorisselected:
MUXC|MUXB|MUXA|IRsensor_number
___________________
0|0|0|0
0|0|1|1
0|1|0|2
0|1|1|3
1|0|0|4
___________________
TheselectedIRsensor,presentontheMUXoutput,isconnectedtotheanalogpinA2ofthemicrocontroller.
ThefollowingstepswillshowyouhowtocreateamaskedsubsysteminSimulinktoreadanIRsensor.
1.UseaConstantblocktodecidewhichIRsensortoread.TakeaConstantblockfromLibrary:Simulink>Sources
andupdatethefollowingsettings:ConstantValue>anyvaluefrom04,SampleTime>0.002,Outputdatatype>uint8.
Thevalues04correspondtotheIRsensornumbers.
2.ConverttheIRsensornumberintoabinaryvaluetoprovideinputstothedigitalpinscorrespondingtoMUXA,MUXB,
MUXCusingBitwiseOperatorblock.Use3BitwiseOperatorblockswithANDoperationandvalues0x1,0x2,0x4which
givesLSBtoMSBrespectivelyfortheselectorbyte.Use3DigitalOutputblocksfromLibrary:SimulinkSupport
PackageforArduinoHardware>CommonandpopulatethePinnumberas7,8,11forMUXA,MUXB,MUXC
respectively.ConnecttheLSBoftheselectorbyte(obtainedfromtheANDoperation)toDigitalOutputblock
correspondingtoMUXA,middlebittoMUXB,MSBtoMUXC.
3.UseanAnalogInputblockfromLibrary:SimulinkSupportPackageforArduinoHardware>Commontoreadpin
A2correspondingtotheMUXoutput.EnterPinnumber2andasampletimeof0.001.TheAnalogInputblockreadsa
valueofdatatypeuint16rangingfrom01023fromtheIRsensor(where0whiteand1023black).UseaDataType
Conversionblocktoconvertthevaluetoint16.ConnecttheoutputofDataTypeConversionblocktoanOutport.
4.SetthepriorityoftheDigitalOutputblocksto1astheselectorbyteneedstobewrittenbeforereadingIRsensorvalue.
SetthepriorityoftheAnalogInputblocktoanyvalue>1toensureitgetsexecutedaftertheselectorbyteiswrittento
MUX.TosetthePriorityofablock,rightclickontheblock>Properties>General>Priority.Toknowmoreaboutblock
prioritiesandtheirimpactonblockexecutionorder,refertoSetBlockProperties.
5.UseaForIteratorSubsystemtoloopthroughallthe5sensors.TheinputtotheForIteratorSubsystemisthe
ConstantblockcreatedinStep1.ThevalueoftheConstantblockis[01234]torepresentallthe5sensors.Use
OutportfortheoutputoftheForIteratorSubsystem.
http://www.mathworks.com/help/supportpkg/arduino/examples/arduinorobotlinefollowerapplication.html

3/11

8/9/2016

LineFollowerApplicationforArduinoRobotMATLAB&SimulinkExample

6.Placetheblockscreatedfromstep24insidetheForIteratorSubsystem.OpentheForIteratorsubsysteminsidethe
IRsensorssubsystemandcheckthesettingsofSelectorandAssignmentblocks.TheSelectorandAssignmentblocks
areusedtoloopthroughoneIRsensornumberatatimeandassignthevaluetotheoutputvector.
7.Createasubsystembyselectingalltheblockscreatedinstep6suchthatitlooksasbelow:

Task2CreateaSimulinksubsystemtocontroltheMotorsontheArduinoRobotMotorBoard
Inthissection,youwilllearnhowtospinthe2motorspresentontheArduinoRobotMotorBoardinbothforwardand
reversedirectionsusingdesiredTorqueSimulinksignals.ThelogicdescribedisimplementedintheMotorssubsystem.
TheschematicoftheArduinoRobotMotorBoardshowsthefollowingconnectionsbetweenthemicrocontrollerandthe
motordriver:

Themotordrivercontrolsthespeedandthedirectionofbothleftandrightmotorsbytakinginputsfromthe
microcontroller.Noticethatthedigitalpins6,5,10,9ofthemicrocontrollerareconnectedtoIN_A1,IN_A2,IN_B1,IN_B2
ofthemotordriver.TheinputsIN_A1,IN_B1ofthemotordrivercorrespondstotheforwardmovementoftheleftandright
motorrespectively.IN_A2,IN_B2correspondstothereversemovementoftheleftandrightmotorrespectively.
Apulsewidthmodulated(PWM)signalcanbeusedtocontrolthemotors.Apositiveinputtothemotordriverfromthe
microcontrollercorrespondstoforwardmovementofthemotors,whereasanegativeinputcorrespondstoreverse
movementofthemotors.Forforwardmovementofboththemotors,youhavetoapplythePWMsignalstoIN_A1and

http://www.mathworks.com/help/supportpkg/arduino/examples/arduinorobotlinefollowerapplication.html

4/11

8/9/2016

LineFollowerApplicationforArduinoRobotMATLAB&SimulinkExample

IN_B1andzerovaluestoIN_A2andIN_B2.Similarlyforreversemovement,youhavetoapplythePWMsignalsonlyto
IN_A2andIN_B2andzerovaluestoIN_A1andIN_B1.
ThefollowingstepsshowyouhowtocreateamaskedsubsysteminSimulinktocontrolthemotors.
1.UseanInportblockstogettheinputfromthemicrocontrollerfortheleftmotor.
2.ThePWMblockpresentinLibrary:SimulinkSupportPackageforArduinoHardware>Commoncanbeusedto
sendPWMsignalstocontrolthemotors.Use2PWMblocksandenterthepinnumbers6,5forIN_A1,IN_A2.
3.TherelationbetweentheinputvalueandIN1(IN_A1/IN_B1),IN2(IN_A2/IN_B2)canbeshownasbelow:
Input|IN1|IN2
________________
+ve|Input|0
ve|0|abs(Input)
________________
Fortheforwardmovement,useaMUXblocktomultiplexinputvalueandaConstantblockof0valueofdatatypeint16.
4.ThePWMblockacceptsvaluesfrom0255(uint8).Forthereversemovement,useanAbsblockwithOutputdatatype
>int16,Integerroundingmode>RoundtoconvertthenegativeinputvaluetopositiveandmultiplexitwiththeConstant
blockusedinstep3withvalue0.
5.UseaSwitchblockwithconditionu2>Thresholdwhereu2>inputandThreshold>0.Iftheconditionismetthen
theforwardmovementmultiplexedvaluesareselectedelsereversemovementmultiplexedvaluesareselected.
6.UseaDEMUXblockandconnecttheoutputstothe2PWMblockscorrespondingtoIN_A1,IN_A2.
7.Repeattheentirelogicfromsteps16fortherightmotor.Enterpinnumber10,9forthe2PWMblockscorresponding
toIN_B1,IN_B2.
8.Createasubsystembyselectingalltheblockssuchthatitlooksasbelow:

Task3BuildaSimulinkmodelwithlinefolloweralgorithm
Inthissection,thelinefolloweralgorithmisimplementedasaclosedloopcontrolsystemwithPIDcontroller.Thelogic
describedisimplementedintheLineFollowerAlgorithmsubsystem.Thealgorithmisdescribedbelow:

http://www.mathworks.com/help/supportpkg/arduino/examples/arduinorobotlinefollowerapplication.html

5/11

8/9/2016

LineFollowerApplicationforArduinoRobotMATLAB&SimulinkExample

ThebluedotsrepresentIRsensors(IR0IR4).WhentheArduinoRobotisatthecenteroftheblacklinethenIR2reads
black(~1023)andIR0,IR1,IR3,IR4readwhite(~0).Athresholdischosentoconverttherangefrom01023toabinary
value.1whenbelowthethreshold(brightorwhitebackground),0whenabovethethreshold(darkorblackbackground).
Aweightisgiventoeachsensor,withhighervaluestotheextremeonesprovidinghighercontrolsignalswhenhigher
deviationisdetected.Theweightedcurrentdeviationiscalculatedfromthebelowequation:

Whentherobotiscenteredonathinblackline,IR2reads0whileIR0,IR1,IR3,IR4read1.
Substitutingthevaluesintheequation,thecurrentdeviationiscalculatedasbelow:

TheDesireddeviationwhentherobotiscenteredonathinblacklineissettoaconstantvalueof0.Theerroris
calculatedas
.Inthiscasewhenthecurrentdeviationis0,the
erroris0andtherobotcanmovestraight.Asummarizedlistofallpossibleerrorsisshowninthebelowtable:

*dividebyzerocase

http://www.mathworks.com/help/supportpkg/arduino/examples/arduinorobotlinefollowerapplication.html

6/11

8/9/2016

LineFollowerApplicationforArduinoRobotMATLAB&SimulinkExample

NoticethatapositiveerrormeansrightturnfortheArduinoRobot>LeftMotortorqueincreases,RightMotortorque
decreases.AnegativeerrorfortheArduinoRobotmeansleftturn>LeftMotortorquedecreases,RightMotortorque
increases.
ToimplementtheabovementionedlinefollowerlogicinSimulinkyoucancreatetwosubsystems:onetogetthecurrent
deviationoftheArduinoRobotusingIRsensorsandanothertocontrolthemotors.Anexampleoftheimplementationcan
beseenintheLineFollowerAlgorithmsubsystemofthemodel.
1.LookinsidetheCurrentdeviationcalculationsubsystem.ItgivesthecurrentdeviationoftheRobotfromthecenterof
blacklineaspertheabovealgorithm.
Pointstonote:

TheIRsensorthresholdvaluesettoaconstantvalueof600.

TheweightsappliedusingGainblocks

SaturateonintegeroverflowoptioncheckedforMathFunctionblock

IntegerroundingmodeasRoundforProductblocktoensurethecurrentdeviationrepresentsaccurateinteger
value.

Switchblocktotakecareofthepossibledividebyzerocaseforcurrentdeviationcalculation.

2.AnalysetheTorqueestimationsubsystem.Itgivesthedesiredtorquevalues(from255to255)fortheleftandtheright
motors.
Pointstonote:

TheBaseTorqueoftheRobotsettoaconstantvalueof100.TheOutputdatatypesettoint16tomatchthedatatype
ofTorque_correction.

Stateflowchartimplementationswitchesbetweenthreedifferentstates:GoStraight,TurnLeft,TurnRightbasedon
valueofTorque_direction.

3.ObservethattheIRsensorssubsystemhasapriorityof1andthattheMotorssubsystemhasmuchlowerprioritysay,
5toensurethecontrolactionisappliedtothemotorsonlyafterobtainingthecurrentdeviationoftheRobotfromtheIR
sensors.
4.InthisexampleaDiscretePIDcontrollerblockisusedtogetthecontrolactionbasedontheerror.Youmayneedto
tunetheP,I,DvaluesiterativelybyplacingtheRobotonthetrack.Makesurethatthebatteriesarechargedenoughto
runthewheelsproperly.

Task4RunthelinefollowerapplicationonArduinoRobotMotorBoard
Inthissection,youwilllearnthestepsrequiredtodeploytheSimulinkAlgorithmontheArduinoRobotMotorBoard.
1.ConnecttheArduinoRobotMotorBoardtoyourhostcomputerusingUSBcable.Thiscablewouldalsopowerthe
board.
2.Inthemodel,clickSimulation>ModelConfigurationParameterstoopenConfigurationParametersdialog.Inthe
dialogthatopens,selecttheHardwareImplementationpaneSelectandArduinoRobotMotorBoardasHardware
boardfromthedropdownmenu.YoucaneithermanuallyselecttheCOMPortnumberorletitremainAutomaticallyand
keeptherestofthesettingswithdefaultvalues.ThenclickApply>OK.Apreconfiguredmodelisincludedforyour
convenience.
3.NextclicktheDeploytoHardwarebuttononthetoolbarofthemodel.
4.Afterthemodelisdownloaded,disconnecttheUSBcablefromyourArduinoRobotMotorBoard.
5.PlacetheRobotonthetrackandturntheRobotpowerswitchonpresentattheArduinoRobotMotorBoard.Themodel
runsontheArduinoRobotMotorBoardandtheArduinoRobotstartsmoving.
6.FactorssuchasambientlightandthicknessofthetrackcanaffecttheabilityoftheRobottotracktheblackline.Ifthe
ArduinoRobotdoesnottracktheblackline.Toovercometheseissues,youcantunetheP,I,DvaluesoftheDiscrete
PIDControllerandchangethevaluesofBaseTorque,IRsensorthresholdinthemodeltomaketheRobottracktheblack
http://www.mathworks.com/help/supportpkg/arduino/examples/arduinorobotlinefollowerapplication.html

7/11

8/9/2016

LineFollowerApplicationforArduinoRobotMATLAB&SimulinkExample

line.DownloadthemodelagainontheArduinoRobotMotorBoardusingDeploytoHardwareoption.Repeattillthe
ArduinoRobotfollowstheblacklineproperly.
7.TurntheRobotswitchoff.

Task5ExtendthemodelwithserialcommunicationbetweentheArduinoRobotControlBoardand
theArduinoRobotMotorBoard
Inthissection,youwilllearnhowtoestablishserialcommunicationbetweentheArduinoRobotMotorBoardandthe
ArduinoRobotControlBoard.YouwilllearnhowtodecodetheKeypadsignalsontheArduinoRobotControlBoardand
howtousethemascontrolsignalsontheArduinoRobotMotorBoard.ThelogicdescribedisimplementedinArduino
RobotControlBoardmodelandArduinoRobotMotorBoardmodel.
ThisexampleusestheKeypadandBuzzerperipheralspresentontheArduinoRobotControlBoardtosendcontrol
commandstoandreactonthefeedbackreceivedfromtheArduinoRobotMotorBoard.TheschematicoftheArduino
RobotControlBoardcanbedownloadedfrom"Schematic&ReferenceDesign"sectionontheArduinoRobotwebsite.The
schematicshowsthattheKeypadisconnectedtopinA0oftheArduinoRobotControlBoard.TheKeypadhas5keys
controllingavoltagedividercircuit.Thevoltagedetectedafterakeypressisrepresentedbya10bit(01023)analogto
digitalvalueonthemicrocontroller.DependingonthedigitalrepresentationofvoltageatpinA0youcanknowwhichkeyis
pressed.

A0_Digital_Value|Key
_____________
010|1
133153|2
319339|3
494514|4
732752|5
_____________
TheschematicoftheArduinoRobotControlBoardshowsthattheBuzzerisconnectedtodigitalpinD5.
YoucancontrolthemovementofwheelsontheArduinoRobotMotorBoardbypressingakeyontheArduinoRobot
ControlBoardandplayatoneonBuzzerbasedonfeedbackreceivedfromtheArduinoRobotMotorBoard.Serial
communicationisusedfordatatransmissionbetweenthetwoboards.
a)SendingserialcommandsfromKeypadofArduinoRobotControlBoard:
YoucanreadthekeypressedontheArduinoRobotControlBoard.Key5canbeusedtoswitchbetweentwomodesof
theRobot:LineFollowermodeandManualmode.Basedontheselectedmodeandthekeypressedyoucansendserial
commandstoArduinoRobotMotorBoard.ThebelowtablesummarizesthedatasentseriallybyArduinoRobotControl
Board:

http://www.mathworks.com/help/supportpkg/arduino/examples/arduinorobotlinefollowerapplication.html

8/11

8/9/2016

LineFollowerApplicationforArduinoRobotMATLAB&SimulinkExample

Mode|Keypressed|Move|Serialdata
__________________________________
LineFollower|NA|trackblacklinecenter|1
Manual|none|stop|0
Manual|1|anticlockwise|2
Manual|2|backward|3
Manual|3|forward|4
Manual|4|clockwise|5
__________________________________

NANotApplicable

1.OpentheArduinoRobotControlBoardmodel.ToknowthekeypressedyoucanuseAnalogInputblockfromLibrary:
SimulinkSupportPackageforArduinoHardware>CommonwithPinnumber0andSampletime0.004.
2.ObservethekeydebouncelogicusingStateflowchartthatvalidatesthekeypressifthekeyremainspressedfor0.016
s.NotethatthekeydebouncetimemayvaryfromoneRobothardwaretoanother.
3.ThemodeSelectStateflowchartdetectsifthekey5ispressedandswitchesfromLineFollowermodetoManualmode
andviceversa.NotethatthedefaultmodeisLineFollower.Anykeypressotherthan5doesnothaveanyeffectonthe
LineFollowermode.
4.TheserialcommandsubsystemdeterminesthedatatobesentintheLineFollowerortheManualmode.IntheManual
mode,youcanmaketheRobotmoveasperthedatasentbythekeypresstotheArduinoRobotMotorBoard.
5.UseSerialTransmitblockfromLibrary:SimulinkSupportPackageforArduinoHardware>CommonwithPort1
tosendthecommandstotheArduinoRobotMotorBoard.TheSerialTransmitblockhasinitialvalueof1indicatingthe
LineFollowermodeasdefault.
6.NotethatthepriorityofAnalogInputKeypadReadblockis1andthatofSerialTransmitblockismuchlowersay,10.
Thisensuresthatyoudeterminethepressedkeybeforesendingthecommandsserially.
b)ReceivingserialdataonArduinoRobotMotorBoardandsendingfeedback:
1.OpentheArduinoRobotMotorBoardmodel.ToreceivetheserialdatafromArduinoRobotControlBoardaSerial
ReceiveblockfromLibrary:SimulinkSupportPackageforArduinoHardware>CommonisusedwithPort1and
Sampletime0.002.NotethattheSerialReceiveblockhasahighersamplingratethantheSerialTransmitblockonthe
ArduinoRobotControlBoardmodeltoensurenottomissanydata.
2.ObservethedecidemodeStateflowchartusedtodecidethemodetobefollowedbytheArduinoRobotMotorBoard.
ThedefaultmodeisLineFollower.NotethattheStatusoutputoftheSerialReceiveblockindicatesifanynewdatais
received.TheStateflowchartusestheStatusinformationtoswitchbetweenthemodesaccordingly.
3.SeetheenabledsubsystemLineFollowermodehasthelinefolloweralgorithmplacedinsideit.
4.TheManualmodeenabledsubsystemusesaSwitchCaseblock.ThedatareceivedfromtheArduinoRobotControl
BoardselectsthecorrespondingmovefromtheCasesubsystems.ThedefaultmoveisStopi.e.bothLeftMotorInput
andRightMotorInputis0.
5.DependingontheselectedmovebytheSwitchCaseblockaSerialTransmitblock(withPort1)sendsfeedbacktothe
ArduinoRobotControlBoard.Valueof1,2,and0issentasfeedback.Value1correspondstoAnticlockwiseorClockwise
moveselection.Value2correspondstoForwardorBackwardmoveselection.Value0correspondstodefaultmoveof
Stop.
6.ThepriorityoftheSwitchCaseblockishigher(1)thanthepriorityoftheSerialTransmitblock(20).
7.YoucanseethatboththeLineFollowermodeandtheManualmodesubsystemsgivethetorquefortheleftandthe
rightmotorasoutputs.TheseoutputsaremergedusingMergeblockandgivenasinputstoMotorssubsystem.The
priorityofSerialReceiveblockishigherthantheMotorssubsystem.
c)ReceivingthefeedbackseriallyonArduinoRobotControlBoard:
1.OpentheArduinoRobotControlBoardmodel.TheSerialReceiveblockwithPort1andSampletime0.002receives
thefeedbackfromtheArduinoRobotMotorBoard.
http://www.mathworks.com/help/supportpkg/arduino/examples/arduinorobotlinefollowerapplication.html

9/11

8/9/2016

LineFollowerApplicationforArduinoRobotMATLAB&SimulinkExample

2.ThedeterminetoneStateflowchartusesthedatareceivedbySerialReceiveblocktodeterminethetonetoplay.A
summaryofreceiveddataandselectedtoneisgivenbelow:
Datareceived|Tone(Hz)
_____________
1|50
2|125
0|0
_____________
3.PulseGeneratorblocksareusedtogenerate50Hzand125Hzfrequency.AConstantblockisusedfor0Hz.
4.ThesetonesaremergedusingMergeblockandgivenasinputtoDigitalOutputblockfromLibrary:Simulink
SupportPackageforArduinoHardware>Common.ThePinnumberissetto5correspondingtoBuzzer.
5.ThepriorityoftheSerialReceiveblockislesserthanSerialTransmitandthatofBuzzerblockisleast.

Task6RuntherespectivemodelsontheArduinoRobotControlBoardandtheArduinoRobotMotor
Board
Inthissection,youwilllearnthestepsrequiredtodeploytheSimulinkAlgorithmontheArduinoRobotControlBoardand
ArduinoRobotMotorBoard.
1.FirstconnecttheArduinoRobotControlBoardtoyourhostcomputerusingUSBcable.
2.IntheArduinoRobotControlBoardmodel,choosetheHardwareboardasArduinoRobotControlBoardunder
Simulation>ModelConfigurationParameters>HardwareImplementation.SelecttheCOMPorteitherManuallyor
Automaticallywhilekeepingrestofthesettingsasdefault.ThenclicktheDeploytoHardwarebuttononthetoolbar.
3.Afterthemodelisdownloaded,disconnecttheUSBcablefromyourArduinoRobotControlBoard.
4.ConnecttheArduinoRobotMotorBoardtoyourhostcomputerusingUSBcable.
5.IntheArduinoRobotMotorBoardmodel,choosetheHardwareboardasArduinoRobotMotorBoardunder
Simulation>ModelConfigurationParameters>HardwareImplementation.SelecttheCOMPorteitherManuallyor
Automaticallywhilekeepingrestofthesettingsasdefault.ThenclicktheDeploytoHardwarebuttononthetoolbar.
6.Afterthemodelisdownloaded,disconnecttheUSBcablefromyourArduinoRobotControlBoard.
7.PlacetheRobotonthegroundandturnthepowerswitchonpresentontheArduinoRobotMotorBoard.Themodelruns
ontheboardandtheArduinoRobotstartsonthedefaultmodeofLineFollower.WhenyoupressKey5itswitchesfrom
LineFollowertoManualmodeandisstoppedasperthedefaultactionunderManualmode.Thenbasedonkeypress1,2,
3,or4iteithermovesAnticlockwise,Backward,Forward,orClockwiseaslongasthekeyispressed.Onceyourelease
thekeytheRobotisinStopmode.OnpressingKey5,theRobotswitchestoLineFollowermode.
8.TurntheRobotswitchoff.

OtherThingstoTry

Verifytheworkingofeachsubsystemforperipheralaccessbyusingsomesampleinputs.Forexample,youcanuse
sampleinputsthroughConstantblocks(valuesrangingfrom255to255)tothesubsystemtocontrolmotorstoseeif
thewheelsspinforwardorbackward.

AdjustthePIDControllersettings.ImprovetheRobot'sabilitytomovefasterandtakesharperturns.

ThepresentexampleusesaconstantIRsensorthresholdvalue.TrytoimplementdynamiccalculationofIRsensor
thresholdwhichcanvaryduetoambientlight.

AconstantvalueofBaseTorqueisusedinthisexample.TrytoadjusttheBaseTorquedynamically.Forinstance,you
canmaketheRobotgofasterbyincreasingtheBaseTorqueforstraightmovement.Toensuresmootherturns,you
candecreasetheBaseTorque.

http://www.mathworks.com/help/supportpkg/arduino/examples/arduinorobotlinefollowerapplication.html

10/11

8/9/2016

LineFollowerApplicationforArduinoRobotMATLAB&SimulinkExample

TrytoaccessotherperipheralspresentontheArduinoRobotControlBoardandtheArduinoRobotMotorBoard.For
example,exploresendingofserialcommandstoArduinoRobotMotorBoardfromthePotentiometerontheArduino
RobotControlBoard.

Summary
ThisexampleshowshowtocreatealinefollowerapplicationandrunitonArduinoRobot.Thisexampleprovidesabasic
linefolloweralgorithmfortheArduinoRobot.Itwillbegreattoseeifyoucanmodifytheexistingalgorithmorimplementa
newonetomakeyourRobottracktheblacklinefasterandfollowthelineirrespectiveofanycrossingsoracuteangled
turnsinthetrackdesign.
Inthisexample,youhavelearnthowtoaccessthedifferentperipheralsofArduinoRobotMotorBoard,ArduinoRobot
ControlBoardusingtheblocksfromSimulinkSupportPackageforArduinoHardware.Youhavelearnthowtoestablish
serialcommunicationbetweentheArduinoRobotControlBoardandtheArduinoRobotMotorBoard.
ThetechniqueofaccessingdifferentperipheralsofArduinoRobotusingblocksfromSimulinklibraryandSimulinkSupport
PackageforArduinocanbeextendedtootherArduinobasedboards.

http://www.mathworks.com/help/supportpkg/arduino/examples/arduinorobotlinefollowerapplication.html

11/11

Вам также может понравиться