Programming Virtual "Robot" - VTAstrobotics/Documentation GitHub Wiki

Setup

Cloning Repository

  • In this onboarding exercise, you will program a virtual robot.
  • To get started, open up VSCode, and clone the repository found at https://github.com/VTAstrobotics/VirtualRobotOnboarding/tree/main.
  • To clone a repository in VSCode, click Clone Git Repository when the application starts, and then paste the following linke https://github.com/VTAstrobotics/VirtualRobotOnboarding.git.
    • You can also clone the repository using git CLI if you prefer (git clone <link>).
image

Creating Your Own Branch

  • Once you have cloned the repository and are inside of the repository folder in VSCode, click on the "Source Control" tab on the left of your screen (icon below)
image
  • On the bottom of the screen, you should see text that says main. Click on that, and a drop down menu should appear from the top of your screen. Click Create new branch from, and then select main. You will be prompted to enter a name. Enter something relating to your name (e.g. ryan_robot), and press enter.
  • On the source control screen on the left, there should be a blue button titled Publish Branch. Click this, and navigate back to the repository on a browser (https://github.com/VTAstrobotics/VirtualRobotOnboarding/tree/main). Click branches. You should see your new branch listed! This allows you to develop code without affecting the main branch. You may have to sign in if prompted.
  • In our team, the main branch should always be the base robot code that we know works. Whenever we are adding a feature, we create a branch and name the branch after the feature we are adding. We can then take the code from the new branch and move it into main if the feature is successful. It is good practice to follow this convention. Don't push straight to main.

Opening the Repository in a Docker Container

Assuming you installed Docker and the Dev Containers extension listed in Software Setup, this should be pretty easy. Press ctrl+shift+p, to bring up the command dropdown. Select Dev Containers: Rebuild and Reopen in Container. Your container should start running (on the first run this can take a while).

Programming

Where am I coding?

  • On the Explorer tab, click on src/subscriber/src/motor_node.cpp. This is the main file you will be editing.
image

What exactly is going on in this repository?

  • This repository has two main nodes, one publishing velocity commands to a topic, and one subscribing to that topic and distributing motor commands to left and right motors. Both nodes are written in C++.
  • This is meant to replicate how a node might operate in a real robot (reading information from a topic, doing math, and publishing new data to another topic).
  • The velocity command publisher has already been created.
  • The code in motor_node_cpp.cpp is meant to subscribe to the topic published by the velocity command publisher, do math, and publish new data to two new topics. If you need a review on publishers and subscribers, and their syntax, head to https://docs.ros.org/en/humble/Tutorials/Beginner-Client-Libraries/Writing-A-Simple-Cpp-Publisher-And-Subscriber.html.
    • Further instructions are actually written in motor_node_cpp.cpp.
  • Here is a diagram of what is supposed to happen after you are finished coding. You are responsible for coding the motor controller node.
image - blue denotes a ROS2 node, and green represents a topic published or subscribed to by a node.

I wrote a solution, how do I test it?

  • navigate to the ros2_ws folder (cd ~/ros2_ws in your VSCode terminal)
  • Enter colcon build into the terminal to build your packages. You should see text showing your build progress.
    • if your build fails, look at the stack trace. Try to figure out why it failed. If you are stuck, contact me (Ryan)
  • If your build is successful, source the workspace (enter source install/setup.bash).
  • you can now use the launch file in the repository. Type ros2 launch start_robot launch.launch.py to launch the velocity command node and the node you wrote.

How can I check if my solution is working?

  • after launching your file, open a new terminal by clicking the "+" in the right hand corner of the terminal (WHILE THE NODES YOU LAUNCHED ARE STILL RUNNING:
image - type `ros2 topic list`. This lists all the topics being used by the currently running nodes. You should see `/cmd_vel`, along with `/right_drive' and '/left_drive' if you implemented your solution correctly. - to actually see the data being published, type `ros2 topic echo ` where `` is the topic name you want to follow. You can do this to check if your node is publishing data as expected. - Another useful command is `ros2 topic info `, which lets you see how many subscribers and publishers a node has.

My solution worked! Now what do I do?

  • Now, you should push your changes to your branch. This allows you to share your code with your team members, and in a real setting would allow your code to be merged into main so it can run on the robot.
  • First, reopen the folder locally by hitting ctrl+shift+p, and selecting Dev Containers: Reopen Folder Locally
  • Once this finishes, click back on the source control tab on the right. Make sure your branch is selected on the bottom instead of main
  • Type a commit message in the messages space, and click the blue commit button.
  • Click on the Sync Changes button. This pushes the code you wrote to our online repository.

Resources

Errors

  • If you find any errors or missed steps in this tutorial, please message me (software lead) and I will fix them. If the error is something small like a typo on this page, feel free to edit it yourself.
⚠️ **GitHub.com Fallback** ⚠️