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