ROS Crash Course

Last modified by Leon Poon on 2021/08/30 23:24

Using C++.

Day 1

main function

g++ source1.cpp -o program1

Paths, "/" and "." and ".." in navigating directories - absolute vs relative

data type - binary itself is meaningless unless you say what information that the binary bits represent

declare variables (int, char, string as series of chars)

statements end with semicolon

cstdio library #include

calling a function - printf

if condition block else block (example: do something different based on argc)

for loop - 3 statements (initialiser, condition, post-loop advancement) and a code block

Homework: study "while" loop and "switch" statement

Day 2

While loop - condition and 1 code block.

Do while loop - code block then condition.

While loop = for loop with empty initialiser/advancement statements

Switch - match expression's result to case label, execute statements until break.

Switch vs if-else differences - number of times expressions are evaluated

write your own function

calling with parameters copied as separate set of var values in function

classes as blueprints.

complex var data type - instances of a class all have the same set of fields

the "." field accessor (var1.func1() etc)

each instance have it's own separate memory for storing data.

Example code - fibonacci sequence.

Value overflow because not enough number of bits to represent large numbers.

Various integer data types - specifier in printf

Homework: implement factorial (1 to 20) calculator

in Linux, most things are case-sensitive. (A not equals a)

Day 3

factorial printer - 2 approaches:

  1. for-loop that goes 1 to 20 and each loop calls fact(n ) function and prints return.
  2. for-loop that goes 1 to 20 and multiplies once each time and print.

set initial values of vars when declaring otherwise initial values are unpredictable

factorial function - implemented in recursive way, closer to mathematical definition

stack memory

stack frame to store var values in each function call

stack overflow/crash (segfault)

base condition to stop the recursion

recursive algorithms are common - e.g. when dealing with trees

spaces in file/directory names and how to navigate - space is special in terminal

understand names chosen by yourself vs names chosen by other people for your use

creating ros package - arguments: package name, dependencies

CMakeLists.txt (.txt file can also be used with other programs)

cmake = build tool - compile many files in some defined ways

package.xml - information for ROS

compiling - carefully read and understand and seek answers to error messages

msg files - fields declaration with data type

create cpp file for a node

quick reading of code for publisher node

homework: implement message subscriber node according to ROS book

Day 4

message subscriber.

argc and agrv

node name during ros::init

topic advertise/subscribe explained.

subscribe() only memorises that this node wants messages and what to do with them.

subscribe() - providing name of function. not calling.

publisher while ros::ok() loop.

subscriber spin() loop - handle events.

the double-colon operator: (1) namespace (2) static member of class

pointers are memory addresses

arrow -> operator

array are elements in sequence in memory.

char ** = pointer of pointer of char data.