(I) Robotis CM-900 C++ crash examples tutorial

This entry is part 4 of 5 in the series Programming CM-900

This is the first example in a serie of cheap (absolutely free and cheap) articles to get some basic and practical C++ knowledge. It will references the great explanations provided at www.cplusplus.com C++ tutorial. And using the very cheap and opensource (about 20$/€) Robotis 32 bits microcontroller board CM-900 (STM32 based) and Arduino based IDE.

Here you can download the file with the code, and you can find links to a lot of free tutorials, courses and books to learn C++ here.

// C++ crash tutorial with Robotis CM-900.

// First example: http://softwaresouls.com/softwaresouls/2013/06/23/c-crash-tutorial-using-robotis-cm-900-board-and-ide-i/

/*
Hello World shows messages on construction and destruction
Also it lets you to salute.
Showing always its assigned identifiction number MyId
*/
class HelloWorld
{
  private:
    int myId; //Object identification num ber

  public:

    // class constructor http://www.cplusplus.com/doc/tutorial/classes/
    HelloWorld(char *message, byte id)
    {
      myId=id;
      SerialUSB.print(message);
      printId();
    }

    // class destructor http://www.cplusplus.com/doc/tutorial/classes/
    ~HelloWorld()
    {
      SerialUSB.print ("Destructing object: ");
      printId();
    }

    void printId()
    {
      SerialUSB.print(" ID:");
      SerialUSB.println(myId);
      SerialUSB.println(" ");
    }

    void Salute(char *name)
    {
      SerialUSB.print("Hi!, ");
      SerialUSB.println(name);
      SerialUSB.print("Regards from object: ");
      printId();
    }
};

/*
void setup() function it's only executed one time at the start of the execution.
It is called from a hidden main() function in the Ronbotis CM-900 IDE core.

\ROBOTIS-v0.9.9\hardware\robotis\cores\robotis\main.cpp
Note: "while (1)" is a forevr loop ( http://www.cplusplus.com/doc/tutorial/control ):

(Basic structure http://www.cplusplus.com/doc/tutorial/program_structure/)

int main(void) {
    setup();

    while (1) {
        loop();
    }
    return 0;
}
*/
void setup()
{
  SerialUSB.begin(); //initialize serial USB connection
  delay(3000); //We will wait 3 seconds, let the user open (Control+Shift+M) the Monitor serial console
}

//We will not see neither the construction nor the destruction of this global object because serial port it's not still initiated
HelloWorld hw0("construction object", 0); //Object construction http://www.cplusplus.com/doc/tutorial/classes/

// A counter to see progress and launch events
int iterationCounter=0; //An integer variable to count iterations http://www.cplusplus.com/doc/tutorial/variables

// void loop() will be executing the sentences it contains while CM-900 is on.
void loop()
{
  // Lets's show only the first 5 iterations http://www.cplusplus.com/doc/tutorial/control/
  if (iterationCounter<5)
  {
    SerialUSB.print("starting iteration #");
    SerialUSB.println(++iterationCounter); // firts, iterationCounter is incremented and then printed

    // We will see the consttruction and destruction messages from this local (inside the loop function) object. Object construction http://www.cplusplus.com/doc/tutorial/classes/
    HelloWorld hw1("constructing object", 1);
    hw1.Salute("Joe");

    if (iterationCounter==3)
    {
        // We will see the consttructiona and destruction messages from this local (inside the "if" block inside the "loop" function) object. Objet construction http://www.cplusplus.com/doc/tutorial/classes/
        HelloWorld hw2("constructing object", 2);
        hw2.Salute("Jones");
    } // Objet hw2 destruction http://www.cplusplus.com/doc/tutorial/classes/

    //Let's show that object hw0 is alive
    hw0.Salute("Pepe");

    SerialUSB.print("ending iteration #");
    SerialUSB.println(iterationCounter++); // first cpunter is printed, then incremented.
  } // Objet hw1 destruction http://www.cplusplus.com/doc/tutorial/classes/
} // Program end. Objet hw0 destruction http://www.cplusplus.com/doc/tutorial/classes/
Series Navigation<< Robotis CM-900 in Toss Mode and querying connected sensors(II) C++ crash examples tutorial using Robotis CM-900 board and IDE (II) >>

Comments are closed.

%d bloggers like this: