Smartcar Shield
DistanceCar.hpp
Go to the documentation of this file.
1 
5 #pragma once
6 
7 #include "../../runtime/Runtime.hpp"
8 #include "../../sensors/odometer/Odometer.hpp"
9 #include "../simple/SimpleCar.hpp"
10 
11 namespace smartcarlib
12 {
13 namespace constants
14 {
15 namespace car
16 {
17 const unsigned long kDefaultPidFrequency = 80;
18 const float kDefaultProportional = 5.0F;
19 const float kDefaultIntegral = 0.0F;
20 const float kDefaultDerivative = 10.0F;
21 const int kOdometersNotAttachedError = -1000;
22 const auto kBreakSpeedScale = 10;
23 } // namespace car
24 } // namespace constants
25 } // namespace smartcarlib
26 
27 class DistanceCar : virtual public SimpleCar
28 {
29 public:
49  DistanceCar(Runtime& runtime, Control& control, Odometer& odometer);
50 
75  DistanceCar(Runtime& runtime,
76  Control& control,
77  Odometer& odometerLeft,
78  Odometer& odometerRight);
79 
90  long getDistance();
91 
113  void setSpeed(float speed) override;
114 
124  float getSpeed();
125 
143  unsigned long frequency
145 
160  virtual void update();
161 
170  void disableCruiseControl();
171 
187  void overrideMotorSpeed(int firstMotorSpeed, int secondMotorSpeed) override;
188 
189 private:
190  Odometer& mOdometerLeft;
191  Odometer& mOdometerRight;
192  Runtime& mRuntime;
193  bool mCruiseControlEnabled{ false };
194  float mProportional{ 0 };
195  float mIntegral{ 0 };
196  float mDerivative{ 0 };
197  unsigned long mFrequency{ 0 };
198  unsigned long mPreviousUpdate{ 0 };
199  float mTargetSpeed{ 0 };
200  float mPreviousControlledSpeed{ 0 };
201  float mIntegratedError{ 0 };
202  float mPreviousError{ 0 };
203 
204  bool areOdometersAttached();
205  bool areOdometersDirectional();
206  float controlMotorSpeed(const float& previousSpeed,
207  const float& targetSpeed,
208  const float& currentSpeed);
209 
213  void brake();
214 };
215 
smartcarlib
Definition: DistanceCar.hpp:11
DistanceCar
Definition: DistanceCar.hpp:27
smartcarlib::constants::car::kOdometersNotAttachedError
const int kOdometersNotAttachedError
Definition: DistanceCar.hpp:21
DistanceCar::getDistance
long getDistance()
Gets the car's travelled distance.
Definition: DistanceCar.cpp:36
Odometer
Definition: Odometer.hpp:26
smartcarlib::constants::car::kDefaultPidFrequency
const unsigned long kDefaultPidFrequency
Definition: DistanceCar.hpp:17
Runtime
Definition: Runtime.hpp:35
Control
Definition: Control.hpp:23
DistanceCar::disableCruiseControl
void disableCruiseControl()
Disable cruise control.
Definition: DistanceCar.cpp:183
DistanceCar::DistanceCar
DistanceCar(Runtime &runtime, Control &control, Odometer &odometer)
Constructs a car equipped with a distance sensor.
Definition: DistanceCar.cpp:17
DistanceCar::enableCruiseControl
void enableCruiseControl(float proportional=smartcarlib::constants::car::kDefaultProportional, float integral=smartcarlib::constants::car::kDefaultIntegral, float derivative=smartcarlib::constants::car::kDefaultDerivative, unsigned long frequency=smartcarlib::constants::car::kDefaultPidFrequency)
Enables the car to move with a stable speed using the odometers.
Definition: DistanceCar.cpp:188
smartcarlib::constants::car::kBreakSpeedScale
const auto kBreakSpeedScale
Definition: DistanceCar.hpp:22
smartcarlib::constants::car::kDefaultProportional
const float kDefaultProportional
Definition: DistanceCar.hpp:18
smartcarlib::constants::car::kDefaultIntegral
const float kDefaultIntegral
Definition: DistanceCar.hpp:19
SimpleCar
Definition: SimpleCar.hpp:10
DistanceCar::update
virtual void update()
Adjusts the cruise control speed.
Definition: DistanceCar.cpp:112
DistanceCar::overrideMotorSpeed
void overrideMotorSpeed(int firstMotorSpeed, int secondMotorSpeed) override
Sets the motor speed individually as a percentage of the motors` total power unless cruise control is...
Definition: DistanceCar.cpp:210
DistanceCar::setSpeed
void setSpeed(float speed) override
Sets the car's speed in meters per second if cruise control is enabled otherwise as a percentage of t...
Definition: DistanceCar.cpp:42
DistanceCar::getSpeed
float getSpeed()
Gets the car's current speed in meters per second.
Definition: DistanceCar.cpp:177
smartcarlib::constants::car::kDefaultDerivative
const float kDefaultDerivative
Definition: DistanceCar.hpp:20