September 8, 2016

The Circle Tutorial

Hello Space

Typically the first program one learns to write in a new system is the classic “Hello World” program, but since we all hope to run our programs on the International Space Station we’ll go with “Hello Space!” By now you should be able to sign into the Zero Robotics website.

Start a new program:

  • Sign in to the ZR website.
  • Click on the “ZR IDE” icon at the top of the page which should take you to the “My Projects” list.
  • Click on the “New Project” button (top right) which should open the new project dialog box.
  • Enter “hellospace” for the Project Name, select FreeMode for the Game and leave the editor set to “Text Editor.”
  • Click the “Submit” button.

You should now see the following code:


//Declare any variables shared between functions here
 
void init(){
    //This function is called once when your code is first loaded.
 
    //IMPORTANT: make sure to set any variables that need an initial value.
    //Do not assume variables will be set to 0 automatically!
}
 
void loop(){
    //This function is called once per second.  Use it to control the satellite.
}

Pages

On the left side of the screen you will see a list of pages in your project. Pages are used to organize code. You can choose to write your entire program in one page or you can split your program into multiple pages. In shared projects, pages are at the heart of the ZR IDE version control system. For more on this and the rest of the IDE features you can see the ZR IDE documentation.

Back to the code…

You are looking at the two main functions used by the ZR system to communicate with your program. In fact this is not a program, it is part of a C++ class. Your code will be inserted into a template that will complete the class. You don’t need to concern yourself too much with this. It is important to realize your code will be running as part of a larger program that will define much of the functionality and that that program will call the init() function and the loop() function to run your code.

init()

Variables that are defined outside of any functions are class members and will be visible throughout the class. In order to initialize these variables the ZR system will call init() at the beginning of each match or simulation. It is very important to set the value for variables that require an initial value before your program begins. Any variable you don’t initialize will be filled with unpredictable values. 

loop()

The loop() function is where the good stuff happens. This function is called once every second of the match. It is here where you will check the state of the universe, make your decisions and instruct the satellite what to do.

The first line…

In order to create your first Hello Space program, you should add the following line of code inside the loop() function.


DEBUG(("\nHello Space!"));

It should look like this: 

void loop(){
    //This function is called once per second.  Use it to control the satellite.
    DEBUG(("\nHello Space!"));
}

DEBUG is a macro that will send formatted messages to the Debug Console in the simulation viewer. DEBUG works just like the C printf() function.

To run the program do the following:

  1. Click the Run icon (a square with a right arrow head – like a play button.)
  2. In the Simulation Settings dialog box select “Create new …” for Load Settings and enter a name in the box that appears to the right.
  3. Click “Run” and your simulation will run.
  4. When complete the Simulation Complete dialog box will pop-up.
  5. Click the “View Simulation” button and you should see the simulation play field.
  6. Click the “Play” button.

You should see your simulation run and the DEBUG messages that your program is writing will show up in the Debug window at the bottom center of the screen. You may notice the comments print out in “chunks” not one per second. This is normal. Re-run the simulation as many times as you want by clicking the “Play” button again.

When you are done running the simulation, close the simulation window and click the “Close” button on the Simulation Complete dialog box. If you run another simulation in the same session, you won’t have to create simulation settings each time. The IDE will assume you want to use the “Last simulated settings” or you can create additional settings or pick any saved settings.

Congratulations!

Hopefully that went smoothly and you’ve created and run your first ZR program. Now we’ll walk through the basics of SPHERES programming.