direcs  2012-09-30
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
direcs.h
Go to the documentation of this file.
1 /*************************************************************************
2  * Copyright (C) Markus Knapp *
3  * www.direcs.de *
4  * *
5  * This file is part of direcs. *
6  * *
7  * direcs is free software: you can redistribute it and/or modify it *
8  * under the terms of the GNU General Public License as published *
9  * by the Free Software Foundation, version 3 of the License. *
10  * *
11  * direcs is distributed in the hope that it will be useful, *
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14  * GNU General Public License for more details. *
15  * *
16  * You should have received a copy of the GNU General Public License *
17  * along with direcs. If not, see <http://www.gnu.org/licenses/>. *
18  * *
19  *************************************************************************/
20 
21 #ifndef DIRECS_H
22 #define DIRECS_H
23 
24 //-------------------------------------------------------------------
25 #include <QtGlobal> // for Q_OS_* Makro!
26 #include <QObject>
27 #include <QtDebug>
28 #include <QMutex>
29 #include <QSocketNotifier>
30 #include <QtGui>
31 #include <QSplashScreen>
32 #include <QMetaType>
33 #include <QtCore/QCoreApplication>
34 
35 #ifdef Q_OS_LINUX
36 #include <phonon>
37 #else
38 #include <Phonon>
39 #endif
40 
41 #ifdef Q_OS_LINUX
42 #include <time.h> // for rand (for the media player / demo mode)
43 #include <stdlib.h> // for rand (for the media player / demo mode)
44 #endif
45 
46 
47 //-------------------------------------------------------------------
48 #include "consoleGui.h"
49 #include "gui.h"
50 #include "joystickDialog.h"
51 #include "settingsDialog.h"
52 #include "aboutDialog.h"
53 
54 #ifndef BUILDFORROBOT
55  #include "plotThread.h"
56 #endif
57 
58 #include "circuit.h"
59 #include "interfaceAvr.h"
60 //#include "heartbeat.h"
61 #include "sensorThread.h"
62 #include "motor.h"
63 #include "servo.h"
64 #include "rgbLed.h"
65 #include "obstacleCheckThread.h"
66 #include "inifile.h"
67 #include "logfile.h"
68 #include "networkThread.h"
69 #include "laserThread.h"
70 #include "joystick.h"
71 #include "head.h"
72 #include "camThread.h"
73 #include "speakThread.h"
74 #include "timerThread.h"
75 #include "demoThread.h"
76 
77 
78 //-------------------------------------------------------------------
79 #include <signal.h> // for SIGINT
80 using namespace std;
81 
82 struct CleanExit
83 {
85  {
86  signal(SIGINT, &CleanExit::exitQt);
87  signal(SIGTERM, &CleanExit::exitQt);
88  // signal(SIGBREAK, &CleanExit::exitQt) ;
89  }
90 
91  static void exitQt(int sig)
92  {
93  Q_UNUSED(sig);
94  // this is now called in the console mode, when hitting ctrl+c (SIGINT)
95  // emit a signal here to call shutdown?
96  QCoreApplication::quit(); // FIXME: threads are not ended!
97  }
98 };
99 //-------------------------------------------------------------------
100 
101 
108 class Direcs : public QObject
109 {
110  Q_OBJECT
111 
112 
113  public:
117  Direcs(bool bConsoleMode, bool bForceSmallGUI, bool bForceLargeGUI);
118  ~Direcs();
119 
120 
124  void init();
125 
129  bool simulationMode() const;
130 
135 
140 
141 
142  public slots:
147  void drive(const int command);
148 
152  void increaseDrivingSpeed(void);
153 
161  void logicalUnit(int sensorAlarm, QDateTime timestamp);
162 
167  void faceTracking(int faces, int faceX, int faceY, int faceRadius);
168 
173  void enableFaceTracking(int state);
174 
179  void showSensorData();
180 
185  void showExitDialog();
186 
192  void enableRemoteControlListening(bool status);
193 
197  void executeRemoteCommand(QString command);
198 
204  void setNetworkState();
205 
209  void executeJoystickCommand(int axisNumber, int axisValue);
210  void executeJoystickCommand(int buttonNumber, bool buttonState);
211 
216  void setSimulationMode(bool status);
217 
222  void setDemoMode(bool status);
223 
227  void nextDemoPhase(int phase);
228 
232  void mediaPlayerFinished();
233 
237  void showSplashMessage(QString text);
238 
242  void finishSplash();
243 
244  /*
245  / **
246  This slot sets (stores) the robots (circuits) state within this class.
247  @param state can be ON or OFF
248  * /
249  void setRobotState(bool state);
250  */
251 
256  void shutdown();
257 
261  void systemerrorcatcher(int errorlevel);
262 
266  void test();
267 
268 
269  signals:
274  void showPreferredDirection(QString direction);
275 
280  void publishConsoleMode(bool state);
281 
285  void showFaceTrackDirection(QString direction);
286 
291  void look(QString direction);
292 
297  void speak(QString text, int phase=0);
298 
302  void initCircuit();
303 
307  void sendNetworkString(QString text);
308 
317  void message(QString text, bool CR=true, bool sayIt=false, bool addTimestamp=true);
318 
323  void splashMessage(QString text);
324 
325 
326  private:
330  void readSettings();
331 
335  void checkArguments();
336 
342  void resetDrivingSpeedTimer(void);
343 
349  void drivingLight(unsigned char color);
350 
351 
352  mutable QMutex *mutex; // make the threads thread-safe (e.g. senorThread, servo...)
353 
354  //Events *myEvent; /// my event filter for grabbing ctrl+c in the console mode
357 #ifndef _ARM_ // only include on _non_ ARM environments!
361 
362 #ifndef BUILDFORROBOT
364 #endif
365 
366  QSplashScreen *splash;
367 #ifndef _ARM_ // only include on _non_ ARM environments!
369 #endif
370  QStringList arguments;
371 #endif
393  QString laserscannerTypeFront; // PLS, LMS, S300. just for reading the value here. @sa laserThread
394  QString laserscannerTypeRear; // PLS, LMS, S300. just for reading the value here. @sa laserThread
395  int laserscannerAngleFront; // this values holds the range of the laserscanner viewing angle. e.g. 180 oder 270 degrees. just for reading the value here. @sa laserThread
396  int laserscannerAngleRear; // this values holds the range of the laserscanner viewing angle. e.g. 180 oder 270 degrees. just for reading the value here. @sa laserThread
397  int laserscannerIgnoreAreaStart; // this is only temporary needed when reading a setting
398  int laserscannerIgnoreAreaEnd; // this is only temporary needed when reading a setting
399  QString laserscannerMounting; // just for reading the value here. @sa laserThread
400  QString commaSeparator;
403  //bool robotIsOn; /// Stores the robots (circuits) state.ON or OFF
404  bool robotDrives;
409  unsigned int networkPortListen;
410  unsigned int networkPortSend;
412  bool useCamera;
433  unsigned int value;
434  float floatValue;
435  bool consoleMode;
440  bool useSmallGUI;
441  QList <QDateTime> obstacleAlarmFrontLeftList;
442  QList <QDateTime> obstacleAlarmFrontRightList;
443  //QList <QDateTime> obstacleAlarmLeftTimestampList; /// A list of the timestamps of the obstacle alarms that left occured.
444  //QList <QDateTime> obstacleAlarmRightTimestampList; /// A list of the timestamps of the obstacle alarms that right occured.
445  Phonon::MediaObject *mediaObject;
446  bool demoMode;
447 
449  static const unsigned int SPLASHTIME = 2000;
450 
452  static const unsigned int DRIVINGSPEEDINCREASER = 10;
453 
454  static const bool ON = true;
455  static const bool OFF = false;
457 
458  static const int FORWARD = 10;
459  static const int BACKWARD = 20;
460  static const int LEFT = 30;
461  static const int RIGHT = 40;
462  static const int TURNLEFT = 50;
463  static const int TURNRIGHT = 60;
464  static const int START = 70;
465  static const int STOP = 80;
466  static const int WAIT = 90;
467  // static const int DIAGONAL_FORWARD_LEFT
468  // static const int DIAGONAL_FORWARD_RIGHT
469  // static const int DIAGONAL_BACKWARD_LEFT
470  // static const int DIAGONAL_BACKWARD_RIGHT
471 
472  static const int SAME = 200;
473  static const int MOTOR1 = 210;
474  static const int MOTOR2 = 220;
475  static const int MOTOR3 = 230;
476  static const int MOTOR4 = 240;
477  static const int ALLMOTORS = 250;
478 
479  static const int MOTOR1FW = 300;
480  static const int MOTOR1BW = 310;
481  static const int MOTOR1OFF = 320;
482  static const int MOTOR2FW = 330;
483  static const int MOTOR2BW = 340;
484  static const int MOTOR2OFF = 350;
485  static const int MOTOR3FW = 360;
486  static const int MOTOR3BW = 370;
487  static const int MOTOR3OFF = 380;
488  static const int MOTOR4FW = 390;
489  static const int MOTOR4BW = 400;
490  static const int MOTOR4OFF = 410;
491 
492  static const int READ_AXIS_X = 500;
493  static const int READ_AXIS_Y = 510;
494  static const int READ_AXIS_Z = 520;
495 
501  static const short int SENSOR1 = 1;
502  static const short int SENSOR2 = 2;
503  static const short int SENSOR3 = 4;
504  static const short int SENSOR4 = 8;
505  static const short int SENSOR5 = 16;
506  static const short int SENSOR6 = 32;
507  static const short int SENSOR7 = 64;
508  static const short int SENSOR8 = 128;
509 
511  static const short int SENSOR16 = 256;
512 
514  static const short int NONE = 0;
515 
516  static const short int OBSTACLEFRONTLEFT = 512;
517  static const short int OBSTACLEFRONTRIGHT = 1024;
518  static const short int OBSTACLESEVERYWHEREINFRONT = 2048;
519 
520  static const short int MAXFALSEALARMS = 2;
521 
522 
524  static const short int MOTORSENSOR1 = 0;
525  static const short int MOTORSENSOR2 = 1;
526 
528  static const short int VOLTAGESENSOR1 = 0;
529  static const short int VOLTAGESENSOR2 = 1;
530 
532  static const float MINIMUMVOLTAGE1 = 10.80;
533  static const float MINIMUMVOLTAGE2 = 21.60;
534 
536  static const unsigned char NUMBEROFSERVOS = 6;
537 
538  static const unsigned char SERVO1 = 0;
539  static const unsigned char SERVO2 = 1;
540  static const unsigned char SERVO3 = 2;
541  static const unsigned char SERVO4 = 3;
542  static const unsigned char SERVO5 = 4;
543  static const unsigned char SERVO6 = 5;
545  static const unsigned char SVSTART = 0;
546  static const unsigned char SVEND = 1;
547  static const unsigned char SVDEFAULT = 2;
548  static const unsigned char SVCURRENT = 3;
549  static const unsigned char SVMIN = 4;
550  static const unsigned char SVMAX = 5;
551 
553  static const unsigned char NUMBEROFRGBLEDS = 6;
554  static const unsigned char RGBLED1 = 0;
555  static const unsigned char RGBLED2 = 1;
556  static const unsigned char RGBLED3 = 2;
557  static const unsigned char RGBLED4 = 3;
558  static const unsigned char RGBLED5 = 4;
559  static const unsigned char RGBLED6 = 5;
561  static const unsigned char RGBLEDDEFAULT = 0;
562  static const unsigned char RGBLEDACTUAL = 1;
563  static const unsigned char RGBLEDMIN = 2;
564  static const unsigned char RGBLEDMAX = 3;
565  static const int MINPWM = 1;
566  static const int MAXPWM = 99;
567 
571  static const int JOYSTICKAXISY2 = 2; // ok
572  static const int JOYSTICKAXISX3 = 3; // ok
573  static const int JOYSTICKAXISX4 = 4;
574  static const int JOYSTICKAXISY5 = 5;
576  static const int JOYSTICKDIVISOR = 128;
577 
581  static const short int LASER1 = 1;
582  static const short int LASER2 = 2;
583  static const short int LASER3 = 4;
584  static const short int LASER4 = 8;
585  static const short int LASER5 = 16;
586 
590  static const int AREA1 = 1;
591  static const int AREA2 = 2;
592 
596  static const unsigned char LEDOFF = 0;
597  static const unsigned char RED = 1;
598  static const unsigned char GREEN = 2;
599  static const unsigned char BLUE = 3;
600  static const unsigned char WHITE = 4;
601 };
602 
603 #endif