Smartcar Shield
DirectionlessOdometer.cpp
Go to the documentation of this file.
1 #include <math.h> // NOLINT(modernize-deprecated-headers)
2 
4 
5 namespace
6 {
7 const int8_t kNotAnInterrupt = -1;
8 const float kMillisecondsInSecond = 1000.0;
9 const float kMillimetersInMeter = 1000.0;
10 } // namespace
11 
12 using namespace smartcarlib::constants::odometer;
13 
15  uint8_t pulsePin,
16  InterruptCallback callback,
17  unsigned long pulsesPerMeter)
18  : mPulsesPerMeterRatio{ pulsesPerMeter > 0 ? static_cast<float>(pulsesPerMeter) / 100.0F
19  : kDefaultPulsesPerMeter / 100.0F }
20  , mMillimetersPerPulse{ pulsesPerMeter > 0
21  ? kMillimetersInMeter / static_cast<float>(pulsesPerMeter)
22  : kMillimetersInMeter / static_cast<float>(kDefaultPulsesPerMeter) }
23  , mRuntime(runtime)
24  , kSensorAttached{ mRuntime.pinToInterrupt(pulsePin) != kNotAnInterrupt }
25 {
26  mRuntime.setPinDirection(pulsePin, mRuntime.getInputState());
27  mRuntime.setInterrupt(static_cast<uint8_t>(mRuntime.pinToInterrupt(pulsePin)),
28  callback,
29  mRuntime.getRisingEdgeMode());
30 }
31 
33 {
34  return static_cast<long>(static_cast<float>(mPulsesCounter) / mPulsesPerMeterRatio);
35 }
36 
38 {
39  // To get the current speed in m/sec, divide the meters per pulse (dx) with
40  // the length between the last two pulses (dt)
41  return mDt > 0 ? kMillisecondsInSecond * mMillimetersPerPulse / static_cast<float>(mDt) : 0.0F;
42 }
43 
45 {
46  return kSensorAttached;
47 }
48 
50 {
51  mPulsesCounter = 0;
52  mPreviousPulse = 0;
53  mDt = 0;
54 }
55 
57 {
58  // Calculate the difference in time between the last two pulses (in microseconds)
59  const auto currentPulse = mRuntime.currentTimeMicros();
60  const auto dt = currentPulse - mPreviousPulse;
61  // Unless this is the first time we are called, if two pulses are too close
62  // then the signal is noisy and they should be ignored
63  if (mPreviousPulse != 0 && dt < kMinimumPulseGap)
64  {
65  return;
66  }
67  // Unless this is the first time we are called then calculate the dT since
68  // on the first time we cannot determine the speed yet
69  if (mPreviousPulse != 0)
70  {
71  mDt = dt;
72  }
73  mPreviousPulse = currentPulse;
75 }
76 
78 {
79  return false;
80 }
DirectionlessOdometer::mPulsesCounter
volatile unsigned long mPulsesCounter
Definition: DirectionlessOdometer.hpp:72
InterruptCallback
A callback to be invoked. Depending on the platform different callback types may be necessary.
DirectionlessOdometer::mDt
volatile unsigned long mDt
Definition: DirectionlessOdometer.hpp:74
Runtime
Definition: Runtime.hpp:35
smartcarlib::constants::odometer::kMinimumPulseGap
const unsigned long kMinimumPulseGap
Definition: Odometer.hpp:21
DirectionlessOdometer::update
virtual void update()
Conducts the distance and speed measurements.
Definition: DirectionlessOdometer.cpp:56
DirectionlessOdometer::mPulsesPerMeterRatio
const float mPulsesPerMeterRatio
Definition: DirectionlessOdometer.hpp:71
DirectionlessOdometer::getDistance
long getDistance() override
Returns the travelled distance in centimeters where sign can indicate direction if there is hardware ...
Definition: DirectionlessOdometer.cpp:32
smartcarlib::constants::odometer
Definition: Odometer.hpp:15
DirectionlessOdometer::DirectionlessOdometer
DirectionlessOdometer(Runtime &runtime, uint8_t pulsePin, InterruptCallback callback, unsigned long pulsesPerMeter)
Constructs an odometer that can measure distance, speed but not direction.
Definition: DirectionlessOdometer.cpp:14
DirectionlessOdometer::getSpeed
float getSpeed() override
Returns the current speed in meters/sec where sign can indicate direction if there is hardware suppor...
Definition: DirectionlessOdometer.cpp:37
DirectionlessOdometer::isAttached
bool isAttached() const override
Returns whether the sensor has been properly attached.
Definition: DirectionlessOdometer.cpp:44
DirectionlessOdometer.hpp
Runtime::currentTimeMicros
virtual unsigned long currentTimeMicros()=0
Gets the amount of microseconds since the microcontroller started running, equivalent of micros in Ar...
smartcarlib::constants::odometer::kDefaultPulsesPerMeter
const unsigned long kDefaultPulsesPerMeter
Definition: Odometer.hpp:20
DirectionlessOdometer::mPreviousPulse
volatile unsigned long mPreviousPulse
Definition: DirectionlessOdometer.hpp:73
STORED_IN_RAM
#define STORED_IN_RAM
Definition: Runtime.hpp:32
DirectionlessOdometer::reset
virtual void reset()
Resets the total travelled distance and speed to 0
Definition: DirectionlessOdometer.cpp:49
DirectionlessOdometer::providesDirection
bool providesDirection() const override
Return whether the sensor is capable of inferring the direction of movement.
Definition: DirectionlessOdometer.cpp:77