-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathSpeedCode.ino
132 lines (116 loc) · 2.56 KB
/
SpeedCode.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
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
int anglepot0= 34;
int anglepot1=35;
int a1=19;
int b1=18;
int a2=5;
int b2=4;
int motor1Pin1 = 14;
int motor1Pin2 = 12;
int motor2Pin1=33;
int motor2Pin2=25;
int pos[2];
int setpos[2];
float kp=15,kd=0.010,ki=0;
long prevt[2];
float eprev[2],einteg[2],deltaT[2],dervative[2],pidOutput[2];
float rpm[2],prevTime[2];
const int pwmChannel_11 = 0;
const int pwmChannel_12 = 1;
const int pwmChannel_21 = 2;
const int pwmChannel_22 = 3;
float pre;
void readencoder1()
{
int f= digitalRead(b1);
if(f>0)
pos[0]++;
else
pos[0]--;
}
void readencoder2()
{
int f= digitalRead(b2);
if(f>0)
pos[1]++;
else
pos[1]--;
}
float pid(int i)
{
float calc;
long currt = micros();
deltaT[i]= ((float)(currt-prevt[i]))/1.0e6;
prevt[i]=currt;
int e=setpos[i]-rpm[i];
dervative[i]= (e-eprev[i])/(deltaT[i]);
einteg[i] = einteg[i]+ e*deltaT[i];
eprev[i]=e;
return calc= kp*e+kd*dervative[i]+ki*einteg[i];
}
void motorwrite(int channel1,int channel2,int val)
{
int dutyc = constrain(fabs(val),0,255);
if(val<0)
{analogWrite(channel2,0);
analogWrite(channel1,dutyc);}
else
{analogWrite(channel1,0);
analogWrite(channel2,dutyc);
}
}
void ENCODER_RPM(int i)
{
// Calculate RPM every second
float currentTime = millis();
if (currentTime - prevTime[i] >= 1000)
{
// Calculate RPM
float rpm[i] = (float)pos[i] * (60000.0 / 493.9) / (currentTime - prevTime[i]);
// Reset the encoder count and update the previous time
pos[i] = 0;
prevTime[i] = currentTime;
}
}
void setup()
{
pinMode(anglepot0,INPUT);
pinMode(anglepot1,INPUT);
pinMode(b1,INPUT);
pinMode(b2,INPUT);
pinMode(motor1Pin1,OUTPUT);
pinMode(motor1Pin2,OUTPUT);
pinMode(motor2Pin1,OUTPUT);
pinMode(motor2Pin2,OUTPUT);
attachInterrupt(digitalPinToInterrupt(a1), readencoder1, RISING);
attachInterrupt(digitalPinToInterrupt(a2), readencoder2, RISING);
lcd.init();
lcd.backlight();
Serial.begin(115200);
}
void loop() {
setpos[0]=map(analogRead(anglepot0),0,4096,-187,187);
setpos[1]=map(analogRead(anglepot1),0,4096,-187,187);
ENCODER_RPM(0);
ENCODER_RPM(1);
pidOutput[0]= pid(0);
pidOutput[1]= pid(1);
motorwrite(pwmChannel_11,pwmChannel_12,pidOutput[0]);
motorwrite(pwmChannel_21,pwmChannel_22,pidOutput[1]);
long t=millis();
if((t-pre)>400){
lcd.setCursor(0, 0);
lcd.print("M1:");
lcd.print(map(pos[0],-187,187,-135,135));
lcd.print(" ");
lcd.print("M2:");
lcd.print(map(pos[1],-187,187,-135,135));
lcd.print(" ");
lcd.setCursor(0, 1);
lcd.print("M3:");
lcd.print(map(pos[2],-187,187,-135,135));
lcd.print(" ");
pre=t;
}
}