-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9dff346
commit b9b675a
Showing
13 changed files
with
181 additions
and
127 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,17 @@ | ||
#include <IR/IR.h> | ||
#include <Arduino.h> | ||
|
||
IR::IR() {} | ||
IR::IR(int leftPin, int rightPin): _left_pin(leftPin), _right_pin(rightPin) {} | ||
|
||
void IR::begin() { | ||
pinMode(_IR_Left_Pin, INPUT); | ||
pinMode(_IR_Right_Pin, INPUT); | ||
|
||
_IR_left = digitalRead(_IR_Left_Pin); | ||
_IR_right = digitalRead(_IR_Right_Pin); | ||
pinMode(_left_pin, INPUT); | ||
pinMode(_right_pin, INPUT); | ||
} | ||
|
||
int IR::getIRLeft() { | ||
_IR_left = digitalRead(_IR_Left_Pin); | ||
return _IR_left; | ||
int IR::getLeftSignal() { | ||
return digitalRead(_left_pin); | ||
} | ||
|
||
int IR::getIRRight() { | ||
_IR_right = digitalRead(_IR_Right_Pin); | ||
return _IR_right; | ||
} | ||
|
||
void IR::printLog() { | ||
_IR_left = digitalRead(_IR_Left_Pin); | ||
_IR_right = digitalRead(_IR_Right_Pin); | ||
|
||
Serial.print("IR L: "); | ||
Serial.println(_IR_left); | ||
Serial.print("IR R: "); | ||
Serial.println(_IR_right); | ||
int IR::getRightSignal() { | ||
return digitalRead(_right_pin); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,17 @@ | ||
#ifndef IR_H | ||
#define IR_H | ||
|
||
#define IR_LEFT_PIN 10 | ||
#define IR_RIGHT_PIN 7 | ||
|
||
class IR | ||
{ | ||
public: | ||
IR(); | ||
void begin(); | ||
void printLog(); | ||
int getIRLeft(); | ||
int getIRRight(); | ||
|
||
private: | ||
const int _IR_Left_Pin = IR_LEFT_PIN; | ||
const int _IR_Right_Pin = IR_RIGHT_PIN; | ||
int _IR_left; | ||
int _IR_right; | ||
public: | ||
IR(int leftPin, int rightPin); | ||
void begin(); | ||
int getLeftSignal(); | ||
int getRightSignal(); | ||
|
||
private: | ||
const int _left_pin; | ||
const int _right_pin; | ||
}; | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,52 +1,43 @@ | ||
#include <LedControl/LedControl.h> | ||
#include <Arduino.h> | ||
|
||
// Constructor to set pin modes | ||
LedControl::LedControl(int pin_A, int pin_B) : _pin_A(pin_A), _pin_B(pin_B) { | ||
pinMode(_pin_A, OUTPUT); | ||
pinMode(_pin_B, OUTPUT); | ||
} | ||
|
||
// Destructor | ||
LedControl::~LedControl() { | ||
// Nothing specific for now | ||
} | ||
LedControl::~LedControl() {} | ||
|
||
// Turn on a specific LED | ||
void LedControl::turnOn(int led) { | ||
digitalWrite(led, HIGH); | ||
} | ||
|
||
// Turn off a specific LED | ||
void LedControl::turnOff(int led) { | ||
digitalWrite(led, LOW); | ||
} | ||
|
||
// Apply state to the LEDs dynamically | ||
void LedControl::applyLedState(bool state_A, bool state_B) { | ||
digitalWrite(_pin_A, state_A ? HIGH : LOW); | ||
digitalWrite(_pin_B, state_B ? HIGH : LOW); | ||
} | ||
|
||
// Dynamic setMode based on the input mode | ||
void LedControl::setMode(int mode) { | ||
switch (mode) { | ||
case 1: // RED -> Mode 1 | ||
case 1: // Mode 1 | ||
applyLedState(LOW, LOW); | ||
break; | ||
case 2: // GREEN -> Mode 2 | ||
case 2: // Mode 2 | ||
applyLedState(HIGH, LOW); | ||
break; | ||
case 3: // LIGHTBLUE -> Mode 3 | ||
case 3: // Mode 3 | ||
applyLedState(LOW, HIGH); | ||
break; | ||
case 4: // YELLOW -> Mode 4 | ||
case 4: // Mode 4 | ||
applyLedState(HIGH, HIGH); | ||
break; | ||
default: | ||
// Default case if mode is not recognized | ||
Serial.println("Invalid mode"); | ||
applyLedState(LOW, LOW); // Default to Mode 1 (OFF) | ||
applyLedState(LOW, LOW); // Default to Mode 1 | ||
break; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
|
||
This directory is intended for project specific (private) libraries. | ||
PlatformIO will compile them to static libraries and link into executable file. | ||
|
||
The source code of each library should be placed in an own separate directory | ||
("lib/your_library_name/[here are source files]"). | ||
|
||
For example, see a structure of the following two libraries `Foo` and `Bar`: | ||
|
||
|--lib | ||
| | | ||
| |--Bar | ||
| | |--docs | ||
| | |--examples | ||
| | |--src | ||
| | |- Bar.c | ||
| | |- Bar.h | ||
| | |- library.json (optional, custom build options, etc) https://docs.platformio.org/page/librarymanager/config.html | ||
| | | ||
| |--Foo | ||
| | |- Foo.c | ||
| | |- Foo.h | ||
| | | ||
| |- README --> THIS FILE | ||
| | ||
|- platformio.ini | ||
|--src | ||
|- main.c | ||
|
||
and a contents of `src/main.c`: | ||
``` | ||
#include <Foo.h> | ||
#include <Bar.h> | ||
|
||
int main (void) | ||
{ | ||
... | ||
} | ||
|
||
``` | ||
|
||
PlatformIO Library Dependency Finder will find automatically dependent | ||
libraries scanning project source files. | ||
|
||
More information about PlatformIO Library Dependency Finder | ||
- https://docs.platformio.org/page/librarymanager/ldf.html |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,89 +1,84 @@ | ||
#include <IRReading/IRTask.h> | ||
#include <IRReading/IRTask.h> | ||
|
||
#define MAX_PWM_SPEED 255 | ||
#define MIN_PWM_SPEED 0 | ||
extern int motorSpeed; | ||
extern int motorDirection; | ||
|
||
IRTask::IRTask(IR &ir, MotorDriver &rightMotor, MotorDriver &leftMotor) | ||
: _ir(ir), _rightMotor(rightMotor), _leftMotor(leftMotor), _taskHandle(NULL), _taskRunning(false) { | ||
motorMaxSpeed = EEPROMConfig::getMemInt(1); | ||
motorWeight = EEPROMConfig::getMemInt(2); | ||
} | ||
|
||
TaskHandle_t IRTask::getTaskHandle() { | ||
return _taskHandle; | ||
// motorMaxSpeed = EEPROMConfig::getMemInt(1); | ||
// motorWeight = EEPROMConfig::getMemInt(2); | ||
// motorSpeed = motorMaxSpeed; | ||
} | ||
|
||
void IRTask::startTask() { | ||
if (_taskHandle == NULL) { | ||
_taskRunning = true; | ||
xTaskCreate(irMeasureTask, "IRTask", _taskStackSize, this, _taskPriority, &_taskHandle); | ||
}else { | ||
_taskRunning = true; | ||
Serial.println("IR task started."); | ||
} | ||
} | ||
|
||
void IRTask::stopTask() { | ||
if (_taskHandle != NULL) { | ||
motorSpeed = 0; | ||
_taskRunning = false; | ||
vTaskDelete(_taskHandle); | ||
_taskHandle = NULL; | ||
_rightMotor.stop(); | ||
_leftMotor.stop(); | ||
Serial.println("IR Task stopped."); | ||
Serial.println("IR task stopped."); | ||
} | ||
} | ||
|
||
void IRTask::suspendTask() { | ||
if (_taskHandle != NULL) { | ||
vTaskSuspend(_taskHandle); | ||
_rightMotor.stop(); | ||
_leftMotor.stop(); | ||
Serial.println("IR Task suspended."); | ||
} | ||
bool IRTask::getIsRunning() { | ||
return _taskRunning; | ||
} | ||
|
||
void IRTask::resumeTask() { | ||
if (_taskHandle != NULL) { | ||
vTaskResume(_taskHandle); | ||
Serial.println("IR Task resumed."); | ||
} | ||
} | ||
|
||
void IRTask::irMeasureTask(void *_parameters) { | ||
IRTask *self = static_cast<IRTask *>(_parameters); | ||
self->_ir.begin(); | ||
|
||
while (self->_taskRunning) { | ||
int irLeft = self->_ir.getIRLeft(); | ||
int irRight = self->_ir.getIRRight(); | ||
|
||
self->motorMaxSpeed = EEPROMConfig::getMemInt(1); | ||
self->motorWeight = EEPROMConfig::getMemInt(2); | ||
while (self->_taskRunning) { | ||
int irLeft = self->_ir.getLeftSignal(); | ||
int irRight = self->_ir.getRightSignal(); | ||
|
||
int leftMotorSpeed = self->motorMaxSpeed; | ||
int rightMotorSpeed = self->motorMaxSpeed; | ||
motorSpeed = 100; | ||
|
||
if (irLeft == LOW && irRight == HIGH) { | ||
leftMotorSpeed = self->motorMaxSpeed - self->motorWeight; | ||
rightMotorSpeed = self->motorMaxSpeed + self->motorWeight; | ||
} else if (irLeft == HIGH && irRight == LOW) { | ||
leftMotorSpeed = self->motorMaxSpeed + self->motorWeight; | ||
rightMotorSpeed = self->motorMaxSpeed - self->motorWeight; | ||
} else if (irLeft == LOW && irRight == LOW) { | ||
self->_rightMotor.stop(); | ||
self->_leftMotor.stop(); | ||
vTaskDelay(pdMS_TO_TICKS(10)); | ||
continue; | ||
} else { | ||
leftMotorSpeed = self->motorMaxSpeed; | ||
rightMotorSpeed = self->motorMaxSpeed; | ||
motorDirection = 0; | ||
} else if(irLeft == HIGH && irRight == LOW) { | ||
motorDirection = 1; | ||
} else if(irLeft == HIGH && irRight == HIGH) { | ||
motorDirection = -1; | ||
}else { | ||
motorSpeed = 0; | ||
motorDirection = 0; | ||
} | ||
|
||
self->_rightMotor.forward(self->motorMaxSpeed); | ||
self->_leftMotor.forward(self->motorMaxSpeed); | ||
vTaskDelay(pdMS_TO_TICKS(10)); | ||
// self->motorMaxSpeed = EEPROMConfig::getMemInt(1); | ||
// self->motorWeight = EEPROMConfig::getMemInt(2); | ||
|
||
// int leftMotorSpeed = self->motorMaxSpeed; | ||
// int rightMotorSpeed = self->motorMaxSpeed; | ||
|
||
// if (irLeft == LOW && irRight == HIGH) { | ||
// leftMotorSpeed = self->motorMaxSpeed - self->motorWeight; | ||
// rightMotorSpeed = self->motorMaxSpeed + self->motorWeight; | ||
// } else if (irLeft == HIGH && irRight == LOW) { | ||
// leftMotorSpeed = self->motorMaxSpeed + self->motorWeight; | ||
// rightMotorSpeed = self->motorMaxSpeed - self->motorWeight; | ||
// } else if (irLeft == LOW && irRight == LOW) { | ||
// self->_rightMotor.stop(); | ||
// self->_leftMotor.stop(); | ||
// vTaskDelay(pdMS_TO_TICKS(10)); | ||
// continue; | ||
// } else { | ||
// leftMotorSpeed = self->motorMaxSpeed; | ||
// rightMotorSpeed = self->motorMaxSpeed; | ||
// } | ||
// self->_rightMotor.forward(self->motorMaxSpeed); | ||
// self->_leftMotor.forward(self->motorMaxSpeed); | ||
|
||
|
||
vTaskDelay(pdMS_TO_TICKS(50)); | ||
} | ||
|
||
Serial.println("Ultrasonic taskrunning false."); | ||
self->_taskRunning = false; | ||
Serial.println("IR taskrunning false."); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.