/
Movement.ino
167 lines (158 loc) · 6.06 KB
/
Movement.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
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
163
164
165
166
167
unsigned long totalSteps = 0;
unsigned long ideal_leftsensor = 0; //calibrated ideal sensor readings. See calibration function below
unsigned long ideal_rightsensor = 0;
//unsigned char leftSetpoint = 8; //setpoint for left sensor during stepForward()
//unsigned char rightSetpoint = 8; //setpoint for right sensor during stepForward()
//unsigned char frontSetpoint = 8; //setpoint for the front sensor. Ideal distance from front wall
unsigned char leftMotorPower = 255; //PWM output to the left motor
unsigned char rightMotorPower = 255; //PWM output to the right motor
void left_right_calibration(){ //calibrates the left and right sensor to the robots ideal position
int i = 0;
for(i; i<=20; i++)
{
ideal_leftsensor += analogRead(L_SENS_PIN);
ideal_rightsensor += analogRead(R_SENS_PIN);
}
ideal_leftsensor = ideal_leftsensor/i;
ideal_rightsensor = ideal_rightsensor/i;
}
void fast_stop(){
for(int j =0; j<1350; j++) // for PWM left: 150 Right: 155. 1350 iterations to stop.
{
analogWrite(EN_LEFT, 100);
analogWrite(EN_RIGHT, 100);
digitalWrite(L_MINUS, HIGH);
digitalWrite(L_PLUS, LOW);
digitalWrite(R_MINUS, HIGH);
digitalWrite(R_PLUS, LOW);
}
digitalWrite(L_PLUS, LOW);
digitalWrite(R_PLUS, LOW);
analogWrite(EN_LEFT, LOW);
analogWrite(EN_RIGHT, LOW);
}
//turnLeft() - turns the robot counterclockwise clockwise 90 degrees
void turnLeft() //Turns left until calibrated steps have been reached
{
totalSteps = leftWheelSteps + rightWheelSteps;
unsigned long initSteps = leftWheelSteps + rightWheelSteps;
unsigned long L_turnProgress = totalSteps - initSteps;
unsigned long L_turnSteps = 80;
while(L_turnProgress < L_turnSteps)
{
digitalWrite(R_PLUS, HIGH);
digitalWrite(R_MINUS, LOW);
digitalWrite(L_PLUS, LOW);
digitalWrite(L_MINUS, HIGH);
analogWrite(EN_LEFT, 15);
analogWrite(EN_RIGHT,15);
totalSteps = leftWheelSteps + rightWheelSteps;
L_turnProgress = totalSteps - initSteps;
}
analogWrite(EN_LEFT, LOW);
analogWrite(EN_RIGHT, LOW);
delay(500);
/* Mapping */
currentFacing = currentFacing > 0 ? --currentFacing : 3; //updates the current facing.
}
//turnRight() - turns the robot clockwise 90 degrees
void turnRight() //Turns right until calibrated steps have been reached
{
totalSteps = leftWheelSteps + rightWheelSteps;
unsigned long initSteps = leftWheelSteps + rightWheelSteps;
unsigned long R_turnProgress = totalSteps - initSteps;
unsigned long R_turnSteps = 88;
while(R_turnProgress < R_turnSteps)
{
digitalWrite(L_PLUS, HIGH);
digitalWrite(L_MINUS, LOW);
digitalWrite(R_PLUS, LOW);
digitalWrite(R_MINUS, HIGH);
analogWrite(EN_LEFT, 15);
analogWrite(EN_RIGHT,15);
totalSteps = leftWheelSteps + rightWheelSteps;
R_turnProgress = totalSteps - initSteps;
}
delay(500);
analogWrite(EN_LEFT, LOW);
analogWrite(EN_RIGHT, LOW);
/* Mapping */
currentFacing = currentFacing < 3 ? ++currentFacing : 0; //updates the current facing.
}
//stepForward - runs both motors to move straight while correcting its path
void stepForward()
{
unsigned char LKP = 1;
unsigned char RKP = 1;
stepProgress = 0; //initialize the amount of distance traveled since last stationary
long leftError = 0; //the difference between the leftSetpoint and the actual left sensor reading
long rightError = 0;
unsigned long initSteps = leftWheelSteps + rightWheelSteps;
totalSteps = leftWheelSteps + rightWheelSteps;
//the difference between the rightSetpoint and the actual right sensor reading
//Movement
while(stepProgress < 250)
{
//Serial.println(stepProgress);
frontSensor = analogRead(F_SENS_PIN); //updates sensor readings
leftSensor = analogRead(L_SENS_PIN);
rightSensor = analogRead(R_SENS_PIN);
leftError = ideal_leftsensor - analogRead(L_SENS_PIN); //finds how far the left sensor is from the desired state
rightError = ideal_rightsensor - analogRead(R_SENS_PIN);
totalSteps = leftWheelSteps + rightWheelSteps;
stepProgress = totalSteps - initSteps;
// Serial.println(stepProgress);
//finds how far the right sensor is from the desired state
/* apply voltage to both motors, complete with proportional control c = Error * Kp, where c is an offset to the motor's PWM voltage*/
//set forward direction
digitalWrite(L_PLUS, HIGH);
digitalWrite(L_MINUS, LOW);
digitalWrite(R_PLUS, HIGH);
digitalWrite(R_MINUS, LOW);
//set speed
if(leftError > 0 )
{
unsigned long L_adjust = 90 + (leftError * LKP);
if(L_adjust > 255)
{
L_adjust = 255;
}
analogWrite(EN_LEFT, L_adjust);
analogWrite(EN_RIGHT, 95);
}
else
{
unsigned long R_adjust = 95 + (rightError * RKP );
if(R_adjust > 255)
{
R_adjust = 255;
}
analogWrite(EN_RIGHT, R_adjust );
analogWrite(EN_LEFT, 90);
}
if (frontSensor <= F_SENSOR_SETPOINT && digitalRead(L_PLUS) == HIGH && digitalRead(L_MINUS) == LOW) //if ideal distance from front wall, stop immediately.
{
/* turn off both motors */
fast_stop();
break;
}
}
/* Mapping */
switch(currentFacing) //updates the current position on the map based on the direction it just stepped.
{
case 0: //steppng north
--yPosition;
break;
case 1: //stepping east
++xPosition;
break;
case 2: //stepping south
++yPosition;
break;
case 3: //stepping west
--xPosition;
break;
}
fast_stop();
delay(2000);
}