-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTestPID2.ino
287 lines (263 loc) · 9.25 KB
/
TestPID2.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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/*
* File name: PID_LF_example
*
* Hardware requirements: an Arduino Pro Mini
* a QTR-8RC Reflectance Sensor Array
* a DRV8835 Dual Motor Driver Carrier
*
* Description: The basic PID control system implemented with
* the line follower with the specified hardware.
* The robot can follow a black line on a white surface
* (or vice versa).
* Related Document: See the written documentation or the LF video from
* Bot Reboot.
*
* Author: Bot Reboot
* modifié par Noukypop01
*/
#include <QTRSensors.h> //Make sure to install the library
/*************************************************************************
* Sensor Array object initialisation
*************************************************************************/
QTRSensors qtr;
const uint8_t SensorCount = 8;
uint16_t sensorValues[SensorCount];
/*************************************************************************
* PID control system variables
*************************************************************************/
float Kp = 0.07; //related to the proportional control term;
//change the value by trial-and-error (ex: 0.07).
float Ki = 0; //related to the integral control term;
//change the value by trial-and-error (ex: 0.0008).
float Kd = 0.9; //related to the derivative control term;
//change the value by trial-and-error (ex: 0.6).
int P;
int I;
int D;
/*************************************************************************
* Global variables
*************************************************************************/
int lastError = 0;
boolean onoff = false;
/*************************************************************************
* Motor speed variables (choose between 0 - no speed, and 255 - maximum speed)
*************************************************************************/
const double rapport = 11/10;
const uint8_t maxspeeda = 150*rapport;
const uint8_t maxspeedb = 150;
const uint8_t basespeeda = 100*rapport;
const uint8_t basespeedb = 100;
/*************************************************************************
* DRV8835 GPIO pins declaration
*************************************************************************/
#define aphase 13 // partie gauche
#define aenbl 11
#define freinMD 8
#define bphase 12 // partie droite
#define benbl 3
#define freinMG 9
int ledMauvais = 44;
int ledBon = 42;
int ledTresBon = 45;
int ledExellant = 43;
int ledcalibrage = 10;
/*************************************************************************
* Buttons pins declaration
*************************************************************************/
int buttoncalibrate = 7; //or pin A3
int buttonstart = 6;
/*************************************************************************
* Function Name: setup
**************************************************************************
* Summary:
* This is the setup function for the Arduino board. It first sets up the
* pins for the sensor array and the motor driver. Then the user needs to
* slide the sensors across the line for 10 seconds as they need to be
* calibrated.
*
* Parameters:
* none
*
* Returns:
* none
*************************************************************************/
void setup() {
qtr.setTypeRC();
qtr.setSensorPins((const uint8_t[]){46, 47, 48, 49, 50, 51, 52, 53}, SensorCount);
qtr.setEmitterPin(7);//LEDON PIN
pinMode(aphase, OUTPUT);
pinMode(aenbl, OUTPUT);
pinMode(bphase, OUTPUT);
pinMode(benbl, OUTPUT);
pinMode(ledMauvais, OUTPUT);
pinMode(ledBon, OUTPUT);
pinMode(ledTresBon, OUTPUT);
pinMode(ledExellant, OUTPUT);
pinMode(ledcalibrage, OUTPUT);
pinMode(buttoncalibrate, INPUT_PULLUP);
pinMode(buttonstart, INPUT_PULLUP);
//digitalWrite(mode, HIGH); //one of the two control interfaces
//(simplified drive/brake operation)
delay(500);
pinMode(5, OUTPUT);
boolean Ok = false;
while (Ok == false) { // the main function won't start until the robot is calibrated
if(digitalRead(buttoncalibrate) == LOW) {
digitalWrite(ledcalibrage, HIGH);
calibration(); //calibrate the robot for 10 seconds
Ok = true;
}
}
digitalWrite(ledcalibrage, LOW);
forward_brake(0, 0); //stop the motors
}
/*************************************************************************
* Function Name: calibration
**************************************************************************
* Summary:
* This is the calibration function for the QTR-8RC Reflectance Sensor Array.
* The function calls the method 'qtr.calibrate()' offered by the imported
* library. For approx. 10 seconds, each of the 8 sensors will calibrate with
* readings from the track.
*
* Parameters:
* none
*
* Returns:
* none
*************************************************************************/
void calibration() {
digitalWrite(LED_BUILTIN, HIGH);
for (uint16_t i = 0; i < 400; i++)
{
qtr.calibrate();
}
digitalWrite(LED_BUILTIN, LOW);
}
/*************************************************************************
* Function Name: loop
**************************************************************************
* Summary:
* This is the main function of this application. When the start button is
* pressed, the robot will toggle between following the track and stopping.
* When following the track, the function calls the PID control method.
*
* Parameters:
* none
*
* Returns:
* none
*************************************************************************/
void loop() {
if(digitalRead(buttonstart) == LOW) {
onoff =! onoff;
if(onoff = true) {
delay(1000);//a delay when the robot starts
}
else {
delay(50);
}
}
if (onoff == true) {
PID_control();
}
else {
forward_brake(0,0); //stop the motors
}
}
/*************************************************************************
* Function Name: forward_brake
**************************************************************************
* Summary:
* This is the control interface function of the motor driver. As shown in
* the Pololu's documentation of the DRV8835 motor driver, when the MODE is
* equal to 1 (the pin is set to output HIGH), the robot will go forward at
* the given speed specified by the parameters. The phase pins control the
* direction of the spin, and the enbl pins control the speed of the motor.
*
* A warning though, depending on the wiring, you might need to change the
* aphase and bphase from LOW to HIGH, in order for the robot to spin forward.
*
* Parameters:
* int posa: int value from 0 to 255; controls the speed of the motor A.
* int posb: int value from 0 to 255; controls the speed of the motor B.
*
* Returns:
* none
*************************************************************************/
void forward_brake(int posa, int posb) {
//set the appropriate values for aphase and bphase so that the robot goes straight
digitalWrite(aphase, HIGH);
digitalWrite(bphase, HIGH);
analogWrite(aenbl, posa);
analogWrite(benbl, posb);
}
/*************************************************************************
* Function Name: PID_control
**************************************************************************
* Summary:
* This is the function of the PID control system. The distinguishing
* feature of the PID controller is the ability to use the three control
* terms of proportional, integral and derivative influence on the controller
* output to apply accurate and optimal control. This correction is applied to
* the speed of the motors, which should be in range of the interval [0, max_speed],
* max_speed <= 255.
*
* Parameters:
* none
*
* Returns:
* none
*************************************************************************/
void PID_control() {
uint16_t position = qtr.readLineBlack(sensorValues); //read the current position
int error = 3500 - position; //3500 is the ideal position (the centre)
testPID(position);
P = error;
I = I + error;
D = error - lastError;
lastError = error;
int motorspeed = P*Kp + I*Ki + D*Kd; //calculate the correction
//needed to be applied to the speed
int motorspeeda = basespeeda + motorspeed;
int motorspeedb = basespeedb - motorspeed;
if (motorspeeda > maxspeeda) {
motorspeeda = maxspeeda;
}
if (motorspeedb > maxspeedb) {
motorspeedb = maxspeedb;
}
if (motorspeeda < 0) {
motorspeeda = 0;
}
if (motorspeedb < 0) {
motorspeedb = 0;
}
forward_brake(motorspeeda, motorspeedb);
}
void testPID(uint16_t pos) {
if (pos == 3500){
digitalWrite(ledExellant, HIGH);
}
else {
digitalWrite(ledExellant, LOW);
}
if (pos >= 3000 && pos <= 4000){
digitalWrite(ledTresBon, HIGH);
}
else {
digitalWrite(ledTresBon, LOW);
}
if (pos >= 2500 && pos <= 4500){
digitalWrite(ledBon, HIGH);
}
else {
digitalWrite(ledBon, LOW);
}
if (pos >= 2000 && pos <= 5000){
digitalWrite(ledMauvais, HIGH);
}
else {
digitalWrite(ledMauvais, LOW);
}
}