forked from commaai/openpilot
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create message mocking tools (commaai#31249)
* add mocking tools * fix map renderer * use for power draw * fix those * whitespace * rename to services * fix the rate * remove
- Loading branch information
Showing
6 changed files
with
79 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
""" | ||
Utilities for generating mock messages for testing. | ||
example in common/tests/test_mock.py | ||
""" | ||
|
||
|
||
import functools | ||
import threading | ||
from typing import List, Union | ||
from cereal.messaging import PubMaster | ||
from cereal.services import SERVICE_LIST | ||
from openpilot.common.mock.generators import generate_liveLocationKalman | ||
from openpilot.common.realtime import Ratekeeper | ||
|
||
|
||
MOCK_GENERATOR = { | ||
"liveLocationKalman": generate_liveLocationKalman | ||
} | ||
|
||
|
||
def generate_messages_loop(services: List[str], done: threading.Event): | ||
pm = PubMaster(services) | ||
rk = Ratekeeper(100) | ||
i = 0 | ||
while not done.is_set(): | ||
for s in services: | ||
should_send = i % (100/SERVICE_LIST[s].frequency) == 0 | ||
if should_send: | ||
message = MOCK_GENERATOR[s]() | ||
pm.send(s, message) | ||
i += 1 | ||
rk.keep_time() | ||
|
||
|
||
def mock_messages(services: Union[List[str], str]): | ||
if isinstance(services, str): | ||
services = [services] | ||
|
||
def decorator(func): | ||
@functools.wraps(func) | ||
def wrapper(*args, **kwargs): | ||
done = threading.Event() | ||
t = threading.Thread(target=generate_messages_loop, args=(services, done)) | ||
t.start() | ||
try: | ||
return func(*args, **kwargs) | ||
finally: | ||
done.set() | ||
t.join() | ||
return wrapper | ||
return decorator |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
from cereal import messaging | ||
|
||
|
||
LOCATION1 = (32.7174, -117.16277) | ||
LOCATION2 = (32.7558, -117.2037) | ||
|
||
LLK_DECIMATION = 10 | ||
RENDER_FRAMES = 15 | ||
DEFAULT_ITERATIONS = RENDER_FRAMES * LLK_DECIMATION | ||
|
||
|
||
def generate_liveLocationKalman(location=LOCATION1): | ||
msg = messaging.new_message('liveLocationKalman') | ||
msg.liveLocationKalman.positionGeodetic = {'value': [*location, 0], 'std': [0., 0., 0.], 'valid': True} | ||
msg.liveLocationKalman.positionECEF = {'value': [0., 0., 0.], 'std': [0., 0., 0.], 'valid': True} | ||
msg.liveLocationKalman.calibratedOrientationNED = {'value': [0., 0., 0.], 'std': [0., 0., 0.], 'valid': True} | ||
msg.liveLocationKalman.velocityCalibrated = {'value': [0., 0., 0.], 'std': [0., 0., 0.], 'valid': True} | ||
msg.liveLocationKalman.status = 'valid' | ||
msg.liveLocationKalman.gpsOK = True | ||
return msg |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters