Professional Documents
Culture Documents
Explore(/tag/typeid/)
Publish(/about/create.jsp)
Login(/account/login/) | SignUp(/account/gopro)
HandTrackingMechanicalArmPyduino+LeapMotion by
poopypigeon(/member/poopypigeon/)inarduino(/tag/typeid/categorytechnology/channelarduino/)
Download
h(/id/HandTrackingMechanicalArmPyduinoLeapMotion/)
11Steps
Collection
.
IMadeit!
Favorite
Share
AboutThisInstructable
Hand tracking robotic arm - Pyduino + Leap Motion
8 9,085 views
173 favorites
License:
poopypigeon
(/member/poopypigeon/)
(http://t.co/Uz9jINioja)
Follow
24
(/member/poopypigeon/)
Bio:Iamgradstudentinappliedphysics.
Checkoutmygame:@poopypigeon
Morebypoopypigeon:
(/id/HandTracking
Hello!
InthistutorialIwillshowyouguysandgalshowtoassembleahandtracking
mechanicalarmwithaLeapMotionController,Arduinodevice,afewmotors,
somebalsawood,paperclips,hotglueandalittlepythoncode.Attheendof
thisyou'llhopefullyhaveamechanicalarmwhichwecancontrolusingaLeap
MotionController.Iwillwalkyouguysthroughanindepthprocessonhowto
hookupyourLeapMotioncontrollertogetjustoneservomotorworkingand
hopefullywhenyouseehowitsdonewithonemotoryoucanjustcopyand
pastethecodearoundafewtimestomakeitworkwith4.OfcourseI'llhelpyou
alongthewayanddon'tbeshyaboutaskinganyquestionsyouhavealongthe
way.Iaimtoprovideasufficientenoughdescriptionofhowthisallworkssothat
evenifyou'renotaPythonexpertyoucanusethesameprocessItakeinmy
pythoncodeandapplyittootherlanguagessuchthatyoucancommunicate
withyourArduinodeviceandLeapMotioncontroller.
Thisprojectiscomposedofseveralpartsthatwe'llworkthroughseparatelyand
thencombineattheend.
TheSteps
ProjectTheory+Explanation
MaterialsList
SettingupPyduinoLibrary
SettingupthePyduinoSketch
Settingupjustoneservomotor(Circuit)
ControllingtheservousingPyduino
SettinguptheLeapMotionController
LinkingtheLeapmotionControllertooneservomotor
Assemblingthearm
MechanicalArmPyduinoLeapMotion/)
(/id/Controlling
ArduinowithpythonbasedwebAPINo
p/)
(/id/Pyduino
InterfacingArduinowithPythonthrough
se/)
Workingoutthefinalcircuit
Testing,DebuggingandOptimizingthecode
Step1:ProjectTheory+Explanation
(/file/F47PUHKI9UDZXY1/)
Thefirstpartandmaybemostforeignpartofthisprojecttosomeisthehand
trackingpartofthings.Luckilywedonothavetodevelopthehandtracking
softwareorhardwareourselves.OurgoodfriendsatLeapMotionhaveinvented
averyniftycontrollerfordoingjustwhatwewant.Wewanttobeabletocontrol
ourarmwithouttouchinganysortofcontrollerandwecandojustthatwitha
LeapMotionController.Ifyouareunfamiliarwiththiscontrollerpleaseheadover
totheLeapMotionsiteandcheckoutafewoftheirvideos
https://www.leapmotion.com/.(https://www.leapmotion.com/.)TheLeapMotion
controlleriscomposedof3infraredLEDSthatemitaround850nanometersand
ithas2IRcamerastocapturemotion.Theideaofthecontrolleristhatthelight
fromtheIRLEDSreflectsoffourhandsandgetscapturedbythetwoIR
camerasonthedevice.Thedevicethenstreamsthedataitcapturedtothe
LeapMotiontrackingsoftwareonyourcomputer.Thetrackingsoftwareprobably
usessomesortofparallaxeffectandotheralgorithmstoreconstructa3D
representationofwhatthedevicesees.Thetrackingalgorithmtheninterprets
thedatasuchaswhatistheorientationandpositionofyourhandsandfingers.
Afterthealgorithmdeterminestothebestofitsabilitywhereyourhandis,the
dataisexportedtotheAPIwhichisreadyforyoutointerfacewithandpulldata
from.TheneatthingistheLeapMotioncontrollercaninterfacewithavarietyof
programminglanguagessoifdon'tknowPythonorcan'tdeciphermycodeyou
canuseyourownprogramminglanguageofchoice.
Wewillendupwritingalittlepieceofsoftwarethatlistensforthedatafromthe
LeapMotioncontroller.Oncewereceivethedatawewillnormalizeitbythesize
oftheLeapMotioncontroller'sinteractionareasothatwecanobtainafraction
ofhowfarinthespaceourhandis.Oncewehavethatfractionwecanmultiplyit
bytherangeofsomemotorandtellourArduinotomoveourservomotortothat
position.Wewillbeabletosynchronizeourhandmotionin3dimensionsto3
differentmotors.Movingaroundeachaxiswillcontroladifferentmotor.That
makessensebecausewhenwemoveourhandupfurtherawayfromtheLeap
Motioncontrollerwewantthattomoveourarmup,whichwecanmakehappen.
Ifyou'refamiliarwithhowanArduinoworksyouknowthatwehavetouploada
sketchontotheboardbeforewecanmakeuseofthedevice.Inorderforusto
dynamicallyinteractwiththeArduinoboardweneedtowritealittlepieceof
softwaresuchthatwecancontrolourArduinowithouthavingtoconstantlykeep
uploadingsketchesontoit.LuckilyIhavealittlepieceofcodethatwillfacilitate
thisprocess.Iwrotetwopreviousinstructabletutorialsonhowtointerfaceyour
ArduinowithPythonoverserialcommunication.That'sjustafancywayof
sayingwe'regoingtohaveourPythoncodesendalittlemessagetoourArduino
deviceandthenourArduinowillinterpretthemessageandthenperformsent
task.Don'tworryI'llwalkyouguysthroughabasicexampleofhowtosetupthe
PyduinolibraryonyourArduinodeviceandonyourcomputersothatwecan
starttalkingtoourArduino.Andifyoudon'tknowPythonIhaveasolutionfor
you!ThesketchthatweuploadontoourArduinodeviceisversatileenoughto
interfacewithusinganylanguagecapableofsendingserialmessagessoifdon't
wanttousePythontosendmessagestoourArduinoyoudon'thaveto!Butyou
willhavetowritealittleextracode.
Letmedirectyourattentionnowtothefigureabove.Letsdefinesome
terminologysoyoucanallunderstandwhatI'mtalkingaboutlater.Atthebase
ofourarmwehave2servomotors,wewillcallthesemotorstheAzimuthal
motor/angleandAltitudemotor/angle.Ifyouareuptodateonyour
observationalastronomyterminologytheAzimuthalangletypicallyrefersto
somecardinaldirection(N,W,E.S)sothisisthemotorthatwillmoveleftand
rightandtheAltitudeangleisthatwhichstartsatthehorizonandextends
upwardtosomestar.TheprimaryarmisthatwhichisattachedtotheAltitude
motor.ThesecondaryarmisattachedtowhatIliketocalltheWristmotorwhich
isattachedtotheprimaryarm.LastlywehaveTheClaw.(insertToystoryclip
here)Theclawisresponsibleforgrabbingthingsandisoperatedbyamotoras
well.
ADVERTISEMENT
Step2:MaterialsList
(/file/F97QU4YI9UDZR64/)
Getyomatshere!
Materials
ComputerwithPythoninstalled(https://www.python.org/)
LeapMotionController
ArduinoUno(orotherarduinodevice)
USBCable
Breadboard
Lotsofwires
2ParallaxStandardServoMotors
2MicroServos(SG90)
BalsaWoodPack(IgotmineatMichaels,pictureattached)
HotGlue
PaperClips
Pliers
XactoKnife
SandPaper
MostofthesesuppliesyoucanpickupataMicroCentershoporonline.For
buildingthestructureyoushouldn'tneedtogotoHomeDepotorLowes,you
canpickupmostofthesuppliesatyourlocalcraftshoporMichaels.
Balsawoodandhotglueisastrongercombinationthanyoumaythink.The
balsawoodreallyhelpsuskeepthetorqueontheservosduetothestructureat
aminimumcomparedtousingaluminumordenserwood.Italsomakesthe
servosmoreresponsivesincethereislessweighttotugaround.
Step3:SettingupPyduinoLibrary
Inorderforustobeabletomoveourmotorsontheflyweneedtobeabletotell
ourArduinodevice"Hey!Movethismotortothisangleyo!"Thiscanbedoneby
sendingamessagethroughtheUSBporttoourArduino.Forabasicoverview
onhowserialcommunicationworksbetweenArduinoandpythoncheckout
http://playground.arduino.cc/interfacing/python.
(http://playground.arduino.cc/interfacing/python.)Beforewebeginletmeinform
youonsomeofthelimitationsthatpythonhaswhencreatingArduinoprojects.
PythondoesnotcreateArduinosketches,norareyougoingtobeuploading
morethan1sketchtoyourboardthisentiretime.Instead,pythonisgoingtobe
sendingsmallstrings(onlyafewchars)toourArduinoboard,ourArduinoboard
willhaveapreloadedsketchassociatedwiththepyduinolibrarytointerpretour
smallmessagethenperformsaidtask.Theniftythingaboutthesketchwe'll
uploadtoourArduinoboardisthatitcanbeusedwithanyprogramming
languagecapableofsendingserialmessages.Soifyoudon'tlikePythonyou
canuseanotherlanguage
.
Checkoutthecodebelow:Youwillwanttosavethecodebelowtoafilecalled:
pyduino.py
You'llwanttoputthatfileinthedirectoryyou'reworkingin.Takeamomentto
readoverthepieceofcodeandseewhatitdoes.Thereisalsoapieceof
samplecodeatthebottomofthelibraryshowingyouhowtouseitwhichI'llget
intonext.Ifyouwouldliketoseeaworkingexampleofhowtousethelibraryto
controlsomethingotherthanaservomotorcheckoutmyotherinstructables.
IfyoudonotwanttousePythonthenyou'llhavetotakeamomentand
transcribethispieceofcodetoanotherlanguage.Formoreinformationabout
thepyduinolibrarycheckouthttps://github.com/lekum/pyduino
def__init__(self,serial_port='/dev/ttyACM0',baud_rate=9600,
read_timeout=5):
"""
InitializestheserialconnectiontotheArduinoboard
"""
self.conn=serial.Serial(serial_port,baud_rate)
self.conn.timeout=read_timeout#Timeoutforreadline()
print'Connectioninitiated'
defservo_write(self,pin_number,digital_value):
"""
Writesthedigital_valueonpin_number
Internallysendsb'WS{pin_number}:{digital_value}'overtheserial
connection
"""
command="WS{}:{}".format(str(pin_number),str(digital_value)).encode()
self.conn.write(command)
defclose(self):
"""
Toensureweareproperlyclosingourconnectiontothe
Arduinodevice.
"""
self.conn.close()
print'ConnectiontoArduinoclosed'
Step4:SettingUpPyduinoSketch
(/file/FJO4XB5I72P670X/)
InorderforustodynamicallycontrolourArduinodeviceweneedtouploada
sketchonitsothatitcaninterpretthemessageswesenditfromourPython
code.Thesetupofthissketchisprettystraightforward.TheArduinodevicewill
communicatewithourcomputerovertheserialport.Formoreinformationon
SerialfunctionsforArduinocheckout:
http://www.arduino.cc/en/Reference/Serial
(http://www.arduino.cc/en/Reference/Serial).TheArduinodevicewillcheckto
seeifithasanycharactersavailableintheserialreceivebufferandifitdoesit
willbeingtoparsetheincomingmessage.Uponreceivingthewholemessage
thedevicewilltheninterpretwhattodowhetheritbeareadfunction,write
functionorsettingapinmode.Thisissketchisversatileenoughtobeableto
setpinmodes,performreadandwriteofdigitalandanalogvaluesaswellas
performservowrites.Anyadditionalfunctionswillneedtobecodedinyourself
butforthepurposesofthisprojectwedonotneedtoaddanything.
Checkoutthepieceofcodebelow.Thisiswhatwe'regoingtousetogetone
servomotorworkingwiththeLeapMotionController.Wewanttobeabletoget
oneservoworkingbeforewegoaheadandgetall4inthere.Thisfileisalso
availableonthegithubpageforthisinstructableat:
https://github.com/theown1/pyduino_leapmotion/blob...
(https://github.com/theown1/pyduino_leapmotion/blob/master/one_servo.ino)
YoucangoaheadanduploadthissketchtoyourArduinodevice.Whenyou
uploadit,youshouldgetapicturethatlookssimilartotheoneabove.Onething
youmightneedtowritedownisthelocationofyourArduinodeviceonyour
computer,youcanfindthisatthebottomrightofthearduinoprogramafteryou
uploadasketch.Formemyarduinoislocatedat:/dev/ttyACM0
{
//parseinformation
//courtesyoflekum
operation=Serial.read();
delay(wait_for_transmission);//Ifnotdelayed,secondcharacterisno
tcorrectlyread
mode=Serial.read();
pin_number=Serial.parseInt();//Waitsforaninttobetransmitted
if(Serial.read()==':')
{
value_to_write=Serial.parseInt();//Collectsthevaluetobewri
tten
}
//ifwerecieveproperinputwriteservo
if(operation=='W')
{
if(mode=='S')
{
servo_write(pin_number,value_to_write);
}
}
}
}
Step5:Settingupjustoneservomotor(Circuit)
(/file/F7OHLY8I8ZQXD6Z/)
AlrightsoifyouhadthechancetolookovertheArduinosketchontheprevious
stepyoumayhavenoticedthatwepredefinedapinforourServoMotor,Pin2.
Goaheadandsetupyourarduinowithanyoneofyourservomotorsfollowing
thecircuitschematicabove.Ifyou'reusinganSG90microservolikeIamyou
mayhavenoticedthatthewirecolorsdonomatchthoseabove.Typicallythe
darkercoloredwirewillbetheground,soformethatwireisbrown.Powerwill
alwaysbethemiddlewirewhichleavesuswithjustonewireleftthatwewire
intooneofourdigitalpins.We'llusethatdigitalpintotelltheservowhereto
move.
Alrightnowthatourcircuitissetupitstimetogetthismotormovingusing
python!
Step6:ControllingtheservousingPyduino
MakesureyourArduinodeviceispluggedintoyourcomputer.Beforewegetour
LeapMotioncontrollersetupwewanttomakesurewecansuccessfullycontrol
oneservowithourPyduinolibrary.We'regoingtofollowanexamplesimilarto
thesweepexampleontheArduinositehoweverjustwithdifferentpinwirings.
Ourpythoncodeisprettysimple,we'regoingtoimportthepyduinolibraryso
thatwecanestablishaserialconnectiontoourArduinodeviceandthenitsas
simpleasusing2linesofcodetomoveourservo.
Savethepieceofcodebelowtoafilecalledone_servo_test.pyandmakesure
itisinthesamedirectoryasyourpyduino.pyfile.
Andthenyoucanrunitthroughtheterminalbytyping$python
one_servo_test.py
Don'tworrytheprogramwilllagalittlebitinthebeginningwhichensuresa
connectiontoyourArduinodeviceandthenyoushouldseeyourservostartto
move!Thecodeshouldbedocumentedenoughforyoutofigureouteverything
thatisgoingon.Ifyou'reunabletoestablishaconnectiontoyourarduinodevice
you'llrecieveanerrorthatsayssomethinglike:[serial.serialutil.SerialException:
couldnotopenport/dev/ttyACM0:[Errno2]Nosuchfileordirectory:
'/dev/ttyACM0']Thenyou'llneeddeclareyourarduinodeviceinthecodeusinga
differentserialport.TheserialportforyourArduinodevicecanbefoundatthe
bottomrightoftheArduinosoftwareafteryouuploadasketchtoyourboard.
Anothercomplicationthatyoumayrunintoisthatifyourservoisnotwiredupto
PIN#2onyourboardthenyou'llneedtoeitherchangeyourwiringorchange
theArduinosketchweuploadedontoourboard,checkthecodedocumentation
forhowandwheretodothat.
frompyduinoimport*
importtime
if__name__=='__main__':
#ifyourarduinowasrunningonaserialportotherthan'/dev/ttyACM0/'
#declare:a=Arduino(serial_port='/dev/tty')
a=Arduino()
#sleeptoensureampletimeforcomputertomakeserialconnection
time.sleep(3)
#declarethepinourservoisattachedto
#makesurethismatchesline26ofone_servo.ino
#thelinethatsays:intSERVO2_PIN=2;
PIN=2
try:
foriinrange(0,1000):
ifi%2==0:
print'180'
#moveservoonpintoanangleof170deg
a.servo_write(PIN,170)
else:
print'10'
#moveservoonpintoanangleof10deg
a.servo_write(PIN,10)
Step7:SettinguptheLeapMotionController
(/file/FYEPFAHI9PH3DQF/)
(/file/FKAPBYEI9PH3DQG/)
IfthisisyourfirsttimeusingtheLeapMotioncontrollerI'llshowyouhowtoset
upthefilesweneedtobeabletousethecontrollereffectively.Iamrunningall
ofthiscodefromanUbuntumachinesoforthoseWindowsandMacusersout
thereyourexperiencemaybeslightlydifferent.I'llguideyouthroughinstalling
thedevelopmentkitfromtheLeapMotiondevelopersiteandthenhopefully
whenyouplugourhardwareinitwillwork.Afterwardswecanstarttomakeour
controllerdocustomthingslikecontrolourservomotor!P.S.Ifyoualreadyhave
theLeapMotionSDKinstalledgototheLibrarysectionofthissteptolearnhow
tosetupourfirstprojectdirectory.
Headoverto:https://developer.leapmotion.com/
(https://developer.leapmotion.com/)You'llneedtosigninbeforeyoucan
downloadtheSDKforyourmachine.ForLInux:youwillhavetountartheSDK
usingacommandlike"tarxvzfLeapDeveloperKit...."AfterextractingtheDev
Kitreadthe"README"fileforinstructionsonhowtoinstallthelinux
dependenciesforLeapandthenchecktheinstructionsonhowtoinstallthe
SDKafterwards.Ifyou'renotrunningonalinuxmachinereadthe"README.txt"
fileanditwillhaveinstructionsonhowtoinstallthesoftwareforWindowsand
Mac.
DependingonwhichversionoftheAPIyou'regoinguse,checkout
https://developer.leapmotion.com/documentation/ind...
(https://developer.leapmotion.com/documentation/index.html?proglang=current)
foralltheavailableplatforms.TheAPIforeachlanguageiswelldocumented.
TakeamomenttoreadthroughtheAPIofyourchoicealittlebitsoyoucanget
abetterunderstandingofhowtheLeapMotionControllerandsoftwareworks
together.Hopefullyyouwereabletotoinstalleverythingcorrectly.Asyour
attorneyIadviseyoutotestyourinstallationnowbeforecontinuing.Plugging
yourLeapMotiondeviceintoyourcomputerwillleadtotheOFFpictureabove.
Simplyrunningthevisualizerwhilethecontrollerisoffwillresultinnooutput
fromthedeviceandshownovisuals.Sotocombatthisissueweneedtostart
the"leapd"service.Onalinuxmachinethiscandonebytyping"sudoleapd"
intoyourterminalandlettingthatprocessrun.Afteryoudothatyoushouldsee
anoutputthatsayssomethinglikeWebSocketserverstarted,LeapMotion
ControllerdetectedandFirmwareisuptodate.Ifyoudonotseethoseoutputs
takeamomenttoconsulttheallmightygoogleaboutanyissuesyou'rehaving.I
havetoadmitthatIhadsometroubleswhenIfirstinstalledthiscontrollerand
wasabletofindanswersongoogleandstackoverflow.Afteryougettheleapd
servicerunningyourLeapDevicewillturnonandyoushouldbeabletoseethe
3LEDslightup.(SeeONimageabove)Youshouldnowbereadytousethe
Visualizer.Whenwe'retestingandcreatingcodewewanttomakesurethe
leapdserviceisalwaysrunningotherwisewecan'tuseourdevice.
LIBRARYSECTION
Beforewecanmakeapieceofcodewe'llneedtosetupthelibrariesforour
project.ToseewhichlibrariesyouneedtocopyfromtheSDKtarballabove
checkoutthislink:https://developer.leapmotion.com/documentation/pyt...
(https://developer.leapmotion.com/documentation/python/devguide/Project_Setu
p.html)(You'llneedadifferentlinkifyou'renotusingthePythonAPI)Forour
pythoncodewe'llneedtogotothedirectory:
LeapDeveloperKit_*_linux/LeapSDK/lib/tofindthelibraries.Afterwardsyou'll
needtocopytheLeap.pyfileandthetwosharedobjectfilesinthex64orx86
directorytowhicheverdirectoryyourotherpiecesofcodearein.
Inyourworkingdirectoryyoushouldnowhavethefollowingfiles
one_servo.ino#arduinosketchforserialinterfacingwithoneservo
one_servo_test.py#pythoncodetotestoneservow.oleapmotion
pyduino.py#serialinterfacinglibraryforarduino
LeapLib/#directorywithLeapMotionlibraries
Leap.py#thesearefromLeapSDK
LeapPython.so
libLeap.so
Inthenextstepwe'llsetupourfirstLeapMotionscripttostreamdatafromthe
deviceandthenusethedatatomoveouroneservomotor.
Step8:LinkingtheLeapmotionControllertooneservo
motor
NowthatyouhopefullyhaveyourLeapMotiondeviceupandrunningweare
goingtocreateourfirstprogramtostreamdatafromthedeviceandthenwe're
goingtolinkituptoourservomotorwiththepyduinolibrary.Anytimeweruna
scriptweneedtohavetheleapdservicerunning.Toknowthatthisserviceis
runningyoushouldbeabletoseethe3illuminatedLEDsonyourLeapdevice.
(SeeONimageinpreviousstep)
OurfirstLeapscriptwillbecomposedofseveralkeycomponents.Thereisa
controllerclasswhichconnectstotheLeapDeviceandalistenerclasswhich
willgetthedatafromthetrackingsoftware.Foranindepthtutorialonhowtoset
upasimpleLeapscriptcheckout:
https://developer.leapmotion.com/documentation/pyt...
(https://developer.leapmotion.com/documentation/python/devguide/Sample_Tut
orial.html)
I'llgiveabriefoverviewofwhatthepieceofcodebelowdoesandhowitworks.
Inthemainpartofthecodeweinitializeaninstanceofourlistenerandcontroller
classesafterwardsweaddthecustomlistenertoourcontroller.Theimportant
partofthescriptiswhatoccurswithinthelistener.Thelistenerisaclassthatwill
obtaindatafromtheLeapdeviceandassesswhattodowiththeinformation
beingstreamed.Thelistenerhastheabilitytoobtain60framesasecondfrom
thedevicehoweverthatisalittletoomuchinformationforwhatwewant.You're
certainlywelcometotryandusethe60framesasecondtocontrolyourArduino
howeverkeepinmindittakesasmallamountoftimetosendamessagetothe
Arduinodeviceandhaveitrespond.Tofixthisissuewe'reonlygoingtowork
with10framesasecondinsteadof60.Thelistenerhasafewbasicfunctions:
OnInitialize,OnConnect,OnDisconnect,OnExit,OnFrame.They'reallpretty
selfexplanatoryfromthenamesbutletsgothroughthemonebyoneand
explainhowwe'regoingtousethemtocontrolourservo.
OnInitializeHerewewanttodeclareourArduinoandestablishaserial
connectiontoit
OnConnectWecanenablesomegesturesforthecontroller,noneedtodo
anythingwiththeArduinohere
OnDisconnectNothingreallyhere.
OnExitWearegoingtoresettheservopositionandclosetheconnectionto
ourArduinodevice
OnFrameThefirstthingtodoisgetthetimebecauseweonlywanttogeta
frameevery10msorso.Onceweobtaintheframefromthecontrolleritwilltell
uswhichhandisintheframeandwhatitspositionis.Fromthiswecanextract
fingerandjointpositionsaswell.Wearegoingtousethenormalizedpositionof
ourhandintheinteractionboxandmultiplythatbytherangeonourmotor.For
instanceifourhandisallthewayleft,thenormalizedpositionwillbe0andso
theangleonourservowillbe0aswell.Formoreinsightyoucanreaduponthe
interactionboxintheLeapAPIdocumentation:
https://developer.leapmotion.com/documentation/pyt...
(https://developer.leapmotion.com/documentation/python/api/Leap.InteractionBo
x.html)Anotherusefullinktolookatistheonefortheframebecausetheframe
storesallourinformationaboutthehandandpositionsofeverything.Tosee
whatinformationisaccessiblewithinourframecheckoutthislink:
https://developer.leapmotion.com/documentation/pyt...
(https://developer.leapmotion.com/documentation/python/api/Leap.Frame.html)
WearegoingtousethesameArduinocircuitthatwecreatedearlier.Ifyou
happenedtochangetheservopintosomethingotherthan2youwillneedto
modifythecodebelow(line17).IfyouareunabletogettheArduinopart
workingmakesureyouhavetheserialconnectiononthecorrectport(line23).
Lines78,82arewhatcontroltheservomotor.Whenyourunthecodetakesome
timetomoveyourhandaroundandseewhatgetsoutput.Thepositionofyour
handwillbeoutputintermsofthenormalizedpositionintheinteractionbox.
Beforeyourunthiscodemakesurethatyourleapdserviceisrunningandthat
yourArduinoispluggedintoyourcomputer.
my_first_leap.py
#SimpleLeapmotionprogramtotrackthepositionofyourhandandmoveonese
rvo
#importthelibrarieswheretheLeapMotionSDKis
importsys
sys.path.insert(0,"LeapLib/")
importLeap,thread,time
fromLeapimportCircleGesture,KeyTapGesture,ScreenTapGesture,SwipeGesture
frompyduinoimport*
classSampleListener(Leap.Listener):
oldtime=time.time()
newtime=time.time()
#FIXMEifservoisnotattachedtopin2
SERVO_PIN=2#AzimuthalServomotorpin
AZIMUTHAL_LIMIT=180#wewantourmotortogobetween0and180
defon_init(self,controller):
#ifyourarduinowasrunningonaserialportotherthan'/dev/ttyACM
0/'
#declare:a=Arduino(serial_port='/dev/ttyXXXX')
self.a=Arduino()
#sleeptoensureampletimeforcomputertomakeserialconnection
time.sleep(3)
Step9:Assemblingthearm
(/file/FJRR8MWI9Q9HZC9/)
(/file/FU9LW3II9UDYDBD/)
(/file/FPWFCOZI9UDYDCC/)
(/file/FVN15VAI9UDYSA5/)
(/file/FK2CZDNI9UDYDCP/)
(/file/FV8ZWV2I9UDYDD2/)
(/file/FQAG0UAI9UDYSA6/)
Hopefullyyoucanseehowwe'regoingtosetuptherestoftheprojectfromour
littleexamplewithmovingjustoneservo.Ifyougottheoneservoworkingwith
theLeapMotioncontrollerweprettymuchhavetherestoftheprojectinthe
bag.Allweneedtodoissettherangesforourotherservosandthentellthe
Arduinotomovethoseservosandthat'sprettymuchtherestofthecodingthat's
required.
UnfortunatelyIdonothavesolidinstructionsonhowtoassemblethearm
becauseitwasatrialanderrorprocessformyselfandIthinkitwillbeagood
puzzleforyouguystoo:)I'lldomybesttoexplainthedesignandshowpictures
ofeacharmbutitwillbeuptoyouguystodotheassembly.IusedonlyBalsa
wood,hotglueandpaperclipstoassemblemyarm.Youcertainlyaren't
restrictedtocreatingithowIhave,feelfreetouseyourownjudgementonthis
one.Someoftheservomotorsarescrewedintothebalsawoodforextra
support,Iusescrewsthatareaboutasbigastheonesyouwouldusetoscrew
anarmintooneofyourservomotors.
Step10:Workingoutthefinalcircuit
(/file/FE5LTNFI9UDZLT5/)
(/file/FGRD99TI9UDZR5H/)
(/file/FO9FYK8I9UDZR5I/)
Thefinalcircuitlooksalittlecomplicatedbutitsreallynot.Weonlyhaveatotal
of6pinsattachedtoourarduinodevicewhenweusethebreadboard.Youwill
have4digitalpins(2,3,4,5)occupiedandthenapowerandgroundone.Onthe
breadboardyou'llsee4mainrowsofwires,eachrowcorrespondstoasingle
servo.Thepowerforeachservomotorisattachedtothepowercolumnonthe
breadboardwhichiswiredtothe5Voutonthearduino.Remembertoputthe
breadboardclosetothearmsothewiresconnectedtotheservoarelong
enoughtoreach.Itrustyouguystobeabletowirein4servos,itsnotrocket
science:p
BeforeyoucanusetheLeapMotiontocontrolallfourservosweneedtoupload
anewsketchontoourArduinodevicethatwillletusdothat.Atthemomentour
currentsketchcanonlysupportonemotor.Checkoutthesketchbelow,ifyou
arenotplanningonusingpins2,3,4,5tocontrolyourservomotorsthenyouwill
needtochangelines30,31,32,33.
https://github.com/theown1/pyduino_leapmotion/blob...
(https://github.com/theown1/pyduino_leapmotion/blob/master/4ServoSketch.ino)
/*
*SketchtocontroltheservopinsofArduinoviaserialinterface
*
*/
#include
charoperation;//Holdsoperation(R,W,...)
charmode;//Holdsthemode(D,A)
intpin_number;//Holdsthepinnumber
intdigital_value;//Holdsthedigitalvalue
intanalog_value;//Holdstheanalogvalue
intvalue_to_write;//Holdsthevaluethatwewanttowrite
intwait_for_transmission=5;//Delayinmsinordertoreceivetheserialda
ta
//createservoobjecttocontrolaservo
//amaximumofeightservoobjectscanbecreated
ServoSERVO2;
ServoSERVO3;
ServoSERVO4;
intSERVO2_PIN=2;//azimuthangle
intSERVO3_PIN=3;//altitudeangle
intSERVO4_PIN=4;//wristangle
voidsetup(){
Serial.begin(9600);//SerialPortat9600baud
Step11:Testing+Debuggingthecode
Hand tracking robotic arm - Pyduino + Leap Motion
Alrighthopefullyyouhavesuccessfullyassembledanarmoratleasthavefour
servosconnectedtoyourArduinodevicewhicharereadytotest.We'regoingto
setthecodeupmuchlikehowwesetitupwithjustoneservo.Gettingtheclaw
workwillinvolvealittlemorethanwhatwedidearlybutitsstillnottoohard.For
theclawallwe'regoingtodoiscalculateanormalizeddistancebetweenour
pointerfingerandthumb.Tofindthedistancebetweenourtwopointswewill
needtoknowalittlevectorarithmetic.We'regoingtofindthedifference
betweenthevectorpointingtoourthumbandpointerfinger.Afterwardswecan
calculatethedistance/magnitudebyfindingthe2normwhichyoucansortof
thinkofasa3DPythagoreantheorem.Toseehowthisisdonecheckoutlines
91to107inthecodebelow.Ifyouhaveanycommentsaboutthecodefeelfree
toaskbutitshouldbefairlystraightforwardifyou'vebeenabletofollowalong
sofar.
Ifyoucan'tgetthecodetoworkmakesureyouhaveyourleapdservicerunning
andyourarduinodeviceconnectedtoyourcomputer.Youcanalsotrytoaddin
thecontrolforthefourservosoneservoatatimeifyougotthecodeinthe
previoussteptowork.
Optimizingthecode
Thereareafewtweakstomakelikethenormalizationlimitforyourpointerand
thumbfingeraswellastherangesontheservos.Isettherangesontheservo
basedonhowIattachedthemotorstothearmbecauseIdidn'twantthemotors
movingthethearmintothegroundorintotheArduinodeviceitself.Youcanfind
thelimitforthenormalizationbetweenthethumbandpointerfingeronline104.
Alsotherangesforthemotorscanbefoundonlines118,119,120.
Thecodecanalsobefoundhere:
https://github.com/theown1/pyduino_leapmotion/blob...
(https://github.com/theown1/pyduino_leapmotion/blob/master/leap_4_joy.py)
#SimpleLeapmotionprogramtotrackthepositionofyourhand
#importthelibrarieswheretheLeapMotionSDKis
importsys
sys.path.insert(0,"LeapLib/")
importLeap,thread,time
fromLeapimportCircleGesture,KeyTapGesture,ScreenTapGesture,SwipeGesture
frompyduinoimport*
importnumpyasnp
classSampleListener(Leap.Listener):
finger_names=['Thumb','Index','Middle','Ring','Pinky']
bone_names=['Metacarpal','Proximal','Intermediate','Distal']
state_names=['STATE_INVALID','STATE_START','STATE_UPDATE','STATE_END']
oldtime=time.time()
newtime=time.time()
defon_init(self,controller):
#ifyourarduinowasrunningonaserialportotherthan'/dev/ttyACM
0/'
#declare:a=Arduino(serial_port='/dev/ttyXXXX')
self.a=Arduino()
#sleeptoensureampletimeforcomputertomakeserialconnection
time.sleep(3)
#DefinePinsforArduinoServos
Wehaveabenicecommentpolicy.
Pleasebepositiveandconstructive.
wIMadeit!
abhilash1(/member/abhilash1)
Helpmeplease
AddImages
PostComment
7monthsago
Reply
(https://cdn.instructables.com/FZU/W5I7/ILJWKHUF/FZUW5I7ILJWKHUF.LARGE.jpg)
X
8
poopypigeon(/member/poopypigeon)(author) . abhilash1(/member/abhilash1)
Hi,
7monthsago
Reply
Iamnottoofamiliarwithdevelopingthistypeofthingonwindows.Ithink
theDLLfortheleapmotionlibraryneedstobecompiledinaspecialway.I
wouldreccomendstartingattheleapmotionsdkpageforofficial
instructionsonhowinstallingtheirsdkforwindows.Iwasdevelopingmy
projectonlinuxwhichisslightlydifferent.
https://developer.leapmotion.com/documentation/index.html?
proglang=current
abhilash1(/member/abhilash1) . poopypigeon(/member/poopypigeon)
imadeit
Reply
7monthsago
thanksforyoursupport
X
8
poopypigeon(/member/poopypigeon)(author)
ayearago
Reply
Iappreciateyourhelpwiththeproperlicensingandreferences.Thesource
codefortheprojecthasnowbeenchangedtoreflectthecommentsandto
reducetheamountofextraneousinformationbeinggiventoreader.
lekum(/member/lekum)
ayearago
Reply
Hi,
I'mtheoriginalauthorofthelibrarypyduino(https://github.com/lekum/pyduino).
Asitisfreesoftware,Idon'thaveanyproblemwithyouincorporatingtoyour
project,orenhancingit,but:
1)ItislicensedasGNUGPLv2
(https://github.com/lekum/pyduino/blob/master/LICENSE),anyderivative(as
yourwork)shouldbeequallylicensed.Idonotseethelicensewithyoursource
code.Thisisveryimportant.
2)Youshouldgiveattributiontotheoriginalcodebaseonwhichyouhavebased
yourwork,mentionandlinkit
3)AsyouareusingGitHub,thepreferredwaywouldbetoforkmyrepositoryso
thatthiscouldbetraceableandeventhinkingincontributingbacktotheoriginal
library(Iacceptpullrequests,ofcourse,thisisFreeSoftware)
Cheers,
yamila_moreno(/member/yamila_moreno)
ayearago
Reply
Hi!Niceandcooltutorial.
Justaquestion,didyou,bychance,tookthepyduinocodefromhere?
https://github.com/lekum/pyduino/blob/master/pyduino/pyduino.pyItlookspretty
muchthesame,eventhecommentsinthecode:)
X
8
poopypigeon(/member/poopypigeon)(author) . yamila_moreno
ayearago
(/member/yamila_moreno)
Reply
Ahyesthatistheoldversionoflibrary.Thatversiondoesnotsupport
servocontrol.
HabitablePlanet(/member/HabitablePlanet)
ayearago
Reply
ayearago
Reply
<3!Verycool.ThisshouldtotallywintheMoveItcontest!
NeelDhebar(/member/NeelDhebar)
Thanksforthetutorial.IfinallyknowhowtoprogramArduinoinPython.
AlthoughIwillusetheArduinoLanguageinmostofmyprojects.
X
|
8
seamster(/member/seamster)
ayearago
Reply
Reallyimpressivestuff!Thankyouforsharingthis!
FEATURED CHANNELS
Woodworking
Paper
KitchenHacks
Puzzles
Space
IceCream
Homesteading
3DPrinting
Sewing
(/tag/type
(/tag/type
(/tag/type
(/tag/type
(/tag/type
(/tag/type
(/tag/type
(/tag/type
(/tag/type
id/category
id/category
id/keyword
id/category
id/keyword
id/keyword
id/category
id/category
id/category
workshop/channelcraft/channel kitchen%20hacks/?play/channel
astronomy/? ice%20cream/? home/channel technology/channel
craft/channel
woodworking/)
paper/)
sort=FAVORITES)
puzzles/)
sort=FAVORITES)sort=FAVORITES)homesteading/) 3DPrinting/)
sewing/)
Newsletter
Join2million+toreceiveinstant
DIYinspirationinyourinbox.
enteremail
I'min!
Mobile
Downloadourapps!
Android(https://play.google.com/store/apps/details?id=com.adsk.instructables)
iOS(https://itunes.apple.com/app/instructables/id586765571)
Windows(http://apps.microsoft.com/windows/enus/app/7afc8194c771441a959054250d6a8300)
AboutUs
FindUs
WhoWeAre(/about/)
Facebook(http://www.facebook.com/instructables)
Advertise(/advertise/)
Youtube(http://www.youtube.com/user/instructablestv)
Contact(/about/contact.jsp)
Twitter(http://www.twitter.com/instructables)
Jobs(/community/PositionsavailableatInstructables/)
Help(/id/howtowriteagreatinstructable/)
Resources
ForTeachers(/teachers/)
ArtistsinResidence(/air)
GiftPremiumAccount(/account/give?sourcea=footer)
Forums(/community/)
Answers(/tag/typequestion/?sort=RECENT)
Sitemap(/sitemap/)
Pinterest(http://www.pinterest.com/instructables)
Google+(https://plus.google.com/+instructables)
TermsofService(http://usa.autodesk.com/adsk/servlet/item?siteID=123112&id=21959721) |
PrivacyStatement(http://usa.autodesk.com/adsk/servlet/item?siteID=123112&id=21292079) |
LegalNotices&Trademarks(http://usa.autodesk.com/legalnoticestrademarks/) | MobileSite(http://www.instructables.com)
(http://usa.autodesk.com/adsk/servlet/pc/index?id=20781545&siteID=123112)
2016Autodesk,Inc.