From da1f4d589176c8910cfff2fe847668e3ce99ec0d Mon Sep 17 00:00:00 2001 From: hhau Date: Sun, 19 Nov 2023 17:14:40 +0100 Subject: [PATCH 01/28] new: dev: initialStructure --- example/plain.cpp | 5 +++ src/Dezibot.h | 45 +++++++++++++++++++++++++++ src/colorDetection/ColorDetection.h | 7 +++++ src/lightDetection/LightDetection.h | 8 +++++ src/motion/Motion.h | 7 +++++ src/motionDetection/MotionDetection.h | 6 ++++ src/multiColorLight/MultiColorLight.h | 6 ++++ 7 files changed, 84 insertions(+) create mode 100644 example/plain.cpp create mode 100644 src/Dezibot.h create mode 100644 src/colorDetection/ColorDetection.h create mode 100644 src/lightDetection/LightDetection.h create mode 100644 src/motion/Motion.h create mode 100644 src/motionDetection/MotionDetection.h create mode 100644 src/multiColorLight/MultiColorLight.h diff --git a/example/plain.cpp b/example/plain.cpp new file mode 100644 index 0000000..c8fad0c --- /dev/null +++ b/example/plain.cpp @@ -0,0 +1,5 @@ +#include "Dezibot.h" + +int main(){ + return 0; +} \ No newline at end of file diff --git a/src/Dezibot.h b/src/Dezibot.h new file mode 100644 index 0000000..11a60ce --- /dev/null +++ b/src/Dezibot.h @@ -0,0 +1,45 @@ +/** + * @file Dezibot.h + * @author your name (you@domain.com) + * @brief + * @version 0.1 + * @date 2023-11-19 + * + * @copyright Copyright (c) 2023 + * + */ +#ifndef Dezibot_h +#define Dezibot_h +#include "Motion.h" +#include "LightDetection.h" +#include "ColorDetection.h" +#include "MultiColorLight.h" +#include "MotionDetection.h" +class Dezibot{ +protected: + +public: +Motion motion; +LightDetection lightDetection; +ColorDetection colorDetection; +MultiColorLight multiColorLight; +MotionDetection motionDetection; + +/* +Display display +IRCommuncation irCommuncation (beinhaltet Kommuniaktion / Annhärung...) +Battery battery +Extension extension +WiFi wifi //wie wird WiFi geschrieben? + +//nur lesender Zugriff, in dieser Klasse sind andere Instanzen mit dem Dezibotinterface gekapselt +Friends friends +OperatingSystem operatingSystem +USBCommunication usbCommunication +Button button +//nicht unique, initzial Dezibot +String robotName + +*/ +}; +#endif //Dezibot_h \ No newline at end of file diff --git a/src/colorDetection/ColorDetection.h b/src/colorDetection/ColorDetection.h new file mode 100644 index 0000000..b89e6d5 --- /dev/null +++ b/src/colorDetection/ColorDetection.h @@ -0,0 +1,7 @@ +#ifndef ColorDetection_h +#define ColorDetection_h + +class ColorDetection{ + +}; +#endif //ColorDetection_h \ No newline at end of file diff --git a/src/lightDetection/LightDetection.h b/src/lightDetection/LightDetection.h new file mode 100644 index 0000000..80827d8 --- /dev/null +++ b/src/lightDetection/LightDetection.h @@ -0,0 +1,8 @@ +#ifndef LightDetection_h +#define LightDetection_h + +//beinhaltet IR + Tageslicht +class LightDetection{ + +}; +#endif //LightDetection_h \ No newline at end of file diff --git a/src/motion/Motion.h b/src/motion/Motion.h new file mode 100644 index 0000000..99cb603 --- /dev/null +++ b/src/motion/Motion.h @@ -0,0 +1,7 @@ +#ifndef Motion_h +#define Motion_h + +class Motion{ + +}; +#endif //Motion_h \ No newline at end of file diff --git a/src/motionDetection/MotionDetection.h b/src/motionDetection/MotionDetection.h new file mode 100644 index 0000000..c01d0bb --- /dev/null +++ b/src/motionDetection/MotionDetection.h @@ -0,0 +1,6 @@ +#ifndef MotionDetection_h +#define MotionDetection_h +class MotionDetection{ + +}; +#endif //MotionDetection \ No newline at end of file diff --git a/src/multiColorLight/MultiColorLight.h b/src/multiColorLight/MultiColorLight.h new file mode 100644 index 0000000..70ea391 --- /dev/null +++ b/src/multiColorLight/MultiColorLight.h @@ -0,0 +1,6 @@ +#ifndef MultiColorLight_h +#define MultiColorLight_h +class MultiColorLight{ + +}; +#endif //MultiColorLight_h \ No newline at end of file From 3258396ae90defeec48097b2f47f4a641b3d9f8e Mon Sep 17 00:00:00 2001 From: Anton Jacker Date: Fri, 24 Nov 2023 15:16:43 +0100 Subject: [PATCH 02/28] test vTask and serial --- src/Dezibot.cpp | 61 +++++++++++++++++++++++++++++++++++++++++++++++++ src/Dezibot.h | 26 ++++++++++++--------- 2 files changed, 76 insertions(+), 11 deletions(-) create mode 100644 src/Dezibot.cpp diff --git a/src/Dezibot.cpp b/src/Dezibot.cpp new file mode 100644 index 0000000..509f068 --- /dev/null +++ b/src/Dezibot.cpp @@ -0,0 +1,61 @@ +// +// Created by Anton Jacker on 24.11.23. +// + + +#include "Dezibot.h" +#include +#include +#include +#include + + +#define SCREEN_WIDTH 128 // OLED display width, in pixels +#define SCREEN_HEIGHT 64 // OLED display height, in pixels + +// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins) +// The pins for I2C are defined by the Wire-library. +// On an arduino UNO: A4(SDA), A5(SCL) +// On an arduino MEGA 2560: 20(SDA), 21(SCL) +// On an arduino LEONARDO: 2(SDA), 3(SCL), ... +#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin) +#define SCREEN_ADDRESS 0x3D ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32 +Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET); + +#define GPIO_LED 48 + +void Dezibot::begin(void) { + Adafruit_NeoPixel ledStrip = Adafruit_NeoPixel(3, GPIO_LED, NEO_GRB + NEO_KHZ800); + if (!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) { + Serial.println(F("SSD1306 allocation failed")); + for (;;); // Don't proceed, loop forever + } + + display.clearDisplay(); + display.drawPixel(10, 10, SSD1306_WHITE); + + Serial.begin(9600); + Serial.println("start"); + + + vTaskDelay(1000); + + while (1) { + /* Blink off (output low) */ + ledStrip.setPixelColor(1, ledStrip.Color(100, 100, 100)); + ledStrip.show(); // Aktualisiere die Farbe des Pixels + vTaskDelay(1000); + /* Blink on (output high) */ + ledStrip.setPixelColor(1, ledStrip.Color(0, 0, 0)); + ledStrip.show(); // Aktualisiere die Farbe des Pixels + vTaskDelay(1000); + + struct timeval tv_now; + gettimeofday(&tv_now, NULL); + int64_t time_us = (int64_t) tv_now.tv_sec * 1000000L + (int64_t) tv_now.tv_usec; + + + Serial.println(time_us); + + } +} \ No newline at end of file diff --git a/src/Dezibot.h b/src/Dezibot.h index 11a60ce..07fe014 100644 --- a/src/Dezibot.h +++ b/src/Dezibot.h @@ -10,21 +10,24 @@ */ #ifndef Dezibot_h #define Dezibot_h -#include "Motion.h" -#include "LightDetection.h" -#include "ColorDetection.h" -#include "MultiColorLight.h" -#include "MotionDetection.h" -class Dezibot{ + +#include "motion/Motion.h" +#include "lightDetection/LightDetection.h" +#include "colorDetection/ColorDetection.h" +#include "multiColorLight/MultiColorLight.h" +#include "motionDetection/MotionDetection.h" + +class Dezibot { protected: public: -Motion motion; -LightDetection lightDetection; -ColorDetection colorDetection; -MultiColorLight multiColorLight; -MotionDetection motionDetection; + Motion motion; + LightDetection lightDetection; + ColorDetection colorDetection; + MultiColorLight multiColorLight; + MotionDetection motionDetection; + void begin(void); /* Display display IRCommuncation irCommuncation (beinhaltet Kommuniaktion / Annhärung...) @@ -42,4 +45,5 @@ String robotName */ }; + #endif //Dezibot_h \ No newline at end of file From 50c760333eb0cf619f5781b8e9ab2abc2b34ee38 Mon Sep 17 00:00:00 2001 From: Anton Jacker Date: Fri, 24 Nov 2023 15:55:59 +0100 Subject: [PATCH 03/28] example ino --- example/example.ino | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 example/example.ino diff --git a/example/example.ino b/example/example.ino new file mode 100644 index 0000000..8999e5a --- /dev/null +++ b/example/example.ino @@ -0,0 +1,13 @@ +#include +#include + +#define GPIO_LED 48 + +Dezibot dezibot = Dezibot(); + +void setup() { + dezibot.begin(); +} + +void loop() { +} From 45101de54c17d05e27c6ca3368fc04af5ef76c76 Mon Sep 17 00:00:00 2001 From: Anton Jacker Date: Fri, 24 Nov 2023 16:13:54 +0100 Subject: [PATCH 04/28] display text test --- src/Dezibot.cpp | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/src/Dezibot.cpp b/src/Dezibot.cpp index 509f068..3285444 100644 --- a/src/Dezibot.cpp +++ b/src/Dezibot.cpp @@ -19,21 +19,33 @@ // On an arduino MEGA 2560: 20(SDA), 21(SCL) // On an arduino LEONARDO: 2(SDA), 3(SCL), ... #define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin) -#define SCREEN_ADDRESS 0x3D ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32 +#define SCREEN_ADDRESS 0x3C ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32 + Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET); #define GPIO_LED 48 void Dezibot::begin(void) { Adafruit_NeoPixel ledStrip = Adafruit_NeoPixel(3, GPIO_LED, NEO_GRB + NEO_KHZ800); + Wire.begin(1, 2); if (!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) { - Serial.println(F("SSD1306 allocation failed")); + Serial.println("SSD1306 allocation failed"); for (;;); // Don't proceed, loop forever } + display.display(); + vTaskDelay(2000); display.clearDisplay(); + vTaskDelay(2000); + + // Draw a single pixel in white display.drawPixel(10, 10, SSD1306_WHITE); + // Show the display buffer on the screen. You MUST call display() after + // drawing commands to make them visible on screen! + display.display(); + vTaskDelay(2000); + Serial.begin(9600); Serial.println("start"); @@ -57,5 +69,14 @@ void Dezibot::begin(void) { Serial.println(time_us); + display.clearDisplay(); + + display.setTextSize(2); // Draw 2X-scale text + display.setTextColor(SSD1306_WHITE); + display.setCursor(10, 0); + display.println(F("scroll")); + display.display(); // Show initial text + vTaskDelay(1000); + } } \ No newline at end of file From 38e2cd6f2a79ea983252f7dc52254d266cb3b5b2 Mon Sep 17 00:00:00 2001 From: Anton Jacker Date: Sat, 25 Nov 2023 11:55:24 +0100 Subject: [PATCH 05/28] add documentation for arduino settings and display --- README.md | 115 ++++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 99 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index 02d8428..944a862 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,10 @@ # Dezibot4 Lib + ## Introduction + This repository contains the Library for the Dezibot4.
It is ment to serve as an Arduino-Library. Therefore the rules for arduinolibrary develop apply:
+ * [Styleguide](https://docs.arduino.cc/learn/contributions/arduino-library-style-guide) * [Libraryspecification](https://arduino.github.io/arduino-cli/0.35/library-specification/) * [Submission-requirements](https://github.com/arduino/library-registry/blob/main/FAQ.md#submission-requirements) @@ -9,20 +12,30 @@ It is ment to serve as an Arduino-Library. Therefore the rules for arduinolibrar In the following the most important points and custom conventions are introduced. ## Code Conventions + ### Don't pass reference -To allow easy usability for users not familier with C++, prevent passing around references. It is better to use accessmethods + +To allow easy usability for users not familier with C++, prevent passing around references. It is better to use +accessmethods + ### Naming + * methods are named in lowerCamelCase -* classes are named in UpperCamelCase +* classes are named in UpperCamelCase * folders containing components are named in lowerCamelCase * methods are named in lowerCamelCase * constants are named in ALL_CAPS_SNAKE_CASE + ### Bytestream + Every class that implements Byte-Based Communication needs to implement the Arduino Streaminterface + ### Components + Every component has a single .h file and one or more .cpp files.
Every component is placed in a seperate folder under src/ that is named equvivalent to the class. -The minimal structure of any .h file is
+The minimal structure of any .h file is
+ ```c++ #ifndef ClassName_h #define ClassName_h @@ -33,36 +46,61 @@ class ClassName{ ``` ## Design Paradigm -During desgin, the Dezibot isn't describe using it's part but instead it's functionality. Under the top-level Dezibotclass, there is a class for every functionality of the robot. Each of that classes consists of two parts. + +During desgin, the Dezibot isn't describe using it's part but instead it's functionality. Under the top-level +Dezibotclass, there is a class for every functionality of the robot. Each of that classes consists of two parts. + ### Part Instances -Each component contains instances of every Robotpart that is used in that component. For example the Motion component contains two motorinstances, one for motorEast and one for motorWest. -Using these instances, it is possible to access more specific methods that interacts directly with the component (configure it, setSpeed,...) + +Each component contains instances of every Robotpart that is used in that component. For example the Motion component +contains two motorinstances, one for motorEast and one for motorWest. +Using these instances, it is possible to access more specific methods that interacts directly with the component ( +configure it, setSpeed,...) + ### Abstractions -The components constains abstractions that combines multiple partMethods to ease the usability. For example for the motioncomponent provides an abstraction for the forwardmovement, that involves two motors and even another component (MotionDetection) + +The components constains abstractions that combines multiple partMethods to ease the usability. For example for the +motioncomponent provides an abstraction for the forwardmovement, that involves two motors and even another component ( +MotionDetection) ## Contributing -When contributing to the project please follow the rules below. At first, follow all rules from this readme. Further rules apply to the usage of git + +When contributing to the project please follow the rules below. At first, follow all rules from this readme. Further +rules apply to the usage of git + ### Branching -Whenever working on the project, create a new branch from the current state of Develop. + +Whenever working on the project, create a new branch from the current state of Develop. Branches should be named as `prefix/#issueid-shortdescription` where prefix is from {feature,fix,refactor}.
When a branch is ready to be used in production, create a mergerequest. -### Mergerequests -The target of each Mergerequest must be the Develop-Branch. Before the merge, each request must be approved by at least one person with Owner role.
-The approve process should consider especially the documentation, naming, implementation. -When the merge is approved and no more commits are added, the last commit must increment the versionnumber in the library.properties file, following the rules of [Semantic Versioning](https://semver.org/) -### Commitmessages -Commitmessages must follow the [gitchangelog](https://github.com/vaab/gitchangelog/blob/master/src/gitchangelog/gitchangelog.rc.reference) pattern. +### Mergerequests + +The target of each Mergerequest must be the Develop-Branch. Before the merge, each request must be approved by at least +one person with Owner role.
+The approve process should consider especially the documentation, naming, implementation. +When the merge is approved and no more commits are added, the last commit must increment the versionnumber in the +library.properties file, following the rules of [Semantic Versioning](https://semver.org/) + +### Commitmessages + +Commitmessages must follow +the [gitchangelog](https://github.com/vaab/gitchangelog/blob/master/src/gitchangelog/gitchangelog.rc.reference) pattern. + ### Language + The language of the project is American English. That includes in particular but not exclusively: + * Sourcecode * Commit Messages -* Documentation +* Documentation A german documentation will be provided but does not replace the english documentation. ### Documentation + #### .h Files + ```C++ /** * @file Dezibot.h @@ -75,7 +113,12 @@ A german documentation will be provided but does not replace the english documen */ ``` + +In the library, the `.h` files should be included using a relative path. +For instance, in `src/Dezibot.h`, to include `src/motion/Motion.h`, you should write `#include "motion/Motion.h"`. + #### Methods + ```C++ /** * @brief @@ -86,4 +129,44 @@ A german documentation will be provided but does not replace the english documen */ ``` +#### Arduino Settings + +* Board: "ESP32-S3-USB-OTG" +* Upload Mode: "UART0 / Hardware CDC" +* USB Mode: "Hardware CDC and JTAG" +* Programmer: "Esptool" + +#### Display + +It is important to specify the SDA and SCL ports by using `Wire.begin(SDA, SCL)`. + +```c++ +#include +#include + +#define SCREEN_WIDTH 128 +#define SCREEN_HEIGHT 64 + +#define OLED_RESET -1 +#define SCREEN_ADDRESS 0x3C + +Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET); + +void Dezibot::begin(void) { + Wire.begin(1, 2); + if (!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) { + // Serial.println("SSD1306 allocation failed"); + for (;;); // Don't proceed, loop forever + } + + + // Draw a single pixel in white + display.drawPixel(10, 10, SSD1306_WHITE); + + // Show the display buffer on the screen. You MUST call display() after + // drawing commands to make them visible on screen! + display.display(); + vTaskDelay(2000); +} +``` From e72595e73999e6c1fd78cc145747990ec3123369 Mon Sep 17 00:00:00 2001 From: hhau Date: Sat, 25 Nov 2023 15:35:58 +0100 Subject: [PATCH 06/28] chg: dev: added method Declarations for MultiColorLight --- example/example.ino | 2 +- src/Dezibot.cpp | 2 +- src/multiColorLight/MultiColorLight.cpp | 31 ++++++++ src/multiColorLight/MultiColorLight.h | 96 ++++++++++++++++++++++++- 4 files changed, 128 insertions(+), 3 deletions(-) create mode 100644 src/multiColorLight/MultiColorLight.cpp diff --git a/example/example.ino b/example/example.ino index 8999e5a..de6830f 100644 --- a/example/example.ino +++ b/example/example.ino @@ -1,4 +1,4 @@ -#include + #include #define GPIO_LED 48 diff --git a/src/Dezibot.cpp b/src/Dezibot.cpp index 3285444..5440a38 100644 --- a/src/Dezibot.cpp +++ b/src/Dezibot.cpp @@ -54,7 +54,7 @@ void Dezibot::begin(void) { while (1) { /* Blink off (output low) */ - ledStrip.setPixelColor(1, ledStrip.Color(100, 100, 100)); + ledStrip.setPixelColor(1, ledStrip.Color(100, 0, 0)); ledStrip.show(); // Aktualisiere die Farbe des Pixels vTaskDelay(1000); /* Blink on (output high) */ diff --git a/src/multiColorLight/MultiColorLight.cpp b/src/multiColorLight/MultiColorLight.cpp new file mode 100644 index 0000000..f7ee1f4 --- /dev/null +++ b/src/multiColorLight/MultiColorLight.cpp @@ -0,0 +1,31 @@ +#include "MultiColorLight.h" + +void MultiColorLight::begin(void){ + +}; + +void MultiColorLight::setLed(uint8_t index , uint32_t color){ + +}; + + +void MultiColorLight::setLed(leds leds, uint32_t color){ + +}; + + +void MultiColorLight::setTopLeds(uint32_t color){ + +}; + +void MultiColorLight::blink(uint16_t amount,uint32_t color, leds leds, uint32_t interval){ + +}; + +void MultiColorLight::turnOff(leds leds){ + +}; + +uint32_t MultiColorLight::color(uint8_t r, uint8_t g, uint8_t b){ + return 0; +}; \ No newline at end of file diff --git a/src/multiColorLight/MultiColorLight.h b/src/multiColorLight/MultiColorLight.h index 70ea391..9231d7b 100644 --- a/src/multiColorLight/MultiColorLight.h +++ b/src/multiColorLight/MultiColorLight.h @@ -1,6 +1,100 @@ +/** + * @file MultiColorLight.h + * @author Saskia Duebener, Hans Haupt + * @brief This component controls the ability to show multicolored light, using the RGB-LEDs + * @version 0.1 + * @date 2023-11-25 + * + * @copyright Copyright (c) 2023 + * + */ #ifndef MultiColorLight_h #define MultiColorLight_h -class MultiColorLight{ +#include +/** + * @brief Describes combinations of leds on the Dezibot. + * With the Robot in Front of you, if you can read the Dezibotlogo, the LED left from the Logo is TOP_LEFT + * + */ +enum leds{ + TOP_LEFT, + TOP_RIGHT, + BOTTOM, + TOP, + ALL }; + +class MultiColorLight{ +public: + + /** + * @brief initialize the multicolor component + * + */ + void begin(void); + + /** + * @brief Set the specified led to the passed color + * @param index ranging from 0-2, 0: Left, 1: Right, 2: Bottom + * @param color A 32-bit unsigned integer representing the color in the format + * 0x00RRGGBB, where RR is the red component, GG is the green + * component, and BB is the blue component. Each color can range between 0 to 100 + */ + void setLed(uint8_t index , uint32_t color); + + /** + * @brief Set the specified leds to the passed color value + * + * @param leds which leds should be updated + * @param color A 32-bit unsigned integer representing the color in the format + * 0x00RRGGBB, where RR is the red component, GG is the green + * component, and BB is the blue component. Each color can range between 0 to 100 + */ + void setLed(leds leds, uint32_t color); + + /** + * @brief sets the two leds on the top of the robot to the specified color + * + * @param color A 32-bit unsigned integer representing the color in the format + * 0x00RRGGBB, where RR is the red component, GG is the green + * component, and BB is the blue component. Each color can range between 0 to 100 + */ + void setTopLeds(uint32_t color); + + /** + * @brief Let LEDs blink, returns after all blinks were executed + * + * @param amount how often should the leds blink + * @param color A 32-bit unsigned integer representing the color in the format + * 0x00RRGGBB, where RR is the red component, GG is the green + * component, and BB is the blue component. + * Each color can range between 0 to 100 + * Defaults to blue + * @param leds which LEDs should blink, default is TOP + * @param interval how many miliseconds the led is on, defaults to 1s + */ + void blink(uint16_t amount,uint32_t color = 0x00006400,leds leds=TOP, uint32_t interval=1000); + + /** + * @brief turn off the given leds + * + * @param leds which leds should be turned off, defaults to ALL + */ + void turnOff(leds leds=ALL); + + /** + * @brief wrapper to calulate the used colorformat from a rgb-value + * + * @param r red (0-100) + * @param g green (0-100) + * @param b blue (0-100) + * @return A 32-bit unsigned integer representing the color in the format + * 0x00RRGGBB, where RR is the red component, GG is the green + * component, and BB is the blue component. + */ + uint32_t color(uint8_t r, uint8_t g, uint8_t b); + +}; + #endif //MultiColorLight_h \ No newline at end of file From 1b330e15206287ab7872e83608673643245c9497 Mon Sep 17 00:00:00 2001 From: hhau Date: Sun, 26 Nov 2023 11:28:17 +0100 Subject: [PATCH 07/28] chg: dev: first version of multiColorLight implemeted --- example/example.ino | 11 +-- src/Dezibot.cpp | 90 ++++--------------------- src/Dezibot.h | 1 + src/multiColorLight/MultiColorLight.cpp | 78 +++++++++++++++++++-- src/multiColorLight/MultiColorLight.h | 22 +++++- 5 files changed, 115 insertions(+), 87 deletions(-) diff --git a/example/example.ino b/example/example.ino index de6830f..7076d96 100644 --- a/example/example.ino +++ b/example/example.ino @@ -1,13 +1,16 @@ - #include -#define GPIO_LED 48 - Dezibot dezibot = Dezibot(); - +const uint8_t MYFOO = 10; void setup() { dezibot.begin(); + } void loop() { +dezibot.multiColorLight.setLed(TOP_LEFT,0x000000FF); +dezibot.multiColorLight.setLed(TOP_RIGHT,dezibot.multiColorLight.color(0,100,0)); +dezibot.multiColorLight.blink(10,0x00FF0000,BOTTOM,500); + +delay(1000); } diff --git a/src/Dezibot.cpp b/src/Dezibot.cpp index 5440a38..dc1dcb7 100644 --- a/src/Dezibot.cpp +++ b/src/Dezibot.cpp @@ -1,82 +1,20 @@ -// -// Created by Anton Jacker on 24.11.23. -// +/** + * @file Dezibot.cpp + * @author Anton Jacker, Hans Haupt, Saskia Duebener + * @brief + * @version 0.1 + * @date 2023-11-26 + * + * @copyright Copyright (c) 2023 + * + */ #include "Dezibot.h" -#include -#include -#include -#include +Dezibot::Dezibot():multiColorLight(){ -#define SCREEN_WIDTH 128 // OLED display width, in pixels -#define SCREEN_HEIGHT 64 // OLED display height, in pixels - -// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins) -// The pins for I2C are defined by the Wire-library. -// On an arduino UNO: A4(SDA), A5(SCL) -// On an arduino MEGA 2560: 20(SDA), 21(SCL) -// On an arduino LEONARDO: 2(SDA), 3(SCL), ... -#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin) -#define SCREEN_ADDRESS 0x3C ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32 - -Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET); - -#define GPIO_LED 48 - +}; void Dezibot::begin(void) { - Adafruit_NeoPixel ledStrip = Adafruit_NeoPixel(3, GPIO_LED, NEO_GRB + NEO_KHZ800); - Wire.begin(1, 2); - if (!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) { - Serial.println("SSD1306 allocation failed"); - for (;;); // Don't proceed, loop forever - } - - display.display(); - vTaskDelay(2000); - display.clearDisplay(); - vTaskDelay(2000); - - // Draw a single pixel in white - display.drawPixel(10, 10, SSD1306_WHITE); - - // Show the display buffer on the screen. You MUST call display() after - // drawing commands to make them visible on screen! - display.display(); - vTaskDelay(2000); - - Serial.begin(9600); - Serial.println("start"); - - - vTaskDelay(1000); - - while (1) { - /* Blink off (output low) */ - ledStrip.setPixelColor(1, ledStrip.Color(100, 0, 0)); - ledStrip.show(); // Aktualisiere die Farbe des Pixels - vTaskDelay(1000); - /* Blink on (output high) */ - ledStrip.setPixelColor(1, ledStrip.Color(0, 0, 0)); - ledStrip.show(); // Aktualisiere die Farbe des Pixels - vTaskDelay(1000); - - struct timeval tv_now; - gettimeofday(&tv_now, NULL); - int64_t time_us = (int64_t) tv_now.tv_sec * 1000000L + (int64_t) tv_now.tv_usec; - - - Serial.println(time_us); - - display.clearDisplay(); - - display.setTextSize(2); // Draw 2X-scale text - display.setTextColor(SSD1306_WHITE); - display.setCursor(10, 0); - display.println(F("scroll")); - display.display(); // Show initial text - vTaskDelay(1000); - - } -} \ No newline at end of file + multiColorLight.begin(); +}; diff --git a/src/Dezibot.h b/src/Dezibot.h index 07fe014..50fcca6 100644 --- a/src/Dezibot.h +++ b/src/Dezibot.h @@ -21,6 +21,7 @@ class Dezibot { protected: public: + Dezibot(); Motion motion; LightDetection lightDetection; ColorDetection colorDetection; diff --git a/src/multiColorLight/MultiColorLight.cpp b/src/multiColorLight/MultiColorLight.cpp index f7ee1f4..1b215cd 100644 --- a/src/multiColorLight/MultiColorLight.cpp +++ b/src/multiColorLight/MultiColorLight.cpp @@ -1,31 +1,97 @@ #include "MultiColorLight.h" -void MultiColorLight::begin(void){ +MultiColorLight::MultiColorLight():rgbLeds(ledAmount,ledPin){ }; -void MultiColorLight::setLed(uint8_t index , uint32_t color){ +void MultiColorLight::begin(void){ + rgbLeds.begin(); +}; +void MultiColorLight::setLed(uint8_t index , uint32_t color){ + if (index > ledAmount-1){ + //TODO: logging + } + rgbLeds.setPixelColor(index, normalizeColor(color)); + rgbLeds.show(); }; void MultiColorLight::setLed(leds leds, uint32_t color){ + switch (leds){ + case TOP_LEFT: + MultiColorLight::setLed(0,color);break; + case TOP_RIGHT: + MultiColorLight::setLed(1,color);break; + case BOTTOM: + MultiColorLight::setLed(2,color);break; + case TOP: + for (int index = 0; index<2; index++){ + MultiColorLight::setLed(index,color); + }break; + case ALL: + for (int index = 0; index<3; index++){ + MultiColorLight::setLed(index,color); + }break; + default: + //TODO logging + break; + } }; void MultiColorLight::setTopLeds(uint32_t color){ - + MultiColorLight::setLed(TOP,color); }; void MultiColorLight::blink(uint16_t amount,uint32_t color, leds leds, uint32_t interval){ + for(uint16_t index = 0; index < amount;index++){ + MultiColorLight::setLed(leds, color); + vTaskDelay(interval); + MultiColorLight::turnOff(leds); + vTaskDelay(interval); + } }; void MultiColorLight::turnOff(leds leds){ - + switch (leds){ + case TOP_LEFT: + MultiColorLight::setLed(0,0);break; + case TOP_RIGHT: + MultiColorLight::setLed(1,0);break; + case BOTTOM: + MultiColorLight::setLed(2,0);break; + case TOP: + for (int index = 0; index<2; index++){ + MultiColorLight::setLed(index,0); + }break; + case ALL: + rgbLeds.clear();break; + default: + //TODO logging + break; + } }; uint32_t MultiColorLight::color(uint8_t r, uint8_t g, uint8_t b){ - return 0; -}; \ No newline at end of file + return rgbLeds.Color(r,g,b); +}; + +//PRIVATE +uint32_t MultiColorLight::normalizeColor(uint32_t color,uint8_t maxBrightness){ + uint8_t red = (color&0x00FF0000)>>16; + uint8_t green = (color&0x0000FF00)>>8; + uint8_t blue = (color&0x000000FF); + if (red > maxBrightness){ + red = maxBrightness; + } + if(green > maxBrightness){ + green = maxBrightness; + } + if(blue > maxBrightness){ + blue = maxBrightness; + } + return MultiColorLight::color(red,green,blue); +} \ No newline at end of file diff --git a/src/multiColorLight/MultiColorLight.h b/src/multiColorLight/MultiColorLight.h index 9231d7b..abe3622 100644 --- a/src/multiColorLight/MultiColorLight.h +++ b/src/multiColorLight/MultiColorLight.h @@ -26,8 +26,14 @@ enum leds{ }; class MultiColorLight{ +protected: + static const uint16_t ledAmount = 3; + static const int16_t ledPin = 48; + static const uint8_t maxBrightness = 100; + Adafruit_NeoPixel rgbLeds; public: + MultiColorLight(); /** * @brief initialize the multicolor component * @@ -94,7 +100,21 @@ public: * component, and BB is the blue component. */ uint32_t color(uint8_t r, uint8_t g, uint8_t b); - + +private: + /** + * @brief normalizes every component of color to not exeed the maxBrightness + * + * @param color A 32-bit unsigned integer representing the color in the format + * 0x00RRGGBB, where RR is the red component, GG is the green + * component, and BB is the blue component. + * @param maxBrigthness maximal level of brightness that is allowed for each color + * @return uint32_t A 32-bit unsigned integer representing the color in the format + * 0x00RRGGBB, where RR is the red component, GG is the green + * component, and BB is the blue component. Where each component can be + * between 0 - maxBrightness + */ + uint32_t normalizeColor(uint32_t color, uint8_t maxBrigthness=maxBrightness); }; #endif //MultiColorLight_h \ No newline at end of file From 4546330c15cc61440876ababdd7ded100db40e40 Mon Sep 17 00:00:00 2001 From: hhau Date: Sun, 26 Nov 2023 11:34:24 +0100 Subject: [PATCH 08/28] fix: dev: bug int turnOff(ALL) fixed --- example/example.ino | 2 ++ src/multiColorLight/MultiColorLight.cpp | 4 +++- src/multiColorLight/MultiColorLight.h | 2 +- 3 files changed, 6 insertions(+), 2 deletions(-) diff --git a/example/example.ino b/example/example.ino index 7076d96..f055251 100644 --- a/example/example.ino +++ b/example/example.ino @@ -12,5 +12,7 @@ dezibot.multiColorLight.setLed(TOP_LEFT,0x000000FF); dezibot.multiColorLight.setLed(TOP_RIGHT,dezibot.multiColorLight.color(0,100,0)); dezibot.multiColorLight.blink(10,0x00FF0000,BOTTOM,500); +delay(1000); +dezibot.multiColorLight.turnOff(ALL); delay(1000); } diff --git a/src/multiColorLight/MultiColorLight.cpp b/src/multiColorLight/MultiColorLight.cpp index 1b215cd..8439256 100644 --- a/src/multiColorLight/MultiColorLight.cpp +++ b/src/multiColorLight/MultiColorLight.cpp @@ -68,7 +68,9 @@ void MultiColorLight::turnOff(leds leds){ MultiColorLight::setLed(index,0); }break; case ALL: - rgbLeds.clear();break; + for (int index = 0; index<3; index++){ + MultiColorLight::setLed(index,0); + }break; default: //TODO logging break; diff --git a/src/multiColorLight/MultiColorLight.h b/src/multiColorLight/MultiColorLight.h index abe3622..6a8580d 100644 --- a/src/multiColorLight/MultiColorLight.h +++ b/src/multiColorLight/MultiColorLight.h @@ -2,7 +2,7 @@ * @file MultiColorLight.h * @author Saskia Duebener, Hans Haupt * @brief This component controls the ability to show multicolored light, using the RGB-LEDs - * @version 0.1 + * @version 0.2 * @date 2023-11-25 * * @copyright Copyright (c) 2023 From 11ac0494d537c7fe1f436a6a5cba502a20578ba0 Mon Sep 17 00:00:00 2001 From: hhau Date: Tue, 5 Dec 2023 23:59:39 +0100 Subject: [PATCH 09/28] chg: dev: added colorconstants --- src/multiColorLight/ColorConstants.h | 9 +++++++++ src/multiColorLight/MultiColorLight.h | 2 +- 2 files changed, 10 insertions(+), 1 deletion(-) create mode 100644 src/multiColorLight/ColorConstants.h diff --git a/src/multiColorLight/ColorConstants.h b/src/multiColorLight/ColorConstants.h new file mode 100644 index 0000000..f180f5e --- /dev/null +++ b/src/multiColorLight/ColorConstants.h @@ -0,0 +1,9 @@ +static const uint32_t RED = 0xFF0000; +static const uint32_t GREEN = 0x00FF00; +static const uint32_t BLUE = 0x0000FF; +static const uint32_t WHITE = 0xFFFFFF; +static const uint32_t ORANGE = 0x961E00; +static const uint32_t YELLOW = 0x965000; +static const uint32_t TURQUOISE = 0x005064; +static const uint32_t PURPEL = 0x320064; +static const uint32_t PINK = 0x960064; diff --git a/src/multiColorLight/MultiColorLight.h b/src/multiColorLight/MultiColorLight.h index 6a8580d..ecfcadb 100644 --- a/src/multiColorLight/MultiColorLight.h +++ b/src/multiColorLight/MultiColorLight.h @@ -11,7 +11,7 @@ #ifndef MultiColorLight_h #define MultiColorLight_h #include - +#include "ColorConstants.h" /** * @brief Describes combinations of leds on the Dezibot. * With the Robot in Front of you, if you can read the Dezibotlogo, the LED left from the Logo is TOP_LEFT From 4be496759f7b3c7fdd1eddb4981672ca6e54fb57 Mon Sep 17 00:00:00 2001 From: hhau Date: Wed, 6 Dec 2023 00:02:09 +0100 Subject: [PATCH 10/28] fix: dev: switched left and right leds --- src/multiColorLight/MultiColorLight.cpp | 6 +++++- src/multiColorLight/MultiColorLight.h | 4 ++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/src/multiColorLight/MultiColorLight.cpp b/src/multiColorLight/MultiColorLight.cpp index 8439256..02adaa2 100644 --- a/src/multiColorLight/MultiColorLight.cpp +++ b/src/multiColorLight/MultiColorLight.cpp @@ -20,6 +20,8 @@ void MultiColorLight::setLed(uint8_t index , uint32_t color){ void MultiColorLight::setLed(leds leds, uint32_t color){ switch (leds){ case TOP_LEFT: + MultiColorLight::setLed(1,color);break; + case TOP_RIGHT: MultiColorLight::setLed(0,color);break; case TOP_RIGHT: MultiColorLight::setLed(1,color);break; @@ -30,7 +32,7 @@ void MultiColorLight::setLed(leds leds, uint32_t color){ MultiColorLight::setLed(index,color); }break; case ALL: - for (int index = 0; index<3; index++){ + for (int index = 0; index Date: Wed, 6 Dec 2023 00:04:16 +0100 Subject: [PATCH 11/28] fix: dev: implemented different maxBrightness for the different colors --- src/multiColorLight/MultiColorLight.cpp | 8 ++++---- src/multiColorLight/MultiColorLight.h | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/multiColorLight/MultiColorLight.cpp b/src/multiColorLight/MultiColorLight.cpp index 02adaa2..985b35a 100644 --- a/src/multiColorLight/MultiColorLight.cpp +++ b/src/multiColorLight/MultiColorLight.cpp @@ -93,11 +93,11 @@ uint32_t MultiColorLight::normalizeColor(uint32_t color,uint8_t maxBrightness){ if (red > maxBrightness){ red = maxBrightness; } - if(green > maxBrightness){ - green = maxBrightness; + if(green > maxBrightness-70){ + green = maxBrightness-70; } - if(blue > maxBrightness){ - blue = maxBrightness; + if(blue > maxBrightness-50){ + blue = maxBrightness-50; } return MultiColorLight::color(red,green,blue); } \ No newline at end of file diff --git a/src/multiColorLight/MultiColorLight.h b/src/multiColorLight/MultiColorLight.h index f611d7a..52ee75d 100644 --- a/src/multiColorLight/MultiColorLight.h +++ b/src/multiColorLight/MultiColorLight.h @@ -29,7 +29,7 @@ class MultiColorLight{ protected: static const uint16_t ledAmount = 3; static const int16_t ledPin = 48; - static const uint8_t maxBrightness = 100; + static const uint8_t maxBrightness = 150; Adafruit_NeoPixel rgbLeds; public: From b12e07539b801649f206a7a35b33e8988b0f4b75 Mon Sep 17 00:00:00 2001 From: hhau Date: Wed, 6 Dec 2023 00:06:10 +0100 Subject: [PATCH 12/28] chg: dev: add wrapper for setLED that takes three color arguments --- src/multiColorLight/MultiColorLight.cpp | 8 ++++++++ src/multiColorLight/MultiColorLight.h | 19 +++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/src/multiColorLight/MultiColorLight.cpp b/src/multiColorLight/MultiColorLight.cpp index 985b35a..f8d3a4d 100644 --- a/src/multiColorLight/MultiColorLight.cpp +++ b/src/multiColorLight/MultiColorLight.cpp @@ -42,11 +42,19 @@ void MultiColorLight::setLed(leds leds, uint32_t color){ }; +void MultiColorLight::setLed(leds leds, uint8_t red, uint8_t green, uint8_t blue){ + MultiColorLight::setLed(leds, MultiColorLight::color(red,green,blue)); +}; + void MultiColorLight::setTopLeds(uint32_t color){ MultiColorLight::setLed(TOP,color); }; +void MultiColorLight::setTopLeds(uint8_t red, uint8_t green, uint8_t blue){ + MultiColorLight::setTopLeds(MultiColorLight::color(red,green,blue)); +}; + void MultiColorLight::blink(uint16_t amount,uint32_t color, leds leds, uint32_t interval){ for(uint16_t index = 0; index < amount;index++){ MultiColorLight::setLed(leds, color); diff --git a/src/multiColorLight/MultiColorLight.h b/src/multiColorLight/MultiColorLight.h index 52ee75d..ce9ba5c 100644 --- a/src/multiColorLight/MultiColorLight.h +++ b/src/multiColorLight/MultiColorLight.h @@ -59,6 +59,16 @@ public: */ void setLed(leds leds, uint32_t color); + /** + * @brief Set the specified leds to the passed color value + * + * @param leds which leds should be updated + * @param red brightness of red, is normalized in the function + * @param green brightness of green, is normalized in the function + * @param blue brightness of blue, is normalized in the function + */ + void setLed(leds leds, uint8_t red, uint8_t green, uint8_t blue); + /** * @brief sets the two leds on the top of the robot to the specified color * @@ -68,6 +78,15 @@ public: */ void setTopLeds(uint32_t color); + /** + * @brief sets the two leds on the top of the robot to the specified color + * + * @param red brightness of red, is normalized in the function + * @param green brightness of green, is normalized in the function + * @param blue brightness of blue, is normalized in the function + */ + void setTopLeds(uint8_t red, uint8_t green, uint8_t blue); + /** * @brief Let LEDs blink, returns after all blinks were executed * From 31f4a2b2816087ab3cb0c9c21c671e3d3c7e6dde Mon Sep 17 00:00:00 2001 From: hhau Date: Wed, 6 Dec 2023 00:08:55 +0100 Subject: [PATCH 13/28] fix: dev: equalized naming of functions in MultiColorLight --- src/multiColorLight/MultiColorLight.cpp | 8 ++------ src/multiColorLight/MultiColorLight.h | 6 +++--- 2 files changed, 5 insertions(+), 9 deletions(-) diff --git a/src/multiColorLight/MultiColorLight.cpp b/src/multiColorLight/MultiColorLight.cpp index f8d3a4d..f0235a4 100644 --- a/src/multiColorLight/MultiColorLight.cpp +++ b/src/multiColorLight/MultiColorLight.cpp @@ -23,8 +23,6 @@ void MultiColorLight::setLed(leds leds, uint32_t color){ MultiColorLight::setLed(1,color);break; case TOP_RIGHT: MultiColorLight::setLed(0,color);break; - case TOP_RIGHT: - MultiColorLight::setLed(1,color);break; case BOTTOM: MultiColorLight::setLed(2,color);break; case TOP: @@ -59,20 +57,18 @@ void MultiColorLight::blink(uint16_t amount,uint32_t color, leds leds, uint32_t for(uint16_t index = 0; index < amount;index++){ MultiColorLight::setLed(leds, color); vTaskDelay(interval); - MultiColorLight::turnOff(leds); + MultiColorLight::turnOffLed(leds); vTaskDelay(interval); } }; -void MultiColorLight::turnOff(leds leds){ +void MultiColorLight::turnOffLed(leds leds){ switch (leds){ case TOP_LEFT: MultiColorLight::setLed(1,0);break; case TOP_RIGHT: MultiColorLight::setLed(0,0);break; - case TOP_RIGHT: - MultiColorLight::setLed(1,0);break; case BOTTOM: MultiColorLight::setLed(2,0);break; case TOP: diff --git a/src/multiColorLight/MultiColorLight.h b/src/multiColorLight/MultiColorLight.h index ce9ba5c..44498cf 100644 --- a/src/multiColorLight/MultiColorLight.h +++ b/src/multiColorLight/MultiColorLight.h @@ -58,7 +58,7 @@ public: * component, and BB is the blue component. Each color can range between 0 to 100 */ void setLed(leds leds, uint32_t color); - + /** * @brief Set the specified leds to the passed color value * @@ -77,7 +77,7 @@ public: * component, and BB is the blue component. Each color can range between 0 to 100 */ void setTopLeds(uint32_t color); - + /** * @brief sets the two leds on the top of the robot to the specified color * @@ -106,7 +106,7 @@ public: * * @param leds which leds should be turned off, defaults to ALL */ - void turnOff(leds leds=ALL); + void turnOffLed(leds leds=ALL); /** * @brief wrapper to calulate the used colorformat from a rgb-value From 68883dadb8c3a9de2cb2e1f3218ec2bcb0072cb4 Mon Sep 17 00:00:00 2001 From: n1i9c9k9 Date: Wed, 13 Dec 2023 12:29:44 +0100 Subject: [PATCH 14/28] functions to implement --- src/motion/Motion.h | 43 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 42 insertions(+), 1 deletion(-) diff --git a/src/motion/Motion.h b/src/motion/Motion.h index 99cb603..16353d8 100644 --- a/src/motion/Motion.h +++ b/src/motion/Motion.h @@ -1,7 +1,48 @@ +/** + * @file Motion.h + * @author Jonathan Schulze, Nick Hübenthal + * @brief This component controls the ability to rotate and change position. + * @version 0.1 + * @date 2023-12-13 + * + * @copyright Copyright (c) 2023 + * + */ + #ifndef Motion_h #define Motion_h class Motion{ +protected: + +public: + Motion(); + + /** + * @brief Initialize the movement component. + * + */ + void begin(void); + + /** + * @brief Move forward for a certain amount of time. + * @param moveForMs Representing the duration of forward moving in milliseconds. + */ + void move(u8int moveForMs); + + /** + * @brief Rotate left for a certain amount of time. + * @param rotateForMs Representing the duration of rotating left in milliseconds. + */ + void rotateLeft(u8int rotateForMs); + + /** + * @brief Rotate right for a certain amount of time. + * @param rotateForMs Representing the duration of rotating right in milliseconds. + */ + void rotateRight(u8int rotateForMs); }; -#endif //Motion_h \ No newline at end of file +#endif //Motion_h + + From 0d1854c8d7db2ca6be588117d4639d29290f8669 Mon Sep 17 00:00:00 2001 From: n1i9c9k9 Date: Wed, 13 Dec 2023 17:12:02 +0100 Subject: [PATCH 15/28] suggestion for motion.h --- src/motion/Motion.h | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/src/motion/Motion.h b/src/motion/Motion.h index 16353d8..7199ef7 100644 --- a/src/motion/Motion.h +++ b/src/motion/Motion.h @@ -28,21 +28,19 @@ public: * @brief Move forward for a certain amount of time. * @param moveForMs Representing the duration of forward moving in milliseconds. */ - void move(u8int moveForMs); + void move(uint8_t moveForMs); /** * @brief Rotate left for a certain amount of time. * @param rotateForMs Representing the duration of rotating left in milliseconds. */ - void rotateLeft(u8int rotateForMs); + void rotateLeft(uint8_t rotateForMs); /** * @brief Rotate right for a certain amount of time. * @param rotateForMs Representing the duration of rotating right in milliseconds. */ - void rotateRight(u8int rotateForMs); + void rotateRight(uint8_t rotateForMs); }; -#endif //Motion_h - - +#endif //Motion_h \ No newline at end of file From 32f82557b3f2dadb377afd5a6c4df3393287506d Mon Sep 17 00:00:00 2001 From: n1i9c9k9 Date: Wed, 3 Jan 2024 11:37:58 +0100 Subject: [PATCH 16/28] changed int to 32 bits and method names --- src/motion/Motion.h | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/src/motion/Motion.h b/src/motion/Motion.h index 7199ef7..e0edb66 100644 --- a/src/motion/Motion.h +++ b/src/motion/Motion.h @@ -11,6 +11,8 @@ #ifndef Motion_h #define Motion_h +#include + class Motion{ protected: @@ -28,19 +30,19 @@ public: * @brief Move forward for a certain amount of time. * @param moveForMs Representing the duration of forward moving in milliseconds. */ - void move(uint8_t moveForMs); + void move(uint32_t moveForMs); /** - * @brief Rotate left for a certain amount of time. - * @param rotateForMs Representing the duration of rotating left in milliseconds. + * @brief Rotate clockwise for a certain amount of time. + * @param rotateForMs Representing the duration of rotating clockwise in milliseconds. */ - void rotateLeft(uint8_t rotateForMs); - + void rotateClockwise(uint32_t rotateForMs); + /** - * @brief Rotate right for a certain amount of time. - * @param rotateForMs Representing the duration of rotating right in milliseconds. + * @brief Rotate anticlockwise for a certain amount of time. + * @param rotateForMs Representing the duration of rotating anticlockwise in milliseconds. */ - void rotateRight(uint8_t rotateForMs); + void rotateAnticlockwise(uint32_t rotateForMs); }; #endif //Motion_h \ No newline at end of file From 4165afd578f098450bed3b71407c58a50c8b1888 Mon Sep 17 00:00:00 2001 From: 99cardz Date: Mon, 8 Jan 2024 15:37:39 +0100 Subject: [PATCH 17/28] implement move method --- src/motion/Motion.h | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/motion/Motion.h b/src/motion/Motion.h index e0edb66..7348769 100644 --- a/src/motion/Motion.h +++ b/src/motion/Motion.h @@ -13,11 +13,15 @@ #define Motion_h #include +const int MOTOR_RIGHT_PIN = 11; +const int MOTOR_LEFT_PIN = 12; + class Motion{ protected: public: + Motion(); /** From 8fa5de4b12f8927e8e7589b2997bb3c3af2ef52f Mon Sep 17 00:00:00 2001 From: 99cardz Date: Mon, 8 Jan 2024 15:37:52 +0100 Subject: [PATCH 18/28] implement move method --- src/motion/Motion.cpp | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 src/motion/Motion.cpp diff --git a/src/motion/Motion.cpp b/src/motion/Motion.cpp new file mode 100644 index 0000000..028aed3 --- /dev/null +++ b/src/motion/Motion.cpp @@ -0,0 +1,41 @@ +/** + * @file Motion.cpp + * @author Jonathan Schulze, Nick Hübenthal + * @brief Implementation of the Motion class. + * @version 0.1 + * @date 2023-12-13 + * + * @copyright Copyright (c) 2023 + * + */ + +#include "Motion.h" + +// Constructor +Motion::Motion() { + +} + +// Initialize the movement component. +void Motion::begin(void) { + +} + +// Move forward for a certain amount of time. +void Motion::move(uint32_t moveForMs) { + analogWrite(MOTOR_LEFT_PIN, 128); + analogWrite(MOTOR_RIGHT_PIN, 128); + vTaskDelay(moveForMs); + analogWrite(MOTOR_LEFT_PIN, 0); + analogWrite(MOTOR_RIGHT_PIN, 0); +} + +// Rotate clockwise for a certain amount of time. +void Motion::rotateClockwise(uint32_t rotateForMs) { + +} + +// Rotate anticlockwise for a certain amount of time. +void Motion::rotateAnticlockwise(uint32_t rotateForMs) { + +} From f2253a7cb5e940e4707d35706b26a66e7298cc0f Mon Sep 17 00:00:00 2001 From: 99cardz Date: Mon, 8 Jan 2024 15:44:36 +0100 Subject: [PATCH 19/28] added Arduino and vtask imports --- src/motion/Motion.h | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/motion/Motion.h b/src/motion/Motion.h index 7348769..eb1eccc 100644 --- a/src/motion/Motion.h +++ b/src/motion/Motion.h @@ -12,6 +12,9 @@ #ifndef Motion_h #define Motion_h #include +#include +#include +#include const int MOTOR_RIGHT_PIN = 11; const int MOTOR_LEFT_PIN = 12; From e42058bb82f6d111c284cd129f2253fccd052e14 Mon Sep 17 00:00:00 2001 From: 99cardz Date: Mon, 8 Jan 2024 16:26:56 +0100 Subject: [PATCH 20/28] experimenting with xTaskCreate --- src/Dezibot.cpp | 54 +------------------------------------------ src/motion/Motion.cpp | 12 ++++++---- 2 files changed, 9 insertions(+), 57 deletions(-) diff --git a/src/Dezibot.cpp b/src/Dezibot.cpp index 3285444..38eb327 100644 --- a/src/Dezibot.cpp +++ b/src/Dezibot.cpp @@ -26,57 +26,5 @@ Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET); #define GPIO_LED 48 void Dezibot::begin(void) { - Adafruit_NeoPixel ledStrip = Adafruit_NeoPixel(3, GPIO_LED, NEO_GRB + NEO_KHZ800); - Wire.begin(1, 2); - if (!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) { - Serial.println("SSD1306 allocation failed"); - for (;;); // Don't proceed, loop forever - } - - display.display(); - vTaskDelay(2000); - display.clearDisplay(); - vTaskDelay(2000); - - // Draw a single pixel in white - display.drawPixel(10, 10, SSD1306_WHITE); - - // Show the display buffer on the screen. You MUST call display() after - // drawing commands to make them visible on screen! - display.display(); - vTaskDelay(2000); - - Serial.begin(9600); - Serial.println("start"); - - - vTaskDelay(1000); - - while (1) { - /* Blink off (output low) */ - ledStrip.setPixelColor(1, ledStrip.Color(100, 100, 100)); - ledStrip.show(); // Aktualisiere die Farbe des Pixels - vTaskDelay(1000); - /* Blink on (output high) */ - ledStrip.setPixelColor(1, ledStrip.Color(0, 0, 0)); - ledStrip.show(); // Aktualisiere die Farbe des Pixels - vTaskDelay(1000); - - struct timeval tv_now; - gettimeofday(&tv_now, NULL); - int64_t time_us = (int64_t) tv_now.tv_sec * 1000000L + (int64_t) tv_now.tv_usec; - - - Serial.println(time_us); - - display.clearDisplay(); - - display.setTextSize(2); // Draw 2X-scale text - display.setTextColor(SSD1306_WHITE); - display.setCursor(10, 0); - display.println(F("scroll")); - display.display(); // Show initial text - vTaskDelay(1000); - - } + motion.begin(); } \ No newline at end of file diff --git a/src/motion/Motion.cpp b/src/motion/Motion.cpp index 028aed3..d4c0113 100644 --- a/src/motion/Motion.cpp +++ b/src/motion/Motion.cpp @@ -20,16 +20,20 @@ Motion::Motion() { void Motion::begin(void) { } - -// Move forward for a certain amount of time. -void Motion::move(uint32_t moveForMs) { +void moveTask(void * moveForMs) { analogWrite(MOTOR_LEFT_PIN, 128); analogWrite(MOTOR_RIGHT_PIN, 128); - vTaskDelay(moveForMs); + vTaskDelay((uint32_t) moveForMs); analogWrite(MOTOR_LEFT_PIN, 0); analogWrite(MOTOR_RIGHT_PIN, 0); } +// Move forward for a certain amount of time. +void Motion::move(uint32_t moveForMs) { + xTaskCreate(moveTask, "Move", configMINIMAL_STACK_SIZE, (void*)moveForMs, 1, NULL); +} + + // Rotate clockwise for a certain amount of time. void Motion::rotateClockwise(uint32_t rotateForMs) { From 3d4b62438ea8f2f7128dfe1f03ed602f557a0dfe Mon Sep 17 00:00:00 2001 From: 99cardz Date: Wed, 10 Jan 2024 12:48:28 +0100 Subject: [PATCH 21/28] added arduino-cli commands to upload and compile --- README.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/README.md b/README.md index b2fb3b1..c0d6b18 100644 --- a/README.md +++ b/README.md @@ -136,6 +136,10 @@ For instance, in `src/Dezibot.h`, to include `src/motion/Motion.h`, you should w * USB Mode: "Hardware CDC and JTAG" * Programmer: "Esptool" +Using `arduino-cli` to compile and upload: +`arduino-cli upload /Users/jo/Documents/Arduino/theSketch -p /dev/cu.usbmodem101 -b esp32:esp32:nora_w10` +`arduino-cli compile /Users/jo/Documents/Arduino/theSketch -p /dev/cu.usbmodem101 -b esp32:esp32:nora_w10` + #### Display It is important to specify the SDA and SCL ports by using `Wire.begin(SDA, SCL)`. From 5a731e5fa418d3c7304bf2da657c93c2d5d735bd Mon Sep 17 00:00:00 2001 From: 99cardz Date: Wed, 10 Jan 2024 13:57:03 +0100 Subject: [PATCH 22/28] vTask Testing --- src/motion/Motion.cpp | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/motion/Motion.cpp b/src/motion/Motion.cpp index d4c0113..049a75d 100644 --- a/src/motion/Motion.cpp +++ b/src/motion/Motion.cpp @@ -31,6 +31,11 @@ void moveTask(void * moveForMs) { // Move forward for a certain amount of time. void Motion::move(uint32_t moveForMs) { xTaskCreate(moveTask, "Move", configMINIMAL_STACK_SIZE, (void*)moveForMs, 1, NULL); + // analogWrite(MOTOR_LEFT_PIN, 128); + // analogWrite(MOTOR_RIGHT_PIN, 128); + // vTaskDelay(moveForMs); + // analogWrite(MOTOR_LEFT_PIN, 0); + // analogWrite(MOTOR_RIGHT_PIN, 0); } From fb3b4211d5a6ddfd3a40bd1ec49686b909ee7a8f Mon Sep 17 00:00:00 2001 From: 99cardz Date: Wed, 10 Jan 2024 15:56:26 +0100 Subject: [PATCH 23/28] tasks working now --- src/motion/Motion.cpp | 31 ++++++++++++++++++++++--------- 1 file changed, 22 insertions(+), 9 deletions(-) diff --git a/src/motion/Motion.cpp b/src/motion/Motion.cpp index 049a75d..5244d24 100644 --- a/src/motion/Motion.cpp +++ b/src/motion/Motion.cpp @@ -11,6 +11,10 @@ #include "Motion.h" +TaskHandle_t xMoveTaskHandle = NULL; +TaskHandle_t xClockwiseTaskHandle = NULL; +TaskHandle_t xAntiClockwiseTaskHandle = NULL; + // Constructor Motion::Motion() { @@ -20,31 +24,40 @@ Motion::Motion() { void Motion::begin(void) { } -void moveTask(void * moveForMs) { +void moveTask(void * args) { analogWrite(MOTOR_LEFT_PIN, 128); analogWrite(MOTOR_RIGHT_PIN, 128); - vTaskDelay((uint32_t) moveForMs); + vTaskDelay((uint32_t) args / portTICK_PERIOD_MS); analogWrite(MOTOR_LEFT_PIN, 0); analogWrite(MOTOR_RIGHT_PIN, 0); + vTaskDelete(xMoveTaskHandle); } // Move forward for a certain amount of time. void Motion::move(uint32_t moveForMs) { - xTaskCreate(moveTask, "Move", configMINIMAL_STACK_SIZE, (void*)moveForMs, 1, NULL); - // analogWrite(MOTOR_LEFT_PIN, 128); - // analogWrite(MOTOR_RIGHT_PIN, 128); - // vTaskDelay(moveForMs); - // analogWrite(MOTOR_LEFT_PIN, 0); - // analogWrite(MOTOR_RIGHT_PIN, 0); + xTaskCreate(moveTask, "Move", 4096, (void*)moveForMs, 10, &xMoveTaskHandle); } +void leftMotorTask(void * args) { + analogWrite(MOTOR_LEFT_PIN, 128); + vTaskDelay((uint32_t) args / portTICK_PERIOD_MS); + analogWrite(MOTOR_LEFT_PIN, 0); + vTaskDelete(xClockwiseTaskHandle); +} // Rotate clockwise for a certain amount of time. void Motion::rotateClockwise(uint32_t rotateForMs) { + xTaskCreate(leftMotorTask, "LeftMotor", 4096, (void*)rotateForMs, 10, &xClockwiseTaskHandle); +} +void rightMotorTask(void * args) { + analogWrite(MOTOR_RIGHT_PIN, 128); + vTaskDelay((uint32_t) args / portTICK_PERIOD_MS); + analogWrite(MOTOR_RIGHT_PIN, 0); + vTaskDelete(xAntiClockwiseTaskHandle); } // Rotate anticlockwise for a certain amount of time. void Motion::rotateAnticlockwise(uint32_t rotateForMs) { - + xTaskCreate(rightMotorTask, "RightMotor", 4096, (void*)rotateForMs, 10, &xAntiClockwiseTaskHandle); } From b8474815c0b4a1a54622c5f8ea862672c822e778 Mon Sep 17 00:00:00 2001 From: 99cardz Date: Tue, 6 Feb 2024 13:33:56 +0100 Subject: [PATCH 24/28] add example sketch --- example/motion.ino | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 example/motion.ino diff --git a/example/motion.ino b/example/motion.ino new file mode 100644 index 0000000..2bdfd47 --- /dev/null +++ b/example/motion.ino @@ -0,0 +1,18 @@ +#include +Dezibot dezibot = Dezibot(); +void setup() { + dezibot.begin(); + Serial.begin(9600); +} +void loop() { + Serial.println("bla"); + dezibot.motion.move(1000); + dezibot.multiColorLight.setLed(BOTTOM, RED); + delay(2000); dezibot.motion.rotateAnticlockwise(1000); + dezibot.multiColorLight.setLed(BOTTOM, GREEN); + delay(2000); dezibot.motion.rotateClockwise(1000); + dezibot.multiColorLight.setLed(BOTTOM, BLUE); + delay(2000); + dezibot.multiColorLight.turnOffLed(); + delay(2000); +} \ No newline at end of file From 45977c95b9018e6741e751bbf6af7f306f6b2c32 Mon Sep 17 00:00:00 2001 From: 99cardz Date: Tue, 6 Feb 2024 13:34:23 +0100 Subject: [PATCH 25/28] formatting --- example/motion.ino | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/example/motion.ino b/example/motion.ino index 2bdfd47..e1f6d37 100644 --- a/example/motion.ino +++ b/example/motion.ino @@ -8,9 +8,11 @@ void loop() { Serial.println("bla"); dezibot.motion.move(1000); dezibot.multiColorLight.setLed(BOTTOM, RED); - delay(2000); dezibot.motion.rotateAnticlockwise(1000); + delay(2000); + dezibot.motion.rotateAnticlockwise(1000); dezibot.multiColorLight.setLed(BOTTOM, GREEN); - delay(2000); dezibot.motion.rotateClockwise(1000); + delay(2000); + dezibot.motion.rotateClockwise(1000); dezibot.multiColorLight.setLed(BOTTOM, BLUE); delay(2000); dezibot.multiColorLight.turnOffLed(); From c8f7e953635699af58d0753cbc9bab884189dfd1 Mon Sep 17 00:00:00 2001 From: hhaupt Date: Sun, 28 Apr 2024 22:38:35 +0200 Subject: [PATCH 26/28] introduced possibility to run move commands without timelimit --- example/example.ino | 18 ---------- example/example/example.ino | 19 ++++++++++ example/{ => motion}/motion.ino | 0 example/plain.cpp | 5 --- src/motion/Motion.cpp | 62 +++++++++++++++++++++------------ src/motion/Motion.h | 29 +++++++++------ 6 files changed, 78 insertions(+), 55 deletions(-) delete mode 100644 example/example.ino create mode 100644 example/example/example.ino rename example/{ => motion}/motion.ino (100%) delete mode 100644 example/plain.cpp diff --git a/example/example.ino b/example/example.ino deleted file mode 100644 index f055251..0000000 --- a/example/example.ino +++ /dev/null @@ -1,18 +0,0 @@ -#include - -Dezibot dezibot = Dezibot(); -const uint8_t MYFOO = 10; -void setup() { - dezibot.begin(); - -} - -void loop() { -dezibot.multiColorLight.setLed(TOP_LEFT,0x000000FF); -dezibot.multiColorLight.setLed(TOP_RIGHT,dezibot.multiColorLight.color(0,100,0)); -dezibot.multiColorLight.blink(10,0x00FF0000,BOTTOM,500); - -delay(1000); -dezibot.multiColorLight.turnOff(ALL); -delay(1000); -} diff --git a/example/example/example.ino b/example/example/example.ino new file mode 100644 index 0000000..e376753 --- /dev/null +++ b/example/example/example.ino @@ -0,0 +1,19 @@ +#include + +Dezibot dezibot = Dezibot(); +const uint8_t MYFOO = 10; +void setup() { + dezibot.begin(); + +} + +void loop() { +dezibot.motion.move(); +delay(1000); +dezibot.motion.rotateAntiClockwise(); +delay(1000); +dezibot.motion.rotateClockwise(); +delay(1000); +dezibot.motion.stop(); +delay(1000); +} diff --git a/example/motion.ino b/example/motion/motion.ino similarity index 100% rename from example/motion.ino rename to example/motion/motion.ino diff --git a/example/plain.cpp b/example/plain.cpp deleted file mode 100644 index c8fad0c..0000000 --- a/example/plain.cpp +++ /dev/null @@ -1,5 +0,0 @@ -#include "Dezibot.h" - -int main(){ - return 0; -} \ No newline at end of file diff --git a/src/motion/Motion.cpp b/src/motion/Motion.cpp index 5244d24..e26df10 100644 --- a/src/motion/Motion.cpp +++ b/src/motion/Motion.cpp @@ -1,6 +1,6 @@ /** * @file Motion.cpp - * @author Jonathan Schulze, Nick Hübenthal + * @author Jonathan Schulze, Nick Hübenthal, Hans Haupt * @brief Implementation of the Motion class. * @version 0.1 * @date 2023-12-13 @@ -15,49 +15,67 @@ TaskHandle_t xMoveTaskHandle = NULL; TaskHandle_t xClockwiseTaskHandle = NULL; TaskHandle_t xAntiClockwiseTaskHandle = NULL; -// Constructor -Motion::Motion() { - -} - // Initialize the movement component. void Motion::begin(void) { -} -void moveTask(void * args) { - analogWrite(MOTOR_LEFT_PIN, 128); - analogWrite(MOTOR_RIGHT_PIN, 128); +}; +void Motion::moveTask(void * args) { + analogWrite(MOTOR_LEFT_PIN, LEFT_MOTOR_DUTY); + analogWrite(MOTOR_RIGHT_PIN, RIGHT_MOTOR_DUTY); vTaskDelay((uint32_t) args / portTICK_PERIOD_MS); analogWrite(MOTOR_LEFT_PIN, 0); analogWrite(MOTOR_RIGHT_PIN, 0); vTaskDelete(xMoveTaskHandle); -} +}; // Move forward for a certain amount of time. void Motion::move(uint32_t moveForMs) { + if (moveForMs > 0){ xTaskCreate(moveTask, "Move", 4096, (void*)moveForMs, 10, &xMoveTaskHandle); -} + } else{ + analogWrite(MOTOR_LEFT_PIN, LEFT_MOTOR_DUTY); + analogWrite(MOTOR_RIGHT_PIN, RIGHT_MOTOR_DUTY); + } +}; -void leftMotorTask(void * args) { - analogWrite(MOTOR_LEFT_PIN, 128); +void Motion::leftMotorTask(void * args) { + analogWrite(MOTOR_LEFT_PIN, LEFT_MOTOR_DUTY); + analogWrite(MOTOR_RIGHT_PIN, 0); vTaskDelay((uint32_t) args / portTICK_PERIOD_MS); analogWrite(MOTOR_LEFT_PIN, 0); vTaskDelete(xClockwiseTaskHandle); -} +}; // Rotate clockwise for a certain amount of time. void Motion::rotateClockwise(uint32_t rotateForMs) { - xTaskCreate(leftMotorTask, "LeftMotor", 4096, (void*)rotateForMs, 10, &xClockwiseTaskHandle); -} + if (rotateForMs > 0){ + xTaskCreate(leftMotorTask, "LeftMotor", 4096, (void*)rotateForMs, 10, &xClockwiseTaskHandle); + } else { + analogWrite(MOTOR_LEFT_PIN,LEFT_MOTOR_DUTY); + analogWrite(MOTOR_RIGHT_PIN,0); + } +}; -void rightMotorTask(void * args) { - analogWrite(MOTOR_RIGHT_PIN, 128); +void Motion::rightMotorTask(void * args) { + analogWrite(MOTOR_RIGHT_PIN, RIGHT_MOTOR_DUTY); + analogWrite(MOTOR_LEFT_PIN,0); vTaskDelay((uint32_t) args / portTICK_PERIOD_MS); analogWrite(MOTOR_RIGHT_PIN, 0); vTaskDelete(xAntiClockwiseTaskHandle); -} +}; // Rotate anticlockwise for a certain amount of time. -void Motion::rotateAnticlockwise(uint32_t rotateForMs) { - xTaskCreate(rightMotorTask, "RightMotor", 4096, (void*)rotateForMs, 10, &xAntiClockwiseTaskHandle); +void Motion::rotateAntiClockwise(uint32_t rotateForMs) { + if(rotateForMs > 0){ + xTaskCreate(rightMotorTask, "RightMotor", 4096, (void*)rotateForMs, 10, &xAntiClockwiseTaskHandle); + } else { + analogWrite(MOTOR_RIGHT_PIN,RIGHT_MOTOR_DUTY); + analogWrite(MOTOR_LEFT_PIN,0); + } +}; + +void Motion::stop(void){ + analogWrite(MOTOR_LEFT_PIN,0); + analogWrite(MOTOR_RIGHT_PIN,0); } + diff --git a/src/motion/Motion.h b/src/motion/Motion.h index eb1eccc..6fa3f66 100644 --- a/src/motion/Motion.h +++ b/src/motion/Motion.h @@ -16,17 +16,17 @@ #include #include -const int MOTOR_RIGHT_PIN = 11; -const int MOTOR_LEFT_PIN = 12; - - class Motion{ protected: + static const uint8_t RIGHT_MOTOR_DUTY = 128; + static const uint8_t LEFT_MOTOR_DUTY = 128; + static const int MOTOR_RIGHT_PIN = 11; + static const int MOTOR_LEFT_PIN = 12; + static void moveTask(void * args); + static void leftMotorTask(void * args); + static void rightMotorTask(void * args); public: - - Motion(); - /** * @brief Initialize the movement component. * @@ -35,21 +35,30 @@ public: /** * @brief Move forward for a certain amount of time. + * Call with moveForMs 0 will start movement, that must be stopped explicit by call to stop(). * @param moveForMs Representing the duration of forward moving in milliseconds. */ - void move(uint32_t moveForMs); + static void move(uint32_t moveForMs=0); /** * @brief Rotate clockwise for a certain amount of time. + * Call with moveForMs 0 will start movement, that must be stopped explicit by call to stop(). * @param rotateForMs Representing the duration of rotating clockwise in milliseconds. */ - void rotateClockwise(uint32_t rotateForMs); + static void rotateClockwise(uint32_t rotateForMs=0); /** * @brief Rotate anticlockwise for a certain amount of time. + * Call with moveForMs 0 will start movement, that must be stopped explicit by call to stop(). * @param rotateForMs Representing the duration of rotating anticlockwise in milliseconds. */ - void rotateAnticlockwise(uint32_t rotateForMs); + static void rotateAntiClockwise(uint32_t rotateForMs=0); + + /** + * @brief stops any current movement, no matter if timebased or endless + * + */ + static void stop(void); }; #endif //Motion_h \ No newline at end of file From 60314ec101750e587ba39bb5ba92d811c8814b44 Mon Sep 17 00:00:00 2001 From: hhaupt Date: Sun, 12 May 2024 09:56:42 +0200 Subject: [PATCH 27/28] change motion from analogWrite to ledc, add fluent interface for motors --- src/motion/Motion.cpp | 49 +++++++++++++++++++++++++------------------ src/motion/Motion.h | 34 ++++++++++++++++++++++++++---- src/motion/Motor.cpp | 33 +++++++++++++++++++++++++++++ 3 files changed, 92 insertions(+), 24 deletions(-) create mode 100644 src/motion/Motor.cpp diff --git a/src/motion/Motion.cpp b/src/motion/Motion.cpp index e26df10..ceff927 100644 --- a/src/motion/Motion.cpp +++ b/src/motion/Motion.cpp @@ -2,7 +2,7 @@ * @file Motion.cpp * @author Jonathan Schulze, Nick Hübenthal, Hans Haupt * @brief Implementation of the Motion class. - * @version 0.1 + * @version 0.2 * @date 2023-12-13 * * @copyright Copyright (c) 2023 @@ -17,14 +17,23 @@ TaskHandle_t xAntiClockwiseTaskHandle = NULL; // Initialize the movement component. void Motion::begin(void) { - + ledc_timer_config_t motor_timer = { + .speed_mode = LEDC_MODE, + .duty_resolution = DUTY_RES, + .timer_num = TIMER, + .freq_hz = FREQUENCY, + .clk_cfg = LEDC_AUTO_CLK + }; + ledc_timer_config(&motor_timer); + Motion::left.begin(); + Motion::right.begin(); }; void Motion::moveTask(void * args) { - analogWrite(MOTOR_LEFT_PIN, LEFT_MOTOR_DUTY); - analogWrite(MOTOR_RIGHT_PIN, RIGHT_MOTOR_DUTY); + Motion::left.setSpeed(LEFT_MOTOR_DUTY); + Motion::right.setSpeed(RIGHT_MOTOR_DUTY); vTaskDelay((uint32_t) args / portTICK_PERIOD_MS); - analogWrite(MOTOR_LEFT_PIN, 0); - analogWrite(MOTOR_RIGHT_PIN, 0); + Motion::left.setSpeed(0); + Motion::right.setSpeed(0); vTaskDelete(xMoveTaskHandle); }; @@ -33,16 +42,16 @@ void Motion::move(uint32_t moveForMs) { if (moveForMs > 0){ xTaskCreate(moveTask, "Move", 4096, (void*)moveForMs, 10, &xMoveTaskHandle); } else{ - analogWrite(MOTOR_LEFT_PIN, LEFT_MOTOR_DUTY); - analogWrite(MOTOR_RIGHT_PIN, RIGHT_MOTOR_DUTY); + Motion::left.setSpeed(4096); + Motion::right.setSpeed(4096); } }; void Motion::leftMotorTask(void * args) { - analogWrite(MOTOR_LEFT_PIN, LEFT_MOTOR_DUTY); - analogWrite(MOTOR_RIGHT_PIN, 0); + Motion::left.setSpeed(LEFT_MOTOR_DUTY); + Motion::right.setSpeed(0); vTaskDelay((uint32_t) args / portTICK_PERIOD_MS); - analogWrite(MOTOR_LEFT_PIN, 0); + Motion::left.setSpeed(0); vTaskDelete(xClockwiseTaskHandle); }; @@ -51,16 +60,16 @@ void Motion::rotateClockwise(uint32_t rotateForMs) { if (rotateForMs > 0){ xTaskCreate(leftMotorTask, "LeftMotor", 4096, (void*)rotateForMs, 10, &xClockwiseTaskHandle); } else { - analogWrite(MOTOR_LEFT_PIN,LEFT_MOTOR_DUTY); - analogWrite(MOTOR_RIGHT_PIN,0); + Motion::left.setSpeed(LEFT_MOTOR_DUTY); + Motion::right.setSpeed(0); } }; void Motion::rightMotorTask(void * args) { - analogWrite(MOTOR_RIGHT_PIN, RIGHT_MOTOR_DUTY); - analogWrite(MOTOR_LEFT_PIN,0); + Motion::right.setSpeed(RIGHT_MOTOR_DUTY); + Motion::left.setSpeed(0); vTaskDelay((uint32_t) args / portTICK_PERIOD_MS); - analogWrite(MOTOR_RIGHT_PIN, 0); + Motion::right.setSpeed(0); vTaskDelete(xAntiClockwiseTaskHandle); }; @@ -69,13 +78,13 @@ void Motion::rotateAntiClockwise(uint32_t rotateForMs) { if(rotateForMs > 0){ xTaskCreate(rightMotorTask, "RightMotor", 4096, (void*)rotateForMs, 10, &xAntiClockwiseTaskHandle); } else { - analogWrite(MOTOR_RIGHT_PIN,RIGHT_MOTOR_DUTY); - analogWrite(MOTOR_LEFT_PIN,0); + Motion::right.setSpeed(RIGHT_MOTOR_DUTY); + Motion::left.setSpeed(0); } }; void Motion::stop(void){ - analogWrite(MOTOR_LEFT_PIN,0); - analogWrite(MOTOR_RIGHT_PIN,0); + Motion::left.setSpeed(0); + Motion::right.setSpeed(0); } diff --git a/src/motion/Motion.h b/src/motion/Motion.h index 6fa3f66..13ae523 100644 --- a/src/motion/Motion.h +++ b/src/motion/Motion.h @@ -1,8 +1,8 @@ /** * @file Motion.h - * @author Jonathan Schulze, Nick Hübenthal + * @author Jonathan Schulze, Nick Hübenthal, Hans Haupt * @brief This component controls the ability to rotate and change position. - * @version 0.1 + * @version 0.2 * @date 2023-12-13 * * @copyright Copyright (c) 2023 @@ -15,11 +15,31 @@ #include #include #include +#include "driver/ledc.h" +#define LEDC_MODE LEDC_LOW_SPEED_MODE +#define TIMER LEDC_TIMER_2 +#define CHANNEL_LEFT LEDC_CHANNEL_3 +#define CHANNEL_RIGHT LEDC_CHANNEL_4 +#define DUTY_RES LEDC_TIMER_13_BIT // Set duty resolution to 13 bits +#define FREQUENCY (5000) // Frequency in Hertz. Set frequency at 5 kHz +class Motor{ + public: + Motor(uint8_t pin, ledc_timer_t timer, ledc_channel_t channel); + void begin(void); + void setSpeed(uint16_t duty); + uint16_t getSpeed(void); + protected: + uint8_t pin; + ledc_timer_t timer; + ledc_channel_t channel; + + uint16_t duty; +}; class Motion{ protected: - static const uint8_t RIGHT_MOTOR_DUTY = 128; - static const uint8_t LEFT_MOTOR_DUTY = 128; + static const uint16_t RIGHT_MOTOR_DUTY = 4096; + static const uint16_t LEFT_MOTOR_DUTY = 4096; static const int MOTOR_RIGHT_PIN = 11; static const int MOTOR_LEFT_PIN = 12; static void moveTask(void * args); @@ -27,6 +47,10 @@ protected: static void rightMotorTask(void * args); public: + //Shared Timer to sync movement + static inline Motor left = Motor(MOTOR_LEFT_PIN,TIMER,CHANNEL_LEFT); + static inline Motor right = Motor(MOTOR_RIGHT_PIN,TIMER,CHANNEL_RIGHT); + /** * @brief Initialize the movement component. * @@ -61,4 +85,6 @@ public: static void stop(void); }; + + #endif //Motion_h \ No newline at end of file diff --git a/src/motion/Motor.cpp b/src/motion/Motor.cpp new file mode 100644 index 0000000..4dfc3a4 --- /dev/null +++ b/src/motion/Motor.cpp @@ -0,0 +1,33 @@ +#include "Motion.h" + +Motor::Motor(uint8_t pin, ledc_timer_t timer, ledc_channel_t channel){ + this->pin = pin; + this->channel = channel; + this->timer = timer; + this->duty = 0; +}; + +void Motor::begin(void){ + pinMode(this->pin,OUTPUT); + ledc_channel_config_t channelConfig = { + .gpio_num = this->pin, + .speed_mode = LEDC_MODE, + .channel = this->channel, + .intr_type = LEDC_INTR_DISABLE, + .timer_sel = this->timer, + .duty = 0, // Set duty to 0% + .hpoint = 0 + }; + ledc_channel_config(&channelConfig); + Serial.println("Motor begin done"); +}; + +void Motor::setSpeed(uint16_t duty){ + this->duty = duty; + ledc_set_duty(LEDC_MODE,this->channel,duty); + ledc_update_duty(LEDC_MODE,this->channel); +}; + +uint16_t Motor::getSpeed(void){ + return this->duty; +}; From ad41f6b5ca3d096864efa8a3d88777547f55e657 Mon Sep 17 00:00:00 2001 From: hhaupt Date: Sun, 12 May 2024 11:41:20 +0200 Subject: [PATCH 28/28] changed numieric constant with correct constant variable in move() --- src/motion/Motion.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/motion/Motion.cpp b/src/motion/Motion.cpp index ceff927..812ca92 100644 --- a/src/motion/Motion.cpp +++ b/src/motion/Motion.cpp @@ -42,8 +42,8 @@ void Motion::move(uint32_t moveForMs) { if (moveForMs > 0){ xTaskCreate(moveTask, "Move", 4096, (void*)moveForMs, 10, &xMoveTaskHandle); } else{ - Motion::left.setSpeed(4096); - Motion::right.setSpeed(4096); + Motion::left.setSpeed(LEFT_MOTOR_DUTY); + Motion::right.setSpeed(RIGHT_MOTOR_DUTY); } };