> For the complete documentation index, see [llms.txt](https://campus-rover.gitbook.io/lab-notebook/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://campus-rover.gitbook.io/lab-notebook/fiiva/self-defined-message.md).

# How to define and Use your own message types

### How to create a new message type?

After created a ROS package, our package is constructed by a src folder, a CMakeLists.txt file, and a package.xml file. We need to create a msg folder to hold all of our new msg file. Then in your msg folder, create a new file \<new\_message>.msg, which contains fields you needed for this message type.\
For each fields in your msg file, define the name of the field and the type of the field (usually use std\_msgs/ or geometry\_msgs/). \<br > For example, if you want your message to have a list of string and an integer, you msg file should look like:

```
std_msgs/String[] list
std_msgs/Int16 int
```

### How to let the new message type recognized by ROS?

There are some modifications you need to make to CMakeLists.txt and package.xml in order to let the new message type recognized by ROS. \<br > For CMakeLists.txt:

1. Make sure message\_generation is in find\_package().
2. Uncomment add\_message\_files() and add your .msg file name to add\_message\_files().
3. Uncomment generate\_messages()
4. Modify catkin\_package() to

```
catkin_package(
  CATKIN_DEPENDS message_runtime
)
```

5. Uncomment include in include\_directories()

For package.xml:

1. Uncomment \<build\_depend>message\_generation\</build\_depend> on line 40
2. Uncomment \<exec\_depend>message\_runtime\</exec\_depend> on line 46

### How to use the newly created message type?

1. How to import the message?

```
from package_name.msg import message_name as message_name
```

If your message type contains a list of buildin message type, also make sure to import that buildin message type:

```
from std_msgs.msg import String
```

2. How to use the message? \<br > The publisher and subscriber's syntax are the same. However, we want to create a new topic name and make sure the new topic message type is specified. For example in my project I have a message type called see\_intruder:

```
self.detect_intruder_pub = rospy.Publisher('/see_intruder', see_intruder, queue_size=1)
self.detect_intruder_sub=rospy.Subscriber('/see_intruder', see_intruder,self.see_intruder_callback)
  
```

Since our new message depends on some build in message type, when we try to access the feild of our msg, we need to do msg.\<field\_name>.data given that the build in message type has a field named data. So in the first example I had, if we want to access the string stored in index int of list, I need to do

```
msg.list[msg.int.data].data
```

### How to check if the new message type is recognized by ROS?

Do a cm in your vnc. if the message is successfully recognized by ROS, you will see the msg file being successfully generated.

### Having an error "No module named <>.msg"?

In your vnc terminal, type the command

```
source ~/catkin_ws/devel/setup.bash
```

This should solve the error.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://campus-rover.gitbook.io/lab-notebook/fiiva/self-defined-message.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
