diff --git a/ROSArduinoBridge/ROSArduinoBridge.ino b/ROSArduinoBridge/ROSArduinoBridge.ino index d851d6f9..c101f676 100644 --- a/ROSArduinoBridge/ROSArduinoBridge.ino +++ b/ROSArduinoBridge/ROSArduinoBridge.ino @@ -60,7 +60,10 @@ //#define ROBOGAIA /* Encoders directly attached to Arduino board */ - #define ARDUINO_ENC_COUNTER + //#define ARDUINO_ENC_COUNTER + + /* Transmissive Optical Sensor with Phototransistor Output with external pull down resistor*/ + #define TOSPO_ENCODER /* L298 Motor driver*/ #define L298_MOTOR_DRIVER @@ -119,6 +122,12 @@ #endif /* Variable initialization */ +#ifdef TOSPO_ENCODER + #include "globals.h" // Fix capitalization to match actual filename + // Initialize global variables with a default direction + MotorDirection leftMotorDirection = STOPPED; + MotorDirection rightMotorDirection = STOPPED; +#endif // A pair of varibles to help parse serial commands (thanks Fergs) int arg = 0; @@ -251,27 +260,47 @@ void setup() { // Initialize the motor controller if used */ #ifdef USE_BASE - #ifdef ARDUINO_ENC_COUNTER - //set as inputs - DDRD &= ~(1< 255) spd = 255; - if (i == LEFT) { - if (reverse == 0) { analogWrite(LEFT_MOTOR_FORWARD, spd); analogWrite(LEFT_MOTOR_BACKWARD, 0); } - else if (reverse == 1) { analogWrite(LEFT_MOTOR_BACKWARD, spd); analogWrite(LEFT_MOTOR_FORWARD, 0); } + if (i == LEFT) { + if (reverse == 0 && spd > 0) { analogWrite(LEFT_MOTOR_FORWARD, spd); analogWrite(LEFT_MOTOR_BACKWARD, 0); leftMotorDirection = FORWARD;} + else if (reverse == 1) { analogWrite(LEFT_MOTOR_BACKWARD, spd); analogWrite(LEFT_MOTOR_FORWARD, 0); leftMotorDirection = BACKWARD;} + else { analogWrite(LEFT_MOTOR_FORWARD, 0); analogWrite(LEFT_MOTOR_BACKWARD, 0); leftMotorDirection = STOPPED; } } else /*if (i == RIGHT) //no need for condition*/ { - if (reverse == 0) { analogWrite(RIGHT_MOTOR_FORWARD, spd); analogWrite(RIGHT_MOTOR_BACKWARD, 0); } - else if (reverse == 1) { analogWrite(RIGHT_MOTOR_BACKWARD, spd); analogWrite(RIGHT_MOTOR_FORWARD, 0); } + if (reverse == 0 && spd > 0) { analogWrite(RIGHT_MOTOR_FORWARD, spd); analogWrite(RIGHT_MOTOR_BACKWARD, 0); rightMotorDirection = FORWARD;} + else if (reverse == 1) { analogWrite(RIGHT_MOTOR_BACKWARD, spd); analogWrite(RIGHT_MOTOR_FORWARD, 0); rightMotorDirection = BACKWARD;} + else { analogWrite(RIGHT_MOTOR_BACKWARD, 0); analogWrite(RIGHT_MOTOR_FORWARD, 0); rightMotorDirection = STOPPED; } } }