`res` задаёт диапазон значений библиотеки, но не настраивает аппаратное разрешение `analogWrite`. Его нужно настроить средствами платформы отдельно. На стандартном Arduino AVR используется 8-битный `analogWrite`, поэтому следует оставить `res = 8`.
### Описание фич
#### Запуск и остановка
-`runSpeed` запускает мотор с указанной скоростью, поддерживает отрицательные значения для вращения в обратную сторону. При значении `0` отключает мотор
-The pins in parentheses are in the order of the constructor, i.e.for example, for`DIR_PWM` the initialization will be `GyverMotor2<GM2::DIR_PWM> motor(gpio, pwm)`, where gpio is a regular digital pin (digitalWrite), pwm is a pin with PWM support (analogWrite)
-`MAX`- maximum PWM value at the specified resolution
-`PWM`- direct PWM `(0.. MAX)`
-`~PWM`- reverse PWM `(MAX.. 0)`
-In brackets, pins are indicated in the order of the designer, i.e.for example, for`DIR_PWM`initialization will`GyverMotor2<GM2::DIR_PWM> motor(gpio, pwm)`where gpio is a regular digital pin (digitalWrite), pwm is a PWM-enabled pin (analogWrite)
-`MAX`- maximum PWM value at the specified permit
-`PWM`- straight CHIM`(0.. MAX)`
-`~PWM`- reverse CHIM`(MAX.. 0)`
Mode selection for H-bridge driver (2 pins, usually IN1 and IN2) - most driver modules for Arduino:
Mode selection for the H-bridge driver (2 pins, usually IN1 and IN2) - most of the driver modules for the Arduino:
- For symmetrical speed operation in both directions, a dual PWM mode is recommended, where`PWM_PWM_SPEED`has a higher speed, and`PWM_PWM_POWER` is more stable at low speeds and has a higher torque on some motors and drivers.Ideal for wheeled robots
- To work in both directions, when the symmetry of the motor operation is not important, you can use the `DIR_PWM_INV` mode - it saves one PWM pin.Drivers that work with`DIR_PWM`(without inversion) are rare
- For one-way operation with speed control (MOSFET or bridge with a closedpin), the `ONLY_PWM` mode is suitableand uses only one pin
- For relay control (without speed control) you can use the `DIR_DIR` mode
- For symmetrical speed of operation in both directions, a mode with two PWM is recommended, where`PWM_PWM_SPEED`has a higher speed, and`PWM_PWM_POWER`It is more stable at low revs and has higher torque on some motors and drivers. Perfect for wheeled robots
- To work in both directions, when the symmetry of the engine is not important, you can use the mode.`DIR_PWM_INV`- he saves one PIN. Drivers who work with`DIR_PWM`(without inversion) are rare.
- For one-way operation with speed control (MOSFET or closed-pin bridge) the mode is suitable`ONLY_PWM`and uses only one pin.
- For relay control (without speed control), you can use the mode`DIR_DIR`
For drivers with separate direction controlI eat cranberries and the speed IN1+IN2+EN needs the `DIR_DIR_PWM` mode.
Drivers with separate direction control and speed IN1+IN2+EN need a mode`DIR_DIR_PWM`.
// set deadtime (delay when changing direction) in microseconds (default 0)
// set deadtime (delay in changing direction) in microseconds (default 0)
voidsetDeadtime(uint8_tus);
// get deadtime in microseconds
// Get deadtime in microseconds
uint8_tgetDeadTime();
// set direction reverse (default false)
// set the reverse direction (silent. false)
voidsetReverse(boolrev);
// get direction reverse
// reverse
boolgetReverse();
// set minimum PWM (default 0)
// set the minimum PWM (failed 0)
voidsetMinDuty(uint16_tduty);
// set the minimum PWM as a % of the maximum (default 0)
// set the minimum PWM in % of the maximum (failed 0)
voidsetMinDutyPerc(uint8_tperc);
// get minimum PWM
// get the minimum PWM
uint16_tgetMinDuty();
// get maximum PWM
// maximise
uint16_tgetMaxDuty();
// set PWM speed (-max.. max)
// set the PWM speed (-max. max. max.
voidrunSpeed(int16_tspeed);
// set the speed as a % of the maximum PWM (-100.. 100%)
// set the speed in % of the maximum PWM (-100). 100%
voidrunSpeedPerc(int8_tperc);
// get the current motor speed in PWM
// Get the current speed of the motor in PIM
int16_tgetSpeed();
// get direction: motor is spinning (1 and -1), motor is stopped (0)
// receive direction: motor turns (1 and -1), motor stops (0)
int8_tgetDir();
// stop.If acceleration is enabled, then smooth
// stop.If the acceleration is activated, the
voidstop();
// active braking (keeps driver pins active)
// Active braking (leaving driver pins active)
voidbrake();
// stop and turn off the motor
voiddisable();
// set acceleration in PWM per second.The real minimum is 5.0 to disable
// set the acceleration in PWM per second. Real minimum - 5.0 to turn off
voidsetAccel(uint16_taccel);
// set the acceleration in percent per second.0 to disable
// set the acceleration as a percentage per second.0 to turn off
voidsetAccelPerc(uint8_tperc);
// get the period at least which tick should be called, ms
// get a period at least less than which you need to call a tick, ms
uint8_tgetDt();
// get target speed (acceleration mode)
// Get the target speed (acceleration mode)
int16_tgetTarget();
// smooth change to the specified speed with setAccel acceleration, call in loop.Returns true when speed is reached
// smooth change to the specified speed with acceleration setAccel, call in loop.Return true when you reach speed
booltick();
```
### Description of features
#### Start and stop
-`runSpeed` starts the motor at the specified speed, supports negative values for rotation in the opposite direction.When set to `0`, turns off the motor
-`stop` - stop and shutdown, equivalent to calling `runSpeed(0)`
-`setReverse` - motor reverse, when set to `true` it will rotate in the opposite direction
-`brake`- active braking via the driver (not supported everywhere)
-`disable` - stop and disable the driver
`res` sets the value range used by the library, but does not configure the hardware resolution of `analogWrite`. Configure it separately with the platform API. Standard Arduino AVR uses 8-bit `analogWrite`, so keep `res = 8`.
### Description of the fit
#### Launch and stop
-`runSpeed`starts the motor at the specified speed, maintains negative values for rotation in the opposite direction. meaning`0`switch off
-`stop`- stop and disconnect, equivalent to a call`runSpeed(0)`
-`setReverse`- engine reverse, when installed`true`It will turn in the opposite direction.
-`brake`Active braking through the driver (not supported everywhere)
-`disable`Stop and disable the driver
#### DeadTime
Inside, `runSpeed`, when changing the direction of rotation, turns off the driver and creates a delay for the specified number of microseconds, after which it sends the required signal to the motor.This is necessary for homemade simple H-bridge drivers to avoid shorting the bridge.Drivers in the form of chips and modules usually have built-in hardware deadtime and do not need to be enabled in the library.
Inside.`runSpeed`When changing the direction of rotation, it turns off the driver and creates a delay for the specified number of microseconds, after which it gives the desired signal to the motor.This is necessary for homemade simple H-bridge drivers to avoid closing the bridge.Drivers in the form of chips and modules usually have built-in hardware deadtime and do not need to include it in the library.
#### MinDuty
Allows you to set the minimum signal to the motor.The value supplied to `runSpeed` will scale linearly from the specified MinDuty to the maximum (if not zero).This allows you to “skip” the values at which the motor cannot move without changing the logic of the speed setting.Example values for 8 bits and`MinDuty=50`:
Allows you to adjust the minimum signal to the motor.The value submitted in`runSpeed`It will scale linearly from MinDuty to maximum (if not zero).This allows you to "skip" the values at which the motor cannot move without changing the logic of the speed setting.Example of values for 8 bits and`MinDuty=50`:
|`runSpeed`|Actual PWM |
|------------|-----------------|
|0 |0 |
|1 |50 |
|50 |90 |
|150 |170 |
|200 |210 |
|255 |255 |
|`runSpeed`|Actual PWM |
|-----------|----------------|
|0 |0 |
|1 |50 |
|50 |90 |
|150 |170 |
|200 |210 |
|255 |255 |
#### Accel
When specifying a non-zero acceleration (in units of PWM per second), the `runSpeed` call will not apply the specified speed immediately, but will instead ramp smoothly towards the specified speed.To work, you need to call the ticker in the main program loop no less often than`getDt`milliseconds (varies from 30 to 200 depending on the acceleration).Calling`stop`will make a smooth stop, while `brake`and`disable` will stop the motor abruptly:
When setting non-zero acceleration (in PWM units per second) call`runSpeed`does not apply the specified speed immediately, instead, the speed will change smoothly to the specified.To work, you need to call a ticker in the main cycle of the program at least less often than`getDt`milliseconds (ranging from 30 to 200 depending on acceleration) A challenge`stop`will make a smooth stop, and`brake`and`disable`Stop the engine abruptly:
```cpp
voidsetup(){
motor.setAccel(80);
motor.runSpeed(170);
motor.setAccel(80);
motor.runSpeed(170);
}
voidloop(){
motor.tick();// smoothly accelerates the motor to speed 170
motor.tick();// smoothly accelerate the motor to a speed of 170
}
```
Might come in handyis used when controlling a heavy wheeled robot to avoid jerks during acceleration and braking.
It can be useful when driving a heavy wheeled robot to avoid jerks during acceleration and braking.
- The library can be found by the name **GyverMotor2** and installed through the library manager in:
- Arduino IDE
- Arduino IDE v2
- PlatformIO
- [Download library](https://github.com/GyverLibs/GyverMotor2/archive/refs/heads/main.zip).zip archive for manual installation:
- Unpack and put in *C:\Program Files (x86)\Arduino\libraries* (Windows x64)
- Unpack and put in *C:\Program Files\Arduino\libraries* (Windows x32)
- Unpack and put in *Documents/Arduino/libraries/*
- (Arduino IDE) automatic installation from .zip: *Sketch/Connect library/Add .ZIP library…* and indicate the downloaded archive
- The library can be found under the name **GyverMotor2** and installed through the library manager in:
- Arduino IDE
- Arduino IDE v2
- PlatformIO
- [Download the library](https://github.com/GyverLibs/GyverMotor2/archive/refs/heads/main.zip).zip archive for manual installation:
- Unpack and put in *C:\Program Files (x86)\Arduino\libraries* (Windows x64)
- Unpack and put in *C:\Program Files\Arduino\libraries* (Windows x32)
- Unpack and put in *Documents/Arduino/libraries/*
- (Arduino IDE) Automatic installation from .zip: *Sketch/Connect library/Add .ZIP library...* and specify downloaded archive
- Read more detailed instructions for installing libraries[here](https://alexgyver.ru/arduino-first/#%D0%A3%D1%81%D1%82%D0%B0%D0%BD%D0%BE%D0%B2%D0%BA%D0%B0_%D0%B1%D0%B8%D0%B1%D0%BB%D0%B8%D0%BE%D1%82%D0%B5%D0%BA)
### Update
- I recommend always updating the library: in new versions errors and bugs are corrected, as well as optimization is carried out and new features are added
- Through the IDE library manager: find the library as during installation and click "Update"
- Manually: **delete the folder with the old version**, and then put the new one in its place.“Replacement” cannot be done: sometimes new versions delete files that will remain after replacement and can lead to errors!
- I recommend always updating the library: new versions fix errors and bugs, as well as optimize and add new features.
- Through the library manager IDE: find the library as when installing and click "Update"
- Manually: **Delete the folder with the old version** and then put the new one in its place.“Replacement” cannot be done: sometimes new versions delete files that will remain when replaced and can lead to errors!
<a id="feedback"></a>
## Bugs and feedback
When you find bugs, create an **Issue**, or better yet, immediately write to [alex@alexgyver.ru](mailto:alex@alexgyver.ru)
The library is open for improvement and your **Pull Requests**!
If you find bugs, create **Issue**, or better write to the mail immediately.[alex@alexgyver.ru](mailto:alex@alexgyver.ru)
The library is open for revision and your **Pull Requests*!
When reporting bugs or incorrect operation of the library, be sure to indicate:
When reporting bugs or incorrect work of the library, it is necessary to specify:
- Library version
- Which MK is used?
- What is used by the IC
- SDK version (for ESP)
- Arduino IDE version
-Do the built-in examples that use functions and constructs that lead to a bug in your code work correctly?
- What code was loaded, what work was expected from it and how it works in reality
- Ideally, attach the minimum code in which the bug is observed.Not a canvas of a thousand lines, but minimal code
-Are embedded examples that use features and designs that cause bugs in your code working correctly?
- What code was downloaded, what work was expected from it and how it works in reality
- Ideally, attach the minimum code in which the bug is observed.Not a canvas of a thousand lines, but a minimum code.
Blocking a user prevents them from interacting with repositories, such as opening or commenting on pull requests or issues. Learn more about blocking a user.