Coding: Difference between revisions

From RoboCo
Jump to navigation Jump to search
Tag: Reverted
m (Protected "Coding" ([Edit=Allow only administrators] (indefinite) [Move=Allow only administrators] (indefinite)))
 
(33 intermediate revisions by 5 users not shown)
Line 1: Line 1:
[[File:Microntroller Code Preview Example.png|thumb|500px|Microcontroller Interface]]
[[File:Microntroller Code Preview Example.png|thumb|500px|Microcontroller Interface]]


==Getting Started==
==Getting Started==


Programming is challenging, especially when you don't know where to start. For those without access to programming classes or extracurriculars via their learning institution, we recommend [https://www.codecademy.com/learn/learn-python?utm_source=pepperjam&utm_medium=affiliate&utm_term=215171&clickId=4023389168&pj_creativeid=8-12462&pj_publisherid=215171 CodeAcademy's Learning Python]. This course is a great way to learn the fundamentals of Python AND is fully virtual, so you can take the class wherever or whenever you want!
Programming is complex, especially when you don't know where to start. For those without access to programming classes or extracurriculars via their learning institution, RoboCo recommends [https://www.codecademy.com/learn/learn-python?utm_source=pepperjam&utm_medium=affiliate&utm_term=215171&clickId=4023389168&pj_creativeid=8-12462&pj_publisherid=215171 CodeAcademy's Learning Python]. This course is a great way to learn the fundamentals of Python AND is fully virtual, so players can take classes whenever they want


In addition to learning the language, you'll also need a program to code in. We recommend [https://code.visualstudio.com/ Visual Studio Code] and its accompanying [https://marketplace.visualstudio.com/items?itemName=ms-python.python Python extension]. This is actually the program we use write code for RoboCo and it comes with a variety of different customization options and extensions to fit all your coding needs (including Java and C+)! You can also try [https://notepad-plus-plus.org/downloads/ Notepad++] and [https://www.sublimetext.com/ Sublime] if Visual Studio Code doesn't interest you.  
In addition to learning the language, players will also need a program to code in. RoboCo recommends [https://code.visualstudio.com/ Visual Studio Code] and its accompanying [https://marketplace.visualstudio.com/items?itemName=ms-python.python Python extension]. This is the program RoboCo developers use to write code for RoboCo. It comes with a variety of different customization options and extensions to fit all your coding needs (including Java and C++). You can also try [https://notepad-plus-plus.org/downloads/ Notepad++] and [https://www.sublimetext.com/ Sublime] if Visual Studio Code is of little interest or isn't available.  


Additionally, RoboCo requires players to use its custom API Documentation. This is the way in which players "talk" to their robots and includes instructions for how to program the different sensors and other controllable parts within the game. You can access the API [https://docs.roboco.co/demo-june22/ here] or by clicking the API button in the Microcontroller interface within RoboCo.
Additionally, RoboCo requires players to use its custom API Documentation. This is the way in which players "talk" to their robots and includes instructions for how to program the different sensors and other controllable parts within the game. Players can access the API [https://docs.roboco.co/latest/ here] or by clicking the API button in the Microcontroller interface within RoboCo.


==Current Available Sensors==
[[#top|Back to Top]]


Sensors are programmable parts that allow a robot to interpret the physical world into processable data. You could think of them as the robot equivalent to our eyes or ears. The robot can be instructed via code to take actions based on the sensor data it receives, which is what allows a robot to complete tasks on its own (like delivering a sandwich across a crowded restaurant or launching a soda can across a room). 
==⚙ Current Available Sensors==


Because there are so many different tasks and environments to tackle in RoboCo, we wanted to start with a wide variety of sensors! Below is a list of all the sensors currently available in the game. Additionally, if you are looking for other controllable parts that can be used with Python, see our [[Controls]] page!
Sensors are programmable parts that allow a robot to interpret the physical world into processable data. Think of them as the robot equivalent to a human's eyes or ears. The robot can be instructed via code to take actions based on the sensor data it receives, which is what allows a robot to complete tasks on its own (like delivering a sandwich across a crowded restaurant or launching a soda can across a room). 
 
Below is a list of all the sensors currently available in the game. Other parts that can be controlled with Python can be found on the [[Controls]] page.


{| class="wikitable"
{| class="wikitable"
Line 23: Line 25:
|Touch Sensor
|Touch Sensor
|[[File:Touch Sensor Icon 2.png|50px]]
|[[File:Touch Sensor Icon 2.png|50px]]
|A part that sense touch input
|A part that sense touch input.
|-
|-
|Force Sensor
|style="background-color:#ECECEC;" | Force Sensor
|[[File:Force Sensor.png|50px]]
|style="background-color:#ECECEC;" | [[File:Force Sensor.png|50px]]
|A part that senses the forces applied to it along 6 axes
|style="background-color:#ECECEC;" | A part that senses the forces applied to it along 6 axes.
|-
|-
|Inertial Sensor / IMU
|Inertial Sensor / IMU
|[[File:Inertial Motion Sensor Icon.png|50px]]
|[[File:Inertial Motion Sensor Icon.png|50px]]
|A sensor for measuring acceleration, rotation, and angular velocity
|A sensor for measuring acceleration, rotation, and angular velocity.
|-
|-
|Distance Sensor
|style="background-color:#ECECEC;" | Distance Sensor
|[[File:Distance Sensor.png|50px]]
|style="background-color:#ECECEC;" | [[File:Distance Sensor.png|50px]]
|A part that senses the distance to the first object it encounters
|style="background-color:#ECECEC;" | A part that senses the distance to the first object it encounters.
|-
|-
|Color Sensor
|Color Sensor
|[[File:Color Sensor Icon.png|50px]]
|[[File:Color Sensor Icon.png|50px]]
|A camera that senses the color in front of it
|A camera that senses the color in front of it.
|-
|-
|}
|}


==How to Load a New or Completed Script==
[[#top|Back to Top]]
 
==How to Load a New or Completed Script==
 
[[File:Microntroller Code Preview Example.png|thumb|350px|RoboCo Code Preview]]


[[File:Microntroller Code Preview Example.png|thumb|400px|RoboCo Code Preview]]
The first step to accessing the programming interface in RoboCo is attaching the microcontroller to a robot. This is the "brain" of a robot, and this can be found in the Powered section of the [[Parts]] inventory.


The first step to accessing the programming interface in RoboCo is attaching the microcontroller to your robot. This is the "brain" of your robot, and this can be found in the Powered section of the Parts inventory.
Once players have added a microcontroller to their robot, they can either hit the "New" button to create a new script or the "Load" button to upload a previous or already completed script from their desktop. Currently, all scripts for RoboCo live in a folder called "Scripts" which players can access via the "Open Script Folder" button or by going to This PC --> Documents --> my games --> RoboCo --> (Your Steam Username) --> Scripts.


Once you've added the microcontroller to your robot you can either hit the "New" button to create a new script or the "Load" button to upload a previous or already completed script from your desktop. Currently, all your scripts for RoboCo live in a folder called "Scripts" which you can access via the "Open Script Folder" button or by going to This PC --> Documents --> my games --> RoboCo --> (Your Steam Username) --> Scripts.
Once players have loaded in their code correctly, they'll see a preview of it in the microcontroller interface.


Once you've loaded in your code correctly, you should see a preview of it in the interface!
[[#top|Back to Top]]


==How to Assign Ports==
==How to Assign Ports==


RoboCo has two options when assigning ports: manual or automatic. If you want to automatically map your ports, simply click the auto-assign button in the Microcontroller interface. If you want to manually map your ports, follow the instructions below:
RoboCo has two options when assigning ports: manual or automatic. If players want to automatically map their ports, simply click the auto-assign button in the microcontroller interface. If they want to manually map their ports, follow the instructions below:


{| class="wikitable"
{| class="wikitable"
Line 64: Line 70:
|-
|-
|Step 1
|Step 1
|Add the microcontroller to your robot to access the script interface
|Add the microcontroller to a robot to access the script interface.
|[[File:Select the MicroController Example.png|400px]]
|[[File:Select the MicroController Example.png|400px]]
|-
|-
|Step 2
|style="background-color:#ECECEC;" | Step 2
|Choose to either create a new script or load an already-created script
|style="background-color:#ECECEC;" | Choose to either create a new script or load an already-created script.
|[[File:Loading a Previous Script Example.png|400px]]
|style="background-color:#ECECEC;" | [[File:Loading a Previous Script Example.png|400px]]
|-
|-
|Step 3
|Step 3
|Assign your parts to ports manually or use the “automatically assign” button
|Assign parts to ports manually or use the “automatically assign” button.
|[[File:Assinging Ports Example.png|400px]]
|[[File:Assinging Ports Example.png|400px]]
|-
|-
|Step 4
|style="background-color:#ECECEC;" | Step 4
|Press the play button (or spacebar) to run your code. If an error occurs, use the in-game debug console to help troubleshoot.
|style="background-color:#ECECEC;" | Press the play button (or spacebar) to run the code. If an error occurs, use the in-game debug console to help troubleshoot.
|[[File:RoboCo Debug Console Example.png|400px]]
|style="background-color:#ECECEC;" | [[File:RoboCo Debug Console Example.png|400px]]
|-
|-
|}
|}


==Troubleshooting==
[[#top|Back to Top]]
 
==⚙ Importing External Scripts==
 
The RoboCo Python Interpreter searches a few places for libraries by default. The main location is <your Steam directory>\steamapps\common\RoboCo\RoboCo_Data\StreamingAssets\Robocode\python\Lib. The game's code libraries are stored here.
 
Adding a file to the Lib directory will allow you to import it from any script in your robots, however, the Lib folder could get reset in the future, and it's inconvenient to set up libraries in the folder over the Scripts folder where the scripts for all of your robots (including ones from the workshop) are stored.
 
The Scripts folder is located far within the user's Documents folder and is not searched for libraries by default. The following code segment adds the Scripts folder to the list of folders to look for libraries in.
 
<pre>
import os, sys
path = str(os.path.expanduser('~'))+'\\Documents\\My Games\\Roboco'
if not os.path.isdir(path): path = str(os.path.expanduser('~'))+'\\My Documents\\My Games\\Roboco'
if os.path.isdir(path):
    for file_path in os.listdir(path):
        for folder in os.listdir(os.path.join(path, file_path)):
            f_path = os.path.join(path, file_path, folder)
            if(os.path.isdir(f_path) and folder == 'Scripts'):
                if not (f_path in sys.path): sys.path.append(f_path)
else:
    print('*Error finding Roboco folder*')
</pre>
 
After the path is added, the files within the Scripts folder can be imported like any other file would be.
 
Importing a script from another known directory is simple, too.
 
For example, on my computer (your steam directory might be different), this code will allow me import a file from the RoboCo game folder.
<pre>
import sys
sys.path.append('C:\Program Files (x86)\Steam\steamapps\common\RoboCo')
</pre>
 
[[#top|Back to Top]]
 
==⚙ Troubleshooting==


RoboCo has a debug console built into its coding interface that allows players to see what lines of code are causing errors. This should be your first step in helping troubleshoot Python code.  
RoboCo has a debug console built into its coding interface that allows players to see what lines of code are causing errors. This should be the first step in helping troubleshoot Python code.  


If you encounter a more complicated situation, we also have a #coding-and-automation-help-desk channel in the [https://discord.gg/mS8zUKMkwV Official RoboCo Discord] to help players troubleshoot more complicated problems. Once you've submitted a question to the channel, one of our developers or equally knowledgeable Discord members should be available to help you with your code.  
If players encounter a more complicated situation, there is a #coding-and-automation-help-desk channel in the [https://discord.gg/mS8zUKMkwV Official RoboCo Discord], which is meant to help troubleshoot more complicated behaviors. Once players have submitted a question to the channel, a member of RoboCo's dev team or an equally knowledgeable Discord member should respond with guidance on how to solve the problem.


Keep in mind, developers generally work in the US Central Time Zone so while staff may not be able to get to your questions right away, they will do their best to answer you within the first 24 hours.
[[#top|Back to Top]]

Latest revision as of 20:59, 19 July 2023

Microcontroller Interface

⚙ Getting Started

Programming is complex, especially when you don't know where to start. For those without access to programming classes or extracurriculars via their learning institution, RoboCo recommends CodeAcademy's Learning Python. This course is a great way to learn the fundamentals of Python AND is fully virtual, so players can take classes whenever they want.

In addition to learning the language, players will also need a program to code in. RoboCo recommends Visual Studio Code and its accompanying Python extension. This is the program RoboCo developers use to write code for RoboCo. It comes with a variety of different customization options and extensions to fit all your coding needs (including Java and C++). You can also try Notepad++ and Sublime if Visual Studio Code is of little interest or isn't available.

Additionally, RoboCo requires players to use its custom API Documentation. This is the way in which players "talk" to their robots and includes instructions for how to program the different sensors and other controllable parts within the game. Players can access the API here or by clicking the API button in the Microcontroller interface within RoboCo.

Back to Top

⚙ Current Available Sensors

Sensors are programmable parts that allow a robot to interpret the physical world into processable data. Think of them as the robot equivalent to a human's eyes or ears. The robot can be instructed via code to take actions based on the sensor data it receives, which is what allows a robot to complete tasks on its own (like delivering a sandwich across a crowded restaurant or launching a soda can across a room).

Below is a list of all the sensors currently available in the game. Other parts that can be controlled with Python can be found on the Controls page.

Name Icon Description
Touch Sensor Touch Sensor Icon 2.png A part that sense touch input.
Force Sensor Force Sensor.png A part that senses the forces applied to it along 6 axes.
Inertial Sensor / IMU Inertial Motion Sensor Icon.png A sensor for measuring acceleration, rotation, and angular velocity.
Distance Sensor Distance Sensor.png A part that senses the distance to the first object it encounters.
Color Sensor Color Sensor Icon.png A camera that senses the color in front of it.

Back to Top

⚙ How to Load a New or Completed Script

RoboCo Code Preview

The first step to accessing the programming interface in RoboCo is attaching the microcontroller to a robot. This is the "brain" of a robot, and this can be found in the Powered section of the Parts inventory.

Once players have added a microcontroller to their robot, they can either hit the "New" button to create a new script or the "Load" button to upload a previous or already completed script from their desktop. Currently, all scripts for RoboCo live in a folder called "Scripts" which players can access via the "Open Script Folder" button or by going to This PC --> Documents --> my games --> RoboCo --> (Your Steam Username) --> Scripts.

Once players have loaded in their code correctly, they'll see a preview of it in the microcontroller interface.

Back to Top

⚙ How to Assign Ports

RoboCo has two options when assigning ports: manual or automatic. If players want to automatically map their ports, simply click the auto-assign button in the microcontroller interface. If they want to manually map their ports, follow the instructions below:

Step Description Example
Step 1 Add the microcontroller to a robot to access the script interface. Select the MicroController Example.png
Step 2 Choose to either create a new script or load an already-created script. Loading a Previous Script Example.png
Step 3 Assign parts to ports manually or use the “automatically assign” button. Assinging Ports Example.png
Step 4 Press the play button (or spacebar) to run the code. If an error occurs, use the in-game debug console to help troubleshoot. RoboCo Debug Console Example.png

Back to Top

⚙ Importing External Scripts

The RoboCo Python Interpreter searches a few places for libraries by default. The main location is <your Steam directory>\steamapps\common\RoboCo\RoboCo_Data\StreamingAssets\Robocode\python\Lib. The game's code libraries are stored here.

Adding a file to the Lib directory will allow you to import it from any script in your robots, however, the Lib folder could get reset in the future, and it's inconvenient to set up libraries in the folder over the Scripts folder where the scripts for all of your robots (including ones from the workshop) are stored.

The Scripts folder is located far within the user's Documents folder and is not searched for libraries by default. The following code segment adds the Scripts folder to the list of folders to look for libraries in.

import os, sys
path = str(os.path.expanduser('~'))+'\\Documents\\My Games\\Roboco'
if not os.path.isdir(path): path = str(os.path.expanduser('~'))+'\\My Documents\\My Games\\Roboco'
if os.path.isdir(path):
    for file_path in os.listdir(path):
        for folder in os.listdir(os.path.join(path, file_path)):
            f_path = os.path.join(path, file_path, folder)
            if(os.path.isdir(f_path) and folder == 'Scripts'):
                if not (f_path in sys.path): sys.path.append(f_path)
else:
    print('*Error finding Roboco folder*')

After the path is added, the files within the Scripts folder can be imported like any other file would be.

Importing a script from another known directory is simple, too.

For example, on my computer (your steam directory might be different), this code will allow me import a file from the RoboCo game folder.

import sys
sys.path.append('C:\Program Files (x86)\Steam\steamapps\common\RoboCo')

Back to Top

⚙ Troubleshooting

RoboCo has a debug console built into its coding interface that allows players to see what lines of code are causing errors. This should be the first step in helping troubleshoot Python code.

If players encounter a more complicated situation, there is a #coding-and-automation-help-desk channel in the Official RoboCo Discord, which is meant to help troubleshoot more complicated behaviors. Once players have submitted a question to the channel, a member of RoboCo's dev team or an equally knowledgeable Discord member should respond with guidance on how to solve the problem.

Back to Top