Skip to content

Commit

Permalink
added updating of ev1 and ev2 locations
Browse files Browse the repository at this point in the history
  • Loading branch information
adenjonah committed May 20, 2024
1 parent b03da75 commit abc2030
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 45 deletions.
1 change: 1 addition & 0 deletions server/json_databases/geojson/CURRENT_LOCATION.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"type": "FeatureCollection", "features": [{"type": "Feature", "properties": {"Name": "Rover"}, "geometry": {"coordinates": [-95.08156321481367, 29.56500391850437], "type": "Point"}, "id": 100}, {"type": "Feature", "properties": {"Name": "Eva1"}, "geometry": {"coordinates": [-95.08173200489527, 29.56480501479513], "type": "Point"}, "id": 101}, {"type": "Feature", "properties": {"Name": "Eva2"}, "geometry": {"coordinates": [-95.08174938354279, 29.564937170822553], "type": "Point"}, "id": 102}]}
2 changes: 1 addition & 1 deletion server/json_databases/geojson/ROVER_LOCATION.geojson
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"type": "FeatureCollection", "features": [{"type": "Feature", "properties": {"Name": "Rover"}, "geometry": {"coordinates": [-95.08188632485191, 29.56471360941181], "type": "Point"}, "id": 100}]}
{"type": "FeatureCollection", "features": [{"type": "Feature", "properties": {"Name": "Rover"}, "geometry": {"coordinates": [-95.08184061347882, 29.564838153239624], "type": "Point"}, "id": 100}]}
2 changes: 1 addition & 1 deletion server/json_databases/geojson/nav_path.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"type": "FeatureCollection", "features": [{"type": "Feature", "geometry": {"type": "LineString", "coordinates": [[-95.08179254063919, 29.564874847782445], [-95.081722737826, 29.56480127017403], [-95.08161789732138, 29.56480262190923], [-95.08151289106338, 29.564804258092494], [-95.08140883901132, 29.564806812141647], [-95.08134254159708, 29.56484639149208], [-95.0813135824489, 29.564887722303105]]}, "properties": {"name": "Rover Path"}}]}
{"type": "FeatureCollection", "features": [{"type": "Feature", "geometry": {"type": "LineString", "coordinates": [[-95.08160677610833, 29.564802807347508], [-95.08150458035765, 29.564804390394595], [-95.08140354413766, 29.564807110489348], [-95.08133966110125, 29.564846265573188], [-95.08131348977675, 29.564888469011976]]}, "properties": {"name": "Rover Path"}}]}
96 changes: 53 additions & 43 deletions server/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
GEOLOGICAL_SITES_FILE = './server/json_databases/geojson/geological_sites.json'
USER_PINS_FILE = './server/json_databases/geojson/user_pins.json'
NAV_PATH = './server/json_databases/geojson/nav_path.json'
ROVER_LOCATION_FILE = './server/json_databases/geojson/ROVER_LOCATION.geojson'
CURRENT_LOCATIONS_FILE = './server/json_databases/geojson/CURRENT_LOCATION.json'

# Keys and IP Addresses
CONFIG_FILE = './server/json_databases/config_keys.json'
Expand Down Expand Up @@ -59,8 +59,6 @@
allow_methods=["*"],
allow_headers=["*"],
)

# Get JSON
async def fetch_json(url: str):
async with httpx.AsyncClient() as client:
response = await client.get(url)
Expand All @@ -70,11 +68,10 @@ async def fetch_json(url: str):

@app.on_event("startup")
async def startup_event():
initialize_database_files()
task = asyncio.create_task(periodic_fetch_and_store())
await asyncio.sleep(1)
scheduler = AsyncIOScheduler()
scheduler.add_job(fetch_and_update_rover_position, 'interval', seconds=3)
scheduler.add_job(fetch_and_update_positions, 'interval', seconds=3)
scheduler.start()

async def periodic_fetch_and_store():
Expand All @@ -100,54 +97,67 @@ async def periodic_fetch_and_store():
pass
await asyncio.sleep(1)

# Function to convert UTM to latitude and longitude
def utm_to_latlon(easting, northing, zone_number=15, zone_letter='R'):
lat, lon = utm.to_latlon(easting, northing, zone_number, zone_letter)
return lat, lon
return utm.to_latlon(easting, northing, zone_number, zone_letter)

# Function to fetch the rover's position and update the GeoJSON file
def fetch_and_update_rover_position():
def fetch_and_update_positions():
try:
# URLs for the rover and IMU data
rover_url = f"http://{tss_ip}/json_data/ROVER.json"
response = requests.get(rover_url)
data = response.json()
imu_url = f"http://{tss_ip}/json_data/IMU.json"

# Fetching data
rover_response = requests.get(rover_url)
rover_data = rover_response.json()

posx = data["rover"]["posx"]
posy = data["rover"]["posy"]

# Spoof data if both posx and posy are 0
if posx == 0 and posy == 0:
latitude = 29.564802807347508
longitude = -95.08160677610833

utm_coords = utm.from_latlon(latitude, longitude)
posx = utm_coords[0] + random.uniform(-30, 30)
posy = utm_coords[1] + random.uniform(-30, 30)
print("Spoofed position data for rover")

lat, lon = utm_to_latlon(posx, posy, utm_coords[2], utm_coords[3])

feature = {
"type": "Feature",
"properties": {
"Name": "Rover"
},
"geometry": {
"coordinates": [lon, lat],
"type": "Point"
},
"id": 100
imu_response = requests.get(imu_url)
imu_data = imu_response.json()

# Getting positions for rover, eva1, and eva2
positions = {
"rover": rover_data["rover"],
"eva1": imu_data["imu"]["eva1"],
"eva2": imu_data["imu"]["eva2"]
}

feature_collection = FeatureCollection([feature])

with open(ROVER_LOCATION_FILE, "w") as f:
features = []

# Spoof and convert positions if they are zero
for idx, (name, pos) in enumerate(positions.items()):
posx, posy = pos["posx"], pos["posy"]

if posx == 0 and posy == 0:
latitude = 29.564802807347508
longitude = -95.08160677610833
utm_coords = utm.from_latlon(latitude, longitude)
# posx = utm_coords[0] + random.uniform(-30, 30)
# posy = utm_coords[1] + random.uniform(-30, 30)
# print(f"Spoofed position data for {name}")

lat, lon = utm_to_latlon(posx, posy, utm_coords[2], utm_coords[3])

feature = {
"type": "Feature",
"properties": {
"Name": name.capitalize()
},
"geometry": {
"coordinates": [lon, lat],
"type": "Point"
},
"id": 100 + idx
}
features.append(feature)

feature_collection = FeatureCollection(features)

with open(CURRENT_LOCATIONS_FILE, "w") as f:
dump(feature_collection, f)

print(f"Updated ROVER_LOCATION.geojson with new position: ({lon}, {lat})")
print(f"Updated CURRENT_LOCATIONS.geojson with new positions for rover, eva1, and eva2")

except Exception as e:
print(f"Error fetching or updating rover position: {e}")
print(f"Error fetching or updating positions: {e}")


# Save new IP or Key
Expand Down Expand Up @@ -229,7 +239,7 @@ class Marker(BaseModel):
async def get_geojson():
geojson_data = {"type": "FeatureCollection", "features": []}

geojson_files = [BOUNDARY_LINES_FILE, DEFAULT_PINS_FILE, GEOLOGICAL_SITES_FILE, USER_PINS_FILE, NAV_PATH, ROVER_LOCATION_FILE]
geojson_files = [BOUNDARY_LINES_FILE, DEFAULT_PINS_FILE, GEOLOGICAL_SITES_FILE, USER_PINS_FILE, NAV_PATH, CURRENT_LOCATIONS_FILE]

for file_path in geojson_files:
if os.path.exists(file_path):
Expand Down

0 comments on commit abc2030

Please sign in to comment.