Smartcar Shield
DifferentialControl.cpp
Go to the documentation of this file.
2 
3 #include "../../utilities/Utilities.hpp"
4 
5 using namespace smartcarlib::utils;
6 using namespace smartcarlib::constants::control;
7 using namespace smartcarlib::constants::motor;
8 
10  : mLeftMotor(leftMotor)
11  , mRightMotor(rightMotor)
12  , mAngle{ 0 }
13  , mSpeed{ 0 }
14 {
15 }
16 
18 {
20  setMotors();
21 }
22 
24 {
25  mSpeed = getConstrain(speed, kMinMotorSpeed, kMaxMotorSpeed);
26  setMotors();
27 }
28 
29 void DifferentialControl::setMotors()
30 {
31  // With differential control, angle represents the ratio of speed between the two motors
32  float ratio = static_cast<float>(kMaxControlAngle - getAbsolute(mAngle))
33  / static_cast<float>(kMaxControlAngle);
34 
35  if (mAngle < kIdleControlAngle)
36  {
37  // Turning to the left (counter clockwise) by setting lower speed to the left motor
38  mLeftMotor.setSpeed(static_cast<int>(static_cast<float>(mSpeed) * ratio));
39  mRightMotor.setSpeed(mSpeed);
40  }
41  else
42  {
43  // Turning to the right (clockwise) by setting lower speed to the right motor
44  mLeftMotor.setSpeed(mSpeed);
45  mRightMotor.setSpeed(static_cast<int>(static_cast<float>(mSpeed) * ratio));
46  }
47 }
48 
49 void DifferentialControl::overrideMotorSpeed(int firstMotorSpeed, int secondMotorSpeed)
50 {
51  firstMotorSpeed = getConstrain(firstMotorSpeed, kMinMotorSpeed, kMaxMotorSpeed);
52  secondMotorSpeed = getConstrain(secondMotorSpeed, kMinMotorSpeed, kMaxMotorSpeed);
53 
54  mLeftMotor.setSpeed(firstMotorSpeed);
55  mRightMotor.setSpeed(secondMotorSpeed);
56 }
DifferentialControl::overrideMotorSpeed
void overrideMotorSpeed(int firstMotorSpeed, int secondMotorSpeed) override
Set the motor speed individually as a percentage of the motors` total power.
Definition: DifferentialControl.cpp:49
DifferentialControl::DifferentialControl
DifferentialControl(Motor &leftMotor, Motor &rightMotor)
Constructs an Ackerman way of controlling the vehicle.
Definition: DifferentialControl.cpp:9
smartcarlib::constants::motor::kMaxMotorSpeed
const int kMaxMotorSpeed
Definition: Motor.hpp:17
smartcarlib::constants::control::kMaxControlAngle
const int kMaxControlAngle
Definition: Control.hpp:15
Motor
Definition: Motor.hpp:22
smartcarlib::constants::motor
Definition: Motor.hpp:13
smartcarlib::utils::getAbsolute
constexpr AnyNumber getAbsolute(AnyNumber number)
Gets the absolute of the supplied number.
Definition: Utilities.hpp:24
DifferentialControl.hpp
DifferentialControl::setSpeed
void setSpeed(int speed) override
Sets the driving speed as percentage of the total motor speed where the sign represents the direction...
Definition: DifferentialControl.cpp:23
smartcarlib::utils
Definition: Utilities.hpp:9
smartcarlib::constants::motor::kMinMotorSpeed
const int kMinMotorSpeed
Definition: Motor.hpp:15
smartcarlib::constants::control::kMinControlAngle
const int kMinControlAngle
Definition: Control.hpp:13
smartcarlib::constants::control
Definition: Control.hpp:11
smartcarlib::constants::control::kIdleControlAngle
const int kIdleControlAngle
Definition: Control.hpp:14
smartcarlib::utils::getConstrain
constexpr AnyNumber getConstrain(AnyNumber number, AnyNumber min, AnyNumber max)
Limit the number between a range.
Definition: Utilities.hpp:46
Motor::setSpeed
virtual void setSpeed(int speed)=0
Sets the motor speed and direction as the percentage of the maximum possible speed,...
DifferentialControl::setAngle
void setAngle(int angle) override
Sets the driving angle in degrees [-90, 90].
Definition: DifferentialControl.cpp:17