This is an extra lesson that I wanted to add.
And here I'm going to talk about Ross two bags, which can be very useful when working with topics.
So this is more of a parenthesis in this course, and it's the only time I will talk about bags so you
can see it as a bonus.
And to understand what bags are and why we need them, let's start with the problem.
Let's use an example.
Let's say you need to test a robot outside so you have a mobile robot or whatever robot.
You need to test your robot outside when it's raining.
And let's say you are working in a university or in a workplace, but then let's say, well, you don't
have access to the robot all the time.
Maybe you cannot take it outside all the time, and maybe when you take it outside, it's not necessarily
raining all the time.
So that's going to be a problem, especially if you need to develop a feature to improve the robot behavior
when it's raining outside.
And so instead of waiting every time that you have access to the robot, that you can take it outside
and that it's raining, what you could do is to run the experiment once.
So let's say you take it one hour outside when it's raining, you run the experiment once and you save
all the data from the topics.
So for example, if you have a camera topic, if you have will topics, while all the topics that you
have in your applications, you could save the ones that you want inside the data set.
And then you could replay this data set later on, and then you could develop the feature of your application
for this data set.
Okay.
That's going to be much more convenient.
And that's exactly what bags allow you to do.
So with a Ros2 bag, you can save the data from a topic for any amount of time you want, and then you
can replay this data as many times as you want.
And let's see how to do that.
So first of all we will need a publisher okay.
So I'm going to start the publisher we just had from the previous activity.
I'm going to start here Ros to run my pip pig.
And let's start the number.
Publisher.
So that's going to publish on a number topic.
And I'm also going to start here to run my Pi PG number counter.
So here we have a subscriber.
And we also have another publisher.
If I do roster topic list you can see that we have a number topic and a number count topic okay.
So let's say we want to save one of those topics or the two of them.
So I am now in my home directory.
Let's create well let's keep things organized I'm going to create a folder named bags okay.
And we're going to save the bags in that folder just so they are not everywhere.
And then we have the Ros2 command line tool if you remember.
And in this command line we also have Ros2 bag.
And with Ros2 bag you can do dash h.
And of course you have all of the possible commands.
Okay.
So here I'm going to do Ros2 bag and how to record a topic.
I'm going to do record, and then I need to provide the name of a topic.
So let's do a Ross two topic list again here.
Let's say I want to save the number count topic.
So I'm going to do record and then slash number count.
Let's press enter.
And you see we have a lot of stuff here.
So it's listening for topics.
You see we have subscribed to the topic number count okay.
It's recording.
And well you see it's hanging in there.
So it's going to record.
As long as this is running then you can press Ctrl C.
And when you press Ctrl C you see that the recording has stopped.
And here from the logs it seems you see I had 64 and 84.
So I have recorded for about 20s, which means that all the data that was published on that number count
topic for those 20s are now recorded inside a bag.
And if I do lose?
You see, I have a Ross bag here with a time stamp.
So if I go inside, what do we have?
Well, you see, we have some information and the data itself with a map file.
So that's going to contain all of the topics you have saved.
And if I show what's in this metadata.yaml.
Well you can find for example, well the duration in nanoseconds, the starting time and then the different
topics that we have saved.
So you see here we have saved the number count topic with the type and then how many messages we have
saved.
You see we have saved 20 messages and a bit more stuff.
Let's clear that.
Let's go back to our bikes folder.
So we don't really need to go inside the bike itself.
But now you know that.
Well, in this bag we have a YAML file with some information and then the messages.
Now let's start this command again.
So Rose two bike record.
And let's add here a dash o option with test.
Because here by default is going to do rosbag two and then a timestamp.
But you can do a dash o to provide the output name okay.
So for example here I'm going to record for a few seconds and then press control C.
And you see I have the test folder okay.
So you can rename the bag with dash o.
And then you should only have.
Well if you're going to record again you see the folder already exists.
So you need to give a new name.
All right.
So test one for example.
And then control C.
And you see we have our test and test one folder.
You can do rose two bag info as well with for example test.
So you just provide the folder name.
And you can see here actually well we have the back side.
We have the duration, the start, the end, the number of messages, the topics we have saved with
the data type, etc..
So you see what we saw before in this YAML file that's saved inside the folder.
What we find basically the same information here, but more readable.
So you don't even need to go inside.
You just do Ross Toolbag info and you get the same information.
And then now you know how to record topics inside the bag.
How to play a bag again, you can do Ross to bag and then play and then just the name.
So for example test is going to replay.
So you see here it's starting playback and it's ending with just a few seconds okay.
So it's going to replay and publish again all the data that was received on that topic.
Let's do a.
So let's do a Ross two topic equal number count okay.
So here we have what we have this node here I'm going to stop all the nodes.
Okay, so now we don't have anything publishing on that number count.
As you can see.
And I'm going to do Ros2 bag play.
And I'm going to play the first one that I have recorded, this one.
So Rosbag two with a timestamp.
And let's press enter.
And you see now we receive the data here on the number count topic.
And you see at this time the counter was at 180.
And then it's continuing to be published for that one was for 20s.
So let's wait a bit more.
And you see, after 20s the bag stops.
So the ros2 bag play command stops and we have received 20 messages.
All right.
So everything you have recorded at that time is going to be published exactly as it was, because you
can see we were reaching 750 for the number count with the number counter node, but when we recode
it, it was 180.
So we are playing back the data that was at that time.
And so you can see that's very useful.
You can just play the data exactly as it was when you tested your robot.
And finally, let's so let's, uh, run the publisher and the counter again and let's do a Ros two bag
and recode again.
So recode and then oh, let's name it test two.
And then, well, you have seen that you can use a topic name, but you can also provide several topic
names okay.
So number count and number.
In this case I'm going to record you see subscribe to topic number count and subscribe to topic number.
So you can record several topics.
I'm going to press Ctrl I'll see if I do.
Let's put that a bit bigger if I do.
Ross to bag info test two.
You see, we have two topics with nine messages for each.
So if you're going to replay, that bag is going to replay all of the topics that you saved.
And finally you can also just do dash A if you want to record all of the topic.
So in this case what.
Test two is already existing.
I'm going to put test three.
And you see now it's subscribing to all of the existing topics.
Okay I'm going to press Ctrl C.
And then if I do Ross two back info test three.
You see we have the number topic.
We even have extra topics that we didn't even see before.
Okay.
Number count.
We have the parameter events and the rows out.
All right so that's it for Ross.
Two bags.
And as you can see that can be a very useful functionality.