diff --git a/Ardunio_2Wheel_Robot_Boilerplate/myAIDriver/AIDriver.cpp b/Ardunio_2Wheel_Robot_Boilerplate/myAIDriver/AIDriver.cpp index 5d536f2..6aca96e 100644 --- a/Ardunio_2Wheel_Robot_Boilerplate/myAIDriver/AIDriver.cpp +++ b/Ardunio_2Wheel_Robot_Boilerplate/myAIDriver/AIDriver.cpp @@ -10,10 +10,38 @@ AIDriver::AIDriver(){ static unsigned int rightBreak = 9; //0 = Go, 1 = Break for right wheel default 9 static unsigned int leftDirection = 13; //0 = Forward, 1 = Backward for left wheel default 13 static unsigned int leftBreak = 8; //0 = Go, 1 = Break for right wheel default 8 + // Digital Pins for the Ultrasonic Sensor + static uint8_t trigPin = 6; + static uint8_t echoPin = 7; // L298N Motor controller motorRight = new L298N(rightSpeedIn,rightDirection,rightBreak); motorLeft = new L298N(leftSpeedIn,leftDirection,leftBreak); + pinMode(trigPin, OUTPUT); + pinMode(echoPin, INPUT); + + //Configure and debug the serial monitor + Serial.begin(9600); + Serial.println("Debug Serial Monitor"); +} + +unsigned int AIDriver::read() { + return timing() / 2.8 / 2; //distance by divisor +} + +unsigned int AIDriver::timing() { + static uint8_t trig = 6; + static uint8_t echo = 7; + digitalWrite(trig, LOW); + delayMicroseconds(2); + digitalWrite(trig, HIGH); + delayMicroseconds(10); + digitalWrite(trig, LOW); + previousMicros = micros(); + while(!digitalRead(echo) && (micros() - previousMicros) <= 20000); // wait for the echo pin HIGH or timeout + previousMicros = micros(); + while(digitalRead(echo) && (micros() - previousMicros) <= 20000); // wait for the echo pin LOW or timeout + return micros() - previousMicros; // duration } void AIDriver::brake(){ diff --git a/Ardunio_2Wheel_Robot_Boilerplate/myAIDriver/AIDriver.h b/Ardunio_2Wheel_Robot_Boilerplate/myAIDriver/AIDriver.h index 454b725..964e6c5 100644 --- a/Ardunio_2Wheel_Robot_Boilerplate/myAIDriver/AIDriver.h +++ b/Ardunio_2Wheel_Robot_Boilerplate/myAIDriver/AIDriver.h @@ -4,25 +4,20 @@ class AIDriver{ public: - const int MAX_SPEED = 255; - const int THREE_QUARTER_SPEED = 138; - const int HALF_SPEED = 92; - const int QUARTER_SPEED = 46; - const int MIN_SPEED = 15; - int rightSignal, rightIn1, rightIn2; - int leftSignal, leftIn1, leftIn2; - AIDriver(); - + void driveForward(unsigned short rightWheel, unsigned short leftWheel); void driveBackward(unsigned short rightWheel, unsigned short leftWheel); void rotateRight(unsigned short turnSpeed); void rotateLeft(unsigned short turnSpeed); void brake(); + unsigned int read(); private: L298N *motorRight; L298N *motorLeft; + unsigned long previousMicros; + unsigned int timing(); }; #endif \ No newline at end of file diff --git a/Ardunio_2Wheel_Robot_Boilerplate/myAIDriver/L298N.cpp b/Ardunio_2Wheel_Robot_Boilerplate/myAIDriver/L298N.cpp index 41c8605..600b061 100644 --- a/Ardunio_2Wheel_Robot_Boilerplate/myAIDriver/L298N.cpp +++ b/Ardunio_2Wheel_Robot_Boilerplate/myAIDriver/L298N.cpp @@ -1,7 +1,5 @@ #include "L298N.h" -typedef void (*CallBackFunction)(); - L298N::L298N(uint8_t pinEnable, uint8_t pinIN1, uint8_t pinIN2) { _pinEnable = pinEnable; _pinIN1 = pinIN1; @@ -9,7 +7,6 @@ L298N::L298N(uint8_t pinEnable, uint8_t pinIN1, uint8_t pinIN2) { _pwmVal = 255; _isMoving = false; _canMove = true; - _lastMs = 0; _direction = STOP; pinMode(_pinEnable, OUTPUT); @@ -45,71 +42,6 @@ void L298N::backward() { _isMoving = true; } -void L298N::run(L298N::Direction direction) { - switch (direction) { - case BACKWARD: - this->backward(); - break; - case FORWARD: - this->forward(); - break; - case STOP: - this->stop(); - break; - } -} - -// Timing and callback - -void L298N::runFor(unsigned long delay, - L298N::Direction direction, - CallBackFunction callback) { - if ((_lastMs == 0) && _canMove) { - _lastMs = millis(); - - switch (direction) { - case FORWARD: - this->forward(); - break; - case BACKWARD: - this->backward(); - break; - case STOP: - default: - this->stop(); - break; - } - } - - if (((millis() - _lastMs) > delay) && _canMove) { - this->stop(); - _lastMs = 0; - _canMove = false; - - callback(); - } -} - -void L298N::runFor(unsigned long delay, L298N::Direction direction) { - this->runFor(delay, direction, fakeCallback); -} - -void L298N::forwardFor(unsigned long delay, CallBackFunction callback) { - this->runFor(delay, FORWARD, callback); -} - -void L298N::forwardFor(unsigned long delay) { - this->runFor(delay, FORWARD); -} - -void L298N::backwardFor(unsigned long delay, CallBackFunction callback) { - this->runFor(delay, BACKWARD, callback); -} - -void L298N::backwardFor(unsigned long delay) { - this->runFor(delay, BACKWARD); -} - void L298N::stop() { digitalWrite(_pinIN1, LOW); digitalWrite(_pinIN2, HIGH); @@ -130,6 +62,4 @@ boolean L298N::isMoving() { L298N::Direction L298N::getDirection() { return _direction; -} - -void L298N::fakeCallback() {} \ No newline at end of file +} \ No newline at end of file diff --git a/Ardunio_2Wheel_Robot_Boilerplate/myAIDriver/L298N.h b/Ardunio_2Wheel_Robot_Boilerplate/myAIDriver/L298N.h index bb0b17c..8812f2e 100644 --- a/Ardunio_2Wheel_Robot_Boilerplate/myAIDriver/L298N.h +++ b/Ardunio_2Wheel_Robot_Boilerplate/myAIDriver/L298N.h @@ -3,46 +3,33 @@ #include "Arduino.h" -typedef void (*CallBackFunction)(); +class L298N { + public: + typedef enum + { + FORWARD = 0, + BACKWARD = 1, + STOP = -1 + } Direction; -class L298N -{ -public: - typedef enum - { - FORWARD = 0, - BACKWARD = 1, - STOP = -1 - } Direction; + L298N(uint8_t pinEnable, uint8_t pinIN1, uint8_t pinIN2); + void setSpeed(unsigned short pwmVal); + unsigned short getSpeed(); + void forward(); + void backward(); + void stop(); + void reset(); + boolean isMoving(); + Direction getDirection(); - L298N(uint8_t pinEnable, uint8_t pinIN1, uint8_t pinIN2); - L298N(uint8_t pinIN1, uint8_t pinIN2); - void setSpeed(unsigned short pwmVal); - unsigned short getSpeed(); - void forward(); - void forwardFor(unsigned long delay, CallBackFunction callback); - void forwardFor(unsigned long delay); - void backward(); - void backwardFor(unsigned long delay, CallBackFunction callback); - void backwardFor(unsigned long delay); - void run(L298N::Direction direction); - void runFor(unsigned long delay, L298N::Direction direction); - void runFor(unsigned long delay, L298N::Direction direction, CallBackFunction callback); - void stop(); - void reset(); - boolean isMoving(); - Direction getDirection(); - -private: - byte _pinEnable; - byte _pinIN1; - byte _pinIN2; - byte _pwmVal; - unsigned long _lastMs; - boolean _canMove; - boolean _isMoving; - L298N::Direction _direction; - static void fakeCallback(); + private: + byte _pinEnable; + byte _pinIN1; + byte _pinIN2; + byte _pwmVal; + boolean _canMove; + boolean _isMoving; + L298N::Direction _direction; }; #endif \ No newline at end of file diff --git a/Ardunio_2Wheel_Robot_Boilerplate/myAIDriver/Ultrasonic.cpp b/Ardunio_2Wheel_Robot_Boilerplate/myAIDriver/Ultrasonic.cpp deleted file mode 100644 index 05de43c..0000000 --- a/Ardunio_2Wheel_Robot_Boilerplate/myAIDriver/Ultrasonic.cpp +++ /dev/null @@ -1,27 +0,0 @@ -#include -#include "Ultrasonic.h" - -Ultrasonic::Ultrasonic(uint8_t trigPin, uint8_t echoPin, unsigned long timeOut) { - trig = trigPin; - echo = echoPin; - pinMode(trig, OUTPUT); - pinMode(echo, INPUT); - timeout = timeOut; -} - -unsigned int Ultrasonic::timing() { - digitalWrite(trig, LOW); - delayMicroseconds(2); - digitalWrite(trig, HIGH); - delayMicroseconds(10); - digitalWrite(trig, LOW); - previousMicros = micros(); - while(!digitalRead(echo) && (micros() - previousMicros) <= timeout); // wait for the echo pin HIGH or timeout - previousMicros = micros(); - while(digitalRead(echo) && (micros() - previousMicros) <= timeout); // wait for the echo pin LOW or timeout - return micros() - previousMicros; // duration -} - -unsigned int Ultrasonic::read() { - return timing() / 2.8 / 2; //distance by divisor -} \ No newline at end of file diff --git a/Ardunio_2Wheel_Robot_Boilerplate/myAIDriver/Ultrasonic.h b/Ardunio_2Wheel_Robot_Boilerplate/myAIDriver/Ultrasonic.h deleted file mode 100644 index 06cb0c0..0000000 --- a/Ardunio_2Wheel_Robot_Boilerplate/myAIDriver/Ultrasonic.h +++ /dev/null @@ -1,20 +0,0 @@ -#ifndef Ultrasonic_h -#define Ultrasonic_h - -class Ultrasonic { - public: - Ultrasonic(uint8_t sigPin) : Ultrasonic(sigPin, sigPin) {}; - Ultrasonic(uint8_t trigPin, uint8_t echoPin, unsigned long timeOut = 20000UL); - unsigned int read(); - void setTimeout(unsigned long timeOut) {timeout = timeOut;} - void setMaxDistance(unsigned long dist) {timeout = dist*28*2;} - - private: - uint8_t trig; - uint8_t echo; - unsigned long previousMicros; - unsigned long timeout; - unsigned int timing(); -}; - -#endif \ No newline at end of file diff --git a/Ardunio_2Wheel_Robot_Boilerplate/myAIDriver/myAIDriver.ino b/Ardunio_2Wheel_Robot_Boilerplate/myAIDriver/myAIDriver.ino index dfcd4c1..bd9c901 100644 --- a/Ardunio_2Wheel_Robot_Boilerplate/myAIDriver/myAIDriver.ino +++ b/Ardunio_2Wheel_Robot_Boilerplate/myAIDriver/myAIDriver.ino @@ -1,34 +1,26 @@ #include "AIDriver.h" -#include "Ultrasonic.h" -//Instantiate a Ultrasonic Sensor called 'distanceRanger' - Ultrasonic distanceRanger(6, 7); - -// Declare a pointer to a AIDriver object + // Declare a pointer to a AIDriver object AIDriver *mrJonesDriving; void setup(){ // sets up once as the program starts - //Configure and debug the serial monitor - Serial.begin(9600); - Serial.println("Debug Serial Monitor"); - - //Instantiate a two wheeled to the pointer 'mrJonesDriving' + // Instantiate a two wheeled to the pointer 'mrJonesDriving' mrJonesDriving = new AIDriver(); } void loop(){ // loops continuously 50 times a second // Read the distanceRanger ulstrasonic sensor and return the object distance in mm - Serial.println(distanceRanger.read()); + Serial.println(mrJonesDriving->read()); - //obj->doSomething(); in this case make the object mrJonesDriving rotate to the right at a speed of 200 + // obj->doSomething(); in this case make the object mrJonesDriving rotate to the right at a speed of 200 mrJonesDriving->rotateRight(125); // speed can be between 0-255 // wait 2000 milliseconds or 2 seconds delay(2000); mrJonesDriving->rotateLeft(125); delay(2000); - //Make mrJonesDriving drive forward left wheel speed 200 and right wheel speeed 200 + // Make mrJonesDriving drive forward left wheel speed 200 and right wheel speeed 200 mrJonesDriving->driveForward(125,125); delay(2000); mrJonesDriving->driveBackward(125,125); delay(2000); - } + } \ No newline at end of file diff --git a/Ardunio_2Wheel_Robot_Boilerplate/myAIDriver/notes.ino b/Ardunio_2Wheel_Robot_Boilerplate/myAIDriver/notes.ino index 0557a94..9d7e61f 100644 --- a/Ardunio_2Wheel_Robot_Boilerplate/myAIDriver/notes.ino +++ b/Ardunio_2Wheel_Robot_Boilerplate/myAIDriver/notes.ino @@ -5,12 +5,12 @@ AIDRIVER METHODS: - mrJonesDriving->rotateLeft(200); - mrJonesDriving->driveForward(200,200); //Make mrJonesDriving drive forward left wheel speed 200 and right wheel speeed 200 - mrJonesDriving->driveBackward(200,200); - - mrJonesDriving->brake(); //Make mrJonesDriving stop - - distanceRanger.read(); Returns the measurement from teh ulatrasonic ranger in mm + - mrJonesDriving->brake(); // Make mrJonesDriving stop instantly + - mrJonesDriving->read(); Returns the measurement from the ultrasonic ranger in mm ARDUINO METHODS: - - delay(1000); // Ardunio stops processing and continues in set state for duration - - Serial.println(distanceRanger.read()); // Prints ranger state to the serial monitor + - delay(1000); // Ardunio stops processing and continues in set state for duration in milliseconds 1000 = 1 second + - Serial.println(mrJonesDriving->read()); // Prints ranger state to the serial monitor - Serial.println("Hello World"); //Prints something to the serial monitor CONTROL STRUCTURES @@ -24,54 +24,35 @@ if (true){ Serial.println("This is false") } -if (distanceRanger.read() >= 100){ - Serial.println("Obstical is more than 100mm away") -} else if (distanceRanger.read() >= 50) { - Serial.println("Obstical is less than 50mm away") -} else { - mrJonesDriving->brake(); -} - -While (distanceRanger.read() >= 500) { - mrJonesDriving->driveForward(255,253); +While (true) { + mrJonesDriving->driveForward(200,200); } - - - Testing code: #include "AIDriver.h" -#include "Ultrasonic.h" - -//Instantiate a Ultrasonic Sensor called 'distanceRanger' - Ultrasonic distanceRanger(6, 7); // Declare a pointer to a AIDriver object AIDriver *mrJonesDriving; void setup(){ // sets up once as the program starts - //Configure and debug the serial monitor - Serial.begin(9600); - Serial.println("Debug Serial Monitor"); - //Instantiate a two wheeled to the pointer 'mrJonesDriving' mrJonesDriving = new AIDriver(); } void loop(){ // loops continuously 50 times a second // Read the distanceRanger ulstrasonic sensor and return the object distance in mm - Serial.println(distanceRanger.read()); + Serial.println(mrJonesDriving->read()); //obj->doSomething(); in this case make the object mrJonesDriving rotate to the right at a speed of 200 - mrJonesDriving->rotateRight(200); // speed can be between 0-255 + mrJonesDriving->rotateRight(125); // speed can be between 0-255 // wait 2000 milliseconds or 2 seconds delay(2000); - mrJonesDriving->rotateLeft(200); + mrJonesDriving->rotateLeft(125); delay(2000); //Make mrJonesDriving drive forward left wheel speed 200 and right wheel speeed 200 - mrJonesDriving->driveForward(200,200); - mrJonesDriving->driveBackward(200,200); - mrJonesDriving->brake(); + mrJonesDriving->driveForward(125,125); + delay(2000); + mrJonesDriving->driveBackward(125,125); delay(2000); }