Skip to content

Commit

Permalink
Integrate and compress libraries
Browse files Browse the repository at this point in the history
  • Loading branch information
benpaddlejones committed Dec 18, 2023
1 parent d2a3662 commit ffbdb6f
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 210 deletions.
28 changes: 28 additions & 0 deletions Ardunio_2Wheel_Robot_Boilerplate/myAIDriver/AIDriver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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(){
Expand Down
13 changes: 4 additions & 9 deletions Ardunio_2Wheel_Robot_Boilerplate/myAIDriver/AIDriver.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
72 changes: 1 addition & 71 deletions Ardunio_2Wheel_Robot_Boilerplate/myAIDriver/L298N.cpp
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
#include "L298N.h"

typedef void (*CallBackFunction)();

L298N::L298N(uint8_t pinEnable, uint8_t pinIN1, uint8_t pinIN2) {
_pinEnable = pinEnable;
_pinIN1 = pinIN1;
_pinIN2 = pinIN2;
_pwmVal = 255;
_isMoving = false;
_canMove = true;
_lastMs = 0;
_direction = STOP;

pinMode(_pinEnable, OUTPUT);
Expand Down Expand Up @@ -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);
Expand All @@ -130,6 +62,4 @@ boolean L298N::isMoving() {

L298N::Direction L298N::getDirection() {
return _direction;
}

void L298N::fakeCallback() {}
}
63 changes: 25 additions & 38 deletions Ardunio_2Wheel_Robot_Boilerplate/myAIDriver/L298N.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
27 changes: 0 additions & 27 deletions Ardunio_2Wheel_Robot_Boilerplate/myAIDriver/Ultrasonic.cpp

This file was deleted.

20 changes: 0 additions & 20 deletions Ardunio_2Wheel_Robot_Boilerplate/myAIDriver/Ultrasonic.h

This file was deleted.

20 changes: 6 additions & 14 deletions Ardunio_2Wheel_Robot_Boilerplate/myAIDriver/myAIDriver.ino
Original file line number Diff line number Diff line change
@@ -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);
}
}
Loading

0 comments on commit ffbdb6f

Please sign in to comment.