-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhomework_22P06.ino
181 lines (143 loc) · 4.67 KB
/
homework_22P06.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
#include <Servo.h>
/////////////////////////////
// Configurable parameters //
/////////////////////////////
// Arduino pin assignment
#define PIN_LED 9
#define PIN_SERVO 10
#define PIN_IR A0
// Framework setting
#define _DIST_TARGET 255
#define _DIST_MIN 100
#define _DIST_MAX 430
// Distance sensor
#define _DIST_ALPHA 0.3
// Servo range
#define _DUTY_MIN 1000
#define _DUTY_NEU 1480
#define _DUTY_MAX 2000
// Servo speed control
#define _SERVO_ANGLE 30
#define _SERVO_SPEED 80
// Event periods
#define _INTERVAL_DIST 20
#define _INTERVAL_SERVO 20
#define _INTERVAL_SERIAL 100
// PID parameters
#define _KP 1.5
//unit: mm
#define _CALI_MIN 69
#define _CALI_MAX 290
//////////////////////
// global variables //
//////////////////////
// Servo instance
Servo myservo;
// Distance sensor
float dist_target; // location to send the ball
float dist_raw, dist_ema;
// Event periods
unsigned long last_sampling_time_dist, last_sampling_time_servo, last_sampling_time_serial;
bool event_dist, event_servo, event_serial;
// Servo speed control
int duty_chg_per_interval;
int duty_target, duty_curr;
// PID variables
float error_curr, error_prev, control, pterm, dterm, iterm;
void setup() {
// initialize GPIO pins for LED and attach servo
myservo.attach(PIN_SERVO);
// move servo to neutral position
//myservo.writeMicroseconds(_DUTY_NEU);
pinMode(PIN_LED, OUTPUT);
// initialize global variables
last_sampling_time_dist = 0;
last_sampling_time_servo = 0;
last_sampling_time_serial = 0;
event_dist = false;
event_servo = false;
event_serial = false;
// initialize serial port
Serial.begin(57600);
// convert angle speed into duty change per interval.
duty_chg_per_interval = (float)(_DUTY_MAX - _DUTY_MIN) * _SERVO_SPEED / 180 * _INTERVAL_SERVO / 1000;
}
void loop() {
/////////////////////
// Event generator //
/////////////////////
// [1615] 거리 측정 주기가 되었는지 검사
if (millis() >= last_sampling_time_dist + _INTERVAL_DIST)
event_dist = true;
// [1615] 서보 제어 주기가 되었는지 검사
if (millis() >= last_sampling_time_servo + _INTERVAL_SERVO)
event_servo= true;
// [1615] Serial 제어 주기가 되었는지 검사
if (millis() >= 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_raw = ir_distance_filtered();
dist_raw = ir_distance_filtered();
// PID control logic
error_curr = _DIST_TARGET - dist_raw;
pterm = error_curr * _KP;
control = pterm;
// duty_target = f(duty_neutral, control)
duty_target = _DUTY_NEU + control;
// keep duty_target value within the range of [_DUTY_MIN, _DUTY_MAX]
if (duty_target < _DUTY_MIN) duty_target = _DUTY_MIN;
else if (duty_target > _DUTY_MAX) duty_target = _DUTY_MAX;
//duty_target = min(max(duty_target, _DUTY_MIN), _DUTY_MAX); // [1615]
last_sampling_time_dist += _INTERVAL_DIST;
}
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;
if(duty_curr > duty_target) duty_curr = duty_target;
}
else {
duty_curr -= duty_chg_per_interval;
if(duty_curr < duty_target) duty_curr = duty_target;
}
// update servo position
myservo.writeMicroseconds(duty_curr);
last_sampling_time_servo += _INTERVAL_SERVO;
}
if(event_serial) {
event_serial = false;
Serial.print("dist_ir:");
Serial.print(dist_raw);
Serial.print(",pterm:");
Serial.print(map(pterm,-1000,1000,510,610));
Serial.print(",duty_target:");
Serial.print(map(duty_target,1000,2000,410,510));
Serial.print(",duty_curr:");
Serial.print(map(duty_curr,1000,2000,410,510));
Serial.println(",Min:100,Low:200,dist_target:255,High:310,Max:410");
last_sampling_time_serial += _INTERVAL_SERIAL;
}
}
float ir_distance(void){ // return value unit: mm
float val, result;
float volt = float(analogRead(PIN_IR));
val = ((6762.0/(volt-9.0))-4.0) * 10.0;
result = 100 + 300.0 / (_CALI_MAX - _CALI_MIN) * (val - _CALI_MIN);
return result;
}
float ir_distance_filtered(void){ // return value unit: mm
//return ir_distance(); // for now, just use ir_distance() without noise filter.
static float val = _DIST_TARGET; // [3088]
static float dist_ema = 0; // [3088]
float raw = ir_distance(); // [3088]
if (raw >= _DIST_MIN && raw <= _DIST_MAX) // [3088]
val = raw; // [3088]
dist_ema = _DIST_ALPHA * raw + (1 - _DIST_ALPHA) * dist_ema; // [3088]
return dist_ema; // [3088]
}