Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[pull] mega from letscontrolit:mega #129

Merged
merged 4 commits into from
Sep 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 83 additions & 0 deletions lib/ServoESP32/.clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
# Google C/C++ Code Style settings
# https://clang.llvm.org/docs/ClangFormatStyleOptions.html
# Author: Kehan Xue, kehan.xue (at) gmail.com

Language: Cpp
BasedOnStyle: Google
AccessModifierOffset: -1
AlignAfterOpenBracket: Align
AlignConsecutiveAssignments: None
AlignOperands: Align
AllowAllArgumentsOnNextLine: true
AllowAllConstructorInitializersOnNextLine: true
AllowAllParametersOfDeclarationOnNextLine: false
AllowShortBlocksOnASingleLine: Empty
AllowShortCaseLabelsOnASingleLine: false
AllowShortFunctionsOnASingleLine: Inline
AllowShortIfStatementsOnASingleLine: Never # To avoid conflict, set this "Never" and each "if statement" should include brace when coding
AllowShortLambdasOnASingleLine: Inline
AllowShortLoopsOnASingleLine: false
AlwaysBreakAfterReturnType: None
AlwaysBreakTemplateDeclarations: Yes
BinPackArguments: true
BreakBeforeBraces: Custom
BraceWrapping:
AfterCaseLabel: false
AfterClass: false
AfterStruct: false
AfterControlStatement: Never
AfterEnum: false
AfterFunction: false
AfterNamespace: false
AfterUnion: false
AfterExternBlock: false
BeforeCatch: false
BeforeElse: false
BeforeLambdaBody: false
IndentBraces: false
SplitEmptyFunction: false
SplitEmptyRecord: false
SplitEmptyNamespace: false
BreakBeforeBinaryOperators: None
BreakBeforeTernaryOperators: true
BreakConstructorInitializers: BeforeColon
BreakInheritanceList: BeforeColon
ColumnLimit: 120
CompactNamespaces: false
ContinuationIndentWidth: 4
Cpp11BracedListStyle: true
DerivePointerAlignment: false # Make sure the * or & align on the left
EmptyLineBeforeAccessModifier: LogicalBlock
FixNamespaceComments: true
IncludeBlocks: Preserve
IndentCaseLabels: true
IndentPPDirectives: None
IndentWidth: 4
KeepEmptyLinesAtTheStartOfBlocks: true
MaxEmptyLinesToKeep: 1
NamespaceIndentation: None
ObjCSpaceAfterProperty: false
ObjCSpaceBeforeProtocolList: true
PointerAlignment: Left
ReflowComments: false
# SeparateDefinitionBlocks: Always # Only support since clang-format 14
SpaceAfterCStyleCast: false
SpaceAfterLogicalNot: false
SpaceAfterTemplateKeyword: true
SpaceBeforeAssignmentOperators: true
SpaceBeforeCpp11BracedList: false
SpaceBeforeCtorInitializerColon: true
SpaceBeforeInheritanceColon: true
SpaceBeforeParens: ControlStatements
SpaceBeforeRangeBasedForLoopColon: true
SpaceBeforeSquareBrackets: false
SpaceInEmptyParentheses: false
SpacesBeforeTrailingComments: 2
SpacesInAngles: false
SpacesInCStyleCastParentheses: false
SpacesInContainerLiterals: false
SpacesInParentheses: false
SpacesInSquareBrackets: false
Standard: c++11
TabWidth: 4
UseTab: Never
38 changes: 32 additions & 6 deletions lib/ServoESP32/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,49 @@ Base on [servo library for stm32f4 (d2a4a47)](https://github.com/arduino-librari

The interface is similar to Arduino/Servo: https://www.arduino.cc/en/Reference/Servo

But the function `atach()` is different:
But the function `attach()` is different:

```c
bool attach(
int pin,
int channel = CHANNEL_NOT_ATTACHED,
int minAngle = MIN_ANGLE,
int maxAngle = MAX_ANGLE,
int minPulseWidth = MIN_PULSE_WIDTH,
int maxPulseWidth = MAX_PULSE_WIDTH
int minAngle = DEFAULT_MIN_ANGLE,
int maxAngle = DEFAULT_MAX_ANGLE,
int minPulseWidthUs = DEFAULT_MIN_PULSE_WIDTH_US,
int maxPulseWidthUs = DEFAULT_MAX_PULSE_WIDTH_US,
int frequency = DEFAULT_FREQUENCY
);
```

More information in [source code documentation](https://github.com/RoboticsBrno/ESP32-Arduino-Servo-Library/blob/master/src/Servo.h#L73).
More information in [source code documentation](src/Servo.h).

Example: [04-SimpleServoAngles](examples/04-SimpleServoAngles/04-SimpleServoAngles.ino)

There are also a ServoFloat and ServoDouble variant available. Use one of these when working in radians.

Example: : [05-SimpleServoRadians](examples/05-SimpleServoRadians/05-SimpleServoRadians.ino)

### IMPORTANT INFO
According testings, the frequency for ESP32 S2/S3/C3 has to be set at least to 200 Hz. Here is an example, how to set just frequency:

```cpp
Servo servo1;
const int servoPin = 4;
const int frequency = 200; // Hz

servo1.attach(
servoPin,
Servo::CHANNEL_NOT_ATTACHED,
Servo::DEFAULT_MIN_ANGLE,
Servo::DEFAULT_MAX_ANGLE,
Servo::DEFAULT_MIN_PULSE_WIDTH_US,
Servo::DEFAULT_MAX_PULSE_WIDTH_US,
frequency
);
```

For more information look at the [PR25](https://github.com/RoboticsBrno/ServoESP32/pull/25)

## PlatformIO

This library is also available at the [PlatformIO](https://platformio.org) as [ServoESP32](https://platformio.org/lib/show/1739/ServoESP32).
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#include <Servo.h>

/*
* Description:
* Example for using floating point angle and radians.
*/

static const int servoPin = 4;

ServoFloat servo1;

float deg2rad(float in) {
return in * M_PI / 180.0;
}

const float minAngle = deg2rad(45.0);
const float maxAngle = deg2rad(120.0);
const float stepAngle = deg2rad(1.0);

void setup() {
Serial.begin(115200);
servo1.attach(servoPin, Servo::CHANNEL_NOT_ATTACHED, minAngle, maxAngle);
}

void loop() {
for (float angleRadians = minAngle; angleRadians <= maxAngle; angleRadians += stepAngle) {
servo1.write(angleRadians);
Serial.println(angleRadians);
delay(20);
}

for (float angleRadians = maxAngle; angleRadians >= minAngle; angleRadians -= stepAngle) {
servo1.write(angleRadians);
Serial.println(angleRadians);
delay(20);
}
}
18 changes: 8 additions & 10 deletions lib/ServoESP32/library.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,17 @@
"description": "Generate RC servo signal on a selected pins with ESP32 device and Arduino framework.",
"homepage": "https://github.com/RoboticsBrno/ServoESP32/",
"license": "MIT",
"repository":
{
"repository": {
"type": "git",
"url": "https://github.com/RoboticsBrno/ServoESP32.git"
},
"authors":
{
"name": "Jaroslav Paral",
"email": "[email protected]",
"url": "http://www.robotikabrno.cz",
"maintainer": true
"authors": {
"name": "Jaroslav Paral",
"email": "[email protected]",
"url": "http://www.robotikabrno.cz",
"maintainer": true
},
"version": "1.0.3",
"version": "1.1.1",
"frameworks": "arduino",
"platforms": "espressif32"
}
}
2 changes: 1 addition & 1 deletion lib/ServoESP32/library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=ServoESP32
version=1.0.3
version=1.1.1
author=Jaroslav Paral
maintainer=Jaroslav Paral <[email protected]>
sentence=Generate RC servo signal on a selected pins with ESP32 device and Arduino framework.
Expand Down
92 changes: 3 additions & 89 deletions lib/ServoESP32/src/Servo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,99 +24,13 @@
* SOFTWARE.
*****************************************************************************/

/*
/**
* Arduino srl - www.arduino.org
* Base on lib for stm32f4 (d2a4a47): https://github.com/arduino-libraries/Servo/blob/master/src/stm32f4/ServoTimers.h
* 2017 Jul 5: Edited by Jaroslav Páral (jarekparal) - [email protected]
*/

// Implementation is in Servo.h
#include <Servo.h>

int Servo::channel_next_free = 0;

Servo::Servo() {
_resetFields();
};

Servo::~Servo() {
detach();
}

bool Servo::attach(int pin, int channel,
int minAngle, int maxAngle,
int minPulseWidth, int maxPulseWidth)
{
if(channel == CHANNEL_NOT_ATTACHED) {
if(channel_next_free == CHANNEL_MAX_NUM) {
return false;
}
_channel = channel_next_free;
channel_next_free++;
} else {
_channel = channel;
}

_pin = pin;
_minAngle = minAngle;
_maxAngle = maxAngle;
_minPulseWidth = minPulseWidth;
_maxPulseWidth = maxPulseWidth;

ledcSetup(_channel, 50, 16); // channel X, 50 Hz, 16-bit depth
ledcAttachPin(_pin, _channel);
return true;
}


bool Servo::detach() {
if (!this->attached()) {
return false;
}

if(_channel == (channel_next_free - 1))
channel_next_free--;

ledcDetachPin(_pin);
_pin = PIN_NOT_ATTACHED;
return true;
}

void Servo::write(int degrees) {
degrees = constrain(degrees, _minAngle, _maxAngle);
writeMicroseconds(_angleToUs(degrees));
}

void Servo::writeMicroseconds(int pulseUs) {
if (!attached()) {
return;
}
pulseUs = constrain(pulseUs, _minPulseWidth, _maxPulseWidth);
_pulseWidthDuty = _usToDuty(pulseUs);
ledcWrite(_channel, _pulseWidthDuty);
}

int Servo::read() {
return _usToAngle(readMicroseconds());
}

int Servo::readMicroseconds() {
if (!this->attached()) {
return 0;
}
int duty = ledcRead(_channel);
return _dutyToUs(duty);
}

bool Servo::attached() const { return _pin != PIN_NOT_ATTACHED; }

int Servo::attachedPin() const { return _pin; }

void Servo::_resetFields(void) {
_pin = PIN_NOT_ATTACHED;
_pulseWidthDuty = 0;
_channel = CHANNEL_NOT_ATTACHED;
_minAngle = MIN_ANGLE;
_maxAngle = MAX_ANGLE;
_minPulseWidth = MIN_PULSE_WIDTH;
_maxPulseWidth = MAX_PULSE_WIDTH;
}
int ServoBase::channel_next_free = 0;
Loading