Smartcar Shield
Loading...
Searching...
No Matches
BrushedMotor.cpp
Go to the documentation of this file.
1#include "BrushedMotor.hpp"
2#include "../../../utilities/Utilities.hpp"
3
4namespace
5{
6const int kMinPwm = 0;
7const int kMaxPwm = 255;
8} // namespace
9
10using namespace smartcarlib::utils;
11using namespace smartcarlib::constants::motor;
12
14 uint8_t forwardPin,
15 uint8_t backwardPin,
16 uint8_t enablePin)
17 : kForwardPin{ forwardPin }
18 , kBackwardPin{ backwardPin }
19 , kEnablePin{ enablePin }
20 , mRuntime(runtime)
21 , kOutput{ mRuntime.getOutputState() }
22 , kLow{ mRuntime.getLowState() }
23 , kHigh{ mRuntime.getHighState() }
24{
25}
26
28 : BrushedMotor{ runtime, pins.forward, pins.backward, pins.enable }
29{
30}
31
33{
34 if (!mAttached)
35 {
36 attach();
37 }
38
39 if (speed < 0)
40 {
41 // Set backward direction
42 mRuntime.setPinState(kForwardPin, kLow);
43 mRuntime.setPinState(kBackwardPin, kHigh);
44 }
45 else if (speed > 0)
46 {
47 // Set forward direction
48 mRuntime.setPinState(kForwardPin, kHigh);
49 mRuntime.setPinState(kBackwardPin, kLow);
50 }
51 else
52 {
53 // Set idle
54 mRuntime.setPinState(kForwardPin, kLow);
55 mRuntime.setPinState(kBackwardPin, kLow);
56 }
57
58 // Get the absolute speed within the [0, 100] range
59 auto absoluteSpeed = getAbsolute(getConstrain(speed, kMinMotorSpeed, kMaxMotorSpeed));
60 // Set the PWM within the [0,255] range
61 mRuntime.setPWM(kEnablePin,
62 getMap(absoluteSpeed, kIdleMotorSpeed, kMaxMotorSpeed, kMinPwm, kMaxPwm));
63}
64
65void BrushedMotor::attach()
66{
67 mRuntime.setPinDirection(kForwardPin, kOutput);
68 mRuntime.setPinDirection(kBackwardPin, kOutput);
69 mRuntime.setPinDirection(kEnablePin, kOutput);
70 mAttached = true;
71}
A brushed motor is controlled via two wires where the flow of current determines the direction of rot...
BrushedMotor(Runtime &runtime, uint8_t forwardPin, uint8_t backwardPin, uint8_t enablePin)
Constructs a brushed DC motor instance.
void setSpeed(int speed) override
Sets the motor speed and direction as the percentage of the maximum possible speed,...
An interface to serve as a wrapper for the native runtime environment, greatly inspired by the Arduin...
Definition Runtime.hpp:36
virtual void setPinDirection(uint8_t pin, uint8_t direction)=0
Set pin direction, equivalent of pinMode in Arduino.
virtual void setPWM(uint8_t pin, int dutyCycle)=0
Set PWM state, equivalent of analogWrite in Arduino.
virtual void setPinState(uint8_t pin, uint8_t state)=0
Set pin state, equivalent of digitalWrite in Arduino.
constexpr AnyNumber getMap(AnyNumber valueToMap, AnyNumber fromLow, AnyNumber fromHigh, AnyNumber toLow, AnyNumber toHigh)
Maps a value from a range to another.
constexpr AnyNumber getConstrain(AnyNumber number, AnyNumber min, AnyNumber max)
Limit the number between a range.
Definition Utilities.hpp:46
constexpr AnyNumber getAbsolute(AnyNumber number)
Gets the absolute of the supplied number.
Definition Utilities.hpp:24
Helper class to represent brushed motor pins.