-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy patharduino_motor.ino
91 lines (82 loc) · 2.15 KB
/
arduino_motor.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
#include <PID_v1.h>
#include <math.h>
#define LIGHT_DEBOUNCE_TIME 3 // in ms
#define N_LIGHT_TICKS 5 // number of ticks to read an ideal speed
uint16_t counterTick = 0;
uint8_t sensorPin = 3;
uint8_t PWM = 9;
// int currTime = 0;
// int prevTime = 0;
const double inchesPerTick= 2.0; //number of plastic spokes
double ips = 0;
double wheel = 7.85; //wheel circumference in inches
double timeInt = 0;
double spd = 0;
double Kp=0, Ki=0, Kd = 0;
double targetSpd = 20;
bool dFlag = true;
bool updated = false;
PID myPID(&ips,&spd,&targetSpd,Kp,Ki,Kd, DIRECT);
void setup() {
Serial.begin(9600);
attachInterrupt(digitalPinToInterrupt(sensorPin), ticker, RISING);
pinMode(PWM,OUTPUT);
analogWrite(PWM,0);
Kp = 0.3; Ki = 10; Kd = 0.05;
myPID.SetOutputLimits(0,255);
myPID.SetTunings(Kp,Ki,Kd);
myPID.SetSampleTime(20);
analogWrite(PWM,0);
myPID.SetMode(AUTOMATIC);
prevTime = micros();
}
void loop(){
updated = false;
delay(100);
updateSerial();
if(!updated) {
updateSpeed();
}
}
void updateSerial(){
if(Serial.available()){
targetSpd = ((double)Serial.read())/255.0;
}
}
// use counterTick to calculate current speed and update PID
void updateSpeed() {
static unsigned long prevTime = 0;
unsigned long currTime = micros();
unsigned long deltaTime = currTime - prevTime;
prevTime = currTime;
ips = ((double)counterTick)/((double)deltaTime) * inchesPerTick * 1000;
counterTick = 0;
writePID();
}
void writePID(){
myPID.Compute();
analogWrite(PWM,spd);
}
void ticker(void){
// remember last triggered time for debouncing
static unsigned long prevTime = 0;
unsigned long currTime = micros();
// test for debouncing
if (currTime - prevTime > LIGHT_DEBOUNCE_TIME) {
prevTime = currTime;
updated = true;
counterTick++;
if (counterTick == N_LIGHT_TICKS) {
updateSpeed();
}
}
}
// void collectips(){
// double k = timeInt/1000.0;
// ips = counterTick/inchesPerTick/k;
// }
// void collectTime(){
// currTime = micros();
// timeInt = currTime - prevTime;
// prevTime = currTime;
// }