From 94fd4b0606d9dd3c66491969356fd96366a6490f Mon Sep 17 00:00:00 2001 From: martin chaparro Date: Sun, 23 Aug 2020 14:44:44 -0500 Subject: [PATCH] cotrol on of --- .gitignore | 9 ++-- .travis.yml | 10 ++-- .vscode/extensions.json | 7 +++ include/README | 39 ++++++++++++++ lib/README | 46 ++++++++++++++++ platformio.ini | 15 ++++++ src/main.cpp | 117 ++++++++++++++++++++++++++++++++++++++++ test/README | 11 ++++ 8 files changed, 245 insertions(+), 9 deletions(-) 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/main.cpp create mode 100644 test/README diff --git a/.gitignore b/.gitignore index 5dac9f5..89cc49c 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ -.pioenvs -.piolibdeps -.clang_complete -.gcc-flags.json +.pio +.vscode/.browse.c_cpp.db* +.vscode/c_cpp_properties.json +.vscode/launch.json +.vscode/ipch diff --git a/.travis.yml b/.travis.yml index 9443843..7c486f1 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,6 +1,6 @@ # Continuous Integration (CI) is the practice, in software # engineering, of merging all developer working copies with a shared mainline -# several times a day < http://docs.platformio.org/page/ci/index.html > +# several times a day < https://docs.platformio.org/page/ci/index.html > # # Documentation: # @@ -8,13 +8,13 @@ # < https://docs.travis-ci.com/user/integration/platformio/ > # # * PlatformIO integration with Travis CI -# < http://docs.platformio.org/page/ci/travis.html > +# < https://docs.platformio.org/page/ci/travis.html > # # * User Guide for `platformio ci` command -# < http://docs.platformio.org/page/userguide/cmd_ci.html > +# < https://docs.platformio.org/page/userguide/cmd_ci.html > # # -# Please choice one of the following templates (proposed below) and uncomment +# Please choose one of the following templates (proposed below) and uncomment # it (remove "# " before each line) or use own configuration according to the # Travis CI documentation (see above). # @@ -42,7 +42,7 @@ # -# Template #2: The project is intended to by used as a library with examples +# Template #2: The project is intended to be used as a library with examples. # # language: python diff --git a/.vscode/extensions.json b/.vscode/extensions.json new file mode 100644 index 0000000..0f0d740 --- /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..191a594 --- /dev/null +++ b/platformio.ini @@ -0,0 +1,15 @@ +; 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:mhetesp32devkit] +platform = espressif32 +board = mhetesp32devkit +framework = arduino +monitor_speed = 115200 \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp new file mode 100644 index 0000000..d158d9b --- /dev/null +++ b/src/main.cpp @@ -0,0 +1,117 @@ +#include +#include +#include +TaskHandle_t TareaProcesador1; +void loop1(void *pvParameters); +volatile int left_motor, throttle_left_motor, throttle_counter_left_motor, throttle_left_motor_memory; +volatile int right_motor, throttle_right_motor, throttle_counter_right_motor, throttle_right_motor_memory; +volatile int dir = 0; +bool FWD = HIGH; +bool BWD = LOW; + +#define STEP_1 13 +#define DIR_1 25 +#define STEP_2 33 +#define DIR_2 14 +MPU6050 mpu6050(Wire); + +float ax, ay, az; +long timer = 0; +void setup() +{ + pinMode(23, OUTPUT); + pinMode(STEP_1, OUTPUT); + pinMode(STEP_2, OUTPUT); + pinMode(DIR_1, OUTPUT); + pinMode(DIR_2, OUTPUT); + digitalWrite(DIR_1, LOW); + digitalWrite(DIR_2, LOW); + Wire.begin(); + Serial.begin(115200); + Wire.begin(); + xTaskCreatePinnedToCore(loop1, "Tarea1", 10000, NULL, 1, &TareaProcesador1, 0); + + delay(2000); // Pause for 2 seconds + + mpu6050.begin(); + mpu6050.calcGyroOffsets(false); +} + +void loop() +{ + mpu6050.update(); + if (millis() - timer > 10) + { + + // ax = mpu6050.getAngleX(); + ay = mpu6050.getAngleY(); + //az = mpu6050.getAngleZ(); + + /* Serial.print(ax), Serial.print(","); + Serial.print(ay), Serial.print(","); + Serial.println(az);*/ + if (ay > 4) + { + dir = -1; + } + else if (ay < -4) + { + dir = 1; + } + else + { + dir = 0; + } + throttle_left_motor = 6 * dir; + throttle_right_motor = 6 * dir; + + timer = millis(); + } +} + +void loop1(void *pvParameters) +{ + for (;;) + { + + throttle_counter_left_motor++; //Increase the throttle_counter_left_motor variable by 1 every time this routine is executed + if (throttle_counter_left_motor > throttle_left_motor_memory) + { //If the number of loops is larger then the throttle_left_motor_memory variable + throttle_counter_left_motor = 0; //Reset the throttle_counter_left_motor variable + throttle_left_motor_memory = throttle_left_motor; //Load the next throttle_left_motor variable + if (throttle_left_motor_memory < 0) + { //If the throttle_left_motor_memory is negative + digitalWrite(DIR_1, BWD); //Set output 3 low to reverse the direction of the stepper controller + throttle_left_motor_memory *= -1; //Invert the throttle_left_motor_memory variable + } + else + digitalWrite(DIR_1, FWD); //Set output 3 high for a forward direction of the stepper motor + } + else if (throttle_counter_left_motor == 1) + digitalWrite(STEP_1, HIGH); //Set output 2 high to create a pulse for the stepper controller + else if (throttle_counter_left_motor == 2) + digitalWrite(STEP_1, LOW); //Set output 2 low because the pulse only has to last for 20us + + //right motor pulse calculations + throttle_counter_right_motor++; //Increase the throttle_counter_right_motor variable by 1 every time the routine is executed + if (throttle_counter_right_motor > throttle_right_motor_memory) + { //If the number of loops is larger then the throttle_right_motor_memory variable + throttle_counter_right_motor = 0; //Reset the throttle_counter_right_motor variable + throttle_right_motor_memory = throttle_right_motor; //Load the next throttle_right_motor variable + if (throttle_right_motor_memory < 0) + { //If the throttle_right_motor_memory is negative + digitalWrite(DIR_2, BWD); //Set output 5 low to reverse the direction of the stepper controller + throttle_right_motor_memory *= -1; //Invert the throttle_right_motor_memory variable + } + else + digitalWrite(DIR_2, FWD); + ; //Set output 5 high for a forward direction of the stepper motor + } + else if (throttle_counter_right_motor == 1) + digitalWrite(STEP_2, HIGH); //Set output 4 high to create a pulse for the stepper controller + else if (throttle_counter_right_motor == 2) + digitalWrite(STEP_2, LOW); + + delay(1); + } +} \ No newline at end of file diff --git a/test/README b/test/README new file mode 100644 index 0000000..df5066e --- /dev/null +++ b/test/README @@ -0,0 +1,11 @@ + +This directory is intended for PIO Unit Testing and project tests. + +Unit Testing is a software testing method by which individual units of +source code, sets of one or more MCU program modules together with associated +control data, usage procedures, and operating procedures, are tested to +determine whether they are fit for use. Unit testing finds problems early +in the development cycle. + +More information about PIO Unit Testing: +- https://docs.platformio.org/page/plus/unit-testing.html