-
Notifications
You must be signed in to change notification settings - Fork 7
Creating a New Node
This will help you create a new node package, set up any dependencies, and get your node to be launched with the rest of the codebase.
-
Make sure you are inside your Docker container. See Getting Started With Viator (Rover) if you do not know how
-
Change your directory to
/srcFirst determine what directory you are currently in usingpwd(print working directory).Type
ls(list) to determine what files and directories are in your current working directory Typecd <name of folder>(change directory) in order to move into a directory, aka a folderDo this until you reach
/urc-2023/src -
Create a new Python node package Run the following command
ros2 pkg create --build-type ament_python <package_name>I ran
ros2 pkg create --build-type ament_python example_node
-
Make a new Python file under your node’s src location. Call it whatever you want, I called it
example.pyFor me it is under
src/example_node/example_node. Yes, it is a bit funky why the two directories have the same name. ¯_(ツ)_/¯ -
Remove the
test_flake8.pyandtest_pep257.pyfiles under your node’s/test. We have another linter, so these would interfereFor me it was in
src/example_node/test -
Open up the
setup.pyfile. Leave it open for now, we will get to it later
-
You will need to import the ROS Python library, create a class representing your node, create an entry point, and write code to “gracefully-ish” shutdown the node when we press
ctrl-c. Make sure to read the Formatting document to make sure you are writing good readable code.An important detail to remember is the name of your node:
super().__init__("this_is_the_name_of_the_node")
import sys
import rclpy
from rclpy.executors import ExternalShutdownException
from rclpy.node import Node
from lib.color_codes import ColorCodes, colorStr
class ExampleNode(Node):
def __init__(self) -> None:
super().__init__("my_example_node")
self.get_logger().info(colorStr("Launching example_node node", ColorCodes.BLUE_OK))
def main(args: list[str] | None = None) -> None:
rclpy.init(args=args)
try:
example = ExampleNode()
rclpy.spin(example)
except KeyboardInterrupt:
pass
except ExternalShutdownException:
example.get_logger().info(colorStr("Shutting down example_node node", ColorCodes.BLUE_OK))
example.destroy_node()
sys.exit(0)-
Go back to your
setup.pyfile. Look at theconsole_scripts, it should look this right now'console_scripts': [],Add an entry that points towards your
main()function found in your Python file that you added. The syntax is the following'console_scripts': ["<name of executable> = <name of package>.<name of the python file that has the main() function>:main"],For me it would be this:
'console_scripts': ["myExampleNode = example_node.example:main"], -
Open the file
robot.launch.pyunder the folder/src/viator_launch. Create a new variable to store your node. Thepackageparameter should be the same name as your node package name. Theexecutableparameter should be the variable name you chose in thesetup.pyfile. Thenameis the name of your node you wrote in the code in thesuper().__init__()method step 7.For me, I wrote the following.
example_node = Node(package="example_node", executable="myExampleNode", name="my_example_node") -
In the same
robot.launch.pyfile, add it to the array in thegenerate_launch_description()function.def generate_launch_description() -> launch.LaunchDescription: # pylint: disable=invalid-name return launch.LaunchDescription( [can_moteus_node, drivebase_node, mission_control_updater_node, arm_node, example_node] # <- I added it to the end of the array )
-
In the
/src/viator_launchfolder, open up thepackage.xmlfile. Add a new<exec_depend>entry to tell that your package should be included during build. The content is the name of your node package.For me it is this.
<exec_depend>example_node</exec_depend>
- All while in the Docker container, execute the shell scripts
./build.shand./launch.shin the command line. You might have to delete the/buildand/installfolders. - Congrats, you have made a new node ! Yippie! Further examples of how to use ROS timers, subscribers, and publishers are found in the
/src/example_node/example_nodenode; go ahead open it up and read the code.