-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEE3.ino
More file actions
162 lines (138 loc) · 4.73 KB
/
EE3.ino
File metadata and controls
162 lines (138 loc) · 4.73 KB
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
#include <ECE3.h>
////////////////motor control definitions//////////////////////////
const int left_nslp_pin=31; // nslp ==> awake & ready for PWM
const int left_dir_pin=29;
const int left_pwm_pin=40;
const int right_nslp_pin=11; // nslp ==> awake & ready for PWM
const int right_dir_pin=30;
const int right_pwm_pin=39;
const int LED_RF = 41;
const int LED_FF = 51;
const int MOTORDEFAULT = 120;
////////////////////////////////////////////////////////////////////
//definition of turning constant
const int TURN = 1200;
//definition of PD constants
const int Kp = 4;
const int Kd = 53;
int prev_error=0; //this is the previous_error
int mergedinput=0; //this is the merged sensorfusion value
uint16_t sensorValues[8];
uint16_t normalValues[8];
void setup()
{
ECE3_Init();
//Serial.begin(9600);
////motor control setup////////
pinMode(left_nslp_pin,OUTPUT);
pinMode(left_dir_pin,OUTPUT);
pinMode(left_pwm_pin,OUTPUT);
pinMode(right_nslp_pin,OUTPUT);
pinMode(right_dir_pin,OUTPUT);
pinMode(right_pwm_pin,OUTPUT);
digitalWrite(left_dir_pin,LOW);
digitalWrite(left_nslp_pin,HIGH);
digitalWrite(right_dir_pin,LOW);
digitalWrite(right_nslp_pin,HIGH);
delay(2000);
}
void loop()
{
// read raw sensor values
ECE3_read_IR(sensorValues);
//check if black line is hit to indicate turning
if (sensorValues[0] >= TURN && sensorValues[1] >= TURN && sensorValues[7] >= TURN && sensorValues[6] >= TURN)
{
turn();
return;
}
mergedinput = sense();
int error = mergedinput - 0; //calculate the error with 0 being the no error state
int control = Kp*error + Kd*(error-prev_error); //apply the the p control
prev_error=error;
int correct; //initialize value for correction to apply
//Serial.print("error: ");
//Serial.println(control);
//delay(2000);
///////////////////checking of direction of control///////////////////////////
if (control > 0 ) //apply motor correction to right wheel
{
correct = control/12; //mapping the control value to range of acceptable values of the motor
//Serial.print("Right correction:");
//Serial.println(correct);
//delay(1000);
analogWrite(left_pwm_pin, MOTORDEFAULT-correct);
analogWrite(right_pwm_pin, MOTORDEFAULT+correct);
}
else if (control < 0) //apply motor correction to left wheel
{
control = control*(-1); //change the value of control to a positive value
correct = control/12; //mapping the control value to range of acceptable values of the motor
//Serial.print("Left correction:");
//Serial.println(correct);
//delay(1000);
analogWrite(left_pwm_pin, MOTORDEFAULT+correct);
analogWrite(right_pwm_pin, MOTORDEFAULT-correct);
}
else if (control == 0) //no error, meaning both motors run at default speed
{
analogWrite(left_pwm_pin, 255);
analogWrite(right_pwm_pin, 255);
}
delay(1);
}
int sense()
{
// print the sensor values as numbers from 0 to 2500, where 0 means maximum reflectance and
// 2500 means minimum reflectance
for (unsigned char i = 0; i < 8; i++)
{
switch (i) {
case 0:
normalValues[i] = map(sensorValues[i], 0, 2500, 0, 150); //267
case 1:
normalValues[i] = map(sensorValues[i], 0, 2500, 0, 150); //290
case 2:
normalValues[i] = map(sensorValues[i], 0, 2500, 0, 150); //336
case 3:
normalValues[i] = map(sensorValues[i], 0, 2200, 0, 150); //290
case 4:
normalValues[i] = map(sensorValues[i], 0, 2200, 0, 150); //406
case 5:
normalValues[i] = map(sensorValues[i], 0, 2500, 0, 150); //398
case 6:
normalValues[i] = map(sensorValues[i], 0, 2500, 0, 150); //464
case 7:
normalValues[i] = map(sensorValues[i], 0, 2500, 0, 150); //511
}
}
///////sensor fusion////////////////
int mergedinputresult = normalValues[0]*(-4) + normalValues[1]*(-3) + normalValues[2]*(-2) + normalValues[3]*(-1) + normalValues[4]*1 + normalValues[5]*2 + normalValues[6]*3 + normalValues[7]*4;
return(mergedinputresult);
//Serial.print("Error: ");
//Serial.println(mergedinput);
//delay(1000);
//Serial.print('\t'); // tab to format the raw data into columns in the Serial monitor
//Serial.println();
}
//function to execute the turn
void turn()
{
delay(50);
digitalWrite(right_dir_pin,HIGH);
for (int i=0; i<750; i++)
{
//Serial.println("turn");
delay(1);
analogWrite(left_pwm_pin, 80);
analogWrite(right_pwm_pin, 80);
}
digitalWrite(right_dir_pin,LOW);
for (int q=0; q<300; q++)
{
//Serial.println("turn");
delay(1);
analogWrite(left_pwm_pin, 40);
analogWrite(right_pwm_pin, 40);
}
}