Smartcar Shield
Loading...
Searching...
No Matches
DirectionlessOdometer.cpp
Go to the documentation of this file.
1#include <math.h> // NOLINT(modernize-deprecated-headers)
2
4
5namespace
6{
7const int8_t kNotAnInterrupt = -1;
8const float kMillisecondsInSecond = 1000.0;
9const float kMillimetersInMeter = 1000.0;
10} // namespace
11
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 mRuntime.disableInterrupts();
35 const auto pulsesCounter = mPulsesCounter;
36 mRuntime.enableInterrupts();
37 return static_cast<long>(static_cast<float>(pulsesCounter) / mPulsesPerMeterRatio);
38}
39
41{
42 // To get the current speed in m/sec, divide the meters per pulse (dx) with
43 // the length between the last two pulses (dt)
44 mRuntime.disableInterrupts();
45 const auto dt = mDt;
46 mRuntime.enableInterrupts();
47 return dt > 0 ? kMillisecondsInSecond * mMillimetersPerPulse / static_cast<float>(dt) : 0.0F;
48}
49
51{
52 return kSensorAttached;
53}
54
56{
57 mRuntime.disableInterrupts();
60 mDt = 0;
61 mRuntime.enableInterrupts();
62}
63
65{
66 // Calculate the difference in time between the last two pulses (in microseconds)
67 const auto currentPulse = mRuntime.currentTimeMicros();
68 const auto dt = currentPulse - mPreviousPulse;
69 // Unless this is the first time we are called, if two pulses are too close
70 // then the signal is noisy and they should be ignored
71 if (mPreviousPulse != 0 && dt < kMinimumPulseGap)
72 {
73 return;
74 }
75 // Unless this is the first time we are called then calculate the dT since
76 // on the first time we cannot determine the speed yet
77 if (mPreviousPulse != 0)
78 {
79 mDt = dt;
80 }
81 mPreviousPulse = currentPulse;
83}
84
86{
87 return false;
88}
#define STORED_IN_RAM
Definition Runtime.hpp:32
volatile unsigned long mPulsesCounter
volatile unsigned long mPreviousPulse
DirectionlessOdometer(Runtime &runtime, uint8_t pulsePin, InterruptCallback callback, unsigned long pulsesPerMeter)
Constructs an odometer that can measure distance, speed but not direction.
bool isAttached() const override
Returns whether the sensor has been properly attached.
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
bool providesDirection() const override
Return whether the sensor is capable of inferring the direction of movement.
virtual void update()
Conducts the distance and speed measurements.
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 int getRisingEdgeMode() const =0
Get the rising edge constant for setting an interrupt.
virtual void enableInterrupts()=0
Enable interrupts.
virtual int8_t pinToInterrupt(uint8_t pin)=0
Gets the interrupt number of the specified pin, equivalent of digitalPinToInterrupt in Arduino.
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 void setInterrupt(uint8_t pin, InterruptCallback callback, int mode)=0
Enables an external hardware interrupt and provides a callback, equivalent to attachInterrupt in Ardu...
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
const unsigned long kDefaultPulsesPerMeter
Definition Odometer.hpp:20