-
Notifications
You must be signed in to change notification settings - Fork 0
/
final_odo.ino
202 lines (153 loc) · 3.5 KB
/
final_odo.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
//non lcd
// #include <ECE3.h>
//
//odo
#include <ECE3_SPEED_LCD.h>
//=-=-
#define left_nslp_pin 31 // nslp ==> awake & ready for PWM
#define left_dir_pin 29
#define left_pwm_pin 40
#define right_nslp_pin 11 // nslp ==> awake & ready for PWM
#define right_dir_pin 30
#define right_pwm_pin 39
uint16_t sensorValues[8];
// global variables
int base = 140;
float leftSpd = base;
float rightSpd = base;
int turndelay = 240;
float Kp = .025;
float Kd = .353;//.35
float Ki = 0;
float previous_error = 0;
float integral = 0;
bool lastTurnAround = false;
bool hasTurned = false;
//=-=-=-=-
void setup()
{
ECE3_Init();
pinMode(left_nslp_pin, OUTPUT);
pinMode(left_dir_pin, OUTPUT);
pinMode(left_pwm_pin, OUTPUT);
pinMode(right_nslp_pin, OUTPUT);
pinMode(right_dir_pin, OUTPUT);
pinMode(right_pwm_pin, OUTPUT);
digitalWrite(left_nslp_pin, HIGH);
digitalWrite(right_nslp_pin, HIGH);
digitalWrite(right_dir_pin, LOW);
digitalWrite(left_dir_pin, LOW);
Serial.begin(9600); // set the data rate in bits per second for serial data transmission
delay(2000);
// increaseTo(base);
}
void loop()
{
// read raw sensor values
ECE3_read_IR(sensorValues);
if (sensorTurnAround() && lastTurnAround && !hasTurned)
{
performTurnAround(turndelay);
lastTurnAround = false;
hasTurned = true;
increaseTo(base);
}
if (sensorTurnAround() && lastTurnAround && hasTurned)
{
stop_car_forever();
}
lastTurnAround = sensorTurnAround();
float measuredValue = combinedValues();
float error = measuredValue;
float derivative = (error - previous_error);
integral = integral + error; // integral + error
float output = Kp * error + Ki * integral + Kd * derivative;
float leftOutput = leftSpd - output;
float rightOutput = rightSpd + output;
if (leftOutput > 255) {
leftOutput = 254;
}
if (rightOutput > 255) {
rightOutput = 254;
}
if (leftOutput < 0) {
leftOutput = 0;
}
if (rightOutput < 0) {
rightOutput = 0;
}
analogWrite(left_pwm_pin, leftOutput);
analogWrite(right_pwm_pin, rightOutput);
// Serial.println(output);
previous_error = error;
//odo
odo();
//
}
int combinedValues()
{
float newVal = 0;
float arr[8] = { -1.875, -1.75, -1.5, -1, 1, 1.5, 1.75, 1.875};
for (int i = 0; i < 8; i++)
{
newVal += arr[i] * sensorValues[i];
}
return newVal;
}
bool sensorTurnAround()
{
int val = 0;
for (int i = 0; i < 8; i++)
{
val = val + sensorValues[i];
}
if (val > 17000)
{
return true;
}
return false;
}
void performTurnAround(int delayTime)
{
decreaseToStop();
digitalWrite(right_dir_pin, LOW);
digitalWrite(left_dir_pin, HIGH);
analogWrite(left_pwm_pin, 255);
analogWrite(right_pwm_pin, 255);
delay(delayTime);
digitalWrite(right_dir_pin, LOW);
digitalWrite(left_dir_pin, LOW);
}
void increaseTo(int baseSpeed)
{
for (int speed = 0 ; speed < baseSpeed; speed += 10)
{
analogWrite(left_pwm_pin, speed);
analogWrite(right_pwm_pin, speed);
delay(20);
}
}
void odo()
{
if ((getEncoderCount_left() + getEncoderCount_right()) / 2 > 15000)
{
digitalWrite(BLUE_LED, HIGH);
}
}
void stop_car_forever()
{
decreaseToStop();
while(1){}
}
void decreaseToStop()
{
digitalWrite(right_dir_pin, HIGH);
digitalWrite(left_dir_pin, HIGH);
analogWrite(left_pwm_pin, 30);
analogWrite(right_pwm_pin, 30);
delay(390);
digitalWrite(right_dir_pin, LOW);
digitalWrite(left_dir_pin, LOW);
analogWrite(left_pwm_pin, 0);
analogWrite(right_pwm_pin, 0);
}