nao机器人学习——hello world(1)

all code here is from Aldebaran documentation

link: http://doc.aldebaran.com/2-1/dev/cpp/tutos/create_module.html#patching-main-cpp

After finish installing C++ SDK, writing code is provided in documentation.

create a folder as project folder, then use command

qisrc create mymodule
in cmd

four files will be created

//these four files is what I created
CMakeLists.txt
main.cpp
qiproject.xml
test.cpp

//these ones are in website
CMakeLists.txt
main.cpp
qibuild.cmake
qibuild.manifest

1. change CMakeLists.txt

original file here

cmake_minimum_required(VERSION 2.8)

# Give a name to the project.
project(mymodule)

# You need this to find the qiBuild CMake framework
include("qibuild.cmake")

# Create a executable named mymodule
# with the source file: main.cpp
qi_create_bin(mymodule "main.cpp")

add things here

qi_use_lib(  )

finished here

cmake_minimum_required(VERSION 2.8)

# Give a name to the project.
project(mymodule)

# You need this to find the qiBuild CMake framework
find_package(qibuild)

# Create a executable named myproject
# with the source file: main.cpp
qi_create_bin(myproject "main.cpp")

# Tell CMake that myproject depends on ALCOMMON and ALPROXIES
# This will set the libraries to link myproject with,
# the include paths, and so on
qi_use_lib(myproject ALCOMMON ALPROXIES)

2. chage in main.cpp

for a executable program, arguments is needed of ip and port, the following function mainly deal with that

finished code

#include 
#include 

#include 
#include 

int main(int argc, char* argv[])
{
  // We will try to connect our broker to a running NAOqi
  int pport = 9559;
  std::string pip = "127.0.0.1";

  // command line parse option
  // check the number of arguments
  if (argc != 1 && argc != 3 && argc != 5)
  {
    std::cerr << "Wrong number of arguments!" << std::endl;
    std::cerr << "Usage: mymodule [--pip robot_ip] [--pport port]" << std::endl;
    exit(2);
  }

  // if there is only one argument it should be IP or PORT
  if (argc == 3)
  {
    if (std::string(argv[1]) == "--pip")
      pip = argv[2];
    else if (std::string(argv[1]) == "--pport")
      pport = atoi(argv[2]);
    else
    {
      std::cerr << "Wrong number of arguments!" << std::endl;
      std::cerr << "Usage: mymodule [--pip robot_ip] [--pport port]" << std::endl;
      exit(2);
    }
  }

  // Sepcified IP or PORT for the connection
  if (argc == 5)
  {
    if (std::string(argv[1]) == "--pport"
        && std::string(argv[3]) == "--pip")
    {
      pport = atoi(argv[2]);
      pip = argv[4];
    }
    else if (std::string(argv[3]) == "--pport"
             && std::string(argv[1]) == "--pip")
    {
      pport = atoi(argv[4]);
      pip = argv[2];
    }
    else
    {
      std::cerr << "Wrong number of arguments!" << std::endl;
      std::cerr << "Usage: mymodule [--pip robot_ip] [--pport port]" << std::endl;
      exit(2);
    }
  }

  // A broker needs a name, an IP and a port to listen:
  const std::string brokerName = "mybroker";

  // Create your own broker
  boost::shared_ptr broker =
    AL::ALBroker::createBroker(brokerName, "0.0.0.0", 54000, pip, pport);

  /**
   * Create a proxy to a module in NAOqi process so that we can call
   * the bind method of this module
   * AL::ALProxy proxy(, );
   */
  // Create a proxy to ALTextToSpeechProxy
  AL::ALProxy proxy(broker, "ALTextToSpeech");

  /**
   * If the bind methode is a void return
   * you can call bind methode using callVoid
   * proxy.callVoid(, , ...)
   */
  // Call say methode
  proxy.callVoid("say", std::string("Sentence to say!"));

  /**
   * Otherwise you can use template call methode
   * type res = proxy.call(, , ...);
   */
  // Call ping function that return a boolean
  bool res = proxy.call("ping");

  return 0;
}
now the NAOqi can be connected.

你可能感兴趣的:(nao,机器人,NAO)