roo_quantity
API Documentation for roo_quantity
Loading...
Searching...
No Matches
velocity.h
Go to the documentation of this file.
1#pragma once
2
3#include <cmath>
4
5#include "roo_flags.h"
6#include "roo_logging.h"
8#include "roo_quantity/time.h"
9
10#if defined(ESP32) || defined(ESP8266) || defined(__linux__)
11#include <string>
12#endif
13
14#if defined(ARDUINO)
15#include <Arduino.h>
16#endif
17
18namespace roo_quantity {
19
20/// Representation of velocity, internally stored as floating-point meters per
21/// second.
22class Velocity {
23 public:
24 /// Creates a velocity object representing an 'unknown' velocity.
25 Velocity() : velocity_(std::nanf("")) {}
26
27 /// Returns the velocity in kilometers per second.
28 float inKilometersPerSecond() const { return velocity_ * 0.001f; }
29
30 /// Returns the velocity in kilometers per hour.
31 float inKilometersPerHour() const { return velocity_ * 3.6f; }
32
33 /// Returns the velocity in kilometers per hour.
34 float inKph() const { return inKilometersPerHour(); }
35
36 /// Returns the velocity in meters per second.
37 float inMetersPerSecond() const { return velocity_; }
38
39 /// Returns the velocity in millimeters per second.
40 float inMillimetersPerSecond() const { return velocity_ * 1000.0f; }
41
42 /// Returns the velocity in micrometers per second.
43 float inMicrometersPerSecond() const { return velocity_ * 1000000.0f; }
44
45 /// Returns the velocity in miles per hour.
46 float inMilesPerHour() const { return velocity_ * 2.2369362921f; }
47
48 /// Returns the velocity in miles per hour.
49 float inMph() const { return inMilesPerHour(); }
50
51 float inKnots() const { return velocity_ * 1.9438444924f; }
52
53 /// Returns whether the object represents an unknown velocity.
54 bool isUnknown() const { return std::isnan(velocity_); }
55
56 bool operator<(const Velocity& other) const {
57 return velocity_ < other.velocity_;
58 }
59
60 bool operator==(const Velocity& other) const {
61 return velocity_ == other.velocity_;
62 }
63
64 bool operator>(const Velocity& other) const {
65 return other.velocity_ < velocity_;
66 }
67
68 bool operator<=(const Velocity& other) const {
69 return !(other.velocity_ < velocity_);
70 }
71
72 bool operator>=(const Velocity& other) const {
73 return !(velocity_ < other.velocity_);
74 }
75
76 bool operator!=(const Velocity& other) const {
77 return !(velocity_ == other.velocity_);
78 }
79
80 inline Velocity& operator+=(const Velocity& other) {
81 velocity_ += other.inMetersPerSecond();
82 return *this;
83 }
84
85 inline Velocity& operator-=(const Velocity& other) {
86 velocity_ -= other.inMetersPerSecond();
87 return *this;
88 }
89
90 inline Velocity& operator*=(float multi) {
91 velocity_ *= multi;
92 return *this;
93 }
94
95 inline Velocity& operator/=(float div) {
96 velocity_ /= div;
97 return *this;
98 }
99
100#if defined(ESP32) || defined(ESP8266) || defined(__linux__)
101 /// Returns the string representation of the velocity.
102 std::string asString() const;
103#endif
104
105#if defined(ARDUINO)
106 String asArduinoString() const;
107#endif
108
109 private:
110 friend Velocity UnknownVelocity();
111
113
114 explicit Velocity(float velocity) : velocity_(velocity) {}
115
116 /// Stored in MetersPerSecond.
117 float velocity_;
118};
119
120inline Velocity VelocityInMetersPerSecond(float velocity);
121
122/// Returns a velocity object representing an unknown velocity.
123inline Velocity UnknownVelocity() { return Velocity(); }
124
125/// Returns a velocity object equivalent to the specified velocity
126/// expressed in kilometers per second.
128 return VelocityInMetersPerSecond(velocity * 1000.0f);
129}
130
131/// Returns a velocity object equivalent to the specified velocity
132/// expressed in meters per second.
133inline Velocity VelocityInMetersPerSecond(float velocity) {
134 return Velocity(velocity);
135}
136
137/// Returns a velocity object equivalent to the specified velocity
138/// expressed in millimeters per second.
140 return VelocityInMetersPerSecond(velocity * 0.001f);
141}
142
143/// Returns a velocity object equivalent to the specified velocity
144/// expressed in micrometers per second.
146 return VelocityInMetersPerSecond(velocity * 0.000001f);
147}
148
149/// Returns a velocity object equivalent to the specified velocity
150/// expressed in kilometers per hour.
151inline Velocity VelocityInKilometersPerHour(float velocity) {
152 return VelocityInMetersPerSecond(velocity * 0.2777777778f);
153}
154
155inline Velocity VelocityInKph(float velocity) {
156 return VelocityInKilometersPerHour(velocity);
157}
158
159/// Returns a velocity object equivalent to the specified velocity
160/// expressed in miles per hour.
161inline Velocity VelocityInMilesPerHour(float velocity) {
162 return VelocityInMetersPerSecond(velocity * 0.44704f);
163}
164
165inline Velocity VelocityInMph(float velocity) {
166 return VelocityInMilesPerHour(velocity);
167}
168
173
178
182
183inline Velocity operator*(Velocity a, float b) {
185}
186
187inline Velocity operator*(float a, Velocity b) {
189}
190
191inline Velocity operator/(Velocity a, float b) {
193}
194
195inline float operator/(Velocity a, Velocity b) {
196 return a.inMetersPerSecond() / b.inMetersPerSecond();
197}
198
199/// Vs time.
200
203}
204
207}
208
211}
212
214 return TimeInSeconds(a.inMeters() / b.inMetersPerSecond());
215}
216
217roo_logging::Stream& operator<<(roo_logging::Stream& os, const Velocity& val);
218
219} // namespace roo_quantity
Representation of length, internally stored as floating-point meters.
Definition length.h:19
float inMeters() const
Returns the length in meters.
Definition length.h:28
Representation of elapsed, internally stored as floating-point seconds.
Definition time.h:22
float inSeconds() const
Returns the time in seconds.
Definition time.h:30
Representation of velocity, internally stored as floating-point meters per second.
Definition velocity.h:22
bool operator>=(const Velocity &other) const
Definition velocity.h:72
Velocity & operator*=(float multi)
Definition velocity.h:90
friend Velocity UnknownVelocity()
Returns a velocity object representing an unknown velocity.
Definition velocity.h:123
float inMicrometersPerSecond() const
Returns the velocity in micrometers per second.
Definition velocity.h:43
friend Velocity VelocityInMetersPerSecond(float)
Returns a velocity object equivalent to the specified velocity expressed in meters per second.
Definition velocity.h:133
float inKnots() const
Definition velocity.h:51
Velocity & operator-=(const Velocity &other)
Definition velocity.h:85
bool operator!=(const Velocity &other) const
Definition velocity.h:76
Velocity()
Creates a velocity object representing an 'unknown' velocity.
Definition velocity.h:25
float inMillimetersPerSecond() const
Returns the velocity in millimeters per second.
Definition velocity.h:40
float inKph() const
Returns the velocity in kilometers per hour.
Definition velocity.h:34
bool operator<(const Velocity &other) const
Definition velocity.h:56
float inKilometersPerSecond() const
Returns the velocity in kilometers per second.
Definition velocity.h:28
float inMilesPerHour() const
Returns the velocity in miles per hour.
Definition velocity.h:46
bool operator==(const Velocity &other) const
Definition velocity.h:60
bool operator<=(const Velocity &other) const
Definition velocity.h:68
Velocity & operator/=(float div)
Definition velocity.h:95
float inMph() const
Returns the velocity in miles per hour.
Definition velocity.h:49
bool isUnknown() const
Returns whether the object represents an unknown velocity.
Definition velocity.h:54
float inMetersPerSecond() const
Returns the velocity in meters per second.
Definition velocity.h:37
Velocity & operator+=(const Velocity &other)
Definition velocity.h:80
float inKilometersPerHour() const
Returns the velocity in kilometers per hour.
Definition velocity.h:31
bool operator>(const Velocity &other) const
Definition velocity.h:64
For convenience conversion from roo_time::Duration.
Definition area.cpp:3
Length LengthInMeters(float length)
Returns a length object equivalent to the specified length expressed in meters.
Definition length.h:133
roo_logging::Stream & operator<<(roo_logging::Stream &os, const Area &val)
Definition area.cpp:57
Velocity VelocityInMilesPerHour(float velocity)
Returns a velocity object equivalent to the specified velocity expressed in miles per hour.
Definition velocity.h:161
Velocity VelocityInMillimetersPerSecond(float velocity)
Returns a velocity object equivalent to the specified velocity expressed in millimeters per second.
Definition velocity.h:139
Velocity VelocityInKilometersPerHour(float velocity)
Returns a velocity object equivalent to the specified velocity expressed in kilometers per hour.
Definition velocity.h:151
Area operator+(Area a, Area b)
Definition area.h:171
Velocity VelocityInMph(float velocity)
Definition velocity.h:165
Velocity VelocityInMetersPerSecond(float velocity)
Returns a velocity object equivalent to the specified velocity expressed in meters per second.
Definition velocity.h:133
Time TimeInSeconds(float time)
Returns a time object equivalent to the specified time expressed in seconds.
Definition time.h:111
Velocity VelocityInMicrometersPerSecond(float velocity)
Returns a velocity object equivalent to the specified velocity expressed in micrometers per second.
Definition velocity.h:145
Area operator-(Area a, Area b)
Definition area.h:175
Velocity UnknownVelocity()
Returns a velocity object representing an unknown velocity.
Definition velocity.h:123
Velocity VelocityInKph(float velocity)
Definition velocity.h:155
Area operator/(Area a, float b)
Definition area.h:191
Area operator*(Area a, float b)
Definition area.h:183
Velocity VelocityInKilometersPerSecond(float velocity)
Returns a velocity object equivalent to the specified velocity expressed in kilometers per second.
Definition velocity.h:127