-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSelf-balancing.ino
131 lines (107 loc) · 3.37 KB
/
Self-balancing.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
#include "Wire.h"
#include "I2Cdev.h"
#include "MPU6050.h"
#include "math.h"
#include <NewPing.h>
#define leftMotorPWMPin 6
#define leftMotorDirPin 7
#define rightMotorPWMPin 5
#define rightMotorDirPin 4
#define TRIGGER_PIN 9
#define ECHO_PIN 8
#define MAX_DISTANCE 1
#define Kp 20
#define Kd 0.05
#define Ki 20
#define sampleTime 0.005
#define targetAngle 10
MPU6050 mpu;
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);
int16_t accY, accZ, gyroX;
volatile int motorPower, gyroRate;
volatile float accAngle, gyroAngle, currentAngle, prevAngle=0, error, prevError=0, errorSum=0;
volatile byte count=0;
int distanceCm;
void setMotors(int leftMotorSpeed, int rightMotorSpeed) {
if(leftMotorSpeed >= 0) {
analogWrite(leftMotorPWMPin, leftMotorSpeed);
digitalWrite(leftMotorDirPin, LOW);
}
else {
analogWrite(leftMotorPWMPin, 255 + leftMotorSpeed);
digitalWrite(leftMotorDirPin, HIGH);
}
if(rightMotorSpeed >= 0) {
analogWrite(rightMotorPWMPin, rightMotorSpeed);
digitalWrite(rightMotorDirPin, LOW);
}
else {
analogWrite(rightMotorPWMPin, 255 + rightMotorSpeed);
digitalWrite(rightMotorDirPin, HIGH);
}
}
void init_PID() {
/* Initialize Timer1 */
cli(); // Disable global interrupts
TCCR1A = 0; // Reset TCCR1A register
TCCR1B = 0; // Reset TCCR1B register
/* Set compare match register to set sample time 5ms */
OCR1A = 9999;
/* Turn on CTC mode */
TCCR1B |= (1 << WGM12);
/* Set CS11 bit for prescaling by 8 */
TCCR1B |= (1 << CS11);
/* Enable timer compare interrupt */
TIMSK1 |= (1 << OCIE1A);
sei(); // Enable global interrupts
}
void setup() {
/* Set the motor control and PWM pins to output mode */
pinMode(leftMotorPWMPin, OUTPUT);
pinMode(leftMotorDirPin, OUTPUT);
pinMode(rightMotorPWMPin, OUTPUT);
pinMode(rightMotorDirPin, OUTPUT);
/* Set the status LED to output mode
pinMode(13, OUTPUT);
/* Initialize the MPU6050 and set offset values
mpu.initialize();
/* Initialize PID sampling loop
init_PID();
}
void loop() {
/* Read acceleration and gyroscope values */
accY = mpu.getAccelerationY();
accZ = mpu.getAccelerationZ();
gyroX = mpu.getRotationX();
/* Set motor power after constraining it
motorPower = constrain(motorPower, -255, 255);
setMotors(motorPower, -motorPower);
/* Measure distance every 100 milliseconds
if((count%20) == 0){
distanceCm = sonar.ping_cm();
}
if((distanceCm < 20) && (distanceCm != 0)) {
setMotors(-motorPower, motorPower);
}
}
/* The ISR will be called every 5 milliseconds */
ISR(TIMER1_COMPA_vect)
{
/* Calculate the angle of inclination */
accAngle = atan2(accY, accZ)*RAD_TO_DEG;
gyroRate = map(gyroX, -32768, 32767, -250, 250);
gyroAngle = (float)gyroRate*sampleTime;
currentAngle = 0.9934*(prevAngle + gyroAngle) + 0.0066*(accAngle);
error = currentAngle - targetAngle;
errorSum = errorSum + error;
errorSum = constrain(errorSum, -300, 300);
/* Calculate output from P, I and D values */
motorPower = Kp*(error) + Ki*(errorSum)*sampleTime - Kd*(currentAngle-prevAngle)/sampleTime;
prevAngle = currentAngle;
/* Toggle the led on pin13 every second */
count++;
if(count == 200) {
count = 0;
digitalWrite(13, !digitalRead(13));
}
}