Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add AiCatapult - an improved version of AiLauncher which also controls steering angle #934

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 72 additions & 0 deletions donkeycar/parts/launch.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import time
from datetime import datetime
from logging import getLogger

class AiLaunch():
'''
Expand Down Expand Up @@ -46,3 +48,73 @@ def run(self, mode, ai_throttle):

return new_throttle


class AiCatapult():
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This class is effectively just a copy of the above class with a very small change to return 0 angle in launch mode. We should probably drive this with a single bool switch in the AiLaunch class above. Also the () should go. Then we should add a config parameter in cfg_complete.py that can trigger the 0 angle behaviour and pass this in complete.py

'''
Comparing to AiLaunch this particular class also controls the angle.

To be able to use it you need to change <mycar>/manage.py:

---- old one ----
from donkeycar.parts.launch import AiLauncher

aiLauncher = AiLauncher(cfg.AI_LAUNCH_DURATION, cfg.AI_LAUNCH_THROTTLE, cfg.AI_LAUNCH_KEEP_ENABLED)
V.add(aiLauncher,
inputs=['user/mode', 'throttle'],
outputs=['throttle'])
---- new one ----
from donkeycar.parts.launch import AiCatapult

aiLauncher = AiCatapult(cfg.AI_LAUNCH_DURATION, cfg.AI_LAUNCH_THROTTLE, cfg.AI_LAUNCH_KEEP_ENABLED)
V.add(aiLauncher,
inputs=['user/mode', 'throttle', 'angle'],
outputs=['throttle', 'angle'])
---- end ----

'''

def __init__(self, launch_duration=1.0, launch_throttle=1.0, keep_enabled=False):
self.active = False
self.enabled = False
self.timer_start = None
self.timer_duration = launch_duration
self.launch_throttle = launch_throttle
self.prev_mode = None
self.trigger_on_switch = keep_enabled
self.logger = getLogger()

def enable_ai_launch(self):
self.enabled = True
self.logger.info(f'AiLauncher is enabled at {datetime.now().time()}')

def run(self, mode, ai_throttle, ai_angle):
new_throttle = ai_throttle
new_angle = ai_angle

if mode != self.prev_mode:
self.prev_mode = mode
if mode == "local" and self.trigger_on_switch:
self.enabled = True
self.logger.info(f'AiLauncher is activated by mode and trigger at {datetime.now().time()}')

if mode == "local" and self.enabled:
if not self.active:
self.active = True
self.timer_start = time.time()
self.logger.info(f'AiLauncher is activated now at {datetime.now().time()}')
else:
duration = time.time() - self.timer_start
if duration > self.timer_duration:
self.active = False
self.enabled = False
self.logger.info(f'AiLauncher is deactivated by duration at {datetime.now().time()}')
else:
if self.active:
self.logger.info(f'AiLauncher is deactivated by mode now at {datetime.now().time()}')
self.active = False

if self.active:
new_throttle = self.launch_throttle
new_angle = 0.0

return new_throttle, new_angle