-
Notifications
You must be signed in to change notification settings - Fork 0
/
laser_offset_PIDRTtune.ino
98 lines (75 loc) · 2.09 KB
/
laser_offset_PIDRTtune.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
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BMP280.h>
#include <Servo.h>
#include <Encoder.h>
#include <MPU6050_tockn.h>
#include <PID_v1.h>
// Constants
const int opticalSensorPin = 2;
const int servoXPin = 9;
const int servoYPin = 10;
const int threshold = 100;
// Encoder pins for X and Y axes
const int encoderXPins[] = {3, 4};
const int encoderYPins[] = {5, 6};
// Variables
int opticalSensorValue;
int servoXPosition = 90;
int servoYPosition = 90;
// Target position and control gain
float targetX = 0;
float targetY = 0;
// Servo objects
Servo servoX;
Servo servoY;
// BMP280 object
Adafruit_BMP280 bmp280;
// Encoders for X and Y axes
Encoder encoderX(encoderXPins[0], encoderXPins[1]);
Encoder encoderY(encoderYPins[0], encoderYPins[1]);
// MPU6050 object
MPU6050 mpu6050(Wire);
// PID Controllers
double inputX, inputY, outputX, outputY;
double setpointX = 0, setpointY = 0;
double kP = 1, kI = 1, kD = 1;
PID pidX(&inputX, &outputX, &setpointX, kP, kI, kD, DIRECT);
PID pidY(&inputY, &outputY, &setpointY, kP, kI, kD, DIRECT);
void setup() {
Serial.begin(9600);
if (!bmp280.begin()) {
Serial.println("Could not find a valid BMP280 sensor, check wiring!");
while (1);
}
pinMode(opticalSensorPin, INPUT);
servoX.attach(servoXPin);
servoY.attach(servoYPin);
servoX.write(servoXPosition);
servoY.write(servoYPosition);
// Initialize MPU6050
Wire.begin();
mpu6050.begin();
mpu6050.calcGyroOffsets(true);
// Initialize PID controllers
pidX.SetMode(AUTOMATIC);
pidY.SetMode(AUTOMATIC);
}
void loop() {
opticalSensorValue = digitalRead(opticalSensorPin);
float currentHeight = getCurrentHeight();
// If the canister is aligned with the laser, adjust the control system
if (opticalSensorValue == HIGH) {
alignWithLaser();
}
// If the canister has traversed the predetermined point in the descending trajectory
if (currentHeight < threshold) {
moveToOffsetIndicator();
}
// Check for and process serial commands to adjust PID tuning parameters
if (Serial.available()) {
processSerialCommand();
}
delay(50);
}
void alignWithL