-
Notifications
You must be signed in to change notification settings - Fork 0
/
28_example_1.ino
273 lines (206 loc) ยท 8.38 KB
/
28_example_1.ino
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
#include <Servo.h> //[3104] Servo header File include
/////////////////////////////
// Configurable parameters //
/////////////////////////////
// Arduino pin assignment
#define PIN_LED 9 // [3110] 9๋ฒํ LED ์ฐ๊ฒฐ
#define PIN_SERVO 10 // [3110] 10๋ฒํ ์๋ณด ์ฐ๊ฒฐ
#define PIN_IR A0 //[3104] ์ ์ธ์ ๊ฑฐ๋ฆฌ์ผ์ PIN - Analog0 ์ ์
// Framework setting
#define _DIST_TARGET 255 //[3104] ํ๊ตฌ๊ณต์ ์์น ์ํฌ ๋ชฉํ
#define _DIST_MIN 100 //[3117] ๊ฑฐ๋ฆฌ ์ต์๊ฐ
#define _DIST_MAX 400 //[3117] ๊ฑฐ๋ฆฌ ์ต๋๊ฐ
// Servo range
#define _DUTY_MIN 1880 //[3100] ์ต์ ์๋ณด ์์น
#define _DUTY_NEU 1520 //[3100] ์ค๋ฆฝ ์๋ณด ์์น
#define _DUTY_MAX 1280 //[3100] ์ต๋ ์๋ณด ์์น
// Servo speed control
#define _SERVO_ANGLE 50
#define _SERVO_SPEED 75
// Event periods
#define _INTERVAL_DIST 20 //[3099] ๊ฐ event ์ฌ์ด์ ์ง์ ํ ์๊ฐ ๊ฐ๊ฒฉ
#define _INTERVAL_SERVO 20
#define _INTERVAL_SERIAL 100
// PID parameters
#define _KP 0.75
#define _KI 0.075
#define _KD 100
#define _ITERM_MAX 500 // iterm ์ต๋๊ฐ
// distance sensor
#define DELAY_MICROS 1500 // under_noise_filter ๋ delay time
#define _DIST_ALPHA 0.35
// Duty value ema
#define _SENSOR_ALPHA 0.6
//////////////////////
// global variables //
//////////////////////
// Servo instance
Servo myservo;
// Distance sensor
float dist_target=_DIST_TARGET; // location to send the ball
float dist_raw, dist_ema, dist_prev; //[1928] ์ธก์ ๋ ๊ฐ๊ณผ ema ํํฐ๋ฅผ ์ ์ฉํ ๊ฐ
// Event periods
unsigned long last_sampling_time_dist, last_sampling_time_servo, last_sampling_time_serial;
//[3104] ๊ฐ event์ ์งํ ์๊ฐ ์ ์ฅ ๋ณ์
bool event_dist, event_servo, event_serial;
//[3104] ๊ฐ event์ ์๊ฐ์ฒดํฌ๋ฅผ ์ํ ๋ณ์ (ex_20์ด ์ฃผ๊ธฐ >> 0์ด(True,์์), 10์ด(False), 20์ด(True))
// Servo speed control
int duty_chg_per_interval_up, duty_chg_per_interval_down; // [3116] ์ฃผ๊ธฐ ๋น ์๋ณด duty๊ฐ ๋ณํ๋
int duty_chg_per_interval_up_max, duty_chg_per_interval_down_max;
int duty_target, duty_curr; //[1928] ๋ชฉํ ์์น์ ํ์ฌ ์์น
// PID variables
float error_curr, error_prev, control, pterm, dterm, iterm;
//error_curr: ํ์ฌ ์ธก์ ๊ฐ๊ณผ ๋ชฉํ๊ฐ์ ์ฐจ์ด
//error_prev: ์ง์ ์ ๊ตฌํ ์ฐจ์ด๋ก, P์ ์ด์์๋ ์ฌ์ฉํ์ง ์์ ๊ฒ์
//control: PID์ ์ด์ ๊ฒฐ๊ณผ๋ก ์ป์ ์ ์ด๊ฐ
//pterm: Proportional term, ํ์ฌ ์ํ์ error๊ฐ์ผ๋ก๋ถํฐ ์ป์ Proportional gain์ ์ ์ฅํ๋ ๋ณ์
// [3099]
const float coE[] = {-0.0000004, 0.0005260, 0.8327958, 35.6473257};
float samples_num = 3;
void setup() {
// initialize GPIO pins for LED and attach servo
myservo.attach(PIN_SERVO); // attach servo
pinMode(PIN_LED,OUTPUT); // initialize GPIO pins
// initialize global variables
event_dist = event_servo = event_serial = true;
pterm = iterm = dterm = 0;
// move servo to neutral position
//myservo.writeMicroseconds(_DUTY_NEU);
duty_curr = _DUTY_NEU;
error_curr = error_prev = 0;
// initialize serial port
Serial.begin(57600);
// convert angle speed into duty change per interval.
// ์ต๋ ์ต์ ๊ฐ ๋ถ๊ท ํ์ด ์์ด, ๊ฐ๊ฐ interval์ ๊ตฌํ์ต๋๋ค.
duty_chg_per_interval_up = duty_chg_per_interval_up_max =
(_DUTY_NEU - _DUTY_MAX) * ((float)_SERVO_SPEED / _SERVO_ANGLE * 2 )
* 1.4 * (_INTERVAL_SERVO / 1000.0);
duty_chg_per_interval_down = duty_chg_per_interval_down_max =
(_DUTY_MIN - _DUTY_NEU) * ((float)_SERVO_SPEED / _SERVO_ANGLE * 2 )
* (_INTERVAL_SERVO / 1000.0);
}
void loop() {
/////////////////////
// Event generator //
/////////////////////
unsigned long time_curr = millis();
if(time_curr >= last_sampling_time_dist + _INTERVAL_DIST){
last_sampling_time_dist += _INTERVAL_DIST;
event_dist = true;
}
if(time_curr >= last_sampling_time_servo + _INTERVAL_SERVO ){
last_sampling_time_servo += _INTERVAL_SERVO;
event_servo = true;
}
if(time_curr >= last_sampling_time_serial + _INTERVAL_SERIAL ){
last_sampling_time_serial += _INTERVAL_SERIAL;
event_serial = true;
}
////////////////////
// Event handlers //
////////////////////
if(event_dist) {
event_dist = false;
// get a distance reading from the distance sensor
dist_ema = ir_distance_filtered(); // [3099] dist_ema?
// PID control logic
error_curr = dist_ema - _DIST_TARGET;
pterm = _KP * error_curr; // [3099]
dterm = _KD * (error_curr - error_prev);
//===============iterm ์ค์ ===============
// 244~264mm : iterm += _KI*control
// 245, 265 : iterm /= 4
// 235~244, 266~275mm : iterm += 0.5*_KI*control
// ~234, 276~ : iterm = 0
//=======================================
if (-10<error_curr & error_curr<10)iterm += _KI * error_curr;
else if ((int)error_curr == 10 | (int)error_curr == -10) iterm /= 4;
else if (-20<=error_curr & error_curr<=20) iterm += 0.5 * _KI * error_curr;
else iterm = 0;
// keep iterm within the range of [-_ITERM_MAX, _ITERM_MAX]
if (iterm>=_ITERM_MAX) iterm = _ITERM_MAX;
else if (iterm<= -_ITERM_MAX) iterm = -_ITERM_MAX;
control = pterm + iterm + dterm;
duty_target = sensor_filtered(_DUTY_NEU - control); // ์ ์ฝ๋ duty : ์ต๋<์ต์ ์ฌ์ '-' ํ์ต๋๋ค.
// keep duty_target value within the range of [_DUTY_MIN, _DUTY_MAX]
if(duty_target < _DUTY_MAX) duty_target = _DUTY_MAX;
else if(duty_target > _DUTY_MIN) duty_target = _DUTY_MIN;
error_prev = error_curr;
}
if(event_servo) {
event_servo=false;
// adjust duty_curr toward duty_target by duty_chg_per_interval
if(duty_target>duty_curr) {
duty_curr += duty_chg_per_interval_down;
if(duty_target < duty_curr)duty_curr = duty_target;
}
else if (duty_target<duty_curr){
duty_curr -= duty_chg_per_interval_up;
if(duty_target > duty_curr) duty_curr = duty_target;
}
// update servo position
myservo.writeMicroseconds(duty_curr);
}
if(event_serial) {
event_serial = false; //[3117]
// dterm = _KD * (error_curr - error_prev)
// keep dterm within the range of [0, 800]
if (map(dterm,-1000,1000,510,610)<0 | map(dterm,-1000,1000,510,610)>800) dterm = 0;
// ์๋ ์ถ๋ ฅ๋ฌธ์ ์์ ์์ด ๋ชจ๋ ๊ทธ๋๋ก ์ฌ์ฉํ๊ธฐ ๋ฐ๋๋๋ค.
Serial.print("IR:");
Serial.print(dist_ema);
Serial.print(",T:");
Serial.print(dist_target);
Serial.print(",P:");
Serial.print(map(pterm,-1000,1000,510,610));
Serial.print(",D:");
Serial.print(map(dterm,-1000,1000,510,610));
Serial.print(",I:");
Serial.print(map(iterm,-1000,1000,510,610));
Serial.print(",DTT:");
Serial.print(map(duty_target,1000,2000,410,510));
Serial.print(",DTC:");
Serial.print(map(duty_curr,1000,2000,410,510));
Serial.println(",-G:245,+G:265,m:0,M:800");
}
}
float ir_distance(void){ // return value unit: mm
float val;
float volt = float(analogRead(PIN_IR));
val = ((6762.0/(volt-9.0))-4.0) * 10.0;
// ์ถํ์ค๋ ์ฝ๋. ์ ์ธ์ ์ผ์ ๊ฐ ๊ทธ๋ํ๊ฐ ์ผ์ง์ ์ด ๋๋๋ก ๋ณด์ ํด์ฃผ๋ ์ฝ๋
val = coE[0] * pow(val, 3) + coE[1] * pow(val, 2) + coE[2] * val + coE[3];
return val;
}
//[3099]
// ================
// ๋ฐ์ฐํ๋ ์ฝ๋.
// ์, ์๋๋ก ํ๋ ๊ฐ๋ค์ ์ก์์ฃผ๋ ์ฝ๋
float under_noise_filter(void){
int currReading;
int largestReading = 0;
for (int i = 0; i < samples_num; i++) {
currReading = ir_distance();
if (currReading > largestReading) { largestReading = currReading; }
// Delay a short time before taking another reading
delayMicroseconds(DELAY_MICROS);
}
return largestReading;
}
float ir_distance_filtered(void){
int currReading;
int lowestReading = 1024;
for (int i = 0; i < samples_num; i++) {
currReading = under_noise_filter();
if (currReading < lowestReading) { lowestReading = currReading; }
}
// ema ํํฐ ์ถ๊ฐ
dist_ema = _DIST_ALPHA*lowestReading + (1-_DIST_ALPHA)*dist_ema;
if (dist_ema>_DIST_MAX) dist_ema=_DIST_MAX;
return dist_ema;
}
//===================================================
// sensor value ema.
float sensor_filtered(float sensor_val){
return _SENSOR_ALPHA * sensor_val + (1 - _SENSOR_ALPHA) * duty_target;
}