direcs  2012-09-30
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
motor.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 MOTOR_H
22 #define MOTOR_H
23 
24 //-------------------------------------------------------------------
25 #include "interfaceAvr.h"
26 //-------------------------------------------------------------------
27 #include <QCoreApplication>
28 #include <QMutex>
29 //-------------------------------------------------------------------
30 
37 class Motor : public QObject
38 {
39  Q_OBJECT
40 
41  public:
42  Motor(InterfaceAvr *i, QMutex *m);
43  ~Motor();
44 
54  bool motorControl(int motor, bool power, int direction);
55 
56 
61  int getMotorSpeed(int motor);
62 
63  //bool isMotorActive();
64  //unsigned int getSteps1();
65  //unsigned int getSteps2();
66 
71  unsigned int getRevolutions(unsigned char motor);
72 
78  double getDrivenDistance(unsigned char motor);
79 
84  void resetMovementCounter(short int motor);
85 
91  bool flashlight(bool state);
92 
93  /*
94  Generates steps for stepper motors.
95  @param steps are the number of steps.
96  void makeSteps(int steps);
97 
98  void parkStepper(unsigned char motor);
99  */
100 
101  public slots:
108  bool setMotorSpeed(int motor, int speed);
109 
114  void setMaximumSpeed(int speed);
115 
122  void setRobotState(bool state);
123 
124 
125 signals:
134  void message(QString text, bool CR=true, bool sayIt=false, bool addTimestamp=true);
135 
136 
137  private:
138  void calculateMovement();
139 
140  QString className;
141  mutable QMutex *mutex; // make this class thread-safe
143  bool robotState; // stores the robot state within this class
148  int motorSpeedAllMotors; // this is the speed for all motors together, when set in one command
149 
150  //unsigned int steps1;
151  //unsigned int steps2;
152  unsigned int revolutions1;
153  unsigned int revolutions2;
154  unsigned int revolutions3;
155  unsigned int revolutions4;
160 
161  static const char ON = true;
162  static const char OFF = false;
163  static const char CLOCK = 1;
165  static const unsigned char HIGH = 1; // For set bit to 1
166  static const unsigned char LOW = 0; // For set bit to 0
167 
168  static const unsigned char BIT0 = 1;
169  static const unsigned char BIT1 = 2;
170  static const unsigned char BIT2 = 4;
171  static const unsigned char BIT3 = 8;
172  static const unsigned char BIT4 = 16;
173  static const unsigned char BIT5 = 32;
174  static const unsigned char BIT6 = 64;
175  static const unsigned char BIT7 = 128;
176 
178  static const int FORWARD = 10;
179  static const int BACKWARD = 20;
180  static const int LEFT = 30;
181  static const int RIGHT = 40;
182  static const int TURNLEFT = 50;
183  static const int TURNRIGHT = 60;
184  static const int START = 70;
185  static const int STOP = 80;
186  static const int WAIT = 90;
187  // static const int DIAGONAL_FORWARD_LEFT
188  // static const int DIAGONAL_FORWARD_RIGHT
189  // static const int DIAGONAL_BACKWARD_LEFT
190  // static const int DIAGONAL_BACKWARD_RIGHT
191 
192  static const int SAME = 200;
193  static const int MOTOR1 = 210;
194  static const int MOTOR2 = 220;
195  static const int MOTOR3 = 230;
196  static const int MOTOR4 = 240;
197  static const int ALLMOTORS = 250;
198 
199  static const int MOTOR1FW = 300;
200  static const int MOTOR1BW = 310;
201  static const int MOTOR1OFF = 320;
202  static const int MOTOR2FW = 330;
203  static const int MOTOR2BW = 340;
204  static const int MOTOR2OFF = 350;
205  static const int MOTOR3FW = 360;
206  static const int MOTOR3BW = 370;
207  static const int MOTOR3OFF = 380;
208  static const int MOTOR4FW = 390;
209  static const int MOTOR4BW = 400;
210  static const int MOTOR4OFF = 410;
211 
212  static const int READ_AXIS_X = 500;
213  static const int READ_AXIS_Y = 510;
214  static const int READ_AXIS_Z = 520;
215 
216  //
218  //
219  static const unsigned char MOTOR1A = BIT0;
220  static const unsigned char MOTOR1B = BIT1;
221  static const unsigned char MOTOR2A = BIT2;
222  static const unsigned char MOTOR2B = BIT3;
223  static const unsigned char MOTORCLOCK = BIT4;
224 
226  static const unsigned char MOTOR1_OFF = 20;
227  static const unsigned char MOTOR1_CLOCKWISE = 21;
228  static const unsigned char MOTOR1_COUNTERCLOCKWISE = 22;
229  static const unsigned char MOTOR2_OFF = 23;
230  static const unsigned char MOTOR2_CLOCKWISE = 24;
231  static const unsigned char MOTOR2_COUNTERCLOCKWISE = 25;
232 
233  static const unsigned char MOTOR1_SPEED_SET = 26;
234  static const unsigned char MOTOR2_SPEED_SET = 27;
235 
236  static const unsigned char MOTOR3_OFF = 50;
237  static const unsigned char MOTOR3_CLOCKWISE = 51;
238  static const unsigned char MOTOR3_COUNTERCLOCKWISE = 52;
239  static const unsigned char MOTOR4_OFF = 53;
240  static const unsigned char MOTOR4_CLOCKWISE = 54;
241  static const unsigned char MOTOR4_COUNTERCLOCKWISE = 55;
242 
243  static const unsigned char MOTOR3_SPEED_SET = 56;
244  static const unsigned char MOTOR4_SPEED_SET = 57;
245 
246  static const unsigned char SPEED_SET_ALLMOTORS = 60;
247 
249  static const unsigned char BOTFORWARD = 80;
250  static const unsigned char BOTBACKWARD = 81;
251  static const unsigned char BOTLEFT = 82;
252  static const unsigned char BOTRIGHT = 83;
253  static const unsigned char BOTTURNLEFT = 84;
254  static const unsigned char BOTTURNRIGHT = 85;
255  static const unsigned char BOTSTART = 86;
256  static const unsigned char BOTSTOP = 87;
257  static const unsigned char BOTWAIT = 88;
258  // static const unsigned char BOTDIAGONAL_FORWARD_LEFT
259  // static const unsigned char BOTDIAGONAL_FORWARD_RIGHT
260  // static const unsigned char BOTDIAGONAL_BACKWARD_LEFT
261  // static const unsigned char BOTDIAGONAL_BACKWARD_RIGHT
262 
263  static const unsigned char FLASHLIGHT_OFF = 40;
264  static const unsigned char FLASHLIGHT_ON = 41;
265 
266 };
267 
268 #endif