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

add setVelocity method and explanation for ESP32 support #19

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
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
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
# Explanation

The library is forked from [L298n](https://github.com/AndreaLombardo/L298N).
The original library is designed for Arduino board, using `analogWrite`, which may cause some problem on ESP32.
Using [ESP32_AnalogWrite](https://github.com/erropix/ESP32_AnalogWrite) or [AnalogWrite_ESP32](https://github.com/pablomarquez76/AnalogWrite_ESP32) can overwrite `ledcWrite` of ESP32 to `analogWrite`.
As [pablomarquez76](https://github.com/pablomarquez76/AnalogWrite_ESP32/issues/6) saind, ESP32 has supported `analogWrite` after 2.0.11. The official document is [at here](https://docs.espressif.com/projects/arduino-esp32/en/latest/api/ledc.html?highlight=analogWrite#analogwrite).The resolution is form 0 to 255, testing normal. So nothing needs change.

I added a method to get input from -255 to 255 to control the motor. It is for easier direction control.

I tested it with PID library. Sometimes PID always output 0. Sometimes it works. I didn't find the reason. It might because ESP32Encoder or this repo use the same timer with PID. I am not sure. Be careful.

The below is original readme.

# L298N Library

An easy to use L298N library to control DC Motors with Arduino.
Expand Down
38 changes: 38 additions & 0 deletions src/L298N.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,44 @@ void L298N::reset() {
_canMove = true;
}

// the velocity is a number between -255 and 255
// positive values are for forward movements
// negative values are for backward movements
void L298N::setVelocity(int velocity){
if (velocity > 255) {
velocity = 255;
} else if (velocity < -255) {
velocity = -255;
}

if (velocity > 0) {
this->setSpeed((unsigned short)velocity);
this->forward();
} else if (velocity < 0) {
this->setSpeed((unsigned short)(-velocity));
this->backward();
} else {
this->stop();
}
}

// the acceleration can be positive or negative
// negative values does not mean deceleration
// it means the direction of the acceleration is negative
void L298N::accelerate(int acceleration) {
int velocity = this->getSpeed();
if (_direction == BACKWARD) {
velocity = -velocity;
}
velocity += acceleration;
if (velocity > 255) {
velocity = 255;
} else if (velocity < -255) {
velocity = -255;
}
this->setVelocity(velocity);
}

boolean L298N::isMoving() {
return _isMoving;
}
Expand Down
2 changes: 2 additions & 0 deletions src/L298N.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ class L298N
void runFor(unsigned long delay, L298N::Direction direction, CallBackFunction callback);
void stop();
void reset();
void setVelocity(int velocity);
void accelerate(int acceleration);
boolean isMoving();
Direction getDirection();

Expand Down