Smartcar Shield
Loading...
Searching...
No Matches
DirectionalOdometer.cpp
Go to the documentation of this file.
1#include <limits.h> // NOLINT(modernize-deprecated-headers)
2
4
6
7namespace
8{
9const 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 mRuntime.disableInterrupts();
38 mNegativePulsesCounter = 0;
39 mRuntime.enableInterrupts();
40}
41
43{
44 // Calculate the difference in time between the last two pulses (in microseconds)
45 const auto currentPulse = mRuntime.currentTimeMicros();
46 const auto dt = currentPulse - mPreviousPulse;
47 // Unless this is the first time we are called, if two pulses are too close
48 // then the signal is noisy and they should be ignored
49 if (mPreviousPulse != 0 && dt < kMinimumPulseGap)
50 {
51 return;
52 }
53
54 mDirectionPinState = mRuntime.getPinState(mDirectionPin);
55
56 // Unless this is the first time we are called then calculate the dT since
57 // on the first time we cannot determine the speed yet
58 if (mPreviousPulse != 0)
59 {
60 mDt = dt;
61 }
62 mPreviousPulse = currentPulse;
63
64 if (mDirectionPinState != kPinStateWhenForward)
65 {
66 mNegativePulsesCounter = mNegativePulsesCounter + 1;
67 }
68 else
69 {
71 }
72}
73
75{
76 mRuntime.disableInterrupts();
77 const auto negativePulses = mNegativePulsesCounter;
78 mRuntime.enableInterrupts();
80 - static_cast<long>(static_cast<float>(negativePulses) / mPulsesPerMeterRatio);
81}
82
84{
85 return DirectionlessOdometer::getSpeed() * static_cast<float>(getDirection());
86}
87
89{
90 mRuntime.disableInterrupts();
91 const auto directionPinState = mDirectionPinState;
92 mRuntime.enableInterrupts();
93 return directionPinState == kPinStateWhenForward ? smartcarlib::constants::odometer::kForward
95}
96
98{
99 return true;
100}
#define STORED_IN_RAM
Definition Runtime.hpp:32
A class to represent directional Odometers.
void update() override
Conducts the distance and speed measurements.
bool providesDirection() const override
Return whether the sensor is capable of inferring the direction of movement.
long getDistance() override
Returns the travelled distance in centimeters where sign can indicate direction if there is hardware ...
int8_t getDirection() const
Get the direction of movement.
void reset() override
Resets the total travelled distance and speed to 0
DirectionalOdometer(Runtime &runtime, uint8_t pulsePin, uint8_t forwardWhenLowPin, InterruptCallback callback, unsigned long pulsesPerMeter)
Constructs an odometer that can measure distance, speed and direction.
float getSpeed() override
Returns the current speed in meters/sec where sign can indicate direction if there is hardware suppor...
A class to represent the common - directionless - Odometers (speed encoders) that can be used to prim...
volatile unsigned long mPulsesCounter
volatile unsigned long mPreviousPulse
float getSpeed() override
Returns the current speed in meters/sec where sign can indicate direction if there is hardware suppor...
virtual void reset()
Resets the total travelled distance and speed to 0
volatile unsigned long mDt
long getDistance() override
Returns the travelled distance in centimeters where sign can indicate direction if there is hardware ...
A callback to be invoked.
An interface to serve as a wrapper for the native runtime environment, greatly inspired by the Arduin...
Definition Runtime.hpp:36
virtual void enableInterrupts()=0
Enable interrupts.
virtual void setPinDirection(uint8_t pin, uint8_t direction)=0
Set pin direction, equivalent of pinMode in Arduino.
virtual void disableInterrupts()=0
Disable interrupts.
virtual uint8_t getInputState() const =0
Get the runtime-specific value representing an INPUT pin state.
virtual int getPinState(uint8_t pin)=0
Get the pin state, equivalent of digitalRead in Arduino.
virtual unsigned long currentTimeMicros()=0
Gets the amount of microseconds since the microcontroller started running, equivalent of micros in Ar...
const unsigned long kMinimumPulseGap
Definition Odometer.hpp:21
Helper class to represent directional odometer pins.