Skip to content

Commit 171c0a6

Browse files
author
Brendan Whitfield
committed
documented pint units
1 parent edb55ba commit 171c0a6

2 files changed

Lines changed: 38 additions & 30 deletions

File tree

‎README.md‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ cmd = obd.commands.RPM # select an OBD command (sensor)
2525
response = connection.query(cmd) # send the command, and parse the response
2626

2727
print(response.value)
28-
print(response.unit)
2928
```
3029

3130
Documentation

‎docs/Responses.md‎

Lines changed: 38 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,10 @@ The `query()` function returns `OBDResponse` objects. These objects have the fol
33
| Property | Description |
44
|----------|------------------------------------------------------------------------|
55
| value | The decoded value from the car |
6-
| unit | The units of the decoded value |
7-
| command | The `OBDCommand` object that triggered this response |
6+
| command | The `OBDCommand` object that triggered this response |
87
| message | The internal `Message` object containing the raw response from the car |
98
| time | Timestamp of response (as given by [`time.time()`](https://docs.python.org/2/library/time.html#time.time)) |
109

11-
The `value` property typically contains numeric values, but can also hold complex structures (depending upon the command that was sent).
1210

1311

1412
---
@@ -27,36 +25,47 @@ if not r.is_null():
2725
---
2826

2927

30-
# Units
28+
# Values
3129

32-
Unit values can be found in the `Unit` class (enum).
30+
The `value` property typically contains a [Pint](http://pint.readthedocs.io/en/latest/) `Quantity` object, but can also hold complex structures (depending on the request). Pint quantities combine a value and unit into a single class, and are used to represent physical values (such as "4 seconds", and "88 mph"). This allows for consistency when doing math and unit conversions. Pint maintains a registry of units, which is exposed in python-OBD as `obd.Unit`.
31+
32+
Below are common operations that can be done with Pint units and quantities. For more information, check out the [Pint Documentation](http://pint.readthedocs.io/en/latest/).
3333

3434
```python
35-
from obd.utils import Unit
36-
```
35+
import obd
36+
37+
>>> response.value
38+
<Quantity(100, 'kph')>
39+
40+
# get the raw python datatype
41+
>>> response.value.magnitude
42+
100
43+
44+
# converts quantities to strings
45+
>>> str(response.value)
46+
'100 kph'
3747

38-
| Name | Value |
39-
|-------------|--------------------|
40-
| NONE | None |
41-
| RATIO | "Ratio" |
42-
| COUNT | "Count" |
43-
| PERCENT | "%" |
44-
| RPM | "RPM" |
45-
| VOLT | "Volt" |
46-
| F | "F" |
47-
| C | "C" |
48-
| SEC | "Second" |
49-
| MIN | "Minute" |
50-
| PA | "Pa" |
51-
| KPA | "kPa" |
52-
| PSI | "psi" |
53-
| KPH | "kph" |
54-
| MPH | "mph" |
55-
| DEGREES | "Degrees" |
56-
| GPS | "Grams per Second" |
57-
| MA | "mA" |
58-
| KM | "km" |
59-
| LPH | "Liters per Hour" |
48+
# convert strings to quantities
49+
>>> obd.Unit("100 kph")
50+
<Quantity(100, 'kph')>
51+
52+
# handles conversions nicely
53+
>>> response.value.to('mph')
54+
<Quantity(62.13711922373341, 'mph')>
55+
56+
# scaler math
57+
>>> response.value / 2
58+
<Quantity(50.0, 'kph')>
59+
60+
# non-scaler math requires you to specify units yourself
61+
>>> response.value + (20 * obd.Unit.kph)
62+
<Quantity(120, 'kph')>
63+
64+
# non-scaler math with different units
65+
# handles unit conversions transparently
66+
>>> response.value + (20 * obd.Unit.mph)
67+
<Quantity(132.18688, 'kph')>
68+
```
6069

6170
---
6271

0 commit comments

Comments
 (0)