Smartcar Shield
BrushedMotor.cpp
Go to the documentation of this file.
1 #include "BrushedMotor.hpp"
2 #include "../../../utilities/Utilities.hpp"
3 
4 namespace
5 {
6 const int kMinPwm = 0;
7 const int kMaxPwm = 255;
8 } // namespace
9 
10 using namespace smartcarlib::utils;
11 using 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 
32 void BrushedMotor::setSpeed(int speed)
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 
65 void BrushedMotor::attach()
66 {
67  mRuntime.setPinDirection(kForwardPin, kOutput);
68  mRuntime.setPinDirection(kBackwardPin, kOutput);
69  mRuntime.setPinDirection(kEnablePin, kOutput);
70  mAttached = true;
71 }
BrushedMotorPins
Helper class to represent brushed motor pins.
Definition: BrushedMotor.hpp:20
BrushedMotor.hpp
smartcarlib::constants::motor::kMaxMotorSpeed
const int kMaxMotorSpeed
Definition: Motor.hpp:17
BrushedMotor::BrushedMotor
BrushedMotor(Runtime &runtime, uint8_t forwardPin, uint8_t backwardPin, uint8_t enablePin)
Constructs a brushed DC motor instance.
Definition: BrushedMotor.cpp:13
Runtime::setPinState
virtual void setPinState(uint8_t pin, uint8_t state)=0
Set pin state, equivalent of digitalWrite in Arduino.
BrushedMotor
Definition: BrushedMotor.hpp:41
smartcarlib::constants::motor::kIdleMotorSpeed
const int kIdleMotorSpeed
Definition: Motor.hpp:16
Runtime
Definition: Runtime.hpp:35
smartcarlib::utils::getMap
constexpr AnyNumber getMap(AnyNumber valueToMap, AnyNumber fromLow, AnyNumber fromHigh, AnyNumber toLow, AnyNumber toHigh)
Maps a value from a range to another.
Definition: Utilities.hpp:104
smartcarlib::constants::motor
Definition: Motor.hpp:13
BrushedMotorPins::backward
const uint8_t backward
Definition: BrushedMotor.hpp:37
smartcarlib::utils::getAbsolute
constexpr AnyNumber getAbsolute(AnyNumber number)
Gets the absolute of the supplied number.
Definition: Utilities.hpp:24
Runtime::setPWM
virtual void setPWM(uint8_t pin, int dutyCycle)=0
Set PWM state, equivalent of analogWrite in Arduino.
BrushedMotorPins::enable
const uint8_t enable
Definition: BrushedMotor.hpp:38
smartcarlib::utils
Definition: Utilities.hpp:9
smartcarlib::constants::motor::kMinMotorSpeed
const int kMinMotorSpeed
Definition: Motor.hpp:15
BrushedMotorPins::forward
const uint8_t forward
Definition: BrushedMotor.hpp:36
BrushedMotor::setSpeed
void setSpeed(int speed) override
Sets the motor speed and direction as the percentage of the maximum possible speed,...
Definition: BrushedMotor.cpp:32
smartcarlib::utils::getConstrain
constexpr AnyNumber getConstrain(AnyNumber number, AnyNumber min, AnyNumber max)
Limit the number between a range.
Definition: Utilities.hpp:46
Runtime::setPinDirection
virtual void setPinDirection(uint8_t pin, uint8_t direction)=0
Set pin direction, equivalent of pinMode in Arduino.