-
Notifications
You must be signed in to change notification settings - Fork 4
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
[WIP] Protocol refactor #46
base: cart-pole-3
Are you sure you want to change the base?
Changes from all commits
66bb6cc
d538140
e4dcbde
8789dad
a9908eb
2f63a06
a62a9cd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,30 +12,46 @@ | |
|
||
def parse_args(): | ||
common = argparse.ArgumentParser( | ||
prog='cartpole', | ||
description='cartpole control experiments' | ||
prog='cartpole', description='cartpole control experiments' | ||
) | ||
|
||
subparsers = common.add_subparsers(title='commands', dest='command', required=True, help='command help') | ||
subparsers = common.add_subparsers( | ||
title='commands', dest='command', required=True, help='command help' | ||
) | ||
|
||
# common arguments | ||
|
||
common.add_argument('-S', '--simulation', action='store_true', help='simulation mode') | ||
common.add_argument( | ||
'-S', '--simulation', action='store_true', help='simulation mode' | ||
) | ||
common.add_argument('-c', '--config', type=str, help='cartpole yaml config file') | ||
common.add_argument('-m', '--mcap', type=str, default='', help='mcap log file') | ||
common.add_argument('-a', '--advance', type=float, default=0.01, help='advance simulation time (seconds)') | ||
common.add_argument( | ||
'-a', | ||
'--advance', | ||
type=float, | ||
default=0.01, | ||
help='advance simulation time (seconds)', | ||
) | ||
|
||
# eval arguments | ||
eval = subparsers.add_parser('eval', help='system identification') | ||
|
||
eval.add_argument('-d', '--duration', type=float, default=10.0, help='experiment duration (seconds)') | ||
eval.add_argument( | ||
'-d', | ||
'--duration', | ||
type=float, | ||
default=10.0, | ||
help='experiment duration (seconds)', | ||
) | ||
eval.add_argument('-O', '--output', type=str, help='output yaml config file') | ||
|
||
return common.parse_args() | ||
|
||
|
||
def evaluate(device: CartPoleBase, config: Config, args: argparse.Namespace) -> None: | ||
log.info('parameters evaluation') | ||
random.seed(0) | ||
|
||
position_margin = 0.01 | ||
position_tolerance = 0.005 | ||
|
@@ -48,8 +64,10 @@ def evaluate(device: CartPoleBase, config: Config, args: argparse.Namespace) -> | |
|
||
log.info(f'run calibration session for {duration:.2f} seconds') | ||
device.reset() | ||
time.sleep(5) | ||
|
||
start = device.get_state() | ||
print(start) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Это потом подчистите? |
||
state = start | ||
|
||
target = Target(position=0, velocity=0, acceleration=0) | ||
|
@@ -59,25 +77,29 @@ def evaluate(device: CartPoleBase, config: Config, args: argparse.Namespace) -> | |
state = device.get_state() | ||
|
||
if abs(target.position - state.cart_position) < position_tolerance: | ||
position = random.uniform(position_max/2, position_max) | ||
position = random.uniform(position_max / 2, position_max) | ||
target.position = position if target.position < 0 else -position | ||
target.velocity = random.uniform(velocity_max/2, velocity_max) | ||
target.acceleration = random.uniform(acceleration_max/2, acceleration_max) | ||
target.velocity = random.uniform(velocity_max / 2, velocity_max) | ||
target.acceleration = random.uniform(acceleration_max / 2, acceleration_max) | ||
|
||
log.info(f'target {target}') | ||
device.set_target(target) | ||
state = device.set_target(target) | ||
|
||
log.publish('/cartpole/state', state, state.stamp) | ||
log.publish('/cartpole/target', target, state.stamp) | ||
log.publish('/cartpole/info', device.get_info(), state.stamp) | ||
Comment on lines
-70
to
-72
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. А почему решили убрать |
||
log.publish('/cartpole/state', state) | ||
log.publish('/cartpole/target', target) | ||
# log.publish('/cartpole/info', device.get_info(), state.stamp) | ||
|
||
states.append(state) | ||
device.advance(advance) | ||
|
||
if args.simulation: | ||
time.sleep(advance) # simulate real time | ||
time.sleep(advance) # simulate real time | ||
|
||
if state.error: | ||
print('ERR', state.error) | ||
|
||
log.info(f'find parameters') | ||
print(len(states)) | ||
parameters = find_parameters(states, config.parameters.gravity) | ||
|
||
log.info(f'parameters: {parameters}') | ||
|
@@ -106,24 +128,26 @@ def main(): | |
|
||
if args.simulation: | ||
log.info('simulation mode') | ||
device = Simulator(integration_step=min(args.advance/20, 0.001)) | ||
device = Simulator(integration_step=min(args.advance / 20, 0.001)) | ||
else: | ||
raise NotImplementedError() | ||
|
||
from cartpole.device import CartPoleDevice | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. МБ сверху лучше? |
||
|
||
device = CartPoleDevice(hard_reset=True) | ||
|
||
if args.config: | ||
log.debug(f'config file: {args.config}') | ||
config = Config.from_yaml_file(args.config) | ||
else: | ||
log.warning('no config file specified, using defaults') | ||
config = Config() | ||
|
||
|
||
device.set_config(config) | ||
|
||
if args.command == 'eval': | ||
evaluate(device, config, args) | ||
else: | ||
raise NotImplementedError() | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
from cartpole.device._device import * | ||
from cartpole.device.device import CartPoleDevice |
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Это чтоб палочка успокоилась?