Skip to content

Instantly share code, notes, and snippets.

@scottstanfield
Created October 12, 2020 19:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save scottstanfield/ce41486b6056d01838a8003ec2ac79bc to your computer and use it in GitHub Desktop.
Save scottstanfield/ce41486b6056d01838a8003ec2ac79bc to your computer and use it in GitHub Desktop.
mb1006.v6 working brain
###
# MSFT Bonsai
# Copyright 2020 Microsoft
# This code is licensed under MIT license (see LICENSE for details)
# Moab Tutorial 1
# This introductory sample demonstrates how to teach a policy for
# controlling a ball on the plate of a "Moab" hardware device.
# To understand this Inkling better, please follow our tutorial walkthrough:
# https://aka.ms/moab/tutorial1
###
inkling "2.0"
using Math
using Goal
# Distances measured in meters
const RadiusOfPlate = 0.1125 # m
# Velocities measured in meters per sec.
const MaxVelocity = 1.0
# Threshold for ball placement
const CloseEnough = 0.01 # [meters] default 0.02 m (2 cm) episilon close enough to center
const PercentOfPlate = 0.7 # [percentage] was 0.5 to drop ball within 50% of inner plate
const vel_range = 0.03 # [m/sec] default is 0.02 or 2 cm/sec
const OffPlate = 0.9 # [percentage] default is 80%; past that is "off the plate"
const PlateVelocity = 0.02 # [m/sec] starting speed of plate
# State received from the simulator after each iteration
type ObservableState {
# Ball X,Y position
ball_x: number<-RadiusOfPlate .. RadiusOfPlate>,
ball_y: number<-RadiusOfPlate .. RadiusOfPlate>,
# Ball X,Y velocity
ball_vel_x: number<-MaxVelocity .. MaxVelocity>,
ball_vel_y: number<-MaxVelocity .. MaxVelocity>,
}
# Action provided as output by policy and sent as
# input to the simulator
type SimAction {
# Range -1 to 1 is a scaled value that represents
# the full plate rotation range supported by the hardware.
input_pitch: number<-1 .. 1>, # rotate about x-axis
input_roll: number<-1 .. 1>, # rotate about y-axis
}
# Per-episode configuration that can be sent to the simulator.
# All iterations within an episode will use the same configuration.
type SimConfig {
# Model initial ball conditions
initial_x: number<-RadiusOfPlate .. RadiusOfPlate>, # in (m)
initial_y: number<-RadiusOfPlate .. RadiusOfPlate>,
# Model initial ball velocity conditions
initial_vel_x: number<-MaxVelocity .. MaxVelocity>, # in (m/s)
initial_vel_y: number<-MaxVelocity .. MaxVelocity>,
# Range -1 to 1 is a scaled value that represents
# the full plate rotation range supported by the hardware.
initial_pitch: number<-1 .. 1>,
initial_roll: number<-1 .. 1>,
}
# Define a concept graph with a single concept
graph (input: ObservableState) {
concept MoveToCenter(input): SimAction {
curriculum {
# The source of training for this concept is a simulator that
# - can be configured for each episode using fields defined in SimConfig,
# - accepts per-iteration actions defined in SimAction, and
# - outputs states with the fields defined in SimState.
source simulator MoabSim(Action: SimAction, Config: SimConfig): ObservableState {
# Automatically launch the simulator with this
# registered package name.
package "Moab"
}
training {
# Limit episodes to 250 iterations instead of the default 1000.
EpisodeIterationLimit: 250
}
# The objective of training is expressed as a goal with two
# subgoals: don't let the ball fall off the plate, and drive
# the ball to the center of the plate.
goal (State: ObservableState) {
avoid `Fall Off Plate`: Math.Hypot(State.ball_x, State.ball_y) in Goal.RangeAbove(RadiusOfPlate * OffPlate)
drive `Center Of Plate`: [State.ball_x, State.ball_y] in Goal.Sphere([0, 0], CloseEnough)
}
lesson `Randomize Start` {
# Specify the configuration parameters that should be varied
# from one episode to the next during this lesson.
scenario {
initial_x: number<-RadiusOfPlate * PercentOfPlate .. RadiusOfPlate * PercentOfPlate>,
initial_y: number<-RadiusOfPlate * PercentOfPlate .. RadiusOfPlate * PercentOfPlate>,
initial_vel_x: number<-MaxVelocity * vel_range .. MaxVelocity * vel_range>,
initial_vel_y: number<-MaxVelocity * vel_range .. MaxVelocity * vel_range>,
initial_pitch: number<-PlateVelocity .. PlateVelocity>,
initial_roll: number<-PlateVelocity .. PlateVelocity>,
}
}
}
}
}
# Special string to hook up the simulator visualizer
# in the web interface.
const SimulatorVisualizer = "/moabviz/"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment