Skip to content

Commit

Permalink
update pinout
Browse files Browse the repository at this point in the history
  • Loading branch information
deascript101 committed Nov 1, 2024
1 parent 9dff346 commit b9b675a
Show file tree
Hide file tree
Showing 13 changed files with 181 additions and 127 deletions.
4 changes: 2 additions & 2 deletions docs/PINOUT.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
| `PWM_Left_2D` | GPIO6 | PWM motor kiri (direksi 2) |
| `IR_Right` | GPIO7 | Sensor IR kanan |
| `STBY` | GPIO8 | Pin Standby Motor |
| `LedState2` | GPIO2 | Indikator Mode LED Kombinasi 2 |
| `LedState2` | GPIO9 | Indikator Mode LED Kombinasi 2 |
| `IR_Left` | GPIO10 | Sensor IR kiri |
| `Trig` | GPIO20 | Trigger sensor ultrasonik |
| `Echo` | GPIO21 | Echo sensor ultrasonik |
Expand All @@ -23,7 +23,7 @@
- LedState1 LOW + LedState2 LOW = MODE 1 ON
- LedState1 LOW + LedState2 HIGH = MODE 2 ON
- LedState1 HIGH + LedState2 LOW = MODE 3 ON
- LedState1 HIGH + LedState2 HIGH = MODE 4 ON
- LedState1 HIGH + LedState2 HIGH = MODE 4 ON

---

Expand Down
29 changes: 7 additions & 22 deletions lib/Modules/IR/IR.cpp
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);
}
24 changes: 9 additions & 15 deletions lib/Modules/IR/IR.h
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
21 changes: 6 additions & 15 deletions lib/Modules/LedControl/LedControl.cpp
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;
}
}
4 changes: 1 addition & 3 deletions lib/Modules/LedControl/LedControl.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,12 @@
class LedControl
{
public:
// Constructor & Destructor
LedControl(int pin_A = LED_STATE_1, int pin_B = LED_STATE_2);
~LedControl();

// Methods for controlling the LEDs
void turnOn(int led);
void turnOff(int led);
void setMode(int mode); // Dynamic Mode
void setMode(int mode); // Dynamic LED Interaction

private:
const int _pin_A;
Expand Down
46 changes: 46 additions & 0 deletions lib/README
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
105 changes: 50 additions & 55 deletions lib/Task/IRReading/IRTask.cpp
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.");
}
3 changes: 1 addition & 2 deletions lib/Task/IRReading/IRTask.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ class IRTask {
IRTask(IR &ir, MotorDriver &rightMotor, MotorDriver &leftMotor);
void startTask();
void stopTask();
void suspendTask();
void resumeTask();
bool getIsRunning();
TaskHandle_t getTaskHandle();

private:
Expand Down
Loading

0 comments on commit b9b675a

Please sign in to comment.