direcs  2012-09-30
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
sensorThread.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 SENSORTHREAD_H
22 #define SENSORTHREAD_H
23 
24 //-------------------------------------------------------------------
25 #include "interfaceAvr.h"
26 //-------------------------------------------------------------------
27 #include <QThread>
28 #include <QMutex>
29 #include <math.h>
30 //-------------------------------------------------------------------
31 
39 class SensorThread : public QThread
40 {
41  Q_OBJECT
42 
43  public:
44  SensorThread(InterfaceAvr *i, QMutex *m);
45  ~SensorThread();
46 
50  void stop();
51 
55  virtual void run();
56 
61  int getIrSensorValue(int sensor);
62 
67  int getUsSensorValue(int sensor);
68 
73  int getMotorSensorValue(int sensor);
74 
79  int getDistance(int sensor);
80 
85  int getDrivenDistance(int sensor);
86 
93  int convertToDistance(int sensorValue);
94 
101  int convertToSensorValue(int distance);
102 
107  int getMAmpere(int motor);
108 
113  float getVoltage(int sensor);
114 
119  int getContactValue(int contact);
120 
125  int getCompassValue(unsigned char axis);
126 
130  int getHeartbeatValue();
131 
132 
133  public slots:
139  bool resetDrivenDistance(int sensor);
140 
147  void setSimulationMode(bool state);
148 
154  void setRobotState(bool state);
155 
161  void setCompassState(bool state);
162 
163 
164  signals:
169  void sensorDataComplete();
170 
175  void contactAlarm(char position, bool state);
176 
180  void sendNetworkString(QString text);
181 
190  void compassDataComplete(float x, float y, float z, float heading);
191 
197  void heartbeat(unsigned char state);
198 
207  void message(QString text, bool CR=true, bool sayIt=false, bool addTimestamp=true);
208 
213  void systemerror(int errorlevel);
214 
215 
216  private:
222  float convertToDegree(int sensorValue);
223 
227  void calculateHeading(void);
228 
234  float convertToVolt(int sensor);
235 
241  bool readInfraredSensor(short int sensor);
242 
248  bool readUltrasonicSensor(short int sensor);
249 
255  bool readVoltageSensor(short int sensor);
256 
262  bool readMotorSensor(short int sensor);
263 
269  bool readDrivenDistance(short int sensor);
270 
276  bool readCompassAxis(short int axis);
277 
283  bool readContact(short int contact);
284 
285 
286  QString className;
287  mutable QMutex *mutex; // make this class thread-safe
289  volatile bool stopped;
291  bool robotState; // stores the robot state within this class
292  bool compassState; // stores the robot state within this class
293 
294  // Every thread sleeps some time, for having a bit more time fo the other threads!
295  // Time in milliseconds
296  static const unsigned long THREADSLEEPTIME = 1000; // Default: 100 ms
297 
298  // Give the infrared sensors some names
299  //
300  // DONT CHANGE THIS NUMBERS!
301  // THEY ARE ALSO USED TO ADRESS THE ARRAY "iRSensorValue[]" !!
302  //
303  static const short int SENSOR1 = 1;
304  static const short int SENSOR2 = 2;
305  static const short int SENSOR3 = 4;
306  static const short int SENSOR4 = 8;
307  static const short int SENSOR5 = 16;
308  static const short int SENSOR6 = 32;
309  static const short int SENSOR7 = 64;
310  static const short int SENSOR8 = 128;
311 
312  // ultrasonic sensor
313  static const short int SENSOR16 = 256;
314 
315  // 5 sensors + 1 = 16
316  // This just for convenience to call the array index by the sensor names.
317  // (wastin' some memory ;-) )
318  //
319  // #define SENSOR4 = 8
320  //
321  // For example: iRSensorValue[SENSOR4] = 0;
322  //
324 
326  static const unsigned char IRSENSORARRAYSIZE = 40;
328 
333 
334  static const short int VOLTAGESENSOR1 = 0;
335  static const short int VOLTAGESENSOR2 = 1;
336 
337  static const int MAXIMUMPLOTHEARTBEAT = 5;
338 
343  static const unsigned char USSENSORARRAYSIZE = 1;
345 
346  static const short int ULTRASONICSENSOR1 = 0;
347 
352  static const unsigned char MOTORSENSORARRAYSIZE = 4;
354 
355  static const short int MOTORSENSOR1 = 0;
356  static const short int MOTORSENSOR2 = 1;
357  static const short int MOTORSENSOR3 = 2;
358  static const short int MOTORSENSOR4 = 3;
359 
360  // defines the size of the drivenDistance[] array
361  // This es equal to the number of motors!
362  static const unsigned char DRIVENDISTANCEARRAYSIZE = 2;
364 
365  static const short int DRIVENDISTANCE1 = 0;
366  static const short int DRIVENDISTANCE2 = 1;
367 
372 
377  static const unsigned char CONVERSIONFACTORMOTORSENSOR = 29;
378 
389  static const float CONVERSIONFACTORVOLTAGESENSOR1 = 243.75;
390  static const float CONVERSIONFACTORVOLTAGESENSOR2 = 162.50;
391 
395  float xAxis;
396  float yAxis;
397  float zAxis;
398  float heading;
399 
400  static const short int XAXIS = 0;
401  static const short int YAXIS = 1;
402  static const short int ZAXIS = 2;
403 
404  static const bool ON = true;
405  static const bool OFF = false;
410  static const unsigned char CONTACT1 = 0;
411  static const unsigned char CONTACT2 = 1;
412  static const unsigned char CONTACT3 = 2;
413  static const unsigned char CONTACT4 = 3;
414 
415  // The position for the contact alarm in the camera image
416  static const char LEFT = 0;
417  static const char RIGHT = 1;
418  static const char TOP = 2;
419  static const char BOTTOM = 3;
420 
421  static const unsigned char CONTACTARRAYSIZE = 4;
423 
424  //
425  // the "serial" commands for the MC
426  //
427  static const unsigned char READ_SENSOR_1 = 1;
428  static const unsigned char READ_SENSOR_2 = 2;
429  static const unsigned char READ_SENSOR_3 = 3;
430  static const unsigned char READ_SENSOR_4 = 4;
431  static const unsigned char READ_SENSOR_5 = 5;
432  static const unsigned char READ_SENSOR_6 = 6;
433  static const unsigned char READ_SENSOR_7 = 7;
434  static const unsigned char READ_SENSOR_8 = 8;
435  static const unsigned char READ_SENSOR_16 = 16;
436 
437  static const unsigned char READ_MOTOR_SENSOR1 = 28;
438  static const unsigned char READ_MOTOR_SENSOR2 = 29;
439 
440  static const unsigned char READ_MOTOR_DISTANCE1 = 30;
441  static const unsigned char READ_MOTOR_DISTANCE2 = 31;
442 
443  static const short int RESET_MOTOR_DISTANCE1 = 32;
444  static const short int RESET_MOTOR_DISTANCE2 = 33;
445 
446  static const unsigned char READ_AXIS_X = 61;
447  static const unsigned char READ_AXIS_Y = 62;
448  static const unsigned char READ_AXIS_Z = 63;
449 
450  static const unsigned char READ_CONTACT1 = 70;
451  static const unsigned char READ_CONTACT2 = 71;
452  static const unsigned char READ_CONTACT3 = 72;
453  static const unsigned char READ_CONTACT4 = 73;
454 
458  static const unsigned char LEDOFF = 0;
459  static const unsigned char RED = 1;
460  static const unsigned char GREEN = 2;
461 };
462 
463 #endif