Compare commits

...

3 Commits

5 changed files with 54 additions and 24 deletions

View File

@ -29,6 +29,7 @@ class Api:
web.get('/api/env', self.env),
web.get('/api/status', self.status),
web.get('/api/costmap_image', self.costmap_image),
web.get("/api/position", self.position),
web.static("/assets", ui_static_directory),
# we're not actually using key anywhere, but doing this allows react router
# to work correctly.
@ -91,3 +92,15 @@ class Api:
raise web.HTTPUnsupportedMediaType()
resp = web.Response(body=image_data, content_type='image/{}'.format(image_format))
return resp
async def position(self, request):
resp = {}
(lat, lon) = self._facts.get_lat_long()
if not lat or not lon:
resp["status"] = False
return web.json_response(resp)
resp["status"] = True
resp["latitude"] = lat
resp["longitude"] = lon
return web.json_response(resp)

View File

@ -15,25 +15,26 @@ import os
import yaml
from ament_index_python import get_package_share_directory
from am_i_up.Ros import Ros
class Facts:
def __init__(self):
self._ros = Ros()
self._start_time = time.monotonic()
self._status_string = None
self._costmap_image_format = None
self._costmap_image_data = None
def set_costmap_image(self, image_format, image_data):
self._costmap_image_format = image_format
self._costmap_image_data = image_data
async def run(self):
await self._ros.run()
def get_lat_long(self):
return self._ros.get_lat_long()
def get_costmap_image(self):
return (self._costmap_image_format, self._costmap_image_data)
def set_status_string(self, status):
self._status_string = status
return self._ros.get_costmap_image()
def get_status(self):
return self._status_string
return self._ros.get_status_string()
def get_uptime(self):
return time.monotonic() - self._start_time

View File

@ -11,28 +11,45 @@
import rclpy
from std_msgs.msg import String
from sensor_msgs.msg import CompressedImage
from sensor_msgs.msg import NavSatFix
import asyncio
class Ros:
def __init__(self, facts):
def __init__(self):
rclpy.init()
self._facts = facts
self._node = rclpy.create_node('am_i_up')
self._node.create_subscription(String, "status", self.status_sub, 1)
self._node.create_subscription(CompressedImage, "image/compressed", self.image_sub, 1)
self._status_sub = self._node.create_subscription(String, "status", self._status_callback, 1)
self._image_sub = self._node.create_subscription(CompressedImage, "image/compressed", self._image_callback, 1)
self._navsat_sub = self._node.create_subscription(NavSatFix, "navsatfix", self._navsat_callback, 1)
def status_sub(self, msg):
self._facts.set_status_string(msg.data)
self._status_string = None
self._lat_long = None
self._costmap_image = None
def image_sub(self, msg):
if msg.format != "jpg" and msg.format != "png":
# I don't know what to do with this image....
return
self._facts.set_costmap_image(msg.format, bytes(msg.data))
def get_status_string(self):
return self._status_string
def get_lat_long(self):
return self._lat_long
def get_costmap_image(self):
return self._costmap_image
async def run(self):
while rclpy.ok():
rclpy.spin_once(self._node, timeout_sec=0)
await asyncio.sleep(1e-4)
def _status_callback(self, msg):
self._status_string = msg.data
def _navsat_callback(self, msg):
self._lat_long = (msg.latitude, msg.longitude)
def _image_callback(self, msg):
if msg.format != "jpg" and msg.format != "png":
# I don't know what to do with this image....
return
self._costmap_image = (msg.format, bytes(msg.data))

View File

@ -10,14 +10,12 @@
#
from am_i_up.Api import Api
from am_i_up.Facts import Facts
from am_i_up.Ros import Ros
import asyncio
def main():
facts = Facts()
ros = Ros(facts)
api = Api(facts)
future = asyncio.gather(api.run(), ros.run())
future = asyncio.gather(api.run(), facts.run())
asyncio.get_event_loop().run_until_complete(future)

View File

@ -1,4 +1,5 @@
<launch>
<node pkg="am_i_up" exec="server" name="am_i_up"/>
<node pkg="data_simulator" exec="image_publisher" name="image_publisher" />
<node pkg="data_simulator" exec="position_publisher" name="position_publisher" />
</launch>