I wantedtodosomething a littlebitdifferent, andthat's beenmadepossiblebytheverygeneroussupportfrommypatrioticorpatriots, dependingonwhereyou'refromfor a while, I wantedtotackleembeddedsystemsas a topic, but I'velookedfor a suitableentrypointthatsimplyenoughtomakeinto a nicevideobutalsolaysthefoundationsforfuturevideowork.
Solet's gettoit.
Let's makeandprogramoverbioticarm.
Theaimofthisvideoistocontrol a robotarmwhileasleep.
Therearevariationsofthisdependingonthepolarityofyoursignals, but I'm notgoingtolookintothattoday.
Solet's programthemicrocontrollernow.
Now, firstly, I mustapologize.
UnlikeVisualStudio, I don't haveasmuchgraphicalcontroloverhowthebrowserIntegrateddevelopmentenvironmentlooksforembed, so I can't reallychangethefontandzoominquiteaseasilyas I caninvisualstudio, sowe'lljusthavetomakedo.
ButallofthesourcecodeforthiswillbeavailableinthegetupandisLinkablefromthedescriptionbelow a blankembedprogram.
Lookslikethisonthisistheenvironmenthaveputonthedarktheme, so I don't blindmyselfanditlooksjustlike a C plusplusprogrammingenvironments.
I'vegotsomefilesandprojectsdownhereontheleft.
I'vegot a codingwindowhereand I'vegotsomeoutputwindowatthebottomonthat.
Actually, we'regoes, we'regoingtoblinkoneoftheladiesontheboardaccordingtothe P W M Signalon, I couldspecifywhichparticularpieceofhardware I wantwhen I'm constructingthispinandontheembedconvenientlyit's ledonenow, thinkingbacktoPWemifyouem.
I'm notgoingtospecifythedutycyclebywritingtothat P W.
M.
Pin.
Thedutycycleis a percentage.
Soif I setthisasno 0.5, whatweshouldexpecttoseeistheledblinkingonenoughat 1/2 2ndintervalonbecause I'm oldfashionedand I don't wantmaintoexitjustincaseitresetstheembedcontroller.
I'm justgoingtositin a loop.
It's compileit.
Socompilingitwasverysimple.
Well, actually, allhavedonewasbuilding.
Butif I clickcompile, itwillrunthroughthebuildprocessonDhegivesme a filetodownload, whichwe'vejustseenhereonthisfileiscalledRobotarmdotlpc 1768 dotbeen, whichmeansit's a binaryfileonThat's thefilethat I'm goingtoDragondropontotheembedwhenitappearslike a flashdrive.
So I'm goingtosetthatToby 10 millisecondsinsteadon I'llintroduceanadditionalvariabletimeoninmywildloop.
I'm goingtosetthedutycycleforthatparticularpintobe a functionofftime.
Infact, specifically, I'm goingtosetittobe a signoftime.
ButSignewill, ofcourse, giveme a valuebetweenminusoneandplusone.
So I'm gonnaaddonetothatanddividethewholethingbytwo.
I wanttimetoincrease, so I'm justgoingtoadd a littletinybittoit.
Let's take a look.
So I justreprogrammedtheembedon I'veresetit, andwhatweseenowis a nice, gentlypulsingledone S o P.
W M isreallyusedforwheneveryouwantsomethingtolookanalog e in a digitaldomain, theledisflashingonenoughsoquickly, andwe'reincontrolofhowquicklyitisflashingonenoughthatwecouldmakeitlooklikedifferentintensities, a verycommontechniqueonanythingwhereyouseeladieswiththissortofbehavior.
Sooneofthepropertiesthatwillneedtosetisknownastheboardrateorbowedrate, dependingonwhereyou'refromandthisis a goodagreementbeforehand, thatspecifieshowmanybitspersecondaregoingtobetransmittedalongtheRxand T X lines, oneswhospecifiedthisspeedthatwhichareyouarewillusefortransmission.
Wealsoneedtospecifytheprotocol.
Ondaverycommonprotocolisknownaseight n.
Oneondhe.
Thistellsthe U.
R.
It's expecting a packageofeightbitswithonestopbitandnoparitychecks.
Andsofortransmitting a singlebiteacrossthe U.
R, wemayassumethatthereis a packetthatlooks a bitlikethiswherethefirstthingthatcomesalongiswhat's knownasthestartbit.
Thenatureofyourcircuitsneedtobeabletohandlehighspeeddigitalsignals, butalsothesystemprocessingthepacketateitherendalsoneedstobemuchfasterbecauseeventhoughherewe'vegot a veryslowclockspeedcomparedtothemicrocontrollerthatperipheralitshandling, the U.
Ifit's got a commportorit's gotheadersonthemotherboardfor a combat, itcantalkvery.
Youareinmyrobotarmapplication.
I'm facilitatingtheurviaBluetooth.
TheactualdatatransmittedviaBluetoothdoesn't lookreallyanythinglikethe U R signal, buttheBluetoothmodulewillreconstructitinto a U R signalthatmyembedmodulecanunderstand.
I could, ontheembedalsousedtheUSBinterfacedirectly.
Thisistheonethat's pluggedintothecomputertomakeitappearlike a flashdrive.
Youneedtodownloadanadditionaldrivertodothat, butitwillmakethe U.
S B portappearlike a virtualcomport.
Now I thinkit's timetolookatbuildingtheroboticarm.
I did a fullvideoaboutthis.
Aboutitwas a livestream Q and A session.
But I'veconvinceditnowinto a coupleofminutesforyoutohave a lookwhotheservermotorsprovidedwithmyrobotarm, wheretheMG 996 areallmetalgear, servomotors.
Anditlookslikethis.
Itcamewith a loadofperipherals I didn't actuallyneed.
Thatthatjointcanbesettothefirstmethod I'm goingtoaddtotheclassissettarget, whichtakes a floatingpointangleindegrees.
AuntclampsitaccordingtotheJointmenandJointMaxvariablesonjustforKickson, because I liketheabuseinthecomments, I'm goingtoadd a gettarget, too.
Next, I'm goingtoadd a functionthatreturnsthedutycyclefor a givenangle.
Giventheconstraintsthatwehave.
And I showedyouthemassforthis a minuteago.
Thismeans I canknowhowtoconstructor, and I'm goingtohave a constructivethattakessomedefaultvaluesinthiscase, a minimumangle, a maximumangleon a defaultpositionon I'm alsooddlyfornowgoingtoaddanupdatepositionfunctiononAllthisisgoingtodoforthisvideoistakethepositiononsetitfromthetargetsotheuserwillusethisjointclassbysettingthetarget, updatingthepositionon, thencalculatingthedutycyclebasedonthatposition.
Thereason I'veaddedthisinnis I think, inthefuture I wanttoplaywithslightlymorecomplicatedtrajectoryplanningbaseduponwhat's hisrequestedoftheservoon.
I cancheckifthebike's valueislessthan 10 forexample, then I knowthat I'm representing a jointnumber, butifitisgreaterthan 10 then I know I'm representing a jointtheaterthisway.
Itdoesn't matterreallywhatbitethesystemreceivesfirstbecauseitlet's sayitreceives a jointanglethat's goingtobegreaterthan 10.
It's justgoingtorejectit.
Sojustbyexaminingthedata, we'vecomeupwith a waytomakeitrobustagainststarterborder.
Theembedhasaccesstothe U R twopinsdirectly, butyoureallywanttousethemdirectly, probablyalwayswanttorunthemthrough a bufferfirst, justincaseyouembedisdoingsomethingelseandcan't respondtothepinsbeingactivatedforyou.
Ourtransfer.
So I'm goingtouse a librarythat's providedbysomeotherpersonintheembedcommunity, andthat's oneofthebrilliantthingsabouttheembedcommunity.
Buttohandlethatbufferforme.
So I'm goingtogotomyrobotarmProjectImportLibraryongofromImportWizard, whichpullsupthiswindowandinhere.
Thelivelythat I'm afterparticularlywasgoingtotypeinurandclickSearchandwe'llseewhatitcomesupwithonthe 1st 1 comesupisbufferedserial, so I'm goingtousethatontoselectitandclickimportnow.
Artisalreadyconfiguredwith a boardrateoff 9600 aneight n oneprotocol, asistheBluetoothmodule.
So I needtodonothingelseinordertouseit.
So I'm goingtogodowntomyinMaineandstartfillinginhowwereadfromthe U R.
Firstly, I'm goingtocreate a temporalvariablecalledTargetJointon I'm goingtointerrogate.
Areanybitesavailableinthebuffetformyyouart?
Andthat's donewiththereadablefunction.
Ifthereis a biteavailabletoread, thenreadit.
Pleaseanswer.
Get, seeifthevalueof C islessthan 10 thenweknowthatthat's ourtargetjoint.
Andifit's notlessthan 10 weknowitrepresents a jointtheater.
So I'm goingtotakethejointobjectforthetargetjointwe'vejusttemporarilystoredonDhesetitsanglerememberingtoeoffsetbyminus 128 tobringitbackdownto a minus 92 plus 90 range.
Andthat's reallyit.
That's howsimpleourembedprogramisgoingtobe.
Obviously, having a microcontrollerunitandBluetoothmoduleontheirownisnotgoingtodoanythingveryuseful.
NowtheBluetoothmodulelivesoverhere, so I'm gonnamakesurethatthisredwire, whichismyVCCthepowerfortheBluetoothmoduleisconnectedtothesamerowasthepowerinontheBluetoothmodule.
So I'm justgoingtopushthatandthebig, emptyspaceinthemiddlehere.
And I'm takingtheParangroundsdirectlyfromtheinputsomethingtopowerthisboardwith a sixvoltpowersupplyandthatgetssplitfromheresothepowerofthegroundsgostraighttotheservos.
Themoduleitselfwillprovide a threevaultthreeoutputformetopowertheBluetoothmodulebecause I don't wantThiooverpowertheBluetoothmodel, eventhoughitsaysitisparablebysixfaults.
If I haveanyproblemswithit, perhaps I canpowertheBluetoothmoduledirectlyoffthesupplytohowever, theservosupplyislikelytobequitenoisyandunstablesimplybecauseit's gotmotorsattachedtoit.
Giventhat I knownothingaboutthequalityoftheservosforthemotoraficionadosoutthere, I don't reallyknowiftheyhaveanycapacityforhandlingbackdrives.
Soifsomethingsomeinertialloaddrivestheserver, I couldquiteeasilyseemypowersupplyrailspikequitehighlyhere.
And I don't wantthatspiketopropagateontotheBluetoothmodule.
No, that's got a maximumofsixvotes.
So I'm usingtheregulatedsupplyofftheembedtoprovidefoodboughtfood.
But I'm alsonowjustgoingtoswitchonthepowerheadoutthatwaybecauseifyouremember, thefirstthingthat's ourembedisgoingtodoissettlethetargetpositionstosomeoneinzerooftherangereachestheservos.
Therewego.
That's quiteexcitingwithit.
Let's notworkonmakingthehostapplicationon.
We'rebackinfamiliarterritorynow.
Thisis a pixelgame.
Engineinstancewereconstructedto 400 by 400 pixels, whereeachpixelistoscreenpixelswide.
Soit's onlygoingtobe a smallwindow.
But I'vegotoverridesfortheunusedcreatefunctionandonnewsupdateon, I'vealsoaddedoneforunuseddestroy, which, ifyoudidn't know, alreadyexists.
What I wanttodoispresent a seriesofslidersthatcontroleachjoint, andtodothat, I'm goingtodo a firstforthechannel.
I'm goingtousesomethirdpartycodeonthisis a gooeylibraryextensioncreatedforpixelgameengineby a memberofthediscocommunitycalledSleepingBear.
Healsohas a YouTubechannel, and I'llputlinkstoboththiscodeonhischannelinthedescriptionbelowonThisjustallowsmetoquicklythrowsomefamiliargooeycomponentsonthescreen.
So I'm goingtoadd, oninstance, offhisgooeycontainerobjectandinmyownuse a createfunction.
I wouldaddsixsliders, oneforeachofthejoint, so I'm specifyingthe X and Y locationoftheslideronitslength, and I'm alsogoingtospecifysomedefaultvalues.
Nowtheslidersinthisnaiveimplementationgofrom 0 to 100.
Regardless, thelengthisthephysicallengthonthescreen, so I'm goingtotakethe 0 to 100 onconvertthattominus 92 plus 90.
Sothenumbersonthescreenareaccuratelygonnareflectwhatanglesthejoint a rat.
Butall I'm reallyafterhereis a quicksliderinonnewsupdate.
Butwhatwe'rereallyinterestedindoinghereistransmittingthisvaluetoourembeddedsystemonthisiswherethingsaregoingtoget a bitWindows e.
I'm afraiditis a similarprocessonLennoxwithsyntaxis a bitdifferent, giventhat I'veincludedaspartofthepixelgameenginewindowsdotage.
Inthisinstance, I'vegotaccesstovariousoperatingsystemfileroutinesonopening a CommPortisreallysimple, infact, toyourprogramisjustgoingtolooklike a regularfilestream.
Sothiswillgiveus a bufferoffjointnumber, jointangle, jointnumber, jointangle, joinednumberJoeangleallthewayforallsixjoints.
I thenwanttoperiodicallyrightthisbuffertothesystemnowonuserupdateisgoingtorunvery, veryquickly, Farfasterthantherateareyouartcancommunicateat.
So I wanttodosomethingon a particulartick.
I don't wanttodoiteveryframeon.
I liketodothisbyaccumulating f elapsedtime.
Soourcreatorvariablecalled f accumulatedtimeonOnlywhenthataccumulatedtimevariableisover a fixedamount.
Do I wanttowriteanythingtothehardwork?
RememberthispointtoeAlso, subtractyourweightamountfromaccumulatedtimeSothisgivesyou a veryprecisetickregardlessoftheframerateoftheapplicationrightintothedeviceisverysimple.
I need a temperavalue, Kel, be w whichisgoingtorepresentthenumberofbitesthathavebeensuccessfullywrittenbythefollowingcommand, whichisrightfiles.
Now, if I wasdoingthiswiththeprofessionalismofsomebodythatworksinindustry I wouldchecktomakesurethatthenumberofbiteswrittenisactuallyhowmany I thinkitison.