From 46e97a4a00b7ffa7e28068d8d4e8b6a7f45d0f5c Mon Sep 17 00:00:00 2001 From: Alexander Klein Date: Wed, 10 Mar 2021 10:25:56 +0100 Subject: [PATCH] Initial commit --- .gitignore | 5 ++ .vscode/extensions.json | 7 ++ include/README | 39 ++++++++++ lib/README | 46 ++++++++++++ platformio.ini | 16 +++++ src/config.h | 23 ++++++ src/main.cpp | 153 ++++++++++++++++++++++++++++++++++++++++ src/motorControl.h | 0 src/mototControl.cpp | 0 9 files changed, 289 insertions(+) create mode 100644 .gitignore create mode 100644 .vscode/extensions.json create mode 100644 include/README create mode 100644 lib/README create mode 100644 platformio.ini create mode 100644 src/config.h create mode 100644 src/main.cpp create mode 100644 src/motorControl.h create mode 100644 src/mototControl.cpp diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..89cc49c --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +.pio +.vscode/.browse.c_cpp.db* +.vscode/c_cpp_properties.json +.vscode/launch.json +.vscode/ipch diff --git a/.vscode/extensions.json b/.vscode/extensions.json new file mode 100644 index 0000000..e80666b --- /dev/null +++ b/.vscode/extensions.json @@ -0,0 +1,7 @@ +{ + // See http://go.microsoft.com/fwlink/?LinkId=827846 + // for the documentation about the extensions.json format + "recommendations": [ + "platformio.platformio-ide" + ] +} diff --git a/include/README b/include/README new file mode 100644 index 0000000..194dcd4 --- /dev/null +++ b/include/README @@ -0,0 +1,39 @@ + +This directory is intended for project header files. + +A header file is a file containing C declarations and macro definitions +to be shared between several project source files. You request the use of a +header file in your project source file (C, C++, etc) located in `src` folder +by including it, with the C preprocessing directive `#include'. + +```src/main.c + +#include "header.h" + +int main (void) +{ + ... +} +``` + +Including a header file produces the same results as copying the header file +into each source file that needs it. Such copying would be time-consuming +and error-prone. With a header file, the related declarations appear +in only one place. If they need to be changed, they can be changed in one +place, and programs that include the header file will automatically use the +new version when next recompiled. The header file eliminates the labor of +finding and changing all the copies as well as the risk that a failure to +find one copy will result in inconsistencies within a program. + +In C, the usual convention is to give header files names that end with `.h'. +It is most portable to use only letters, digits, dashes, and underscores in +header file names, and at most one dot. + +Read more about using header files in official GCC documentation: + +* Include Syntax +* Include Operation +* Once-Only Headers +* Computed Includes + +https://gcc.gnu.org/onlinedocs/cpp/Header-Files.html diff --git a/lib/README b/lib/README new file mode 100644 index 0000000..6debab1 --- /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 a 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/platformio.ini b/platformio.ini new file mode 100644 index 0000000..74216ef --- /dev/null +++ b/platformio.ini @@ -0,0 +1,16 @@ +; PlatformIO Project Configuration File +; +; Build options: build flags, source filter +; Upload options: custom upload port, speed and extra flags +; Library options: dependencies, extra library storages +; Advanced options: extra scripting +; +; Please visit documentation for the other options and examples +; https://docs.platformio.org/page/projectconf.html + +[env:esp32doit-devkit-v1] +platform = espressif32 +board = esp32doit-devkit-v1 +framework = arduino +monitor_speed = 115200 +lib_deps = stempedia/DabbleESP32@^1.5.1 diff --git a/src/config.h b/src/config.h new file mode 100644 index 0000000..a1915fb --- /dev/null +++ b/src/config.h @@ -0,0 +1,23 @@ +//Pin Configs +#define M_DIR_11 15 +#define M_DIR_12 0 +#define M_PWM_1 5 +#define M_DIR_21 4 +#define M_DIR_22 2 +#define M_PWM_2 18 + +//Motors +#define LEFT_MOTOR 1 +#define RIGHT_MOTOR 2 +#define BOTH_MOTOR 3 + +//Directions +#define STOP 0 +#define FORWARD 1 +#define BACKWARD 2 + +//PWM Configs +#define PWM_FREQ 5000 +#define PWM_RES 8 +#define PWM_CHANNEL_M1 0 +#define PWM_CHANNEL_M2 1 \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp new file mode 100644 index 0000000..cfdc4fe --- /dev/null +++ b/src/main.cpp @@ -0,0 +1,153 @@ +#include + +#include "config.h" + +#define CUSTOM_SETTINGS +#define INCLUDE_GAMEPAD_MODULE +#include + + +// Functions +void setMotorDirection(uint8_t motor ,uint8_t direction = FORWARD); +void setMotorSpeed(uint8_t motor, uint8_t speed); + +void setup() { + Serial.begin(115200); // make sure your Serial Monitor is also set at this baud rate. + Dabble.begin("Kleiax-Rover"); //set bluetooth name of your device + + //SETUP PINS + pinMode(M_DIR_11, OUTPUT); + pinMode(M_DIR_12, OUTPUT); + pinMode(M_DIR_21, OUTPUT); + pinMode(M_DIR_22, OUTPUT); + + digitalWrite(M_DIR_11, LOW); + digitalWrite(M_DIR_12, LOW); + digitalWrite(M_DIR_21, LOW); + digitalWrite(M_DIR_22, LOW); + + ledcSetup(PWM_CHANNEL_M1, PWM_FREQ, PWM_RES); + ledcSetup(PWM_CHANNEL_M2, PWM_FREQ, PWM_RES); + ledcAttachPin(M_PWM_1, PWM_CHANNEL_M1); + ledcAttachPin(M_PWM_2, PWM_CHANNEL_M2); + ledcWrite(M_PWM_1, 0); + ledcWrite(M_PWM_2, 0); + + + //Start speed + setMotorSpeed(BOTH_MOTOR, 255); + +} + +void loop() { + Dabble.processInput(); //this function is used to refresh data obtained from smartphone.Hence calling this function is mandatory in order to get data properly from your mobile. + //Serial.print("KeyPressed: "); + if (GamePad.isUpPressed()) { + Serial.print("Up\n"); + setMotorDirection(BOTH_MOTOR, FORWARD); + } else if (GamePad.isDownPressed()) { + Serial.print("Down\n"); + setMotorDirection(BOTH_MOTOR, BACKWARD); + } else if (GamePad.isLeftPressed()) { + Serial.print("Left\n"); + setMotorDirection(LEFT_MOTOR, BACKWARD); + setMotorDirection(RIGHT_MOTOR, FORWARD); + } else if (GamePad.isRightPressed()) { + Serial.print("Right\n"); + setMotorDirection(LEFT_MOTOR, FORWARD); + setMotorDirection(RIGHT_MOTOR, BACKWARD); + + } else if (GamePad.isSquarePressed()) { + setMotorDirection(LEFT_MOTOR, FORWARD); + Serial.print("1\n"); + } else if (GamePad.isTrianglePressed()) { + setMotorDirection(LEFT_MOTOR, BACKWARD); + Serial.print("2\n"); + } else if (GamePad.isCirclePressed()) { + setMotorDirection(RIGHT_MOTOR, FORWARD); + Serial.print("3\n"); + } else if (GamePad.isCrossPressed()) { + setMotorDirection(RIGHT_MOTOR, BACKWARD); + Serial.print("4\n"); + + + + + } else { + setMotorDirection(BOTH_MOTOR, STOP); + } +} + +void setMotorDirection(uint8_t motor ,uint8_t direction) { + if (motor == LEFT_MOTOR) { + if (direction == FORWARD) { + digitalWrite(M_DIR_11, HIGH); + digitalWrite(M_DIR_12, LOW); + } else if (direction == BACKWARD) { + digitalWrite(M_DIR_11, LOW); + digitalWrite(M_DIR_12, HIGH); + } else if (direction == STOP) { + digitalWrite(M_DIR_11, LOW); + digitalWrite(M_DIR_12, LOW); + } else { + digitalWrite(M_DIR_21, LOW); + digitalWrite(M_DIR_22, LOW); + Serial.print("setDirection: undefined direction\n"); + } + } else if (motor == RIGHT_MOTOR) { + if (direction == FORWARD) { + digitalWrite(M_DIR_21, HIGH); + digitalWrite(M_DIR_22, LOW); + } else if (direction == BACKWARD) { + digitalWrite(M_DIR_21, LOW); + digitalWrite(M_DIR_22, HIGH); + } else if (direction == STOP) { + digitalWrite(M_DIR_21, LOW); + digitalWrite(M_DIR_22, LOW); + } else { + digitalWrite(M_DIR_21, LOW); + digitalWrite(M_DIR_22, LOW); + Serial.print("setDirection: undefined direction\n"); + } + } else if (motor == BOTH_MOTOR) { + if (direction == FORWARD) { + digitalWrite(M_DIR_11, HIGH); + digitalWrite(M_DIR_12, LOW); + digitalWrite(M_DIR_21, HIGH); + digitalWrite(M_DIR_22, LOW); + } else if (direction == BACKWARD) { + digitalWrite(M_DIR_11, LOW); + digitalWrite(M_DIR_12, HIGH); + digitalWrite(M_DIR_21, LOW); + digitalWrite(M_DIR_22, HIGH); + } else if (direction == STOP) { + digitalWrite(M_DIR_11, LOW); + digitalWrite(M_DIR_12, LOW); + digitalWrite(M_DIR_21, LOW); + digitalWrite(M_DIR_22, LOW); + }else { + digitalWrite(M_DIR_11, LOW); + digitalWrite(M_DIR_12, LOW); + digitalWrite(M_DIR_21, LOW); + digitalWrite(M_DIR_22, LOW); + Serial.print("setDirection: undefined direction\n"); + } + } else { + Serial.print("setDirection: undefined motor\n"); + } +} + +void setMotorSpeed(uint8_t motor, uint8_t speed) { + if (motor == LEFT_MOTOR) { + ledcWrite(PWM_CHANNEL_M1, speed); + } else if (motor == RIGHT_MOTOR) { + ledcWrite(PWM_CHANNEL_M2, speed); + } else if (motor == BOTH_MOTOR) { + ledcWrite(PWM_CHANNEL_M1, speed); + ledcWrite(PWM_CHANNEL_M2, speed); + } else { + ledcWrite(PWM_CHANNEL_M1, 0); + ledcWrite(PWM_CHANNEL_M2, 0); + Serial.print("setSpeed: undefined motor \n"); + } +} \ No newline at end of file diff --git a/src/motorControl.h b/src/motorControl.h new file mode 100644 index 0000000..e69de29 diff --git a/src/mototControl.cpp b/src/mototControl.cpp new file mode 100644 index 0000000..e69de29