-
Notifications
You must be signed in to change notification settings - Fork 0
/
pololu-pid.ino
206 lines (156 loc) · 4.31 KB
/
pololu-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
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
/*
Name: pololu_pid.ino
Created: 4/3/2022 4:00:33 PM
Author: Scott Scherzer
*/
#include <Pololu3piPlus32U4.h>
#include <Servo.h>
#include "UltrasonicController.h"
#include "PIDController.h"
using namespace Pololu3piPlus32U4;
// pin assignments
const uint8_t US_TRIG_PIN = 22, US_ECHO_PIN = 21, SERVO_PIN = 20;
constexpr int POS_LEN = 7;
const double SETPOINT = 15.0;
// TODO move to US controller
// us reading data and pid limits
const double US_MIN_DISTANCE = 2.0, MAX_DISTANCE = 200.0;
/* hardware init */
Servo headServo;
Motors motors;
Ultrasonic us(false, 10ul, US_TRIG_PIN, US_ECHO_PIN);
PID pid(false);
/* head servo data */
// milliseconds interval for scheduler
const unsigned long SERVO_PERIOD = 50ul;
// timers for servo
unsigned long servoTimer1, servoTimer2;
// angle servo is currently facing
int servoAngle = 90;
// index of HEAD_POSITIONS array
int servoPos = 3;
// debug switch
bool bDebugHeadServo = false;
// logic for servo sweeping right or left
bool sweepingClockwise = true;
// logic to stop US from sending pings if US is moving
bool servoMoving = false;
// legal head positions (angles) servo can point
const int HEAD_POSITIONS[POS_LEN] = { 45, 60, 75, 90, 105, 120, 135 };
// position readings from each angle
double distances[POS_LEN] = { };
// motor data
const unsigned long MOTOR_PERIOD = 50ul;
unsigned long motorTimer1, motorTimer2;
const int MIN_SPEED = 50;
const int BASE_SPEED = 100;
const int MAX_SPEED = 150;
int leftSpeed = BASE_SPEED, rightSpeed = BASE_SPEED;
// the setup function runs once when you press reset or power the board
void setup()
{
Serial.begin(9600);
motors.flipLeftMotor(true);
motors.flipRightMotor(true);
headServo.attach(SERVO_PIN);
headServo.write(servoAngle);
pid.KP = 4.0;
pid.KI = 0.5;
pid.KD = 2.0;
delay(1000);
}
// the loop function runs over and over again until power down or reset
void loop()
{
setServo();
readUltrasonic();
setMotorsSpeeds();
}
// util methods
int handleLimit(int value, int min, int max)
{
if (value >= max)
value = max;
else if (value <= min)
value = min;
}
void setServo()
{
servoTimer1 = millis();
// poll servo
if (servoTimer1 > servoTimer2 + SERVO_PERIOD && !servoMoving)
{
servoMoving = true;
sweepHead();
headServo.write(servoAngle);
// store last time ran
servoTimer2 = servoTimer1;
}
// allow servo to finish sweep
// TUNE wait period
else if (servoTimer1 > servoTimer2 + SERVO_PERIOD && servoMoving)
{
servoMoving = false;
servoTimer2 = servoTimer1;
if (bDebugHeadServo) headServoDebug("Head Servo");
}
}
/**
* Simple controller to move head
* reads from headPositions
* @returns void. sets servo head to a position
*/
void sweepHead()
{
// toggle direction
if (servoPos == 6 || servoPos == 0)
sweepingClockwise = !sweepingClockwise;
// start at 0 then ascend
if (sweepingClockwise)
servoPos = (7 + servoPos + 1) % 7;
// start at 6 then decend
else
servoPos = (7 + servoPos - 1) % 7;
// update the currentAngle
servoAngle = HEAD_POSITIONS[servoPos];
}
void readUltrasonic()
{
us.timer1 = millis();
//send one ping, write to correct index
if (us.timer1 > us.timer2 + us.PERIOD && !servoMoving)
{
//send ping
distances[servoPos] = us.sendPing();
if (us.bDebug) us.debug("Ultrasonic");
//store last time ran
us.timer2 = us.timer1;
}
}
void setMotorsSpeeds()
{
motorTimer1 = millis();
if (motorTimer1 > motorTimer2 + MOTOR_PERIOD)
{
// side pid corrections
pid.currentError = distances[servoPos] - SETPOINT;
pid.gain = pid.calculatePID(pid.currentError);
leftSpeed = BASE_SPEED - pid.gain;
rightSpeed = BASE_SPEED + pid.gain;
leftSpeed = handleLimit(leftSpeed, MIN_SPEED, MAX_SPEED);
rightSpeed = handleLimit(rightSpeed, MIN_SPEED, MAX_SPEED);
motors.setSpeeds(leftSpeed, rightSpeed);
if (pid.bDebug) pid.debug("side pid");
motorTimer2 = motorTimer1;
}
}
// output data to serial monitor
void headServoDebug(char label[])
{
Serial.println(label);
Serial.print(" | sweepingCW: "); Serial.print(sweepingClockwise);
Serial.print(" | angle: "); Serial.print(servoAngle);
Serial.print(" | position: "); Serial.print(servoPos);
Serial.print(" | T1: "); Serial.print(servoTimer1);
Serial.println(" | T2: "); Serial.println(servoTimer2);
}