-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathCustomPicar.cpp
278 lines (257 loc) · 5.79 KB
/
CustomPicar.cpp
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
#include <iostream>//
#include "CustomPicar.h"
using namespace std;
using namespace auto_car;
Servo::Servo()
{
}
Servo::Servo(PCA9685 pca_, int motorPin, int timeTerm, bool printFlag)
{
board = pca_;
pin = motorPin;
term = timeTerm;
if (pin == Steering)
{
minVal = 270;
maxVal = 470;
centerVal = (minVal + maxVal) / 2; //370
}
else if (pin == Pan)
{
minVal = 190;
maxVal = 530;
centerVal = 360;
}
else if (pin == Tilt)
{
minVal = 300;
maxVal = 650;
centerVal = 321; //350 ¿¡¼º¯°æ 5/23
}
else
{
cout << "Wrong servo object setting." << endl;
cout << "program exit" << endl;
exit(1);
}
length = maxVal - minVal;
rate = 10;
value = centerVal;
}
void Servo::setValue(uint16_t set_val)
{
if (set_val > maxVal) set_val = maxVal;
if (set_val < minVal) set_val = minVal;
//cout << " @pin :" << pin << ", value " << value << ", set_val :" << set_val << endl;
//¼º¸°¢ Ãâ·Â.
//if (print) cout << " @servo pin :" << pin << ", ratio : " << (((double)(set_val - minVal) / (double)length) * 100.0) << endl;
while (value < set_val)
{
value += 1;
board.set_pwm(pin, 0, value);
usleep(term);
}
while (value > set_val)
{
value -= 1;
board.set_pwm(pin, 0, value);
usleep(term);
}
}
void Servo::setRatio(double ratio)
{
uint16_t val = minVal + length * (ratio / 100);
setValue(val);
}
void Servo::resetCenter()
{
if (print) cout << " @pin :" << pin << " centerVal :" << centerVal << endl;
setValue(centerVal + 10);
setValue(centerVal - 10);
setValue(centerVal);
}
void Servo::operator++(int)
{
uint16_t val = value + (length * (rate / 100));
if (val > maxVal) val = maxVal;
setValue(val);
cout << " @pin :" << pin << ", setRatio = " << ((val - minVal) * 100.0) / length << endl;
}
void Servo::operator--(int)
{
uint16_t val = value - (length * (rate / 100));
if (val < minVal) val = minVal;
setValue(val);
cout << " @pin :" << pin << ", setRatio = " << ((val - minVal) * 100.0) / length << endl;
}
Wheel::Wheel()
{
}
Wheel::Wheel(PCA9685 pca_, int leftPin, int rightPin)
{
if (wiringPiSetup() == -1)
{
cout << "wiringPiSetup FAIL!!" << '\n';
exit(1);
}
pinMode(0, OUTPUT); //BCM_17,
pinMode(2, OUTPUT); //BCM_27,
backwardFlag = false;
board = pca_;
left = leftPin;
right = rightPin;
maxVal = 4000;
minVal = 0;
length = maxVal - minVal;
rate = 10;
}
void Wheel::go(double speed)
{
digitalWrite(0, 0);
digitalWrite(2, 0);
if (speed > 100) speed = 100;
if (speed < 0) speed = 0;
uint16_t val = length * (speed / 100);
board.set_pwm(left, 0, val);
board.set_pwm(right, 0, val);
}
void Wheel::stop()
{
board.set_pwm(left, 0, 0);
board.set_pwm(right, 0, 0);
}
void Wheel::backward(double speed)
{
digitalWrite(0, 1);
digitalWrite(2, 1);
if (speed > 100) speed = 100;
if (speed < 0) speed = 0;
uint16_t val = length * (speed / 100);
board.set_pwm(left, 0, val);
board.set_pwm(right, 0, val);
}
UltraSonic::UltraSonic(int trigerPin, int echoPin)
{
TRIGPIN = trigerPin;
ECHOPIN = echoPin;
if (wiringPiSetup() == -1)
{
cout << "wiringPiSetup FAIL!!" << '\n';
exit(1);
}
pinMode(TRIGPIN, OUTPUT);
pinMode(ECHOPIN, INPUT);
}
double UltraSonic::distance()
{
double distance, start, stop;
digitalWrite(TRIGPIN, 0);
delayMicroseconds(2);
digitalWrite(TRIGPIN, 1);
delayMicroseconds(20);
digitalWrite(TRIGPIN, 0);
while (digitalRead(ECHOPIN) == 0);
start = micros();
while (digitalRead(ECHOPIN) == 1);
stop = micros();
distance = (stop - start) / 58;
return distance;
}
PicarLED::PicarLED(int ledPin)
{
LEDPIN = ledPin;
if (wiringPiSetup() == -1)
{
cout << "wiringPiSetup FAIL!!" << '\n';
exit(1);
}
pinMode(LEDPIN, OUTPUT);
}
void PicarLED::on()
{
digitalWrite(LEDPIN, 1);
}
void PicarLED::off()
{
digitalWrite(LEDPIN, 0);
}
ManualMode::ManualMode(PCA9685 pca_, double spd)
{
M_steering = Servo(pca_, Steering);
M_cam_tilt = Servo(pca_, Tilt);
M_cam_pan = Servo(pca_, Pan);
M_DCmotor = Wheel(pca_, LeftWheel, RightWheel);
speed = spd; //basic speed
}
void ManualMode::input(int key_)
{
switch (key_)
{
case 'a': //steering left
M_steering--;
break;
case 'd': //steering right
M_steering++;
break;
case 's': //stop
M_DCmotor.stop();
break;
case 'w': //go and speed up
M_DCmotor.go(speed);
break;
case 'x': //go backward
M_DCmotor.backward(speed);
break;
case 'j': //cam left
M_cam_pan++;
break;
case 'l': //cam right
M_cam_pan--;
break;
case 'i': //cam up
M_cam_tilt++;
break;
case 'k': //cam down
M_cam_tilt--;
break;
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
speed = (key_ - '0') * 10;
cout << "current speed = " << speed << endl;
break;
case -1:
cout << "wait input & camera out" << endl;
break;
default:
cout << "wrong key input." << endl;
break;
}
}
void ManualMode::guide()
{
cout << "---------------------[key setting]------------------" << endl;
cout << " w : go & speed up | i : up" << endl;
cout << " a s d : left, stop,right | j l : left, right" << endl;
cout << " x : backward | k : down" << endl;
cout << " (move) | (cam)" << endl;
cout << " '0' is exit. |" << endl;
cout << "----------------------------------------------------" << endl;
cout << " 1, 2, 3, 4, 5, 6, 7, 8, 9 = speed setting" << endl;
cout << "----------------------------------------------------" << endl;
}
void allServoReset(PCA9685 pca_)
{
Servo a_steering(pca_, Steering);
Servo a_cam_tilt(pca_, Tilt);
Servo a_cam_pan(pca_, Pan);
a_steering.resetCenter();
a_cam_tilt.resetCenter();
a_cam_pan.resetCenter();
}