Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UPDATE robocomp cli cpp11 #8

Merged
merged 5 commits into from
May 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ PROJECT( ${component_name} )
SET(RC_COMPONENT_PATH $${CMAKE_CURRENT_SOURCE_DIR} CACHE STRING "component base path" FORCE)
SET(RC_COMPONENT_DEVEL_PATH "$${CMAKE_CURRENT_SOURCE_DIR}" CACHE STRING "component binary path" FORCE)
#SET(RC_COMPONENT_DEVEL_PATH "$${WORKSPACE_PATH}/devel" CACHE STRING "component binary path")
SET(RC_COMPONENT_INSTALL_PATH "/opt/robocomp" CACHE STRING "component install path")
SET(RC_COMPONENT_INSTALL_PATH "/home/robocomp/robocomp" CACHE STRING "component install path")

#commented as now devel is merged into source space
#get_filename_component( COMPONENT_NAME $${CMAKE_CURRENT_SOURCE_DIR} DIRECTORY)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ SET ( SOURCES
$$ENV{ROBOCOMP}/classes/rapplication/rapplication.cpp
$$ENV{ROBOCOMP}/classes/sigwatch/sigwatch.cpp
$$ENV{ROBOCOMP}/classes/qlog/qlog.cpp
$$ENV{ROBOCOMP}/classes/grafcetStep/GRAFCETStep.cpp
main.cpp
genericmonitor.cpp
commonbehaviorI.cpp
Expand All @@ -43,9 +44,28 @@ ${wrap_ui}
# no-char8_t to avoid a problem with ICE Connection lib.
add_definitions(-fmax-errors=1 -fno-char8_t)

find_package(Qt6 COMPONENTS Core Widgets StateMachine REQUIRED)
SET (QT_LIBRARIES
$${QT_LIBRARIES}
Qt6::Core
Qt6::Widgets
Qt6::StateMachine )

add_definitions(-I/usr/include/x86_64-linux-gnu/qt6/QtOpenGLWidgets/)

if( USE_QT6)
LIST(APPEND SOURCES $$ENV{ROBOCOMP}/classes/abstract_graphic_viewer/abstract_graphic_viewer.cpp)
LIST(APPEND HEADERS $$ENV{ROBOCOMP}/classes/abstract_graphic_viewer/abstract_graphic_viewer.h)
LIST(APPEND LIBS QGLViewer-qt6 Qt6OpenGLWidgets)
else()
list(APPEND SOURCES $$ENV{ROBOCOMP}/classes/abstract_graphic_viewer_qt5/abstract_graphic_viewer.cpp)
list(APPEND HEADERS $$ENV{ROBOCOMP}/classes/abstract_graphic_viewer_qt5/abstract_graphic_viewer.h)
LIST(APPEND LIBS QGLViewer-qt5)
endif()

# Specify construction and link process
ADD_EXECUTABLE( ${component_name} $${SOURCES} $${MOC_SOURCES} $${RC_SOURCES} $${UI_HEADERS} )
TARGET_LINK_LIBRARIES( ${component_name} $${LIBS} $${STATIC_LIBS} $${SPECIFIC_LIBS} $${QT_LIBRARIES} $${Ice_LIBRARIES})
TARGET_LINK_LIBRARIES( ${component_name} $${LIBS} $${STATIC_LIBS} $${SPECIFIC_LIBS} $${QT_LIBRARIES} $${Ice_LIBRARIES} )
SET_TARGET_PROPERTIES(${component_name}
PROPERTIES
CXX_STANDARD 20
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ GenericMonitor::GenericMonitor(GenericWorker *_worker,Ice::CommunicatorPtr _comm
this->communicator = _communicator;
period = 100;
state = RoboCompCommonBehavior::State::Starting;
QObject::connect(this, SIGNAL(initializeWorker(int)), worker, SLOT(initialize(int)));
QObject::connect(this, SIGNAL(initializeWorker()), worker, SLOT(initializeWorker()));
}
/**
* \brief Default destructor
Expand Down Expand Up @@ -60,7 +60,7 @@ int GenericMonitor::getPeriod()
void GenericMonitor::setPeriod(int _period)
{
period =_period;
worker->setPeriod(_period);
worker->setPeriod(worker->Compute, _period);
}
/**
* \brief Kill component
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ Q_OBJECT

signals:
void kill();
void initializeWorker(int);
void initializeWorker();
};

#endif // GENERICMONITOR_H
Original file line number Diff line number Diff line change
Expand Up @@ -24,36 +24,118 @@ GenericWorker::GenericWorker(${constructor_proxies}) : ${inherited_constructor}
{

${statemachine_initialization}

${require_and_publish_proxies_creation}

mutex = new QMutex();
${state_statemachine}

${gui_setup}
Period = BASIC_PERIOD;
${compute_connect}
${transition_statemachine}

${add_state_statemachine}

${configure_statemachine}

${gui_setup}
}

/**
* \brief Default destructor
*/
GenericWorker::~GenericWorker()
{
for (auto state : states) {
delete state;
}

}
void GenericWorker::killYourSelf()
{
rDebug("Killing myself");
emit kill();
}

void GenericWorker::initializeWorker()
{
statemachine.start();

connect(&hibernationChecker, SIGNAL(timeout()), this, SLOT(hibernationCheck()));

auto error = statemachine.errorString();
if (error.length() > 0){
qWarning() << error;
throw error;
}

}

/**
* \brief Change compute period
* @param nameState name state "Compute" or "Emergency"
* @param per Period in ms
*/
void GenericWorker::setPeriod(int p)
void GenericWorker::setPeriod(STATES state, int p)
{
switch (state)
{
case STATES::Compute:
this->period = p;
states[STATES::Compute]->setPeriod(this->period);
std::cout << "Period Compute changed " << p << "ms" << std::endl<< std::flush;
break;

case STATES::Emergency:
states[STATES::Emergency]->setPeriod(this->period);
std::cout << "Period Emergency changed " << p << "ms" << std::endl<< std::flush;
break;

default:
std::cerr<<"No change in the period, the state parameter must be 'Compute' or 'Emergency'."<< std::endl<< std::flush;
break;
}
}

int GenericWorker::getPeriod(STATES state)
{
if (state < 0 || state >= STATES::NumberOfStates) {
std::cerr << "Invalid state parameter." << std::endl << std::flush;
return -1;
}
return states[state]->getPeriod();
}

void GenericWorker::hibernationCheck()
{
rDebug("Period changed"+QString::number(p));
Period = p;
timer.start(Period);
//Time between activity to activate hibernation
static const int HIBERNATION_TIMEOUT = 5000;

static std::chrono::high_resolution_clock::time_point lastWakeTime = std::chrono::high_resolution_clock::now();
static int originalPeriod = this->period;
static bool isInHibernation = false;

// Update lastWakeTime by calling a function
if (hibernation)
{
hibernation = false;
lastWakeTime = std::chrono::high_resolution_clock::now();

// Restore period
if (isInHibernation)
{
this->setPeriod(STATES::Compute, originalPeriod);
isInHibernation = false;
}
}

auto now = std::chrono::high_resolution_clock::now();
auto elapsedTime = std::chrono::duration_cast<std::chrono::milliseconds>(now - lastWakeTime);

//HIBERNATION_TIMEOUT exceeded, change period
if (elapsedTime.count() > HIBERNATION_TIMEOUT && !isInHibernation)
{
isInHibernation = true;
originalPeriod = this->getPeriod(STATES::Compute);
this->setPeriod(STATES::Compute, 500);
}
}

${agm_methods}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@
${gui_includes}
${statemachine_includes}
#include <CommonBehavior.h>
#include <grafcetStep/GRAFCETStep.h>
#include <QStateMachine>
#include <QEvent>
#include <QString>
#include <functional>

${interfaces_includes}
${agm_includes}
Expand All @@ -45,12 +50,17 @@ Q_OBJECT
GenericWorker(${constructor_proxies});
virtual ~GenericWorker();
virtual void killYourSelf();
virtual void setPeriod(int p);

virtual bool setParams(RoboCompCommonBehavior::ParameterList params) = 0;
QMutex *mutex;
${agm_methods}

enum STATES { Initialize, Compute, Emergency, Restore, NumberOfStates };
void setPeriod(STATES state, int p);
int getPeriod(STATES state);

QStateMachine statemachine;
QTimer hibernationChecker;
atomic_bool hibernation = false;

${agm_methods}

${create_proxies}

Expand All @@ -60,21 +70,24 @@ Q_OBJECT
protected:
${statemachine_creation}

QTimer timer;
int Period;
${agm_attributes_creation}

private:

int period = BASIC_PERIOD;
std::vector<GRAFCETStep*> states;

public slots:
${statemachine_slots}
${virtual_compute}
virtual void initialize(int period) = 0;
${virtual_statemachine}

void initializeWorker();
void hibernationCheck();


signals:
void kill();
${statemachine_signals}
${signal_statemachine}
};

#endif
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ void SpecificMonitor::initialize()
killYourSelf();
}
state = RoboCompCommonBehavior::State::Running;
emit initializeWorker(period);
emit initializeWorker();
}

bool SpecificMonitor::sendParamsToWorker(RoboCompCommonBehavior::ParameterList params)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
#include "genericmonitor.h"

/**
\brief
@author authorname
* \brief
* @author authorname
*/
class SpecificMonitor : public GenericMonitor
{
Expand All @@ -35,8 +35,7 @@ class SpecificMonitor : public GenericMonitor

void readConfig(RoboCompCommonBehavior::ParameterList &params );
void run();
void initialize();

void initialize();
bool sendParamsToWorker(RoboCompCommonBehavior::ParameterList params);
bool checkParams(RoboCompCommonBehavior::ParameterList l);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,38 +53,44 @@ bool SpecificWorker::setParams(RoboCompCommonBehavior::ParameterList params)
// innerModel = std::make_shared(innermodel_path);
// }
// catch(const std::exception &e) { qFatal("Error reading config params"); }


${innermodel_and_viewer_attribute_init}

${agm_innermodel_association}

${state_machine_start}

${dsr_set_params}

return true;
}

void SpecificWorker::initialize(int period)
void SpecificWorker::initialize()
{
std::cout << "Initialize worker" << std::endl;
this->Period = period;
if(this->startup_check_flag)
{
this->startup_check();
}
else
{

#ifdef HIBERNATION_ENABLED
hibernationChecker.start(500);
#endif

this->setPeriod(STATES::Compute, 100);
//this->setPeriod(STATES::Emergency, 500);

${statemachine_initialize_to_compute}
${dsr_initialize}
timer.start(Period);
}

}

${compute_method}

${emergency_method}

${restore_method}

int SpecificWorker::startup_check()
{
std::cout << "Startup check" << std::endl;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
#ifndef SPECIFICWORKER_H
#define SPECIFICWORKER_H

#define HIBERNATION_ENABLED

#include <genericworker.h>
${dsr_includes}

Expand All @@ -44,9 +46,8 @@ Q_OBJECT
${subscribes_method_definitions}

public slots:
${compute}
${state_machine_method}
int startup_check();
void initialize(int period);
${statemachine_methods_definitions}
${dsr_slots}
private:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ def wrap_ice(self):
interface_names = []

if self.component.recursiveImports is not None and self.component.ice_interfaces_names is not None:
for im in sorted(self.component.recursiveImports + self.component.ice_interfaces_names):
for im in sorted(self.component.recursiveImports + self.component.ice_interfaces_names):<<<<<<< development
name = im.split('/')[-1].split('.')[0]
interface_names.append(name)

result = "ROBOCOMP_IDSL_TO_ICE( CommonBehavior "
result += ' '.join(interface_names)
result += ")\n"
Expand Down
Loading
Loading