From ae9c6451a974d0f984157804a2dd8454df8af310 Mon Sep 17 00:00:00 2001 From: jpunkt Date: Tue, 14 Dec 2021 20:07:55 +0100 Subject: [PATCH] initial commit --- .gitignore | 5 +++ .vscode/extensions.json | 7 +++ include/Motor.h | 15 +++++++ include/README | 39 +++++++++++++++++ lib/README | 46 +++++++++++++++++++ platformio.ini | 16 +++++++ src/Motor.cpp | 34 +++++++++++++++ src/main.cpp | 97 +++++++++++++++++++++++++++++++++++++++++ test/README | 11 +++++ 9 files changed, 270 insertions(+) create mode 100644 .gitignore create mode 100644 .vscode/extensions.json create mode 100644 include/Motor.h create mode 100644 include/README create mode 100644 lib/README create mode 100644 platformio.ini create mode 100644 src/Motor.cpp create mode 100644 src/main.cpp create mode 100644 test/README 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..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/Motor.h b/include/Motor.h new file mode 100644 index 0000000..03df795 --- /dev/null +++ b/include/Motor.h @@ -0,0 +1,15 @@ +#include + +class Motor +{ +private: + uint8_t pwm_pin; + uint8_t in1_pin; + uint8_t in2_pin; + +public: + Motor(uint8_t pwm_pin, uint8_t in1_pin, uint8_t in2_pin); + void setup(); + void run(uint8_t speed, bool forward); + void stop(bool freewheel); +}; 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..0b16fa8 --- /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:teensy41] +platform = teensy +board = teensy41 +framework = arduino + +upload_protocol = teensy-cli \ No newline at end of file diff --git a/src/Motor.cpp b/src/Motor.cpp new file mode 100644 index 0000000..0a3a892 --- /dev/null +++ b/src/Motor.cpp @@ -0,0 +1,34 @@ +#include "Motor.h" + +Motor::Motor(uint8_t pwm, uint8_t in1, uint8_t in2) +{ + pwm_pin = pwm; + in1_pin = in1; + in2_pin = in2; +} + +void Motor::setup() +{ + pinMode(pwm_pin, OUTPUT); + pinMode(in1_pin, OUTPUT); + pinMode(in2_pin, OUTPUT); +} + +void Motor::run(uint8_t speed, bool forward) +{ + if (speed == 0) { + return stop(false); + } + + digitalWrite(in1_pin, forward ? HIGH : LOW); + digitalWrite(in2_pin, forward ? LOW : HIGH); + analogWrite(pwm_pin, speed); +} + +void Motor::stop(bool freewheel) +{ + analogWrite(pwm_pin, 0); + + digitalWrite(in1_pin, freewheel ? LOW : HIGH); + digitalWrite(in2_pin, freewheel ? LOW : HIGH); +} \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp new file mode 100644 index 0000000..624f77a --- /dev/null +++ b/src/main.cpp @@ -0,0 +1,97 @@ +#include +#include "Motor.h" + +// Vertical motor top +#define VERT_UP_PWM 3 +#define VERT_UP_AIN2 4 +#define VERT_UP_AIN1 5 + +// Vertical motor bottom +#define VERT_DOWN_PWM 9 +#define VERT_DOWN_AIN2 7 +#define VERT_DOWN_AIN1 8 + +// Horizontal motor left +#define HORZ_LEFT_PWM 24 +#define HORZ_LEFT_AIN2 25 +#define HORZ_LEFT_AIN1 26 + +// Horizontal motor right +#define HORZ_RIGHT_PWM 29 +#define HORZ_RIGHT_AIN2 27 +#define HORZ_RIGHT_AIN1 28 + +// Vertical sensors +#define VERT_END_OUTER 40 +#define VERT_END_INNER 39 +#define VERT_CNT_OUTER 38 +#define VERT_CNT_INNER 37 + +// Horizontal sensors +#define HORZ_END_OUTER 33 +#define HORZ_END_INNER 34 +#define HORZ_CNT_INNER 35 +#define HORZ_CNT_OUTER 36 + +volatile int32_t hor_pos; +volatile int32_t vert_pos; + +Motor vert_up(VERT_UP_PWM, VERT_UP_AIN1, VERT_UP_AIN2); +Motor vert_down(VERT_DOWN_PWM, VERT_DOWN_AIN1, VERT_DOWN_AIN2); +Motor horz_left(HORZ_LEFT_PWM, HORZ_LEFT_AIN1, HORZ_LEFT_AIN2); +Motor horz_right(HORZ_RIGHT_PWM, HORZ_RIGHT_AIN1, HORZ_RIGHT_AIN2); + +int32_t count(int pinA, int pinB) { + if (digitalRead(pinA)) return digitalRead(pinB) ? 1 : -1; + else return digitalRead(pinB) ? -1 : 1; +} + +void hor_count() { + hor_pos += count(HORZ_CNT_INNER, HORZ_CNT_OUTER); +} + +void vert_count() { + vert_pos += count(VERT_CNT_INNER, VERT_CNT_OUTER); +} + +void setup() { + Serial.begin(115200); + + pinMode(LED_BUILTIN, OUTPUT); + + hor_pos = 0; + vert_pos = 0; + + vert_up.setup(); + vert_down.setup(); + vert_up.stop(true); + vert_down.stop(true); + + horz_left.setup(); + horz_right.setup(); + horz_left.stop(true); + horz_right.stop(true); + + pinMode(HORZ_CNT_INNER, INPUT); + pinMode(HORZ_CNT_OUTER, INPUT); + pinMode(VERT_CNT_INNER, INPUT); + pinMode(VERT_CNT_OUTER, INPUT); + + digitalWrite(HORZ_CNT_INNER, LOW); + digitalWrite(HORZ_CNT_OUTER, LOW); + digitalWrite(VERT_CNT_INNER, LOW); + digitalWrite(VERT_CNT_OUTER, LOW); + + attachInterrupt(digitalPinToInterrupt(HORZ_CNT_INNER), hor_count, CHANGE); + // attachInterrupt(digitalPinToInterrupt(HORZ_CNT_OUTER), hor_count, CHANGE); + attachInterrupt(digitalPinToInterrupt(VERT_CNT_INNER), vert_count, CHANGE); + // attachInterrupt(digitalPinToInterrupt(VERT_CNT_OUTER), vert_count, CHANGE); +} + +void loop() { + // Serial.printf("hor_count1: %i | \t hor_count2: %i | \t vert_count1: %i | \t vert_count2: %i \n", digitalRead(HORZ_CNT_INNER), digitalRead(HORZ_CNT_OUTER), digitalRead(VERT_CNT_INNER), digitalRead(VERT_CNT_OUTER)); + + Serial.printf("hor_count %i | \t vert_count %i \n", hor_pos, vert_pos); + + delay(500); +} \ No newline at end of file diff --git a/test/README b/test/README new file mode 100644 index 0000000..b94d089 --- /dev/null +++ b/test/README @@ -0,0 +1,11 @@ + +This directory is intended for PlatformIO 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 PlatformIO Unit Testing: +- https://docs.platformio.org/page/plus/unit-testing.html