Beginner Tutorial - Part 2: The minimal program

From NXT++

Jump to: navigation, search

Now, we finally get to actually make something! Although this program does nothing, it will be one of the most important (and hardest) obstacle to overcome. Here is the basic time line of an NXT++ program:

  1. Include NXT++.h
  2. Create a connection
  3. Open the connection
  4. If the open succeeds:
    1. Send commands to the NXT
    2. Close the connection


If we translate these steps into code, and add a *tiny* pinch of magic, we get this:

#include "NXT++.h"
 
int main()
{
    Comm::NXTComm comm;
    if(NXT::Open(&comm)) //initialize the NXT and continue if it succeeds
    {
        //Here's where we add the fun stuff, but we'll hold off for now
        NXT::Close(&comm); //close the NXT
    }
    return 0;
}

This is also a good time to introduce Bluetooth support. Normally you would have to have your robot tethered to the USB port all the time, but with Bluetooth, you can control your robot wirelessly from anywhere within range. The NXT comes with built in Bluetooth support, but in order for your computer to send and receive commands, you'll need to purchase a Bluetooth dongle. If you have a Bluetooth dongle and want to use Bluetooth, all you need to do is enable Bluetooth on the NXT's menu and change NXT::Open(&comm) to NXT::OpenBT(&comm) in your code. Note: it takes about 15 seconds to initiate a connection with the NXT so be patient :).

If that compiles and runs smoothly, it's a sign that everything is set up right. However, if you encounter problems, something is probably wrong with how you compile it. I would suggest asking on the forums for help.

On to Part 3: Let's see something turn

Personal tools