TinyRobotics
Loading...
Searching...
No Matches
BrushedMotor.h
1#include <algorithm>
2#pragma once
3#include <Arduino.h>
4
5#include "IMotor.h"
6
7namespace tinyrobotics {
8
9/**
10 * @class BrushedMotor
11 * @ingroup motors
12 * @brief High-level H-Bridge motor driver class for bidirectional DC motor
13 * control.
14 *
15 * This class provides an easy interface for controlling a DC motor using a
16 * standard H-Bridge driver (e.g., L298N, L293D, TB6612FNG). It supports:
17 * - Forward, reverse, and stop (brake) control
18 * - PWM speed control (signed, -255 to 255 or limited by setConstraints)
19 * - Speed setting in percent (-100 to 100)
20 * - Optional PWM frequency configuration (if supported by platform)
21 * - Speed limiting for safety or motor tuning
22 *
23 * Usage Example:
24 * @code
25 * HBridge motor(5, 6); // IN1=5, IN2=6,
26 * motor.setConstraints(-200, 200); // Limit speed range
27 * motor.setSpeed(50); // Half speed forward
28 * motor.setSpeed(-50); // Half speed reverse
29 * motor.stop(); // Brake
30 * @endcode
31 *
32 * @note If your platform does not support analogWriteFreq, the default PWM
33 * frequency will be used.
34 */
35template <typename T = float>
36class BrushedMotor : public IMotor<T> {
37 public:
38 /**
39 * @brief Empty constructor for late pin assignment.
40 */
41 BrushedMotor(uint8_t id = 0) { this->setID(id); }
42
43 /**
44 * @brief Set the pins for the HBridge after construction.
45 */
46 void setPins(int pin1, int pin2, int pwmFreq = 20000) {
47 pinIn1 = pin1;
48 pinIn2 = pin2;
49 this->pwmFreq = pwmFreq;
50 }
51
52 bool begin() {
53 if (!isPinsSet()) return false;
54
55 pinMode(pinIn1, OUTPUT);
56 pinMode(pinIn2, OUTPUT);
57
58#ifdef SUPPORTS_ANALOG_WRITE_FREQ
59 if (pwmFreq > 0) analogWriteFreq(pinIn1, pwmFreq);
60 if (pwmFreq > 0) analogWriteFreq(pinIn2, pwmFreq);
61#endif
62 stop();
63 return true;
64 }
65
66 /// Constrain the speed range to a subset of -255..255. This can be used to
67 /// limit the maximum speed for safety or to match the characteristics of a
68 /// specific motor.
69 void setConstraints(T minSpeed, T maxSpeed) {
70 minSpeed = std::max(minSpeed, -255);
71 maxSpeed = std::min(maxSpeed, 255);
72 }
73
74 /// Invert the direction logic
75 void setReverse(bool reverse) { is_reverse = reverse; }
76
77 /**
78 * Set motor speed as a percentage (-100 to 100).
79 * @param percent Speed percentage: -100 (full reverse) to 100 (full forward)
80 */
81 // Set value as percentage (-100 to 100)
82 bool setValuePercent(T percent) override {
83 lastValuePercent = constrain(percent, -100, 100);
84 int pwmValue = map(lastValuePercent,(T) -100, (T) 100, minSpeed, maxSpeed);
85 setSpeedValue(pwmValue);
86 return true;
87 }
88
89 T getValuePercent() const override { return lastValuePercent; }
90
91 /** Stop the motor (brake) */
92 void end() override { stop(); }
93
94 /// Check if the control pins have been set (i.e., not -1).
95 bool isPinsSet() const {
96 return pinIn1 != -1 && pinIn2 != -1;
97 }
98
99 protected:
100 int pinIn1 = -1;
101 int pinIn2 = -1;
102 T minSpeed = -255;
103 T maxSpeed = 255;
104 int pwmFreq = 20000; // Default PWM frequency (20 kHz)
105 bool is_reverse = false;
106 T lastValuePercent = 0.0f;
107
108 bool stop() {
109 digitalWrite(pinIn1, LOW);
110 digitalWrite(pinIn2, LOW);
111 return true;
112 }
113
114 /** Set motor speed and direction. Speed: -255 (full reverse) to 255 (full
115 * forward) */
116 void setSpeedValue(float speed) {
117 speed = constrain(speed, minSpeed, maxSpeed);
118 if (is_reverse) speed = -speed; // Invert speed if reverse is set
119 if (speed > 0) {
120 analogWrite(pinIn1, speed);
121 digitalWrite(pinIn2, LOW);
122 } else if (speed < 0) {
123 digitalWrite(pinIn1, LOW);
124 analogWrite(pinIn2, -speed);
125 } else if (speed == 0) {
126 stop();
127 }
128 }
129};
130
131} // namespace tinyrobotics
High-level H-Bridge motor driver class for bidirectional DC motor control.
Definition: BrushedMotor.h:36
void setConstraints(T minSpeed, T maxSpeed)
Definition: BrushedMotor.h:69
bool setValuePercent(T percent) override
Definition: BrushedMotor.h:82
void setReverse(bool reverse)
Invert the direction logic.
Definition: BrushedMotor.h:75
void setPins(int pin1, int pin2, int pwmFreq=20000)
Set the pins for the HBridge after construction.
Definition: BrushedMotor.h:46
void end() override
Definition: BrushedMotor.h:92
BrushedMotor(uint8_t id=0)
Empty constructor for late pin assignment.
Definition: BrushedMotor.h:41
bool isPinsSet() const
Check if the control pins have been set (i.e., not -1).
Definition: BrushedMotor.h:95
void setSpeedValue(float speed)
Definition: BrushedMotor.h:116
Abstract base class for all motor driver types.
Definition: IMotor.h:21