Smartcar Shield
DirectionalOdometer.cpp
Go to the documentation of this file.
1 #include <limits.h> // NOLINT(modernize-deprecated-headers)
2 
4 
6 
7 namespace
8 {
9 const auto kInvalidPinState = INT_MIN;
10 }
11 
13  uint8_t pulsePin,
14  uint8_t forwardWhenLowPin,
15  InterruptCallback callback,
16  unsigned long pulsesPerMeter)
17  : DirectionlessOdometer(runtime, pulsePin, callback, pulsesPerMeter)
18  , mDirectionPin{ forwardWhenLowPin }
19  , mRuntime(runtime)
20  , kPinStateWhenForward{ mRuntime.getLowState() }
21  , mDirectionPinState{ kInvalidPinState }
22 {
23  mRuntime.setPinDirection(mDirectionPin, mRuntime.getInputState());
24 }
25 
28  InterruptCallback callback,
29  unsigned long pulsesPerMeter)
30  : DirectionalOdometer(runtime, pins.pulse, pins.direction, callback, pulsesPerMeter)
31 {
32 }
33 
35 {
37  mNegativePulsesCounter = 0;
38 }
39 
41 {
42  // Calculate the difference in time between the last two pulses (in microseconds)
43  const auto currentPulse = mRuntime.currentTimeMicros();
44  const auto dt = currentPulse - mPreviousPulse;
45  // Unless this is the first time we are called, if two pulses are too close
46  // then the signal is noisy and they should be ignored
47  if (mPreviousPulse != 0 && dt < kMinimumPulseGap)
48  {
49  return;
50  }
51 
52  mDirectionPinState = mRuntime.getPinState(mDirectionPin);
53 
54  // Unless this is the first time we are called then calculate the dT since
55  // on the first time we cannot determine the speed yet
56  if (mPreviousPulse != 0)
57  {
58  mDt = dt;
59  }
60  mPreviousPulse = currentPulse;
61 
62  if (mDirectionPinState != kPinStateWhenForward)
63  {
64  mNegativePulsesCounter++;
65  }
66  else
67  {
69  }
70 }
71 
73 {
75  - static_cast<long>(static_cast<float>(mNegativePulsesCounter) / mPulsesPerMeterRatio);
76 }
77 
79 {
80  return DirectionlessOdometer::getSpeed() * static_cast<float>(getDirection());
81 }
82 
84 {
85  return mDirectionPinState == kPinStateWhenForward ? smartcarlib::constants::odometer::kForward
87 }
88 
90 {
91  return true;
92 }
Runtime::getPinState
virtual int getPinState(uint8_t pin)=0
Get the pin state, equivalent of digitalRead in Arduino.
DirectionalOdometer::DirectionalOdometer
DirectionalOdometer(Runtime &runtime, uint8_t pulsePin, uint8_t forwardWhenLowPin, InterruptCallback callback, unsigned long pulsesPerMeter)
Constructs an odometer that can measure distance, speed and direction.
Definition: DirectionalOdometer.cpp:12
DirectionalOdometer.hpp
DirectionlessOdometer::mPulsesCounter
volatile unsigned long mPulsesCounter
Definition: DirectionlessOdometer.hpp:72
DirectionlessOdometer
Definition: DirectionlessOdometer.hpp:14
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
DirectionalOdometer::reset
void reset() override
Resets the total travelled distance and speed to 0
Definition: DirectionalOdometer.cpp:34
DirectionalOdometer::update
void update() override
Conducts the distance and speed measurements.
Definition: DirectionalOdometer.cpp:40
DirectionalOdometer::getDirection
int8_t getDirection() const
Get the direction of movement.
Definition: DirectionalOdometer.cpp:83
Runtime
Definition: Runtime.hpp:35
smartcarlib::constants::odometer::kMinimumPulseGap
const unsigned long kMinimumPulseGap
Definition: Odometer.hpp:21
DirectionalOdometer::getDistance
long getDistance() override
Returns the travelled distance in centimeters where sign can indicate direction if there is hardware ...
Definition: DirectionalOdometer.cpp:72
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
DirectionalOdometer::providesDirection
bool providesDirection() const override
Return whether the sensor is capable of inferring the direction of movement.
Definition: DirectionalOdometer.cpp:89
DirectionalOdometer::getSpeed
float getSpeed() override
Returns the current speed in meters/sec where sign can indicate direction if there is hardware suppor...
Definition: DirectionalOdometer.cpp:78
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
smartcarlib::constants::odometer::kBackward
const int8_t kBackward
Definition: Odometer.hpp:18
DirectionalOdometer
Definition: DirectionalOdometer.hpp:32
smartcarlib::constants::odometer::kForward
const int8_t kForward
Definition: Odometer.hpp:17
DirectionalOdometerPins
Helper class to represent directional odometer pins.
Definition: DirectionalOdometer.hpp:14
Runtime::currentTimeMicros
virtual unsigned long currentTimeMicros()=0
Gets the amount of microseconds since the microcontroller started running, equivalent of micros in Ar...
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