-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcar_code.ino
177 lines (144 loc) · 4.9 KB
/
car_code.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
//* Project Name: car_code
//* Car Name: Falcon 10
//* Description: Controls the movement of the car by using 2 ultrasonic sensors, 2 DC motors and LM298N
//* Principle used: PID
//* Made by Students in Damascus University, CAE Department: Ashraf AlAssi, Ibrahim AlSabagh, Amir AlAwa and Mahmoud AlHariri
//==========================================
// https://playground.arduino.cc/Code/PIDLibrary/
#include <PID_v1.h>
// https://github.com/Martinsos/arduino-lib-hc-sr04
#include <HCSR04.h>
#include <DCMotor.h>
#include <Helper.h>
// PINs used
#define PIN_VCC_FRONT 2
#define PIN_TRIGGER_FRONT 3
#define PIN_ECHO_FRONT 4
#define PIN_VCC_REAR 5
#define PIN_TRIGGER_REAR 6
#define PIN_ECHO_REAR 7
#define PIN_IN1 8
#define PIN_IN2 9
#define PIN_EN 11
#define PIN_IN3 12
#define PIN_IN4 13
// PIDs
double SetpointFront; // distance in cm
double InputFront, OutputFront;
double KpFront = 2, KiFront = 1e-3, KdFront = 0;
PID pidFront(&InputFront, &OutputFront, &SetpointFront, KpFront, KiFront, KdFront, DIRECT);
double SetpointRear; // distance in cm
double InputRear, OutputRear;
double KpRear = 2, KiRear = 1e-3, KdRear = 0;
PID pidRear(&InputRear, &OutputRear, &SetpointRear, KpRear, KiRear, KdRear, DIRECT);
// Ultrasonic sensors
UltraSonicDistanceSensor frontSensor(PIN_TRIGGER_FRONT, PIN_ECHO_FRONT);
UltraSonicDistanceSensor rearSensor(PIN_TRIGGER_REAR, PIN_ECHO_REAR);
// DC Motors
bool isMovingForward = true;
DCMotor rightMotor(PIN_IN1, PIN_IN2, PIN_EN, &isMovingForward);
DCMotor leftMotor(PIN_IN3, PIN_IN4, PIN_EN, &isMovingForward);
// Helper class
Helper helper;
void setup()
{
// Set baud rate
Serial.begin(9600);
// Initialize the inputs
InputFront = 0;
InputRear = 0;
// Setting the preferred distance in cm
SetpointFront = 15;
SetpointRear = 15;
// Turning the PIDs on
pidFront.SetOutputLimits(-255, 255);
pidFront.SetSampleTime(50);
pidFront.SetMode(AUTOMATIC);
pidRear.SetOutputLimits(-255, 255);
pidRear.SetSampleTime(50);
pidRear.SetMode(AUTOMATIC);
// Initializing sensors Vcc pins
uint8_t sensorsVccPins[] = {PIN_VCC_FRONT, PIN_VCC_REAR};
helper.pinModes(sensorsVccPins, OUTPUT);
helper.digitalWrites(sensorsVccPins, HIGH);
// This delay is just for safety so the car won't start immediately
delay(5 * 1000);
}
// Declaring some variables to take an average for the measured distance
double avrFront = 0;
double avrRear = 0;
const int sampleNum = 10;
int counter = 0;
// A unified output
double Output = 0;
void loop()
{
// Calculates the distance in cm at a given temperature (19.307°C by default)
InputFront = frontSensor.measureDistanceCm(27);
InputRear = rearSensor.measureDistanceCm(27);
// Make sure the distance is positive
if (InputRear <= 0)
InputRear = 0;
if (InputFront <= 0)
InputFront = 0;
// Summing the inputs to calculate the average distance
avrFront += InputFront;
avrRear += InputRear;
counter++;
delay(1);
// If counter equals sampleNum, enter the loop and start calculating the correct response. Otherwise continue to the next measurement
if (counter == sampleNum)
{
// Calculating the average and assigning it to inputs
avrFront /= sampleNum;
avrRear /= sampleNum;
InputFront = avrFront;
InputRear = avrRear;
avrFront = avrRear = counter = 0;
// Computing the correct response (output) as a PWM to send it to the dc motors
pidFront.Compute();
pidRear.Compute();
// Make sure the output is positive. Output is negative when the error is negative, i.e., the distance is larger than the setpoint
OutputRear = abs(OutputRear);
OutputFront = abs(OutputFront);
// Processing...
// Stops if distance is too small
if (InputFront < SetpointFront && InputRear < SetpointRear)
OutputFront = OutputRear = Output = 0;
// Move towards the rear (backward)
else if (InputFront < SetpointFront && InputRear > SetpointRear)
{
isMovingForward = false;
Output = OutputRear;
OutputFront = 0;
}
// Move towards the front (forward)
else if (InputFront > SetpointFront && InputRear < SetpointRear)
{
isMovingForward = true;
Output = OutputFront;
OutputRear = 0;
}
// Change the output according to the last output used
else if (InputFront > SetpointFront && InputRear > SetpointRear)
{
if (isMovingForward)
{
Output = OutputFront;
OutputRear = 0;
}
else
{
Output = OutputRear;
OutputFront = 0;
}
}
rightMotor.move(Output);
leftMotor.move(Output);
// Plotting input and output for debugging purposes
helper.plot("InputFront", InputFront);
helper.plot("OutputFront", OutputFront);
helper.plot("InputRear", InputRear);
helper.plot("OutputRear", OutputRear, true);
}
}