diff --git a/docs/PINOUT.md b/docs/PINOUT.md index 552b178..adc56d9 100644 --- a/docs/PINOUT.md +++ b/docs/PINOUT.md @@ -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 | @@ -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 --- diff --git a/lib/Modules/IR/IR.cpp b/lib/Modules/IR/IR.cpp index fe47cc2..247c76f 100644 --- a/lib/Modules/IR/IR.cpp +++ b/lib/Modules/IR/IR.cpp @@ -1,32 +1,17 @@ #include #include -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); } \ No newline at end of file diff --git a/lib/Modules/IR/IR.h b/lib/Modules/IR/IR.h index 8174d5a..7fac28d 100644 --- a/lib/Modules/IR/IR.h +++ b/lib/Modules/IR/IR.h @@ -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 \ No newline at end of file diff --git a/lib/Modules/LedControl/LedControl.cpp b/lib/Modules/LedControl/LedControl.cpp index 6d23113..b62b69d 100644 --- a/lib/Modules/LedControl/LedControl.cpp +++ b/lib/Modules/LedControl/LedControl.cpp @@ -1,52 +1,43 @@ #include #include -// 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; } } \ No newline at end of file diff --git a/lib/Modules/LedControl/LedControl.h b/lib/Modules/LedControl/LedControl.h index f38ceb4..4a39bb9 100644 --- a/lib/Modules/LedControl/LedControl.h +++ b/lib/Modules/LedControl/LedControl.h @@ -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; diff --git a/lib/README b/lib/README new file mode 100644 index 0000000..2593a33 --- /dev/null +++ b/lib/README @@ -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 +#include + +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 diff --git a/lib/Task/IRReading/IRTask.cpp b/lib/Task/IRReading/IRTask.cpp index ba89bb0..d61a0f1 100644 --- a/lib/Task/IRReading/IRTask.cpp +++ b/lib/Task/IRReading/IRTask.cpp @@ -1,89 +1,84 @@ -#include +#include -#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(_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."); } \ No newline at end of file diff --git a/lib/Task/IRReading/IRTask.h b/lib/Task/IRReading/IRTask.h index 20d8f74..4882b7d 100644 --- a/lib/Task/IRReading/IRTask.h +++ b/lib/Task/IRReading/IRTask.h @@ -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: diff --git a/lib/Task/ModeSelection/ModeSelectionTask.cpp b/lib/Task/ModeSelection/ModeSelectionTask.cpp index 8d7aa9a..0b42da8 100644 --- a/lib/Task/ModeSelection/ModeSelectionTask.cpp +++ b/lib/Task/ModeSelection/ModeSelectionTask.cpp @@ -39,30 +39,55 @@ void ModeSelectionTask::modeSelectionTaskFunction(void *parameter) { case 1: // WebSocketTask only Serial.println("Mode 1: Manual Control"); ledControl.setMode(1); - self->_ultrasonicTask.stopTask(); + if (self->_ultrasonicTask.getIsRunning()) { + self->_ultrasonicTask.stopTask(); + } + if (self->_irTask.getIsRunning()) { + self->_irTask.stopTask(); + } break; case 2: // WebSocketTask + UltrasonicTask Serial.println("Mode 2: Obstacle Avoidance"); ledControl.setMode(2); - self->_ultrasonicTask.startTask(); + if (!self->_ultrasonicTask.getIsRunning()) { + self->_ultrasonicTask.startTask(); + } + if (self->_irTask.getIsRunning()) { + self->_irTask.stopTask(); + } break; case 3: // IRTask only (Line Following) Serial.println("Mode 3: Line Following"); ledControl.setMode(3); - self->_ultrasonicTask.stopTask(); + if (self->_ultrasonicTask.getIsRunning()) { + self->_ultrasonicTask.stopTask(); + } + if (!self->_irTask.getIsRunning()) { + self->_irTask.startTask(); + } break; case 4: // Patrol Mode or Tuning Serial.println("Mode 4: Auto Patrol"); ledControl.setMode(4); - self->_ultrasonicTask.stopTask(); + if (self->_ultrasonicTask.getIsRunning()) { + self->_ultrasonicTask.stopTask(); + } + if (self->_irTask.getIsRunning()) { + self->_irTask.stopTask(); + } break; default: Serial.println("Unknown mode. No action taken."); - self->_ultrasonicTask.stopTask(); + if (self->_ultrasonicTask.getIsRunning()) { + self->_ultrasonicTask.stopTask(); + } + if (self->_irTask.getIsRunning()) { + self->_irTask.stopTask(); + } break; } diff --git a/lib/Task/Ultrasonic/UltrasonicTask.cpp b/lib/Task/Ultrasonic/UltrasonicTask.cpp index ecd11de..528f88b 100644 --- a/lib/Task/Ultrasonic/UltrasonicTask.cpp +++ b/lib/Task/Ultrasonic/UltrasonicTask.cpp @@ -15,6 +15,10 @@ void UltrasonicTask::startTask() { } } +bool UltrasonicTask::getIsRunning() { + return _taskRunning; +} + void UltrasonicTask::stopTask() { if (_taskHandle != NULL) { speed = 0; diff --git a/lib/Task/Ultrasonic/UltrasonicTask.h b/lib/Task/Ultrasonic/UltrasonicTask.h index 0b61337..cd6e397 100644 --- a/lib/Task/Ultrasonic/UltrasonicTask.h +++ b/lib/Task/Ultrasonic/UltrasonicTask.h @@ -9,10 +9,22 @@ class UltrasonicTask { public: UltrasonicTask(Ultrasonic &ultrasonic); void startTask(); + bool getIsRunning(); void stopTask(); +// TOTAL 400KB +// ULTRASONIC -> 12KB + +// 400 - 12 = 388KB + + +//400KB x 1024 = 409600 Bytes. +// 409600 / 4 = 102400 Word + +// 1 WORD = 4 Bytes, +// 1024 Bytes = 1KB private: - const uint32_t _taskStackSize = 3072; + const uint32_t _taskStackSize = 3072; // word -> 12288 Byte -> 12KB const UBaseType_t _taskPriority = 5; int speed; Ultrasonic _ultrasonic; diff --git a/platformio.ini b/platformio.ini index 063aad3..eadf7ba 100644 --- a/platformio.ini +++ b/platformio.ini @@ -15,9 +15,9 @@ framework = arduino monitor_port = /dev/cu.usbmodem2101 monitor_speed = 9600 board_build.partitions = default.csv -upload_port = /dev/cu.usbmodem2101 -; upload_port = 192.168.4.1 -; upload_protocol = ota +; upload_port = /dev/cu.usbmodem2101 +upload_port = 192.168.4.1 +upload_protocol = ota lib_deps = esphome/ESPAsyncWebServer-esphome@^3.2.2 links2004/WebSockets@^2.6.1 diff --git a/src/main.cpp b/src/main.cpp index 82e1ce1..8e6c311 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -6,7 +6,7 @@ #include #include #include -#include +#include #include #include #include @@ -27,6 +27,9 @@ // Ultrasonic #define TRIGGER_PIN 20 #define ECHO_PIN 21 +// IR Line Follow +#define IR_LEFT 10 +#define IR_RIGHT 7 // BAT CALC #define BATTERY_ADC_PIN 0 #define VOLTAGE_DIVIDER_FACTOR 2 @@ -43,7 +46,7 @@ int motorDirection = 0; EEPROMConfig eepromConfig; Ultrasonic ultrasonic(TRIGGER_PIN, ECHO_PIN); Buzzer buzzer(1); -IR ir; +IR ir(IR_LEFT, IR_RIGHT); WebServerTask webServerTask; WebSocketTask webSocketTask; @@ -74,11 +77,11 @@ void setup() { buzzer.begin(); ultrasonic.begin(); + ir.begin(); delay(1000); Serial.println("Setting up WiFi..."); - String macAddr = WiFi.macAddress(); String lastFourCharMacAddr = macAddr.substring(macAddr.length() - 4); String ssid = String(SSID) + "-" + lastFourCharMacAddr; @@ -113,4 +116,6 @@ void setup() { Serial.println("open in browser http://cuybot.local"); } -void loop() {} +void loop() { + +}