feature/servo_motor_component #17
5
.vscode/settings.json
vendored
Normal file
5
.vscode/settings.json
vendored
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
{
|
||||||
|
"files.associations": {
|
||||||
|
"new": "cpp"
|
||||||
|
}
|
||||||
|
}
|
@ -27,3 +27,8 @@ build_flags =
|
|||||||
|
|
||||||
; nfc addr
|
; nfc addr
|
||||||
-D NFC_ADDR=0x28
|
-D NFC_ADDR=0x28
|
||||||
|
;;; Servo config ;;;
|
||||||
|
;;;;;;;;;;;;;;;;;;;;;;
|
||||||
|
-D RIGHT_POS=18
|
||||||
|
-D MIDDLE_POS=32
|
||||||
|
-D LEFT_POS=52
|
||||||
|
@ -4,6 +4,7 @@
|
|||||||
#include "Arduino.h"
|
#include "Arduino.h"
|
||||||
#include "DolibarrClient.h"
|
#include "DolibarrClient.h"
|
||||||
#include <M5Stack.h>
|
#include <M5Stack.h>
|
||||||
|
#include "ServoMotorComponent.h"
|
||||||
|
|
||||||
class Program {
|
class Program {
|
||||||
public:
|
public:
|
||||||
@ -18,6 +19,7 @@ public:
|
|||||||
void loop();
|
void loop();
|
||||||
private:
|
private:
|
||||||
DolibarrClient *client;
|
DolibarrClient *client;
|
||||||
|
ServoMotorComponent *servo;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
59
lib/ServoMotorComponent/src/ServoMotorComponent.cpp
Normal file
59
lib/ServoMotorComponent/src/ServoMotorComponent.cpp
Normal 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);
|
||||||
|
}
|
33
lib/ServoMotorComponent/src/ServoMotorComponent.h
Normal file
33
lib/ServoMotorComponent/src/ServoMotorComponent.h
Normal 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
|
@ -55,6 +55,7 @@ lib_deps =
|
|||||||
m5stack/M5Stack@^0.4.5 ; M5 Lib
|
m5stack/M5Stack@^0.4.5 ; M5 Lib
|
||||||
m5stack/M5GFX@^0.1.9 ; M5 Lib pour le LCD
|
m5stack/M5GFX@^0.1.9 ; M5 Lib pour le LCD
|
||||||
m5stack/Module_GRBL_13.2@^0.0.3 ; M5 Lib pour Stepper (GRBL)
|
m5stack/Module_GRBL_13.2@^0.0.3 ; M5 Lib pour Stepper (GRBL)
|
||||||
|
dlloydev/ESP32 ESP32S2 AnalogWrite ; Lib pour le Servo Motor
|
||||||
; example:
|
; example:
|
||||||
; erropix/ESP32 AnalogWrite@0.2
|
; erropix/ESP32 AnalogWrite@0.2
|
||||||
|
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
#include "Program.h"
|
#include "Program.h"
|
||||||
#include "Arduino.h"
|
#include "Arduino.h"
|
||||||
|
|||||||
#include "DolibarrClient.h"
|
#include "DolibarrClient.h"
|
||||||
|
#include "ServoMotorComponent.h"
|
||||||
|
|
||||||
int initialize_wifi(WifiConfig wifi) {
|
int initialize_wifi(WifiConfig wifi) {
|
||||||
WiFiClass::mode(WIFI_STA); //Optional
|
WiFiClass::mode(WIFI_STA); //Optional
|
||||||
@ -17,13 +18,16 @@ int initialize_wifi(WifiConfig wifi) {
|
|||||||
|
|
||||||
Program::Program() {
|
Program::Program() {
|
||||||
Serial.begin(MONITOR_SPEED);
|
Serial.begin(MONITOR_SPEED);
|
||||||
struct WifiConfig wifi_c = {WIFI_SSID, WIFI_PASSWORD};
|
this->servo = new ServoMotorComponent(2, 1, 0.1);
|
||||||
struct DolibarrConfig dolibarr = {DOLIBARR_URL, DOLIBARR_API_TOKEN};
|
//struct WifiConfig wifi_c = {WIFI_SSID, WIFI_PASSWORD};
|
||||||
initialize_wifi(wifi_c);
|
//struct DolibarrConfig dolibarr = {DOLIBARR_URL, DOLIBARR_API_TOKEN};
|
||||||
this->client = new DolibarrClient(dolibarr);
|
//initialize_wifi(wifi_c);
|
||||||
|
//this->client = new DolibarrClient(dolibarr);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Program::loop() {
|
void Program::loop() {
|
||||||
|
this->servo->refresh();
|
||||||
|
this->servo->setDesiredPosition(Position::LEFT);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user
vire tes changement dans Program.CPP avant de push