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/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/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 new file mode 100644 index 0000000..950e1c7 --- /dev/null +++ b/lib/ServoMotorComponent/src/ServoMotorComponent.cpp @@ -0,0 +1,59 @@ +#include "ServoMotorComponent.h" + + +///////////////////////////////////////////////////////////////////////////////////// +// 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 = MIDDLE_POS; + this->lastUpTime = millis(); + this->updatePeriod = updatePeriod; + this->step = step; + this->myservo.write(this->PIN, this->currentPosition); +} + +/** + * Set the desired position + * @desiredPosition: Give desired position + */ +void ServoMotorComponent::setDesiredPosition(Position desiredPosition) { + switch (desiredPosition) { + case Position::LEFT: + this->desiredposition = LEFT_POS; + break; + case Position::MIDDLE: + this->desiredposition = MIDDLE_POS; + break; + case Position::RIGHT: + this->desiredposition = RIGHT_POS; + break; + default: + break; + } +} + + +/** + * Write a new servoMotor position when it's necessary + */ +void ServoMotorComponent::refresh() { + if (this->desiredposition == this->currentPosition + || millis() - this->lastUpTime <= this->updatePeriod) return; + + if (this->currentPosition > this->desiredposition) { + this->currentPosition -= this->step; + } + if (this->currentPosition < this->desiredposition) { + this->currentPosition += this->step; + } + this->lastUpTime = millis(); + this->myservo.write(this->PIN, this->currentPosition); +} \ No newline at end of file diff --git a/lib/ServoMotorComponent/src/ServoMotorComponent.h b/lib/ServoMotorComponent/src/ServoMotorComponent.h new file mode 100644 index 0000000..e493743 --- /dev/null +++ b/lib/ServoMotorComponent/src/ServoMotorComponent.h @@ -0,0 +1,33 @@ +#ifndef SERVOMOTOT_COMPONENT_H +#define SERVOMOTOT_COMPONENT_H + +#include + +enum Position { + LEFT, + MIDDLE, + RIGHT +}; + +class ServoMotorComponent +{ +public: + ServoMotorComponent(int PIN, unsigned long updatePeriod, float step = 1); + void setDesiredPosition(Position desiredPosition); + void refresh(); + +private: + int PIN; + float currentPosition; + float desiredposition; + Servo myservo; + unsigned long lastUpTime; + unsigned long updatePeriod; + float step; +}; + + + + + +#endif //SERVOMOTOT_COMPONENT_H diff --git a/platformio.ini b/platformio.ini index 590c0d3..f926a91 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) + dlloydev/ESP32 ESP32S2 AnalogWrite ; Lib pour le Servo Motor ; example: ; erropix/ESP32 AnalogWrite@0.2 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); }