Merge pull request 'feature/servo_motor_component' (#17) from feature/servo_motor_component into develop

Reviewed-on: #17
Reviewed-by: Nicolas <nicolas.sansd@gmail.com>
This commit is contained in:
Mathis 2024-01-19 16:46:00 +01:00
commit c498489cbb
7 changed files with 113 additions and 4 deletions

5
.vscode/settings.json vendored Normal file
View File

@ -0,0 +1,5 @@
{
"files.associations": {
"new": "cpp"
}
}

View File

@ -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

View File

@ -4,6 +4,7 @@
#include "Arduino.h"
#include "DolibarrClient.h"
#include <M5Stack.h>
#include "ServoMotorComponent.h"
class Program {
public:
@ -18,6 +19,7 @@ public:
void loop();
private:
DolibarrClient *client;
ServoMotorComponent *servo;
};
#endif

View File

@ -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);
}

View File

@ -0,0 +1,33 @@
#ifndef SERVOMOTOT_COMPONENT_H
#define SERVOMOTOT_COMPONENT_H
#include <Servo.h>
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

View File

@ -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

View File

@ -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);
}