Skip to content

Commit

Permalink
Demo mode is activated even with GPS module. add COURSE_DIVIDE_FACTOR…
Browse files Browse the repository at this point in the history
… to dummy.py
  • Loading branch information
hishizuka committed Dec 18, 2023
1 parent 5d478b5 commit f6f7371
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 70 deletions.
2 changes: 1 addition & 1 deletion modules/sensor/gps/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@
else:
SensorGPS = Dummy_GPS

app_logger.info(f"{SensorGPS.__name__} chosen")
app_logger.info(f"GPS ({SensorGPS.__name__})")
7 changes: 4 additions & 3 deletions modules/sensor/gps/adafruit_uart.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@
except:
pass

if _SENSOR_GPS_ADAFRUIT_UART:
app_logger.info("GPS_ADAFRUIT_UART")


class Adafruit_GPS(AbstractSensorGPS):
NULL_VALUE = None
Expand All @@ -41,6 +38,10 @@ async def quit(self):

# experimental code
async def update(self):
if self.config.G_DUMMY_OUTPUT:
await self.output_dummy()
return

g = self.adafruit_gps

try:
Expand Down
4 changes: 4 additions & 0 deletions modules/sensor/gps/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -311,3 +311,7 @@ def get_utc_time(self, gps_time):

if not self.is_time_modified:
self.is_time_modified = set_time(gps_time)

async def output_dummy(self):
from .dummy import Dummy_GPS
await Dummy_GPS(self.config, self.values).update()
119 changes: 60 additions & 59 deletions modules/sensor/gps/dummy.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

class Dummy_GPS(AbstractSensorGPS):
LOG_SPEED = 5
COURSE_DIVIDE_FACTOR = 500
COURSE_RAND_FACTOR = 0.5 # np.random.randint(0,10)/10

@property
Expand Down Expand Up @@ -54,70 +55,70 @@ def set_position_from_course(self, course, idx):
self.values["track_str"] = get_track_str(self.values["track"])

async def update(self):
if self.config.G_DUMMY_OUTPUT:
course_i = pre_course_i = 0

try:
while True:
await self.sleep()
if not self.config.G_DUMMY_OUTPUT:
return
course_i = pre_course_i = 0

try:
while True:
await self.sleep()

if self.config.logger is None or (
not self.config.logger.course.is_set
and self.config.logger.position_log.shape[0] == 0
):
continue

# unit: m/s
# self.values['speed'] = random.randint(1,6) * 3.6
self.values["speed"] = np.random.randint(13, 83) / 10 # 5 - 30km/h

self.values["pre_lat"] = self.values["lat"]
self.values["pre_lon"] = self.values["lon"]
self.values["pre_track"] = self.values["track"]

# generate dummy position from log
if self.config.logger.position_log.shape[0] > 0:
self.set_position_from_log(
self.config.logger.position_log[course_i]
)

if self.config.logger is None or (
not self.config.logger.course.is_set
and self.config.logger.position_log.shape[0] == 0
):
if course_i == pre_course_i:
course_i += 1 * self.LOG_SPEED
continue

# self.values['track_str'] = "-"
# unit: m/s
# self.values['speed'] = random.randint(1,6) * 3.6
self.values["speed"] = np.random.randint(13, 83) / 10 # 5 - 30km/h

self.values["pre_lat"] = self.values["lat"]
self.values["pre_lon"] = self.values["lon"]
self.values["pre_track"] = self.values["track"]

# generate dummy position from log
if self.config.logger.position_log.shape[0] > 0:
self.set_position_from_log(
self.config.logger.position_log[course_i]
)

if course_i == pre_course_i:
course_i += 1 * self.LOG_SPEED
continue
else:
pre_course_i = course_i
course_i += 1 * self.LOG_SPEED
if course_i >= len(self.config.logger.position_log):
course_i = pre_course_i = 0
continue

# from course
else:
# TODO No need to do this for each loop, unless the course can be changed in between ?
course_n = len(self.config.logger.course.latitude)
pre_course_i = course_i
course_i += 1 * self.LOG_SPEED
if course_i >= len(self.config.logger.position_log):
course_i = pre_course_i = 0
continue

self.set_position_from_course(
self.config.logger.course, course_i
)
# from course
else:
# TODO No need to do this for each loop, unless the course can be changed in between ?
course_n = len(self.config.logger.course.latitude)

pre_course_i = course_i
course_i += int(course_n / 200) + 1
if course_i >= course_n:
pre_course_i = 0
course_i = course_i % course_n

self.config.logger.course.get_index(
self.values["lat"],
self.values["lon"],
self.values["track"],
self.config.G_GPS_SEARCH_RANGE,
self.config.G_GPS_ON_ROUTE_CUTOFF,
self.azimuth_cutoff,
self.set_position_from_course(
self.config.logger.course, course_i
)

self.values["timestamp"] = datetime.now()
self.get_sleep_time(self.config.G_GPS_INTERVAL)
pre_course_i = course_i
course_i += int(course_n / self.COURSE_DIVIDE_FACTOR) + 1
if course_i >= course_n:
pre_course_i = 0
course_i = course_i % course_n

self.config.logger.course.get_index(
self.values["lat"],
self.values["lon"],
self.values["track"],
self.config.G_GPS_SEARCH_RANGE,
self.config.G_GPS_ON_ROUTE_CUTOFF,
self.azimuth_cutoff,
)

self.values["timestamp"] = datetime.now()
self.get_sleep_time(self.config.G_GPS_INTERVAL)

except asyncio.CancelledError:
pass
except asyncio.CancelledError:
pass
7 changes: 4 additions & 3 deletions modules/sensor/gps/gpsd.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,6 @@
except:
pass

if _SENSOR_GPS_GPSD:
app_logger.info("GPS")


class GPSD(AbstractSensorGPS):
gps_thread = None
Expand All @@ -51,6 +48,10 @@ async def quit(self):
self.gps_thread.stop()

async def update(self):
if self.config.G_DUMMY_OUTPUT:
await self.output_dummy()
return

g = self.gps_thread.data_stream
try:
while True:
Expand Down
8 changes: 4 additions & 4 deletions modules/sensor/gps/i2c.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,6 @@
app_logger.exception("Failed to init GPS_I2C")


if _SENSOR_GPS_I2C:
app_logger.info("GPS_I2C")


class GPS_I2C(AbstractSensorGPS):
NULL_VALUE = None

Expand All @@ -32,6 +28,10 @@ def sensor_init(self):
super().sensor_init()

async def update(self):
if self.config.G_DUMMY_OUTPUT:
await self.output_dummy()
return

g = self.i2c_gps

try:
Expand Down

0 comments on commit f6f7371

Please sign in to comment.