C++ main entry point

From Rsewiki
Revision as of 09:01, 23 January 2024 by Jca (Talk | contribs)

Jump to: navigation, search

Back to Robobot B.

Back to Robobot software description

Contents

Main

C++ starts executing a function called main(int argc, char ** argv)

In Robobot, this is rather simple and the intention is explained here.

The main.cpp file looks like this (slightly reduced):

/*
#***************************************************************************
#*   Copyright (C) 2023 by DTU
#*   jcan@dtu.dk
#*
#* The MIT License (MIT)  https://mit-license.org/
#***************************************************************************/
// System libraries
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <string>
//
// include local files for data values and functions
#include "uservice.h"
#include "cmixer.h"
#include "sgpiod.h"
#include "bplan20.h"
#include "bplan40.h"

int main (int argc, char **argv)
{ // prepare all modules and start data flow
  bool setupOK = service.setup(argc, argv);
  if (setupOK)
  { // turn on LED on port 16
    gpio.setPin(16, 1);
    // run the planned missions in this sequence
    // example odometry drive using distance and turned angle
    plan20.run();
    // example odometry drive using distance and turned angle
    plan40.run();
    //
    mixer.setVelocity(0.0);
    mixer.setTurnrate(0.0);
    sleep(1); // to allow robot to stop while logging is running
    // turn off led 16
    gpio.setPin(16, 0);
  }
  // close all logfiles
  service.terminate();
}

The main() function

The main function handles:

  • setup all modules by calling setup(...) in the service module.
  • run the mission (or missions) needed.
  • stop and terminate properly; the service module handles the termination details.

Run mission

The entire mission can be divided into smaller parts that can be tested individually.

One example could be plan20 in the example above in the main.cpp file.

int main(...)
{
   ...
   plan20.run();
   ...
}

The behaviour plan itself (in bplan20.cpp) tests whether the plan should be active.

void BPlan20::run()
{
  if (not setupDone)
    setup();
  if (ini["plan20"]["run"] == "false")
    return;
 bool finished = false;
 bool lost = false;
 ...
}


The ini["plan20"]["run"] refers to the configuration file.

The plan can be activated by setting the run flag to "true" in the robot.ini configuration file.

; part of robot.ini file
[plan20]
log = true
run = true
print = true

The sequence of the entire mission is handled in this main function. In this case, the mission consists of plan20 and plan40.

You are, of course, allowed to change whatever you like, especially if you find more innovative ways to do it. The main objective has been to make it readable, understandable and then functional.

Copyright

All software, specific for Robobot, is free to use and change, according to the MIT License, one of the least restrictive copyright types.

Include files

The compiler needs to know where these functions are defined to call functions. C++ handles this by including the needed files before compiling.

Where to look for these files is indicated by the brackets, like

#include <string>
#include "bplan20.h"

The first file included is string in <..> brackets, indicating that this is a system library, and the path for such libraries is part of the compile parameters.

The second file bplan20.h is in ".." bracket, indicating that the file is in the same directory as the file that includes the file.

Personal tools
Namespaces

Variants
Actions
Navigation
Toolbox