V0.1

Low-level code to control each subsystem. Everything due by the 26th.

Drive Train
​/* * Motors * --- * Mindy Huang */ #include   /* Module Defines ---*/ enum Direction { FORWARD, BACKWARD, SPIN_LEFT, SPIN_RIGHT }; /*Module Functions -*/ unsigned char TestForKey(void) { unsigned char KeyEventOccurred; KeyEventOccurred = Serial.available; return KeyEventOccurred; } void RespondToKey(void) { unsigned char theKey = Serial.read; Serial.print(theKey); Serial.print(", ASCII="); Serial.println(theKey,HEX); switch (theKey) { case '0': Stop; Serial.println("stop"); break; case '1': goForward(100); Serial.println("goForward"); break; case '2': goBackward(100); Serial.println("goBackward"); break; case '3': SpinLeft(100); Serial.println("SpinLeft"); break; case '4': SpinRight(100); Serial.println("SpinRight"); break; } } void goForward(int val) { digitalWrite(MOTOR_1_DIR_PIN, HIGH); digitalWrite(MOTOR_2_DIR_PIN, HIGH); analogWrite(MOTOR_1_ENABLE_PIN, val); analogWrite(MOTOR_2_ENABLE_PIN, val); } void goBackward(int val){ digitalWrite(MOTOR_1_DIR_PIN, LOW); digitalWrite(MOTOR_2_DIR_PIN, LOW); analogWrite(MOTOR_1_ENABLE_PIN, val); analogWrite(MOTOR_2_ENABLE_PIN, val); } void SpinLeft(int val) { digitalWrite(MOTOR_1_DIR_PIN, HIGH); digitalWrite(MOTOR_2_DIR_PIN, LOW); analogWrite(MOTOR_1_ENABLE_PIN, val); analogWrite(MOTOR_2_ENABLE_PIN, val); } void SpinRight(int val) { digitalWrite(MOTOR_1_DIR_PIN, LOW); digitalWrite(MOTOR_2_DIR_PIN, HIGH); analogWrite(MOTOR_1_ENABLE_PIN, val); analogWrite(MOTOR_2_ENABLE_PIN, val); } void Stop{ analogWrite(MOTOR_1_ENABLE_PIN, 0); analogWrite(MOTOR_2_ENABLE_PIN, 0); } /* Main Program ---*/ void setup { // initialize serial communication at 9600 bits per second: Serial.begin(9600); pinMode(MOTOR_1_DIR_PIN, OUTPUT); pinMode(MOTOR_1_ENABLE_PIN, OUTPUT); pinMode(MOTOR_2_DIR_PIN, OUTPUT); pinMode(MOTOR_2_ENABLE_PIN, OUTPUT); digitalWrite(HEARTBEAT_LED, HIGH); TMRArd_InitTimer(HEARTBEAT_TIMER, ONE_SEC); Stop; } void loop { static Direction dir = FORWARD; if(TestForKey) RespondToKey; if(TMRArd_IsTimerExpired(HEARTBEAT_TIMER)) { TMRArd_InitTimer(HEARTBEAT_TIMER, ONE_SEC); digitalWrite(HEARTBEAT_LED, !digitalRead(HEARTBEAT_LED)); } } 
 * 1) define MOTOR_1_DIR_PIN   2
 * 2) define MOTOR_1_ENABLE_PIN 3
 * 3) define MOTOR_2_DIR_PIN   4
 * 4) define MOTOR_2_ENABLE_PIN 5
 * 1) define HEARTBEAT_LED 13
 * 2) define ONE_SEC  1000
 * 3) define HEARTBEAT_TIMER   1

Beacon Sensor
/* * Beacons * --- * Mindy Huang */ #include   /* Module Defines ---*/ /*Module Functions -*/ unsigned char TestForKey(void) { unsigned char KeyEventOccurred; KeyEventOccurred = Serial.available; return KeyEventOccurred; } bool isFacingServer(int min, int max) { return (max - min > 530); } /* Main Program ---*/ void setup { // initialize serial communication at 9600 bits per second: Serial.begin(9600); pinMode(BEACON_SERVER_PIN, INPUT); pinMode(BEACON_EXCHANGE_PIN, INPUT); pinMode(HEARTBEAT_LED, OUTPUT); digitalWrite(HEARTBEAT_LED, HIGH); TMRArd_InitTimer(HEARTBEAT_TIMER, ONE_SEC); } void loop { static int minServerBeaconVal = 0; static int maxServerBeaconVal = 0; static int minExchangeBeaconVal = 0; static int maxExchangeBeaconVal = 0; // clear min and max beacon values intermittently if (TMRArd_IsTimerExpired(BEACON_CLEAR_TIMER)) { minServerBeaconVal = 0; maxServerBeaconVal = 0; minExchangeBeaconVal = 0; maxExchangeBeaconVal = 0; }    int currExchangeBeaconVal = analogRead(BEACON_EXCHANGE_PIN); int currServerBeaconVal = analogRead(BEACON_SERVER_PIN); // update exchange beacon values if(currExchangeBeaconVal < minExchangeBeaconVal) minExchangeBeaconVal = currExchangeBeaconVal; else if (currExchangeBeaconVal > maxExchangeBeaconVal) maxExchangeBeaconVal = currExchangeBeaconVal; // update server beacon values if(currServerBeaconVal < minServerBeaconVal) minServerBeaconVal = currServerBeaconVal; else if (currServerBeaconVal > maxServerBeaconVal) maxServerBeaconVal = currServerBeaconVal; Serial.println(currServerBeaconVal); if(isFacingServer(minServerBeaconVal, maxServerBeaconVal)) Serial.println("facing server"); if(TMRArd_IsTimerExpired(HEARTBEAT_TIMER)) { TMRArd_InitTimer(HEARTBEAT_TIMER, ONE_SEC); digitalWrite(HEARTBEAT_LED, !digitalRead(HEARTBEAT_LED)); } }
 * 1) define BEACON_SERVER_PIN   A0
 * 2) define BEACON_EXCHANGE_PIN   A1
 * 1) define HEARTBEAT_LED 13
 * 2) define ONE_SEC  1000
 * 1) define HEARTBEAT_TIMER 1
 * 2) define BEACON_CLEAR_TIMER 2

Line Sensor
/* * Light sensors * --- * Mindy Huang */  /* Module Defines ---*/ #define LIGHT_PIN_FRONT A0 #define LIGHT_PIN_CENTER A1  #define THRESHOLD_VAL 300 /*Module Functions -*/ bool isOverLine(int val) { return (val < THRESHOLD_VAL); } bool isAlignedWithLine { return isOverLine(analogRead(LIGHT_PIN_FRONT)) && isOverLine(analogRead(LIGHT_PIN_CENTER)); } void setup { // initialize serial communication at 9600 bits per second: Serial.begin(9600); pinMode(LIGHT_PIN_FRONT, INPUT); pinMode(LIGHT_PIN_CENTER, INPUT); } void loop { if(isAlignedWithLine) Serial.println("aligned"); Serial.print("T: "); Serial.println(analogRead(LIGHT_PIN_FRONT)); Serial.print("NT: "); Serial.println(analogRead(LIGHT_PIN_CENTER)); delay(500); }

Bumper Sensor
/* * Bumper sensors * --- * Mindy Huang */  /* Module Defines ---*/ #define BUMPER_FL_PIN   8 #define BUMPER_FR_PIN   9 #define BUMPER_BR_PIN   10 #define BUMPER_BL_PIN   11 #define BUMPER_TURNTABLE_PIN   12 #define FRONT 0 #define BACK  1 /*Module Functions -*/ bool isBumperPressed(int pinNum) { return (digitalRead(pinNum) == HIGH); } bool isAlignedWithWall(int side) { switch(side) { case FRONT: return (digitalRead(BUMPER_FR_PIN) == HIGH) && (digitalRead(BUMPER_FL_PIN) == HIGH); case BACK: return (digitalRead(BUMPER_BR_PIN) == HIGH) && (digitalRead(BUMPER_BL_PIN) == HIGH); } } void setup { // initialize serial communication at 9600 bits per second: Serial.begin(9600); pinMode(BUMPER_BL_PIN, INPUT); pinMode(BUMPER_BR_PIN, INPUT); pinMode(BUMPER_FL_PIN, INPUT); pinMode(BUMPER_FR_PIN, INPUT); pinMode(BUMPER_TURNTABLE_PIN, INPUT); } void loop { if(isBumperPressed(BUMPER_FR_PIN)) Serial.println("front right"); if(isBumperPressed(BUMPER_FL_PIN)) Serial.println("front left"); if(isBumperPressed(BUMPER_BL_PIN)) Serial.println("back left"); if(isBumperPressed(BUMPER_BR_PIN)) Serial.println("back right"); if(isBumperPressed(BUMPER_TURNTABLE_PIN)) Serial.println("turntable"); if(isAlignedWithWall(FRONT)) Serial.println("Front aligned"); if(isAlignedWithWall(BACK)) Serial.println("back aligned"); }