Skip to content

Commit

Permalink
✨ feat: Cleaner can use Auto mode
Browse files Browse the repository at this point in the history
Add `Auto mode` function for ESP32 cleaner, Set the motor more `slower`:
`pwm` 100 to 50.

Issue #1 -Auto mode
  • Loading branch information
Hsun1031 committed Mar 19, 2023
1 parent b07adab commit 674bae2
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 5 deletions.
2 changes: 1 addition & 1 deletion include/header.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
#define CLEANER_DEFAULT_MOTOR_B01_CHANNEL 3
#define CLEANER_DEFAULT_MOTOR_B02_CHANNEL 4

#define CLEANER_DEFAULT_MOTOR_PWM 100
#define CLEANER_DEFAULT_MOTOR_PWM 50

// Ledc setting
#define CLEANER_DEFAULT_LEDC_FREQ 100000UL
Expand Down
7 changes: 7 additions & 0 deletions platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ default_envs = esp32doit-devkit-v1
[env:esp32doit-devkit-v1]
platform = espressif32@6.0.1
board = esp32doit-devkit-v1
monitor_raw = yes
monitor_filters =
direct
esp32_exception_decoder
build_flags =
-DCORE_DEBUG_LEVEL=5
-DCONFIG_ARDUHAL_LOG_COLORS=1
framework = arduino
monitor_speed = 115200
lib_deps =
Expand Down
61 changes: 57 additions & 4 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,13 @@
#include "components/my_network.h"

TaskHandle_t Task1;
VL53L0X_value value;

void Task1code(void* parameter);

void autoTask();
uint8_t autoModeNum = 0;

char *sta_ssid = NULL;
char *sta_passwd = NULL;
char *ap_ssid = NULL;
Expand Down Expand Up @@ -39,6 +43,7 @@ Network_conf network_conf = {
void setup() {
Serial.begin(115200);
while(!Serial);
Serial.setDebugOutput(true);

setup_data(&my_cleaner_conf, &network_conf);
setup_module(&my_cleaner_conf);
Expand All @@ -47,31 +52,43 @@ void setup() {
setup_server(&my_cleaner_conf, &network_conf, &cleaner_mode, &url);

xTaskCreatePinnedToCore(Task1code, "Task1", 10240, NULL, 1, &Task1, 1);

if(get_electricity_value() <= 1500) {
Serial.printf("Can not get Battery value, enter USB mode,\n");
set_IRF520_PWM(0);
motor_stop();
cleaner_mode = 1;
} else {
set_IRF520_PWM(255);
}

delay(500);
}

void loop() {
loop_server();
if(cleaner_mode == 0) { // OTA mode.
motor_stop();
autoModeNum = 0;
if(!update_loop(url)) {
cleaner_mode = 2;
ESP.restart();
} else {
cleaner_mode = 2;
}
} else if(cleaner_mode == 1) { // Game mode.

autoModeNum = 0;
} else if(cleaner_mode == 2) { // Auto mode.

autoTask();
}
}

void Task1code(void* parameter) {
uint16_t k = 0;

for(;;) {
VL53L0X_value value = loop_VL53L0X();
Serial.printf("%d\t%d\n", value.left, value.right);
value = loop_VL53L0X();
ESP_LOGI("VL53L0X", "%d\t%d", value.left, value.right);

if( k >= 29 ) {
show_dashboard(cleaner_mode);
Expand All @@ -82,3 +99,39 @@ void Task1code(void* parameter) {
vTaskDelay(30 / portTICK_PERIOD_MS);
}
}
unsigned long autoModeTurnTime = 0;
void autoTask() {
if(cleaner_mode != 2) {
motor_stop();
return;
}

if(autoModeNum == 0) {
if(( value.left != -1 && value.left <= 50) || (value.right != -1 && value.right <= 50) ) {
motor_stop();
autoModeNum = 1;
} else {
motor_forward();
}
} else if(autoModeNum == 1) {
if(value.left >= 70 || value.right >= 70) {
autoModeNum = 2;
} else {
motor_backward();
}
} else if(autoModeNum == 2) {
if(value.left > value.right) {
motor_left();
} else {
motor_right();
}
autoModeNum = 3;
autoModeTurnTime = millis();

} else if(autoModeNum == 3) {
if(value.left >= 80 && value.right >= 80) {
motor_stop();
autoModeNum = 0;
}
}
}

0 comments on commit 674bae2

Please sign in to comment.