gtsam.lyx 71 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769
  1. #LyX 2.2 created this file. For more info see http://www.lyx.org/
  2. \lyxformat 508
  3. \begin_document
  4. \begin_header
  5. \save_transient_properties true
  6. \origin unavailable
  7. \textclass article
  8. \begin_preamble
  9. \usepackage{times}
  10. \usepackage{listings}
  11. \usepackage[noend]{algpseudocode}
  12. \usepackage[usenames,dvipsnames]{color}
  13. % This is the color used for MATLAB comments below
  14. \definecolor{MyDarkGreen}{rgb}{0.0,0.4,0.0}
  15. % For faster processing, load Matlab syntax for listings
  16. \lstloadlanguages{Matlab}%
  17. \lstset{language=Matlab, % Use MATLAB
  18. frame=single, % Single frame around code
  19. basicstyle=\small\ttfamily, % Use small true type font
  20. keywordstyle=[1]\color{Blue}\bf, % MATLAB functions bold and blue
  21. keywordstyle=[2]\color{Purple}, % MATLAB function arguments purple
  22. keywordstyle=[3]\color{Blue}\underbar, % User functions underlined and blue
  23. identifierstyle=, % Nothing special about identifiers
  24. % Comments small dark green courier
  25. commentstyle=\usefont{T1}{pcr}{m}{sl}\color{MyDarkGreen},
  26. stringstyle=\color{Purple}, % Strings are purple
  27. showstringspaces=false, % Don't put marks in string spaces
  28. tabsize=5, % 5 spaces per tab
  29. %
  30. %%% Put standard MATLAB functions not included in the default
  31. %%% language here
  32. morekeywords={normpdf,normcdf,Pose2,Pose2SLAM},
  33. %
  34. %%% Put MATLAB function parameters here
  35. morekeywords=[2]{on, off, interp},
  36. %
  37. %%% Put user defined functions here
  38. morekeywords=[3]{FindESS, homework_example, gtsamSharedNoiseModel_Sigmas},
  39. %
  40. morecomment=[l][\color{Blue}]{...}, % Line continuation (...) like blue comment
  41. numbers=left, % Line numbers on left
  42. firstnumber=1, % Line numbers start with line 1
  43. numberstyle=\tiny\color{Blue}, % Line numbers are blue
  44. stepnumber=1 % Line numbers go in steps of 1
  45. }
  46. \end_preamble
  47. \use_default_options false
  48. \maintain_unincluded_children false
  49. \language english
  50. \language_package default
  51. \inputencoding auto
  52. \fontencoding T1
  53. \font_roman "ae" "default"
  54. \font_sans "default" "default"
  55. \font_typewriter "default" "default"
  56. \font_math "auto" "auto"
  57. \font_default_family rmdefault
  58. \use_non_tex_fonts false
  59. \font_sc false
  60. \font_osf false
  61. \font_sf_scale 100 100
  62. \font_tt_scale 100 100
  63. \graphics default
  64. \default_output_format default
  65. \output_sync 0
  66. \bibtex_command default
  67. \index_command default
  68. \paperfontsize 10
  69. \spacing onehalf
  70. \use_hyperref false
  71. \papersize custom
  72. \use_geometry true
  73. \use_package amsmath 1
  74. \use_package amssymb 1
  75. \use_package cancel 0
  76. \use_package esint 0
  77. \use_package mathdots 1
  78. \use_package mathtools 0
  79. \use_package mhchem 1
  80. \use_package stackrel 0
  81. \use_package stmaryrd 0
  82. \use_package undertilde 0
  83. \cite_engine natbib
  84. \cite_engine_type authoryear
  85. \biblio_style plainnat
  86. \use_bibtopic false
  87. \use_indices false
  88. \paperorientation portrait
  89. \suppress_date false
  90. \justification true
  91. \use_refstyle 0
  92. \index Index
  93. \shortcut idx
  94. \color #008000
  95. \end_index
  96. \paperwidth 7.44in
  97. \paperheight 9.68in
  98. \leftmargin 1in
  99. \topmargin 1in
  100. \rightmargin 1in
  101. \bottommargin 1in
  102. \secnumdepth 3
  103. \tocdepth 3
  104. \paragraph_separation indent
  105. \paragraph_indentation default
  106. \quotes_language english
  107. \papercolumns 1
  108. \papersides 1
  109. \paperpagestyle default
  110. \tracking_changes false
  111. \output_changes false
  112. \html_math_output 0
  113. \html_css_as_file 0
  114. \html_be_strict false
  115. \end_header
  116. \begin_body
  117. \begin_layout Title
  118. Factor Graphs and GTSAM:
  119. \begin_inset Newline newline
  120. \end_inset
  121. A Hands-on Introduction
  122. \end_layout
  123. \begin_layout Author
  124. Frank Dellaert
  125. \begin_inset Newline newline
  126. \end_inset
  127. Technical Report number GT-RIM-CP&R-2014-XXX
  128. \end_layout
  129. \begin_layout Date
  130. September 2014
  131. \end_layout
  132. \begin_layout Standard
  133. \begin_inset CommandInset include
  134. LatexCommand input
  135. filename "common_macros.tex"
  136. \end_inset
  137. \end_layout
  138. \begin_layout Section*
  139. Overview
  140. \end_layout
  141. \begin_layout Standard
  142. In this document I provide a hands-on introduction to both factor graphs
  143. and GTSAM.
  144. This is an updated version from the 2012 TR that is tailored to our GTSAM
  145. 3.0 library and beyond.
  146. \end_layout
  147. \begin_layout Standard
  148. \series bold
  149. Factor graphs
  150. \series default
  151. are graphical models
  152. \begin_inset CommandInset citation
  153. LatexCommand citep
  154. key "Koller09book"
  155. \end_inset
  156. that are well suited to modeling complex estimation problems, such as Simultane
  157. ous Localization and Mapping (SLAM) or Structure from Motion (SFM).
  158. You might be familiar with another often used graphical model, Bayes networks,
  159. which are directed acyclic graphs.
  160. A
  161. \series bold
  162. factor graph,
  163. \series default
  164. however, is a
  165. \emph on
  166. bipartite
  167. \emph default
  168. graph consisting of factors connected to variables.
  169. The
  170. \series bold
  171. variables
  172. \series default
  173. represent the unknown random variables in the estimation problem, whereas
  174. the
  175. \series bold
  176. factors
  177. \series default
  178. represent probabilistic constraints on those variables, derived from measuremen
  179. ts or prior knowledge.
  180. In the following sections I will illustrate this with examples from both
  181. robotics and vision.
  182. \end_layout
  183. \begin_layout Standard
  184. The GTSAM toolbox (GTSAM stands for
  185. \begin_inset Quotes eld
  186. \end_inset
  187. Georgia Tech Smoothing and Mapping
  188. \begin_inset Quotes erd
  189. \end_inset
  190. ) toolbox is a BSD-licensed C++ library based on factor graphs, developed
  191. at the Georgia Institute of Technology by myself, many of my students,
  192. and collaborators.
  193. It provides state of the art solutions to the SLAM and SFM problems, but
  194. can also be used to model and solve both simpler and more complex estimation
  195. problems.
  196. It also provides a MATLAB interface which allows for rapid prototype developmen
  197. t, visualization, and user interaction.
  198. \end_layout
  199. \begin_layout Standard
  200. GTSAM exploits sparsity to be computationally efficient.
  201. Typically measurements only provide information on the relationship between
  202. a handful of variables, and hence the resulting factor graph will be sparsely
  203. connected.
  204. This is exploited by the algorithms implemented in GTSAM to reduce computationa
  205. l complexity.
  206. Even when graphs are too dense to be handled efficiently by direct methods,
  207. GTSAM provides iterative methods that are quite efficient regardless.
  208. \end_layout
  209. \begin_layout Standard
  210. You can download the latest version of GTSAM at
  211. \begin_inset Flex URL
  212. status open
  213. \begin_layout Plain Layout
  214. http://tinyurl.com/gtsam
  215. \end_layout
  216. \end_inset
  217. .
  218. \end_layout
  219. \begin_layout Standard
  220. \begin_inset CommandInset toc
  221. LatexCommand tableofcontents
  222. \end_inset
  223. \end_layout
  224. \begin_layout Standard
  225. \begin_inset Newpage pagebreak
  226. \end_inset
  227. \end_layout
  228. \begin_layout Section
  229. Factor Graphs
  230. \end_layout
  231. \begin_layout Standard
  232. Let us start with a one-page primer on factor graphs, which in no way replaces
  233. the excellent and detailed reviews by
  234. \begin_inset CommandInset citation
  235. LatexCommand citet
  236. key "Kschischang01it"
  237. \end_inset
  238. and
  239. \begin_inset CommandInset citation
  240. LatexCommand citet
  241. key "Loeliger04spm"
  242. \end_inset
  243. .
  244. \end_layout
  245. \begin_layout Standard
  246. \begin_inset Float figure
  247. placement h
  248. wide false
  249. sideways false
  250. status open
  251. \begin_layout Plain Layout
  252. \align center
  253. \begin_inset Graphics
  254. filename images/hmm.pdf
  255. scale 60
  256. BoundingBox 40bp 37bp 400bp 150bp
  257. clip
  258. \end_inset
  259. \begin_inset Caption Standard
  260. \begin_layout Plain Layout
  261. \begin_inset CommandInset label
  262. LatexCommand label
  263. name "fig:unrolledHMM"
  264. \end_inset
  265. An HMM, unrolled over three time-steps, represented by a Bayes net.
  266. \end_layout
  267. \end_inset
  268. \end_layout
  269. \end_inset
  270. \end_layout
  271. \begin_layout Standard
  272. \begin_inset ERT
  273. status open
  274. \begin_layout Plain Layout
  275. \backslash
  276. noindent
  277. \end_layout
  278. \end_inset
  279. Figure
  280. \begin_inset CommandInset ref
  281. LatexCommand ref
  282. reference "fig:unrolledHMM"
  283. \end_inset
  284. shows the
  285. \series bold
  286. Bayes network
  287. \series default
  288. for a hidden Markov model (HMM) over three time steps.
  289. In a Bayes net, each node is associated with a conditional density: the
  290. top Markov chain encodes the prior
  291. \begin_inset Formula $P(X_{1})$
  292. \end_inset
  293. and transition probabilities
  294. \begin_inset Formula $P(X_{2}|X_{1})$
  295. \end_inset
  296. and
  297. \begin_inset Formula $P(X_{3}|X_{2})$
  298. \end_inset
  299. , whereas measurements
  300. \begin_inset Formula $Z_{t}$
  301. \end_inset
  302. depend only on the state
  303. \begin_inset Formula $X_{t}$
  304. \end_inset
  305. , modeled by conditional densities
  306. \begin_inset Formula $P(Z_{t}|X_{t})$
  307. \end_inset
  308. .
  309. Given known measurements
  310. \begin_inset Formula $z_{1}$
  311. \end_inset
  312. ,
  313. \begin_inset Formula $z_{2}$
  314. \end_inset
  315. and
  316. \begin_inset Formula $z_{3}$
  317. \end_inset
  318. we are interested in the hidden state sequence
  319. \begin_inset Formula $(X_{1},X_{2},X_{3})$
  320. \end_inset
  321. that maximizes the posterior probability
  322. \begin_inset Formula $P(X_{1},X_{2},X_{3}|Z_{1}=z_{1},Z_{2}=z_{2},Z_{3}=z_{3})$
  323. \end_inset
  324. .
  325. Since the measurements
  326. \begin_inset Formula $Z_{1}$
  327. \end_inset
  328. ,
  329. \begin_inset Formula $Z_{2}$
  330. \end_inset
  331. , and
  332. \begin_inset Formula $Z_{3}$
  333. \end_inset
  334. are
  335. \emph on
  336. known
  337. \emph default
  338. , the posterior is proportional to the product of six
  339. \series bold
  340. factors
  341. \series default
  342. , three of which derive from the the Markov chain, and three likelihood
  343. factors defined as
  344. \begin_inset Formula $L(X_{t};z)\propto P(Z_{t}=z|X_{t})$
  345. \end_inset
  346. :
  347. \begin_inset Formula
  348. \[
  349. P(X_{1},X_{2},X_{3}|Z_{1},Z_{2},Z_{3})\propto P(X_{1})P(X_{2}|X_{1})P(X_{3}|X_{2})L(X_{1};z_{1})L(X_{2};z_{2})L(X_{3};z_{3})
  350. \]
  351. \end_inset
  352. \end_layout
  353. \begin_layout Standard
  354. \begin_inset ERT
  355. status open
  356. \begin_layout Plain Layout
  357. \backslash
  358. vspace{-3mm}
  359. \end_layout
  360. \end_inset
  361. \begin_inset Float figure
  362. placement H
  363. wide false
  364. sideways false
  365. status open
  366. \begin_layout Plain Layout
  367. \align center
  368. \begin_inset Graphics
  369. filename images/hmm-FG.pdf
  370. scale 60
  371. BoundingBox 30bp 40bp 340bp 130bp
  372. clip
  373. \end_inset
  374. \begin_inset Caption Standard
  375. \begin_layout Plain Layout
  376. \begin_inset CommandInset label
  377. LatexCommand label
  378. name "fig:HMM-FG"
  379. \end_inset
  380. An HMM with observed measurements, unrolled over time, represented as a
  381. factor graph.
  382. \end_layout
  383. \end_inset
  384. \end_layout
  385. \end_inset
  386. \end_layout
  387. \begin_layout Standard
  388. \begin_inset ERT
  389. status open
  390. \begin_layout Plain Layout
  391. \backslash
  392. noindent
  393. \end_layout
  394. \end_inset
  395. This motivates a different graphical model, a
  396. \series bold
  397. factor graph
  398. \series default
  399. , in which we only represent the unknown variables
  400. \begin_inset Formula $X_{1}$
  401. \end_inset
  402. ,
  403. \begin_inset Formula $X_{2}$
  404. \end_inset
  405. , and
  406. \begin_inset Formula $X_{3}$
  407. \end_inset
  408. , connected to factors that encode probabilistic information on them, as
  409. in Figure
  410. \begin_inset CommandInset ref
  411. LatexCommand ref
  412. reference "fig:HMM-FG"
  413. \end_inset
  414. .
  415. To do maximum a-posteriori (MAP) inference, we then maximize the product
  416. \begin_inset Formula
  417. \[
  418. f(X_{1},X_{2},X_{3})=\prod f_{i}(\mathcal{X}_{i})
  419. \]
  420. \end_inset
  421. i.e., the value of the factor graph.
  422. It should be clear from the figure that the connectivity of a factor graph
  423. encodes, for each factor
  424. \begin_inset Formula $f_{i}$
  425. \end_inset
  426. , which subset of variables
  427. \begin_inset Formula $\mathcal{X}_{i}$
  428. \end_inset
  429. it depends on.
  430. In the examples below, we use factor graphs to model more complex MAP inference
  431. problems in robotics.
  432. \end_layout
  433. \begin_layout Section
  434. \begin_inset CommandInset label
  435. LatexCommand label
  436. name "sec:Robot-Localization"
  437. \end_inset
  438. Modeling Robot Motion
  439. \end_layout
  440. \begin_layout Subsection
  441. Modeling with Factor Graphs
  442. \end_layout
  443. \begin_layout Standard
  444. Before diving into a SLAM example, let us consider the simpler problem of
  445. modeling robot motion.
  446. This can be done with a
  447. \emph on
  448. continuous
  449. \emph default
  450. Markov chain, and provides a gentle introduction to GTSAM.
  451. \end_layout
  452. \begin_layout Standard
  453. \begin_inset Float figure
  454. placement h
  455. wide false
  456. sideways false
  457. status open
  458. \begin_layout Plain Layout
  459. \align center
  460. \begin_inset Graphics
  461. filename images/FactorGraph.pdf
  462. scale 80
  463. BoundingBox 40bp 585bp 300bp 625bp
  464. clip
  465. \end_inset
  466. \begin_inset Caption Standard
  467. \begin_layout Plain Layout
  468. \begin_inset CommandInset label
  469. LatexCommand label
  470. name "fig:OdometryFG"
  471. \end_inset
  472. Factor graph for robot localization.
  473. \end_layout
  474. \end_inset
  475. \end_layout
  476. \end_inset
  477. \end_layout
  478. \begin_layout Standard
  479. \begin_inset ERT
  480. status open
  481. \begin_layout Plain Layout
  482. \backslash
  483. noindent
  484. \end_layout
  485. \end_inset
  486. The factor graph for a simple example is shown in Figure
  487. \begin_inset CommandInset ref
  488. LatexCommand ref
  489. reference "fig:OdometryFG"
  490. \end_inset
  491. .
  492. There are three variables
  493. \begin_inset Formula $x_{1}$
  494. \end_inset
  495. ,
  496. \begin_inset Formula $x_{2}$
  497. \end_inset
  498. , and
  499. \begin_inset Formula $x_{3}$
  500. \end_inset
  501. which represent the poses of the robot over time, rendered in the figure
  502. by the open-circle variable nodes.
  503. In this example, we have one
  504. \series bold
  505. unary factor
  506. \series default
  507. \begin_inset Formula $f_{0}(x_{1})$
  508. \end_inset
  509. on the first pose
  510. \begin_inset Formula $x_{1}$
  511. \end_inset
  512. that encodes our prior knowledge about
  513. \begin_inset Formula $x_{1}$
  514. \end_inset
  515. , and two
  516. \series bold
  517. binary factors
  518. \series default
  519. that relate successive poses, respectively
  520. \begin_inset Formula $f_{1}(x_{1},x_{2};o_{1})$
  521. \end_inset
  522. and
  523. \begin_inset Formula $f_{2}(x_{2},x_{3};o_{2})$
  524. \end_inset
  525. , where
  526. \begin_inset Formula $o_{1}$
  527. \end_inset
  528. and
  529. \begin_inset Formula $o_{2}$
  530. \end_inset
  531. represent odometry measurements.
  532. \end_layout
  533. \begin_layout Subsection
  534. Creating a Factor Graph
  535. \end_layout
  536. \begin_layout Standard
  537. The following C++ code, included in GTSAM as an example, creates the factor
  538. graph in Figure
  539. \begin_inset CommandInset ref
  540. LatexCommand ref
  541. reference "fig:OdometryFG"
  542. \end_inset
  543. :
  544. \end_layout
  545. \begin_layout Standard
  546. \begin_inset CommandInset include
  547. LatexCommand lstinputlisting
  548. filename "Code/OdometryExample.cpp"
  549. lstparams "aboveskip=10pt,basicstyle={\\ttfamily\\small},caption={Excerpt from examples/OdometryExample.cpp},captionpos=b,frame=single,identifierstyle={\\bfseries},label={listing:OdometryExample},language={C++},numbers=left"
  550. \end_inset
  551. \end_layout
  552. \begin_layout Standard
  553. \begin_inset ERT
  554. status open
  555. \begin_layout Plain Layout
  556. \backslash
  557. noindent
  558. \end_layout
  559. \end_inset
  560. Above, line 2 creates an empty factor graph.
  561. We then add the factor
  562. \begin_inset Formula $f_{0}(x_{1})$
  563. \end_inset
  564. on lines 5-8 as an instance of
  565. \series bold
  566. \emph on
  567. PriorFactor<T>
  568. \series default
  569. \emph default
  570. , a templated class provided in the slam subfolder, with
  571. \series bold
  572. \emph on
  573. T=Pose2
  574. \series default
  575. \emph default
  576. .
  577. Its constructor takes a variable
  578. \series bold
  579. \emph on
  580. Key
  581. \series default
  582. \emph default
  583. (in this case 1), a mean of type
  584. \series bold
  585. \emph on
  586. Pose2,
  587. \series default
  588. \emph default
  589. created on Line 5, and a noise model for the prior density.
  590. We provide a diagonal Gaussian of type
  591. \series bold
  592. \emph on
  593. noiseModel::Diagonal
  594. \series default
  595. \emph default
  596. by specifying three standard deviations in line 7, respectively 30 cm.
  597. \begin_inset space ~
  598. \end_inset
  599. on the robot's position, and 0.1 radians on the robot's orientation.
  600. Note that the
  601. \series bold
  602. \emph on
  603. Sigmas
  604. \series default
  605. \emph default
  606. constructor returns a shared pointer, anticipating that typically the same
  607. noise models are used for many different factors.
  608. \end_layout
  609. \begin_layout Standard
  610. Similarly, odometry measurements are specified as
  611. \series bold
  612. \emph on
  613. Pose2
  614. \series default
  615. \emph default
  616. on line 11, with a slightly different noise model defined on line 12-13.
  617. We then add the two factors
  618. \begin_inset Formula $f_{1}(x_{1},x_{2};o_{1})$
  619. \end_inset
  620. and
  621. \begin_inset Formula $f_{2}(x_{2},x_{3};o_{2})$
  622. \end_inset
  623. on lines 14-15, as instances of yet another templated class,
  624. \series bold
  625. \emph on
  626. BetweenFactor<T>
  627. \series default
  628. \emph default
  629. , again with
  630. \series bold
  631. \emph on
  632. T=Pose2
  633. \series default
  634. \emph default
  635. .
  636. \end_layout
  637. \begin_layout Standard
  638. When running the example (
  639. \emph on
  640. make OdometryExample.run
  641. \emph default
  642. on the command prompt), it will print out the factor graph as follows:
  643. \family typewriter
  644. \size small
  645. \begin_inset CommandInset include
  646. LatexCommand verbatiminput
  647. filename "Code/OdometryOutput1.txt"
  648. \end_inset
  649. \end_layout
  650. \begin_layout Subsection
  651. Factor Graphs versus Values
  652. \end_layout
  653. \begin_layout Standard
  654. At this point it is instructive to emphasize two important design ideas
  655. underlying GTSAM:
  656. \end_layout
  657. \begin_layout Enumerate
  658. The factor graph and its embodiment in code specify the joint probability
  659. distribution
  660. \begin_inset Formula $P(X|Z)$
  661. \end_inset
  662. over the
  663. \emph on
  664. entire
  665. \emph default
  666. trajectory
  667. \begin_inset Formula $X\define\{x_{1},x_{2},x_{3}\}$
  668. \end_inset
  669. of the robot, rather than just the last pose.
  670. This
  671. \emph on
  672. smoothing
  673. \emph default
  674. view of the world gives GTSAM its name:
  675. \begin_inset Quotes eld
  676. \end_inset
  677. smoothing and mapping
  678. \begin_inset Quotes erd
  679. \end_inset
  680. .
  681. Later in this document we will talk about how we can also use GTSAM to
  682. do filtering (which you often do
  683. \emph on
  684. not
  685. \emph default
  686. want to do) or incremental inference (which we do all the time).
  687. \end_layout
  688. \begin_layout Enumerate
  689. A factor graph in GTSAM is just the specification of the probability density
  690. \begin_inset Formula $P(X|Z)$
  691. \end_inset
  692. , and the corresponding
  693. \series bold
  694. \emph on
  695. FactorGraph
  696. \series default
  697. \emph default
  698. class and its derived classes do not ever contain a
  699. \begin_inset Quotes eld
  700. \end_inset
  701. solution
  702. \begin_inset Quotes erd
  703. \end_inset
  704. .
  705. Rather, there is a separate type
  706. \series bold
  707. \emph on
  708. Values
  709. \series default
  710. \emph default
  711. that is used to specify specific values for (in this case)
  712. \begin_inset Formula $x_{1}$
  713. \end_inset
  714. ,
  715. \begin_inset Formula $x_{2}$
  716. \end_inset
  717. , and
  718. \begin_inset Formula $x_{3}$
  719. \end_inset
  720. , which can then be used to evaluate the probability (or, more commonly,
  721. the error) associated with particular values.
  722. \end_layout
  723. \begin_layout Standard
  724. The latter point is often a point of confusion with beginning users of GTSAM.
  725. It helps to remember that when designing GTSAM we took a functional approach
  726. of classes corresponding to mathematical objects, which are usually immutable.
  727. You should think of a factor graph as a
  728. \emph on
  729. function
  730. \emph default
  731. to be applied to values -as the notation
  732. \begin_inset Formula $f(X)\propto P(X|Z)$
  733. \end_inset
  734. implies- rather than as an object to be modified.
  735. \end_layout
  736. \begin_layout Subsection
  737. Non-linear Optimization in GTSAM
  738. \end_layout
  739. \begin_layout Standard
  740. The listing below creates a
  741. \series bold
  742. \emph on
  743. Values
  744. \series default
  745. \emph default
  746. instance, and uses it as the initial estimate to find the maximum a-posteriori
  747. (MAP) assignment for the trajectory
  748. \begin_inset Formula $X$
  749. \end_inset
  750. :
  751. \end_layout
  752. \begin_layout Standard
  753. \begin_inset CommandInset include
  754. LatexCommand lstinputlisting
  755. filename "Code/OdometryOptimize.cpp"
  756. lstparams "aboveskip=10pt,basicstyle={\\ttfamily\\small},caption={Excerpt from examples/OdometryExample.cpp},captionpos=b,frame=single,identifierstyle={\\bfseries},label={listing:OdometryOptimize},language={C++},numbers=left"
  757. \end_inset
  758. \end_layout
  759. \begin_layout Standard
  760. \begin_inset ERT
  761. status open
  762. \begin_layout Plain Layout
  763. \backslash
  764. noindent
  765. \end_layout
  766. \end_inset
  767. Lines 2-5 in Listing
  768. \begin_inset CommandInset ref
  769. LatexCommand ref
  770. reference "listing:OdometryOptimize"
  771. \end_inset
  772. create the initial estimate, and on line 8 we create a non-linear Levenberg-Mar
  773. quardt style optimizer, and call
  774. \series bold
  775. \emph on
  776. optimize
  777. \series default
  778. \emph default
  779. using default parameter settings.
  780. The reason why GTSAM needs to perform non-linear optimization is because
  781. the odometry factors
  782. \begin_inset Formula $f_{1}(x_{1},x_{2};o_{1})$
  783. \end_inset
  784. and
  785. \begin_inset Formula $f_{2}(x_{2},x_{3};o_{2})$
  786. \end_inset
  787. are non-linear, as they involve the orientation of the robot.
  788. This also explains why the factor graph we created in Listing
  789. \begin_inset CommandInset ref
  790. LatexCommand ref
  791. reference "listing:OdometryExample"
  792. \end_inset
  793. is of type
  794. \series bold
  795. \emph on
  796. NonlinearFactorGraph
  797. \series default
  798. \emph default
  799. .
  800. The optimization class linearizes this graph, possibly multiple times,
  801. to minimize the non-linear squared error specified by the factors.
  802. \end_layout
  803. \begin_layout Standard
  804. The relevant output from running the example is as follows:
  805. \family typewriter
  806. \size small
  807. \begin_inset CommandInset include
  808. LatexCommand verbatiminput
  809. filename "Code/OdometryOutput2.txt"
  810. \end_inset
  811. \end_layout
  812. \begin_layout Standard
  813. \begin_inset ERT
  814. status open
  815. \begin_layout Plain Layout
  816. \backslash
  817. noindent
  818. \end_layout
  819. \end_inset
  820. It can be seen that, subject to very small tolerance, the ground truth
  821. solution
  822. \begin_inset Formula $x_{1}=(0,0,0)$
  823. \end_inset
  824. ,
  825. \begin_inset Formula $x_{2}=(2,0,0)$
  826. \end_inset
  827. , and
  828. \begin_inset Formula $x_{3}=(4,0,0)$
  829. \end_inset
  830. is recovered.
  831. \end_layout
  832. \begin_layout Subsection
  833. \begin_inset CommandInset label
  834. LatexCommand label
  835. name "subsec:Full-Posterior-Inference"
  836. \end_inset
  837. Full Posterior Inference
  838. \end_layout
  839. \begin_layout Standard
  840. GTSAM can also be used to calculate the covariance matrix for each pose
  841. after incorporating the information from all measurements
  842. \begin_inset Formula $Z$
  843. \end_inset
  844. .
  845. Recognizing that the factor graph encodes the
  846. \series bold
  847. posterior density
  848. \series default
  849. \begin_inset Formula $P(X|Z)$
  850. \end_inset
  851. , the mean
  852. \begin_inset Formula $\mu$
  853. \end_inset
  854. together with the covariance
  855. \begin_inset Formula $\Sigma$
  856. \end_inset
  857. for each pose
  858. \begin_inset Formula $x$
  859. \end_inset
  860. approximate the
  861. \series bold
  862. marginal posterior density
  863. \series default
  864. \begin_inset Formula $P(x|Z)$
  865. \end_inset
  866. .
  867. Note that this is just an approximation, as even in this simple case the
  868. odometry factors are actually non-linear in their arguments, and GTSAM
  869. only computes a Gaussian approximation to the true underlying posterior.
  870. \end_layout
  871. \begin_layout Standard
  872. The following C++ code will recover the posterior marginals:
  873. \end_layout
  874. \begin_layout Standard
  875. \begin_inset CommandInset include
  876. LatexCommand lstinputlisting
  877. filename "Code/OdometryMarginals.cpp"
  878. lstparams "aboveskip=10pt,basicstyle={\\ttfamily\\small},captionpos=b,frame=single,identifierstyle={\\bfseries},language={C++},numbers=left,caption={Excerpt from examples/OdometryExample.cpp},label={listing:OdometryMarginals}"
  879. \end_inset
  880. The relevant output from running the example is as follows:
  881. \size footnotesize
  882. \begin_inset CommandInset include
  883. LatexCommand verbatiminput
  884. filename "Code/OdometryOutput3.txt"
  885. \end_inset
  886. \end_layout
  887. \begin_layout Standard
  888. \begin_inset ERT
  889. status open
  890. \begin_layout Plain Layout
  891. \backslash
  892. noindent
  893. \end_layout
  894. \end_inset
  895. What we see is that the marginal covariance
  896. \begin_inset Formula $P(x_{1}|Z)$
  897. \end_inset
  898. on
  899. \begin_inset Formula $x_{1}$
  900. \end_inset
  901. is simply the prior knowledge on
  902. \begin_inset Formula $x_{1}$
  903. \end_inset
  904. , but as the robot moves the uncertainty in all dimensions grows without
  905. bound, and the
  906. \begin_inset Formula $y$
  907. \end_inset
  908. and
  909. \begin_inset Formula $\theta$
  910. \end_inset
  911. components of the pose become (positively) correlated.
  912. \end_layout
  913. \begin_layout Standard
  914. An important fact to note when interpreting these numbers is that covariance
  915. matrices are given in
  916. \emph on
  917. relative
  918. \emph default
  919. coordinates, not absolute coordinates.
  920. This is because internally GTSAM optimizes for a change with respect to
  921. a linearization point, as do all nonlinear optimization libraries.
  922. \end_layout
  923. \begin_layout Section
  924. Robot Localization
  925. \end_layout
  926. \begin_layout Subsection
  927. Unary Measurement Factors
  928. \end_layout
  929. \begin_layout Standard
  930. In this section we add measurements to the factor graph that will help us
  931. actually
  932. \emph on
  933. localize
  934. \emph default
  935. the robot over time.
  936. The example also serves as a tutorial on creating new factor types.
  937. \end_layout
  938. \begin_layout Standard
  939. \begin_inset Float figure
  940. placement h
  941. wide false
  942. sideways false
  943. status open
  944. \begin_layout Plain Layout
  945. \align center
  946. \begin_inset Graphics
  947. filename images/FactorGraph2.pdf
  948. scale 80
  949. BoundingBox 70bp 550bp 300bp 630bp
  950. clip
  951. \end_inset
  952. \begin_inset Caption Standard
  953. \begin_layout Plain Layout
  954. \begin_inset CommandInset label
  955. LatexCommand label
  956. name "fig:LocalizationFG"
  957. \end_inset
  958. Robot localization factor graph with unary measurement factors at each time
  959. step.
  960. \end_layout
  961. \end_inset
  962. \end_layout
  963. \end_inset
  964. \end_layout
  965. \begin_layout Standard
  966. \begin_inset ERT
  967. status open
  968. \begin_layout Plain Layout
  969. \backslash
  970. noindent
  971. \end_layout
  972. \end_inset
  973. In particular, we use
  974. \series bold
  975. unary measurement factors
  976. \series default
  977. to handle external measurements.
  978. The example from Section
  979. \begin_inset CommandInset ref
  980. LatexCommand ref
  981. reference "sec:Robot-Localization"
  982. \end_inset
  983. is not very useful on a real robot, because it only contains factors correspond
  984. ing to odometry measurements.
  985. These are imperfect and will lead to quickly accumulating uncertainty on
  986. the last robot pose, at least in the absence of any external measurements
  987. (see Section
  988. \begin_inset CommandInset ref
  989. LatexCommand ref
  990. reference "subsec:Full-Posterior-Inference"
  991. \end_inset
  992. ).
  993. Figure
  994. \begin_inset CommandInset ref
  995. LatexCommand ref
  996. reference "fig:LocalizationFG"
  997. \end_inset
  998. shows a new factor graph where the prior
  999. \begin_inset Formula $f_{0}(x_{1})$
  1000. \end_inset
  1001. is omitted and instead we added three unary factors
  1002. \begin_inset Formula $f_{1}(x_{1};z_{1})$
  1003. \end_inset
  1004. ,
  1005. \begin_inset Formula $f_{2}(x_{2};z_{2})$
  1006. \end_inset
  1007. , and
  1008. \begin_inset Formula $f_{3}(x_{3};z_{3})$
  1009. \end_inset
  1010. , one for each localization measurement
  1011. \begin_inset Formula $z_{t}$
  1012. \end_inset
  1013. , respectively.
  1014. Such unary factors are applicable for measurements
  1015. \begin_inset Formula $z_{t}$
  1016. \end_inset
  1017. that depend
  1018. \emph on
  1019. only
  1020. \emph default
  1021. on the current robot pose, e.g., GPS readings, correlation of a laser range-finde
  1022. r in a pre-existing map, or indeed the presence of absence of ceiling lights
  1023. (see
  1024. \begin_inset CommandInset citation
  1025. LatexCommand citet
  1026. key "Dellaert99b"
  1027. \end_inset
  1028. for that amusing example).
  1029. \end_layout
  1030. \begin_layout Subsection
  1031. Defining Custom Factors
  1032. \end_layout
  1033. \begin_layout Standard
  1034. In GTSAM, you can create custom unary factors by deriving a new class from
  1035. the built-in class
  1036. \series bold
  1037. \emph on
  1038. NoiseModelFactor1<T>
  1039. \series default
  1040. \emph default
  1041. , which implements a unary factor corresponding to a measurement likelihood
  1042. with a Gaussian noise model,
  1043. \begin_inset Formula
  1044. \[
  1045. L(q;m)=exp\left\{ -\frac{1}{2}\SqrMah{h(q)}{m}{\Sigma}\right\} \define f(q)
  1046. \]
  1047. \end_inset
  1048. where
  1049. \begin_inset Formula $m$
  1050. \end_inset
  1051. is the measurement,
  1052. \begin_inset Formula $q$
  1053. \end_inset
  1054. is the unknown variable,
  1055. \begin_inset Formula $h(q)$
  1056. \end_inset
  1057. is a (possibly nonlinear) measurement function, and
  1058. \begin_inset Formula $\Sigma$
  1059. \end_inset
  1060. is the noise covariance.
  1061. Note that
  1062. \begin_inset Formula $m$
  1063. \end_inset
  1064. is considered
  1065. \emph on
  1066. known
  1067. \emph default
  1068. above, and the likelihood
  1069. \begin_inset Formula $L(q;m)$
  1070. \end_inset
  1071. \begin_inset Note Note
  1072. status open
  1073. \begin_layout Plain Layout
  1074. of
  1075. \begin_inset Formula $q$
  1076. \end_inset
  1077. given
  1078. \begin_inset Formula $m$
  1079. \end_inset
  1080. \end_layout
  1081. \end_inset
  1082. will only ever be evaluated as a function of
  1083. \begin_inset Formula $q$
  1084. \end_inset
  1085. , which explains why it is a unary factor
  1086. \begin_inset Formula $f(q)$
  1087. \end_inset
  1088. .
  1089. It is always the unknown variable
  1090. \begin_inset Formula $q$
  1091. \end_inset
  1092. that is either likely or unlikely, given the measurement.
  1093. \end_layout
  1094. \begin_layout Standard
  1095. \series bold
  1096. Note:
  1097. \series default
  1098. many people get this backwards, often misled by the conditional density
  1099. notation
  1100. \begin_inset Formula $P(m|q)$
  1101. \end_inset
  1102. .
  1103. In fact, the likelihood
  1104. \begin_inset Formula $L(q;m)$
  1105. \end_inset
  1106. is
  1107. \emph on
  1108. defined
  1109. \emph default
  1110. as any function of
  1111. \begin_inset Formula $q$
  1112. \end_inset
  1113. proportional to
  1114. \begin_inset Formula $P(m|q)$
  1115. \end_inset
  1116. .
  1117. \end_layout
  1118. \begin_layout Standard
  1119. Listing
  1120. \begin_inset CommandInset ref
  1121. LatexCommand vref
  1122. reference "listing:LocalizationFactor"
  1123. \end_inset
  1124. shows an example on how to define the custom factor class
  1125. \series bold
  1126. \emph on
  1127. UnaryFactor
  1128. \series default
  1129. \emph default
  1130. which implements a
  1131. \begin_inset Quotes eld
  1132. \end_inset
  1133. GPS-like
  1134. \begin_inset Quotes erd
  1135. \end_inset
  1136. measurement likelihood:
  1137. \end_layout
  1138. \begin_layout Standard
  1139. \begin_inset CommandInset include
  1140. LatexCommand lstinputlisting
  1141. filename "Code/LocalizationFactor.cpp"
  1142. lstparams "aboveskip=10pt,basicstyle={\\ttfamily\\small},captionpos=b,frame=single,identifierstyle={\\bfseries},language={C++},numbers=left,caption={Excerpt from examples/LocalizationExample.cpp},label={listing:LocalizationFactor}"
  1143. \end_inset
  1144. \end_layout
  1145. \begin_layout Standard
  1146. \begin_inset ERT
  1147. status open
  1148. \begin_layout Plain Layout
  1149. \backslash
  1150. noindent
  1151. \end_layout
  1152. \end_inset
  1153. In defining the derived class on line 1, we provide the template argument
  1154. \series bold
  1155. \emph on
  1156. Pose2
  1157. \series default
  1158. \emph default
  1159. to indicate the type of the variable
  1160. \begin_inset Formula $q$
  1161. \end_inset
  1162. , whereas the measurement is stored as the instance variables
  1163. \series bold
  1164. \emph on
  1165. mx_
  1166. \series default
  1167. \emph default
  1168. and
  1169. \series bold
  1170. \emph on
  1171. my_
  1172. \series default
  1173. \emph default
  1174. , defined on line 2.
  1175. The constructor on lines 5-6 simply passes on the variable key
  1176. \begin_inset Formula $j$
  1177. \end_inset
  1178. and the noise model to the superclass, and stores the measurement values
  1179. provided.
  1180. The most important function to has be implemented by every factor class
  1181. is
  1182. \series bold
  1183. \emph on
  1184. evaluateError
  1185. \series default
  1186. \emph default
  1187. , which should return
  1188. \begin_inset Formula
  1189. \[
  1190. E(q)\define h(q)-m
  1191. \]
  1192. \end_inset
  1193. which is done on line 12.
  1194. Importantly, because we want to use this factor for nonlinear optimization
  1195. (see e.g.,
  1196. \begin_inset CommandInset citation
  1197. LatexCommand citealt
  1198. key "Dellaert06ijrr"
  1199. \end_inset
  1200. for details), whenever the optional argument
  1201. \begin_inset Formula $H$
  1202. \end_inset
  1203. is provided, a
  1204. \series bold
  1205. \emph on
  1206. Matrix
  1207. \series default
  1208. \emph default
  1209. reference, the function should assign the
  1210. \series bold
  1211. Jacobian
  1212. \series default
  1213. of
  1214. \begin_inset Formula $h(q)$
  1215. \end_inset
  1216. to it, evaluated at the provided value for
  1217. \begin_inset Formula $q$
  1218. \end_inset
  1219. .
  1220. This is done for this example on line 11.
  1221. In this case, the Jacobian of the 2-dimensional function
  1222. \begin_inset Formula $h$
  1223. \end_inset
  1224. , which just returns the position of the robot,
  1225. \begin_inset Formula
  1226. \[
  1227. h(q)=\left[\begin{array}{c}
  1228. q_{x}\\
  1229. q_{y}
  1230. \end{array}\right]
  1231. \]
  1232. \end_inset
  1233. with respect the 3-dimensional pose
  1234. \begin_inset Formula $q=\left(q_{x},q_{y},q_{\theta}\right)$
  1235. \end_inset
  1236. , yields the following simple
  1237. \begin_inset Formula $2\times3$
  1238. \end_inset
  1239. matrix in tangent space which is the same the as the rotation matrix:
  1240. \end_layout
  1241. \begin_layout Standard
  1242. \begin_inset Formula
  1243. \[
  1244. H=\left[\begin{array}{ccc}
  1245. \cos(q_{\theta}) & -\sin(q_{\theta}) & 0\\
  1246. \sin(q_{\theta}) & \cos(q_{\theta}) & 0
  1247. \end{array}\right]
  1248. \]
  1249. \end_inset
  1250. \end_layout
  1251. \begin_layout Subsection
  1252. Using Custom Factors
  1253. \end_layout
  1254. \begin_layout Standard
  1255. The following C++ code fragment illustrates how to create and add custom
  1256. factors to a factor graph:
  1257. \end_layout
  1258. \begin_layout Standard
  1259. \begin_inset CommandInset include
  1260. LatexCommand lstinputlisting
  1261. filename "Code/LocalizationExample2.cpp"
  1262. lstparams "aboveskip=10pt,basicstyle={\\ttfamily\\small},caption={Excerpt from examples/LocalizationExample.cpp},captionpos=b,frame=single,identifierstyle={\\bfseries},label={listing:LocalizationExample2},language={C++},numbers=left"
  1263. \end_inset
  1264. \begin_inset ERT
  1265. status open
  1266. \begin_layout Plain Layout
  1267. \backslash
  1268. noindent
  1269. \end_layout
  1270. \end_inset
  1271. In Listing
  1272. \begin_inset CommandInset ref
  1273. LatexCommand vref
  1274. reference "listing:LocalizationExample2"
  1275. \end_inset
  1276. , we create the noise model on line 2-3, which now specifies two standard
  1277. deviations on the measurements
  1278. \begin_inset Formula $m_{x}$
  1279. \end_inset
  1280. and
  1281. \begin_inset Formula $m_{y}$
  1282. \end_inset
  1283. .
  1284. On lines 4-6 we create
  1285. \series bold
  1286. \emph on
  1287. shared_ptr
  1288. \series default
  1289. \emph default
  1290. versions of three newly created
  1291. \series bold
  1292. \emph on
  1293. UnaryFactor
  1294. \series default
  1295. \emph default
  1296. instances, and add them to graph.
  1297. GTSAM uses shared pointers to refer to factors in factor graphs, and
  1298. \series bold
  1299. \emph on
  1300. boost::make_shared
  1301. \series default
  1302. \emph default
  1303. is a convenience function to simultaneously construct a class and create
  1304. a
  1305. \series bold
  1306. \emph on
  1307. shared_ptr
  1308. \series default
  1309. \emph default
  1310. to it.
  1311. \begin_inset Note Note
  1312. status collapsed
  1313. \begin_layout Plain Layout
  1314. and on lines 4-6 we add three newly created
  1315. \series bold
  1316. \emph on
  1317. UnaryFactor
  1318. \series default
  1319. \emph default
  1320. instances to the graph.
  1321. \end_layout
  1322. \end_inset
  1323. We obtain the factor graph from Figure
  1324. \begin_inset CommandInset ref
  1325. LatexCommand vref
  1326. reference "fig:LocalizationFG"
  1327. \end_inset
  1328. .
  1329. \family typewriter
  1330. \size small
  1331. \begin_inset Note Note
  1332. status collapsed
  1333. \begin_layout Plain Layout
  1334. The relevant output from running the example is as follows:
  1335. \family typewriter
  1336. \size small
  1337. \begin_inset CommandInset include
  1338. LatexCommand verbatiminput
  1339. filename "Code/LocalizationOutput4.txt"
  1340. \end_inset
  1341. \end_layout
  1342. \end_inset
  1343. \end_layout
  1344. \begin_layout Subsection
  1345. Full Posterior Inference
  1346. \end_layout
  1347. \begin_layout Standard
  1348. The three GPS factors are enough to fully constrain all unknown poses and
  1349. tie them to a
  1350. \begin_inset Quotes eld
  1351. \end_inset
  1352. global
  1353. \begin_inset Quotes erd
  1354. \end_inset
  1355. reference frame, including the three unknown orientations.
  1356. If not, GTSAM would have exited with a singular matrix exception.
  1357. The marginals can be recovered exactly as in Section
  1358. \begin_inset CommandInset ref
  1359. LatexCommand ref
  1360. reference "subsec:Full-Posterior-Inference"
  1361. \end_inset
  1362. , and the solution and marginal covariances are now given by the following:
  1363. \size footnotesize
  1364. \begin_inset CommandInset include
  1365. LatexCommand verbatiminput
  1366. filename "Code/LocalizationOutput5.txt"
  1367. \end_inset
  1368. \end_layout
  1369. \begin_layout Standard
  1370. Comparing this with the covariance matrices in Section
  1371. \begin_inset CommandInset ref
  1372. LatexCommand ref
  1373. reference "subsec:Full-Posterior-Inference"
  1374. \end_inset
  1375. , we can see that the uncertainty no longer grows without bounds as measurement
  1376. uncertainty accumulates.
  1377. Instead, the
  1378. \begin_inset Quotes eld
  1379. \end_inset
  1380. GPS
  1381. \begin_inset Quotes erd
  1382. \end_inset
  1383. measurements more or less constrain the poses evenly, as expected.
  1384. \end_layout
  1385. \begin_layout Standard
  1386. \begin_inset Float figure
  1387. placement h
  1388. wide false
  1389. sideways false
  1390. status open
  1391. \begin_layout Plain Layout
  1392. \align center
  1393. \begin_inset Float figure
  1394. wide false
  1395. sideways false
  1396. status open
  1397. \begin_layout Plain Layout
  1398. \align center
  1399. \begin_inset Graphics
  1400. filename images/Odometry.pdf
  1401. width 80text%
  1402. BoundingBox 70bp 310bp 525bp 500bp
  1403. clip
  1404. \end_inset
  1405. \end_layout
  1406. \begin_layout Plain Layout
  1407. \begin_inset Caption Standard
  1408. \begin_layout Plain Layout
  1409. Odometry marginals
  1410. \end_layout
  1411. \end_inset
  1412. \end_layout
  1413. \end_inset
  1414. \end_layout
  1415. \begin_layout Plain Layout
  1416. \align center
  1417. \begin_inset space ~
  1418. \end_inset
  1419. \end_layout
  1420. \begin_layout Plain Layout
  1421. \align center
  1422. \begin_inset Float figure
  1423. wide false
  1424. sideways false
  1425. status open
  1426. \begin_layout Plain Layout
  1427. \align center
  1428. \begin_inset Graphics
  1429. filename images/Localization.pdf
  1430. width 80text%
  1431. BoundingBox 70bp 310bp 525bp 500bp
  1432. clip
  1433. \end_inset
  1434. \end_layout
  1435. \begin_layout Plain Layout
  1436. \begin_inset Caption Standard
  1437. \begin_layout Plain Layout
  1438. Localization Marginals
  1439. \end_layout
  1440. \end_inset
  1441. \end_layout
  1442. \end_inset
  1443. \begin_inset Caption Standard
  1444. \begin_layout Plain Layout
  1445. \begin_inset CommandInset label
  1446. LatexCommand label
  1447. name "fig:CompareMarginals"
  1448. \end_inset
  1449. Comparing the marginals resulting from the
  1450. \begin_inset Quotes eld
  1451. \end_inset
  1452. odometry
  1453. \begin_inset Quotes erd
  1454. \end_inset
  1455. factor graph in Figure
  1456. \begin_inset CommandInset ref
  1457. LatexCommand ref
  1458. reference "fig:OdometryFG"
  1459. \end_inset
  1460. and the
  1461. \begin_inset Quotes eld
  1462. \end_inset
  1463. localization
  1464. \begin_inset Quotes erd
  1465. \end_inset
  1466. factor graph in Figure
  1467. \begin_inset CommandInset ref
  1468. LatexCommand ref
  1469. reference "fig:LocalizationFG"
  1470. \end_inset
  1471. .
  1472. \end_layout
  1473. \end_inset
  1474. \end_layout
  1475. \end_inset
  1476. \end_layout
  1477. \begin_layout Standard
  1478. It helps a lot when we view this graphically, as in Figure
  1479. \begin_inset CommandInset ref
  1480. LatexCommand ref
  1481. reference "fig:CompareMarginals"
  1482. \end_inset
  1483. , where I show the marginals on position as covariance ellipses that contain
  1484. 68.26% of all probability mass.
  1485. For the odometry marginals, it is immediately apparent from the figure
  1486. that (1) the uncertainty on pose keeps growing, and (2) the uncertainty
  1487. on angular odometry translates into increasing uncertainty on y.
  1488. The localization marginals, in contrast, are constrained by the unary factors
  1489. and are all much smaller.
  1490. In addition, while less apparent, the uncertainty on the middle pose is
  1491. actually smaller as it is constrained by odometry from two sides.
  1492. \end_layout
  1493. \begin_layout Standard
  1494. You might now be wondering how we produced these figures.
  1495. The answer is via the MATLAB interface of GTSAM, which we will demonstrate
  1496. in the next section.
  1497. \end_layout
  1498. \begin_layout Standard
  1499. \begin_inset Newpage pagebreak
  1500. \end_inset
  1501. \end_layout
  1502. \begin_layout Section
  1503. \begin_inset CommandInset label
  1504. LatexCommand label
  1505. name "sec:Pose2SLAM"
  1506. \end_inset
  1507. \begin_inset CommandInset label
  1508. LatexCommand label
  1509. name "sec:WithMarginals"
  1510. \end_inset
  1511. PoseSLAM
  1512. \end_layout
  1513. \begin_layout Subsection
  1514. Loop Closure Constraints
  1515. \end_layout
  1516. \begin_layout Standard
  1517. The simplest instantiation of a SLAM problem is
  1518. \series bold
  1519. PoseSLAM
  1520. \series default
  1521. , which avoids building an explicit map of the environment.
  1522. The goal of SLAM is to simultaneously localize a robot and map the environment
  1523. given incoming sensor measurements
  1524. \begin_inset CommandInset citation
  1525. LatexCommand citep
  1526. key "DurrantWhyte06ram"
  1527. \end_inset
  1528. .
  1529. Besides wheel odometry, one of the most popular sensors for robots moving
  1530. on a plane is a 2D laser-range finder, which provides both odometry constraints
  1531. between successive poses, and loop-closure constraints when the robot re-visits
  1532. a previously explored part of the environment.
  1533. \end_layout
  1534. \begin_layout Standard
  1535. \begin_inset Float figure
  1536. placement h
  1537. wide false
  1538. sideways false
  1539. status open
  1540. \begin_layout Plain Layout
  1541. \align center
  1542. \begin_inset Graphics
  1543. filename images/FactorGraph3.pdf
  1544. scale 80
  1545. BoundingBox 40bp 585bp 330bp 710bp
  1546. clip
  1547. \end_inset
  1548. \begin_inset Caption Standard
  1549. \begin_layout Plain Layout
  1550. \begin_inset CommandInset label
  1551. LatexCommand label
  1552. name "fig:Pose2SLAM"
  1553. \end_inset
  1554. Factor graph for PoseSLAM.
  1555. \end_layout
  1556. \end_inset
  1557. \end_layout
  1558. \end_inset
  1559. \end_layout
  1560. \begin_layout Standard
  1561. \begin_inset ERT
  1562. status open
  1563. \begin_layout Plain Layout
  1564. \backslash
  1565. noindent
  1566. \end_layout
  1567. \end_inset
  1568. A factor graph example for PoseSLAM is shown in Figure
  1569. \begin_inset CommandInset ref
  1570. LatexCommand ref
  1571. reference "fig:Pose2SLAM"
  1572. \end_inset
  1573. .
  1574. The following C++ code, included in GTSAM as an example, creates this factor
  1575. graph in code:
  1576. \end_layout
  1577. \begin_layout Standard
  1578. \begin_inset CommandInset include
  1579. LatexCommand lstinputlisting
  1580. filename "Code/Pose2SLAMExample.cpp"
  1581. lstparams "aboveskip=10pt,basicstyle={\\ttfamily\\small},caption={Excerpt from examples/Pose2SLAMExample.cpp},captionpos=b,frame=single,identifierstyle={\\bfseries},label={listing:Pose2SLAMExample},language={C++},numbers=left"
  1582. \end_inset
  1583. \end_layout
  1584. \begin_layout Standard
  1585. \begin_inset ERT
  1586. status open
  1587. \begin_layout Plain Layout
  1588. \backslash
  1589. noindent
  1590. \end_layout
  1591. \end_inset
  1592. As before, lines 1-4 create a nonlinear factor graph and add the unary
  1593. factor
  1594. \begin_inset Formula $f_{0}(x_{1})$
  1595. \end_inset
  1596. .
  1597. As the robot travels through the world, it creates binary factors
  1598. \begin_inset Formula $f_{t}(x_{t},x_{t+1})$
  1599. \end_inset
  1600. corresponding to odometry, added to the graph in lines 6-12 (Note that
  1601. M_PI_2 refers to pi/2).
  1602. But line 15 models a different event: a
  1603. \series bold
  1604. loop closure
  1605. \series default
  1606. .
  1607. For example, the robot might recognize the same location using vision or
  1608. a laser range finder, and calculate the geometric pose constraint to when
  1609. it first visited this location.
  1610. This is illustrated for poses
  1611. \begin_inset Formula $x_{5}$
  1612. \end_inset
  1613. and
  1614. \begin_inset Formula $x_{2}$
  1615. \end_inset
  1616. , and generates the (red) loop closing factor
  1617. \begin_inset Formula $f_{5}(x_{5},x_{2})$
  1618. \end_inset
  1619. .
  1620. \end_layout
  1621. \begin_layout Standard
  1622. \begin_inset Float figure
  1623. placement h
  1624. wide false
  1625. sideways false
  1626. status open
  1627. \begin_layout Plain Layout
  1628. \align center
  1629. \begin_inset Graphics
  1630. filename images/example1.pdf
  1631. width 80text%
  1632. BoundingBox 30bp 170bp 610bp 630bp
  1633. clip
  1634. \end_inset
  1635. \begin_inset Caption Standard
  1636. \begin_layout Plain Layout
  1637. \begin_inset CommandInset label
  1638. LatexCommand label
  1639. name "fig:example"
  1640. \end_inset
  1641. The result of running optimize on the factor graph in Figure
  1642. \begin_inset CommandInset ref
  1643. LatexCommand ref
  1644. reference "fig:Pose2SLAM"
  1645. \end_inset
  1646. .
  1647. \end_layout
  1648. \end_inset
  1649. \end_layout
  1650. \end_inset
  1651. \end_layout
  1652. \begin_layout Standard
  1653. We can optimize this factor graph as before, by creating an initial estimate
  1654. of type
  1655. \series bold
  1656. \emph on
  1657. Values
  1658. \series default
  1659. \emph default
  1660. , and creating and running an optimizer.
  1661. The result is shown graphically in Figure
  1662. \begin_inset CommandInset ref
  1663. LatexCommand ref
  1664. reference "fig:example"
  1665. \end_inset
  1666. , along with covariance ellipses shown in green.
  1667. These covariance ellipses in 2D indicate the marginal over position, over
  1668. all possible orientations, and show the area which contain 68.26% of the
  1669. probability mass (in 1D this would correspond to one standard deviation).
  1670. The graph shows in a clear manner that the uncertainty on pose
  1671. \begin_inset Formula $x_{5}$
  1672. \end_inset
  1673. is now much less than if there would be only odometry measurements.
  1674. The pose with the highest uncertainty,
  1675. \begin_inset Formula $x_{4}$
  1676. \end_inset
  1677. , is the one furthest away from the unary constraint
  1678. \begin_inset Formula $f_{0}(x_{1})$
  1679. \end_inset
  1680. , which is the only factor tying the graph to a global coordinate frame.
  1681. \end_layout
  1682. \begin_layout Standard
  1683. The figure above was created using an interface that allows you to use GTSAM
  1684. from within MATLAB, which provides for visualization and rapid development.
  1685. We discuss this next.
  1686. \end_layout
  1687. \begin_layout Subsection
  1688. Using the MATLAB Interface
  1689. \end_layout
  1690. \begin_layout Standard
  1691. A large subset of the GTSAM functionality can be accessed through wrapped
  1692. classes from within MATLAB
  1693. \begin_inset Foot
  1694. status open
  1695. \begin_layout Plain Layout
  1696. GTSAM also allows you to wrap your own custom-made classes, although this
  1697. is outside the scope of this manual
  1698. \end_layout
  1699. \end_inset
  1700. .
  1701. The following code excerpt is the MATLAB equivalent of the C++ code in
  1702. Listing
  1703. \begin_inset CommandInset ref
  1704. LatexCommand ref
  1705. reference "listing:Pose2SLAMExample"
  1706. \end_inset
  1707. :
  1708. \begin_inset CommandInset include
  1709. LatexCommand lstinputlisting
  1710. filename "Code/Pose2SLAMExample.m"
  1711. lstparams "aboveskip=10pt,basicstyle={\\ttfamily\\small},captionpos=b,frame=single,identifierstyle={\\bfseries},language={Matlab},numbers=left,caption={Excerpt from matlab/gtsam\\_examples/Pose2SLAMExample.m},label={listing:Pose2SLAMExample-MATLAB}"
  1712. \end_inset
  1713. \end_layout
  1714. \begin_layout Standard
  1715. \begin_inset ERT
  1716. status open
  1717. \begin_layout Plain Layout
  1718. \backslash
  1719. noindent
  1720. \end_layout
  1721. \end_inset
  1722. Note that the code is almost identical, although there are a few syntax
  1723. and naming differences:
  1724. \end_layout
  1725. \begin_layout Itemize
  1726. Objects are created by calling a constructor instead of allocating them
  1727. on the heap.
  1728. \end_layout
  1729. \begin_layout Itemize
  1730. Namespaces are done using dot notation, i.e.,
  1731. \series bold
  1732. \emph on
  1733. noiseModel::Diagonal::SigmasClasses
  1734. \series default
  1735. \emph default
  1736. becomes
  1737. \series bold
  1738. \emph on
  1739. noiseModel.Diagonal.Sigmas
  1740. \series default
  1741. \emph default
  1742. .
  1743. \end_layout
  1744. \begin_layout Itemize
  1745. \series bold
  1746. \emph on
  1747. Vector
  1748. \series default
  1749. \emph default
  1750. and
  1751. \series bold
  1752. \emph on
  1753. Matrix
  1754. \series default
  1755. \emph default
  1756. classes in C++ are just vectors/matrices in MATLAB.
  1757. \end_layout
  1758. \begin_layout Itemize
  1759. As templated classes do not exist in MATLAB, these have been hardcoded in
  1760. the GTSAM interface, e.g.,
  1761. \series bold
  1762. \emph on
  1763. PriorFactorPose2
  1764. \series default
  1765. \emph default
  1766. corresponds to the C++ class
  1767. \series bold
  1768. \emph on
  1769. PriorFactor<Pose2>
  1770. \series default
  1771. \emph default
  1772. , etc.
  1773. \end_layout
  1774. \begin_layout Standard
  1775. After executing the code, you can call
  1776. \emph on
  1777. whos
  1778. \emph default
  1779. on the MATLAB command prompt to see the objects created.
  1780. Note that the indicated
  1781. \emph on
  1782. Class
  1783. \emph default
  1784. corresponds to the wrapped C++ classes:
  1785. \size small
  1786. \begin_inset CommandInset include
  1787. LatexCommand verbatiminput
  1788. filename "Code/whos.txt"
  1789. \end_inset
  1790. \size default
  1791. \begin_inset ERT
  1792. status open
  1793. \begin_layout Plain Layout
  1794. \backslash
  1795. noindent
  1796. \end_layout
  1797. \end_inset
  1798. In addition, any GTSAM object can be examined in detail, yielding identical
  1799. output to C++:
  1800. \size small
  1801. \begin_inset CommandInset include
  1802. LatexCommand verbatiminput
  1803. filename "Code/print.txt"
  1804. \end_inset
  1805. \size default
  1806. \begin_inset ERT
  1807. status open
  1808. \begin_layout Plain Layout
  1809. \backslash
  1810. noindent
  1811. \end_layout
  1812. \end_inset
  1813. And it does not stop there: we can also call some of the functions defined
  1814. for factor graphs.
  1815. E.g.,
  1816. \end_layout
  1817. \begin_layout Standard
  1818. \size small
  1819. \begin_inset CommandInset include
  1820. LatexCommand verbatiminput
  1821. filename "Code/calls.txt"
  1822. \end_inset
  1823. \size default
  1824. \begin_inset ERT
  1825. status open
  1826. \begin_layout Plain Layout
  1827. \backslash
  1828. noindent
  1829. \end_layout
  1830. \end_inset
  1831. computes the sum-squared error
  1832. \begin_inset Formula $\frac{1}{2}\sum_{i}\SqrMah{h_{i}(X_{i})}{z_{i}}{\Sigma}$
  1833. \end_inset
  1834. before and after optimization.
  1835. \end_layout
  1836. \begin_layout Subsection
  1837. Reading and Optimizing Pose Graphs
  1838. \end_layout
  1839. \begin_layout Standard
  1840. \begin_inset Float figure
  1841. placement h
  1842. wide false
  1843. sideways false
  1844. status open
  1845. \begin_layout Plain Layout
  1846. \align center
  1847. \begin_inset Graphics
  1848. filename images/w100-result.pdf
  1849. width 80text%
  1850. BoundingBox 30bp 170bp 610bp 630bp
  1851. clip
  1852. \end_inset
  1853. \begin_inset Caption Standard
  1854. \begin_layout Plain Layout
  1855. \begin_inset CommandInset label
  1856. LatexCommand label
  1857. name "fig:w100"
  1858. \end_inset
  1859. MATLAB plot of small Manhattan world example with 100 poses (due to Ed Olson).
  1860. The initial estimate is shown in green.
  1861. The optimized trajectory, with covariance ellipses, in blue.
  1862. \end_layout
  1863. \end_inset
  1864. \end_layout
  1865. \end_inset
  1866. \end_layout
  1867. \begin_layout Standard
  1868. The ability to work in MATLAB adds a much quicker development cycle, and
  1869. effortless graphical output.
  1870. The optimized trajectory in Figure
  1871. \begin_inset CommandInset ref
  1872. LatexCommand ref
  1873. reference "fig:w100"
  1874. \end_inset
  1875. was produced by the code below, in which
  1876. \emph on
  1877. load2D
  1878. \emph default
  1879. reads TORO files.
  1880. To see how plotting is done, refer to the full source code.
  1881. \begin_inset CommandInset include
  1882. LatexCommand lstinputlisting
  1883. filename "Code/Pose2SLAMExample-graph.m"
  1884. lstparams "aboveskip=10pt,basicstyle={\\ttfamily\\small},captionpos=b,frame=single,identifierstyle={\\bfseries},language={Matlab},numbers=left,caption={Excerpt from matlab/gtsam\\_examples/Pose2SLAMExample\\_graph.m},label={listing:Pose2SLAMExample-graph}"
  1885. \end_inset
  1886. \end_layout
  1887. \begin_layout Subsection
  1888. PoseSLAM in 3D
  1889. \end_layout
  1890. \begin_layout Standard
  1891. PoseSLAM can easily be extended to 3D poses, but some care is needed to
  1892. update 3D rotations.
  1893. GTSAM supports both
  1894. \series bold
  1895. quaternions
  1896. \series default
  1897. and
  1898. \begin_inset Formula $3\times3$
  1899. \end_inset
  1900. \series bold
  1901. rotation matrices
  1902. \series default
  1903. to represent 3D rotations.
  1904. The selection is made via the compile flag GTSAM_USE_QUATERNIONS.
  1905. \end_layout
  1906. \begin_layout Standard
  1907. \begin_inset Float figure
  1908. placement h
  1909. wide false
  1910. sideways false
  1911. status open
  1912. \begin_layout Plain Layout
  1913. \align center
  1914. \begin_inset Graphics
  1915. filename images/sphere2500-result.pdf
  1916. width 70text%
  1917. BoundingBox 60bp 150bp 610bp 610bp
  1918. clip
  1919. \end_inset
  1920. \begin_inset Caption Standard
  1921. \begin_layout Plain Layout
  1922. \begin_inset CommandInset label
  1923. LatexCommand label
  1924. name "fig:w100-1"
  1925. \end_inset
  1926. 3D plot of sphere example (due to Michael Kaess).
  1927. The very wrong initial estimate, derived from odometry, is shown in green.
  1928. The optimized trajectory is shown red.
  1929. Code below:
  1930. \end_layout
  1931. \end_inset
  1932. \end_layout
  1933. \end_inset
  1934. \end_layout
  1935. \begin_layout Standard
  1936. \begin_inset CommandInset include
  1937. LatexCommand lstinputlisting
  1938. filename "Code/Pose3SLAMExample-graph.m"
  1939. lstparams "aboveskip=10pt,basicstyle={\\ttfamily\\small},captionpos=b,frame=single,identifierstyle={\\bfseries},language={Matlab},numbers=left,caption={Excerpt from matlab/gtsam\\_examples/Pose3SLAMExample\\_graph.m},label={listing:Pose3SLAMExample-graph-1}"
  1940. \end_inset
  1941. \end_layout
  1942. \begin_layout Section
  1943. \begin_inset CommandInset label
  1944. LatexCommand label
  1945. name "sec:Landmark-based-SLAM"
  1946. \end_inset
  1947. Landmark-based SLAM
  1948. \end_layout
  1949. \begin_layout Subsection
  1950. Basics
  1951. \end_layout
  1952. \begin_layout Standard
  1953. \begin_inset Float figure
  1954. placement h
  1955. wide false
  1956. sideways false
  1957. status open
  1958. \begin_layout Plain Layout
  1959. \align center
  1960. \begin_inset Graphics
  1961. filename images/FactorGraph4.pdf
  1962. scale 80
  1963. BoundingBox 50bp 590bp 290bp 710bp
  1964. clip
  1965. \end_inset
  1966. \begin_inset Caption Standard
  1967. \begin_layout Plain Layout
  1968. \begin_inset CommandInset label
  1969. LatexCommand label
  1970. name "fig:SLAM"
  1971. \end_inset
  1972. Factor graph for landmark-based SLAM
  1973. \end_layout
  1974. \end_inset
  1975. \end_layout
  1976. \end_inset
  1977. \end_layout
  1978. \begin_layout Standard
  1979. \begin_inset ERT
  1980. status open
  1981. \begin_layout Plain Layout
  1982. \backslash
  1983. noindent
  1984. \end_layout
  1985. \end_inset
  1986. In
  1987. \series bold
  1988. landmark-based SLAM
  1989. \series default
  1990. , we explicitly build a map with the location of observed landmarks, which
  1991. introduces a second type of variable in the factor graph besides robot
  1992. poses.
  1993. An example factor graph for a landmark-based SLAM example is shown in Figure
  1994. \begin_inset CommandInset ref
  1995. LatexCommand ref
  1996. reference "fig:SLAM"
  1997. \end_inset
  1998. , which shows the typical connectivity: poses are connected in an odometry
  1999. Markov chain, and landmarks are observed from multiple poses, inducing
  2000. binary factors.
  2001. In addition, the pose
  2002. \begin_inset Formula $x_{1}$
  2003. \end_inset
  2004. has the usual prior on it.
  2005. \end_layout
  2006. \begin_layout Standard
  2007. \begin_inset Float figure
  2008. placement h
  2009. wide false
  2010. sideways false
  2011. status open
  2012. \begin_layout Plain Layout
  2013. \align center
  2014. \begin_inset Graphics
  2015. filename images/example2.pdf
  2016. scale 47
  2017. BoundingBox 90bp 220bp 520bp 555bp
  2018. clip
  2019. \end_inset
  2020. \begin_inset Caption Standard
  2021. \begin_layout Plain Layout
  2022. \begin_inset CommandInset label
  2023. LatexCommand label
  2024. name "fig:PlanarSLAMExample"
  2025. \end_inset
  2026. The optimized result along with covariance ellipses for both poses (in green)
  2027. and landmarks (in blue).
  2028. Also shown are the trajectory (red) and landmark sightings (cyan).
  2029. \end_layout
  2030. \end_inset
  2031. \end_layout
  2032. \end_inset
  2033. \end_layout
  2034. \begin_layout Standard
  2035. The factor graph from Figure
  2036. \begin_inset CommandInset ref
  2037. LatexCommand ref
  2038. reference "fig:SLAM"
  2039. \end_inset
  2040. can be created using the MATLAB code in Listing
  2041. \begin_inset CommandInset ref
  2042. LatexCommand ref
  2043. reference "listing:PlanarSLAMExample"
  2044. \end_inset
  2045. .
  2046. As before, on line 2 we create the factor graph, and Lines 8-18 create
  2047. the prior/odometry chain we are now familiar with.
  2048. However, the code on lines 20-25 is new: it creates three
  2049. \series bold
  2050. measurement factors
  2051. \series default
  2052. , in this case
  2053. \begin_inset Quotes eld
  2054. \end_inset
  2055. bearing/range
  2056. \begin_inset Quotes erd
  2057. \end_inset
  2058. measurements from the pose to the landmark.
  2059. \end_layout
  2060. \begin_layout Standard
  2061. \begin_inset Newpage pagebreak
  2062. \end_inset
  2063. \end_layout
  2064. \begin_layout Standard
  2065. \begin_inset CommandInset include
  2066. LatexCommand lstinputlisting
  2067. filename "Code/PlanarSLAMExample.m"
  2068. lstparams "aboveskip=10pt,basicstyle={\\ttfamily\\small},captionpos=b,frame=single,identifierstyle={\\bfseries},language={Matlab},numbers=left,caption={Excerpt from matlab/gtsam\\_examples/PlanarSLAMExample.m},label={listing:PlanarSLAMExample}"
  2069. \end_inset
  2070. \end_layout
  2071. \begin_layout Subsection
  2072. Of Keys and Symbols
  2073. \end_layout
  2074. \begin_layout Standard
  2075. \begin_inset ERT
  2076. status open
  2077. \begin_layout Plain Layout
  2078. \backslash
  2079. noindent
  2080. \end_layout
  2081. \end_inset
  2082. The only unexplained code is on lines 4-6: here we create integer keys
  2083. for the poses and landmarks using the
  2084. \series bold
  2085. \emph on
  2086. symbol
  2087. \series default
  2088. \emph default
  2089. function.
  2090. In GTSAM, we address all variables using the
  2091. \series bold
  2092. \emph on
  2093. Ke
  2094. \emph default
  2095. y
  2096. \series default
  2097. type, which is just a typedef to
  2098. \series bold
  2099. \emph on
  2100. size_t
  2101. \series default
  2102. \emph default
  2103. \begin_inset Foot
  2104. status open
  2105. \begin_layout Plain Layout
  2106. a 32 or 64 bit integer, depending on your platform
  2107. \end_layout
  2108. \end_inset
  2109. .
  2110. The keys do not have to be numbered continuously, but they do have to be
  2111. unique within a given factor graph.
  2112. For factor graphs with different types of variables, we provide the
  2113. \series bold
  2114. \emph on
  2115. symbol
  2116. \series default
  2117. \emph default
  2118. function in MATLAB, and the
  2119. \series bold
  2120. \emph on
  2121. Symbol
  2122. \series default
  2123. \emph default
  2124. type in C++, to help you create (large) integer keys that are far apart
  2125. in the space of possible keys, so you don't have to think about starting
  2126. the point numbering at some arbitrary offset.
  2127. To create a a
  2128. \emph on
  2129. symbol key
  2130. \emph default
  2131. you simply provide a character and an integer index.
  2132. You can use base 0 or 1, or use arbitrary indices: it does not matter.
  2133. In the code above, we we use 'x' for poses, and 'l' for landmarks.
  2134. \begin_inset Note Note
  2135. status collapsed
  2136. \begin_layout Plain Layout
  2137. , and use the resulting keys
  2138. \series bold
  2139. \emph on
  2140. i1
  2141. \series default
  2142. \emph default
  2143. ,
  2144. \series bold
  2145. \emph on
  2146. i2
  2147. \series default
  2148. \emph default
  2149. ,
  2150. \series bold
  2151. \emph on
  2152. i3
  2153. \series default
  2154. \emph default
  2155. ,
  2156. \series bold
  2157. \emph on
  2158. j1
  2159. \series default
  2160. \emph default
  2161. , and
  2162. \series bold
  2163. \emph on
  2164. j2
  2165. \series default
  2166. \emph default
  2167. to create the factors in the correct way.
  2168. \end_layout
  2169. \end_inset
  2170. \end_layout
  2171. \begin_layout Standard
  2172. The optimized result for the factor graph created by Listing
  2173. \begin_inset CommandInset ref
  2174. LatexCommand ref
  2175. reference "listing:PlanarSLAMExample"
  2176. \end_inset
  2177. is shown in Figure
  2178. \begin_inset CommandInset ref
  2179. LatexCommand ref
  2180. reference "fig:PlanarSLAMExample"
  2181. \end_inset
  2182. , and it is readily apparent that the landmark
  2183. \begin_inset Formula $l_{1}$
  2184. \end_inset
  2185. with two measurements is better localized.
  2186. In MATLAB we can also examine the actual numerical values, and doing so
  2187. reveals some more GTSAM magic:
  2188. \end_layout
  2189. \begin_layout Standard
  2190. \size small
  2191. \begin_inset CommandInset include
  2192. LatexCommand verbatiminput
  2193. filename "Code/PlanarSLAMExample.txt"
  2194. \end_inset
  2195. \end_layout
  2196. \begin_layout Standard
  2197. \begin_inset ERT
  2198. status open
  2199. \begin_layout Plain Layout
  2200. \backslash
  2201. noindent
  2202. \end_layout
  2203. \end_inset
  2204. Indeed, the keys generated by symbol are automatically detected by the
  2205. \series bold
  2206. \emph on
  2207. print
  2208. \series default
  2209. \emph default
  2210. method in the
  2211. \series bold
  2212. \emph on
  2213. Values
  2214. \series default
  2215. \emph default
  2216. class, and rendered in human-readable form
  2217. \begin_inset Quotes eld
  2218. \end_inset
  2219. x1
  2220. \begin_inset Quotes erd
  2221. \end_inset
  2222. ,
  2223. \begin_inset Quotes eld
  2224. \end_inset
  2225. l2
  2226. \begin_inset Quotes erd
  2227. \end_inset
  2228. , etc, rather than as large, unwieldy integers.
  2229. This magic extends to most factors and other classes where the
  2230. \series bold
  2231. Key
  2232. \series default
  2233. type is used.
  2234. \end_layout
  2235. \begin_layout Subsection
  2236. A Larger Example
  2237. \end_layout
  2238. \begin_layout Standard
  2239. \begin_inset Float figure
  2240. placement h
  2241. wide false
  2242. sideways false
  2243. status open
  2244. \begin_layout Plain Layout
  2245. \align center
  2246. \begin_inset Graphics
  2247. filename images/littleRobot.pdf
  2248. width 90text%
  2249. BoundingBox 0bp 200bp 612bp 600bp
  2250. clip
  2251. \end_inset
  2252. \begin_inset Caption Standard
  2253. \begin_layout Plain Layout
  2254. \begin_inset CommandInset label
  2255. LatexCommand label
  2256. name "fig:littleRobot"
  2257. \end_inset
  2258. A larger example with about 100 poses and 30 or so landmarks, as produced
  2259. by gtsam_examples/PlanarSLAMExample_graph.m
  2260. \end_layout
  2261. \end_inset
  2262. \end_layout
  2263. \end_inset
  2264. \end_layout
  2265. \begin_layout Standard
  2266. GTSAM comes with a slightly larger example that is read from a .graph file
  2267. by PlanarSLAMExample_graph.m, shown in Figure
  2268. \begin_inset CommandInset ref
  2269. LatexCommand ref
  2270. reference "fig:littleRobot"
  2271. \end_inset
  2272. .
  2273. To not clutter the figure only the marginals are shown, not the lines of
  2274. sight.
  2275. This example, with 119 (multivariate) variables and 517 factors optimizes
  2276. in less than 10 ms.
  2277. \end_layout
  2278. \begin_layout Subsection
  2279. A Real-World Example
  2280. \end_layout
  2281. \begin_layout Standard
  2282. \begin_inset Float figure
  2283. placement h
  2284. wide false
  2285. sideways false
  2286. status open
  2287. \begin_layout Plain Layout
  2288. \align center
  2289. \begin_inset Graphics
  2290. filename images/Victoria.pdf
  2291. width 90text%
  2292. BoundingBox 0bp 0bp 420bp 180bp
  2293. clip
  2294. \end_inset
  2295. \begin_inset Caption Standard
  2296. \begin_layout Plain Layout
  2297. \begin_inset CommandInset label
  2298. LatexCommand label
  2299. name "fig:Victoria-1"
  2300. \end_inset
  2301. Small section of optimized trajectory and landmarks (trees detected in a
  2302. laser range finder scan) from data recorded in Sydney's Victoria Park (dataset
  2303. due to Jose Guivant, U.
  2304. Sydney).
  2305. \end_layout
  2306. \end_inset
  2307. \end_layout
  2308. \end_inset
  2309. \end_layout
  2310. \begin_layout Standard
  2311. A real-world example is shown in Figure
  2312. \begin_inset CommandInset ref
  2313. LatexCommand ref
  2314. reference "fig:Victoria-1"
  2315. \end_inset
  2316. , using data from a well known dataset collected in Sydney's Victoria Park,
  2317. using a truck equipped with a laser range-finder.
  2318. The covariance matrices in this figure were computed very efficiently,
  2319. as explained in detail in
  2320. \begin_inset CommandInset citation
  2321. LatexCommand citep
  2322. key "Kaess09ras"
  2323. \end_inset
  2324. .
  2325. The exact covariances (blue, smaller ellipses) obtained by our fast algorithm
  2326. coincide with the exact covariances based on full inversion (orange, mostly
  2327. hidden by blue).
  2328. The much larger conservative covariance estimates (green, large ellipses)
  2329. were based on our earlier work in
  2330. \begin_inset CommandInset citation
  2331. LatexCommand citep
  2332. key "Kaess08tro"
  2333. \end_inset
  2334. .
  2335. \end_layout
  2336. \begin_layout Standard
  2337. \begin_inset Newpage pagebreak
  2338. \end_inset
  2339. \end_layout
  2340. \begin_layout Section
  2341. Structure from Motion
  2342. \end_layout
  2343. \begin_layout Standard
  2344. \begin_inset Float figure
  2345. placement h
  2346. wide false
  2347. sideways false
  2348. status open
  2349. \begin_layout Plain Layout
  2350. \align center
  2351. \begin_inset Graphics
  2352. filename images/cube.pdf
  2353. width 80text%
  2354. BoundingBox 60bp 90bp 612bp 380bp
  2355. clip
  2356. \end_inset
  2357. \begin_inset Caption Standard
  2358. \begin_layout Plain Layout
  2359. \begin_inset CommandInset label
  2360. LatexCommand label
  2361. name "fig:SFMExample"
  2362. \end_inset
  2363. An optimized
  2364. \begin_inset Quotes eld
  2365. \end_inset
  2366. Structure from Motion
  2367. \begin_inset Quotes erd
  2368. \end_inset
  2369. with 10 cameras arranged in a circle, observing the 8 vertices of a
  2370. \begin_inset Formula $20\times20\times20$
  2371. \end_inset
  2372. cube centered around the origin.
  2373. The camera is rendered with color-coded axes, (RGB for XYZ) and the viewing
  2374. direction is is along the positive Z-axis.
  2375. Also shown are the 3D error covariance ellipses for both cameras and points.
  2376. \end_layout
  2377. \end_inset
  2378. \end_layout
  2379. \end_inset
  2380. \end_layout
  2381. \begin_layout Standard
  2382. \begin_inset ERT
  2383. status open
  2384. \begin_layout Plain Layout
  2385. \backslash
  2386. noindent
  2387. \end_layout
  2388. \end_inset
  2389. \series bold
  2390. Structure from Motion
  2391. \series default
  2392. (SFM) is a technique to recover a 3D reconstruction of the environment
  2393. from corresponding visual features in a collection of
  2394. \emph on
  2395. unordered
  2396. \emph default
  2397. images, see Figure
  2398. \begin_inset CommandInset ref
  2399. LatexCommand ref
  2400. reference "fig:SFMExample"
  2401. \end_inset
  2402. .
  2403. In GTSAM this is done using exactly the same factor graph framework, simply
  2404. using SFM-specific measurement factors.
  2405. In particular, there is a
  2406. \series bold
  2407. projection factor
  2408. \series default
  2409. that calculates the reprojection error
  2410. \begin_inset Formula $f(x_{i},p_{j};z_{ij},K)$
  2411. \end_inset
  2412. for a given camera pose
  2413. \begin_inset Formula $x_{i}$
  2414. \end_inset
  2415. (a
  2416. \series bold
  2417. \emph on
  2418. Pose3
  2419. \series default
  2420. \emph default
  2421. ) and point
  2422. \begin_inset Formula $p_{j}$
  2423. \end_inset
  2424. (a
  2425. \series bold
  2426. \emph on
  2427. Point3
  2428. \series default
  2429. \emph default
  2430. ).
  2431. The factor is parameterized by the 2D measurement
  2432. \begin_inset Formula $z_{ij}$
  2433. \end_inset
  2434. (a
  2435. \series bold
  2436. \emph on
  2437. Point2
  2438. \series default
  2439. \emph default
  2440. ), and known calibration parameters
  2441. \begin_inset Formula $K$
  2442. \end_inset
  2443. (of type
  2444. \series bold
  2445. \emph on
  2446. Cal3_S2
  2447. \series default
  2448. \emph default
  2449. ).
  2450. The following listing shows how to create the factor graph:
  2451. \begin_inset CommandInset include
  2452. LatexCommand lstinputlisting
  2453. filename "Code/SFMExample.m"
  2454. lstparams "aboveskip=10pt,basicstyle={\\ttfamily\\small},captionpos=b,frame=single,identifierstyle={\\bfseries},language={Matlab},numbers=left,caption={Excerpt from matlab/gtsam\\_examples/SFMExample.m},label={listing:SFMExample}"
  2455. \end_inset
  2456. \end_layout
  2457. \begin_layout Standard
  2458. \begin_inset ERT
  2459. status open
  2460. \begin_layout Plain Layout
  2461. \backslash
  2462. noindent
  2463. \end_layout
  2464. \end_inset
  2465. In Listing
  2466. \begin_inset CommandInset ref
  2467. LatexCommand ref
  2468. reference "listing:SFMExample"
  2469. \end_inset
  2470. , assuming that the factor graph was already created, we add measurement
  2471. factors in the double loop.
  2472. We loop over images with index
  2473. \begin_inset Formula $i$
  2474. \end_inset
  2475. , and in this example the data is given as two cell arrays: Z{i} specifies
  2476. a set of measurements
  2477. \begin_inset Formula $z_{k}$
  2478. \end_inset
  2479. in image
  2480. \begin_inset Formula $i$
  2481. \end_inset
  2482. , and
  2483. \begin_inset Formula $J\{i\}$
  2484. \end_inset
  2485. specifies the corresponding point index.
  2486. The specific factor type we use is a
  2487. \series bold
  2488. \emph on
  2489. GenericProjectionFactorCal3_S2
  2490. \series default
  2491. \emph default
  2492. , which is the MATLAB equivalent of the C++ class
  2493. \series bold
  2494. \emph on
  2495. GenericProjectionFactor<Cal3_S2>
  2496. \series default
  2497. \emph default
  2498. , where
  2499. \series bold
  2500. \emph on
  2501. Cal3_S2
  2502. \series default
  2503. \emph default
  2504. is the camera calibration type we choose to use (the standard, no-radial
  2505. distortion, 5 parameter calibration matrix).
  2506. As before landmark-based SLAM (Section
  2507. \begin_inset CommandInset ref
  2508. LatexCommand ref
  2509. reference "sec:Landmark-based-SLAM"
  2510. \end_inset
  2511. ), here we use symbol keys except we now use the character 'p' to denote
  2512. points, rather than 'l' for landmark.
  2513. \end_layout
  2514. \begin_layout Standard
  2515. Important note: a very tricky and difficult part of making SFM work is (a)
  2516. data association, and (b) initialization.
  2517. GTSAM does neither of these things for you: it simply provides the
  2518. \begin_inset Quotes eld
  2519. \end_inset
  2520. bundle adjustment
  2521. \begin_inset Quotes erd
  2522. \end_inset
  2523. optimization.
  2524. In the example, we simply assume the data association is known (it is encoded
  2525. in the J sets), and we initialize with the ground truth, as the intent
  2526. of the example is simply to show you how to set up the optimization problem.
  2527. \end_layout
  2528. \begin_layout Section
  2529. iSAM: Incremental Smoothing and Mapping
  2530. \end_layout
  2531. \begin_layout Standard
  2532. GTSAM provides an incremental inference algorithm based on a more advanced
  2533. graphical model, the Bayes tree, which is kept up to date by the
  2534. \series bold
  2535. iSAM
  2536. \series default
  2537. algorithm (incremental Smoothing and Mapping, see
  2538. \begin_inset CommandInset citation
  2539. LatexCommand citet
  2540. key "Kaess08tro,Kaess12ijrr"
  2541. \end_inset
  2542. for an in-depth treatment).
  2543. For mobile robots operating in real-time it is important to have access
  2544. to an updated map as soon as new sensor measurements come in.
  2545. iSAM keeps the map up-to-date in an efficient manner.
  2546. \end_layout
  2547. \begin_layout Standard
  2548. Listing
  2549. \begin_inset CommandInset ref
  2550. LatexCommand ref
  2551. reference "listing:iSAMExample"
  2552. \end_inset
  2553. shows how to use iSAM in a simple visual SLAM example.
  2554. In line 1-2 we create a
  2555. \series bold
  2556. \emph on
  2557. NonlinearISAM
  2558. \series default
  2559. \emph default
  2560. object which will relinearize and reorder the variables every 3 steps.
  2561. The corect value for this parameter depends on how non-linear your problem
  2562. is and how close you want to be to gold-standard solution at every step.
  2563. In iSAM 2.0, this parameter is not needed, as iSAM2 automatically determines
  2564. when linearization is needed and for which variables.
  2565. \end_layout
  2566. \begin_layout Standard
  2567. The example involves eight 3D points that are seen from eight successive
  2568. camera poses.
  2569. Hence in the first step -which is omitted here- all eight landmarks and
  2570. the first pose are properly initialized.
  2571. In the code this is done by perturbing the known ground truth, but in a
  2572. real application great care is needed to properly initialize poses and
  2573. landmarks, especially in a monocular sequence.
  2574. \end_layout
  2575. \begin_layout Standard
  2576. \begin_inset Newpage pagebreak
  2577. \end_inset
  2578. \begin_inset CommandInset include
  2579. LatexCommand lstinputlisting
  2580. filename "Code/VisualISAMExample.cpp"
  2581. lstparams "aboveskip=10pt,basicstyle={\\ttfamily\\small},captionpos=b,frame=single,identifierstyle={\\bfseries},language={C++},numbers=left,caption={Excerpt from examples/VisualISAMExample.cpp},label={listing:iSAMExample}"
  2582. \end_inset
  2583. \end_layout
  2584. \begin_layout Standard
  2585. The remainder of the code illustrates a typical iSAM loop:
  2586. \end_layout
  2587. \begin_layout Enumerate
  2588. Create factors for new measurements.
  2589. Here, in lines 9-18, a small
  2590. \series bold
  2591. \emph on
  2592. NonlinearFactorGraph
  2593. \series default
  2594. \emph default
  2595. is created to hold the new factors of type
  2596. \series bold
  2597. \emph on
  2598. GenericProjectionFactor<Pose3, Point3, Cal3_S2>
  2599. \series default
  2600. \emph default
  2601. .
  2602. \end_layout
  2603. \begin_layout Enumerate
  2604. Create an initial estimate for all newly introduced variables.
  2605. In this small example, all landmarks have been observed in frame 1 and
  2606. hence the only new variable that needs to be initialized at each time step
  2607. is the new pose.
  2608. This is done in lines 20-22.
  2609. Note we assume a good initial estimate is available as
  2610. \emph on
  2611. initial_x[i]
  2612. \emph default
  2613. .
  2614. \end_layout
  2615. \begin_layout Enumerate
  2616. Finally, we call
  2617. \emph on
  2618. isam.update()
  2619. \emph default
  2620. , which takes the factors and initial estimates, and incrementally updates
  2621. the solution, which is available through the method
  2622. \emph on
  2623. isam.estimate()
  2624. \emph default
  2625. , if desired.
  2626. \end_layout
  2627. \begin_layout Standard
  2628. \begin_inset Newpage pagebreak
  2629. \end_inset
  2630. \end_layout
  2631. \begin_layout Section
  2632. More Applications
  2633. \end_layout
  2634. \begin_layout Standard
  2635. While a detailed discussion of all the things you can do with GTSAM will
  2636. take us too far, below is a small survey of what you can expect to do,
  2637. and which we did using GTSAM.
  2638. \end_layout
  2639. \begin_layout Standard
  2640. \begin_inset Note Note
  2641. status open
  2642. \begin_layout Plain Layout
  2643. \begin_inset Float figure
  2644. wide false
  2645. sideways false
  2646. status collapsed
  2647. \begin_layout Plain Layout
  2648. \align center
  2649. \begin_inset Graphics
  2650. filename figures/SAM/FactorGraph.pdf
  2651. scale 80
  2652. BoundingBox 40bp 600bp 300bp 792bp
  2653. clip
  2654. \end_inset
  2655. \begin_inset Caption Standard
  2656. \begin_layout Plain Layout
  2657. \begin_inset CommandInset label
  2658. LatexCommand label
  2659. name "fig:Filtering"
  2660. \end_inset
  2661. Factor graph explanation of Filtering.
  2662. \end_layout
  2663. \end_inset
  2664. \end_layout
  2665. \end_inset
  2666. \end_layout
  2667. \begin_layout Plain Layout
  2668. The factor graph for landmark-based SLAM is shown in Figure
  2669. \begin_inset CommandInset ref
  2670. LatexCommand ref
  2671. reference "fig:Filtering"
  2672. \end_inset
  2673. .
  2674. \end_layout
  2675. \end_inset
  2676. \end_layout
  2677. \begin_layout Subsection
  2678. Conjugate Gradient Optimization
  2679. \end_layout
  2680. \begin_layout Standard
  2681. \begin_inset Float figure
  2682. placement h
  2683. wide false
  2684. sideways false
  2685. status open
  2686. \begin_layout Plain Layout
  2687. \align center
  2688. \begin_inset Graphics
  2689. filename images/Beijing.pdf
  2690. width 70text%
  2691. BoundingBox 100bp 240bp 500bp 550bp
  2692. clip
  2693. \end_inset
  2694. \begin_inset Caption Standard
  2695. \begin_layout Plain Layout
  2696. \begin_inset CommandInset label
  2697. LatexCommand label
  2698. name "fig:Beijing"
  2699. \end_inset
  2700. A map of Beijing, with a spanning tree shown in black, and the remaining
  2701. \emph on
  2702. loop-closing
  2703. \emph default
  2704. constraints shown in red.
  2705. A spanning tree can be used as a
  2706. \emph on
  2707. preconditioner
  2708. \emph default
  2709. by GTSAM.
  2710. \end_layout
  2711. \end_inset
  2712. \end_layout
  2713. \end_inset
  2714. \end_layout
  2715. \begin_layout Standard
  2716. GTSAM also includes efficient preconditioned conjugate gradients (PCG) methods
  2717. for solving large-scale SLAM problems.
  2718. While direct methods, popular in the literature, exhibit quadratic convergence
  2719. and can be quite efficient for sparse problems, they typically require
  2720. a lot of storage and efficient elimination orderings to be found.
  2721. In contrast, iterative optimization methods only require access to the
  2722. gradient and have a small memory footprint, but can suffer from poor convergenc
  2723. e.
  2724. Our method,
  2725. \emph on
  2726. subgraph preconditioning
  2727. \emph default
  2728. , explained in detail in
  2729. \begin_inset CommandInset citation
  2730. LatexCommand citet
  2731. key "Dellaert10iros,Jian11iccv"
  2732. \end_inset
  2733. , combines the advantages of direct and iterative methods, by identifying
  2734. a sub-problem that can be easily solved using direct methods, and solving
  2735. for the remaining part using PCG.
  2736. The easy sub-problems correspond to a spanning tree, a planar subgraph,
  2737. or any other substructure that can be efficiently solved.
  2738. An example of such a subgraph is shown in Figure
  2739. \begin_inset CommandInset ref
  2740. LatexCommand ref
  2741. reference "fig:Beijing"
  2742. \end_inset
  2743. .
  2744. \end_layout
  2745. \begin_layout Subsection
  2746. Visual Odometry
  2747. \end_layout
  2748. \begin_layout Standard
  2749. A gentle introduction to vision-based sensing is
  2750. \series bold
  2751. Visual Odometry
  2752. \series default
  2753. (abbreviated VO, see e.g.
  2754. \begin_inset CommandInset citation
  2755. LatexCommand citet
  2756. key "Nister04cvpr2"
  2757. \end_inset
  2758. ), which provides pose constraints between successive robot poses by tracking
  2759. or associating visual features in successive images taken by a camera mounted
  2760. rigidly on the robot.
  2761. GTSAM includes both C++ and MATLAB example code, as well as VO-specific
  2762. factors to help you on the way.
  2763. \end_layout
  2764. \begin_layout Subsection
  2765. Visual SLAM
  2766. \end_layout
  2767. \begin_layout Standard
  2768. \series bold
  2769. Visual SLAM
  2770. \series default
  2771. (see e.g.,
  2772. \begin_inset CommandInset citation
  2773. LatexCommand citet
  2774. key "Davison03iccv"
  2775. \end_inset
  2776. ) is a SLAM variant where 3D points are observed by a camera as the camera
  2777. moves through space, either mounted on a robot or moved around by hand.
  2778. GTSAM, and particularly iSAM (see below), can easily be adapted to be used
  2779. as the back-end optimizer in such a scenario.
  2780. \end_layout
  2781. \begin_layout Subsection
  2782. Fixed-lag Smoothing and Filtering
  2783. \end_layout
  2784. \begin_layout Standard
  2785. GTSAM can easily perform recursive estimation, where only a subset of the
  2786. poses are kept in the factor graph, while the remaining poses are marginalized
  2787. out.
  2788. In all examples above we explicitly optimize for all variables using all
  2789. available measurements, which is called
  2790. \series bold
  2791. Smoothing
  2792. \series default
  2793. because the trajectory is
  2794. \begin_inset Quotes eld
  2795. \end_inset
  2796. smoothed
  2797. \begin_inset Quotes erd
  2798. \end_inset
  2799. out, and this is where GTSAM got its name (GT
  2800. \emph on
  2801. Smoothing
  2802. \emph default
  2803. and Mapping).
  2804. When instead only the last few poses are kept in the graph, one speaks
  2805. of
  2806. \series bold
  2807. Fixed-lag Smoothing
  2808. \series default
  2809. .
  2810. Finally, when only the single most recent poses is kept, one speaks of
  2811. \series bold
  2812. Filtering
  2813. \series default
  2814. , and indeed the original formulation of SLAM was filter-based
  2815. \begin_inset CommandInset citation
  2816. LatexCommand citep
  2817. key "Smith87b"
  2818. \end_inset
  2819. .
  2820. \end_layout
  2821. \begin_layout Subsection
  2822. Discrete Variables and HMMs
  2823. \end_layout
  2824. \begin_layout Standard
  2825. Finally, factor graphs are not limited to continuous variables: GTSAM can
  2826. also be used to model and solve discrete optimization problems.
  2827. For example, a Hidden Markov Model (HMM) has the same graphical model structure
  2828. as the Robot Localization problem from Section
  2829. \begin_inset CommandInset ref
  2830. LatexCommand ref
  2831. reference "sec:Robot-Localization"
  2832. \end_inset
  2833. , except that in an HMM the variables are discrete.
  2834. GTSAM can optimize and perform inference for discrete models.
  2835. \end_layout
  2836. \begin_layout Section*
  2837. Acknowledgements
  2838. \end_layout
  2839. \begin_layout Standard
  2840. GTSAM was made possible by the efforts of many collaborators at Georgia
  2841. Tech and elsewhere, including but not limited to Doru Balcan, Chris Beall,
  2842. Alex Cunningham, Alireza Fathi, Eohan George, Viorela Ila, Yong-Dian Jian,
  2843. Michael Kaess, Kai Ni, Carlos Nieto, Duy-Nguyen Ta, Manohar Paluri, Christian
  2844. Potthast, Richard Roberts, Grant Schindler, and Stephen Williams.
  2845. In addition, Paritosh Mohan helped me with the manual.
  2846. Many thanks all for your hard work!
  2847. \end_layout
  2848. \begin_layout Standard
  2849. \begin_inset Newpage pagebreak
  2850. \end_inset
  2851. \begin_inset CommandInset bibtex
  2852. LatexCommand bibtex
  2853. bibfiles "gtsam"
  2854. options "apalike"
  2855. \end_inset
  2856. \end_layout
  2857. \end_body
  2858. \end_document