-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_controller.c
191 lines (176 loc) · 4.53 KB
/
main_controller.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
188
189
190
191
#include <Servo.h>
#include <assert.h>
//#define SETTING
const int SMALL_DELAY = 100;
const int DEFAULT_DELAY = 500;
const int BIG_DELAY = 1000;
struct Robo_fish {
enum {
MOVE_FORWARD,
MOVE_LEFT,
MOVE_RIGHT,
STOP,
};
static const int NUM_SERVOS = 3;
const int CENTER[NUM_SERVOS] = {
82,
96,
88,
};
const int SMALL_DELTA[NUM_SERVOS] = {
5,
10,
15,
};
const int DEFAULT_DELTA[NUM_SERVOS] = {
10,
20,
30,
};
const int BIG_DELTA[NUM_SERVOS] = {
20,
30,
45,
};
const int MIN_BORDERS_FORWARD[NUM_SERVOS] = {
CENTER[0] - DEFAULT_DELTA[0],
CENTER[1] - DEFAULT_DELTA[1],
CENTER[2] - DEFAULT_DELTA[2],
};
const int MAX_BORDERS_FORWARD[NUM_SERVOS] = {
CENTER[0] + DEFAULT_DELTA[0],
CENTER[1] + DEFAULT_DELTA[1],
CENTER[2] + DEFAULT_DELTA[2],
};
const int MIN_BORDERS_LEFT[NUM_SERVOS] = {
CENTER[0] - SMALL_DELTA[0],
CENTER[1] - SMALL_DELTA[1],
CENTER[2] - SMALL_DELTA[2],
};
const int MAX_BORDERS_LEFT[NUM_SERVOS] = {
CENTER[0] + BIG_DELTA[0],
CENTER[1] + BIG_DELTA[1],
CENTER[2] + BIG_DELTA[2],
};
const int MIN_BORDERS_RIGHT[NUM_SERVOS] = {
CENTER[0] - BIG_DELTA[0],
CENTER[1] - BIG_DELTA[1],
CENTER[2] - BIG_DELTA[2],
};
const int MAX_BORDERS_RIGHT[NUM_SERVOS] = {
CENTER[0] + SMALL_DELTA[0],
CENTER[1] + SMALL_DELTA[1],
CENTER[2] + SMALL_DELTA[2],
};
Servo servo[NUM_SERVOS];
int pos[NUM_SERVOS];
int current_state = STOP;
void modify_state(int input) {
if (input == 'F') {
current_state = MOVE_FORWARD;
} else if (input == 'L') {
current_state = MOVE_LEFT;
} else if (input == 'R') {
current_state = MOVE_RIGHT;
} else if (input == 'S') {
current_state = STOP;
} else {
assert(0);
}
}
void rotate(Servo *servo, int *current_position, const int LEFT_BORDER, const int RIGHT_BORDER) {
if (*current_position != LEFT_BORDER) {
*current_position = LEFT_BORDER;
} else {
*current_position = RIGHT_BORDER;
}
servo->write(*current_position);
}
void move_forward() {
for (int i = 0; i < NUM_SERVOS; i++) {
rotate(&servo[i], &pos[i], MIN_BORDERS_FORWARD[i], MAX_BORDERS_FORWARD[i]);
}
}
void move_left() {
for (int i = 0; i < NUM_SERVOS; i++) {
rotate(&servo[i], &pos[i], MIN_BORDERS_LEFT[i], MAX_BORDERS_LEFT[i]);
}
}
void move_right() {
for (int i = 0; i < NUM_SERVOS; i++) {
rotate(&servo[i], &pos[i], MIN_BORDERS_RIGHT[i], MAX_BORDERS_RIGHT[i]);
}
}
void move_stop() {
for (int i = 0; i < NUM_SERVOS; i++) {
servo[i].write(CENTER[i]);
}
}
};
Robo_fish my_fish;
const char SIMPLE_GREETINGS[] = "F, L, R or S accepts";
const char GREETINGS[] = "Type 'F' for move forward, 'L' for move left, 'R' for move right ans 'S' for stop moving";
int is_correct_input(int input) {
return input == 'F' ||
input == 'L' ||
input == 'R' ||
input == 'S';
}
void setup() {
Serial.begin(9600);
my_fish.servo[0].attach(2);
my_fish.servo[1].attach(3);
my_fish.servo[2].attach(4);
Serial.println(SIMPLE_GREETINGS);
Serial.flush();
#ifdef SETTING
pinMode(A0, INPUT);
#endif
}
void loop() {
delay(SMALL_DELAY);
#ifdef SETTING
int rotat = map(analogRead(A0), 0, 1023, 0, 180);
Serial.println(rotat);
Serial.flush();
my_fish.servo[0].write(rotat); // 82 is center
my_fish.servo[1].write(rotat); // 96 is center
my_fish.servo[2].write(rotat); // 88 is center
return;
#endif
if (Serial.available()) {
int input = Serial.read();
if (input != 10) { // enter button
Serial.print("You typed: ");
Serial.println(input);
Serial.flush();
if (is_correct_input(input)) {
my_fish.modify_state(input);
Serial.print("Current state: ");
Serial.println(my_fish.current_state);
Serial.flush();
}
}
} else {
delay(DEFAULT_DELAY);
if (my_fish.current_state == my_fish.MOVE_FORWARD) {
Serial.println("Moving forward...");
Serial.flush();
my_fish.move_forward();
} else if (my_fish.current_state == my_fish.MOVE_LEFT) {
Serial.println("Moving left...");
Serial.flush();
my_fish.move_left();
} else if (my_fish.current_state == my_fish.MOVE_RIGHT) {
Serial.println("Moving right...");
Serial.flush();
my_fish.move_right();
} else if (my_fish.current_state == my_fish.STOP) {
Serial.println("Stopped...");
Serial.flush();
my_fish.move_stop();
}else {
assert(0);
}
}
}