Robobot software building blocks
Line 75: | Line 75: | ||
#include "cmixer.h" | #include "cmixer.h" | ||
#include "bplan1.h" | #include "bplan1.h" | ||
− | |||
// create class instance | // create class instance | ||
BPlan1 plan1; | BPlan1 plan1; | ||
− | |||
void BPlan1::setup() | void BPlan1::setup() | ||
{ // ensure there is default values in ini-file | { // ensure there is default values in ini-file | ||
Line 100: | Line 98: | ||
} | } | ||
setupDone = true; | setupDone = true; | ||
− | } | + | } |
− | + | BPlan1::~BPlan1() | |
− | BPlan1::~BPlan1() | + | { |
− | { | + | |
terminate(); | terminate(); | ||
− | } | + | } |
− | + | ||
− | void BPlan1::run() | + | void BPlan1::run() |
− | { | + | { |
if (not setupDone) setup(); | if (not setupDone) setup(); | ||
toLog("Plan1 started"); | toLog("Plan1 started"); | ||
Line 130: | Line 127: | ||
sleep(1); | sleep(1); | ||
toLog("Plan1 finished"); | toLog("Plan1 finished"); | ||
− | } | + | } |
− | + | ||
− | + | void BPlan1::terminate() | |
− | void BPlan1::terminate() | + | { |
− | { | + | |
fclose(logfile); | fclose(logfile); | ||
− | } | + | } |
− | + | ||
void BPlan1::toLog(const char* message) | void BPlan1::toLog(const char* message) | ||
{ | { | ||
Line 152: | Line 148: | ||
} | } | ||
− | #include files are library definitions if in <math.h> brackets or local definitions if in "usupport.h" quotes. | + | the ''#include'' files are library definitions if in <math.h> brackets or local definitions if in "usupport.h" quotes. |
The next line | The next line |
Revision as of 16:23, 29 December 2023
Back to Robobot.
Back to Robobot software description
Contents |
C++ code
The Raubase software is built in modules with mostly only one function, structured from the 'NASREM' architecture.
Figure. The NASREM model is shown on the top right. This figure is for level 3 and primarily shows the behaviour and vision-related blocks.
File names
Each module has a header-file with the definition (e.g. bplan.h) of a class and a file with implementation (e.g. bplan.cpp).
The first letter in the filename is related to the NASREM model as:
- sxxxxx.h/.cpp are sensor retrival classes.
- mxxxxx.h/.cpp are modelling and feature extraction classes.
- cxxxxx.h/.cpp are control classes.
- bxxxxx.h/.cpp are behaviour classes.
- uxxxxx.h/.cpp are utility functions.
C++ class structure
All classes have the same base structure. As an example (of a relatively simple class), the behaviour example called 'bplan1' is shown below; first the class definition in bplan1.h (all comments removed):
1 #pragma once 2 using namespace std; 3 class BPlan1 4 { 5 public: 6 ~BPlan1(); 7 void setup(); 8 void run(); 9 void terminate(); 10 private: 11 void toLog(const char * message); 12 int logCnt = 0; 13 bool toConsole = true; 14 FILE * logfile = nullptr; 15 bool setupDone = false; 16 }; 17 extern BPlan1 plan1;
Line 1 is just to ensure that this definition is read once only.
Line 2 is to simplify some notations
Line 3 Is the start of the class definition, the class names start with Capital letters, following the normal convention for type definitions.
Line 6 Is the destructor that will ensure log files are properly closed.
Line 7-9 define the general functions to start (setup), run and terminate the class. Setup reads the configuration file (robot.ini), and prepares logfiles and other initialization tasks.
Line 11 defines a function to make it easier to save data to the logfile, in this case, the log is text messages. The function adds a timestamp for each call so that the timing of the message can be compared with other logfiles.
Line 13-15 are other private variables.
Line 17 creates a reference to an instance of this class (the instance is created in the implementation file bplan1.cpp).
Class implementation file
All the function definitions need to be implemented, this is in the '*.cpp' file. A reduced example for the 'bplan1.cpp' function is shown below:
#include <string> #include <string.h> #include <math.h> #include "uservice.h" #include "mpose.h" #include "utime.h" #include "cmixer.h" #include "bplan1.h" // create class instance BPlan1 plan1; void BPlan1::setup() { // ensure there is default values in ini-file if (not ini.has("plan1")) { // no data yet, so generate some default values ini["plan1"]["log"] = "true"; ini["plan1"]["run"] = "false"; ini["plan1"]["print"] = "true"; } // get values from ini-file toConsole = ini["plan20"]["print"] == "true"; // if (ini["plan1"]["log"] == "true") { // open logfile std::string fn = service.logPath + "log_plan1.txt"; logfile = fopen(fn.c_str(), "w"); std::fprintf(logfile, "%% Mission plan1 logfile\n"); fprintf(logfile, "%% 1 \tTime (sec)\n"); fprintf(logfile, "%% 2 \tInteger or float (for Matlab to plot\n"); fprintf(logfile, "%% 3 \t%% Mission status (mostly for debug)\n"); } setupDone = true; } BPlan1::~BPlan1() { terminate(); } void BPlan1::run() { if (not setupDone) setup(); toLog("Plan1 started"); toLog("Forward 0.1 m/s start"); mixer.setVelocity(0.1); sleep(3); toLog("reverse 0.1m/s and turning +1 rad/s (CCV)"); mixer.setTurnrate(3.14); usleep(500000); // toLog("Servo 1 to -600 (500 units/sec) and vel=0"); servo.setServo(1, 1, -600, 500); mixer.setVelocity(0.5); mixer.setTurnrate(0.0);// but continue turning sleep(1); // } toLog("Servo 1 disable and stop"); servo.setServo(1, 0); mixer.setVelocity(0.0); sleep(1); toLog("Plan1 finished"); } void BPlan1::terminate() { fclose(logfile); } void BPlan1::toLog(const char* message) { UTime t("now"); if (logfile != nullptr) { fprintf(logfile, "%lu.%04ld %d %% %s\n", t.getSec(), t.getMicrosec()/100, logCnt, message); } if (toConsole) ... logCnt++; }
the #include files are library definitions if in <math.h> brackets or local definitions if in "usupport.h" quotes.
The next line
// create class instance BPlan1 plan1;
is the actual creation of the class instance, with the name plan1.
The remaining parts are the definition code for the functions defined in the header-file bplan1.h
Each function definition starts with the class type name followed by '::'.
The setup reads from the configuration structure, The structure itself is provided by the uservice module, e.g.:
if (ini["plan1"]["log"] == "true")
where the section is [plan1] and the item log. The (hertil)