arduino stuffs
Diffstat (limited to 'car_driver/arduino/arduino.ino')
-rw-r--r--car_driver/arduino/arduino.ino111
1 files changed, 39 insertions, 72 deletions
diff --git a/car_driver/arduino/arduino.ino b/car_driver/arduino/arduino.ino
index a0afd59..fee8153 100644
--- a/car_driver/arduino/arduino.ino
+++ b/car_driver/arduino/arduino.ino
@@ -1,27 +1,12 @@
#define HEADER 'H'
-#define TOTAL_BYTES 2 // hmmm
-#define number_of_keys 5
+#define TOTAL_BYTES 3 // hmmm
-enum { MOV_LEFT, MOV_RIGHT, MOV_FORWARD, MOV_BACK, MOV_ROTATE, MOV_STOP };
-
-const char tags[] = {'w', 'a', 's', 'd', 'v'};
-const char *states[] = {"Left", "Right", "Forward", "Back", "Rotate", "Stop"};
-const int DIR_LEFT = 0;
-const int DIR_RIGHT = 1;
-const int DIR_CENTER = 2;
-
-const char *locationString[] = {"Left", "Right", "Center"}; // labels for debug
-
-const int LED_PIN = 13;
-
-int commandState = MOV_STOP; // what robot is told to do
-
-const char MOVE_FORWARD = 'w'; // move forward
-const char MOVE_LEFT = 'a'; // move left
-const char MOVE_RIGHT = 'd'; // move right
-const char MOVE_BACK = 's'; // move back
-const char STOP = 'v'; // stop
+const int MOVE_FORWARD[2] = {0, 2}; // 2 == -1 == up
+const int MOVE_LEFT[2] = {2, 0}; // move left
+const int MOVE_RIGHT[2] = {1, 0}; // move right
+const int MOVE_BACK[2] = {0, 1}; // move back
+const int MOVE_STOP[2] = {0, 0}; // stop
void setup() {
Serial.begin(9600);
@@ -31,16 +16,21 @@ void setup() {
void loop() {
if (Serial.available() >= TOTAL_BYTES) {
- // sample format: Hv or Hd, etc.
+ // sample format: H11 or H02, etc.
char header = Serial.read();
- if (header == HEADER) { // done reading header, next letter is a tag
- char tag = Serial.read(); // read the first tag
- if (is_tag_valid(tag) == true) {
- Serial.write(tag);
- Serial.println(" recieved");
- processCommand(tag); // process the command
+ if (header == HEADER) { // done reading header, next number is a tag
+ char tmp = Serial.read();
+ int x = (tmp - '0');
+ tmp = Serial.read();
+ int y = (tmp - '0');
+ Serial.println(y, BIN);
+ Serial.println(x, BIN);
+ Serial.println("this is what I recived boss");
+ if (is_tag_valid(x, y)) {
+ processCommand(x, y); // process the command
} else {
- Serial.write(tag);
+ Serial.print(x, BIN);
+ Serial.print(y, BIN);
Serial.println(": unknown tag");
}
} else {
@@ -50,56 +40,33 @@ void loop() {
}
}
-bool is_tag_valid(char tag) {
- for (int i = 0; i < number_of_keys; i++) {
- Serial.print(tags[i]);
- if (tag == tags[i]) {
- return true;
- }
- }
- return false;
-}
-
-void changeCmdState(int newState) {
- if (newState != commandState) {
- Serial.print("cc");
- Serial.print(states[commandState]);
- Serial.print("->");
- Serial.println(states[newState]);
- commandState = newState;
- }
+bool is_tag_valid(const int x, const int y) {
+ return x > 0 && x < 3 && y > 0 && y < 3;
}
-void processCommand(char cmd) {
- switch (cmd) {
- case STOP: {
- changeCmdState(MOV_STOP);
+// @param cmd the thing that tells it what to do, usually a vector(01, 22).
+void processCommand(const int x, const int y) {
+ const int cmd[] = {x, y};
+ if (cmd == MOVE_FORWARD) {
+ forward();
+ } else if (arrayCmp(cmd, MOVE_STOP)) {
stop();
- break;
- }
- case MOVE_LEFT: {
- changeCmdState(MOV_ROTATE);
+ } else if (arrayCmp(cmd, MOVE_LEFT)) {
left();
- break;
- }
- case MOVE_RIGHT: {
- changeCmdState(MOV_ROTATE);
+ } else if (arrayCmp(cmd, MOVE_RIGHT)) {
right();
- break;
- }
- case MOVE_FORWARD: {
- changeCmdState(MOV_FORWARD);
- forward();
- } break;
- case MOVE_BACK: { // s is hard without 40 buttons
- changeCmdState(MOV_BACK);
+ } else if (arrayCmp(cmd, MOVE_BACK)) {
backward();
- break;
- }
- default:
+ } else {
Serial.print('[');
- Serial.write(cmd);
+ Serial.print(x);
+ Serial.print(cmd[0]);
+ Serial.print(cmd[1]);
+ Serial.print(y);
Serial.println("] Ignored");
- break;
}
+}
+
+bool arrayCmp(const int array[], const int array2[]) {
+ return array[0] == array2[0] && array[1] == array2[1];
} \ No newline at end of file