-
Notifications
You must be signed in to change notification settings - Fork 0
/
Actions.c
187 lines (176 loc) · 5.74 KB
/
Actions.c
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#include <xc.h>
#include "Actions.h"
#include "dc_motor.h"
#include "color.h"
extern int second;
extern int end;
void memory_pushAction(int action, action_stack *a_stack) { //increase the pointer
a_stack->stack[a_stack->top] = action; //save the most recent action
}
void memory_pushTime(int time, action_stack *a_stack) { //increase the pointer
a_stack->time[++a_stack->top] = time; //save the most recent action
}
int memory_popAction(action_stack *a_stack) {
int top = a_stack->stack[a_stack->top]; //save the most recent action
return top;
}
int memory_popTime(action_stack *a_stack) {
int time_top = a_stack->time[a_stack->top--]; //save the most recent action
return time_top;
}
int stack_empty(action_stack *a_stack) {
int empty;
if (a_stack->top == -1) {
empty = 1;
} else {
empty = 0;
}
return empty;
}
void buggy_makeColorActions(DC_motor *mL, DC_motor *mR,action_stack *stack)
{
float colorValue = color_getColorValue();
int isColor = color_getColor(colorValue);
/*
* 0 - red
* 1 - blue
* 2 - light_blue
* 3 - green
* 4 - orange
* 5 - white
* 6 - yellow
* 7 - black
* 8 - pink
* 9 - repeat
* 10 - error
*/
//use for color testing
//after each action do for a while full speed ahead to be perfectly aligned with the wall
switch (isColor)
{
case red:
reverse(mL,mR); // reverse half a square to get a better turn space
// turn right 90
memory_pushAction(red, stack);
turn(mL, mR, 90, RIGHT); // 1 is right
break;
case blue:
reverse(mL,mR); // reverse half a square to get a better turn space
//turn 180
memory_pushAction(blue, stack);
turn(mL, mR, 180, LEFT); // 0 is left.
break;
case light_blue:
reverse(mL,mR); // reverse half a square to get a better turn space
//turn left 135
memory_pushAction(light_blue,stack);
turn(mL, mR, 135, LEFT);
break;
case green:
reverse(mL,mR); // reverse half a square to get a better turn space
//TURN LEFT 90
memory_pushAction(green,stack);
turn(mL, mR, 90, LEFT);
break;
case orange:
reverse(mL,mR); // reverse half a square to get a better turn space
//turn right 135
memory_pushAction(orange,stack);
turn(mL, mR, 135, RIGHT);
break;
case white:
reverse(mL,mR); // reverse half a square to get a better turn space
// maze end - retrace the steps back and come home
memory_pushAction(white,stack);
buggy_makeReverseActions(mL, mR, stack);
break;
case yellow:
reverse(mL,mR); // reverse half a square to get a better turn space
// reverse 1 square and turn right 90
memory_pushAction(yellow,stack);
reverse(mL,mR);
reverse(mL,mR);
turn(mL, mR, 90, RIGHT);
break;
case black:
memory_pushAction(black,stack);
// enter search mode
break;
case pink: //pink is not working
reverse(mL,mR); // reverse half a square to get a better turn space
// reverse 1 square and turn left 90
memory_pushAction(pink,stack);
reverse(mL,mR);
reverse(mL,mR);
turn(mL, mR, 90, LEFT);
break; //pink is not working
case repeat: break;
default: break;
}
}
void buggy_makeReverseActions(DC_motor *mL, DC_motor *mR, action_stack *stack) {
led_off();
Main_beams = 1;
Low_beams = 1;
while (!stack_empty(stack)) {
switch (memory_popAction(stack)) {
case red:
forward(mL, mR);
// turn left 90
turn(mL, mR, 90, LEFT); // 1 is right
break;
case blue:
forward(mL, mR);
//turn 180
turn(mL, mR, 180, RIGHT); // 0 is left
break;
case light_blue:
forward(mL, mR);
//turn right 135
turn(mL, mR, 135, RIGHT);
break;
case green:
forward(mL, mR);
//TURN right 90
turn(mL, mR, 90, RIGHT);
break;
case orange:
forward(mL, mR);
//turn left 135
turn(mL, mR, 135, LEFT);
break;
case white:
// do nothing other than reverse
break;
case yellow:
forward(mL, mR);
//forward(mL, mR); //same way move a bit so rotation is easier
turn(mL, mR, 90, LEFT);
forward(mL, mR);
forward(mL, mR);
forward_to_parallel(mL,mR);
break;
case black:
// enter search mode
break;
case pink:
forward(mL, mR);
turn(mL, mR, 90, RIGHT);
forward(mL, mR);
forward(mL, mR);
forward_to_parallel(mL,mR);
break;
case repeat: break;
default: break;
}
second = 0;
int run_time = memory_popTime(stack);
while (second <= run_time) {
fullSpeedReverse(mL, mR, speed);
}
stop_slow(mL, mR);
reverse_to_parallel(mL,mR); // ram into wall to make parrell
}
Brake_light = 1;
end = 1; //would be nicer to just trigger an interrupt in the system
}