Repository navigation
Expand file tree
/
Copy pathservo.cpp
More file actions
251 lines (219 loc) · 4.66 KB
/
Copy pathservo.cpp
File metadata and controls
251 lines (219 loc) · 4.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
#include "arduino.h"
#include "servo.h"
#include <QCoreApplication>
#include <QSerialPortInfo>
#include <QDebug>
using namespace QArduino;
Servo::Servo(Arduino *board) : m_board(board), m_min(0), m_max(180)
{
m_pin = NULL;
}
Servo::~Servo()
{
delete m_pin;
}
void Servo::m_saturate(const float &minValue, const float &maxValue, quint8 &value)
{
if(minValue > value)
{
value = minValue;
}
else if(maxValue < value)
{
value = maxValue;
}
}
void Servo::setBoard(Arduino *board)
{
m_board = board;
m_pin = NULL;
}
Arduino* Servo::board()
{
return m_board;
}
float Servo::minValue() const
{
return m_min;
}
float Servo::maxValue() const
{
return m_max;
}
AnalogWritePin Servo::pin()
{
if(m_pin == NULL)
{
emit error("Servo is not attached to any pin.");
}
return (AnalogWritePin) *m_pin;
}
void Servo::setMin(const float &min)
{
m_min = min;
}
void Servo::setMax(const float &max)
{
m_max = max;
}
void Servo::attach(const AnalogWritePin &pin)
{
if(m_board == NULL)
{
emit error("Servo is not connected to any board.");
return;
}
// {'s', 'a', pin}
QByteArray data;
data.append('s');
data.append('a');
data.append(pin);
if(m_board->m_device->write(data) == 3)
{
if(m_board->m_device->waitForBytesWritten(100))
{
m_pin = new AnalogWritePin(pin);
qDebug() << "Servo: attached to pin" << pin;
}
}
}
bool Servo::isAttached()
{
if(m_board == NULL)
{
emit error("Servo is not connected to any board.");
return false;
}
// {'s', 'i', '0'}
QByteArray data;
data.append('s');
data.append('i');
data.append('0');
if(m_board->m_device->write(data) == 3)
{
if(m_board->m_device->waitForReadyRead(100))
{
m_board->m_read();
qDebug() << "Servo: isAttached successful.";
}
}
return m_board->m_data;
}
void Servo::detach()
{
// {'s', 'd', '0'}
QByteArray data;
data.append('s');
data.append('d');
data.append('0');
if(m_board->m_device->write(data) == 3)
{
if(m_board->m_device->waitForBytesWritten(100))
{
qDebug() << "Servo: detached";
}
}
}
void Servo::write(quint8 value)
{
if(m_board == NULL)
{
emit error("Servo is not connected to any board.");
return;
}
if(m_pin == NULL)
{
emit error("Servo is not attached to any pin.");
return;
}
// saturate to protect device
m_saturate(m_min, m_max, value);
// {'s', 'w', value}
QByteArray data;
data.append('s');
data.append('w');
data.append(value - 128);
if(m_board->m_device->write(data) == 3)
{
if(m_board->m_device->waitForBytesWritten(100))
{
qDebug() << "Servo: write value" << value;
}
}
}
quint8 Servo::read()
{
if(m_board == NULL)
{
emit error("Servo is not connected to any board.");
return 0;
}
if(m_pin == NULL)
{
emit error("Servo is not attached to any pin.");
return 0;
}
// {'s', 'r', '0'}
QByteArray data;
data.append('s');
data.append('r');
data.append('0');
if(m_board->m_device->write(data) == 3)
{
if(m_board->m_device->waitForReadyRead(100))
{
m_board->m_read();
qDebug() << "Servo: read successful.";
}
}
return m_board->m_data;
}
void StandardServo::setAngle(quint8 angle)
{
Servo::write(angle);
m_angle = angle;
qDebug() << "Servo: angle set to" << angle;
}
void StandardServo::goMinAngle()
{
Servo::write(minValue());
m_angle = minValue();
qDebug() << "Servo: angle set to minimum";
}
void StandardServo::goMaxAngle()
{
Servo::write(maxValue());
m_angle = maxValue();
qDebug() << "Servo: angle set to maximum";
}
quint8 StandardServo::angle() const
{
return m_angle;
}
void ContinuousServo::stop()
{
Servo::write(maxValue() - minValue() / 2);
qDebug() << "Servo: stopped";
}
qint8 ContinuousServo::speed() const
{
return m_speed;
}
void ContinuousServo::setSpeed(ContinuousServo::Direction direction, qint8 speed)
{
m_speed = speed;
if(direction == CCW)
{
m_speed = - m_speed;
}
quint8 value = ((float) (m_speed + 100)) * (maxValue() - minValue()) / 200;
Servo::write(value);
qDebug() << "Servo: speed set to" << speed;
}
void ContinuousServo::setSpeed(qint8 speed)
{
m_speed = speed;
quint8 value = ((float) (speed + 100)) * (maxValue() - minValue()) / 200;
Servo::write(value);
qDebug() << "Servo: speed set to" << speed;
}