Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
L298LIB.cpp@0:dd3d9657fd9f, 2022-06-04 (annotated)
- Committer:
- kennethdhdl
- Date:
- Sat Jun 04 05:41:18 2022 +0000
- Revision:
- 0:dd3d9657fd9f
L298N motor driver library
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| kennethdhdl | 0:dd3d9657fd9f | 1 | #include "L298LIB.h" |
| kennethdhdl | 0:dd3d9657fd9f | 2 | L298LIB::L298LIB(PinName pinEnable, PinName pinIN1, PinName pinIN2, PinName pinIN3, PinName pinIN4): |
| kennethdhdl | 0:dd3d9657fd9f | 3 | _pwm(pinEnable), |
| kennethdhdl | 0:dd3d9657fd9f | 4 | _pinIN1 (pinIN1), |
| kennethdhdl | 0:dd3d9657fd9f | 5 | _pinIN2 (pinIN2), |
| kennethdhdl | 0:dd3d9657fd9f | 6 | _pinIN3 (pinIN3), |
| kennethdhdl | 0:dd3d9657fd9f | 7 | _pinIN4 (pinIN4) |
| kennethdhdl | 0:dd3d9657fd9f | 8 | { |
| kennethdhdl | 0:dd3d9657fd9f | 9 | _pwm.period_us(25); // 40kHz |
| kennethdhdl | 0:dd3d9657fd9f | 10 | _pwm = 0.; |
| kennethdhdl | 0:dd3d9657fd9f | 11 | _pinIN1 = 0; |
| kennethdhdl | 0:dd3d9657fd9f | 12 | _pinIN2 = 0; |
| kennethdhdl | 0:dd3d9657fd9f | 13 | _pinIN3 = 0; |
| kennethdhdl | 0:dd3d9657fd9f | 14 | _pinIN4 = 0; |
| kennethdhdl | 0:dd3d9657fd9f | 15 | } |
| kennethdhdl | 0:dd3d9657fd9f | 16 | void L298LIB::setSpeed(float pwmVal) |
| kennethdhdl | 0:dd3d9657fd9f | 17 | { |
| kennethdhdl | 0:dd3d9657fd9f | 18 | _pwm = pwmVal; |
| kennethdhdl | 0:dd3d9657fd9f | 19 | } |
| kennethdhdl | 0:dd3d9657fd9f | 20 | float L298LIB::getSpeed() |
| kennethdhdl | 0:dd3d9657fd9f | 21 | { |
| kennethdhdl | 0:dd3d9657fd9f | 22 | return _pwm; |
| kennethdhdl | 0:dd3d9657fd9f | 23 | } |
| kennethdhdl | 0:dd3d9657fd9f | 24 | void L298LIB::forward() |
| kennethdhdl | 0:dd3d9657fd9f | 25 | { |
| kennethdhdl | 0:dd3d9657fd9f | 26 | _pinIN1=1; |
| kennethdhdl | 0:dd3d9657fd9f | 27 | _pinIN2=0; |
| kennethdhdl | 0:dd3d9657fd9f | 28 | _pinIN3=1; |
| kennethdhdl | 0:dd3d9657fd9f | 29 | _pinIN4=0; |
| kennethdhdl | 0:dd3d9657fd9f | 30 | } |
| kennethdhdl | 0:dd3d9657fd9f | 31 | void L298LIB::backward() |
| kennethdhdl | 0:dd3d9657fd9f | 32 | { |
| kennethdhdl | 0:dd3d9657fd9f | 33 | _pinIN1=0; |
| kennethdhdl | 0:dd3d9657fd9f | 34 | _pinIN2=1; |
| kennethdhdl | 0:dd3d9657fd9f | 35 | _pinIN3=0; |
| kennethdhdl | 0:dd3d9657fd9f | 36 | _pinIN4=1; |
| kennethdhdl | 0:dd3d9657fd9f | 37 | } |
| kennethdhdl | 0:dd3d9657fd9f | 38 | void L298LIB::stop() |
| kennethdhdl | 0:dd3d9657fd9f | 39 | { |
| kennethdhdl | 0:dd3d9657fd9f | 40 | _pinIN1=0; |
| kennethdhdl | 0:dd3d9657fd9f | 41 | _pinIN2=0; |
| kennethdhdl | 0:dd3d9657fd9f | 42 | _pinIN3=0; |
| kennethdhdl | 0:dd3d9657fd9f | 43 | _pinIN4=0; |
| kennethdhdl | 0:dd3d9657fd9f | 44 | } |
| kennethdhdl | 0:dd3d9657fd9f | 45 | void L298LIB::run(float vel) |
| kennethdhdl | 0:dd3d9657fd9f | 46 | { |
| kennethdhdl | 0:dd3d9657fd9f | 47 | if (vel>=0) { |
| kennethdhdl | 0:dd3d9657fd9f | 48 | setSpeed((vel>1.0f) ? 1.0f:vel); |
| kennethdhdl | 0:dd3d9657fd9f | 49 | forward(); |
| kennethdhdl | 0:dd3d9657fd9f | 50 | } else { |
| kennethdhdl | 0:dd3d9657fd9f | 51 | setSpeed((vel<-1.0f) ? 1.0f:-vel); |
| kennethdhdl | 0:dd3d9657fd9f | 52 | backward(); |
| kennethdhdl | 0:dd3d9657fd9f | 53 | } |
| kennethdhdl | 0:dd3d9657fd9f | 54 | } |