A Guide to Robot Programming

A Guide to Robot Programming

Hello, RoboProgrammers!

Two weeks ago, we defined and talked about how excellent robot programming is. Last week, we revealed that Python was our coding language of choice. But how does one get their code from their script editor into RoboCo? Well, that’s what today’s devblog is all about. Here’s a step-by-step guide to how to program your robot in RoboCo!

Step 1: Grab a Microcontroller

To open the robot programming main menu, you first need to add a microcontroller to your robot. A brand new part, the microcontroller is the “brain” of your robot. You can find this part in the Powered section of the Parts inventory. 

Now that you’ve added the microcontroller to your robot go to the Controls interface and select the part. That’ll bring up a menu like the one below:

RoboCo Microcontroller Interface

This is essentially the main menu for robot programming in RoboCo and where you’ll load and attach your code. 

Step 2: Write a New Script or Load a Previous Script

Now that you’ve attached your microcontroller, we can go ahead and start adding in the code itself. To start, you have a couple of options. 

Option 1: Add New Script

If you haven’t written code for your robot already, you can create a new script. You’ll have the option to name your code and then save it to the “Script Folder.” The Script Folder is a new file management location separate from your robots folder where all your scripts for RoboCo live, thus making it super easy to reuse code across multiple robots. 

Example Scripts Folder

The easiest way to access it is by clicking the “Open Script Folder” button in the microcontroller interface. You can also access it by going into File Explorer and following the path: This PC → Documents → my games → RoboCo → [Your Steam Username] → Scripts. 

There are a variety of apps you can use to write this code. We recommend Visual Studio Code, but you can also use other applications like Notepad ++ and Sublime. If you decide to use Visual Studio Code, we recommend downloading the Python extension with it. 

Example of Code within Visual Studio Code

You’ll use a custom API to talk to your robot, like asking a sensor what it detected or telling a motor to spin. To learn how to do this, you can access the API documentation by clicking the “API” button in the microcontroller interface. 

RoboCo API Documentation

Option 2: Load a Script

If you have already written code for your robot, you can load the code directly into RoboCo by clicking the “Load Button.” That’ll take you straight into your Script Folder, where you will select the desired code. 

Step 3: Assign Your Ports

Once you’ve written your new code or loaded in your existing code, your interface should look like this: 

We included a “Script Preview” so that you could easily check that your script matches what you expect after you load it in. When you edit and save a script in your external script editor, RoboCo will detect that the file changed and automatically update with the changes. This allows for quick iteration if you have RoboCo on one monitor and your script editor on another.

For your robot to function, you have to map and assign your ports to the necessary parts. These could include any controllable parts (motors, pistons, LED panels, lightbulbs, etc.) or our brand new sensors, which we will cover in a future dev blog! 

You can either choose to map these parts automatically using the “Auto Assign” button or map them manually, like in the example below. This option is beneficial if you have multiple microcontrollers or if you want to limit parts to specific actions. You can do this by either selecting the part in the Controls Interface or by clicking on the part with the Select Tool on your robot. 

Step 3.5: Check For Errors

In addition to a script preview, we also have an in-game debug message console! When you hit play, the game will begin executing code. If an error occurs while playing, a notification will appear. You’ll then be able to open up the Debug Console, allowing you to see the full error message as well as a history of errors.

The Debug Console also lets you filter to errors or print statements, clear the history, or set the game to Pause on Error. You can also copy the error and paste it into your script editor to investigate there. We hope it will make diagnosing problems and getting back to playing with your robot much easier!

Step 4: Let Your Robot Do Its Thing!

And after all that’s said and done, it’s time for your robot to do its thing!

When you save your robot, you’ll notice a little icon next to it like the one pictured below. 

This is meant to show that it comes with Python coding. When you share your robot via Steam Workshop, it will be accompanied with any associated code. Not only will this allow players who have trouble with coding to experience full-blown robot programming, but it will also allow players to build on each other’s code, eventually leading to super-robots that can solve any challenge thrown at them (or that’s the dream anyway). 

And that’s it for this week’s devblog! Was this a helpful guide? Are you excited about adding your Python code below? Let us know on social media and Discord! Next week, we’re premiering the first episode of Check Out Our Robots, so stay tuned for all the video-goodness!

For the latest news on RoboCo, follow us on Twitter, YouTube, Facebook, Instagram, and Tik Tok! You can also connect with other community members and us by joining our official Discord and Reddit

Don’t forget to add RoboCo to your Steam Wishlist!