-
Notifications
You must be signed in to change notification settings - Fork 2
/
motor+sensor+PID.ino
145 lines (106 loc) · 2.72 KB
/
motor+sensor+PID.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
#include <VL6180X.h>
#include <Wire.h>
VL6180X sensor;
int leftWheelB = 23; //A1
int leftWheelA = 22; //A2
int rightWheelA = 20; //B1
int rightWheelB = 21; //B2
int onboardLED = 13;
int speed;
int SenVal;
int setpoint = 100;
// Initialize PID terms
float k = -0.8;
float Kp = 1.2;
float Ki = 0;
float Kd = 0;
float error;
float last_error = 0;
float integral_error;
float differential_error;
float pTerm;
float dTerm;
float iTerm;
float motor_output;
void setup() {
// Pin configuration
pinMode(leftWheelB, OUTPUT);
pinMode(leftWheelA, OUTPUT);
pinMode(rightWheelB, OUTPUT);
pinMode(rightWheelA, OUTPUT);
pinMode(onboardLED, OUTPUT);
// Show when robot is on
digitalWrite(onboardLED, HIGH);
Serial.begin(9600);
Wire.begin();
sensor.init();
sensor.configureDefault();
sensor.writeReg(VL6180X::SYSRANGE__MAX_CONVERGENCE_TIME, 30);
sensor.writeReg16Bit(VL6180X::SYSALS__INTEGRATION_PERIOD, 50);
sensor.setTimeout(500);
sensor.stopContinuous();
delay(3000);
sensor.startInterleavedContinuous(100);
}
void setDriveSpeedForward() {
analogWrite(leftWheelA, speed);
analogWrite(leftWheelB, 0);
analogWrite(rightWheelA, speed);
analogWrite(rightWheelB, 0);
}
void setDriveSpeedBackward() {
speed = -1*speed;
analogWrite(leftWheelA, 0);
analogWrite(leftWheelB, speed);
analogWrite(rightWheelA, 0);
analogWrite(rightWheelB, speed);
}
/*void setDriveSpeed(int reading) {
// Going forward
if (reading > thres){
analogWrite(leftWheelA, speed);
analogWrite(leftWheelB, 0);
analogWrite(rightWheelA, speed);
analogWrite(rightWheelB, 0);
}
// Going backward
else if (reading < thres){
analogWrite(leftWheelA, 0);
analogWrite(leftWheelB, speed);
analogWrite(rightWheelA, 0);
analogWrite(rightWheelB, speed);
}
}*/
void loop() {
SenVal = sensor.readRangeContinuousMillimeters();
Serial.println(SenVal);
Serial.print(" ");
// PID
error = setpoint - SenVal; //Serial.print(error);
pTerm = error * Kp;
differential_error = error - last_error;
dTerm = differential_error * Kd;
last_error = error;
integral_error = integral_error + error;
iTerm = integral_error * Ki;
motor_output = k * (pTerm + dTerm + iTerm);
Serial.print(" ");
Serial.print(motor_output);
Serial.println();
if (motor_output > 255) {
motor_output = 255;
}
if (motor_output < -255){
motor_output = -255;
}
speed = motor_output;
// Drive
if (motor_output > 0) {
setDriveSpeedForward();
}
else if (motor_output < 0) {
setDriveSpeedBackward();
}
delay(20);
// Serial Monitor
}