From 01b99d9716af6dda8291126a2c3c754bb7c3df67 Mon Sep 17 00:00:00 2001 From: Mathis Date: Thu, 16 Nov 2023 09:55:30 +0100 Subject: [PATCH 01/12] [feature/Servo_Motor] - add ServoMotorComponent --- .vscode/settings.json | 5 +++ .../ServoMotorComponent.cpp | 32 +++++++++++++++++++ lib/ServoMotorComponent/ServoMotorComponent.h | 27 ++++++++++++++++ platformio.ini | 1 + 4 files changed, 65 insertions(+) create mode 100644 .vscode/settings.json create mode 100644 lib/ServoMotorComponent/ServoMotorComponent.cpp create mode 100644 lib/ServoMotorComponent/ServoMotorComponent.h diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..03ba48b --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,5 @@ +{ + "files.associations": { + "new": "cpp" + } +} \ No newline at end of file diff --git a/lib/ServoMotorComponent/ServoMotorComponent.cpp b/lib/ServoMotorComponent/ServoMotorComponent.cpp new file mode 100644 index 0000000..11c6871 --- /dev/null +++ b/lib/ServoMotorComponent/ServoMotorComponent.cpp @@ -0,0 +1,32 @@ +#include "ServoMotorComponent.h" + +ServoMotorComponent::ServoMotorComponent(int PIN) { + this->PIN = PIN; + this->position = 0; + this->myservo.attach(PIN); + this->myservo.write(0); +} + +void ServoMotorComponent::goLeft() { + this->myservo.write(0); + this->position = 0; +} + +void ServoMotorComponent::goRight() { + this->myservo.write(180); + this->position = 180; +} + +void ServoMotorComponent::goMiddle() { + this->myservo.write(90); + this->position = 90; +} + +int ServoMotorComponent::getPosition() { + return this->position; +} + +void ServoMotorComponent::setPin(int PIN) { + this->PIN = PIN; + this->myservo.attach(PIN); +} diff --git a/lib/ServoMotorComponent/ServoMotorComponent.h b/lib/ServoMotorComponent/ServoMotorComponent.h new file mode 100644 index 0000000..c5a08c1 --- /dev/null +++ b/lib/ServoMotorComponent/ServoMotorComponent.h @@ -0,0 +1,27 @@ +#ifndef SERVOMOTOT_COMPONENT_H +#define SERVOMOTOT_COMPONENT_H + +#include + +class ServoMotorComponent +{ +public: + ServoMotorComponent(int PIN); + ~ServoMotorComponent() = default; + void goLeft(); + void goRight(); + void goMiddle(); + int getPosition(); + void setPin(int PIN); + +private: + int PIN; + int position; + Servo myservo; +}; + + + + + +#endif //SERVOMOTOT_COMPONENT_H diff --git a/platformio.ini b/platformio.ini index 9724c56..fb42dd8 100644 --- a/platformio.ini +++ b/platformio.ini @@ -54,6 +54,7 @@ lib_deps = bblanchon/ArduinoJson@^6.21.3 ; JSON serializer et deserializer m5stack/M5Stack@^0.4.5 ; M5 Lib m5stack/M5GFX@^0.1.9 ; M5 Lib pour le LCD + arduino-libraries/Servo@^1.2.1 ; Lib pour le Servo Motor ; example: ; erropix/ESP32 AnalogWrite@0.2 -- 2.47.1 From 844cbf817ccdd6eea76c87962b9afa0c51cbf79a Mon Sep 17 00:00:00 2001 From: Mathis Date: Fri, 17 Nov 2023 09:59:15 +0100 Subject: [PATCH 02/12] [feature/ServoMotor] - change servo librarie --- lib/ServoMotorComponent/{ => src}/ServoMotorComponent.cpp | 8 ++++---- lib/ServoMotorComponent/{ => src}/ServoMotorComponent.h | 0 platformio.ini | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) rename lib/ServoMotorComponent/{ => src}/ServoMotorComponent.cpp (80%) rename lib/ServoMotorComponent/{ => src}/ServoMotorComponent.h (100%) diff --git a/lib/ServoMotorComponent/ServoMotorComponent.cpp b/lib/ServoMotorComponent/src/ServoMotorComponent.cpp similarity index 80% rename from lib/ServoMotorComponent/ServoMotorComponent.cpp rename to lib/ServoMotorComponent/src/ServoMotorComponent.cpp index 11c6871..fef7531 100644 --- a/lib/ServoMotorComponent/ServoMotorComponent.cpp +++ b/lib/ServoMotorComponent/src/ServoMotorComponent.cpp @@ -4,21 +4,21 @@ ServoMotorComponent::ServoMotorComponent(int PIN) { this->PIN = PIN; this->position = 0; this->myservo.attach(PIN); - this->myservo.write(0); } void ServoMotorComponent::goLeft() { - this->myservo.write(0); + this->myservo.write(this->PIN, 0); this->position = 0; } void ServoMotorComponent::goRight() { - this->myservo.write(180); + this->myservo.write(this->PIN, 180); + this->position = 180; } void ServoMotorComponent::goMiddle() { - this->myservo.write(90); + this->myservo.write(this->PIN, 90); this->position = 90; } diff --git a/lib/ServoMotorComponent/ServoMotorComponent.h b/lib/ServoMotorComponent/src/ServoMotorComponent.h similarity index 100% rename from lib/ServoMotorComponent/ServoMotorComponent.h rename to lib/ServoMotorComponent/src/ServoMotorComponent.h diff --git a/platformio.ini b/platformio.ini index fb42dd8..324e983 100644 --- a/platformio.ini +++ b/platformio.ini @@ -54,7 +54,7 @@ lib_deps = bblanchon/ArduinoJson@^6.21.3 ; JSON serializer et deserializer m5stack/M5Stack@^0.4.5 ; M5 Lib m5stack/M5GFX@^0.1.9 ; M5 Lib pour le LCD - arduino-libraries/Servo@^1.2.1 ; Lib pour le Servo Motor + dlloydev/ESP32 ESP32S2 AnalogWrite ; example: ; erropix/ESP32 AnalogWrite@0.2 -- 2.47.1 From 3c7aea15a90eae1b2168056b19e48a840f475b67 Mon Sep 17 00:00:00 2001 From: Mathis Date: Fri, 8 Dec 2023 09:45:07 +0100 Subject: [PATCH 03/12] [ServoMotor] - rework servoMotor for adding speed and easing for movement --- include/Program.h | 2 ++ .../src/ServoMotorComponent.cpp | 26 ++++++++++++++++--- .../src/ServoMotorComponent.h | 5 ++++ src/Program.cpp | 25 +++++------------- 4 files changed, 36 insertions(+), 22 deletions(-) diff --git a/include/Program.h b/include/Program.h index b4f30bb..4ceb880 100644 --- a/include/Program.h +++ b/include/Program.h @@ -4,6 +4,7 @@ #include "Arduino.h" #include "DolibarrClient.h" #include +#include "ServoMotorComponent.h" class Program { public: @@ -18,6 +19,7 @@ public: void loop(); private: DolibarrClient *client; + ServoMotorComponent *servo; }; #endif diff --git a/lib/ServoMotorComponent/src/ServoMotorComponent.cpp b/lib/ServoMotorComponent/src/ServoMotorComponent.cpp index fef7531..4a67a25 100644 --- a/lib/ServoMotorComponent/src/ServoMotorComponent.cpp +++ b/lib/ServoMotorComponent/src/ServoMotorComponent.cpp @@ -2,23 +2,25 @@ ServoMotorComponent::ServoMotorComponent(int PIN) { this->PIN = PIN; - this->position = 0; + this->position = 30; + this->speed = 100; + this->easing = 0.5; this->myservo.attach(PIN); } void ServoMotorComponent::goLeft() { - this->myservo.write(this->PIN, 0); + this->myservo.write(this->PIN, 52, this->speed, this->easing); this->position = 0; } void ServoMotorComponent::goRight() { - this->myservo.write(this->PIN, 180); + this->myservo.write(this->PIN, 18, this->speed, this->easing); this->position = 180; } void ServoMotorComponent::goMiddle() { - this->myservo.write(this->PIN, 90); + this->myservo.write(this->PIN, 32, this->speed, this->easing); this->position = 90; } @@ -29,4 +31,20 @@ int ServoMotorComponent::getPosition() { void ServoMotorComponent::setPin(int PIN) { this->PIN = PIN; this->myservo.attach(PIN); + return; +} + +void ServoMotorComponent::setSpeed(int speed) { + this->speed = speed; + return; +} + +void ServoMotorComponent::pause() { + this->myservo.pause(); + return; +} + +void ServoMotorComponent::setEasing(float easing) { + this->easing = easing; + return; } diff --git a/lib/ServoMotorComponent/src/ServoMotorComponent.h b/lib/ServoMotorComponent/src/ServoMotorComponent.h index c5a08c1..3d74c92 100644 --- a/lib/ServoMotorComponent/src/ServoMotorComponent.h +++ b/lib/ServoMotorComponent/src/ServoMotorComponent.h @@ -13,10 +13,15 @@ public: void goMiddle(); int getPosition(); void setPin(int PIN); + void pause(); + void setSpeed(int speed); + void setEasing(float easing); private: int PIN; int position; + int speed; + float easing; Servo myservo; }; diff --git a/src/Program.cpp b/src/Program.cpp index 8454899..3d7ebe7 100644 --- a/src/Program.cpp +++ b/src/Program.cpp @@ -2,28 +2,17 @@ #include "Arduino.h" #include "DolibarrClient.h" -int initialize_wifi(WifiConfig wifi) { - WiFiClass::mode(WIFI_STA); //Optional - WiFi.setSleep(false); - WiFi.begin(wifi.ssid, wifi.password); - Serial.print("Connecting "); - while(WiFiClass::status() != WL_CONNECTED){ - delay(WAITING_WIFI_DELAY); - Serial.print("."); - } - Serial.println("Connected to the WiFi network"); - return 0; -} - Program::Program() { - Serial.begin(MONITOR_SPEED); - struct WifiConfig wifi_c = {WIFI_SSID, WIFI_PASSWORD}; - struct DolibarrConfig dolibarr = {DOLIBARR_URL, DOLIBARR_API_TOKEN}; - initialize_wifi(wifi_c); - this->client = new DolibarrClient(dolibarr); + this->servo = new ServoMotorComponent(2); } void Program::loop() { + // this->servo->goLeft(); + // delay(3000); + this->servo->goMiddle(); + // delay(3000); + // this->servo->goRight(); + // delay(3000); } -- 2.47.1 From a66882e87778f64699e44e0b9ed16d161e90fe14 Mon Sep 17 00:00:00 2001 From: Mathis Date: Fri, 19 Jan 2024 09:48:21 +0100 Subject: [PATCH 04/12] Refacto servoMotor --- .../src/ServoMotorComponent.cpp | 70 ++++++++++--------- .../src/ServoMotorComponent.h | 26 ++++--- 2 files changed, 53 insertions(+), 43 deletions(-) diff --git a/lib/ServoMotorComponent/src/ServoMotorComponent.cpp b/lib/ServoMotorComponent/src/ServoMotorComponent.cpp index 4a67a25..2b9a515 100644 --- a/lib/ServoMotorComponent/src/ServoMotorComponent.cpp +++ b/lib/ServoMotorComponent/src/ServoMotorComponent.cpp @@ -1,50 +1,56 @@ #include "ServoMotorComponent.h" -ServoMotorComponent::ServoMotorComponent(int PIN) { +ServoMotorComponent::ServoMotorComponent(int PIN, unsigned long updatePeriod) { this->PIN = PIN; - this->position = 30; - this->speed = 100; - this->easing = 0.5; this->myservo.attach(PIN); + this->desiredposition = Position::MIDDLE; + this->currentPosition = 0; + this->lastUpTime = millis(); + this->updatePeriod = updatePeriod; } -void ServoMotorComponent::goLeft() { - this->myservo.write(this->PIN, 52, this->speed, this->easing); - this->position = 0; +int ServoMotorComponent::getCurrentPosition() { + return this->myservo.read(this->PIN); } -void ServoMotorComponent::goRight() { - this->myservo.write(this->PIN, 18, this->speed, this->easing); - - this->position = 180; +void ServoMotorComponent::setCurrentPosition() { + this->currentPosition = this->myservo.read(this->PIN); } -void ServoMotorComponent::goMiddle() { - this->myservo.write(this->PIN, 32, this->speed, this->easing); - this->position = 90; +void ServoMotorComponent::setDesiredPosition(Position desiredPosition) { + switch (desiredPosition) { + case Position::LEFT: + this->desiredposition = 52; + break; + case Position::MIDDLE: + this->desiredposition = 32; + break; + case Position::RIGHT: + this->desiredposition = 18; + break; + default: + break; + } } -int ServoMotorComponent::getPosition() { - return this->position; + +void ServoMotorComponent::refresh() { + if (this->desiredposition == this->currentPosition + || millis() - this->lastUpTime <= this->updatePeriod) return; + + if (this->currentPosition > this->desiredposition) { + this->currentPosition--; + } + if (this->currentPosition < this->desiredposition) { + this->currentPosition++; + } + this->lastUpTime = millis(); + this->myservo.write(this->PIN, this->currentPosition); } + void ServoMotorComponent::setPin(int PIN) { this->PIN = PIN; this->myservo.attach(PIN); return; -} - -void ServoMotorComponent::setSpeed(int speed) { - this->speed = speed; - return; -} - -void ServoMotorComponent::pause() { - this->myservo.pause(); - return; -} - -void ServoMotorComponent::setEasing(float easing) { - this->easing = easing; - return; -} +} \ No newline at end of file diff --git a/lib/ServoMotorComponent/src/ServoMotorComponent.h b/lib/ServoMotorComponent/src/ServoMotorComponent.h index 3d74c92..4ea850d 100644 --- a/lib/ServoMotorComponent/src/ServoMotorComponent.h +++ b/lib/ServoMotorComponent/src/ServoMotorComponent.h @@ -3,26 +3,30 @@ #include +enum Position { + LEFT, + MIDDLE, + RIGHT +}; + class ServoMotorComponent { public: - ServoMotorComponent(int PIN); + ServoMotorComponent(int PIN, unsigned long updatePeriod); ~ServoMotorComponent() = default; - void goLeft(); - void goRight(); - void goMiddle(); - int getPosition(); + int getCurrentPosition(); + void setCurrentPosition(); + void setDesiredPosition(Position desiredPosition); void setPin(int PIN); - void pause(); - void setSpeed(int speed); - void setEasing(float easing); + void refresh(); private: int PIN; - int position; - int speed; - float easing; + int currentPosition; + int desiredposition; Servo myservo; + unsigned long lastUpTime; + unsigned long updatePeriod; }; -- 2.47.1 From 81c9ececf6cc0b6c87f7b4c743ef05dd36d26112 Mon Sep 17 00:00:00 2001 From: Mathis Date: Fri, 19 Jan 2024 11:35:25 +0100 Subject: [PATCH 05/12] Retour Clement --- .../src/ServoMotorComponent.cpp | 37 ++++++++++--------- .../src/ServoMotorComponent.h | 11 ++---- src/Program.cpp | 25 +++++++++---- 3 files changed, 42 insertions(+), 31 deletions(-) diff --git a/lib/ServoMotorComponent/src/ServoMotorComponent.cpp b/lib/ServoMotorComponent/src/ServoMotorComponent.cpp index 2b9a515..d23dd40 100644 --- a/lib/ServoMotorComponent/src/ServoMotorComponent.cpp +++ b/lib/ServoMotorComponent/src/ServoMotorComponent.cpp @@ -1,22 +1,29 @@ #include "ServoMotorComponent.h" -ServoMotorComponent::ServoMotorComponent(int PIN, unsigned long updatePeriod) { + +///////////////////////////////////////////////////////////////////////////////////// +// Functions for setting up the ServoMotor +///////////////////////////////////////////////////////////////////////////////////// + +/** + * Constructor. + * Prepares the ServoMotor. + */ +ServoMotorComponent::ServoMotorComponent(int PIN, unsigned long updatePeriod, float step) { this->PIN = PIN; this->myservo.attach(PIN); this->desiredposition = Position::MIDDLE; - this->currentPosition = 0; + this->currentPosition = 32; this->lastUpTime = millis(); this->updatePeriod = updatePeriod; + this->step = step; + this->myservo.write(this->PIN, this->currentPosition); } -int ServoMotorComponent::getCurrentPosition() { - return this->myservo.read(this->PIN); -} - -void ServoMotorComponent::setCurrentPosition() { - this->currentPosition = this->myservo.read(this->PIN); -} - +/** + * Set the desired position + * @desiredPosition: Give desired position + */ void ServoMotorComponent::setDesiredPosition(Position desiredPosition) { switch (desiredPosition) { case Position::LEFT: @@ -34,6 +41,9 @@ void ServoMotorComponent::setDesiredPosition(Position desiredPosition) { } +/** + * Write a new servoMotor position when it's necessary + */ void ServoMotorComponent::refresh() { if (this->desiredposition == this->currentPosition || millis() - this->lastUpTime <= this->updatePeriod) return; @@ -46,11 +56,4 @@ void ServoMotorComponent::refresh() { } this->lastUpTime = millis(); this->myservo.write(this->PIN, this->currentPosition); -} - - -void ServoMotorComponent::setPin(int PIN) { - this->PIN = PIN; - this->myservo.attach(PIN); - return; } \ No newline at end of file diff --git a/lib/ServoMotorComponent/src/ServoMotorComponent.h b/lib/ServoMotorComponent/src/ServoMotorComponent.h index 4ea850d..e493743 100644 --- a/lib/ServoMotorComponent/src/ServoMotorComponent.h +++ b/lib/ServoMotorComponent/src/ServoMotorComponent.h @@ -12,21 +12,18 @@ enum Position { class ServoMotorComponent { public: - ServoMotorComponent(int PIN, unsigned long updatePeriod); - ~ServoMotorComponent() = default; - int getCurrentPosition(); - void setCurrentPosition(); + ServoMotorComponent(int PIN, unsigned long updatePeriod, float step = 1); void setDesiredPosition(Position desiredPosition); - void setPin(int PIN); void refresh(); private: int PIN; - int currentPosition; - int desiredposition; + float currentPosition; + float desiredposition; Servo myservo; unsigned long lastUpTime; unsigned long updatePeriod; + float step; }; diff --git a/src/Program.cpp b/src/Program.cpp index 3d7ebe7..8454899 100644 --- a/src/Program.cpp +++ b/src/Program.cpp @@ -2,17 +2,28 @@ #include "Arduino.h" #include "DolibarrClient.h" +int initialize_wifi(WifiConfig wifi) { + WiFiClass::mode(WIFI_STA); //Optional + WiFi.setSleep(false); + WiFi.begin(wifi.ssid, wifi.password); + Serial.print("Connecting "); + while(WiFiClass::status() != WL_CONNECTED){ + delay(WAITING_WIFI_DELAY); + Serial.print("."); + } + Serial.println("Connected to the WiFi network"); + return 0; +} + Program::Program() { - this->servo = new ServoMotorComponent(2); + Serial.begin(MONITOR_SPEED); + struct WifiConfig wifi_c = {WIFI_SSID, WIFI_PASSWORD}; + struct DolibarrConfig dolibarr = {DOLIBARR_URL, DOLIBARR_API_TOKEN}; + initialize_wifi(wifi_c); + this->client = new DolibarrClient(dolibarr); } void Program::loop() { - // this->servo->goLeft(); - // delay(3000); - this->servo->goMiddle(); - // delay(3000); - // this->servo->goRight(); - // delay(3000); } -- 2.47.1 From 1e59d9dc46bcec92ef8e3a10718a2e0a356ec509 Mon Sep 17 00:00:00 2001 From: Mathis Date: Fri, 19 Jan 2024 16:37:35 +0100 Subject: [PATCH 06/12] add config.ini --- config.ini | 7 +++++++ lib/ServoMotorComponent/src/ServoMotorComponent.cpp | 12 ++++++------ src/Program.cpp | 12 ++++++++---- 3 files changed, 21 insertions(+), 10 deletions(-) diff --git a/config.ini b/config.ini index dff2d75..4c3c3ba 100644 --- a/config.ini +++ b/config.ini @@ -14,3 +14,10 @@ build_flags = -D MONITOR_SPEED=${config.monitor_speed} ; DO NOT TOUCH --- END -D WAITING_WIFI_DELAY=1000 + + ;;;;;;;;;;;;;;;;;;;;;; + ;;; Servo config ;;; + ;;;;;;;;;;;;;;;;;;;;;; + -D RIGHT_POS=18 + -D MIDDLE_POS=32 + -D LEFT_POS=52 diff --git a/lib/ServoMotorComponent/src/ServoMotorComponent.cpp b/lib/ServoMotorComponent/src/ServoMotorComponent.cpp index d23dd40..950e1c7 100644 --- a/lib/ServoMotorComponent/src/ServoMotorComponent.cpp +++ b/lib/ServoMotorComponent/src/ServoMotorComponent.cpp @@ -13,7 +13,7 @@ ServoMotorComponent::ServoMotorComponent(int PIN, unsigned long updatePeriod, f this->PIN = PIN; this->myservo.attach(PIN); this->desiredposition = Position::MIDDLE; - this->currentPosition = 32; + this->currentPosition = MIDDLE_POS; this->lastUpTime = millis(); this->updatePeriod = updatePeriod; this->step = step; @@ -27,13 +27,13 @@ ServoMotorComponent::ServoMotorComponent(int PIN, unsigned long updatePeriod, f void ServoMotorComponent::setDesiredPosition(Position desiredPosition) { switch (desiredPosition) { case Position::LEFT: - this->desiredposition = 52; + this->desiredposition = LEFT_POS; break; case Position::MIDDLE: - this->desiredposition = 32; + this->desiredposition = MIDDLE_POS; break; case Position::RIGHT: - this->desiredposition = 18; + this->desiredposition = RIGHT_POS; break; default: break; @@ -49,10 +49,10 @@ void ServoMotorComponent::refresh() { || millis() - this->lastUpTime <= this->updatePeriod) return; if (this->currentPosition > this->desiredposition) { - this->currentPosition--; + this->currentPosition -= this->step; } if (this->currentPosition < this->desiredposition) { - this->currentPosition++; + this->currentPosition += this->step; } this->lastUpTime = millis(); this->myservo.write(this->PIN, this->currentPosition); diff --git a/src/Program.cpp b/src/Program.cpp index 8454899..0126fe1 100644 --- a/src/Program.cpp +++ b/src/Program.cpp @@ -1,6 +1,7 @@ #include "Program.h" #include "Arduino.h" #include "DolibarrClient.h" +#include "ServoMotorComponent.h" int initialize_wifi(WifiConfig wifi) { WiFiClass::mode(WIFI_STA); //Optional @@ -17,13 +18,16 @@ int initialize_wifi(WifiConfig wifi) { Program::Program() { Serial.begin(MONITOR_SPEED); - struct WifiConfig wifi_c = {WIFI_SSID, WIFI_PASSWORD}; - struct DolibarrConfig dolibarr = {DOLIBARR_URL, DOLIBARR_API_TOKEN}; - initialize_wifi(wifi_c); - this->client = new DolibarrClient(dolibarr); + this->servo = new ServoMotorComponent(2, 1, 0.1); + //struct WifiConfig wifi_c = {WIFI_SSID, WIFI_PASSWORD}; + //struct DolibarrConfig dolibarr = {DOLIBARR_URL, DOLIBARR_API_TOKEN}; + //initialize_wifi(wifi_c); + //this->client = new DolibarrClient(dolibarr); } void Program::loop() { + this->servo->refresh(); + this->servo->setDesiredPosition(Position::LEFT); } -- 2.47.1 From f3dc069f3b78b28a6e0dc60a9ead435904bbb1b3 Mon Sep 17 00:00:00 2001 From: Mathis Date: Thu, 16 Nov 2023 09:55:30 +0100 Subject: [PATCH 07/12] [feature/Servo_Motor] - add ServoMotorComponent --- .vscode/settings.json | 5 +++ .../ServoMotorComponent.cpp | 32 +++++++++++++++++++ lib/ServoMotorComponent/ServoMotorComponent.h | 27 ++++++++++++++++ platformio.ini | 1 + 4 files changed, 65 insertions(+) create mode 100644 .vscode/settings.json create mode 100644 lib/ServoMotorComponent/ServoMotorComponent.cpp create mode 100644 lib/ServoMotorComponent/ServoMotorComponent.h diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..03ba48b --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,5 @@ +{ + "files.associations": { + "new": "cpp" + } +} \ No newline at end of file diff --git a/lib/ServoMotorComponent/ServoMotorComponent.cpp b/lib/ServoMotorComponent/ServoMotorComponent.cpp new file mode 100644 index 0000000..11c6871 --- /dev/null +++ b/lib/ServoMotorComponent/ServoMotorComponent.cpp @@ -0,0 +1,32 @@ +#include "ServoMotorComponent.h" + +ServoMotorComponent::ServoMotorComponent(int PIN) { + this->PIN = PIN; + this->position = 0; + this->myservo.attach(PIN); + this->myservo.write(0); +} + +void ServoMotorComponent::goLeft() { + this->myservo.write(0); + this->position = 0; +} + +void ServoMotorComponent::goRight() { + this->myservo.write(180); + this->position = 180; +} + +void ServoMotorComponent::goMiddle() { + this->myservo.write(90); + this->position = 90; +} + +int ServoMotorComponent::getPosition() { + return this->position; +} + +void ServoMotorComponent::setPin(int PIN) { + this->PIN = PIN; + this->myservo.attach(PIN); +} diff --git a/lib/ServoMotorComponent/ServoMotorComponent.h b/lib/ServoMotorComponent/ServoMotorComponent.h new file mode 100644 index 0000000..c5a08c1 --- /dev/null +++ b/lib/ServoMotorComponent/ServoMotorComponent.h @@ -0,0 +1,27 @@ +#ifndef SERVOMOTOT_COMPONENT_H +#define SERVOMOTOT_COMPONENT_H + +#include + +class ServoMotorComponent +{ +public: + ServoMotorComponent(int PIN); + ~ServoMotorComponent() = default; + void goLeft(); + void goRight(); + void goMiddle(); + int getPosition(); + void setPin(int PIN); + +private: + int PIN; + int position; + Servo myservo; +}; + + + + + +#endif //SERVOMOTOT_COMPONENT_H diff --git a/platformio.ini b/platformio.ini index 590c0d3..03efb22 100644 --- a/platformio.ini +++ b/platformio.ini @@ -55,6 +55,7 @@ lib_deps = m5stack/M5Stack@^0.4.5 ; M5 Lib m5stack/M5GFX@^0.1.9 ; M5 Lib pour le LCD m5stack/Module_GRBL_13.2@^0.0.3 ; M5 Lib pour Stepper (GRBL) + arduino-libraries/Servo@^1.2.1 ; Lib pour le Servo Motor ; example: ; erropix/ESP32 AnalogWrite@0.2 -- 2.47.1 From 027015b33b58a04f5ffa742c99ee3fc0d5cbb6ab Mon Sep 17 00:00:00 2001 From: Mathis Date: Fri, 17 Nov 2023 09:59:15 +0100 Subject: [PATCH 08/12] [feature/ServoMotor] - change servo librarie --- lib/ServoMotorComponent/{ => src}/ServoMotorComponent.cpp | 8 ++++---- lib/ServoMotorComponent/{ => src}/ServoMotorComponent.h | 0 platformio.ini | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) rename lib/ServoMotorComponent/{ => src}/ServoMotorComponent.cpp (80%) rename lib/ServoMotorComponent/{ => src}/ServoMotorComponent.h (100%) diff --git a/lib/ServoMotorComponent/ServoMotorComponent.cpp b/lib/ServoMotorComponent/src/ServoMotorComponent.cpp similarity index 80% rename from lib/ServoMotorComponent/ServoMotorComponent.cpp rename to lib/ServoMotorComponent/src/ServoMotorComponent.cpp index 11c6871..fef7531 100644 --- a/lib/ServoMotorComponent/ServoMotorComponent.cpp +++ b/lib/ServoMotorComponent/src/ServoMotorComponent.cpp @@ -4,21 +4,21 @@ ServoMotorComponent::ServoMotorComponent(int PIN) { this->PIN = PIN; this->position = 0; this->myservo.attach(PIN); - this->myservo.write(0); } void ServoMotorComponent::goLeft() { - this->myservo.write(0); + this->myservo.write(this->PIN, 0); this->position = 0; } void ServoMotorComponent::goRight() { - this->myservo.write(180); + this->myservo.write(this->PIN, 180); + this->position = 180; } void ServoMotorComponent::goMiddle() { - this->myservo.write(90); + this->myservo.write(this->PIN, 90); this->position = 90; } diff --git a/lib/ServoMotorComponent/ServoMotorComponent.h b/lib/ServoMotorComponent/src/ServoMotorComponent.h similarity index 100% rename from lib/ServoMotorComponent/ServoMotorComponent.h rename to lib/ServoMotorComponent/src/ServoMotorComponent.h diff --git a/platformio.ini b/platformio.ini index 03efb22..f926a91 100644 --- a/platformio.ini +++ b/platformio.ini @@ -55,7 +55,7 @@ lib_deps = m5stack/M5Stack@^0.4.5 ; M5 Lib m5stack/M5GFX@^0.1.9 ; M5 Lib pour le LCD m5stack/Module_GRBL_13.2@^0.0.3 ; M5 Lib pour Stepper (GRBL) - arduino-libraries/Servo@^1.2.1 ; Lib pour le Servo Motor + dlloydev/ESP32 ESP32S2 AnalogWrite ; Lib pour le Servo Motor ; example: ; erropix/ESP32 AnalogWrite@0.2 -- 2.47.1 From 9a8a84a59e352beb835e723fd60946dc74134ac6 Mon Sep 17 00:00:00 2001 From: Mathis Date: Fri, 8 Dec 2023 09:45:07 +0100 Subject: [PATCH 09/12] [ServoMotor] - rework servoMotor for adding speed and easing for movement --- include/Program.h | 2 ++ .../src/ServoMotorComponent.cpp | 26 ++++++++++++++++--- .../src/ServoMotorComponent.h | 5 ++++ src/Program.cpp | 25 +++++------------- 4 files changed, 36 insertions(+), 22 deletions(-) diff --git a/include/Program.h b/include/Program.h index b4f30bb..4ceb880 100644 --- a/include/Program.h +++ b/include/Program.h @@ -4,6 +4,7 @@ #include "Arduino.h" #include "DolibarrClient.h" #include +#include "ServoMotorComponent.h" class Program { public: @@ -18,6 +19,7 @@ public: void loop(); private: DolibarrClient *client; + ServoMotorComponent *servo; }; #endif diff --git a/lib/ServoMotorComponent/src/ServoMotorComponent.cpp b/lib/ServoMotorComponent/src/ServoMotorComponent.cpp index fef7531..4a67a25 100644 --- a/lib/ServoMotorComponent/src/ServoMotorComponent.cpp +++ b/lib/ServoMotorComponent/src/ServoMotorComponent.cpp @@ -2,23 +2,25 @@ ServoMotorComponent::ServoMotorComponent(int PIN) { this->PIN = PIN; - this->position = 0; + this->position = 30; + this->speed = 100; + this->easing = 0.5; this->myservo.attach(PIN); } void ServoMotorComponent::goLeft() { - this->myservo.write(this->PIN, 0); + this->myservo.write(this->PIN, 52, this->speed, this->easing); this->position = 0; } void ServoMotorComponent::goRight() { - this->myservo.write(this->PIN, 180); + this->myservo.write(this->PIN, 18, this->speed, this->easing); this->position = 180; } void ServoMotorComponent::goMiddle() { - this->myservo.write(this->PIN, 90); + this->myservo.write(this->PIN, 32, this->speed, this->easing); this->position = 90; } @@ -29,4 +31,20 @@ int ServoMotorComponent::getPosition() { void ServoMotorComponent::setPin(int PIN) { this->PIN = PIN; this->myservo.attach(PIN); + return; +} + +void ServoMotorComponent::setSpeed(int speed) { + this->speed = speed; + return; +} + +void ServoMotorComponent::pause() { + this->myservo.pause(); + return; +} + +void ServoMotorComponent::setEasing(float easing) { + this->easing = easing; + return; } diff --git a/lib/ServoMotorComponent/src/ServoMotorComponent.h b/lib/ServoMotorComponent/src/ServoMotorComponent.h index c5a08c1..3d74c92 100644 --- a/lib/ServoMotorComponent/src/ServoMotorComponent.h +++ b/lib/ServoMotorComponent/src/ServoMotorComponent.h @@ -13,10 +13,15 @@ public: void goMiddle(); int getPosition(); void setPin(int PIN); + void pause(); + void setSpeed(int speed); + void setEasing(float easing); private: int PIN; int position; + int speed; + float easing; Servo myservo; }; diff --git a/src/Program.cpp b/src/Program.cpp index 8454899..3d7ebe7 100644 --- a/src/Program.cpp +++ b/src/Program.cpp @@ -2,28 +2,17 @@ #include "Arduino.h" #include "DolibarrClient.h" -int initialize_wifi(WifiConfig wifi) { - WiFiClass::mode(WIFI_STA); //Optional - WiFi.setSleep(false); - WiFi.begin(wifi.ssid, wifi.password); - Serial.print("Connecting "); - while(WiFiClass::status() != WL_CONNECTED){ - delay(WAITING_WIFI_DELAY); - Serial.print("."); - } - Serial.println("Connected to the WiFi network"); - return 0; -} - Program::Program() { - Serial.begin(MONITOR_SPEED); - struct WifiConfig wifi_c = {WIFI_SSID, WIFI_PASSWORD}; - struct DolibarrConfig dolibarr = {DOLIBARR_URL, DOLIBARR_API_TOKEN}; - initialize_wifi(wifi_c); - this->client = new DolibarrClient(dolibarr); + this->servo = new ServoMotorComponent(2); } void Program::loop() { + // this->servo->goLeft(); + // delay(3000); + this->servo->goMiddle(); + // delay(3000); + // this->servo->goRight(); + // delay(3000); } -- 2.47.1 From dea01d3e8454b113f4124984388c901f83263f0b Mon Sep 17 00:00:00 2001 From: Mathis Date: Fri, 19 Jan 2024 09:48:21 +0100 Subject: [PATCH 10/12] Refacto servoMotor --- .../src/ServoMotorComponent.cpp | 70 ++++++++++--------- .../src/ServoMotorComponent.h | 26 ++++--- 2 files changed, 53 insertions(+), 43 deletions(-) diff --git a/lib/ServoMotorComponent/src/ServoMotorComponent.cpp b/lib/ServoMotorComponent/src/ServoMotorComponent.cpp index 4a67a25..2b9a515 100644 --- a/lib/ServoMotorComponent/src/ServoMotorComponent.cpp +++ b/lib/ServoMotorComponent/src/ServoMotorComponent.cpp @@ -1,50 +1,56 @@ #include "ServoMotorComponent.h" -ServoMotorComponent::ServoMotorComponent(int PIN) { +ServoMotorComponent::ServoMotorComponent(int PIN, unsigned long updatePeriod) { this->PIN = PIN; - this->position = 30; - this->speed = 100; - this->easing = 0.5; this->myservo.attach(PIN); + this->desiredposition = Position::MIDDLE; + this->currentPosition = 0; + this->lastUpTime = millis(); + this->updatePeriod = updatePeriod; } -void ServoMotorComponent::goLeft() { - this->myservo.write(this->PIN, 52, this->speed, this->easing); - this->position = 0; +int ServoMotorComponent::getCurrentPosition() { + return this->myservo.read(this->PIN); } -void ServoMotorComponent::goRight() { - this->myservo.write(this->PIN, 18, this->speed, this->easing); - - this->position = 180; +void ServoMotorComponent::setCurrentPosition() { + this->currentPosition = this->myservo.read(this->PIN); } -void ServoMotorComponent::goMiddle() { - this->myservo.write(this->PIN, 32, this->speed, this->easing); - this->position = 90; +void ServoMotorComponent::setDesiredPosition(Position desiredPosition) { + switch (desiredPosition) { + case Position::LEFT: + this->desiredposition = 52; + break; + case Position::MIDDLE: + this->desiredposition = 32; + break; + case Position::RIGHT: + this->desiredposition = 18; + break; + default: + break; + } } -int ServoMotorComponent::getPosition() { - return this->position; + +void ServoMotorComponent::refresh() { + if (this->desiredposition == this->currentPosition + || millis() - this->lastUpTime <= this->updatePeriod) return; + + if (this->currentPosition > this->desiredposition) { + this->currentPosition--; + } + if (this->currentPosition < this->desiredposition) { + this->currentPosition++; + } + this->lastUpTime = millis(); + this->myservo.write(this->PIN, this->currentPosition); } + void ServoMotorComponent::setPin(int PIN) { this->PIN = PIN; this->myservo.attach(PIN); return; -} - -void ServoMotorComponent::setSpeed(int speed) { - this->speed = speed; - return; -} - -void ServoMotorComponent::pause() { - this->myservo.pause(); - return; -} - -void ServoMotorComponent::setEasing(float easing) { - this->easing = easing; - return; -} +} \ No newline at end of file diff --git a/lib/ServoMotorComponent/src/ServoMotorComponent.h b/lib/ServoMotorComponent/src/ServoMotorComponent.h index 3d74c92..4ea850d 100644 --- a/lib/ServoMotorComponent/src/ServoMotorComponent.h +++ b/lib/ServoMotorComponent/src/ServoMotorComponent.h @@ -3,26 +3,30 @@ #include +enum Position { + LEFT, + MIDDLE, + RIGHT +}; + class ServoMotorComponent { public: - ServoMotorComponent(int PIN); + ServoMotorComponent(int PIN, unsigned long updatePeriod); ~ServoMotorComponent() = default; - void goLeft(); - void goRight(); - void goMiddle(); - int getPosition(); + int getCurrentPosition(); + void setCurrentPosition(); + void setDesiredPosition(Position desiredPosition); void setPin(int PIN); - void pause(); - void setSpeed(int speed); - void setEasing(float easing); + void refresh(); private: int PIN; - int position; - int speed; - float easing; + int currentPosition; + int desiredposition; Servo myservo; + unsigned long lastUpTime; + unsigned long updatePeriod; }; -- 2.47.1 From 15bd7c774186d26c79cce45a371d8bceb7495e52 Mon Sep 17 00:00:00 2001 From: Mathis Date: Fri, 19 Jan 2024 11:35:25 +0100 Subject: [PATCH 11/12] Retour Clement --- .../src/ServoMotorComponent.cpp | 37 ++++++++++--------- .../src/ServoMotorComponent.h | 11 ++---- src/Program.cpp | 25 +++++++++---- 3 files changed, 42 insertions(+), 31 deletions(-) diff --git a/lib/ServoMotorComponent/src/ServoMotorComponent.cpp b/lib/ServoMotorComponent/src/ServoMotorComponent.cpp index 2b9a515..d23dd40 100644 --- a/lib/ServoMotorComponent/src/ServoMotorComponent.cpp +++ b/lib/ServoMotorComponent/src/ServoMotorComponent.cpp @@ -1,22 +1,29 @@ #include "ServoMotorComponent.h" -ServoMotorComponent::ServoMotorComponent(int PIN, unsigned long updatePeriod) { + +///////////////////////////////////////////////////////////////////////////////////// +// Functions for setting up the ServoMotor +///////////////////////////////////////////////////////////////////////////////////// + +/** + * Constructor. + * Prepares the ServoMotor. + */ +ServoMotorComponent::ServoMotorComponent(int PIN, unsigned long updatePeriod, float step) { this->PIN = PIN; this->myservo.attach(PIN); this->desiredposition = Position::MIDDLE; - this->currentPosition = 0; + this->currentPosition = 32; this->lastUpTime = millis(); this->updatePeriod = updatePeriod; + this->step = step; + this->myservo.write(this->PIN, this->currentPosition); } -int ServoMotorComponent::getCurrentPosition() { - return this->myservo.read(this->PIN); -} - -void ServoMotorComponent::setCurrentPosition() { - this->currentPosition = this->myservo.read(this->PIN); -} - +/** + * Set the desired position + * @desiredPosition: Give desired position + */ void ServoMotorComponent::setDesiredPosition(Position desiredPosition) { switch (desiredPosition) { case Position::LEFT: @@ -34,6 +41,9 @@ void ServoMotorComponent::setDesiredPosition(Position desiredPosition) { } +/** + * Write a new servoMotor position when it's necessary + */ void ServoMotorComponent::refresh() { if (this->desiredposition == this->currentPosition || millis() - this->lastUpTime <= this->updatePeriod) return; @@ -46,11 +56,4 @@ void ServoMotorComponent::refresh() { } this->lastUpTime = millis(); this->myservo.write(this->PIN, this->currentPosition); -} - - -void ServoMotorComponent::setPin(int PIN) { - this->PIN = PIN; - this->myservo.attach(PIN); - return; } \ No newline at end of file diff --git a/lib/ServoMotorComponent/src/ServoMotorComponent.h b/lib/ServoMotorComponent/src/ServoMotorComponent.h index 4ea850d..e493743 100644 --- a/lib/ServoMotorComponent/src/ServoMotorComponent.h +++ b/lib/ServoMotorComponent/src/ServoMotorComponent.h @@ -12,21 +12,18 @@ enum Position { class ServoMotorComponent { public: - ServoMotorComponent(int PIN, unsigned long updatePeriod); - ~ServoMotorComponent() = default; - int getCurrentPosition(); - void setCurrentPosition(); + ServoMotorComponent(int PIN, unsigned long updatePeriod, float step = 1); void setDesiredPosition(Position desiredPosition); - void setPin(int PIN); void refresh(); private: int PIN; - int currentPosition; - int desiredposition; + float currentPosition; + float desiredposition; Servo myservo; unsigned long lastUpTime; unsigned long updatePeriod; + float step; }; diff --git a/src/Program.cpp b/src/Program.cpp index 3d7ebe7..8454899 100644 --- a/src/Program.cpp +++ b/src/Program.cpp @@ -2,17 +2,28 @@ #include "Arduino.h" #include "DolibarrClient.h" +int initialize_wifi(WifiConfig wifi) { + WiFiClass::mode(WIFI_STA); //Optional + WiFi.setSleep(false); + WiFi.begin(wifi.ssid, wifi.password); + Serial.print("Connecting "); + while(WiFiClass::status() != WL_CONNECTED){ + delay(WAITING_WIFI_DELAY); + Serial.print("."); + } + Serial.println("Connected to the WiFi network"); + return 0; +} + Program::Program() { - this->servo = new ServoMotorComponent(2); + Serial.begin(MONITOR_SPEED); + struct WifiConfig wifi_c = {WIFI_SSID, WIFI_PASSWORD}; + struct DolibarrConfig dolibarr = {DOLIBARR_URL, DOLIBARR_API_TOKEN}; + initialize_wifi(wifi_c); + this->client = new DolibarrClient(dolibarr); } void Program::loop() { - // this->servo->goLeft(); - // delay(3000); - this->servo->goMiddle(); - // delay(3000); - // this->servo->goRight(); - // delay(3000); } -- 2.47.1 From fe671182cd0470bf65514e1f2a33bad1e1417d09 Mon Sep 17 00:00:00 2001 From: Mathis Date: Fri, 19 Jan 2024 16:37:35 +0100 Subject: [PATCH 12/12] add config.ini --- config.ini | 5 +++++ lib/ServoMotorComponent/src/ServoMotorComponent.cpp | 12 ++++++------ src/Program.cpp | 12 ++++++++---- 3 files changed, 19 insertions(+), 10 deletions(-) diff --git a/config.ini b/config.ini index 89f0c25..5052dee 100644 --- a/config.ini +++ b/config.ini @@ -27,3 +27,8 @@ build_flags = ; nfc addr -D NFC_ADDR=0x28 + ;;; Servo config ;;; + ;;;;;;;;;;;;;;;;;;;;;; + -D RIGHT_POS=18 + -D MIDDLE_POS=32 + -D LEFT_POS=52 diff --git a/lib/ServoMotorComponent/src/ServoMotorComponent.cpp b/lib/ServoMotorComponent/src/ServoMotorComponent.cpp index d23dd40..950e1c7 100644 --- a/lib/ServoMotorComponent/src/ServoMotorComponent.cpp +++ b/lib/ServoMotorComponent/src/ServoMotorComponent.cpp @@ -13,7 +13,7 @@ ServoMotorComponent::ServoMotorComponent(int PIN, unsigned long updatePeriod, f this->PIN = PIN; this->myservo.attach(PIN); this->desiredposition = Position::MIDDLE; - this->currentPosition = 32; + this->currentPosition = MIDDLE_POS; this->lastUpTime = millis(); this->updatePeriod = updatePeriod; this->step = step; @@ -27,13 +27,13 @@ ServoMotorComponent::ServoMotorComponent(int PIN, unsigned long updatePeriod, f void ServoMotorComponent::setDesiredPosition(Position desiredPosition) { switch (desiredPosition) { case Position::LEFT: - this->desiredposition = 52; + this->desiredposition = LEFT_POS; break; case Position::MIDDLE: - this->desiredposition = 32; + this->desiredposition = MIDDLE_POS; break; case Position::RIGHT: - this->desiredposition = 18; + this->desiredposition = RIGHT_POS; break; default: break; @@ -49,10 +49,10 @@ void ServoMotorComponent::refresh() { || millis() - this->lastUpTime <= this->updatePeriod) return; if (this->currentPosition > this->desiredposition) { - this->currentPosition--; + this->currentPosition -= this->step; } if (this->currentPosition < this->desiredposition) { - this->currentPosition++; + this->currentPosition += this->step; } this->lastUpTime = millis(); this->myservo.write(this->PIN, this->currentPosition); diff --git a/src/Program.cpp b/src/Program.cpp index 8454899..0126fe1 100644 --- a/src/Program.cpp +++ b/src/Program.cpp @@ -1,6 +1,7 @@ #include "Program.h" #include "Arduino.h" #include "DolibarrClient.h" +#include "ServoMotorComponent.h" int initialize_wifi(WifiConfig wifi) { WiFiClass::mode(WIFI_STA); //Optional @@ -17,13 +18,16 @@ int initialize_wifi(WifiConfig wifi) { Program::Program() { Serial.begin(MONITOR_SPEED); - struct WifiConfig wifi_c = {WIFI_SSID, WIFI_PASSWORD}; - struct DolibarrConfig dolibarr = {DOLIBARR_URL, DOLIBARR_API_TOKEN}; - initialize_wifi(wifi_c); - this->client = new DolibarrClient(dolibarr); + this->servo = new ServoMotorComponent(2, 1, 0.1); + //struct WifiConfig wifi_c = {WIFI_SSID, WIFI_PASSWORD}; + //struct DolibarrConfig dolibarr = {DOLIBARR_URL, DOLIBARR_API_TOKEN}; + //initialize_wifi(wifi_c); + //this->client = new DolibarrClient(dolibarr); } void Program::loop() { + this->servo->refresh(); + this->servo->setDesiredPosition(Position::LEFT); } -- 2.47.1