[Jderobot-admin] jderobot-r1233 - in trunk/src/stable/components: basic_component basic_component_qt basic_component_qt/gui
fperez en jderobot.org
fperez en jderobot.org
Jue Mayo 29 12:15:32 CEST 2014
Author: fperez
Date: 2014-05-29 12:15:32 +0200 (Thu, 29 May 2014)
New Revision: 1233
Modified:
trunk/src/stable/components/basic_component/CMakeLists.txt
trunk/src/stable/components/basic_component/basic_component.cfg
trunk/src/stable/components/basic_component/basic_component.cpp
trunk/src/stable/components/basic_component_qt/CMakeLists.txt
trunk/src/stable/components/basic_component_qt/gui/gui.cpp
trunk/src/stable/components/basic_component_qt/gui/gui.h
trunk/src/stable/components/basic_component_qt/gui/threadgui.cpp
trunk/src/stable/components/basic_component_qt/gui/threadgui.h
Log:
tarea #264 - basic component refactoring
Modified: trunk/src/stable/components/basic_component/CMakeLists.txt
===================================================================
--- trunk/src/stable/components/basic_component/CMakeLists.txt 2014-05-27 17:15:22 UTC (rev 1232)
+++ trunk/src/stable/components/basic_component/CMakeLists.txt 2014-05-29 10:15:32 UTC (rev 1233)
@@ -1,5 +1,5 @@
-SET( SOURCE_FILES control.cpp basic_component.cpp API.cpp gui.cpp)
+SET( SOURCE_FILES control/control.cpp control/threadcontrol.cpp gui/gui.cpp gui/threadgui.cpp basic_component.cpp shared.cpp)
include_directories(
Modified: trunk/src/stable/components/basic_component/basic_component.cfg
===================================================================
--- trunk/src/stable/components/basic_component/basic_component.cfg 2014-05-27 17:15:22 UTC (rev 1232)
+++ trunk/src/stable/components/basic_component/basic_component.cfg 2014-05-29 10:15:32 UTC (rev 1233)
@@ -1,3 +1,6 @@
-basic_component.Camera1.Proxy=cameraA:tcp -h localhost -p 9991
+basic_component.Camera1.Proxy=cameraA:tcp -h localhost -p 9999
basic_component.Camera2.Proxy=cameraB:tcp -h localhost -p 9992
+#delete this line or set OFF as value to disable the gui
+basic_component.Gui=ON
+
Modified: trunk/src/stable/components/basic_component/basic_component.cpp
===================================================================
--- trunk/src/stable/components/basic_component/basic_component.cpp 2014-05-27 17:15:22 UTC (rev 1232)
+++ trunk/src/stable/components/basic_component/basic_component.cpp 2014-05-29 10:15:32 UTC (rev 1233)
@@ -1,128 +1,74 @@
-#include "control.h"
-#include "API.h"
-#include "gui.h"
-#define cycle_control 100 //miliseconds
-#define cycle_gui 50 //miliseconds
+//ICE
+#include <Ice/Ice.h>
+#include <IceUtil/IceUtil.h>
+#include <pthread.h>
+#include "gui/threadgui.h"
+#include "gui/gui.h"
+#include "control/control.h"
+#include "control/threadcontrol.h"
-//Global Memory
-basic_component::Api *api;
+// Global members
-void *showGui(void*) {
+basic_component::ThreadControl* threadControl;
+basic_component::ThreadGui* threadGui;
- struct timeval a, b;
- long totalb, totala;
- int cont = 0;
- long diff;
- basic_component::Gui *gui;
+//Launching the control "thread"
+void controlStart() {
- gui = new basic_component::Gui(api);
+ threadControl->start();
+}
+//Launching the gui thred
+void* guiThread(void*) {
- while (true) {
- gettimeofday(&a, NULL);
- totala = a.tv_sec * 1000000 + a.tv_usec;
-
- gui->display(api);
-
-
- //Sleep Algorithm
- gettimeofday(&b, NULL);
- totalb = b.tv_sec * 1000000 + b.tv_usec;
- diff = (totalb - totala) / 1000;
- if (diff < 0 || diff > cycle_gui)
- diff = cycle_gui;
- else
- diff = cycle_gui - diff;
-
- /*Sleep Algorithm*/
- usleep(diff * 1000);
- if (diff < 33)
- usleep(33 * 1000);
- //printf("GUI %.30lf seconds elapsed, %d\n", diff, cont);
- cont++;
-
- }
+ threadGui->start();
}
-int main(int argc, char** argv) {
- pthread_t thr_gui;
+int main(int argc, char* argv[]) {
- basic_component::Control *control;
- int status;
- Ice::CommunicatorPtr ic;
- struct timeval a, b;
- long diff;
- long totalb, totala;
- bool guiActivated = 0;
- bool controlActivated = 0;
+ try{
+ //We initialize Ice here to be able to add some other options to the configuration file such as
+ //the posibility to show or not show the GUI.
+ Ice::CommunicatorPtr ic = Ice::initialize(argc, argv);
+
+ //Shared memory object
+ basic_component::Shared* sm = new basic_component::Shared();
+
+ //Creates the control&processing thread manager
+ threadControl = new basic_component::ThreadControl(ic, sm);
- api = new basic_component::Api();
- control = new basic_component::Control();
+ Ice::PropertiesPtr prop = ic->getProperties();
- pthread_mutex_init(&api->controlGui, NULL);
+ //Let's check if the user want to show the gui or not. This setting must be in the .cfg file
+ std::string gui = prop->getPropertyWithDefault("basic_component.Gui", "miss");
+ if (!boost::iequals(gui , "miss") && !boost::iequals(gui, "OFF")) {
+
+ pthread_t t_gui;
+ //Creates the gui thread manager
+ threadGui = new basic_component::ThreadGui(sm);
- try {
+ //Creates the thread for the control&processing
+ pthread_create(&t_gui, NULL, &guiThread, NULL);
+
+ }else
+ std::cout << "No Gui mode" << std::endl;
- //-----------------ICE----------------//
- ic = Ice::initialize(argc, argv);
-
- // Get driver camera
- Ice::ObjectPrx camara1 = ic->propertyToProxy("basic_component.Camera1.Proxy");
- if (0 == camara1)
- throw "Could not create proxy to camera1 server";
-
- // cast to CameraPrx
- control->cprx1 = jderobot::CameraPrx::checkedCast(camara1);
- if (0 == control->cprx1)
- throw "Invalid proxy";
-
- //-----------------END ICE----------------//
-
- //****************************** Processing the Control ******************************///
- api->guiVisible = true;
- control->UpdateSensorsICE(api);
-
- pthread_create(&thr_gui, NULL, &showGui, NULL);
-
- while (api->guiVisible) {
- gettimeofday(&a, NULL);
- totala = a.tv_sec * 1000000 + a.tv_usec;
-
-
- control->UpdateSensorsICE(api); // Update sensors
-
- //Sleep Algorithm
- gettimeofday(&b, NULL);
- totalb = b.tv_sec * 1000000 + b.tv_usec;
- diff = (totalb - totala) / 1000;
- if (diff < 0 || diff > cycle_control)
- diff = cycle_control;
- else
- diff = cycle_control - diff;
-
- /*Sleep Algorithm*/
- usleep(diff * 1000);
- if (diff < 33)
- usleep(33 * 1000);
- //printf("CONTROL %.15lf seconds elapsed\n", diff);
- }
-
- //****************************** END Processing the Control ******************************///
- if (guiActivated)
- pthread_join(thr_gui, NULL);
-
+ //We use the main thread to manage the control&processing thread, so the gui has it's own thread that is
+ //periodically updated.
+ controlStart();
+
+
+
} catch (const Ice::Exception& ex) {
std::cerr << ex << std::endl;
- status = 1;
+ exit(-1);
} catch (const char* msg) {
std::cerr << msg << std::endl;
- status = 1;
+ exit(-1);
}
- if (ic)
- ic->destroy();
return 0;
-}
+}
Modified: trunk/src/stable/components/basic_component_qt/CMakeLists.txt
===================================================================
--- trunk/src/stable/components/basic_component_qt/CMakeLists.txt 2014-05-27 17:15:22 UTC (rev 1232)
+++ trunk/src/stable/components/basic_component_qt/CMakeLists.txt 2014-05-29 10:15:32 UTC (rev 1233)
@@ -1,17 +1,10 @@
if (${QT_COMPILE})
- SET(qt_SOURCES main.cpp
- sensors/threadsensors.cpp sensors/sensors.cpp
- gui/threadgui.cpp gui/gui.cpp
- )
+ SET(qt_SOURCES basic_component_qt.cpp gui/gui.cpp)
- SET(qt_HEADERS
- sensors/threadsensors.h sensors/sensors.h
- gui/threadgui.h gui/gui.h
+ SET(qt_HEADERS gui/gui.h)
- )
-
include_directories(
${INTERFACES_CPP_DIR}
${LIBS_DIR}/
@@ -22,16 +15,19 @@
add_executable( basic_component_qt
- main.cpp
- sensors/threadsensors.cpp sensors/sensors.cpp
- gui/threadgui.cpp gui/gui.cpp
- ${qt_HEADERS_MOC}
+ basic_component_qt.cpp
+ control/threadcontrol.cpp
+ control/control.cpp
+ gui/threadgui.cpp
+ gui/gui.cpp
+ shared.cpp
+ ${qt_HEADERS_MOC}
)
target_link_libraries(basic_component_qt
${CMAKE_THREAD_LIBS_INIT}
${OpenCV_LIBRARIES}
- ${QT_LIBRARIES_JDE}
+ ${QT_LIBRARIES_JDE}
JderobotInterfaces
jderobotutil
${ZeroCIce_LIBRARIES}
Modified: trunk/src/stable/components/basic_component_qt/gui/gui.cpp
===================================================================
--- trunk/src/stable/components/basic_component_qt/gui/gui.cpp 2014-05-27 17:15:22 UTC (rev 1232)
+++ trunk/src/stable/components/basic_component_qt/gui/gui.cpp 2014-05-29 10:15:32 UTC (rev 1233)
@@ -1,41 +1,52 @@
#include "gui.h"
-
-GUI::GUI(Sensors* sensors)
+namespace basic_component_qt {
+Gui::Gui(Shared* sm)
{
- this->sensors = sensors;
+ this->sm = sm;
+ //Creation and setup of Gui elements
QGridLayout* mainLayout = new QGridLayout();
-
labelImage = new QLabel();
-
mainLayout->addWidget(labelImage, 0, 0);
-
setLayout(mainLayout);
-
setVisible(true);
+ //We connect a signat to a slot to be able to do something from an incoming signal.
connect(this, SIGNAL(signal_updateGUI()), this, SLOT(on_updateGUI_recieved()));
show();
}
-void GUI::updateThreadGUI()
+
+void Gui::updateThreadGUI()
{
+ //emit the signal to update the gui
emit signal_updateGUI();
}
-void GUI::on_updateGUI_recieved()
+void Gui::on_updateGUI_recieved()
{
- cv::Mat frame = this->sensors->getImage();
+ //Create and displays the image taken from the shared memory.
+ //cv::Mat
+ cv::Mat frame = this->sm->getImage();
QImage imageQt = QImage((const unsigned char*)(frame.data),
frame.cols,
frame.rows,
frame.step,
QImage::Format_RGB888);
-
+/*
+ //IplImage
+ IplImage* frame = this->sm->getImage();
+ QImage imageQt = QImage((const unsigned char*)(frame->imageData),
+ frame->width,
+ frame->height,
+ QImage::Format_RGB888);
+*/
+
labelImage->setPixmap(QPixmap::fromImage(imageQt));
}
+}
Modified: trunk/src/stable/components/basic_component_qt/gui/gui.h
===================================================================
--- trunk/src/stable/components/basic_component_qt/gui/gui.h 2014-05-27 17:15:22 UTC (rev 1232)
+++ trunk/src/stable/components/basic_component_qt/gui/gui.h 2014-05-29 10:15:32 UTC (rev 1233)
@@ -3,19 +3,20 @@
#include <QtGui>
-#include "../sensors/sensors.h"
+#include "../shared.h"
-class GUI:public QWidget
+namespace basic_component_qt {
+class Gui : public QWidget
{
Q_OBJECT
public:
- GUI(Sensors* sensors);
+ Gui(Shared* sm);
void updateThreadGUI();
private:
QLabel* labelImage;
- Sensors* sensors;
+ Shared* sm;
signals:
void signal_updateGUI();
@@ -24,5 +25,5 @@
void on_updateGUI_recieved();
};
-
+}
#endif // GUI_H
Modified: trunk/src/stable/components/basic_component_qt/gui/threadgui.cpp
===================================================================
--- trunk/src/stable/components/basic_component_qt/gui/threadgui.cpp 2014-05-27 17:15:22 UTC (rev 1232)
+++ trunk/src/stable/components/basic_component_qt/gui/threadgui.cpp 2014-05-29 10:15:32 UTC (rev 1233)
@@ -1,16 +1,17 @@
#include "threadgui.h"
-threadGUI::threadGUI(Sensors* sensors)
+namespace basic_component_qt {
+ThreadGui::ThreadGui(Shared* sm)
{
- this->sensors = sensors;
-
- gui = new GUI(sensors);
+ //New instace of Gui with the shared memory object.
+ gui = new Gui(sm);
gui->show();
}
-void threadGUI::run()
+void ThreadGui::start()
{
+ //Calculates the refreshing time of the gui.
struct timeval a, b;
long totalb, totala;
long diff;
@@ -19,6 +20,7 @@
gettimeofday(&a, NULL);
totala = a.tv_sec * 1000000 + a.tv_usec;
+ //updates the gui (image shown in this component)
gui->updateThreadGUI();
gettimeofday(&b, NULL);
@@ -37,3 +39,4 @@
usleep(33 * 1000);
}
}
+}
Modified: trunk/src/stable/components/basic_component_qt/gui/threadgui.h
===================================================================
--- trunk/src/stable/components/basic_component_qt/gui/threadgui.h 2014-05-27 17:15:22 UTC (rev 1232)
+++ trunk/src/stable/components/basic_component_qt/gui/threadgui.h 2014-05-29 10:15:32 UTC (rev 1233)
@@ -1,28 +1,24 @@
#ifndef THREADGUI_H
#define THREADGUI_H
-#include <QtGui>
-
#include <iostream>
#include <sys/time.h>
#include "gui.h"
-#include "../sensors/sensors.h"
+#include "../shared.h"
#define cycle_gui 50 //miliseconds
-
-class threadGUI:public QThread
+namespace basic_component_qt {
+class ThreadGui
{
public:
- threadGUI(Sensors* sensors);
+ ThreadGui(Shared* sm);
+ void start();
private:
- GUI* gui;
- Sensors* sensors;
-protected:
- void run();
+ Gui* gui;
};
-
+}
#endif // THREADGUI_H
More information about the Jderobot-admin
mailing list