direcs
2012-09-30
Main Page
Related Pages
Classes
Files
File List
File Members
All
Classes
Files
Functions
Variables
Typedefs
Enumerations
Enumerator
Macros
Pages
circuit.cpp
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
#include "
circuit.h
"
22
23
Circuit::Circuit
(
InterfaceAvr
*i, QMutex *m)
24
{
25
// get the name of this class (this is for debugging messages)
26
className
= this->staticMetaObject.className();
27
28
// copy the pointer from the original object
29
interface1
= i;
30
mutex
= m;
31
32
circuitState
=
true
;
// We think positive
33
firstInitDone
=
false
;
34
compassCircuitState
=
false
;
35
36
expectedAtmelAnswer
=
"error"
;
37
38
// theAtmelcommands
39
commandInitCircuit
=
"re"
;
40
commandInitCompass
=
"cc"
;
41
commandSleep
=
"sl"
;
42
}
43
44
45
Circuit::~Circuit
()
46
{
47
}
48
49
50
bool
Circuit::initCircuit
()
51
{
52
QString answer =
"error"
;
53
54
55
if
(
circuitState
)
// maybe robot is already recognized as OFF by the interface class (e.g. path to serial port not found)!
56
{
57
// Lock the mutex. If another thread has locked the mutex then this call will block until that thread has unlocked it.
58
mutex
->lock();
59
60
//-------------------------------------------------------
61
// Basic init for all the bits on the robot circuit
62
//-------------------------------------------------------
63
64
// sending RESET (INIT) command
65
if
(
interface1
->
sendString
(
"re"
,
className
) ==
true
)
66
{
67
// check if the robot answers with "ok"
68
if
(
interface1
->
receiveString
(answer,
className
) ==
true
)
69
{
70
// everthing's fine :-)
71
if
(answer ==
"*re#"
)
72
{
73
// Unlock the mutex
74
mutex
->unlock();
75
76
// ciruit init okay
77
firstInitDone
=
true
;
78
circuitState
=
true
;
79
emit
robotState
(
true
);
80
81
return
true
;
82
}
83
}
84
}
85
86
// Unlock the mutex.
87
mutex
->unlock();
88
89
}
90
91
qDebug(
"INFO from initCircuit: Robot is OFF."
);
92
firstInitDone
=
true
;
93
circuitState
=
false
;
94
emit
robotState
(
false
);
95
96
return
false
;
97
}
98
99
100
bool
Circuit::initCompass
()
101
{
102
QString answer =
"error"
;
103
104
105
if
(
circuitState
)
// maybe robot is already recognized as OFF by the interface class (e.g. path to serial port not found)!
106
{
107
// Lock the mutex. If another thread has locked the mutex then this call will block until that thread has unlocked it.
108
mutex
->lock();
109
110
// check if the 3D compass sensor is connected to the Atmel board
111
if
(
interface1
->
sendString
(
"cc"
,
className
) ==
true
)
112
{
113
// check if the robot answers with "ok"
114
if
(
interface1
->
receiveString
(answer,
className
) ==
true
)
115
{
116
if
(answer ==
"*ok#"
)
117
{
118
// Unlock the mutex
119
mutex
->unlock();
120
121
compassCircuitState
=
true
;
122
emit
compassState
(
true
);
123
124
return
true
;
125
}
126
}
127
}
128
129
// Unlock the mutex.
130
mutex
->unlock();
131
132
}
133
134
compassCircuitState
=
false
;
135
emit
compassState
(
false
);
136
137
return
false
;
138
}
139
140
141
bool
Circuit::isConnected
()
142
{
143
// if not tried to init hardware, do this!
144
if
(
firstInitDone
==
false
)
145
{
146
initCircuit
();
147
firstInitDone
=
true
;
148
}
149
150
return
circuitState
;
151
}
152
153
154
bool
Circuit::compassConnected
()
155
{
156
// if not tried to init the robots (and compass) hardware, do this!
157
if
(
firstInitDone
==
false
)
158
{
159
initCircuit
();
160
firstInitDone
=
true
;
161
}
162
163
return
compassCircuitState
;
164
}
165
166
167
void
Circuit::setRobotState
(
bool
state)
168
{
169
// store the state within this class
170
circuitState
= state;
171
// qDebug("Circuit::setRobotState: state=%d", circuitState);
172
}
173
174
175
bool
Circuit::sleep
()
176
{
177
QString answer =
"error"
;
178
179
180
if
(
circuitState
)
// maybe robot is already recognized as OFF by the interface class (e.g. path to serial port not found)!
181
{
182
// Lock the mutex. If another thread has locked the mutex then this call will block until that thread has unlocked it.
183
mutex
->lock();
184
185
// sending SLEEP command
186
if
(
interface1
->
sendString
(
"sl"
,
className
) ==
true
)
187
{
188
// check if the robot answers with "sl"
189
if
(
interface1
->
receiveString
(answer,
className
) ==
true
)
190
{
191
// everthing's fine
192
if
(answer ==
"*sl#"
)
193
{
194
// Unlock the mutex
195
mutex
->unlock();
196
197
return
true
;
198
}
199
}
200
}
201
202
// Unlock the mutex.
203
mutex
->unlock();
204
205
}
206
207
return
false
;
208
}
direcs
src
circuit.cpp
Generated on Sat Feb 2 2013 21:41:01 for direcs by
1.8.1.1